Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMAGING-340] Support Extensions to the PNG 1.2 Specification, Version 1.5.0 #269

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ The <action> type attribute can be add,update,fix,remove.
<action dev="ggregory" type="add" due-to="Arnout Engelen">Add an Imaging-specific security page #439.</action>
<action dev="ggregory" type="add" due-to="Gary Gregory">Add Maven property commons.taglist.version for debugging.</action>
<action dev="ggregory" type="add" due-to="Stefal, Gary Gregory">Add support of GPSHPositioningError in GpsTagConstants #451.</action>
<action dev="kinow" type="add" due-to="Glavo">Support Extensions from PNG 1.2 Specification, Version 1.5.0 #269.</action>
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Dependabot, Gary Gregory">Bump org.apache.commons:commons-parent from 69 to 78 #400, #406, #428, #430, #436, #442, #445, #545.</action>
<action dev="ggregory" type="update" due-to="Dependabot">Bump org.apache.commons:commons-lang3 from 3.14.0 to 3.17.0 #416, #423, #431.</action>
Expand Down
10 changes: 10 additions & 0 deletions src/conf/spotbugs-exclude-filter.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,16 @@
<Method name="&lt;init&gt;" />
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
<Match>
<Class name="org.apache.commons.imaging.formats.png.PngImageMetadata" />
<Method name="getExif" />
<Bug pattern="EI_EXPOSE_REP" />
</Match>
<Match>
<Class name="org.apache.commons.imaging.formats.png.PngImageMetadata" />
<Method name="&lt;init&gt;" />
<Bug pattern="EI_EXPOSE_REP2" />
</Match>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These look good to me!

<!-- Reason: We don't want to implement Serializable at all. -->
<Match>
<Bug pattern="SE_COMPARATOR_SHOULD_BE_SERIALIZABLE" />
Expand Down
205 changes: 171 additions & 34 deletions src/main/java/org/apache/commons/imaging/formats/png/ChunkType.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,80 +16,217 @@
*/
package org.apache.commons.imaging.formats.png;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.apache.commons.imaging.common.BinaryFunctions;
import org.apache.commons.imaging.formats.png.chunks.PngChunk;
import org.apache.commons.imaging.formats.png.chunks.PngChunkGama;
import org.apache.commons.imaging.formats.png.chunks.PngChunkIccp;
import org.apache.commons.imaging.formats.png.chunks.PngChunkIdat;
import org.apache.commons.imaging.formats.png.chunks.PngChunkIhdr;
import org.apache.commons.imaging.formats.png.chunks.PngChunkItxt;
import org.apache.commons.imaging.formats.png.chunks.PngChunkPhys;
import org.apache.commons.imaging.formats.png.chunks.PngChunkPlte;
import org.apache.commons.imaging.formats.png.chunks.PngChunkScal;
import org.apache.commons.imaging.formats.png.chunks.PngChunkText;
import org.apache.commons.imaging.formats.png.chunks.PngChunkZtxt;

/**
* Type of a PNG chunk.
* Type of PNG chunk.
*
* @see <a href="https://www.w3.org/TR/png/#11Chunks">Portable Network Graphics Specification - Chunk specifications</a>
*/
public enum ChunkType {

/** Image header */
IHDR,
/**
* Image header
*/
IHDR(PngChunkIhdr::new),

/** Palette */
PLTE,
/**
* Palette
*/
PLTE(PngChunkPlte::new),

/** Image data */
IDAT,
/**
* Image data
*/
IDAT(PngChunkIdat::new),

/** Image trailer */
/**
* Image trailer
*/
IEND,

/** Transparency */
/**
* Transparency
*/
tRNS,

/** Primary chromaticities and white point */
/**
* Primary chromaticities and white point
*/
cHRM,

/** Image gamma */
gAMA,
/**
* Image gamma
*/
gAMA(PngChunkGama::new),

/** Embedded ICC profile */
iCCP,
/**
* Embedded ICC profile
*/
iCCP(PngChunkIccp::new),

/** Significant bits */
/**
* Significant bits
*/
sBIT,

/** Standard RGB color space */
/**
* Standard RGB color space
*/
sRGB,

/** Textual data */
tEXt,
/**
* Textual data
*/
tEXt(PngChunkText::new),

/** Compressed textual data */
zTXt,
/**
* Compressed textual data
*/
zTXt(PngChunkZtxt::new),

/** International textual data */
iTXt,
/**
* International textual data
*/
iTXt(PngChunkItxt::new),

/** Background color */
/**
* Background colour
*/
bKGD,

/** Image histogram */
/**
* Image histogram
*/
hIST,

/** Physical pixel dimensions */
pHYs,
/**
* Physical pixel dimensions
*/
pHYs(PngChunkPhys::new),

/** Physical scale */
sCAL,

/** Suggested palette */
/**
* Suggested palette
*/
sPLT,

/** Image last-modification time */
tIME;
/**
* Image last-modification time
*/
tIME,

/*
* PNGEXT
*/

/**
* Image offset
*
* @since 1.0-alpha6
*/
oFFs(Extension.PNGEXT),

/**
* Calibration of pixel values
*
* @since 1.0-alpha6
*/
pCAL(Extension.PNGEXT),

/**
* Physical scale
*/
sCAL(Extension.PNGEXT, PngChunkScal::new),

/**
* GIF Graphic Control Extension
*
* @since 1.0-alpha6
*/
gIFg(Extension.PNGEXT),

/**
* GIF Application Extension
*
* @since 1.0-alpha6
*/
gIFx(Extension.PNGEXT),

/**
* Indicator of Stereo Image
*
* @since 1.0-alpha6
*/
sTER(Extension.PNGEXT),

/**
* Exchangeable Image File (Exif) Profile
*
* @since 1.0-alpha6
*/
eXIf(Extension.PNGEXT),

;

@FunctionalInterface
private interface ChunkConstructor {
PngChunk make(int length, int chunkType, int crc, byte[] bytes) throws IOException;
}

private static final ChunkType[] types = ChunkType.values();

static ChunkType findType(int chunkType) {
for (ChunkType type : types) {
if (type.value == chunkType) {
return type;
}
}
return null;
}

static PngChunk makeChunk(int length, int chunkType, int crc, byte[] bytes) throws IOException {
ChunkType type = findType(chunkType);
return type != null && type.constructor != null
? type.constructor.make(length, chunkType, crc, bytes)
: new PngChunk(length, chunkType, crc, bytes);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good simplification here 👍


final byte[] array;
final int value;
final Extension extension;
final ChunkConstructor constructor;

ChunkType() {
this(null, null);
}

ChunkType(Extension extension) {
this(extension, null);
}

ChunkType(ChunkConstructor constructor) {
this(null, constructor);
}

ChunkType(Extension extension, ChunkConstructor constructor) {
final char[] chars = name().toCharArray();
array = name().getBytes(StandardCharsets.UTF_8);
value = BinaryFunctions.charsToQuad(chars[0], chars[1], chars[2], chars[3]);
this.array = name().getBytes(StandardCharsets.UTF_8);
this.value = BinaryFunctions.charsToQuad(chars[0], chars[1], chars[2], chars[3]);
this.extension = extension;
this.constructor = constructor;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.png;

/**
* PNG extension types.
*
* @since 1.0-alpha6
*/
enum Extension {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should be called PngExtension, as there are PngImageMetadata, PngImageParser, etc. inside the oac.imaging.formats.png package. Although we also have ChunkType 🤷 , I think that's fine.

/**
* @see <a href="http://ftp-osl.osuosl.org/pub/libpng/documents/pngext-1.5.0.html">Extensions to the PNG 1.2 Specification, Version 1.5.0</a>
*/
PNGEXT,

/**
* @see <a href="https://wiki.mozilla.org/APNG_Specification">APNG Specification</a>
*/
APNG,
}
Loading
Loading