Code Monkey home page Code Monkey logo

ptoj's Introduction

PtoJ

Testing DPI preservation in Cocoa image transcoding

Issue: DPI not respected when converting PNG to JP2 with NSBitmapImageRep or CGImageDestinationRef

I have a PNG with a DPI of 144. When I load the bits from disk into an NSBitmapImageRep, this is properly recognized: the size.width and pixelsWide values are different.

If I then use representationUsingType:NSJPEGFileType to convert the NSBitmapImageRep to classic JPEG, this info is preserved. If I load the output NSData back into an NSBitmapImageRep or write to disk and open it with Preview.app, I see a DPI of 144. Same thing if I use CGImage to convert to JPEG with CGImageDestinationCreateWithData's kUTTypeJPEG.

However, if I do the same thing but replace NSJPEGFileType and kUTTypeJPEG with NSJPEG2000FileType or kUTTypeJPEG2000 respectively, this doesn't happen. Programmatically I see pixelsWide and size.width as the same value, and Preview.app shows a DPI of 72.

Note that for CGImageDestinationAddImage(), I'm even providing a properties dictionary that sets kCGImagePropertyDPIWidth and kCGImagePropertyDPIHeight.

At first I thought that maybe JP2 just didn't support DPI…but I can edit the image in Preview.app and get it back up to 144 DPI.

Am I missing something, or should I be filing a Radar on this?

I've put a little CLI tool on GitHub to demonstrate this. It takes one command line argument (the input image path), and outputs 4 copies to the same directory as the input:

  • .jpg with NS calls
  • .jpg with CG calls
  • .jp2 with NS calls
  • .jp2 with CG calls

Feed it an image with 144 DPI and you should see the first two come out as 144 DPI, and the last two come out as 72 DPI.

ptoj's People

Contributors

jbrjake avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.