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);
}
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);
}