Skip to content

Commit

Permalink
[IMAGING-340] Add support to PNG 1.2 Extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Glavo authored and kinow committed Dec 25, 2024
1 parent 062f9b7 commit eebaf6a
Show file tree
Hide file tree
Showing 10 changed files with 1,157 additions and 818 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
<suppressionsLocation>checkstyle-suppressions.xml</suppressionsLocation>
<includeTestSourceDirectory>false</includeTestSourceDirectory>
<enableRulesSummary>false</enableRulesSummary>
<excludes>target/**</excludes>
<excludes>target/**</excludes>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -383,7 +383,7 @@
<email>ggregory at apache.org</email>
<url>https://www.garygregory.com</url>
<organization>The Apache Software Foundation</organization>
<organizationUrl>https://www.apache.org/</organizationUrl>
<organizationUrl>https://www.apache.org/</organizationUrl>
<roles>
<role>PMC Member</role>
</roles>
Expand Down
1,508 changes: 754 additions & 754 deletions src/changes/changes.xml

Large diffs are not rendered by default.

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>
<!-- Reason: We don't want to implement Serializable at all. -->
<Match>
<Bug pattern="SE_COMPARATOR_SHOULD_BE_SERIALIZABLE" />
Expand Down
204 changes: 171 additions & 33 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,9 +16,22 @@
*/
package org.apache.commons.imaging.formats.png;

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

import org.apache.commons.imaging.ImagingException;
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.
Expand All @@ -27,69 +40,194 @@
*/
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-alpha4
*/
oFFs(Extension.PNGEXT),

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

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

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

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

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

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

;

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

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, ImagingException {
ChunkType type = findType(chunkType);
return type != null && type.constructor != null
? type.constructor.make(length, chunkType, crc, bytes)
: new PngChunk(length, chunkType, crc, bytes);
}

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-alpha4
*/
enum Extension {
/**
* @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

0 comments on commit eebaf6a

Please sign in to comment.