Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrong ImageTypeSpecifier returned by TurboJpegImageReader #129

Closed
eroux opened this issue May 3, 2021 · 0 comments
Closed

wrong ImageTypeSpecifier returned by TurboJpegImageReader #129

eroux opened this issue May 3, 2021 · 0 comments
Labels

Comments

@eroux
Copy link

eroux commented May 3, 2021

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

@jbaiter jbaiter added the bug label May 6, 2021
@jbaiter jbaiter closed this as completed in bc16b22 May 6, 2021
jbaiter added a commit that referenced this issue May 6, 2021
Fix getImageTypes() implementations on readers (fixes #129)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants