As the title says, I'm trying to use custom quantization tables to compress an image in JPEG format. My problem is the resulting file can't be opened and the error is:
Quantization table 0x00 was not defined
This is how my code looks like:
JPEGImageWriteParam params = new JPEGImageWriteParam(null);
if (mQMatrix != null) {
JPEGHuffmanTable[] huffmanDcTables = {JPEGHuffmanTable.StdDCLuminance, JPEGHuffmanTable.StdDCChrominance};
JPEGHuffmanTable[] huffmanAcTables = {JPEGHuffmanTable.StdACLuminance, JPEGHuffmanTable.StdACChrominance};
dumpMatrices(mQMatrix);
params.setEncodeTables(mQMatrix, huffmanDcTables, huffmanAcTables);
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
Iterator writers = ImageIO.getImageWritersByFormatName("JPEG");
ImageWriter imageWriter = (ImageWriter) writers.next();
ImageOutputStream imageOutputStream = ImageIO.createImageOutputStream(outputStream);
imageWriter.setOutput(imageOutputStream);
imageWriter.write(null, new IIOImage(mSourceImage, null, null), params);
mCompressedImageSize = outputStream.size();
try (FileOutputStream fileOutputStream = new FileOutputStream(mOutFileName)) {
fileOutputStream.write(outputStream.toByteArray());
}
mCompressedImage = ImageIO.read(new ByteArrayInputStream(outputStream.toByteArray()));
My guess is that it has something to do with the metadata, but I had no luck finding a solution.
Thanks, R.
UPDATE: Using a hex viewer I determined that the quantization table (DQT - 0xFF, 0xDB section) isn't getting written to the output file. I'm assuming I have to force it to be written somehow.
UPDATE 2: So after actually debugging execution, what I found is that if the tables are set in the parameters object, then metadata isn't generated for neither the quantization not the Huffman tables. If the metadata is missing, then the tables aren't being written in the file. The thing is I see no way to customize the contents of the metadata.