You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2020/08/19 23:29:15 UTC

[commons-imaging] branch master updated: [IMAGING-264]: use Math.round before casting the image dpi into an int value.

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


The following commit(s) were added to refs/heads/master by this push:
     new 5898b20  [IMAGING-264]: use Math.round before casting the image dpi into an int value.
     new 2b3e71c  Merge pull request #94 from kinow/IMAGING-264
5898b20 is described below

commit 5898b20030d0b5e8d975004f095e8532d13cd1e2
Author: Bruno P. Kinoshita <ki...@apache.org>
AuthorDate: Thu Aug 20 01:40:36 2020 +1200

    [IMAGING-264]: use Math.round before casting the image dpi into an int value.
---
 src/changes/changes.xml                            |   5 ++-
 .../imaging/formats/bmp/BmpImageParser.java        |   4 +-
 .../imaging/formats/bmp/BmpImageParserTest.java    |  48 +++++++++++++++++++++
 .../images/bmp/IMAGING-264/test-72_6-dpi.bmp       | Bin 0 -> 7722 bytes
 4 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 881ea2f..d8ed68d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,7 +45,10 @@ The <action> type attribute can be add,update,fix,remove.
   </properties>
   <body>
     <release version="1.0-alpha3" date="2020-??-??" description="Third 1.0 alpha release">
-      <action issue="IMAGING-263" dev="kinow" type="fix" due-to"Gary Lucas">
+      <action issue="IMAGING-264" dev="kinow" type="fix">
+        BMP Parser physicalWidthDpi and physicalHeightDpi truncated before rounding off.
+      </action>
+      <action issue="IMAGING-263" dev="kinow" type="fix" due-to="Gary Lucas">
         Failure when reading a partial raster from a floating-point TIFF
       </action>
     </release>
diff --git a/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java b/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
index 0604798..09d5600 100644
--- a/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/bmp/BmpImageParser.java
@@ -586,10 +586,10 @@ public class BmpImageParser extends ImageParser {
         // boolean progressive = (fPNGChunkIHDR.InterlaceMethod != 0);
         //
         // pixels per meter
-        final int physicalWidthDpi = (int) (bhi.hResolution * .0254);
+        final int physicalWidthDpi = (int) Math.round(bhi.hResolution * .0254);
         final float physicalWidthInch = (float) ((double) width / (double) physicalWidthDpi);
         // int physicalHeightDpi = 72;
-        final int physicalHeightDpi = (int) (bhi.vResolution * .0254);
+        final int physicalHeightDpi = (int) Math.round(bhi.vResolution * .0254);
         final float physicalHeightInch = (float) ((double) height / (double) physicalHeightDpi);
 
         final String formatDetails = "Bmp (" + (char) bhi.identifier1
diff --git a/src/test/java/org/apache/commons/imaging/formats/bmp/BmpImageParserTest.java b/src/test/java/org/apache/commons/imaging/formats/bmp/BmpImageParserTest.java
new file mode 100644
index 0000000..c5ba974
--- /dev/null
+++ b/src/test/java/org/apache/commons/imaging/formats/bmp/BmpImageParserTest.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.imaging.formats.bmp;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+
+import org.apache.commons.imaging.ImageInfo;
+import org.apache.commons.imaging.ImageReadException;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests for the {@link BmpImageParser}.
+ * @since 1.0-alpha3
+ */
+public class BmpImageParserTest {
+
+    /**
+     * For https://issues.apache.org/jira/browse/IMAGING-264.
+     * @throws IOException 
+     * @throws ImageReadException 
+     */
+    @Test
+    public void testImageWidthRounding() throws ImageReadException, IOException {
+        String file = "/images/bmp/IMAGING-264/test-72_6-dpi.bmp";
+        File bmp = new File(BmpImageParser.class.getResource(file).getFile());
+        final BmpImageParser parser = new BmpImageParser();
+        ImageInfo imageInfo = parser.getImageInfo(bmp, Collections.emptyMap());
+        assertEquals(73, imageInfo.getPhysicalWidthDpi(), "Expected 72.6 resolution to be rounded to 73");
+    }
+}
diff --git a/src/test/resources/images/bmp/IMAGING-264/test-72_6-dpi.bmp b/src/test/resources/images/bmp/IMAGING-264/test-72_6-dpi.bmp
new file mode 100644
index 0000000..d6ee31f
Binary files /dev/null and b/src/test/resources/images/bmp/IMAGING-264/test-72_6-dpi.bmp differ