posted April 21st, 2008

Naked light had a slight error with JPEGs saved with a DPI other than 72.

Naked light would handle the sizes gracefully, and in fact, used the right DPI setting to determine the size of your image. For example, a 300 DPI, 600 pixel-wide image would open up 2 inches wide. A 96 DPI, 600 pixel-wide image would be 6.25 inches wide. But when you actually looked at the image, it was plain to see that something was amiss:

The top image is a 600 × 375 pixel JPEG image saved from Photoshop via Save For Web…. The bottom is a 600 × 375, 300 DPI JPEG image saved via Save As…. Save For Web… strips all DPI information, whereas Save As… does not. Both images have identical pixel data, but Naked light chose to skewer the one with the DPI set.

Turns out this is a really common trap in Apple’s NSImage and NSImageRep classes that I’ve run into at least once before. 99% of the time, sending objects of these classes a size message returns the pixel size.

But this is purely coincidental: it actually returns the point size 100% of the time. It just so happens that most of the time, one pixel is one point. When it’s not, Naked light would incorrectly upload a smaller texture (in this case, ignoring 76% of the pixel data).

The fix is, of course, incredibly simple:

NSImageRep *rep = [image bestRepresentationForDevice: nil];
[image setSize: NSMakeSize([rep pixelsWide], [rep pixelsHigh])];

Leave a Reply