Skip to content

Commit

Permalink
Simplify conditions and avoid extra checks.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturobernalg committed Jun 15, 2021
1 parent 471475c commit a1e712b
Show file tree
Hide file tree
Showing 14 changed files with 23 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -503,10 +503,6 @@ public Dimension getImageSize(final ByteSource byteSource, Map<String, Object> p

final BmpHeaderInfo bhi = readBmpHeaderInfo(byteSource);

if (bhi == null) {
throw new ImageReadException("BMP: couldn't read header");
}

return new Dimension(bhi.width, bhi.height);

}
Expand Down Expand Up @@ -557,10 +553,6 @@ public ImageInfo getImageInfo(final ByteSource byteSource, Map<String, Object> p
ic = readImageContents(is, FormatCompliance.getDefault());
}

if (ic == null) {
throw new ImageReadException("Couldn't read BMP Data");
}

final BmpHeaderInfo bhi = ic.bhi;
final byte[] colorTable = ic.colorTable;

Expand Down Expand Up @@ -658,9 +650,6 @@ public BufferedImage getBufferedImage(final InputStream inputStream, Map<String,
}

final BmpImageContents ic = readImageContents(inputStream, FormatCompliance.getDefault());
if (ic == null) {
throw new ImageReadException("Couldn't read BMP Data");
}

final BmpHeaderInfo bhi = ic.bhi;
// byte colorTable[] = ic.colorTable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,7 @@ private List<GifBlock> readBlocks(final GifHeaderInfo ghi, final InputStream is,
completeCode);
}

// if (label == new String("ICCRGBG1"))
//{
// GIF's can have embedded ICC Profiles - who knew?
//}

if ((label != null) && (label.length > 0)) {
if (label.length > 0) {
final GenericGifBlock block = readGenericGIFBlock(is,
completeCode, label);
result.add(block);
Expand Down Expand Up @@ -491,10 +486,6 @@ public Dimension getImageSize(final ByteSource byteSource, final Map<String, Obj
throws ImageReadException, IOException {
final GifImageContents blocks = readFile(byteSource, false);

if (blocks == null) {
throw new ImageReadException("GIF: Couldn't read blocks");
}

final GifHeaderInfo bhi = blocks.gifHeaderInfo;
if (bhi == null) {
throw new ImageReadException("GIF: Couldn't read Header");
Expand Down Expand Up @@ -537,10 +528,6 @@ public ImageMetadata getMetadata(final ByteSource byteSource, final Map<String,
throws ImageReadException, IOException {
final GifImageContents imageContents = readFile(byteSource, false);

if (imageContents == null) {
throw new ImageReadException("GIF: Couldn't read blocks");
}

final GifHeaderInfo bhi = imageContents.gifHeaderInfo;
if (bhi == null) {
throw new ImageReadException("GIF: Couldn't read Header");
Expand Down Expand Up @@ -574,10 +561,6 @@ public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Obj
throws ImageReadException, IOException {
final GifImageContents blocks = readFile(byteSource, false);

if (blocks == null) {
throw new ImageReadException("GIF: Couldn't read blocks");
}

final GifHeaderInfo bhi = blocks.gifHeaderInfo;
if (bhi == null) {
throw new ImageReadException("GIF: Couldn't read Header");
Expand Down Expand Up @@ -818,10 +801,6 @@ public List<BufferedImage> getAllBufferedImages(final ByteSource byteSource)
throws ImageReadException, IOException {
final GifImageContents imageContents = readFile(byteSource, false);

if (imageContents == null) {
throw new ImageReadException("GIF: Couldn't read blocks");
}

final GifHeaderInfo ghi = imageContents.gifHeaderInfo;
if (ghi == null) {
throw new ImageReadException("GIF: Couldn't read Header");
Expand All @@ -840,10 +819,6 @@ public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<Str
throws ImageReadException, IOException {
final GifImageContents imageContents = readFile(byteSource, false);

if (imageContents == null) {
throw new ImageReadException("GIF: Couldn't read blocks");
}

final GifHeaderInfo ghi = imageContents.gifHeaderInfo;
if (ghi == null) {
throw new ImageReadException("GIF: Couldn't read Header");
Expand Down Expand Up @@ -1113,14 +1088,13 @@ public void writeImage(final BufferedImage src, final OutputStream os, Map<Strin
public String getXmpXml(final ByteSource byteSource, final Map<String, Object> params)
throws ImageReadException, IOException {
try (InputStream is = byteSource.getInputStream()) {
final FormatCompliance formatCompliance = null;
final GifHeaderInfo ghi = readHeader(is, formatCompliance);
final GifHeaderInfo ghi = readHeader(is, null);

if (ghi.globalColorTableFlag) {
readColorTable(is, ghi.sizeOfGlobalColorTable);
}

final List<GifBlock> blocks = readBlocks(ghi, is, true, formatCompliance);
final List<GifBlock> blocks = readBlocks(ghi, is, true, null);

final List<String> result = new ArrayList<>();
for (final GifBlock block : blocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,11 @@ public byte[] writeIPTCBlock(List<IptcRecord> elements)
byte[] blockData;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (BinaryOutputStream bos = new BinaryOutputStream(baos, getByteOrder())) {
if (charset != null && !charset.equals(DEFAULT_CHARSET)) {
if (!charset.equals(DEFAULT_CHARSET)) {
bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
bos.write(IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER);
bos.write(ENV_TAG_CODED_CHARACTER_SET);
byte[] codedCharset = charset.equals(StandardCharsets.UTF_8) ? CHARACTER_ESCAPE_SEQUENCE: charset.name().getBytes(StandardCharsets.ISO_8859_1);
byte[] codedCharset = CHARACTER_ESCAPE_SEQUENCE;
bos.write2Bytes(codedCharset.length);
bos.write(codedCharset);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ public byte[] getICCProfileBytes(final ByteSource byteSource, final Map<String,
final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.iCCP },
true);

if ((chunks == null) || (chunks.isEmpty())) {
// throw new ImageReadException("Png: No chunks");
if (chunks.isEmpty()) {
return null;
}

Expand All @@ -264,7 +263,7 @@ public Dimension getImageSize(final ByteSource byteSource, final Map<String, Obj
throws ImageReadException, IOException {
final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.IHDR, }, true);

if ((chunks == null) || (chunks.isEmpty())) {
if (chunks.isEmpty()) {
throw new ImageReadException("Png: No chunks");
}

Expand All @@ -282,7 +281,7 @@ public ImageMetadata getMetadata(final ByteSource byteSource, final Map<String,
throws ImageReadException, IOException {
final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.tEXt, ChunkType.zTXt, }, false);

if ((chunks == null) || (chunks.isEmpty())) {
if (chunks.isEmpty()) {
return null;
}

Expand Down Expand Up @@ -343,10 +342,7 @@ public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Obj
ChunkType.iTXt,
}, false);

// if(chunks!=null)
// System.out.println("chunks: " + chunks.size());

if ((chunks == null) || (chunks.isEmpty())) {
if (chunks.isEmpty()) {
throw new ImageReadException("PNG: no chunks");
}

Expand Down Expand Up @@ -506,7 +502,7 @@ public BufferedImage getBufferedImage(final ByteSource byteSource, Map<String, O
ChunkType.sRGB,
}, false);

if ((chunks == null) || (chunks.isEmpty())) {
if (chunks.isEmpty()) {
throw new ImageReadException("PNG: no chunks");
}

Expand Down Expand Up @@ -653,8 +649,8 @@ public BufferedImage getBufferedImage(final ByteSource byteSource, Map<String, O
scanExpediter.drive();

if (iccProfile != null) {
final Boolean is_srgb = new IccProfileParser().issRGB(iccProfile);
if (is_srgb == null || !is_srgb.booleanValue()) {
final boolean is_srgb = new IccProfileParser().issRGB(iccProfile);
if (!is_srgb) {
final ICC_ColorSpace cs = new ICC_ColorSpace(iccProfile);

final ColorModel srgbCM = ColorModel.getRGBdefault();
Expand Down Expand Up @@ -721,7 +717,7 @@ public String getXmpXml(final ByteSource byteSource, final Map<String, Object> p

final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.iTXt }, false);

if ((chunks == null) || (chunks.isEmpty())) {
if (chunks.isEmpty()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ public void writeImage(final BufferedImage src, final OutputStream os, Map<Strin
final PaletteFactory paletteFactory = new PaletteFactory();

if (hasAlpha) {
palette = paletteFactory.makeQuantizedRgbaPalette(src, hasAlpha, maxColors);
palette = paletteFactory.makeQuantizedRgbaPalette(src, true, maxColors);
writeChunkPLTE(os, palette);
writeChunkTRNS(os, palette);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,6 @@ public Dimension getImageSize(final ByteSource byteSource, final Map<String, Obj
throws ImageReadException, IOException {
final FileInfo info = readHeader(byteSource);

if (info == null) {
throw new ImageReadException("PNM: Couldn't read Header");
}

return new Dimension(info.width, info.height);
}

Expand All @@ -251,10 +247,6 @@ public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Obj
throws ImageReadException, IOException {
final FileInfo info = readHeader(byteSource);

if (info == null) {
throw new ImageReadException("PNM: Couldn't read Header");
}

final List<String> comments = new ArrayList<>();

final int bitsPerPixel = info.getBitDepth() * info.getNumComponents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ public byte[] getICCProfileBytes(final ByteSource byteSource, final Map<String,
final List<ImageResourceBlock> blocks = readImageResourceBlocks(byteSource,
new int[] { IMAGE_RESOURCE_ID_ICC_PROFILE, }, 1);

if ((blocks == null) || (blocks.isEmpty())) {
if (blocks.isEmpty()) {
return null;
}

Expand All @@ -402,9 +402,6 @@ public byte[] getICCProfileBytes(final ByteSource byteSource, final Map<String,
public Dimension getImageSize(final ByteSource byteSource, final Map<String, Object> params)
throws ImageReadException, IOException {
final PsdHeaderInfo bhi = readHeader(byteSource);
if (bhi == null) {
throw new ImageReadException("PSD: couldn't read header");
}

return new Dimension(bhi.columns, bhi.rows);

Expand Down Expand Up @@ -446,10 +443,6 @@ public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Obj
final PsdImageContents imageContents = readImageContents(byteSource);
// ImageContents imageContents = readImage(byteSource, false);

if (imageContents == null) {
throw new ImageReadException("PSD: Couldn't read blocks");
}

final PsdHeaderInfo header = imageContents.header;
if (header == null) {
throw new ImageReadException("PSD: Couldn't read Header");
Expand Down Expand Up @@ -557,10 +550,6 @@ public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<Str
final PsdImageContents imageContents = readImageContents(byteSource);
// ImageContents imageContents = readImage(byteSource, false);

if (imageContents == null) {
throw new ImageReadException("PSD: Couldn't read blocks");
}

final PsdHeaderInfo header = imageContents.header;
if (header == null) {
throw new ImageReadException("PSD: Couldn't read Header");
Expand Down Expand Up @@ -673,10 +662,6 @@ public String getXmpXml(final ByteSource byteSource, final Map<String, Object> p

final PsdImageContents imageContents = readImageContents(byteSource);

if (imageContents == null) {
throw new ImageReadException("PSD: Couldn't read blocks");
}

final PsdHeaderInfo header = imageContents.header;
if (header == null) {
throw new ImageReadException("PSD: Couldn't read Header");
Expand All @@ -685,7 +670,7 @@ public String getXmpXml(final ByteSource byteSource, final Map<String, Object> p
final List<ImageResourceBlock> blocks = readImageResourceBlocks(byteSource,
new int[] { IMAGE_RESOURCE_ID_XMP, }, -1);

if ((blocks == null) || (blocks.isEmpty())) {
if (blocks.isEmpty()) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public BufferedImage getTiffImage(final Map<String, Object> params)
*/
public BufferedImage getTiffImage(final ByteOrder byteOrder) throws ImageReadException,
IOException {
final Map<String, Object> params = null;
return getTiffImage(byteOrder, params);
return getTiffImage(byteOrder, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,16 +347,11 @@ public boolean dumpImageFile(final PrintWriter pw, final ByteSource byteSource)
// try
{
final FormatCompliance formatCompliance = FormatCompliance.getDefault();
final Map<String, Object> params = null;
final TiffContents contents = new TiffReader(true).readContents(
byteSource, params, formatCompliance);
byteSource, null, formatCompliance);

final List<TiffDirectory> directories = contents.directories;

if (directories == null) {
return false;
}

for (int d = 0; d < directories.size(); d++) {
final TiffDirectory directory = directories.get(d);

Expand Down Expand Up @@ -392,8 +387,7 @@ public boolean dumpImageFile(final PrintWriter pw, final ByteSource byteSource)
public FormatCompliance getFormatCompliance(final ByteSource byteSource)
throws ImageReadException, IOException {
final FormatCompliance formatCompliance = FormatCompliance.getDefault();
final Map<String, Object> params = null;
new TiffReader(isStrict(params)).readContents(byteSource, params,
new TiffReader(isStrict(null)).readContents(byteSource, null,
formatCompliance);
return formatCompliance;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ private void interpretStrip(
if (bitsPerPixel == 24) {
// 24 bit case, we don't mask the red byte because any
// sign-extended bits get covered by opacity mask
k = 0;
for (int i = i0; i < i1; i++) {
for (int j = 0; j < width; j++, k += 3) {
final int rgb = 0xff000000
Expand All @@ -199,7 +198,6 @@ private void interpretStrip(
} else {
// 32 bit case, we don't mask the high byte because any
// sign-extended bits get shifted up and out of result
k = 0;
for (int i = i0; i < i1; i++) {
for (int j = 0; j < width; j++, k += 4) {
final int rgb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,9 @@ private List<TiffElement> analyzeOldTiff(final Map<Integer, TiffOutputField> fro
IOException {
try {
final ByteSource byteSource = new ByteSourceArray(exifBytes);
final Map<String, Object> params = null;
final FormatCompliance formatCompliance = FormatCompliance.getDefault();
final TiffContents contents = new TiffReader(false).readContents(
byteSource, params, formatCompliance);
byteSource, null, formatCompliance);

final List<TiffElement> elements = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ private IccProfileInfo readICCProfileInfo(InputStream is) {
printCharQuad("ProfileCreatorSignature", profileCreatorSignature);
}

final byte[] profileId = null;
skipBytes(is, 16, "Not a Valid ICC Profile");
// readByteArray("ProfileID", 16, is,
// "Not a Valid ICC Profile");
Expand Down Expand Up @@ -258,7 +257,7 @@ private IccProfileInfo readICCProfileInfo(InputStream is) {
profileConnectionSpace, profileFileSignature,
primaryPlatformSignature, variousFlags, deviceManufacturer,
deviceModel, renderingIntent, profileCreatorSignature,
profileId, tags);
null, tags);

if (LOGGER.isLoggable(Level.FINEST)) {
LOGGER.finest("issRGB: " + result.issRGB());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/imaging/icc/IccTag.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void dump(final PrintWriter pw, final String prefix) throws ImageReadExce
+ ")");

if (data == null) {
pw.println(prefix + "data: " + Arrays.toString(data));
pw.println(prefix + "data: " + Arrays.toString((byte[]) null));
} else {
pw.println(prefix + "data: " + data.length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public boolean performNextMedianCut(final List<ColorGroup> colorGroups, final bo
&& colorGroup.alphaDiff > colorGroup.redDiff
&& colorGroup.alphaDiff > colorGroup.greenDiff
&& colorGroup.alphaDiff > colorGroup.blueDiff) {
doCut(colorGroup, ColorComponent.ALPHA, colorGroups, ignoreAlpha);
doCut(colorGroup, ColorComponent.ALPHA, colorGroups, false);
} else if (colorGroup.redDiff > colorGroup.greenDiff
&& colorGroup.redDiff > colorGroup.blueDiff) {
doCut(colorGroup, ColorComponent.RED, colorGroups, ignoreAlpha);
Expand Down

0 comments on commit a1e712b

Please sign in to comment.