A pure Java alternative to Jai imageio for detecting CMYK images

First, I would like to explain the situation / requirements that led to the problem:

In our web application, we cannot support CMYK images (JPEG) because IE 8 and earlier cannot display them Therefore, we need to detect someone who wants to upload such an image and reject it

Unfortunately, Java's imageio does not read these images, nor does it prevent me from getting the detected color space From the debugging point of view, jpegimagereader internally obtains color space code 11 (which will mean jcs_ycck), but I can't safely access this information

When querying the reader's image type, I don't have any content for CMYK, so I may assume that there is no image type = unsupported image

I use the imaging tool to convert the source CMYK image to RGB to test whether it can be read (I try to simulate the administrator's step when getting the message "CMYK is not supported") However, jpegimagereader will not read the image because it assumes (annotated in the source code) 3 components of RGB color space, but the image header will report 4 components (possibly RGBA or ARGB), so it will throw an illegalargumentexception exception

Therefore, imageio is not an option, because I can't reliably obtain the color space of the image, and I can't tell the administrator why, otherwise, some internal images (which can be displayed by the browser) can be accepted

This led me to try Jai imageio. His clibjpegimagereader did a good job and read all my test images correctly

However, since we deploy applications in JBoss that may host other applications, we want to isolate them as much as possible For AFAIK, I need to install Jai imageio into JRE, otherwise I can make the native libraries available to use them, so other applications may also access them, which may cause side effects (at least we will test a lot to ensure that this is not the case)

This is the explanation of this problem. Here again: is there any pure Java alternative to Jai imageio to reliably detect and possibly convert CMYK images?

Thank you in advance,

Thomas

Solution

I found a solution that can meet our needs: Apache Commons sanselan This library reads JPEG header files very quickly and accurately (at least all my test images) and some other image formats

The disadvantage is that it won't read JPEG image data, but I can use basic JRE tools

It is very easy to read JPEG images for conversion (imageio also refuses to read):

JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(new FileInputStream( new File(pFilename) ) );
BufferedImage sourceImg = decoder.decodeAsBufferedImage();

So if sanselan tells me that the image is actually CMYK, I get the raster of the source image and convert myself:

for( /*each pixel in the raster,which is represented as int[4]*/ )
{  
   double k = pixel[3] / 255.0;

   double r = (255.0 - pixel[0])*k;
   double g = (255.0 - pixel[1])*k;
   double b = (255.0 - pixel[2])*k;
}

This gives fairly good results when the RGB image is not too bright or dark However, I don't know why multiplying with K prevents brightening JPEG is actually decoded in local code, CMYK - > RGB conversion. I get different things. I just try multiplication to see the visual effect

I would appreciate it if anyone could make a difference in this regard

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>