Re: Reading Adobe Photoshop header, Obj-C
Re: Reading Adobe Photoshop header, Obj-C
- Subject: Re: Reading Adobe Photoshop header, Obj-C
- From: Thomas Engelmeier <email@hidden>
- Date: Tue, 28 Jun 2011 21:57:34 +0200
On 28.06.2011, at 21:37, Charles Srstka wrote:
> On Jun 28, 2011, at 12:00 PM, Jens Alfke wrote:
>
>> Another way (that I often use) is to define a C struct that matches the fields and layout of the header, then cast the bytes to a pointer to that struct and read the fields. It makes your code look a lot cleaner, but you have to be careful to (a) make sure the compiler packs the struct fields properly, and (b) you byte-swap all integer/float fields appropriately.
>
> It’s my understanding that reading/writing a struct directly to/from a file is considered poor practice, because of the two things you mentioned: endian issues, and the fact that you don’t know how the compiler will align the fields — and even if you test that, you don’t know that it’s not going to change in some future version of clang or gcc, or that Apple’s not going to switch to a new architecture or something in which the compilers will emit different output.
- the endian issue you'll have to deal with anyway
- regarding struct alignment:
the 80ies file format had tons of of serialized struct data which usually better end up in a struct.
Well, streamed reading has bad performance, so you'd not want to use it in image (or movie) manipulation code.
Streamed reading just to put it back into a struct is another anti-pattern.
The "don't" is to use binary structs for new defined protocols for transfer, for the given reasons.
Regards,
Tom_E
_______________________________________________
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