MCPcopy Create free account
hub / github.com/KhronosGroup/KTX-Software / ktxTexture2_constructCopy

Function ktxTexture2_constructCopy

lib/texture2.c:535–621  ·  view source on GitHub ↗

* @memberof ktxTexture2 @private * @~English * @brief Construct a ktxTexture by copying a source ktxTexture. * * @param[in] This pointer to a ktxTexture2-sized block of memory to * initialize. * @param[in] orig pointer to the source texture to copy. * * @return KTX_SUCCESS on success, other KTX_* enum values on error. * * @exception KTX_OUT_OF_MEMORY Not enough memor

Source from the content-addressed store, hash-verified

533 * @exception KTX_OUT_OF_MEMORY Not enough memory for the texture data.
534 */
535KTX_error_code
536ktxTexture2_constructCopy(ktxTexture2* This, ktxTexture2* orig)
537{
538 KTX_error_code result;
539
540 memcpy(This, orig, sizeof(ktxTexture2));
541 // Zero all the pointers to make error handling easier
542 This->_protected = NULL;
543 This->_private = NULL;
544 This->pDfd = NULL;
545 This->kvData = NULL;
546 This->kvDataHead = NULL;
547 This->pData = NULL;
548
549 This->_protected =
550 (ktxTexture_protected*)malloc(sizeof(ktxTexture_protected));
551 if (!This->_protected)
552 return KTX_OUT_OF_MEMORY;
553 // Must come before memcpy of _protected so as to close an active stream.
554 if (!orig->pData && ktxTexture_isActiveStream((ktxTexture*)orig))
555 ktxTexture2_LoadImageData(orig, NULL, 0);
556 memcpy(This->_protected, orig->_protected, sizeof(ktxTexture_protected));
557
558 ktx_size_t privateSize = sizeof(ktxTexture2_private)
559 + sizeof(ktxLevelIndexEntry) * (orig->numLevels - 1);
560 This->_private = (ktxTexture2_private*)malloc(privateSize);
561 if (This->_private == NULL) {
562 result = KTX_OUT_OF_MEMORY;
563 goto cleanup;
564 }
565 memcpy(This->_private, orig->_private, privateSize);
566 if (orig->_private->_sgdByteLength > 0) {
567 This->_private->_supercompressionGlobalData
568 = (ktx_uint8_t*)malloc(orig->_private->_sgdByteLength);
569 if (!This->_private->_supercompressionGlobalData) {
570 result = KTX_OUT_OF_MEMORY;
571 goto cleanup;
572 }
573 memcpy(This->_private->_supercompressionGlobalData,
574 orig->_private->_supercompressionGlobalData,
575 orig->_private->_sgdByteLength);
576 }
577
578 This->pDfd = (ktx_uint32_t*)malloc(*orig->pDfd);
579 if (!This->pDfd) {
580 result = KTX_OUT_OF_MEMORY;
581 goto cleanup;
582 }
583 memcpy(This->pDfd, orig->pDfd, *orig->pDfd);
584
585 if (orig->kvDataHead) {
586 ktxHashList_ConstructCopy(&This->kvDataHead, orig->kvDataHead);
587 } else if (orig->kvData) {
588 This->kvData = (ktx_uint8_t*)malloc(orig->kvDataLen);
589 if (!This->kvData) {
590 result = KTX_OUT_OF_MEMORY;
591 goto cleanup;
592 }

Callers 2

ktxTexture2_CreateCopyFunction · 0.85

Calls 4

ktxHashList_DestructFunction · 0.85

Tested by

no test coverage detected