dbmdz/imageio-jnr

wrong ImageTypeSpecifier returned by TurboJpegImageReader

Closed this issue · 0 comments

eroux commented

As exemplified by the code below, TurboJpegImageReader returns a wrong ImageTypeSpecifier with 3 components instead of 1 for a grayscale jpeg. This makes our server crash when we try to reconcile the image with the ICC profile (read through apache.commons.imaging), which has the correct value (1 component).

package io.bdrc.iiif;

import static org.junit.Assert.assertEquals;

import java.awt.color.ICC_Profile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

import org.apache.commons.imaging.ImageReadException;
import org.apache.commons.imaging.Imaging;
import org.junit.Test;

import com.google.common.collect.Streams;

import de.digitalcollections.turbojpeg.imageio.TurboJpegImageReader;
import io.bdrc.iiif.exceptions.IIIFException;
import io.bdrc.iiif.exceptions.InvalidParametersException;
import io.bdrc.iiif.exceptions.UnsupportedFormatException;

public class TestNbComponents {

    @Test
    public void bugNbComponents() throws IOException, UnsupportedFormatException, InvalidParametersException, IIIFException, UnsupportedOperationException, ImageReadException {
        final String infilename = "src/test/resources/gray-icc.jpg";
        // reader number of components through TurboJpegImageReader
        ImageReader reader =
                Streams.stream(ImageIO.getImageReadersByFormatName("jpeg"))
                    .filter(TurboJpegImageReader.class::isInstance)
                    .findFirst()
                    .get();
        InputStream is = new FileInputStream(new File(infilename));
        ImageInputStream iis = ImageIO.createImageInputStream(is);
        reader.setInput(iis);
        final int nbComponentsImage = reader.getRawImageType(0).getNumComponents();
        System.out.println("nbcomponents image: "+nbComponentsImage);
        reader.dispose();
        iis.close();
        is.close();
        is = new FileInputStream(new File(infilename));
        ICC_Profile icc = Imaging.getICCProfile(is, infilename);
        final int nbComponentsIcc = icc.getNumComponents();
        System.out.println("nbcomponents icc: "+nbComponentsIcc);
        is.close();
        assertEquals(nbComponentsIcc, nbComponentsImage);
    }
    
}

gray-icc

Possibly related: haraldk/TwelveMonkeys#573