Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » iText »

[iText-questions] [patch] Correctly read DPI value from BMP files

Chad Loder

2007-06-27

Replies:

I believe the bitmap codec has a floating point truncation error that
causes a 300 dpi bitmap to be processed as 299 dpi. A 300 dpi image
specifies an xPelsPerMeter of 11811, which is multiplied by 0.0254
(one inch in meters), yielding an effective DPI of 299.9994. I do
not think it is correct to truncate this when converting to an integer.

The correct method IMHO is to convert the double to a float, then
round the float to an integer. The following diff achieves the correct
results.

Comments?

Index: com/lowagie/text/pdf/codec/BmpImage.java
===================================================================
--- com/lowagie/text/pdf/codec/BmpImage.java  6 Jun 2007 23:09:56 -0000  1.1.1.6
+++ com/lowagie/text/pdf/codec/BmpImage.java  27 Jun 2007 21:36:32 -0000
@@(protected) @@
     BmpImage bmp = new BmpImage(is, noHeader, size);
     try {
        Image img = bmp.getImage();
-        img.setDpi((int)((double)bmp.xPelsPerMeter * 0.0254), (int)((double)bmp.yPelsPerMeter * 0.0254));
+        img.setDpi(
+          Math.round((float)((double)bmp.xPelsPerMeter * 0.0254)),
+          Math.round((float)((double)bmp.yPelsPerMeter * 0.0254)));
+        System.out.println("bmp dpiX is " + img.getDpiX());
        img.setOriginalType(Image.ORIGINAL_BMP);
        return img;
     }

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
iText-questions mailing list
iText-questions@(protected)
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/
©2008 junlu.com - Jax Systems, LLC, U.S.A.