Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Questions about CGImage




On Feb 11, 2005, at 8:54 AM, Michael Becker wrote:

Am 11.02.2005 um 15:29 schrieb Scott Thompson:
One way to do that would be to create an offscreen bitmap and draw the image from the JPEG into the offscreen bitmap context. Then you can create another CGImage from the pixels of the offscreen context and use it.

Phew, sounds easy to you, probably ;-)
Could you maybe provide some pseudo-code or just something for me to hang on to? I understand what you are saying but I have not the slightest clue as of how to get there...

Any kind of pseudo-code-skeleton or something would be great!!

I can try. Hang on... (I haven't compiled this code. Things like error checking might be nice... and everything may not be cleaned up properly).

// allocate some memory to hold the offscreen bitmap
// height and width are the height and width you want to use for your
// offscreen (probably the largest size of the thumbnail)
unsigned rowBytes = imageWidth * 4;
void *imageBuffer = malloc(rowBytes * height);
CGRect offscreenRect = CGRectMake(0, 0, height, width);

// Create an offscreen bitmap. First we create a color space (that should
// be compatible with the display?). Then we create a bitmap context on our
// offscreen buffer.
CGColorSpaceRef bitmapColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceUserRGB);
CGContextRef offscreenContext = CGBitmapContextCreate(imageBuffer, width, height, 8, rowBytes, bitmapColorSpace, kCGImageAlphaPremultipliedFirst);

// Using the code you already have, you create a CGImageRef to the JPEG data.
// Let's call that myJPEGImage. In this step you draw that JPEG image into
// the offscreen context:
CGContextClearRect(offscreenContext, offscreenRect);
CGContextDrawImage(offscreenContext, offscreenRect, myJPEGImage);

// Now you could get rid of myJPEGImage since it is buffered on the offscreen
// context.
CGImageRelease(myJPEGImage);
myJPEGImage = NULL;

// Now you've got the JPEG drawn into memory. You can probably also get rid
// of the bitmap contex since we're done drawing into that memory
CGContextRelease(offscreenContext);
offscreenContext = NULL;

// All we're left with is a chunk of memory that holds the image. We need to
// create ANOTHER CGImage that knows how to draw out of that memory. To create
// a CGImage we need a data provider:

CGDataProviderRef memoryDataProvider = CGDataProviderCreateWithData(NULL, imageBuffer, rowBytes * height, NULL);
CGImageRef cachedImage = CGImageCreate(width, height, 8, 32, rowBytes, bitmapColorSpace, kCGImageAlphaPremultipliedFirst, memoryDataProvider, NULL, true, kCGRenderingIntentDefault);
CGDataProviderRelease(memoryDataProvider);

// we're done with the color space as well.
CGColorSpaceRelease(bitmapColorSpace);
bitmapColorSpace = NULL;


At this point you have your cached image (cachedImage in the code) and you can use it to draw to the screen. At some point you will have to release the cached image, and after you have done so you can "free" the image buffer.

If I had to guess, I would say that iPhoto is not using Quartz to draw it's image thumbnails. I suspect that, instead, they are probably using OpenGL and textured quads to get the absolute best performance. Still, you could try the code above and see if it improves the drawing speed of your application enough that you are happy with it.

Scott
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Quartz-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/quartz-dev/email@hidden

This email sent to email@hidden

References: 
 >Questions about CGImage (From: Michael Becker <email@hidden>)
 >Re: Questions about CGImage (From: Scott Thompson <email@hidden>)
 >Re: Questions about CGImage (From: Michael Becker <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.