[JAI-ImageIO] color space conversions

P

patriarch24

Hello all,
I'm new to image processing (not to Java), and I try hard to do a
thing that seems simple to me : convert images' color space to another
color space.
As input, I can get many formats (mainly jpeg, then png and tiff) in
different color spaces (mainly RGB, then CMYK) and I want to transform
all inputs in tiff/sRGB.
Converting the encoding in tiff is pretty simple using JAI (operator
encode does the stuff), but I can't figure out the use of the
"ColorConvert" operator (that seems simple, but by the way it's not).
I tried many code samples from all the web, mixing JAI and the
standard API, but never got a good result.

Thanks in advance.
The two main problems are :
- the ColorModel and image SampleModel are not compatible
- the convertion is done but the resulting image is negative.

So my question is : how can I convert from a color space to another
the simplest (and the right) way ?

==========

I managed to write a code creating an output, but this output is
really darker than the initial one. This is the code (got it from the
web) :

-------------------

private static void convertToCmykColorSpace(String fileName) {
PlanarImage planarImage = JAI.create("fileload", fileName);
ColorModel colorModelInput = planarImage.getColorModel();
ColorSpace colorSpaceInput = colorModelInput.getColorSpace();
ICC_Profile profileOutput = null;
try {
profileOutput = ICC_Profile.getInstance("C:/java/libs/JAI/CMYK.pf");
} catch (IOException e) {
e.printStackTrace();
return;
}
ColorSpace colorspaceOutput = new ICC_ColorSpace(profileOutput);
PlanarImage planarImageProfile = convertColorSpace(planarImage,
colorSpaceInput, colorspaceOutput);

File imageFile = new File(fileName);
String newFilename = FilenameUtils.getFullPath(fileName)
+ FilenameUtils.getBaseName(fileName) + "_convertedtocmyk."
+ FilenameUtils.getExtension(fileName);

JAI.create("filestore", planarImageProfile, newFilename);
}

private static PlanarImage convertColorSpace(PlanarImage
planarImageInput,
ColorSpace colorSpaceInput, ColorSpace colorSpaceOutput) {

ColorModel colorModelInput = RasterFactory.createComponentColorModel(
planarImageInput.getSampleModel().getDataType(),
colorSpaceInput, false, false, Transparency.OPAQUE);

ImageLayout imageLayoutInput = new ImageLayout();
imageLayoutInput.setColorModel(colorModelInput);
RenderingHints RenderingHintsInput = new RenderingHints(
JAI.KEY_IMAGE_LAYOUT, imageLayoutInput);
ParameterBlock parameterBlockInput = new ParameterBlock();
parameterBlockInput.addSource(planarImageInput);
parameterBlockInput.add(planarImageInput.getSampleModel().getDataType());
PlanarImage planarInputImageInputWithProfile = JAI.create("format",
parameterBlockInput, RenderingHintsInput);

ColorModel colorModelOutput = RasterFactory
..createComponentColorModel(planarInputImageInputWithProfile
..getSampleModel().getDataType(), colorSpaceOutput,
false, false, Transparency.OPAQUE);

ImageLayout imageLayoutOutput = new ImageLayout();
imageLayoutOutput.setSampleModel(colorModelOutput
..createCompatibleSampleModel(planarInputImageInputWithProfile
..getWidth(), planarInputImageInputWithProfile
..getHeight()));
RenderingHints renderingHintsOutput = new RenderingHints(
JAI.KEY_IMAGE_LAYOUT, imageLayoutOutput);
ParameterBlock parameterBlockOutput = new ParameterBlock();
parameterBlockOutput.addSource(planarInputImageInputWithProfile);
parameterBlockOutput.add(colorModelOutput);

return JAI.create("ColorConvert", parameterBlockOutput,
renderingHintsOutput);
}
 
K

Knute Johnson

patriarch24 said:
Hello all,
I'm new to image processing (not to Java), and I try hard to do a
thing that seems simple to me : convert images' color space to another
color space.
As input, I can get many formats (mainly jpeg, then png and tiff) in
different color spaces (mainly RGB, then CMYK) and I want to transform
all inputs in tiff/sRGB.
Converting the encoding in tiff is pretty simple using JAI (operator
encode does the stuff), but I can't figure out the use of the
"ColorConvert" operator (that seems simple, but by the way it's not).
I tried many code samples from all the web, mixing JAI and the
standard API, but never got a good result.

Thanks in advance.
The two main problems are :
- the ColorModel and image SampleModel are not compatible
- the convertion is done but the resulting image is negative.

So my question is : how can I convert from a color space to another
the simplest (and the right) way ?

==========

I managed to write a code creating an output, but this output is
really darker than the initial one. This is the code (got it from the
web) :

-------------------

private static void convertToCmykColorSpace(String fileName) {
PlanarImage planarImage = JAI.create("fileload", fileName);
ColorModel colorModelInput = planarImage.getColorModel();
ColorSpace colorSpaceInput = colorModelInput.getColorSpace();
ICC_Profile profileOutput = null;
try {
profileOutput = ICC_Profile.getInstance("C:/java/libs/JAI/CMYK.pf");
} catch (IOException e) {
e.printStackTrace();
return;
}
ColorSpace colorspaceOutput = new ICC_ColorSpace(profileOutput);
PlanarImage planarImageProfile = convertColorSpace(planarImage,
colorSpaceInput, colorspaceOutput);

File imageFile = new File(fileName);
String newFilename = FilenameUtils.getFullPath(fileName)
+ FilenameUtils.getBaseName(fileName) + "_convertedtocmyk."
+ FilenameUtils.getExtension(fileName);

JAI.create("filestore", planarImageProfile, newFilename);
}

private static PlanarImage convertColorSpace(PlanarImage
planarImageInput,
ColorSpace colorSpaceInput, ColorSpace colorSpaceOutput) {

ColorModel colorModelInput = RasterFactory.createComponentColorModel(
planarImageInput.getSampleModel().getDataType(),
colorSpaceInput, false, false, Transparency.OPAQUE);

ImageLayout imageLayoutInput = new ImageLayout();
imageLayoutInput.setColorModel(colorModelInput);
RenderingHints RenderingHintsInput = new RenderingHints(
JAI.KEY_IMAGE_LAYOUT, imageLayoutInput);
ParameterBlock parameterBlockInput = new ParameterBlock();
parameterBlockInput.addSource(planarImageInput);
parameterBlockInput.add(planarImageInput.getSampleModel().getDataType());
PlanarImage planarInputImageInputWithProfile = JAI.create("format",
parameterBlockInput, RenderingHintsInput);

ColorModel colorModelOutput = RasterFactory
.createComponentColorModel(planarInputImageInputWithProfile
.getSampleModel().getDataType(), colorSpaceOutput,
false, false, Transparency.OPAQUE);

ImageLayout imageLayoutOutput = new ImageLayout();
imageLayoutOutput.setSampleModel(colorModelOutput
.createCompatibleSampleModel(planarInputImageInputWithProfile
.getWidth(), planarInputImageInputWithProfile
.getHeight()));
RenderingHints renderingHintsOutput = new RenderingHints(
JAI.KEY_IMAGE_LAYOUT, imageLayoutOutput);
ParameterBlock parameterBlockOutput = new ParameterBlock();
parameterBlockOutput.addSource(planarInputImageInputWithProfile);
parameterBlockOutput.add(colorModelOutput);

return JAI.create("ColorConvert", parameterBlockOutput,
renderingHintsOutput);
}

Here is a simple program that reads a color image and converts it to
grayscale using ColorConvertOp. Using this as a sample you should be
able to convert anything. I wrote this for a fellow that wanted to draw
a transparent color image over a grayscale image but the ColorConvertOp
is what you are interested in.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;

public class test6 extends JPanel {
BufferedImage image;
BufferedImage gray;
BufferedImage color;

public test6() {
setPreferredSize(new Dimension(400,300));
try {
// read an image from the disk
image = ImageIO.read(new File("kittens.jpg"));

// create a grayscale image the same size
gray = new BufferedImage(image.getWidth(),image.getHeight(),
BufferedImage.TYPE_BYTE_GRAY);

// create a color image with alpha for output
color = new BufferedImage(400,300,BufferedImage.TYPE_INT_ARGB);

// convert the original colored image to grayscale
ColorConvertOp op = new ColorConvertOp(
image.getColorModel().getColorSpace(),
gray.getColorModel().getColorSpace(),null);
op.filter(image,gray);

// draw the grayscale image on the output image
Graphics g = color.getGraphics();
g.drawImage(gray,0,0,null);

// draw some transparent blue pixels over the whole image
g.setColor(new Color(0,0,255,80));
g.fillRect(0,0,400,300);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

public void paintComponent(Graphics g) {
g.drawImage(color,0,0,null);
}

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

test6 t6 = new test6();
f.add(t6,BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
}
 
P

patriarch24

This seems interesting, thank you. But it converts to grayscale image,
which is a current use case (I think I have found billions code
samples for that). Converting from rgb to cmyk or from cmyk to rgb
really seems to be a hard question (resulting images are too bright or
too dark depending on the direction in my samples).
Any other suggestion ?
 
K

Knute Johnson

patriarch24 said:
This seems interesting, thank you. But it converts to grayscale image,
which is a current use case (I think I have found billions code
samples for that). Converting from rgb to cmyk or from cmyk to rgb
really seems to be a hard question (resulting images are too bright or
too dark depending on the direction in my samples).
Any other suggestion ?

I don't know how to create a CMYK ColorSpace. Why don't you send me an
image in CMYK and I'll play with it.
 
P

patriarch24

In addition, I would like to set the image icc profile. I can't figure
out the way to do that in pure Java (I managed to do it using JMagick,
the ImageMagick Java port).

Can anyone help me ?
 
Joined
Apr 23, 2009
Messages
1
Reaction score
0
Did u find a solution

Hi Friends.

I also want to convert a CMYK image to RGB image, the image being JPEG. Was anyone able to convert the image from CMYK to JPEG.

Any help is highly appreciated

Thanks, in advance
Ankur.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top