0

I have an image in the form of an NSURL as input. I converted this url to NSImage and then to NSData from which I could get CGImageRef. This imageRef helped me extracting the raw data information from the image such as the height, width, bytesPerRow, etc.

Here's the code that I used:

NSString * urlName = [url path];
NSImage *image = [[NSImage alloc] initWithContentsOfFile:urlName];
NSData *imageData = [image TIFFRepresentation];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)CFBridgingRetain(imageData), NULL);
CGImageRef imageRef =  CGImageSourceCreateImageAtIndex(source, 0, NULL);
NSUInteger numberOfBitsPerPixel = CGImageGetBitsPerPixel(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
...
...

Now, I checked the size of the image using:

int sz = [imageData length];

which is different from - int sz' = bytesPerRow * height

I cannot understand why is there such a difference. And sz is actually half of sz'.

Am I making some mistake while extracting various info? From what I can get is that maybe while conversion of image to NSData some decompressions are done. In such a case, what should I use that can get me the reliable data.

I am new to the world image processing in Objective-C, so please bear with me!

P.S. I actually checked the size of the file that I am getting as input in the form of NSURL which is same as sz.

Sankalp
  • 2,796
  • 3
  • 30
  • 43

1 Answers1

2

Try This:

Instead of

NSData *imageData = [image TIFFRepresentation];

use this:

NSData *imageData = [image TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:0];
PR Singh
  • 653
  • 5
  • 15
  • Will this decompress the `.tiff` that I have as an input since the problem that I had identified is that the input image is already compressed, so that's the size of the image is coming out to be lesser? – Sankalp Oct 03 '13 at 10:32
  • I tried using this method call for `NSImage`, but this also is maintaining the compressed size. – Sankalp Oct 03 '13 at 10:37
  • you want to archived NSImage to NSData after that viceversa, right? – PR Singh Oct 03 '13 at 10:58
  • Right. I am taking the size of the `NSData` as: `int sz = [imageData length];` which is 330KB. But, it should actually be `1044 * 580 = 605KB` (when decompressed). I think `NSData` is required for this purpose, if you any other method, then let me know. Also, the main focus here is how to decompress the image so that I can get the actual data in the form of `NSData`. – Sankalp Oct 03 '13 at 11:00
  • when we convert NSImage to NSData, image will compress, after that when unarchive image (conversion from NSData to NSImage), will get same image, if you want whole snippet of code I can provide you, try that one, if satisfy then you can use. – PR Singh Oct 03 '13 at 11:08
  • No, the problem at hand is not that the image is getting compressed when converting from NSImage to NSData; it's actually this - the image that I have as an input in the form of `NSURL` is already in compressed form (`.tiff` in this case). So, to extract real information, that image needs to be decompressed. Could you help me out with that problem? – Sankalp Oct 03 '13 at 11:18
  • do you want to convert .tiff image to any other format like png,jpeg..etc.. ? – PR Singh Oct 03 '13 at 12:13
  • No. What I have to do requires decompressed data from the input image, since I have to reproduce it somewhere else. – Sankalp Oct 03 '13 at 12:30