You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/07/04 13:42:55 UTC

[commons-imaging] branch master updated (4a508c68 -> 14635a91)

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

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


    from 4a508c68 Add ByteSource.path(Path)
     new 888ede97 Reuse IOUtils
     new 9ed37712 Use try-with-resources in GifImageParser
     new 90841f17 Use try-with-resources in IcoImageParser
     new 76e367a2 Use Java naming conventions
     new 14635a91 Use input parameter in exception message

The 5 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/imaging/bytesource/ByteSource.java     |   5 +-
 .../commons/imaging/common/BinaryFunctions.java    |   2 +-
 .../imaging/formats/gif/GifImageParser.java        | 288 ++++++++++-----------
 .../imaging/formats/ico/IcoImageParser.java        | 245 +++++++++---------
 4 files changed, 271 insertions(+), 269 deletions(-)


[commons-imaging] 02/05: Use try-with-resources in GifImageParser

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 9ed3771297034741748f8c0d9f4f6405b9eec15c
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 4 09:33:50 2023 -0400

    Use try-with-resources in GifImageParser
---
 .../imaging/formats/gif/GifImageParser.java        | 288 ++++++++++-----------
 1 file changed, 142 insertions(+), 146 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java b/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java
index ce971f82..ba34704f 100644
--- a/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/gif/GifImageParser.java
@@ -955,189 +955,185 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements
         }
         final int paletteSize = palette2.length() + (hasAlpha ? 1 : 0);
 
-        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
+        try (BinaryOutputStream bos = BinaryOutputStream.littleEndian(os)) {
 
-        // write Header
-        os.write(0x47); // G magic numbers
-        os.write(0x49); // I
-        os.write(0x46); // F
+            // write Header
+            os.write(0x47); // G magic numbers
+            os.write(0x49); // I
+            os.write(0x46); // F
 
-        os.write(0x38); // 8 version magic numbers
-        os.write(0x39); // 9
-        os.write(0x61); // a
+            os.write(0x38); // 8 version magic numbers
+            os.write(0x39); // 9
+            os.write(0x61); // a
 
-        // Logical Screen Descriptor.
+            // Logical Screen Descriptor.
 
-        bos.write2Bytes(width);
-        bos.write2Bytes(height);
+            bos.write2Bytes(width);
+            bos.write2Bytes(height);
 
-        final int colorTableScaleLessOne = (paletteSize > 128) ? 7
-                : (paletteSize > 64) ? 6 : (paletteSize > 32) ? 5
-                        : (paletteSize > 16) ? 4 : (paletteSize > 8) ? 3
-                                : (paletteSize > 4) ? 2
-                                        : (paletteSize > 2) ? 1 : 0;
+            final int colorTableScaleLessOne = (paletteSize > 128) ? 7
+                    : (paletteSize > 64) ? 6
+                            : (paletteSize > 32) ? 5
+                                    : (paletteSize > 16) ? 4
+                                            : (paletteSize > 8) ? 3 : (paletteSize > 4) ? 2 : (paletteSize > 2) ? 1 : 0;
 
-        final int colorTableSizeInFormat = 1 << (colorTableScaleLessOne + 1);
-        {
-            final byte colorResolution = (byte) colorTableScaleLessOne; // TODO:
-            final int packedFields = (7 & colorResolution) * 16;
-            bos.write(packedFields); // one byte
-        }
-        {
-            final byte backgroundColorIndex = 0;
-            bos.write(backgroundColorIndex);
-        }
-        {
-            final byte pixelAspectRatio = 0;
-            bos.write(pixelAspectRatio);
-        }
+            final int colorTableSizeInFormat = 1 << (colorTableScaleLessOne + 1);
+            {
+                final byte colorResolution = (byte) colorTableScaleLessOne; // TODO:
+                final int packedFields = (7 & colorResolution) * 16;
+                bos.write(packedFields); // one byte
+            }
+            {
+                final byte backgroundColorIndex = 0;
+                bos.write(backgroundColorIndex);
+            }
+            {
+                final byte pixelAspectRatio = 0;
+                bos.write(pixelAspectRatio);
+            }
 
-        //{
+            // {
             // write Global Color Table.
 
-        //}
-
-        { // ALWAYS write GraphicControlExtension
-            bos.write(EXTENSION_CODE);
-            bos.write((byte) 0xf9);
-            // bos.write(0xff & (kGraphicControlExtension >> 8));
-            // bos.write(0xff & (kGraphicControlExtension >> 0));
-
-            bos.write((byte) 4); // block size;
-            final int packedFields = hasAlpha ? 1 : 0; // transparency flag
-            bos.write((byte) packedFields);
-            bos.write((byte) 0); // Delay Time
-            bos.write((byte) 0); // Delay Time
-            bos.write((byte) (hasAlpha ? palette2.length() : 0)); // Transparent
-            // Color
-            // Index
-            bos.write((byte) 0); // terminator
-        }
+            // }
+
+            { // ALWAYS write GraphicControlExtension
+                bos.write(EXTENSION_CODE);
+                bos.write((byte) 0xf9);
+                // bos.write(0xff & (kGraphicControlExtension >> 8));
+                // bos.write(0xff & (kGraphicControlExtension >> 0));
+
+                bos.write((byte) 4); // block size;
+                final int packedFields = hasAlpha ? 1 : 0; // transparency flag
+                bos.write((byte) packedFields);
+                bos.write((byte) 0); // Delay Time
+                bos.write((byte) 0); // Delay Time
+                bos.write((byte) (hasAlpha ? palette2.length() : 0)); // Transparent
+                // Color
+                // Index
+                bos.write((byte) 0); // terminator
+            }
 
-        if (null != xmpXml) {
-            bos.write(EXTENSION_CODE);
-            bos.write(APPLICATION_EXTENSION_LABEL);
+            if (null != xmpXml) {
+                bos.write(EXTENSION_CODE);
+                bos.write(APPLICATION_EXTENSION_LABEL);
 
-            bos.write(XMP_APPLICATION_ID_AND_AUTH_CODE.length); // 0x0B
-            bos.write(XMP_APPLICATION_ID_AND_AUTH_CODE);
+                bos.write(XMP_APPLICATION_ID_AND_AUTH_CODE.length); // 0x0B
+                bos.write(XMP_APPLICATION_ID_AND_AUTH_CODE);
 
-            final byte[] xmpXmlBytes = xmpXml.getBytes(StandardCharsets.UTF_8);
-            bos.write(xmpXmlBytes);
+                final byte[] xmpXmlBytes = xmpXml.getBytes(StandardCharsets.UTF_8);
+                bos.write(xmpXmlBytes);
 
-            // write "magic trailer"
-            for (int magic = 0; magic <= 0xff; magic++) {
-                bos.write(0xff - magic);
-            }
-
-            bos.write((byte) 0); // terminator
+                // write "magic trailer"
+                for (int magic = 0; magic <= 0xff; magic++) {
+                    bos.write(0xff - magic);
+                }
 
-        }
+                bos.write((byte) 0); // terminator
 
-        { // Image Descriptor.
-            bos.write(IMAGE_SEPARATOR);
-            bos.write2Bytes(0); // Image Left Position
-            bos.write2Bytes(0); // Image Top Position
-            bos.write2Bytes(width); // Image Width
-            bos.write2Bytes(height); // Image Height
+            }
 
-            {
-                final boolean localColorTableFlag = true;
-                // boolean LocalColorTableFlag = false;
-                final boolean interlaceFlag = false;
-                final boolean sortFlag = false;
-                final int sizeOfLocalColorTable = colorTableScaleLessOne;
-
-                // int SizeOfLocalColorTable = 0;
-
-                final int packedFields;
-                if (localColorTableFlag) {
-                    packedFields = (LOCAL_COLOR_TABLE_FLAG_MASK
-                            | (interlaceFlag ? INTERLACE_FLAG_MASK : 0)
-                            | (sortFlag ? SORT_FLAG_MASK : 0)
-                            | (7 & sizeOfLocalColorTable));
-                } else {
-                    packedFields = (0
-                            | (interlaceFlag ? INTERLACE_FLAG_MASK : 0)
-                            | (sortFlag ? SORT_FLAG_MASK : 0)
-                            | (7 & sizeOfLocalColorTable));
+            { // Image Descriptor.
+                bos.write(IMAGE_SEPARATOR);
+                bos.write2Bytes(0); // Image Left Position
+                bos.write2Bytes(0); // Image Top Position
+                bos.write2Bytes(width); // Image Width
+                bos.write2Bytes(height); // Image Height
+
+                {
+                    final boolean localColorTableFlag = true;
+                    // boolean LocalColorTableFlag = false;
+                    final boolean interlaceFlag = false;
+                    final boolean sortFlag = false;
+                    final int sizeOfLocalColorTable = colorTableScaleLessOne;
+
+                    // int SizeOfLocalColorTable = 0;
+
+                    final int packedFields;
+                    if (localColorTableFlag) {
+                        packedFields = (LOCAL_COLOR_TABLE_FLAG_MASK | (interlaceFlag ? INTERLACE_FLAG_MASK : 0)
+                                | (sortFlag ? SORT_FLAG_MASK : 0) | (7 & sizeOfLocalColorTable));
+                    } else {
+                        packedFields = (0 | (interlaceFlag ? INTERLACE_FLAG_MASK : 0) | (sortFlag ? SORT_FLAG_MASK : 0)
+                                | (7 & sizeOfLocalColorTable));
+                    }
+                    bos.write(packedFields); // one byte
                 }
-                bos.write(packedFields); // one byte
             }
-        }
 
-        { // write Local Color Table.
-            for (int i = 0; i < colorTableSizeInFormat; i++) {
-                if (i < palette2.length()) {
-                    final int rgb = palette2.getEntry(i);
+            { // write Local Color Table.
+                for (int i = 0; i < colorTableSizeInFormat; i++) {
+                    if (i < palette2.length()) {
+                        final int rgb = palette2.getEntry(i);
 
-                    final int red = 0xff & (rgb >> 16);
-                    final int green = 0xff & (rgb >> 8);
-                    final int blue = 0xff & (rgb >> 0);
+                        final int red = 0xff & (rgb >> 16);
+                        final int green = 0xff & (rgb >> 8);
+                        final int blue = 0xff & (rgb >> 0);
 
-                    bos.write(red);
-                    bos.write(green);
-                    bos.write(blue);
-                } else {
-                    bos.write(0);
-                    bos.write(0);
-                    bos.write(0);
+                        bos.write(red);
+                        bos.write(green);
+                        bos.write(blue);
+                    } else {
+                        bos.write(0);
+                        bos.write(0);
+                        bos.write(0);
+                    }
                 }
             }
-        }
 
-        { // get Image Data.
+            { // get Image Data.
 //            int image_data_total = 0;
 
-            int lzwMinimumCodeSize = colorTableScaleLessOne + 1;
-            // LZWMinimumCodeSize = Math.max(8, LZWMinimumCodeSize);
-            if (lzwMinimumCodeSize < 2) {
-                lzwMinimumCodeSize = 2;
-            }
+                int lzwMinimumCodeSize = colorTableScaleLessOne + 1;
+                // LZWMinimumCodeSize = Math.max(8, LZWMinimumCodeSize);
+                if (lzwMinimumCodeSize < 2) {
+                    lzwMinimumCodeSize = 2;
+                }
 
-            // TODO:
-            // make
-            // better
-            // choice
-            // here.
-            bos.write(lzwMinimumCodeSize);
-
-            final MyLzwCompressor compressor = new MyLzwCompressor(
-                    lzwMinimumCodeSize, ByteOrder.LITTLE_ENDIAN, false); // GIF
-            // Mode);
-
-            final byte[] imageData = Allocator.byteArray(width * height);
-            for (int y = 0; y < height; y++) {
-                for (int x = 0; x < width; x++) {
-                    final int argb = src.getRGB(x, y);
-                    final int rgb = 0xffffff & argb;
-                    int index;
-
-                    if (hasAlpha) {
-                        final int alpha = 0xff & (argb >> 24);
-                        final int alphaThreshold = 255;
-                        if (alpha < alphaThreshold) {
-                            index = palette2.length(); // is transparent
+                // TODO:
+                // make
+                // better
+                // choice
+                // here.
+                bos.write(lzwMinimumCodeSize);
+
+                final MyLzwCompressor compressor = new MyLzwCompressor(lzwMinimumCodeSize, ByteOrder.LITTLE_ENDIAN,
+                        false); // GIF
+                // Mode);
+
+                final byte[] imageData = Allocator.byteArray(width * height);
+                for (int y = 0; y < height; y++) {
+                    for (int x = 0; x < width; x++) {
+                        final int argb = src.getRGB(x, y);
+                        final int rgb = 0xffffff & argb;
+                        int index;
+
+                        if (hasAlpha) {
+                            final int alpha = 0xff & (argb >> 24);
+                            final int alphaThreshold = 255;
+                            if (alpha < alphaThreshold) {
+                                index = palette2.length(); // is transparent
+                            } else {
+                                index = palette2.getPaletteIndex(rgb);
+                            }
                         } else {
                             index = palette2.getPaletteIndex(rgb);
                         }
-                    } else {
-                        index = palette2.getPaletteIndex(rgb);
-                    }
 
-                    imageData[y * width + x] = (byte) index;
+                        imageData[y * width + x] = (byte) index;
+                    }
                 }
-            }
 
-            final byte[] compressed = compressor.compress(imageData);
-            writeAsSubBlocks(bos, compressed);
+                final byte[] compressed = compressor.compress(imageData);
+                writeAsSubBlocks(bos, compressed);
 //            image_data_total += compressed.length;
-        }
+            }
 
-        // palette2.dump();
+            // palette2.dump();
 
-        bos.write(TERMINATOR_BYTE);
+            bos.write(TERMINATOR_BYTE);
 
-        bos.close();
+        }
         os.close();
     }
 }


[commons-imaging] 05/05: Use input parameter in exception message

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 14635a910dcf1b931254688f6fc2dd1e037df125
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 4 09:42:39 2023 -0400

    Use input parameter in exception message
---
 src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
index 47d11c4a..ff64f36a 100644
--- a/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
+++ b/src/main/java/org/apache/commons/imaging/common/BinaryFunctions.java
@@ -233,7 +233,7 @@ public final class BinaryFunctions {
         try {
             return IOUtils.toByteArray(is, Allocator.check(length));
         } catch (IOException e) {
-            throw new IOException(exception + " length: " + length);
+            throw new IOException(exception + ", name: " + name + ", length: " + length);
         }
     }
 


[commons-imaging] 03/05: Use try-with-resources in IcoImageParser

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 90841f17cd7a0ffa34eb44a1bfb631022289e9ea
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 4 09:37:31 2023 -0400

    Use try-with-resources in IcoImageParser
---
 .../imaging/formats/ico/IcoImageParser.java        | 233 +++++++++++----------
 1 file changed, 119 insertions(+), 114 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
index 520bd045..d47feef3 100644
--- a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
@@ -649,139 +649,144 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
             bitCount = 8;
         }
 
-        final BinaryOutputStream bos = BinaryOutputStream.littleEndian(os);
+        try (BinaryOutputStream bos = BinaryOutputStream.littleEndian(os)) {
 
-        int scanline_size = (bitCount * src.getWidth() + 7) / 8;
-        if ((scanline_size % 4) != 0) {
-            scanline_size += 4 - (scanline_size % 4); // pad scanline to 4 byte
-                                                      // size.
-        }
-        int t_scanline_size = (src.getWidth() + 7) / 8;
-        if ((t_scanline_size % 4) != 0) {
-            t_scanline_size += 4 - (t_scanline_size % 4); // pad scanline to 4
-                                                          // byte size.
-        }
-        final int imageSize = 40 + 4 * (bitCount <= 8 ? (1 << bitCount) : 0)
-                + src.getHeight() * scanline_size + src.getHeight()
-                * t_scanline_size;
-
-        // ICONDIR
-        bos.write2Bytes(0); // reserved
-        bos.write2Bytes(1); // 1=ICO, 2=CUR
-        bos.write2Bytes(1); // count
-
-        // ICONDIRENTRY
-        int iconDirEntryWidth = src.getWidth();
-        int iconDirEntryHeight = src.getHeight();
-        if (iconDirEntryWidth > 255 || iconDirEntryHeight > 255) {
-            iconDirEntryWidth = 0;
-            iconDirEntryHeight = 0;
-        }
-        bos.write(iconDirEntryWidth);
-        bos.write(iconDirEntryHeight);
-        bos.write((bitCount >= 8) ? 0 : (1 << bitCount));
-        bos.write(0); // reserved
-        bos.write2Bytes(1); // color planes
-        bos.write2Bytes(bitCount);
-        bos.write4Bytes(imageSize);
-        bos.write4Bytes(22); // image offset
-
-        // BITMAPINFOHEADER
-        bos.write4Bytes(40); // size
-        bos.write4Bytes(src.getWidth());
-        bos.write4Bytes(2 * src.getHeight());
-        bos.write2Bytes(1); // planes
-        bos.write2Bytes(bitCount);
-        bos.write4Bytes(0); // compression
-        bos.write4Bytes(0); // image size
-        bos.write4Bytes(pixelDensity == null ? 0 : (int) Math.round(pixelDensity.horizontalDensityMetres())); // x pixels per meter
-        bos.write4Bytes(pixelDensity == null ? 0 : (int) Math.round(pixelDensity.horizontalDensityMetres())); // y pixels per meter
-        bos.write4Bytes(0); // colors used, 0 = (1 << bitCount) (ignored)
-        bos.write4Bytes(0); // colors important
-
-        if (palette != null) {
-            for (int i = 0; i < (1 << bitCount); i++) {
-                if (i < palette.length()) {
-                    final int argb = palette.getEntry(i);
-                    bos.write3Bytes(argb);
+            int scanline_size = (bitCount * src.getWidth() + 7) / 8;
+            if ((scanline_size % 4) != 0) {
+                scanline_size += 4 - (scanline_size % 4); // pad scanline to 4 byte
+                                                          // size.
+            }
+            int t_scanline_size = (src.getWidth() + 7) / 8;
+            if ((t_scanline_size % 4) != 0) {
+                t_scanline_size += 4 - (t_scanline_size % 4); // pad scanline to 4
+                                                              // byte size.
+            }
+            final int imageSize = 40 + 4 * (bitCount <= 8 ? (1 << bitCount) : 0) + src.getHeight() * scanline_size
+                    + src.getHeight() * t_scanline_size;
+
+            // ICONDIR
+            bos.write2Bytes(0); // reserved
+            bos.write2Bytes(1); // 1=ICO, 2=CUR
+            bos.write2Bytes(1); // count
+
+            // ICONDIRENTRY
+            int iconDirEntryWidth = src.getWidth();
+            int iconDirEntryHeight = src.getHeight();
+            if (iconDirEntryWidth > 255 || iconDirEntryHeight > 255) {
+                iconDirEntryWidth = 0;
+                iconDirEntryHeight = 0;
+            }
+            bos.write(iconDirEntryWidth);
+            bos.write(iconDirEntryHeight);
+            bos.write((bitCount >= 8) ? 0 : (1 << bitCount));
+            bos.write(0); // reserved
+            bos.write2Bytes(1); // color planes
+            bos.write2Bytes(bitCount);
+            bos.write4Bytes(imageSize);
+            bos.write4Bytes(22); // image offset
+
+            // BITMAPINFOHEADER
+            bos.write4Bytes(40); // size
+            bos.write4Bytes(src.getWidth());
+            bos.write4Bytes(2 * src.getHeight());
+            bos.write2Bytes(1); // planes
+            bos.write2Bytes(bitCount);
+            bos.write4Bytes(0); // compression
+            bos.write4Bytes(0); // image size
+            bos.write4Bytes(pixelDensity == null ? 0 : (int) Math.round(pixelDensity.horizontalDensityMetres())); // x
+                                                                                                                  // pixels
+                                                                                                                  // per
+                                                                                                                  // meter
+            bos.write4Bytes(pixelDensity == null ? 0 : (int) Math.round(pixelDensity.horizontalDensityMetres())); // y
+                                                                                                                  // pixels
+                                                                                                                  // per
+                                                                                                                  // meter
+            bos.write4Bytes(0); // colors used, 0 = (1 << bitCount) (ignored)
+            bos.write4Bytes(0); // colors important
+
+            if (palette != null) {
+                for (int i = 0; i < (1 << bitCount); i++) {
+                    if (i < palette.length()) {
+                        final int argb = palette.getEntry(i);
+                        bos.write3Bytes(argb);
+                        bos.write(0);
+                    } else {
+                        bos.write4Bytes(0);
+                    }
+                }
+            }
+
+            int bitCache = 0;
+            int bitsInCache = 0;
+            final int rowPadding = scanline_size - (bitCount * src.getWidth() + 7) / 8;
+            for (int y = src.getHeight() - 1; y >= 0; y--) {
+                for (int x = 0; x < src.getWidth(); x++) {
+                    final int argb = src.getRGB(x, y);
+                    // Remember there is a relation between having a rgb palette and the bit count, see above comment
+                    if (palette == null) {
+                        if (bitCount == 24) {
+                            bos.write3Bytes(argb);
+                        } else if (bitCount == 32) {
+                            bos.write4Bytes(argb);
+                        }
+                    } else if (bitCount < 8) {
+                        final int rgb = 0xffffff & argb;
+                        final int index = palette.getPaletteIndex(rgb);
+                        bitCache <<= bitCount;
+                        bitCache |= index;
+                        bitsInCache += bitCount;
+                        if (bitsInCache >= 8) {
+                            bos.write(0xff & bitCache);
+                            bitCache = 0;
+                            bitsInCache = 0;
+                        }
+                    } else if (bitCount == 8) {
+                        final int rgb = 0xffffff & argb;
+                        final int index = palette.getPaletteIndex(rgb);
+                        bos.write(0xff & index);
+                    }
+                }
+
+                if (bitsInCache > 0) {
+                    bitCache <<= (8 - bitsInCache);
+                    bos.write(0xff & bitCache);
+                    bitCache = 0;
+                    bitsInCache = 0;
+                }
+
+                for (int x = 0; x < rowPadding; x++) {
                     bos.write(0);
-                } else {
-                    bos.write4Bytes(0);
                 }
             }
-        }
 
-        int bitCache = 0;
-        int bitsInCache = 0;
-        final int rowPadding = scanline_size - (bitCount * src.getWidth() + 7) / 8;
-        for (int y = src.getHeight() - 1; y >= 0; y--) {
-            for (int x = 0; x < src.getWidth(); x++) {
-                final int argb = src.getRGB(x, y);
-                // Remember there is a relation between having a rgb palette and the bit count, see above comment
-                if (palette == null) {
-                    if (bitCount == 24) {
-                        bos.write3Bytes(argb);
-                    } else if (bitCount == 32) {
-                        bos.write4Bytes(argb);
+            final int t_row_padding = t_scanline_size - (src.getWidth() + 7) / 8;
+            for (int y = src.getHeight() - 1; y >= 0; y--) {
+                for (int x = 0; x < src.getWidth(); x++) {
+                    final int argb = src.getRGB(x, y);
+                    final int alpha = 0xff & (argb >> 24);
+                    bitCache <<= 1;
+                    if (alpha == 0) {
+                        bitCache |= 1;
                     }
-                } else if (bitCount < 8) {
-                    final int rgb = 0xffffff & argb;
-                    final int index = palette.getPaletteIndex(rgb);
-                    bitCache <<= bitCount;
-                    bitCache |= index;
-                    bitsInCache += bitCount;
+                    bitsInCache++;
                     if (bitsInCache >= 8) {
                         bos.write(0xff & bitCache);
                         bitCache = 0;
                         bitsInCache = 0;
                     }
-                } else if (bitCount == 8) {
-                    final int rgb = 0xffffff & argb;
-                    final int index = palette.getPaletteIndex(rgb);
-                    bos.write(0xff & index);
                 }
-            }
-
-            if (bitsInCache > 0) {
-                bitCache <<= (8 - bitsInCache);
-                bos.write(0xff & bitCache);
-                bitCache = 0;
-                bitsInCache = 0;
-            }
 
-            for (int x = 0; x < rowPadding; x++) {
-                bos.write(0);
-            }
-        }
-
-        final int t_row_padding = t_scanline_size - (src.getWidth() + 7) / 8;
-        for (int y = src.getHeight() - 1; y >= 0; y--) {
-            for (int x = 0; x < src.getWidth(); x++) {
-                final int argb = src.getRGB(x, y);
-                final int alpha = 0xff & (argb >> 24);
-                bitCache <<= 1;
-                if (alpha == 0) {
-                    bitCache |= 1;
-                }
-                bitsInCache++;
-                if (bitsInCache >= 8) {
+                if (bitsInCache > 0) {
+                    bitCache <<= (8 - bitsInCache);
                     bos.write(0xff & bitCache);
                     bitCache = 0;
                     bitsInCache = 0;
                 }
-            }
 
-            if (bitsInCache > 0) {
-                bitCache <<= (8 - bitsInCache);
-                bos.write(0xff & bitCache);
-                bitCache = 0;
-                bitsInCache = 0;
-            }
-
-            for (int x = 0; x < t_row_padding; x++) {
-                bos.write(0);
+                for (int x = 0; x < t_row_padding; x++) {
+                    bos.write(0);
+                }
             }
         }
-        bos.close();
     }
 }


[commons-imaging] 04/05: Use Java naming conventions

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 76e367a28aa0c1741b884cdf37333a22f249ed7e
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 4 09:38:36 2023 -0400

    Use Java naming conventions
---
 .../imaging/formats/ico/IcoImageParser.java        | 34 +++++++++++-----------
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
index d47feef3..8f180d5e 100644
--- a/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/ico/IcoImageParser.java
@@ -475,15 +475,15 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
         // display the icon on a < 32 BPP screen. But it's still used instead of
         // alpha
         // if the image would be completely transparent with alpha...
-        int t_scanline_size = (width + 7) / 8;
-        if ((t_scanline_size % 4) != 0) {
-            t_scanline_size += 4 - (t_scanline_size % 4); // pad scanline to 4
+        int tScanlineSize = (width + 7) / 8;
+        if ((tScanlineSize % 4) != 0) {
+            tScanlineSize += 4 - (tScanlineSize % 4); // pad scanline to 4
                                                           // byte size.
         }
-        final int colorMapSizeBytes = t_scanline_size * (height / 2);
+        final int colorMapSizeBytes = tScanlineSize * (height / 2);
         byte[] transparencyMap = null;
         try {
-            transparencyMap = readBytes("transparency_map",
+            transparencyMap = readBytes("transparencyMap",
                     bmpInputStream, colorMapSizeBytes,
                     "Not a Valid ICO File");
         } catch (final IOException ioEx) {
@@ -511,7 +511,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
                 for (int x = 0; x < resultImage.getWidth(); x++) {
                     int alpha = 0xff;
                     if (transparencyMap != null) {
-                        final int alphaByte = 0xff & transparencyMap[t_scanline_size
+                        final int alphaByte = 0xff & transparencyMap[tScanlineSize
                                 * (bmpImage.getHeight() - y - 1) + (x / 8)];
                         alpha = 0x01 & (alphaByte >> (7 - (x % 8)));
                         alpha = (alpha == 0) ? 0xff : 0x00;
@@ -651,18 +651,18 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
 
         try (BinaryOutputStream bos = BinaryOutputStream.littleEndian(os)) {
 
-            int scanline_size = (bitCount * src.getWidth() + 7) / 8;
-            if ((scanline_size % 4) != 0) {
-                scanline_size += 4 - (scanline_size % 4); // pad scanline to 4 byte
+            int scanlineSize = (bitCount * src.getWidth() + 7) / 8;
+            if ((scanlineSize % 4) != 0) {
+                scanlineSize += 4 - (scanlineSize % 4); // pad scanline to 4 byte
                                                           // size.
             }
-            int t_scanline_size = (src.getWidth() + 7) / 8;
-            if ((t_scanline_size % 4) != 0) {
-                t_scanline_size += 4 - (t_scanline_size % 4); // pad scanline to 4
+            int tScanlineSize = (src.getWidth() + 7) / 8;
+            if ((tScanlineSize % 4) != 0) {
+                tScanlineSize += 4 - (tScanlineSize % 4); // pad scanline to 4
                                                               // byte size.
             }
-            final int imageSize = 40 + 4 * (bitCount <= 8 ? (1 << bitCount) : 0) + src.getHeight() * scanline_size
-                    + src.getHeight() * t_scanline_size;
+            final int imageSize = 40 + 4 * (bitCount <= 8 ? (1 << bitCount) : 0) + src.getHeight() * scanlineSize
+                    + src.getHeight() * tScanlineSize;
 
             // ICONDIR
             bos.write2Bytes(0); // reserved
@@ -718,7 +718,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
 
             int bitCache = 0;
             int bitsInCache = 0;
-            final int rowPadding = scanline_size - (bitCount * src.getWidth() + 7) / 8;
+            final int rowPadding = scanlineSize - (bitCount * src.getWidth() + 7) / 8;
             for (int y = src.getHeight() - 1; y >= 0; y--) {
                 for (int x = 0; x < src.getWidth(); x++) {
                     final int argb = src.getRGB(x, y);
@@ -759,7 +759,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
                 }
             }
 
-            final int t_row_padding = t_scanline_size - (src.getWidth() + 7) / 8;
+            final int tRowPadding = tScanlineSize - (src.getWidth() + 7) / 8;
             for (int y = src.getHeight() - 1; y >= 0; y--) {
                 for (int x = 0; x < src.getWidth(); x++) {
                     final int argb = src.getRGB(x, y);
@@ -783,7 +783,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> {
                     bitsInCache = 0;
                 }
 
-                for (int x = 0; x < t_row_padding; x++) {
+                for (int x = 0; x < tRowPadding; x++) {
                     bos.write(0);
                 }
             }


[commons-imaging] 01/05: Reuse IOUtils

Posted by gg...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 888ede979fe9d784ca9f5fc6d90f22d54783775a
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jul 4 09:29:06 2023 -0400

    Reuse IOUtils
---
 src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
index f3e65796..4aeee8c8 100644
--- a/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
+++ b/src/main/java/org/apache/commons/imaging/bytesource/ByteSource.java
@@ -23,6 +23,7 @@ import java.nio.file.Path;
 import java.util.Objects;
 
 import org.apache.commons.imaging.common.BinaryFunctions;
+import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.build.AbstractOrigin;
 import org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin;
 import org.apache.commons.io.build.AbstractOrigin.FileOrigin;
@@ -78,8 +79,8 @@ public class ByteSource {
             BinaryFunctions.skipBytes(is, start);
             succeeded = true;
         } finally {
-            if (!succeeded && is != null) {
-                is.close();
+            if (!succeeded) {
+                IOUtils.close(is);
             }
         }
         return is;