How to make bitmaps at 300 dpi ?

Tuesday, December 09 2003 @ 09:18 AM UTC

Contributed by: cocoa

I am trying to get a bitmap preview of a vector based file, but I always get a bitmap @ 72 dpi, how to do to get an image at a higher resolution ?

NSImage doesn't have a notion of resolution per se. The "resolution" is deducted from the relationship between the size (of which units are conventional to the program manipulating it) of the NSImage and the number of pixels in the NSBitmapImageRep.
You may better understand with a small example:
first decide the units used by your NSImage: let's says you are using inches. The use wants an 8" wide image at 300 dpi. Then looks at your vector image format and try to discover what is its unit system. If you are manipulating Postscript files (PDF, EPS, PS, …) then the unit is the typographic dot and there are 72 typo-dots per inch. (Not exactly but the rounding has always been taken for this kind of calculation).
Here is the source of confusion: people forget that the 72 dpi comes for the definition of the typographic dot and think it is related to the "screen" resolution. In fact, it is the opposite, the original Mac had a screen resolution of 72 dpi to match the typographic dot size... Note that now screens have all kind of resolution, generally 96 dpi…, but we don't care here, the actual screen resolution is only of interest for those needing to present to the end user a drawing scaled at a precise factor, so that at 100% an 1 inch-size drawing will be physically one inch on screen...

If the original image reports a width of 634 dots, this is equivalent to 634.0 / 72.0 = 8.8056 inches or 8.8056 * 300 = 2,642 pixels. This means you need a NSImage with a width of 634.0 (use setSize to define the size of a NSImage) and containing an NSBitmapImageRep with a width of 2,642 pixels. (Do the same calulation for the height, of course).
What if you are using centimeters instead of inches ?
Well the calculation will be same (taken into account that one inch is 2.54 centimeters) until you come to draw your image on screen in a NSView. Then you will have to do the same calculation taking into account both the relationship between centimeters and inches and the zoom factor (if you allow the end user to zoom in the image) to know what would be the NSSize of the enclosing view that will display the original image (you don't change the size of the NSImage at this stage but the size of the rectangle in which to draw it).
When working with inches, one easely forget this calculation at this step, because of course the unit factor is 1.0 and nobody takes the time to write * 1.0 or / 1.0...
And if you want the drawing to be "real" scale on screen, it is here you have to taken into account the hardware resolution of the output device… (if the screen has a resolution of 96 dpi you will have to multiply the size of the view by 96.0 / 72.0 = 1.3333, not that doing this will probably makes you image looks a little bit blurred on screen).