Documentation + fix for minor issue when quality could not be determined.

This commit is contained in:
Harald Kuhr 2012-04-19 17:51:19 +02:00
parent 465eb2ecb3
commit 7bcfd228b9

View File

@ -37,46 +37,47 @@ import java.io.IOException;
import java.util.List;
/**
* JPEGQuality
* Determines an approximate JPEG compression quality value from the quantization tables.
*
* @author <a href="mailto:harald.kuhr@gmail.com">Harald Kuhr</a>
* @author last modified by $Author: haraldk$
* @version $Id: JPEGQuality.java,v 1.0 16.02.12 17:07 haraldk Exp$
*/
public class JPEGQuality {
// TODO: Something for the metadata.jpeg package?
// TODO: Make a test that verifies that the values are in line with parameter set to JPEGImageWriter
public final class JPEGQuality {
static final int NUM_QUANT_TABLES = 4; /* Quantization tables are numbered 0..3 */
static final int DCT_SIZE_2 = 64; /* DCT_SIZE squared; # of elements in a block */
public static void main(String[] args) throws IOException {
for (String arg : args) {
float quality = getJPEGQuality(ImageIO.createImageInputStream(new File(arg)));
System.err.println(arg + " quality: " + quality + "/" + (int) (quality * 100));
}
}
// TODO: Do we want 1-100 (%) or 0-1 (float) as result? The latter is more in line with the quality parameter given
// to the writer (in JPEGImageWriteParam). OTOH, it should also be possible to instruct the writer to use the tables
// from the original image directly, which would provide more "accurate" results.
/**
* Determines an approximate JPEG compression quality value from the quantization tables.
* The value will be in the range {@code [0...1]}, where {@code 1} is the best possible value.
*
*
* @param segments a list of JPEG segments containing the DQT quantization tables.
* @return a float in the range {@code [0...1]}, representing the JPEG quality,
* or {@code -1} if the quality can't be determined.
* @throws IIOException if a JPEG format error is found during parsing.
* @throws IOException if an I/O exception occurs during parsing.
*
* @see javax.imageio.plugins.jpeg.JPEGImageWriteParam#setCompressionQuality(float)
* @see JPEG#DQT
*/
public static float getJPEGQuality(final List<JPEGSegment> segments) throws IOException {
return getJPEGQuality(getQuantizationTables(segments)) / 100f;
int quality = getJPEGQuality(getQuantizationTables(segments));
return quality >= 0 ? quality / 100f : quality;
}
/**
* Determines an approximate JPEG compression quality value from the quantization tables.
* The value will be in the range {@code [0...1]}, where {@code 1} is the best possible value.
*
* @param input an image input stream containing JPEG data.
* @return a float in the range {@code [0...1]}, representing the JPEG quality,
* or {@code -1} if the quality can't be determined.
* @throws IIOException if a JPEG format error is found during parsing.
* @throws IOException if an I/O exception occurs during parsing.
*
* @see javax.imageio.plugins.jpeg.JPEGImageWriteParam#setCompressionQuality(float)
* @see JPEG#DQT
*/
public static float getJPEGQuality(final ImageInputStream input) throws IOException {
return getJPEGQuality(JPEGSegmentUtil.readSegments(input, JPEG.DQT, null));
}
@ -242,4 +243,11 @@ public class JPEGQuality {
return tables;
}
public static void main(String[] args) throws IOException {
for (String arg : args) {
float quality = getJPEGQuality(ImageIO.createImageInputStream(new File(arg)));
System.err.println(arg + " quality: " + quality + "/" + (int) (quality * 100));
}
}
}