bool VisualTextureContainer::initWithARGBPixelData(PixelColor* argbPixels, size_t imageWidth, size_t imageHeight, bool debug) { bool success = true; this->imageRect.width = imageWidth; this->imageRect.height = imageHeight; 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; } this->textureName = theVisualGraphics->getNextFreeTextureName(); this->aTextureIsSet = true; VisualTextureContainer::textureRefCountMap[this->textureName] = 1; success = theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->imageRect.width, this->imageRect.height, this->useRectExtension, const_cast<const uint32**>(&(argbPixels)), debug); if (this->useRectExtension == false) { this->logicalSize.width = (double)this->imageRect.width / (double)this->textureRect.width; this->logicalSize.height = (double)this->imageRect.height / (double)this->textureRect.height; } else { this->logicalSize.width = (double)this->textureRect.width; this->logicalSize.height = (double)this->textureRect.height; } return success; }
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 copyARGBBitmapDataToTexture(uint32 textureNumber, uint32 imageWidth, uint32 imageHeight, bool canUseRectExtension, uint32** bitmapData) { VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); return theVisualGraphics->copyARGBBitmapDataToTexture(textureNumber, imageWidth, imageHeight, canUseRectExtension, const_cast<const uint32**>(bitmapData)); }
void VisualTextureContainer::applyConvolutionFilter(const VisualConvolutionFilter& aConvolutionFilter) { VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); uint16 dataType; uint16 pixelFormat = kGL_BGRA; // BGRA on intel machines and ARGB on ppc #if TARGET_OS_WIN dataType = kGL_UNSIGNED_BYTE; #else #if __BIG_ENDIAN__ dataType = kGL_UNSIGNED_INT_8_8_8_8_REV; #else dataType = kGL_UNSIGNED_INT_8_8_8_8; #endif #endif if (theVisualGraphics->doesSupportGLConvolutionFilter()) { int prevReadBuffer = theVisualGraphics->getCurrentColorBufferForPixelReadingOperations(); theVisualGraphics->setColorBufferForPixelReadingOperations(kGL_BACK_COLOR_BUFFER); int prevDrawBuffer = theVisualGraphics->getCurrentColorBufferForPixelDrawingOperations(); theVisualGraphics->setColorBufferForPixelDrawingOperations(kGL_BACK_COLOR_BUFFER); uint32* prevPixels = NULL; char errStr[512]; uint8 numberOfBytesPerChannel = 0; uint8 numberOfChannels = 0; // channel == color resp. alpha channel uint8 numberOfBytesPerPixel = 0; uint32 numberOfBytesPerRow = 0; if ((pixelFormat == kGL_RGBA) || (pixelFormat == kGL_BGRA)) { numberOfChannels = 4; } else { sprintf(errStr, "unknown format %d in file: %s (line: %d) [%s])", pixelFormat, __FILE__, __LINE__, __FUNCTION__); writeLog(errStr); return; } if ((dataType == kGL_UNSIGNED_INT_8_8_8_8_REV) || (dataType == kGL_UNSIGNED_INT_8_8_8_8) || (dataType == kGL_UNSIGNED_BYTE)) { numberOfBytesPerChannel = 1; // // 1 byte (== 8 bits) per color/channel } else { sprintf(errStr, "unknown type %d in file: %s (line: %d) [%s])", dataType, __FILE__, __LINE__, __FUNCTION__); writeLog(errStr); return; } numberOfBytesPerPixel = numberOfBytesPerChannel * numberOfChannels; numberOfBytesPerRow = numberOfBytesPerPixel * this->textureRect.width; // read previous pixels if ((pixelFormat == kGL_RGBA) || (pixelFormat == kGL_BGRA)) { prevPixels = (uint32*)calloc((numberOfBytesPerRow / numberOfBytesPerPixel) * this->textureRect.height, numberOfBytesPerPixel); } VisualCamera aCamera; aCamera.setOrthographicProjection(); theVisualGraphics->readPixels(aCamera.getMaxLeftCoord(), aCamera.getMaxBottomCoord(), this->textureRect.width, this->textureRect.height, &prevPixels, pixelFormat, dataType, aCamera); PixelColor* pixels = NULL; pixels = this->createARGBImagePixels(); if (pixels == NULL) { sprintf(errStr, "pixels == NULL in file: %s (line: %d) [%s])", __FILE__, __LINE__, __FUNCTION__); writeLog(errStr); return; } theVisualGraphics->drawPixels(&pixels, aCamera.getMaxLeftCoord(), aCamera.getMaxBottomCoord(), this->imageRect.width, this->imageRect.height, pixelFormat, dataType, &aConvolutionFilter); free(pixels); pixels = NULL; BottomLeftPositionedPixelRect clipRect; clipRect.bottomLeftPixel.x = 0; clipRect.bottomLeftPixel.y = 0; clipRect.pixelRect.width = this->textureRect.width; clipRect.pixelRect.height = this->textureRect.height; theVisualGraphics->enableTexturing(this->useRectExtension); theVisualGraphics->copyFramebufferToTexture(this->textureName, this->useRectExtension, clipRect, pixelFormat, dataType); theVisualGraphics->disableTexturing(this->useRectExtension); // restore previous pixels theVisualGraphics->drawPixels(&prevPixels, aCamera.getMaxLeftCoord(), aCamera.getMaxBottomCoord(), this->textureRect.width, this->textureRect.height, pixelFormat, dataType); free(prevPixels); theVisualGraphics->setColorBufferForPixelDrawingOperations(prevDrawBuffer); theVisualGraphics->setColorBufferForPixelReadingOperations(prevReadBuffer); } else { PixelColor* pixels = this->createARGBImagePixels(); PixelColor* filteredPixels = NULL; aConvolutionFilter.applyToPixelData(pixels, this->imageRect.width, this->imageRect.height, pixelFormat, dataType, &filteredPixels); free(pixels); pixels = NULL; bool success = false; const PixelColor* constFilteredPixels = const_cast<const PixelColor*>(filteredPixels); success = theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->imageRect.width, this->imageRect.height, this->useRectExtension, &constFilteredPixels); if (filteredPixels != NULL) { free(filteredPixels); } } }
bool VisualTextureContainer::initWithStyledString(VisualStyledString& styledString) { this->releaseTextureData(); bool success = true; const VisualString* aVisualString = &(styledString); VisualStringStyle& stringStyle = styledString.getStyle(); if (aVisualString->getNumberOfCharacters() == 0) { return false; } VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); uint32 maxPixelWidth = theVisualGraphics->getCanvasPixelWidth(); uint32 maxPixelHeight = theVisualGraphics->getCanvasPixelHeight(); this->textureName = theVisualGraphics->getNextFreeTextureName(); this->aTextureIsSet = true; VisualTextureContainer::textureRefCountMap[this->textureName] = 1; this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); #if TARGET_OS_MAC char alignmentStr[32]; switch (stringStyle.horizontalAlignment) { case (kLeftAligned): strcpy(alignmentStr, "left"); break; case(kCenterAligned): strcpy(alignmentStr, "center"); break; case(kRightAligned): strcpy(alignmentStr, "right"); break; default: break; } success = getDimensionsOfCocoaStringBitmap((void*)aVisualString, (void*)&stringStyle, &(this->imageRect.width), &(this->imageRect.height), maxPixelWidth, maxPixelHeight, alignmentStr); if (!success) { return success; } 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; } PixelColor* pixelBuffer = (uint32*)calloc(this->textureRect.width * this->textureRect.height, sizeof(uint32)); success = getCocoaStringBitmapData((void*)&styledString, this->textureRect.width, this->textureRect.height, alignmentStr, &(pixelBuffer)); success = theVisualGraphics->copyARGBBitmapDataToTexture(this->textureName, this->textureRect.width, this->textureRect.height, this->useRectExtension, const_cast<const uint32**>(&(pixelBuffer))); #endif #if TARGET_OS_WIN wchar_t* stringValueRef = (wchar_t*)(styledString.getCharactersPointer()); uint8 red = (uint8)(stringStyle.fontColor.r * 255.0f); uint8 green = (uint8)(stringStyle.fontColor.g * 255.0f); uint8 blue = (uint8)(stringStyle.fontColor.b * 255.0f); if (!stringValueRef) { success = false; return success; } success = theVisualGraphics->makeTextureOfStringWin(stringValueRef, styledString.getNumberOfCharacters(), this->textureName, this->textureRect.width, this->textureRect.height, this->imageRect.width, this->imageRect.height, stringStyle.fontNameStr, (uint16)stringStyle.fontSize, red, green, blue, stringStyle.horizontalAlignment, maxPixelWidth, maxPixelHeight); #endif if (this->useRectExtension == false) { this->logicalSize.width = (double)this->imageRect.width / (double)this->textureRect.width; this->logicalSize.height = (double)this->imageRect.height / (double)this->textureRect.height; } else { this->logicalSize.width = (double)this->textureRect.width; this->logicalSize.height = (double)this->textureRect.height; } return success; }
OSStatus VisualTextureContainer::initWithString(const VisualString& stringValue, VisualStringStyle& stringStyle) { this->releaseTextureData(); if (this->pixelBuffer != NULL) { free(this->pixelBuffer); this->pixelBuffer = NULL; } OSStatus status = noErr; VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); UInt16 maxPixelWidth = theVisualGraphics->getCanvasPixelWidth(); UInt16 maxPixelHeight = theVisualGraphics->getCanvasPixelHeight(); this->textureName = theVisualGraphics->getNextFreeTextureName(); this->textureIsSet = true; VisualTextureContainer::textureRefCountMap[this->textureName] = 1; this->useRectExtension = theVisualGraphics->canUseTextureRectExtension(); #if TARGET_OS_MAC char alignmentStr[32]; switch (stringStyle.horizontalAlignment) { case (kLeftAligned): strcpy(alignmentStr, "left"); break; case(kCenterAligned): strcpy(alignmentStr, "center"); break; case(kRightAligned): strcpy(alignmentStr, "right"); break; default: break; } status = getDimensionsOfCocoaStringBitmap((void*)&stringValue, &(this->imageWidth), &(this->imageHeight), const_cast<char*>(stringStyle.fontNameStr), &(stringStyle.fontSize), stringStyle.fontColor.r, stringStyle.fontColor.g, stringStyle.fontColor.b, maxPixelWidth, maxPixelHeight, alignmentStr); 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; } this->pixelBuffer = (UInt32*)calloc(this->textureWidth * this->textureHeight, sizeof(UInt32)); status = getCocoaStringBitmapData((void*)&stringValue, this->textureWidth, this->textureHeight, const_cast<char*>(stringStyle.fontNameStr), stringStyle.fontSize, stringStyle.fontColor.r, stringStyle.fontColor.g, stringStyle.fontColor.b, alignmentStr, &(this->pixelBuffer)); 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 wchar_t* stringValueRef = (wchar_t*)stringValue.getCharactersPointer(); UInt8 red = (UInt8)(stringStyle.fontColor.r * 255.0f); UInt8 green = (UInt8)(stringStyle.fontColor.g * 255.0f); UInt8 blue = (UInt8)(stringStyle.fontColor.b * 255.0f); status = theVisualGraphics->makeTextureOfStringWin(stringValueRef, stringValue.getNumberOfCharacters(), this->textureName, this->textureWidth, this->textureHeight, this->imageWidth, this->imageHeight, stringStyle.fontNameStr, (UInt16)stringStyle.fontSize, red, green, blue, stringStyle.horizontalAlignment, maxPixelWidth, maxPixelHeight); #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; }
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; }
OSStatus copyBitmapDataToTexture(UInt32 textureNumber, UInt32 width, UInt32 height, bool canUseRectExtension, UInt16 format, UInt16 dataType, UInt32** bitmapData) { VisualGraphics* theVisualGraphics = VisualGraphics::getInstance(); return theVisualGraphics->copyARGBBitmapDataToTexture(textureNumber, width, height, canUseRectExtension, format, dataType, const_cast<const UInt32**>(bitmapData)); }