bool wxBitmapDataObject::SetData( size_t nSize, const void *pBuf ) { Clear(); if ((pBuf == NULL) || (nSize == 0)) return false; Handle picHandle = NewHandle( nSize ); memcpy( *picHandle, pBuf, nSize ); m_pictHandle = picHandle; CGImageRef cgImageRef = 0; CFDataRef data = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, (const UInt8*) pBuf, nSize, kCFAllocatorNull); CGImageSourceRef source = CGImageSourceCreateWithData( data, NULL ); if ( source ) { cgImageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL); CFRelease( source ); } CFRelease( data ); if ( cgImageRef ) { m_bitmap.Create( CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) ); CGRect r = CGRectMake( 0 , 0 , CGImageGetWidth(cgImageRef) , CGImageGetHeight(cgImageRef) ); // since our context is upside down we dont use CGContextDrawImage wxMacDrawCGImage( (CGContextRef) m_bitmap.GetHBITMAP() , &r, cgImageRef ) ; CGImageRelease(cgImageRef); cgImageRef = NULL; } return m_bitmap.IsOk(); }
CGImageRef ImageSource::createFrameAtIndex(size_t index, float* scale) { UNUSED_PARAM(scale); if (!initialized()) return 0; #if !PLATFORM(IOS) UNUSED_PARAM(scale); RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata))); #else // Subsampling can be 1, 2 or 3, which means quarter-, sixteenth- and sixty-fourth-size, respectively. // A zero or negative value means no subsampling. int subsampling = scale ? static_cast<int>(log2f(1.0f / std::max(0.1f, std::min(1.0f, *scale)))) : -1; RetainPtr<CGImageRef> image = adoptCF(CGImageSourceCreateImageAtIndex(m_decoder, index, imageSourceOptions(SkipMetadata, subsampling))); // <rdar://problem/7371198> - CoreGraphics changed the default caching behaviour in iOS 4.0 to kCGImageCachingTransient // which caused a performance regression for us since the images had to be resampled/recreated every time we called // CGContextDrawImage. We now tell CG to cache the drawn images. See also <rdar://problem/14366755> - // CoreGraphics needs to un-deprecate kCGImageCachingTemporary since it's still not the default. #if COMPILER(CLANG) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" #endif CGImageSetCachingFlags(image.get(), kCGImageCachingTemporary); #if COMPILER(CLANG) #pragma clang diagnostic pop #endif if (scale) { if (subsampling > 0) *scale = static_cast<float>(CGImageGetWidth(image.get())) / size(DoNotRespectImageOrientation).width(); else { ASSERT(static_cast<int>(CGImageGetWidth(image.get())) == size(DoNotRespectImageOrientation).width()); *scale = 1; } } #endif // !PLATFORM(IOS) CFStringRef imageUTI = CGImageSourceGetType(m_decoder); static const CFStringRef xbmUTI = CFSTR("public.xbitmap-image"); if (!imageUTI || !CFEqual(imageUTI, xbmUTI)) return image.leakRef(); // If it is an xbm image, mask out all the white areas to render them transparent. const CGFloat maskingColors[6] = {255, 255, 255, 255, 255, 255}; RetainPtr<CGImageRef> maskedImage = adoptCF(CGImageCreateWithMaskingColors(image.get(), maskingColors)); if (!maskedImage) return image.leakRef(); return maskedImage.leakRef(); }
void DrawUsingCGImage( void ) { OSErr err = noErr; Handle hOpenTypeList = NewHandle(0); long numTypes = 0; FSSpec theFSSpec; Rect bounds = { 45, 10, 100, 100 }; GraphicsImportComponent importer = 0; CGImageRef imageRef = 0; CGContextRef context = NULL; CGRect rect; BuildGraphicsImporterValidFileTypes( hOpenTypeList, &numTypes ); HLock( hOpenTypeList ); err = GetOneFileWithPreview(numTypes, (OSTypePtr)*hOpenTypeList, &theFSSpec, NULL); DisposeHandle( hOpenTypeList ); if ( err ) return; // locate and open a graphics importer component which can be used to draw the // selected file. If a suitable importer is not found the ComponentInstance // is set to NULL. err = GetGraphicsImporterForFile( &theFSSpec, // specifies the file to be drawn &importer ); // pointer to the returned GraphicsImporterComponent window = NewCWindow( NULL, &bounds, "\pDraw Using CGImage", false, documentProc, (WindowPtr)-1, true, 0); // import the image as a CGImage err = GraphicsImportCreateCGImage( importer, &imageRef, kGraphicsImportCreateCGImageUsingCurrentSettings ); if (err) return; SizeWindow( window, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ), false ); ShowWindow(window); // create a Core Graphics Context from the window port err = QDBeginCGContext(GetWindowPort(window), &context); if (err) return; // make a rectangle designating the location and dimensions in user space of the bounding box in which to draw the image rect = CGRectMake( 0, 0, CGImageGetWidth( imageRef ), CGImageGetHeight( imageRef ) ); // draw the image CGContextDrawImage( context, rect, imageRef ); // end the the context we had for the port QDEndCGContext(GetWindowPort(window), &context); // close the importer instance CloseComponent( importer ); }
void PoofItGood( Point centerPt ) { CGRect box; WindowRef window; Rect bounds; CGContextRef context; CGImageRef image; float windowWidth; float windowHeight; int i; image = GetThePoofImage(); if ( image == NULL ) goto Bail; windowWidth = CGImageGetWidth( image ) / NUMBER_OF_POOF_ANIM_FRAMES; windowHeight = CGImageGetHeight( image ); // Start our animation bounds at the first item in the animation strip box.origin.x = box.origin.y = 0; box.size.width = CGImageGetWidth( image ); box.size.height = CGImageGetHeight( image ); bounds.top = centerPt.v - (SInt16)(windowHeight / 2); bounds.left = centerPt.h - (SInt16)(windowWidth / 2); bounds.bottom = bounds.top + (SInt16)windowHeight; bounds.right = bounds.left + (SInt16)windowWidth; CreateNewWindow( kOverlayWindowClass, 0, &bounds, &window ); CreateCGContextForPort( GetWindowPort( window ), &context ); ShowWindow( window ); for ( i = 1; i <= NUMBER_OF_POOF_ANIM_FRAMES; i++ ) { CGContextClearRect( context, box ); CGContextDrawImage( context, box, image ); CGContextFlush( context ); Delay( EventTimeToTicks( POOF_ANIMATION_DELAY ), NULL ); box.origin.x -= windowWidth; } CGContextRelease( context ); CGImageRelease( image ); DisposeWindow( window ); Bail: return; }
DataSourceSurfaceCG::DataSourceSurfaceCG(CGImageRef aImage) { mFormat = SurfaceFormat::B8G8R8A8; mImage = aImage; mCg = CreateBitmapContextForImage(aImage); if (mCg == nullptr) { // error creating context return; } // Get image width, height. We'll use the entire image. CGFloat w = CGImageGetWidth(aImage); CGFloat h = CGImageGetHeight(aImage); CGRect rect = {{0,0},{w,h}}; // Draw the image to the bitmap context. Once we draw, the memory // allocated for the context for rendering will then contain the // raw image data in the specified color space. CGContextDrawImage(mCg, rect, aImage); // Now we can get a pointer to the image data associated with the bitmap // context. mData = CGBitmapContextGetData(mCg); assert(mData); }
bool GiCanvasIos::drawImage(CGImageRef image, const Point2d& centerM, bool autoScale) { CGContextRef context = m_draw->getContext(); bool ret = false; if (context && image) { Point2d ptD = centerM * m_draw->xf().modelToDisplay(); float w = CGImageGetWidth(image); float h = CGImageGetHeight(image); if (autoScale) { w *= m_draw->xf().getViewScale(); h *= m_draw->xf().getViewScale(); } CGAffineTransform af = CGAffineTransformMake(1, 0, 0, -1, 0, m_draw->height()); af = CGAffineTransformTranslate(af, ptD.x - w * 0.5f, m_draw->height() - (ptD.y + h * 0.5f)); CGContextConcatCTM(context, af); CGContextDrawImage(context, CGRectMake(0, 0, w, h), image); CGContextConcatCTM(context, CGAffineTransformInvert(af)); ret = true; } return ret; }
CGImageRef GiCanvasIos::cachedBitmap(bool invert) { CGImageRef image = m_draw->_caches[0]; if (!image || !invert) return image; // 调用者不能释放图像 size_t w = CGImageGetWidth(image); // 图像宽度,像素单位,不是点单位 size_t h = CGImageGetHeight(image); CGImageRef newimg = NULL; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, w * 4, colorSpace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorSpace); if (context) { CGAffineTransform af = CGAffineTransformMake(1, 0, 0, -1, 0, h); CGContextConcatCTM(context, af); // 图像是朝上的,上下文坐标系朝下,上下颠倒显示 CGContextDrawImage(context, CGRectMake(0, 0, w, h), image); CGContextConcatCTM(context, CGAffineTransformInvert(af)); newimg = CGBitmapContextCreateImage(context); // 得到上下颠倒的新图像 CGContextRelease(context); } return newimg; // 由调用者释放图像, CGImageRelease }
bool ImageIODecoder::readHeader() { CFURLRef imageURLRef; CGImageSourceRef sourceRef; // diciu, if ReadHeader is called twice in a row make sure to release the previously allocated imageRef if (imageRef != NULL) CGImageRelease(imageRef); imageRef = NULL; imageURLRef = CFURLCreateFromFileSystemRepresentation( NULL, (const UInt8*)m_filename.c_str(), m_filename.size(), false ); sourceRef = CGImageSourceCreateWithURL( imageURLRef, NULL ); CFRelease( imageURLRef ); if ( !sourceRef ) return false; imageRef = CGImageSourceCreateImageAtIndex( sourceRef, 0, NULL ); CFRelease( sourceRef ); if( !imageRef ) return false; m_width = CGImageGetWidth( imageRef ); m_height = CGImageGetHeight( imageRef ); CGColorSpaceRef colorSpace = CGImageGetColorSpace( imageRef ); if( !colorSpace ) return false; m_type = CGColorSpaceGetNumberOfComponents( colorSpace ) > 1 ? CV_8UC3 : CV_8UC1; return true; }
//----------------------------------------------------------------------------------- static void DrawTheMTView(CGContextRef ctx, MTViewData* data) { CGRect dstRect; #if CG_COORDINATES TransformHIViewToCG(ctx, data->theView); #endif // Draw the image first, before stroking the path; otherwise the path gets overwritten if (data->theImage != NULL) { dstRect = CGRectMake(0, 0, CGImageGetWidth(data->theImage), CGImageGetHeight(data->theImage)); #if CG_COORDINATES CGContextDrawImage(ctx, dstRect, data->theImage); #else HIViewDrawCGImage(ctx, &dstRect, data->theImage); #endif } if (data->thePath != NULL) { CGPathApply(data->thePath, (void*)ctx, MyCGPathApplier); CGContextStrokePath(ctx); } } // DrawTheMTView
void BitmapImage::checkForSolidColor() { m_checkedForSolidColor = true; if (frameCount() > 1) { m_isSolidColor = false; return; } CGImageRef image = frameAtIndex(0); // Currently we only check for solid color in the important special case of a 1x1 image. if (image && CGImageGetWidth(image) == 1 && CGImageGetHeight(image) == 1) { unsigned char pixel[4]; // RGBA RetainPtr<CGContextRef> bmap(AdoptCF, CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), deviceRGBColorSpaceRef(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big)); if (!bmap) return; GraphicsContext(bmap.get()).setCompositeOperation(CompositeCopy); CGRect dst = { {0, 0}, {1, 1} }; CGContextDrawImage(bmap.get(), dst, image); if (pixel[3] == 0) m_solidColor = Color(0, 0, 0, 0); else m_solidColor = Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]); m_isSolidColor = true; } }
bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleToFit) { const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth(image)); const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHeight(image)); SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); SkBitmap tmp; if (!tmp.allocPixels(info)) { return false; } if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), image)) { return false; } CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image); switch (cgInfo) { case kCGImageAlphaNone: case kCGImageAlphaNoneSkipLast: case kCGImageAlphaNoneSkipFirst: SkASSERT(SkBitmap::ComputeIsOpaque(tmp)); tmp.setAlphaType(kOpaque_SkAlphaType); break; default: // we don't know if we're opaque or not, so compute it. if (SkBitmap::ComputeIsOpaque(tmp)) { tmp.setAlphaType(kOpaque_SkAlphaType); } } *dst = tmp; return true; }
static UncompressedImage CGImageToUncompressedImage(CGImageRef image) { if (image == nullptr) return UncompressedImage(); size_t width = CGImageGetWidth(image); size_t height = CGImageGetHeight(image); if ((0 == width) || (0 == height)) return UncompressedImage(); size_t bits_per_pixel = CGImageGetBitsPerPixel(image); size_t bits_per_component = CGImageGetBitsPerComponent(image); CGColorSpaceRef colorspace = CGImageGetColorSpace(image); size_t row_size; UncompressedImage::Format format; CGColorSpaceRef bitmap_colorspace; CGBitmapInfo bitmap_info; if ((8 == bits_per_pixel) && (8 == bits_per_component) && (CGColorSpaceGetModel(colorspace) == kCGColorSpaceModelMonochrome)) { row_size = width; format = UncompressedImage::Format::GRAY; static CGColorSpaceRef grey_colorspace = CGColorSpaceCreateDeviceGray(); bitmap_colorspace = grey_colorspace; bitmap_info = 0; } else { static CGColorSpaceRef rgb_colorspace = CGColorSpaceCreateDeviceRGB(); bitmap_colorspace = rgb_colorspace; if ((24 == bits_per_pixel) && (8 == bits_per_component)) { row_size = width * 3; format = UncompressedImage::Format::RGB; bitmap_info = kCGBitmapByteOrder32Big; } else { row_size = width * 4; format = UncompressedImage::Format::RGBA; bitmap_info = kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big; } } std::unique_ptr<uint8_t[]> uncompressed(new uint8_t[height * row_size]); CGContextRef bitmap = CGBitmapContextCreate(uncompressed.get(), width, height, 8, row_size, bitmap_colorspace, bitmap_info); if (nullptr == bitmap) { return UncompressedImage(); } AtScopeExit(bitmap) { CFRelease(bitmap); }; CGContextDrawImage(bitmap, CGRectMake(0, 0, width, height), image); return UncompressedImage(format, row_size, width, height, std::move(uncompressed)); }
BitmapImage::BitmapImage(CGImageRef cgImage, ImageObserver* observer) : Image(observer) , m_currentFrame(0) , m_frames(0) , m_frameTimer(0) , m_repetitionCount(cAnimationNone) , m_repetitionCountStatus(Unknown) , m_repetitionsComplete(0) , m_decodedSize(0) , m_decodedPropertiesSize(0) , m_frameCount(1) , m_isSolidColor(false) , m_checkedForSolidColor(false) , m_animationFinished(true) , m_allDataReceived(true) , m_haveSize(true) , m_sizeAvailable(true) , m_haveFrameCount(true) { CGFloat width = CGImageGetWidth(cgImage); CGFloat height = CGImageGetHeight(cgImage); m_decodedSize = width * height * 4; m_size = IntSize(width, height); // Since we don't have a decoder, we can't figure out the image orientation. // Set m_sizeRespectingOrientation to be the same as m_size so it's not 0x0. m_sizeRespectingOrientation = IntSize(width, height); m_frames.grow(1); m_frames[0].m_frame = cgImage; m_frames[0].m_hasAlpha = true; m_frames[0].m_haveMetadata = true; checkForSolidColor(); }
void BitmapImage::checkForSolidColor() { m_checkedForSolidColor = true; if (frameCount() > 1) m_isSolidColor = false; else { CGImageRef image = frameAtIndex(0); // Currently we only check for solid color in the important special case of a 1x1 image. if (image && CGImageGetWidth(image) == 1 && CGImageGetHeight(image) == 1) { unsigned char pixel[4]; // RGBA CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); CGContextRef bmap = CGBitmapContextCreate(pixel, 1, 1, 8, sizeof(pixel), space, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); if (bmap) { GraphicsContext(bmap).setCompositeOperation(CompositeCopy); CGRect dst = { {0, 0}, {1, 1} }; CGContextDrawImage(bmap, dst, image); if (pixel[3] == 0) m_solidColor = Color(0, 0, 0, 0); else m_solidColor = Color(pixel[0] * 255 / pixel[3], pixel[1] * 255 / pixel[3], pixel[2] * 255 / pixel[3], pixel[3]); m_isSolidColor = true; CFRelease(bmap); } CFRelease(space); } } }
CGContextRef CreateBitmapContextForImage(CGImageRef image) { CGColorSpaceRef colorSpace; size_t width = CGImageGetWidth(image); size_t height = CGImageGetHeight(image); int bitmapBytesPerRow = (width * 4); int bitmapByteCount = (bitmapBytesPerRow * height); void *data = calloc(bitmapByteCount, 1); //XXX: which color space should we be using here? colorSpace = CGColorSpaceCreateDeviceRGB(); assert(colorSpace); // we'd like to pass nullptr as the first parameter // to let Quartz manage this memory for us. However, // on 10.5 and older CGBitmapContextGetData will return // nullptr instead of the associated buffer so we need // to manage it ourselves. CGContextRef cg = CGBitmapContextCreate(data, width, height, 8, bitmapBytesPerRow, colorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); assert(cg); CGColorSpaceRelease(colorSpace); return cg; }
void GL::Image::load(const unsigned char *buf, size_t bufSize) { CFDataRef data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, (const UInt8*)buf, (CFIndex)bufSize, kCFAllocatorNull); if (data != NULL) { CGImageSourceRef imageSource = CGImageSourceCreateWithData(data, NULL); if (imageSource != NULL) { CGImageRef img = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL); if (img != NULL) { width_ = (int)CGImageGetWidth(img); height_ = (int)CGImageGetHeight(img); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); if (colorSpace != NULL) { char *texData = (char*)calloc(width_ * height_ * 4, sizeof(char)); CGContextRef ctx = CGBitmapContextCreate(texData, width_, height_, 8, width_ * 4, colorSpace, kCGBitmapByteOrder32Host | kCGImageAlphaPremultipliedFirst); if (ctx != NULL) { CGContextDrawImage(ctx, CGRectMake(0.0, 0.0, width_, height_), img); CGContextRelease(ctx); loadTextureData_(texData); } free(texData); CGColorSpaceRelease(colorSpace); } CGImageRelease(img); } CFRelease(imageSource); } CFRelease(data); } }
BitmapImage::BitmapImage(CGImageRef cgImage, ImageObserver* observer) : Image(observer) , m_currentFrame(0) , m_frames(0) , m_frameTimer(0) , m_repetitionCount(cAnimationNone) , m_repetitionCountStatus(Unknown) , m_repetitionsComplete(0) , m_isSolidColor(false) , m_checkedForSolidColor(false) , m_animationFinished(true) , m_allDataReceived(true) , m_haveSize(true) , m_sizeAvailable(true) , m_decodedSize(0) , m_haveFrameCount(true) , m_frameCount(1) { initPlatformData(); CGFloat width = CGImageGetWidth(cgImage); CGFloat height = CGImageGetHeight(cgImage); m_decodedSize = width * height * 4; m_size = IntSize(width, height); m_frames.grow(1); m_frames[0].m_frame = cgImage; m_frames[0].m_hasAlpha = true; m_frames[0].m_haveMetadata = true; checkForSolidColor(); }
bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); if (NULL == imageSrc) { return false; } SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL); if (NULL == image) { return false; } SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); const int width = SkToInt(CGImageGetWidth(image)); const int height = SkToInt(CGImageGetHeight(image)); bm->setInfo(SkImageInfo::MakeN32Premul(width, height)); if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } if (!this->allocPixelRef(bm, NULL)) { return false; } SkAutoLockPixels alp(*bm); if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), image)) { return false; } CGImageAlphaInfo info = CGImageGetAlphaInfo(image); switch (info) { case kCGImageAlphaNone: case kCGImageAlphaNoneSkipLast: case kCGImageAlphaNoneSkipFirst: SkASSERT(SkBitmap::ComputeIsOpaque(*bm)); bm->setAlphaType(kOpaque_SkAlphaType); break; default: // we don't know if we're opaque or not, so compute it. if (SkBitmap::ComputeIsOpaque(*bm)) { bm->setAlphaType(kOpaque_SkAlphaType); } } if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) { // CGBitmapContext does not support unpremultiplied, so the image has been premultiplied. // Convert to unpremultiplied. for (int i = 0; i < width; ++i) { for (int j = 0; j < height; ++j) { uint32_t* addr = bm->getAddr32(i, j); *addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr); } } bm->setAlphaType(kUnpremul_SkAlphaType); } return true; }
unsigned char* os_image_load_from_file(const char* filename, int* outWidth, int* outHeight, int* outChannels, int unused) { const int fileHandle = open(filename, O_RDONLY); struct stat statBuffer; fstat(fileHandle, &statBuffer); const size_t bytesInFile = (size_t)(statBuffer.st_size); uint8_t* fileData = (uint8_t*)(mmap(NULL, bytesInFile, PROT_READ, MAP_SHARED, fileHandle, 0)); if (fileData == MAP_FAILED) { fprintf(stderr, "Couldn't open file '%s' with mmap\n", filename); return NULL; } CFDataRef fileDataRef = CFDataCreateWithBytesNoCopy(NULL, fileData, bytesInFile, kCFAllocatorNull); CGDataProviderRef imageProvider = CGDataProviderCreateWithCFData(fileDataRef); const char* suffix = strrchr(filename, '.'); if (!suffix || suffix == filename) { suffix = ""; } CGImageRef image; if (strcasecmp(suffix, ".png") == 0) { image = CGImageCreateWithPNGDataProvider(imageProvider, NULL, true, kCGRenderingIntentDefault); } else if ((strcasecmp(suffix, ".jpg") == 0) || (strcasecmp(suffix, ".jpeg") == 0)) { image = CGImageCreateWithJPEGDataProvider(imageProvider, NULL, true, kCGRenderingIntentDefault); } else { munmap(fileData, bytesInFile); close(fileHandle); CFRelease(imageProvider); CFRelease(fileDataRef); fprintf(stderr, "Unknown suffix for file '%s'\n", filename); return NULL; } const int width = (int)CGImageGetWidth(image); const int height = (int)CGImageGetHeight(image); const int channels = 4; CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); const int bytesPerRow = (width * channels); const int bytesInImage = (bytesPerRow * height); uint8_t* result = (uint8_t*)(malloc(bytesInImage)); const int bitsPerComponent = 8; CGContextRef context = CGBitmapContextCreate(result, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); CGContextRelease(context); CFRelease(image); munmap(fileData, bytesInFile); close(fileHandle); CFRelease(imageProvider); CFRelease(fileDataRef); *outWidth = width; *outHeight = height; *outChannels = channels; return result; }
void pop_img_props (CGImageRef source_image, img_prop im_props) { // Populate the remaining image property struct values im_props->image_w = CGImageGetWidth (source_image); im_props->image_h = CGImageGetHeight (source_image); im_props->bits_ppixel = CGImageGetBitsPerPixel (source_image); im_props->bytes_row = CGImageGetBytesPerRow (source_image); } // pop_img_props
IntSize SourceSurfaceCG::GetSize() const { IntSize size; size.width = CGImageGetWidth(mImage); size.height = CGImageGetHeight(mImage); return size; }
static RetainPtr<CGImageRef> createCroppedImageIfNecessary(CGImageRef image, const IntSize& bounds) { if (image && (CGImageGetWidth(image) != static_cast<size_t>(bounds.width()) || CGImageGetHeight(image) != static_cast<size_t>(bounds.height()))) { return adoptCF(CGImageCreateWithImageInRect(image, CGRectMake(0, 0, bounds.width(), bounds.height()))); } return image; }
SInt32 CAUGuiGraphic::getWidth() { SInt32 width = 0; if ( Image != NULL ) width = CGImageGetWidth (Image); return width; }
// Ok, a bit freaky coz we want non-premultipied alpha! // BBDataBuffer *BBLoadImageData( BBDataBuffer *buf,String path,Array<int> info ) { path=String( "data/" )+path; NSString *nspath=path.ToNSString(); //This was apparently buggy in iOS2.x, but NO MORE? UIImage *uiimage=[ UIImage imageNamed:nspath ]; if( !uiimage ) return 0; CGImageRef cgimage=uiimage.CGImage; int width=CGImageGetWidth( cgimage ); int height=CGImageGetHeight( cgimage ); int pitch=CGImageGetBytesPerRow( cgimage ); int bpp=CGImageGetBitsPerPixel( cgimage ); if( bpp!=24 && bpp!=32 ) return 0; CFDataRef cfdata=CGDataProviderCopyData( CGImageGetDataProvider( cgimage ) ); unsigned char *src=(unsigned char*)CFDataGetBytePtr( cfdata ); int srclen=(int)CFDataGetLength( cfdata ); if( !buf->_New( width*height*4 ) ) return 0; unsigned char *dst=(unsigned char*)buf->WritePointer(); int y; switch( bpp ) { case 24: for( y=0; y<height; ++y ) { for( int x=0; x<width; ++x ) { *dst++=*src++; *dst++=*src++; *dst++=*src++; *dst++=255; } src+=pitch-width*3; } break; case 32: for( y=0; y<height; ++y ) { memcpy( dst,src,width*4 ); dst+=width*4; src+=pitch; } break; } if( info.Length()>0 ) info[0]=width; if( info.Length()>1 ) info[1]=height; CFRelease( cfdata ); return buf; }
static void patternCallback(void* info, CGContextRef context) { CGImageRef platformImage = static_cast<CGImageRef>(info); if (!platformImage) return; CGRect rect = GraphicsContext(context).roundToDevicePixels( FloatRect(0, 0, CGImageGetWidth(platformImage), CGImageGetHeight(platformImage))); CGContextDrawImage(context, rect, platformImage); }
bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); if (NULL == imageSrc) { return false; } SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL); if (NULL == image) { return false; } SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); const int width = CGImageGetWidth(image); const int height = CGImageGetHeight(image); bm->setConfig(SkBitmap::kARGB_8888_Config, width, height); if (SkImageDecoder::kDecodeBounds_Mode == mode) { return true; } if (!this->allocPixelRef(bm, NULL)) { return false; } bm->lockPixels(); bm->eraseColor(SK_ColorTRANSPARENT); // use the same colorspace, so we don't change the pixels at all CGColorSpaceRef cs = CGImageGetColorSpace(image); CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height, 8, bm->rowBytes(), cs, BITMAP_INFO); if (NULL == cg) { // perhaps the image's colorspace does not work for a context, so try just rgb cs = CGColorSpaceCreateDeviceRGB(); cg = CGBitmapContextCreate(bm->getPixels(), width, height, 8, bm->rowBytes(), cs, BITMAP_INFO); CFRelease(cs); } CGContextDrawImage(cg, CGRectMake(0, 0, width, height), image); CGContextRelease(cg); CGImageAlphaInfo info = CGImageGetAlphaInfo(image); switch (info) { case kCGImageAlphaNone: case kCGImageAlphaNoneSkipLast: case kCGImageAlphaNoneSkipFirst: SkASSERT(SkBitmap::ComputeIsOpaque(*bm)); bm->setIsOpaque(true); break; default: // we don't know if we're opaque or not, so compute it. bm->computeAndSetOpaquePredicate(); } bm->unlockPixels(); return true; }
CGImageRef BitmapImage::getFirstCGImageRefOfSize(const IntSize& size) { size_t count = frameCount(); for (size_t i = 0; i < count; ++i) { CGImageRef cgImage = frameAtIndex(i); if (cgImage && IntSize(CGImageGetWidth(cgImage), CGImageGetHeight(cgImage)) == size) return cgImage; } // Fallback to the default CGImageRef if we can't find the right size return getCGImageRef(); }
//----------------------------------------------------------------------------- CGBitmap::CGBitmap (CGImageRef image) : image (image) , imageSource (0) , layer (0) , bits (0) , dirty (false) , bytesPerRow (0) { CGImageRetain (image); size.x = CGImageGetWidth (image); size.y = CGImageGetHeight (image); }
uint32_t UtilCG::Texture::LoadTexture(const char *const aPath) { CFStringRef Path = CFStringCreateWithCString(kCFAllocatorDefault, aPath, kCFStringEncodingASCII); CFURLRef Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, Path, kCFURLPOSIXPathStyle, false); CGImageSourceRef Source = CGImageSourceCreateWithURL(Url, NULL); CGImageRef pImage = CGImageSourceCreateImageAtIndex(Source, 0, NULL); if ( !pImage ) { CFRelease(Url); CFRelease(Path); CFRelease(Source); return INVALID_HANDLE; } CGDataProviderRef pDataProvider = CGImageGetDataProvider(pImage); if ( pDataProvider ) { size_t width = CGImageGetWidth(pImage); size_t height = CGImageGetHeight(pImage); //size_t bytesPerRow = CGImageGetBytesPerRow(pImage); //size_t bitsPerPixel = CGImageGetBitsPerPixel(pImage); //size_t bitsPerComponent = CGImageGetBitsPerComponent(pImage); CFDataRef DataRef = CGDataProviderCopyData(pDataProvider); const UInt8* pData = CFDataGetBytePtr(DataRef); uint32_t hTexID = 0; glGenTextures(1, &hTexID); glBindTexture(GL_TEXTURE_2D, hTexID); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (GLint)width, (GLint)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pData); glBindTexture(GL_TEXTURE_2D, 0); CFRelease(Url); CFRelease(Path); CFRelease(Source); CGImageRelease(pImage); // CGDataProviderRelease(pDataProvider); return hTexID; } CFRelease(Url); CFRelease(Path); CFRelease(Source); CGImageRelease(pImage); CGDataProviderRelease(pDataProvider); return INVALID_HANDLE; }
CGImageRef CGimageResize(CGImageRef image, CGSize maxSize) { // calcualte size CGFloat ratio = MAX(CGImageGetWidth(image)/maxSize.width,CGImageGetHeight(image)/maxSize.height); size_t width = CGImageGetWidth(image)/ratio; size_t height = CGImageGetHeight(image)/ratio; // resize CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width*4, colorspace, kCGImageAlphaPremultipliedLast); CGColorSpaceRelease(colorspace); if(context == NULL) return nil; // draw image to context (resizing it) CGContextDrawImage(context, CGRectMake(0, 0, width, height), image); // extract resulting image from context CGImageRef imgRef = CGBitmapContextCreateImage(context); CGContextRelease(context); return imgRef; }