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
The following commit(s) were added to refs/heads/master by this push: new 466ff568 Use Java 8 APIs 466ff568 is described below commit 466ff5689c36a4f20c244a9f0136fefc9f4dec32 Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Wed Oct 5 21:23:25 2022 -0400 Use Java 8 APIs --- .github/workflows/maven.yml | 100 ++++++++++----------- .../commons/imaging/common/ByteConversions.java | 17 ++-- .../imaging/formats/dcx/DcxImageParser.java | 21 ++--- .../imaging/formats/gif/GifImageParser.java | 12 +-- .../imaging/formats/icns/IcnsImageParser.java | 8 +- .../imaging/formats/ico/IcoImageParser.java | 6 +- .../imaging/formats/jpeg/decoder/JpegDecoder.java | 11 ++- .../imaging/formats/pcx/PcxImageParser.java | 6 +- .../imaging/formats/png/chunks/PngChunkPlte.java | 5 +- .../formats/psd/dataparsers/DataParserIndexed.java | 11 +-- .../commons/imaging/formats/tiff/TiffField.java | 25 ++---- .../commons/imaging/formats/tiff/TiffReader.java | 3 +- .../formats/tiff/fieldtypes/FieldTypeDouble.java | 17 ++-- .../formats/tiff/fieldtypes/FieldTypeFloat.java | 3 +- .../formats/tiff/fieldtypes/FieldTypeRational.java | 14 +-- .../PhotometricInterpreterPalette.java | 22 ++--- .../formats/tiff/write/ImageDataOffsets.java | 12 +-- .../formats/tiff/write/TiffImageWriterBase.java | 13 ++- .../formats/tiff/write/TiffOutputDirectory.java | 5 +- .../commons/imaging/palette/ColorSpaceSubset.java | 5 +- 20 files changed, 127 insertions(+), 189 deletions(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 7c505f2c..0f98d91c 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -1,50 +1,50 @@ -# 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. - -name: Java CI - -on: [push, pull_request] - -permissions: - contents: read - -jobs: - build: - runs-on: ${{ matrix.os }} - continue-on-error: ${{ matrix.experimental }} - strategy: - matrix: - os: [ubuntu-latest, windows-latest] - java: [ 8, 11, 17 ] - experimental: [false] -# include: -# - java: 18-ea -# os: ubuntu-latest -# experimental: true - - steps: - # https://github.com/actions/checkout/issues/226#issuecomment-854736025 - - name: Prepare git - run: |- - git config --global core.autocrlf false - git config --global core.eol lf - - uses: actions/checkout@v3.1.0 - - name: Set up JDK ${{ matrix.java }} - uses: actions/setup-java@v3.5.1 - with: - distribution: 'temurin' - java-version: ${{ matrix.java }} - - name: Build with Maven - run: mvn -V --no-transfer-progress +# 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. + +name: Java CI + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + build: + runs-on: ${{ matrix.os }} + continue-on-error: ${{ matrix.experimental }} + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + java: [ 8, 11, 17 ] + experimental: [false] +# include: +# - java: 18-ea +# os: ubuntu-latest +# experimental: true + + steps: + # https://github.com/actions/checkout/issues/226#issuecomment-854736025 + - name: Prepare git + run: |- + git config --global core.autocrlf false + git config --global core.eol lf + - uses: actions/checkout@v3.1.0 + - name: Set up JDK ${{ matrix.java }} + uses: actions/setup-java@v3.5.1 + with: + distribution: 'temurin' + java-version: ${{ matrix.java }} + - name: Build with Maven + run: mvn -V --no-transfer-progress diff --git a/src/main/java/org/apache/commons/imaging/common/ByteConversions.java b/src/main/java/org/apache/commons/imaging/common/ByteConversions.java index 043352c9..c2b07bb4 100644 --- a/src/main/java/org/apache/commons/imaging/common/ByteConversions.java +++ b/src/main/java/org/apache/commons/imaging/common/ByteConversions.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.common; import java.nio.ByteOrder; +import java.util.Arrays; /** * Convenience methods for converting data types to and from @@ -244,9 +245,7 @@ public final class ByteConversions { private static int[] toUInt16s(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { final int[] result = new int[length / 2]; - for (int i = 0; i < result.length; i++) { - result[i] = toUInt16(bytes, offset + 2 * i, byteOrder); - } + Arrays.setAll(result, i -> toUInt16(bytes, offset + 2 * i, byteOrder)); return result; } @@ -272,9 +271,7 @@ public final class ByteConversions { private static int[] toInts(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { final int[] result = new int[length / 4]; - for (int i = 0; i < result.length; i++) { - result[i] = toInt(bytes, offset + 4 * i, byteOrder); - } + Arrays.setAll(result, i -> toInt(bytes, offset + 4 * i, byteOrder)); return result; } @@ -342,9 +339,7 @@ public final class ByteConversions { private static double[] toDoubles(final byte[] bytes, final int offset, final int length, final ByteOrder byteOrder) { final double[] result = new double[length / 8]; - for (int i = 0; i < result.length; i++) { - result[i] = toDouble(bytes, offset + 8 * i, byteOrder); - } + Arrays.setAll(result, i -> toDouble(bytes, offset + 8 * i, byteOrder)); return result; } @@ -403,9 +398,7 @@ public final class ByteConversions { final ByteOrder byteOrder, final boolean unsignedType) { final RationalNumber[] result = new RationalNumber[length / 8]; - for (int i = 0; i < result.length; i++) { - result[i] = toRational(bytes, offset + 8 * i, byteOrder, unsignedType); - } + Arrays.setAll(result, i -> toRational(bytes, offset + 8 * i, byteOrder, unsignedType)); return result; } } diff --git a/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java b/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java index f818f814..b5447775 100644 --- a/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/dcx/DcxImageParser.java @@ -128,8 +128,7 @@ public class DcxImageParser extends ImageParser<PcxImagingParameters> { final int id = read4Bytes("Id", is, "Not a Valid DCX File", getByteOrder()); final List<Long> pageTable = new ArrayList<>(1024); for (int i = 0; i < 1024; i++) { - final long pageOffset = 0xFFFFffffL & read4Bytes("PageTable", is, - "Not a Valid DCX File", getByteOrder()); + final long pageOffset = 0xFFFFffffL & read4Bytes("PageTable", is, "Not a Valid DCX File", getByteOrder()); if (pageOffset == 0) { break; } @@ -137,20 +136,13 @@ public class DcxImageParser extends ImageParser<PcxImagingParameters> { } if (id != DcxHeader.DCX_ID) { - throw new ImageReadException( - "Not a Valid DCX File: file id incorrect"); + throw new ImageReadException("Not a Valid DCX File: file id incorrect"); } if (pageTable.size() == 1024) { - throw new ImageReadException( - "DCX page table not terminated by zero entry"); - } - - final Object[] objects = pageTable.toArray(); - final long[] pages = new long[objects.length]; - for (int i = 0; i < objects.length; i++) { - pages[i] = ((Long) objects[i]); + throw new ImageReadException("DCX page table not terminated by zero entry"); } + final long[] pages = pageTable.stream().mapToLong(Long::longValue).toArray(); return new DcxHeader(id, pages); } } @@ -166,10 +158,7 @@ public class DcxImageParser extends ImageParser<PcxImagingParameters> { public final BufferedImage getBufferedImage(final ByteSource byteSource, final PcxImagingParameters params) throws ImageReadException, IOException { final List<BufferedImage> list = getAllBufferedImages(byteSource); - if (list.isEmpty()) { - return null; - } - return list.get(0); + return list.isEmpty() ? null : list.get(0); } @Override 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 b08ede02..36a6678e 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 @@ -1106,18 +1106,18 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements continue; } - final byte[] GIF_MAGIC_TRAILER = new byte[256]; + final byte[] gifMagicTrailer = new byte[256]; for (int magic = 0; magic <= 0xff; magic++) { - GIF_MAGIC_TRAILER[magic] = (byte) (0xff - magic); + gifMagicTrailer[magic] = (byte) (0xff - magic); } if (blockBytes.length < XMP_APPLICATION_ID_AND_AUTH_CODE.length - + GIF_MAGIC_TRAILER.length) { + + gifMagicTrailer.length) { continue; } if (!compareBytes(blockBytes, blockBytes.length - - GIF_MAGIC_TRAILER.length, GIF_MAGIC_TRAILER, 0, - GIF_MAGIC_TRAILER.length)) { + - gifMagicTrailer.length, gifMagicTrailer, 0, + gifMagicTrailer.length)) { throw new ImageReadException( "XMP block in GIF missing magic trailer."); } @@ -1127,7 +1127,7 @@ public class GifImageParser extends ImageParser<GifImagingParameters> implements blockBytes, XMP_APPLICATION_ID_AND_AUTH_CODE.length, blockBytes.length - - (XMP_APPLICATION_ID_AND_AUTH_CODE.length + GIF_MAGIC_TRAILER.length), + - (XMP_APPLICATION_ID_AND_AUTH_CODE.length + gifMagicTrailer.length), StandardCharsets.UTF_8); result.add(xml); } diff --git a/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java b/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java index 659dc171..14556ce9 100644 --- a/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/icns/IcnsImageParser.java @@ -146,6 +146,7 @@ public class IcnsImageParser extends ImageParser<IcnsImagingParameters> { } static class IcnsElement { + static final IcnsElement[] EMPTY_ARRAY = {}; public final int type; public final int elementSize; public final byte[] data; @@ -208,12 +209,7 @@ public class IcnsImageParser extends ImageParser<IcnsImagingParameters> { remainingSize -= icnsElement.elementSize; } - final IcnsElement[] icnsElements = new IcnsElement[icnsElementList.size()]; - for (int i = 0; i < icnsElements.length; i++) { - icnsElements[i] = icnsElementList.get(i); - } - - return new IcnsContents(icnsHeader, icnsElements); + return new IcnsContents(icnsHeader, icnsElementList.toArray(IcnsElement.EMPTY_ARRAY)); } } 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 2fbd81c4..982536ad 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 @@ -590,11 +590,7 @@ public class IcoImageParser extends ImageParser<IcoImagingParameters> { final FileHeader fileHeader = contents.fileHeader; final List<BufferedImage> result = new ArrayList<>(fileHeader.iconCount); for (int i = 0; i < fileHeader.iconCount; i++) { - final IconData iconData = contents.iconDatas[i]; - - final BufferedImage image = iconData.readBufferedImage(); - - result.add(image); + result.add(contents.iconDatas[i].readBufferedImage()); } return result; diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java index b7db94f8..cc35e7e9 100644 --- a/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java +++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java @@ -106,9 +106,7 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { final int yMCUs = (sofnSegment.height + vSize - 1) / vSize; final Block[] mcu = allocateMCUMemory(); final Block[] scaledMCU = new Block[mcu.length]; - for (int i = 0; i < scaledMCU.length; i++) { - scaledMCU[i] = new Block(hSize, vSize); - } + Arrays.setAll(scaledMCU, i -> new Block(hSize, vSize)); final int[] preds = new int[sofnSegment.numberOfComponents]; ColorModel colorModel; WritableRaster raster; @@ -249,10 +247,11 @@ public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor { + table.destinationIdentifier); } quantizationTables[table.destinationIdentifier] = table; - final int[] quantizationMatrixInt = new int[64]; + final int mSize = 64; + final int[] quantizationMatrixInt = new int[mSize]; ZigZag.zigZagToBlock(table.getElements(), quantizationMatrixInt); - final float[] quantizationMatrixFloat = new float[64]; - for (int j = 0; j < 64; j++) { + final float[] quantizationMatrixFloat = new float[mSize]; + for (int j = 0; j < mSize; j++) { quantizationMatrixFloat[j] = quantizationMatrixInt[j]; } Dct.scaleDequantizationMatrix(quantizationMatrixFloat); diff --git a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java index a868a3b4..5e6ff79b 100644 --- a/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java +++ b/src/main/java/org/apache/commons/imaging/formats/pcx/PcxImageParser.java @@ -262,12 +262,10 @@ public class PcxImageParser extends ImageParser<PcxImagingParameters> { final int hDpi = toUInt16(pcxHeaderBytes, 12, getByteOrder()); final int vDpi = toUInt16(pcxHeaderBytes, 14, getByteOrder()); final int[] colormap = new int[16]; - for (int i = 0; i < 16; i++) { - colormap[i] = 0xff000000 + Arrays.setAll(colormap, i -> 0xff000000 | ((0xff & pcxHeaderBytes[16 + 3 * i]) << 16) | ((0xff & pcxHeaderBytes[16 + 3 * i + 1]) << 8) - | (0xff & pcxHeaderBytes[16 + 3 * i + 2]); - } + | (0xff & pcxHeaderBytes[16 + 3 * i + 2])); final int reserved = 0xff & pcxHeaderBytes[64]; final int nPlanes = 0xff & pcxHeaderBytes[65]; final int bytesPerLine = toUInt16(pcxHeaderBytes, 66, getByteOrder()); diff --git a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java index 8bc2d32c..b62dff30 100644 --- a/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java +++ b/src/main/java/org/apache/commons/imaging/formats/png/chunks/PngChunkPlte.java @@ -20,6 +20,7 @@ import static org.apache.commons.imaging.common.BinaryFunctions.readByte; import java.io.ByteArrayInputStream; import java.io.IOException; +import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.formats.png.GammaCorrection; @@ -77,9 +78,7 @@ public class PngChunkPlte extends PngChunk { // } public void correct(final GammaCorrection gammaCorrection) { - for (int i = 0; i < rgb.length; i++) { - rgb[i] = gammaCorrection.correctARGB(rgb[i]); - } + Arrays.setAll(rgb, i -> gammaCorrection.correctARGB(rgb[i])); } } diff --git a/src/main/java/org/apache/commons/imaging/formats/psd/dataparsers/DataParserIndexed.java b/src/main/java/org/apache/commons/imaging/formats/psd/dataparsers/DataParserIndexed.java index da06f455..aae9f58f 100644 --- a/src/main/java/org/apache/commons/imaging/formats/psd/dataparsers/DataParserIndexed.java +++ b/src/main/java/org/apache/commons/imaging/formats/psd/dataparsers/DataParserIndexed.java @@ -16,6 +16,8 @@ */ package org.apache.commons.imaging.formats.psd.dataparsers; +import java.util.Arrays; + import org.apache.commons.imaging.formats.psd.PsdImageContents; public class DataParserIndexed extends DataParser { @@ -23,17 +25,16 @@ public class DataParserIndexed extends DataParser { public DataParserIndexed(final byte[] colorModeData) { colorTable = new int[256]; - for (int i = 0; i < 256; i++) { + Arrays.setAll(colorTable, i -> { final int red = 0xff & colorModeData[0 * 256 + i]; final int green = 0xff & colorModeData[1 * 256 + i]; final int blue = 0xff & colorModeData[2 * 256 + i]; final int alpha = 0xff; - final int rgb = ((0xff & alpha) << 24) | ((0xff & red) << 16) + // return RGB + return ((0xff & alpha) << 24) | ((0xff & red) << 16) | ((0xff & green) << 8) | ((0xff & blue) << 0); - - colorTable[i] = rgb; - } + }); } @Override diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java index c1ab6f28..6fee68ef 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffField.java @@ -22,6 +22,7 @@ import java.io.StringWriter; import java.nio.ByteOrder; import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; import java.util.Locale; import java.util.logging.Level; @@ -449,17 +450,13 @@ public class TiffField { if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; final int[] result = new int[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = numbers[i].intValue(); - } + Arrays.setAll(result, i -> numbers[i].intValue()); return result; } if (o instanceof short[]) { final short[] numbers = (short[]) o; final int[] result = new int[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = 0xffff & numbers[i]; - } + Arrays.setAll(result, i -> 0xffff & numbers[i]); return result; } if (o instanceof int[]) { @@ -485,33 +482,25 @@ public class TiffField { if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; final double[] result = new double[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = numbers[i].doubleValue(); - } + Arrays.setAll(result, i -> numbers[i].doubleValue()); return result; } if (o instanceof short[]) { final short[] numbers = (short[]) o; final double[] result = new double[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = numbers[i]; - } + Arrays.setAll(result, i -> numbers[i]); return result; } if (o instanceof int[]) { final int[] numbers = (int[]) o; final double[] result = new double[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = numbers[i]; - } + Arrays.setAll(result, i -> numbers[i]); return result; } if (o instanceof float[]) { final float[] numbers = (float[]) o; final double[] result = new double[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - result[i] = numbers[i]; - } + Arrays.setAll(result, i -> numbers[i]); return result; } if (o instanceof double[]) { diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java index 8bca029a..3cc59096 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/TiffReader.java @@ -418,8 +418,7 @@ public class TiffReader extends BinaryFileParser { final ByteSourceFile bsf = (ByteSourceFile) byteSource; for (int i = 0; i < elements.size(); i++) { final TiffDirectory.ImageDataElement element = elements.get(i); - data[i] = new TiffImageData.ByteSourceData(element.offset, - element.length, bsf); + data[i] = new TiffImageData.ByteSourceData(element.offset, element.length, bsf); } } else { for (int i = 0; i < elements.size(); i++) { diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java index a8c6ba71..61eac42d 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeDouble.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.formats.tiff.fieldtypes; import java.nio.ByteOrder; +import java.util.Arrays; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.common.ByteConversions; @@ -31,8 +32,7 @@ public class FieldTypeDouble extends FieldType { public Object getValue(final TiffField entry) { final byte[] bytes = entry.getByteArrayValue(); if (entry.getCount() == 1) { - return ByteConversions.toDouble(bytes, - entry.getByteOrder()); + return ByteConversions.toDouble(bytes, entry.getByteOrder()); } return ByteConversions.toDoubles(bytes, entry.getByteOrder()); } @@ -40,21 +40,16 @@ public class FieldTypeDouble extends FieldType { @Override public byte[] writeData(final Object o, final ByteOrder byteOrder) throws ImageWriteException { if (o instanceof Double) { - return ByteConversions.toBytes(((Double) o).doubleValue(), - byteOrder); + return ByteConversions.toBytes(((Double) o).doubleValue(), byteOrder); } if (o instanceof double[]) { - final double[] numbers = (double[]) o; - return ByteConversions.toBytes(numbers, byteOrder); + return ByteConversions.toBytes((double[]) o, byteOrder); } if (!(o instanceof Double[])) { throw new ImageWriteException("Invalid data", o); } - final Double[] numbers = (Double[]) o; - final double[] values = new double[numbers.length]; - for (int i = 0; i < values.length; i++) { - values[i] = numbers[i].doubleValue(); - } + final double[] values = new double[((Double[]) o).length]; + Arrays.setAll(values, i -> ((Double[]) o)[i].doubleValue()); return ByteConversions.toBytes(values, byteOrder); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java index 5b28e238..d90d4a34 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeFloat.java @@ -31,8 +31,7 @@ public class FieldTypeFloat extends FieldType { public Object getValue(final TiffField entry) { final byte[] bytes = entry.getByteArrayValue(); if (entry.getCount() == 1) { - return ByteConversions.toFloat(bytes, - entry.getByteOrder()); + return ByteConversions.toFloat(bytes, entry.getByteOrder()); } return ByteConversions.toFloats(bytes, entry.getByteOrder()); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java index cf2e8d6c..ab603994 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/fieldtypes/FieldTypeRational.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.formats.tiff.fieldtypes; import java.nio.ByteOrder; +import java.util.Arrays; import org.apache.commons.imaging.ImageWriteException; import org.apache.commons.imaging.common.ByteConversions; @@ -51,16 +52,12 @@ public class FieldTypeRational extends FieldType { } if (o instanceof Number) { final Number number = (Number) o; - final RationalNumber rationalNumber = RationalNumber.valueOf(number.doubleValue()); - return ByteConversions.toBytes(rationalNumber, byteOrder); + return ByteConversions.toBytes(RationalNumber.valueOf(number.doubleValue()), byteOrder); } if (o instanceof Number[]) { final Number[] numbers = (Number[]) o; final RationalNumber[] rationalNumbers = new RationalNumber[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - final Number number = numbers[i]; - rationalNumbers[i] = RationalNumber.valueOf(number.doubleValue()); - } + Arrays.setAll(rationalNumbers, RationalNumber::valueOf); return ByteConversions.toBytes(rationalNumbers, byteOrder); } if (!(o instanceof double[])) { @@ -68,10 +65,7 @@ public class FieldTypeRational extends FieldType { } final double[] numbers = (double[]) o; final RationalNumber[] rationalNumbers = new RationalNumber[numbers.length]; - for (int i = 0; i < numbers.length; i++) { - final double number = numbers[i]; - rationalNumbers[i] = RationalNumber.valueOf(number); - } + Arrays.setAll(rationalNumbers, RationalNumber::valueOf); return ByteConversions.toBytes(rationalNumbers, byteOrder); } } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java b/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java index 6f10fbcf..60fe73cb 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/photometricinterpreters/PhotometricInterpreterPalette.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.formats.tiff.photometricinterpreters; import java.io.IOException; +import java.util.Arrays; import org.apache.commons.imaging.ImageReadException; import org.apache.commons.imaging.common.ImageBuilder; @@ -38,20 +39,19 @@ public class PhotometricInterpreterPalette extends PhotometricInterpreter { final int bitsPerPixel = getBitsPerSample(0); final int colormapScale = (1 << bitsPerPixel); indexColorMap = new int[colormapScale]; - for (int index = 0; index < colormapScale; index++) { - final int red = (colorMap[index] >> 8) & 0xff; - final int green = (colorMap[index + (colormapScale)] >> 8) & 0xff; - final int blue = (colorMap[index + (2 * colormapScale)] >> 8) & 0xff; - indexColorMap[index] = 0xff000000 | (red << 16) | (green << 8) - | blue; - } + Arrays.setAll(indexColorMap, i -> { + final int red = (colorMap[i] >> 8) & 0xff; + final int green = (colorMap[i + (colormapScale)] >> 8) & 0xff; + final int blue = (colorMap[i + (2 * colormapScale)] >> 8) & 0xff; + return 0xff000000 | (red << 16) | (green << 8) | blue; + }); - // Fix for IMAGING-247 5/17/2020 + // Fix for IMAGING-247 5/17/2020 // This interpreter is used with TIFF_COMPRESSION_PACKBITS (32773). - // which unpacks to 8 bits per sample. But if the bits-per-pixel + // which unpacks to 8 bits per sample. But if the bits-per-pixel // is less than 8 bits, some authoring tools do not zero-out the - // unused bits. This results in cases where the decoded by index - // exceeds the size of the palette. So we set up a mask to protect + // unused bits. This results in cases where the decoded by index + // exceeds the size of the palette. So we set up a mask to protect // the code from an array bounds exception. int temp = 0; for (int i = 0; i < bitsPerPixel; i++) { diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java index db44ff5d..efd26c4c 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/ImageDataOffsets.java @@ -16,6 +16,8 @@ */ package org.apache.commons.imaging.formats.tiff.write; +import java.util.Arrays; + import org.apache.commons.imaging.formats.tiff.TiffElement; class ImageDataOffsets { @@ -23,18 +25,12 @@ class ImageDataOffsets { final TiffOutputField imageDataOffsetsField; final TiffOutputItem[] outputItems; - ImageDataOffsets(final TiffElement.DataElement[] imageData, - final int[] imageDataOffsets, - final TiffOutputField imageDataOffsetsField) { + ImageDataOffsets(final TiffElement.DataElement[] imageData, final int[] imageDataOffsets, final TiffOutputField imageDataOffsetsField) { this.imageDataOffsets = imageDataOffsets; this.imageDataOffsetsField = imageDataOffsetsField; outputItems = new TiffOutputItem[imageData.length]; - for (int i = 0; i < imageData.length; i++) { - final TiffOutputItem item = new TiffOutputItem.Value("TIFF image data", - imageData[i].getData()); - outputItems[i] = item; - } + Arrays.setAll(outputItems, i -> new TiffOutputItem.Value("TIFF image data", imageData[i].getData())); } diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java index 97a0dd7d..5c16bd16 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffImageWriterBase.java @@ -34,6 +34,7 @@ import java.io.OutputStream; import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -294,7 +295,7 @@ public abstract class TiffImageWriterBase { final int nRows = b.length / nBytesPerRow; for (int iRow = 0; iRow < nRows; iRow++) { final int offset = iRow * nBytesPerRow; - for (int i = nBytesPerRow-1; i >= bytesPerSample; i--) { + for (int i = nBytesPerRow - 1; i >= bytesPerSample; i--) { b[offset + i] -= b[offset + i - bytesPerSample]; } } @@ -383,8 +384,7 @@ public abstract class TiffImageWriterBase { int t6Options = 0; if (compression == TIFF_COMPRESSION_CCITT_1D) { for (int i = 0; i < strips.length; i++) { - strips[i] = T4AndT6Compression.compressModifiedHuffman( - strips[i], width, strips[i].length / ((width + 7) / 8)); + strips[i] = T4AndT6Compression.compressModifiedHuffman(strips[i], width, strips[i].length / ((width + 7) / 8)); } } else if (compression == TIFF_COMPRESSION_CCITT_GROUP_3) { final Integer t4Parameter = params.getT4Options(); @@ -422,8 +422,7 @@ public abstract class TiffImageWriterBase { "T.6 compression with the uncompressed mode extension is not yet supported"); } for (int i = 0; i < strips.length; i++) { - strips[i] = T4AndT6Compression.compressT6(strips[i], width, - strips[i].length / ((width + 7) / 8)); + strips[i] = T4AndT6Compression.compressT6(strips[i], width, strips[i].length / ((width + 7) / 8)); } } else if (compression == TIFF_COMPRESSION_PACKBITS) { for (int i = 0; i < strips.length; i++) { @@ -455,9 +454,7 @@ public abstract class TiffImageWriterBase { } final TiffElement.DataElement[] imageData = new TiffElement.DataElement[strips.length]; - for (int i = 0; i < strips.length; i++) { - imageData[i] = new TiffImageData.Data(0, strips[i].length, strips[i]); - } + Arrays.setAll(imageData, i -> new TiffImageData.Data(0, strips[i].length, strips[i])); final TiffOutputSet outputSet = new TiffOutputSet(byteOrder); final TiffOutputDirectory directory = outputSet.addRootDirectory(); diff --git a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java index b3e3995a..dec72012 100644 --- a/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java +++ b/src/main/java/org/apache/commons/imaging/formats/tiff/write/TiffOutputDirectory.java @@ -24,6 +24,7 @@ import static org.apache.commons.imaging.formats.tiff.constants.TiffConstants.TI import java.io.IOException; import java.nio.ByteOrder; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; @@ -714,9 +715,7 @@ public final class TiffOutputDirectory extends TiffOutputItem { final int[] imageDataOffsets = new int[imageData.length]; final int[] imageDataByteCounts = new int[imageData.length]; - for (int i = 0; i < imageData.length; i++) { - imageDataByteCounts[i] = imageData[i].length; - } + Arrays.setAll(imageDataByteCounts, i -> imageData[i].length); // Append imageData-related fields to first directory imageDataOffsetField = new TiffOutputField(offsetTag, diff --git a/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java b/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java index d3e99753..22abf092 100644 --- a/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java +++ b/src/main/java/org/apache/commons/imaging/palette/ColorSpaceSubset.java @@ -17,6 +17,7 @@ package org.apache.commons.imaging.palette; import java.io.Serializable; +import java.util.Arrays; import java.util.Comparator; import java.util.logging.Logger; @@ -41,9 +42,7 @@ class ColorSpaceSubset { mins = new int[PaletteFactory.COMPONENTS]; maxs = new int[PaletteFactory.COMPONENTS]; - for (int i = 0; i < PaletteFactory.COMPONENTS; i++) { - maxs[i] = precisionMask; - } + Arrays.fill(maxs, precisionMask); rgb = -1; }