OSStatus VisualTextureContainer::initWithURL(VisualString& anURL) { this->releaseTextureData(); if (this->pixelBuffer != NULL) { free(this->pixelBuffer); this->pixelBuffer = NULL; } OSStatus osStatus = noErr; CFURLRef imageURL = CFURLCreateWithString(kCFAllocatorDefault, anURL.getCharactersPointer(), NULL); CGImageSourceRef imageSource = CGImageSourceCreateWithURL(imageURL, NULL); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL); this->imageWidth = CGImageGetWidth(imageRef); this->imageHeight = CGImageGetHeight(imageRef); VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); if (this->useRectExtension == false) { this->textureWidth = theVisualGraphics->power2Ceiling(this->imageWidth); this->textureHeight = theVisualGraphics->power2Ceiling(this->imageHeight); } else { this->textureWidth = this->imageWidth; this->textureHeight = this->imageHeight; } CGContextRef context = theVisualGraphics->createBitmapContext(this->textureWidth, this->textureHeight); CGContextTranslateCTM(context, 0.0, (float)this->textureHeight + (float)(this->textureHeight - this->imageHeight)); CGContextScaleCTM(context, 1.0, -1.0); CGRect rect = CGRectMake(0, (this->textureHeight - this->imageHeight), this->imageWidth, this->imageHeight); CGContextDrawImage(context, rect, imageRef); this->pixelBuffer = static_cast<UInt32*>(CGBitmapContextGetData(context)); this->textureName = theVisualGraphics->getNextFreeTextureName(); this->textureIsSet = true; VisualTextureContainer::textureRefCountMap[this->textureName] = 1; theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureWidth, this->textureHeight, this->useRectExtension, this->pixelFormat, this->dataType, const_cast<const UInt32**>(&(this->pixelBuffer))); CGContextRelease(context); if (this->pixelBuffer) { free(this->pixelBuffer); this->pixelBuffer = NULL; } CGImageRelease(imageRef); if (this->useRectExtension == false) { this->textureLogicalWidth = (double)this->imageWidth / (double)this->textureWidth; this->textureLogicalHeight = (double)this->imageHeight / (double)this->textureHeight; } else { this->textureLogicalWidth = (double)this->textureWidth; this->textureLogicalHeight = (double)this->textureHeight; } return osStatus; }
bool VisualTextureContainer::initWithEncodedData(const char* const bufferData, size_t size) { bool success = true; bool debug = false; this->releaseTextureData(); uint32* aPixelBuffer = NULL; #if TARGET_OS_WIN HGLOBAL hGlobal = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, (SIZE_T)size); if (!hGlobal) return false; BYTE* pDest = (BYTE*)::GlobalLock(hGlobal); memcpy(pDest, bufferData, size); ::GlobalUnlock(hGlobal); IStream* pStream = NULL; if (::CreateStreamOnHGlobal(hGlobal, FALSE, &pStream) != S_OK) return false; Gdiplus::Bitmap* bitmap = Gdiplus::Bitmap::FromStream(pStream); bitmap->RotateFlip(Gdiplus::RotateNoneFlipY); this->imageRect.width = bitmap->GetWidth(); this->imageRect.height = bitmap->GetHeight(); VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); if (this->useRectExtension == false) { this->textureRect.width = theVisualGraphics->power2Ceiling(this->imageRect.width); this->textureRect.height = theVisualGraphics->power2Ceiling(this->imageRect.height); } else { this->textureRect.width = this->imageRect.width; this->textureRect.height = this->imageRect.height; } aPixelBuffer = (uint32*)malloc(this->imageRect.width * this->imageRect.height * sizeof(uint32)); Gdiplus::Rect rect(0, 0, this->imageRect.width, this->imageRect.height); Gdiplus::BitmapData* bitmapData = new Gdiplus::BitmapData; bitmapData->Width = this->imageRect.width; bitmapData->Height = this->imageRect.height; bitmapData->Stride = sizeof(uint32) * bitmapData->Width; bitmapData->PixelFormat = PixelFormat32bppARGB; bitmapData->Scan0 = (VOID*)aPixelBuffer; Gdiplus::Status status = Gdiplus::Ok; status = bitmap->LockBits(&rect, Gdiplus::ImageLockModeRead | Gdiplus::ImageLockModeUserInputBuf, PixelFormat32bppPARGB, bitmapData); #endif #if TARGET_OS_MAC CFDataRef dataRef = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (UInt8*)bufferData, (CFIndex)size, kCFAllocatorDefault); CFDictionaryRef options = NULL; CGImageSourceRef imageSourceRef = CGImageSourceCreateWithData(dataRef, options); CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSourceRef, 0, options); this->imageRect.width = CGImageGetWidth(imageRef); this->imageRect.height = CGImageGetHeight(imageRef); VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); if (this->useRectExtension == false) { this->textureRect.width = theVisualGraphics->power2Ceiling(this->imageRect.width); this->textureRect.height = theVisualGraphics->power2Ceiling(this->imageRect.height); } else { this->textureRect.width = this->imageRect.width; this->textureRect.height = this->imageRect.height; } CGContextRef contextPtr = theVisualGraphics->createBitmapContext(this->imageRect.width, this->imageRect.height); CGContextTranslateCTM(contextPtr, 0, this->imageRect.height); CGContextScaleCTM(contextPtr, 1.0f, -1.0f); CGRect rect = CGRectMake(0, 0, this->imageRect.width, this->imageRect.height); CGContextDrawImage(contextPtr, rect, imageRef); aPixelBuffer = static_cast<uint32*>(CGBitmapContextGetData(contextPtr)); #endif PixelColor* interleavedARGBColorPixelBuffer = NULL; if (debug == true) { interleavedARGBColorPixelBuffer = VisualColorTools::createARGBCheckPixels(this->textureRect.width, this->textureRect.height); } else { interleavedARGBColorPixelBuffer = static_cast<PixelColor*>(aPixelBuffer); } success = this->initWithARGBPixelData(interleavedARGBColorPixelBuffer, this->imageRect.width, this->imageRect.height); #if TARGET_OS_MAC CGContextRelease(contextPtr); CGImageRelease(imageRef); #endif #if TARGET_OS_WIN bitmap->UnlockBits(bitmapData); #endif return success; }
OSStatus VisualTextureContainer::initWithFile(const VisualFile& aFile, OSType fileType) { this->releaseTextureData(); if (this->pixelBuffer != NULL) { free(this->pixelBuffer); this->pixelBuffer = NULL; } OSStatus status = noErr; char errLog[128]; GraphicsImportComponent gi = 0; status = VisualQuickTime::getGraphicsImporterForFile(aFile, gi, fileType); if (status != noErr) { sprintf(errLog, "err %ld: after getGraphicsImporterForFile() in file: %s (line: %d) [%s])", status, __FILE__, __LINE__, __FUNCTION__); writeLog(errLog); return status; } status = VisualQuickTime::setMaxQuality(gi); if (status != noErr) { sprintf(errLog, "err %ld: after setMaxQuality() in file: %s (line: %d) [%s])", status, __FILE__, __LINE__, __FUNCTION__); writeLog(errLog); return status; } VisualQuickTime::getImageDimensions(gi, this->imageWidth, this->imageHeight); VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); if (this->useRectExtension == false) { this->textureWidth = theVisualGraphics->power2Ceiling(this->imageWidth); this->textureHeight = theVisualGraphics->power2Ceiling(this->imageHeight); } else { this->textureWidth = this->imageWidth; this->textureHeight = this->imageHeight; } status = VisualQuickTime::flipImportMatrix(gi, this->imageWidth, this->imageHeight); if (status != noErr) { sprintf(errLog, "err %ld: flipImportMatrix", status); writeLog(errLog); return status; } #if TARGET_OS_MAC CGImageRef imageRef = VisualQuickTime::createCGImageRef(gi); if (imageRef == NULL) { char errStr[256]; sprintf(errStr, "imageRef is NULL in file: %s (line: %d) [%s])", __FILE__, __LINE__, __FUNCTION__); writeLog(errStr); return NULL; } CGContextRef contextPtr = theVisualGraphics->createBitmapContext(this->textureWidth, this->textureHeight); CGRect rect = CGRectMake(0, (this->textureHeight - this->imageHeight), this->imageWidth, this->imageHeight); CGContextDrawImage(contextPtr, rect, imageRef); this->pixelBuffer = static_cast<UInt32*>(CGBitmapContextGetData(contextPtr)); #endif #if TARGET_OS_WIN this->pixelBuffer = VisualQuickTime::getBitmapOfDrawing(gi, this->textureWidth, this->textureHeight); #endif CloseComponent(gi); this->textureName = theVisualGraphics->getNextFreeTextureName(); this->textureIsSet = true; VisualTextureContainer::textureRefCountMap[this->textureName] = 1; #if TARGET_OS_WIN theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureWidth, this->textureHeight, this->useRectExtension, this->pixelFormat, kGL_UNSIGNED_BYTE, const_cast<const UInt32**>(&(this->pixelBuffer))); #endif #if TARGET_OS_MAC theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureWidth, this->textureHeight, this->useRectExtension, this->pixelFormat, this->dataType, const_cast<const UInt32**>(&(this->pixelBuffer))); #endif #if TARGET_OS_WIN if (this->pixelBuffer != NULL) { free(this->pixelBuffer); this->pixelBuffer = NULL; } #endif #if TARGET_OS_MAC CGContextRelease(contextPtr); if (this->pixelBuffer != NULL) { free(this->pixelBuffer); this->pixelBuffer = NULL; } CGImageRelease(imageRef); #endif if (this->useRectExtension == false) { this->textureLogicalWidth = (double)this->imageWidth / (double)this->textureWidth; this->textureLogicalHeight = (double)this->imageHeight / (double)this->textureHeight; } else { this->textureLogicalWidth = (double)this->textureWidth; this->textureLogicalHeight = (double)this->textureHeight; } return status; }