• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: I need float resolution bitmap data from a JPG using CIImage or similar helper
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: I need float resolution bitmap data from a JPG using CIImage or similar helper


  • Subject: Re: I need float resolution bitmap data from a JPG using CIImage or similar helper
  • From: Steve Christensen <email@hidden>
  • Date: Tue, 01 Jun 2010 15:37:51 -0700

It seems like you're jumping through a lot of hoops. If you picked up where David left off, you could do something like this. (Typed in Mail so YMMV.)

CGImageSourceRef source = CGImageSourceCreateWithURL(urlToFile, (CFDictionaryRef)[NSDictionary dictionaryWithObject:[NSNumber numberWithBOOL:YES] forKey:(id)kCGImageSourceShouldAllowFloat]);
CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0);
NSUInteger width = CGImageGetWidth(image);
NSUInteger height = CGImageGetHeight(image);
NSUInteger bytesPerRow = 4 * sizeof(float) * width;
float* rawData = (float*)malloc(bytesPerRow * height);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rawData, width, height, 32, bytesPerRow, colorspace, kCGImageAlphaNoneSkipLast | kCGBitmapFloatComponents);
CGRect bounds = CGRectMake(0, 0, width, height);


CGContextDrawImage(context, bounds, image);

CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
CGImageRelease(image);
CFRelease(source);


On Jun 1, 2010, at 1:04 PM, Robert Lorentz wrote:

On Jun 1, 2010, at 2:16 PM, David Duncan wrote:

On May 31, 2010, at 5:40 PM, Robert Lorentz wrote:

What I REALLY want to achieve (this can be poor performance): take a JPG (or PNG is great too) file on disk, result with a float *bytes; array holding the floating point precision data from the JPG as R G B A format. Anything that achieves that is a solution here - my current route is to use CIImage just because loading up JPG is so easy with it.

You can use ImageIO to do this, just ask for Floating point precision from your images. This is something that you set with the image source options. Something like this (error checking and mem management missing):

CGImageSourceRef src = CGImageSourceCreateWithURL(urlToFile, (CFDictionaryRef)[NSDictionary dictionaryWithObject:[NSNumber numberWithBOOL:YES] forKey:(id)kCGImageSourceShouldAllowFloat]);
CGImageRef img = CGImageSourceCreateAtIndex(src, 0);



OK This looks more along the lines of what I want to be doing, as opposed to having to create contexts and draw in to them to get the data, which is awkward and seems very backwards. I follow you as far as creating the CGImageRef -- yes, I see how I pull in an image file and dictate to read data in as float as opposed to byte-length integers....


However, what do I do with img?? I don't see anything in any of the CG* documentation for me getting an array of floats "out of" a CGImageRef. What would the next step be for me to access this data? Or are you simply suggesting this is another way that I can get a JPG in to a data type to then write out to a context?

I'm sorry if I didn't mention this before but, while I've read as deeply as I can in the docs, I am completely new to cocoa and even more new to CG/CI/Quartz/etc

// create a CG context, using my float array to hold it's data. Therefore, IF I draw to it
// correctly, I should get rawData filled with the bitmap float data of my CIImage
CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGBitmapFloatComponents);

Are you sure the context was actually created? Specifically for float contexts, you need the kCGImageAlpha[Premultilied | None]Last flag as well.

I guess it's being created; CGBitmapContextCreate documentation states for return value I either get a bitmap context or NULL if it can't be created... I do get a valid pointer returned that I'm able to use. When using KCGImageAlphaPremultipliedLast I get better results .. maybe? Also I'm now using the NSGraphicsContext code suggested by Jens


00 00 00 00 80 00 00 00 00 00 00 00 00 00 00 00

^^ is repeated for my entire block of memory, after I draw my image on to the context. Definitely not the data I'm expecting.



..My code now looks like:

[.. load up and display CIImage myImage so I can verify it's correct]

CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGBitmapFloatComponents | kCGImageAlphaPremultipliedLast );
NSGraphicsContext *nsContext = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped: NO];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext: nsContext];

// Should be, and certainly seems to be, drawing to the context created above, with data stored in rawData
[myImage drawInRect:[self bounds] fromRect:[self bounds] operation:NSCompositeSourceIn fraction:1.0];

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >I need float resolution bitmap data from a JPG using CIImage or similar helper (From: Robert Lorentz <email@hidden>)
 >Re: I need float resolution bitmap data from a JPG using CIImage or similar helper (From: David Duncan <email@hidden>)
 >Re: I need float resolution bitmap data from a JPG using CIImage or similar helper (From: Robert Lorentz <email@hidden>)

  • Prev by Date: Re: -[NSDocument fileURL] is observable (KVO). Documented?
  • Next by Date: Re: Monster memory leak and I can't figure out why
  • Previous by thread: Re: I need float resolution bitmap data from a JPG using CIImage or similar helper
  • Next by thread: Monster memory leak and I can't figure out why
  • Index(es):
    • Date
    • Thread