Skip to content

Commit

Permalink
Merge pull request #29 from iwbh15/dev_ph0.2
Browse files Browse the repository at this point in the history
Disable chroma subsampling in JPEGCodec
  • Loading branch information
dgault authored Nov 30, 2023
2 parents 5ebe5af + 714e1a0 commit 2819b77
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/main/java/ome/codecs/CodecOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public class CodecOptions {
* Whether or not the decompressed data will be stored as YCbCr.
*/
public boolean ycbcr;

/**
* Whether or not use chroma subsampling.
*/
public boolean disableChromaSubsampling;

// -- Constructors --

Expand Down
86 changes: 79 additions & 7 deletions src/main/java/ome/codecs/JPEGCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@
import loci.common.RandomAccessInputStream;
import ome.codecs.gui.AWTImageTools;

//::phaub 09.02.23
import javax.imageio.IIOImage;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import javax.imageio.IIOException;

import javax.imageio.ImageTypeSpecifier;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.NamedNodeMap;
import javax.imageio.metadata.IIOMetadata;


/**
Expand Down Expand Up @@ -96,7 +98,7 @@ public byte[] compress(byte[] data, CodecOptions options)
options.bitsPerSample / 8, false, options.littleEndian, options.signed);

try {
//::phaub 09.02.23 (Adjustable jpeg quality)
// Adjustable jpeg quality
// How to use:
// Set jpegquality using CodecOptions in the calling object (e.g. QuPath using OMEPyramidWriter()):
// CodecOptions options = new CodecOptions();
Expand All @@ -108,22 +110,41 @@ public byte[] compress(byte[] data, CodecOptions options)
jpegquality = options.quality;
}
jpegquality = Math.max(0.25, Math.min(1.0, jpegquality));


// Disable chroma subsampling
// How to use:
// Set disableChromaSubsampling using CodecOptions in the calling object (e.g. QuPath using OMEPyramidWriter()):
// CodecOptions options = new CodecOptions();
// options.disableChromaSubsampling = true;
// writer.setCodecOptions(options)
boolean disableChromaSubsampling = jpegquality>=0.9;
if (options.disableChromaSubsampling) {
disableChromaSubsampling = options.disableChromaSubsampling;
}

ImageWriter jpgWriter = ImageIO.getImageWritersByFormatName("jpg").next();
if (jpgWriter == null) {
return null;
}
ImageWriteParam jpgWriteParam = jpgWriter.getDefaultWriteParam();
jpgWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
jpgWriteParam.setCompressionQuality((float) jpegquality);
if (jpegquality == 1.0)
jpgWriteParam.setSourceSubsampling(1, 1, 0, 0);

ImageOutputStream stream = new MemoryCacheImageOutputStream(out);
try {
Iterator<ImageWriter> iterator = ImageIO.getImageWritersByFormatName("jpeg");
if (iterator.hasNext()) {
ImageWriter writer = iterator.next();
writer.setOutput(stream);
IIOImage outputImage = new IIOImage(img, null, null);

IIOImage outputImage = null;
if (!disableChromaSubsampling) // Use chroma subsampling YUV420
outputImage = new IIOImage(img, null, null);
else
outputImage = createIIOImageNoChromaSubsampling(jpgWriter, img, jpgWriteParam);

writer.write(null, outputImage, jpgWriteParam);
}
} finally {
Expand All @@ -138,6 +159,57 @@ public byte[] compress(byte[] data, CodecOptions options)
return out.toByteArray();
}

/**
* Create an IIOImage object with disabled chroma subsampling.
*/
private IIOImage createIIOImageNoChromaSubsampling(ImageWriter jpgWriter, BufferedImage img, ImageWriteParam jpgWriteParam)
throws CodecException {
// Disable JPEG chroma subsampling
// http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/BaseOptimizer.java
// http://svn.apache.org/repos/asf/shindig/trunk/java/gadgets/src/main/java/org/apache/shindig/gadgets/rewrite/image/JpegImageUtils.java
try {
IIOMetadata metadata = jpgWriter.getDefaultImageMetadata(new ImageTypeSpecifier(img.getColorModel(), img.getSampleModel()), jpgWriteParam);
Node rootNode = metadata!=null ? metadata.getAsTree("javax_imageio_jpeg_image_1.0") : null;
boolean metadataUpdated = false;
// The top level root node has two children, out of which the second one will
// contain all the information related to image markers.
if (rootNode!=null && rootNode.getLastChild() != null) {
Node markerNode = rootNode.getLastChild();
NodeList markers = markerNode.getChildNodes();
// Search for 'SOF' marker where subsampling information is stored.
for (int i = 0; i < markers.getLength(); i++) {
Node node = markers.item(i);
// 'SOF' marker can have
// 1 child node if the color representation is greyscale,
// 3 child nodes if the color representation is YCbCr, and
// 4 child nodes if the color representation is YCMK.
// This subsampling applies only to YCbCr.
if (node.getNodeName().equalsIgnoreCase("sof") && node.hasChildNodes() && node.getChildNodes().getLength() == 3) {
// In 'SOF' marker, first child corresponds to the luminance channel, and setting
// the HsamplingFactor and VsamplingFactor to 1, will imply 4:4:4 chroma subsampling.
NamedNodeMap attrMap = node.getFirstChild().getAttributes();
// SamplingModes: UNKNOWN(-2), DEFAULT(-1), YUV444(17), YUV422(33), YUV420(34), YUV411(65)
int samplingmode = 17; // YUV444
attrMap.getNamedItem("HsamplingFactor").setNodeValue((samplingmode & 0xf) + "");
attrMap.getNamedItem("VsamplingFactor").setNodeValue(((samplingmode >> 4) & 0xf) + "");
metadataUpdated = true;
break;
}
}
}
// Read the updated metadata from the metadata node tree.
if (metadataUpdated)
metadata.setFromTree("javax_imageio_jpeg_image_1.0", rootNode);

IIOImage iioImage = new IIOImage(img, null, metadata);

return iioImage;
}
catch (IOException e) {
throw new CodecException("Could not create IIOImage object with disbaled chroma subsampling", e);
}
}

/**
* The CodecOptions parameter should have the following fields set:
* {@link CodecOptions#interleaved interleaved}
Expand Down

0 comments on commit 2819b77

Please sign in to comment.