Re: determine image type
Re: determine image type
- Subject: Re: determine image type
- From: Robert Cerny <email@hidden>
- Date: Thu, 5 Oct 2006 13:35:24 +0200
Great,
that's it.
Thanks
Robert
On 5.10.2006, at 12:02, Heinrich Giesen wrote:
Hello,
On 04.10.2006, at 18:07, Robert Cerny wrote:
I have a lot of files from MacOS 9 on the server, but they doesn't
contain file extensions nor have correct file type in the header. I
know that it's a mix of jpg, psd and tiff files and I'm able to
create icons for them. it means I can create a NSImage of the data.
How can I find which type of NSImage I have?
Let us assume you create an image with:
NSData *imgData = [[NSData alloc] initWithContentsOfFile:filePath];
NSImage *img = [[NSImage alloc] initWithData:imgData];
Now position to the contents of imgData:
const char *buffer = [imgData bytes];
and check the first bytes of the buffer, i.e. check the magic numbers.
(some examples):
BOOL isJPEG = (buffer[0]==0xFF) && (buffer[1]==0xD8) && (buffer[2]
==0xFF);
BOOL isLitteEndianTIFF = (buffer[0]=='I') && (buffer[1]=='I');
BOOL isBigEndianTIFF = (buffer[0]=='M') && (buffer[1]=='M');
BOOL isGIF = (buffer[0]=='G') && (buffer[1]=='I') && (buffer[2]=='F');
BOOL isBMP = (buffer[0]=='B') && (buffer[1]=='M');
BOOL isPSD = (buffer[0]=='8') && (buffer[1]=='B') && (buffer[2]
=='P') && (buffer[3]=='S');
BOOL isPNG = (buffer[0]==0x89) && (buffer[1]=='P') && (buffer[2]
=='N') && (buffer[3]=='G')
&& (buffer[4]==0x0d) && (buffer[5]==0x0a) && (buffer[6]==0x1a)
&& (buffer[7]==0x0a);
NSImage and NSImageRep do the same to identify the image type.
Good luck
Heinrich
--
Heinrich Giesen
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden