merging 4 single band images into one 4-band cmyk image

View: New views
10 Messages — Rating Filter:   Alert me  

merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

hi all,
  i 'm new in java2d and now i want to combine four colorspace c,m,y,k tiff file into one cmyk tiff file. I used bandmerge operator to create and save the image. but the image cannot be viewed by fax viewer and it appears to be white in photoshop.

I also used the code in this link:
http://java.sun.com/products/javamedia/jai/forDevelopers/samples/BandMergeTest.java

to merge the four files but not succeed. i'm sure the four files is in the right format,c,m,y,k respectively.

does anybody know what's wrong with that? thanks!
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=370282

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

for using bandmerge op to create image, here is my code:
String[] args={"f:\\tiff\\c.tif","f:\\tiff\\m.tif","f:\\tiff\\y.tif","f:\\tiff\\k.tif"};
PlanarImage[] inputs = new PlanarImage[args.length];
for(int im=0;im<args.length;im++)
inputs[im = JAI.create("fileload", args[im]);
ParameterBlock pb = new ParameterBlock();
for(int im=0;im<args.length;im++)
pb.setSource(inputs[im], im);
PlanarImage result = JAI.create("bandmerge",pb,null);
JAI.create("filestore",result,"f:\\multiband.tiff","TIFF");
that's seems reasonable, anything i've missed?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=370284

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


RE: Re: merging 4 single band images into one 4-band cmyk image

by Nidel, Mike :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I think I know your problem.

When you create the 4-band image, you haven't given it any information to
tell it that it's a CMYK image. It could just as easily be RGBA or anything
else.

Here is what I think you want to do: create a ColorModel that corresponds to
your CMYK format, most likely a ComponentColorModel. When you construct it,
pass in the result of ColorSpace.getInstance(ColorSpace.TYPE_CMYK), along
with the other parameters.

Then, when you have the ColorModel built, pass that into an ImageLayout.
Then add the ImageLayout to a new RenderingHints. Finally, pass the
RenderingHints to the call to JAI.create() and that should be it. Then
the TIFF writer should know that it's a CMYK image and write it accordingly.
You could verify this by getting a separate program such as libtiff to
examine the created TIFF file and make sure the metadata is right.

If you need help with any of the details, feel free to ask us on the list.


> -----Original Message-----
> From: jai-interest@... [mailto:jai-
> interest@...]
> Sent: Tuesday, November 03, 2009 4:56 AM
> To: interest@...
> Subject: [JAI] Re: merging 4 single band images into one 4-band cmyk
> image
>
> for using bandmerge op to create image, here is my code:
> String[]
> args={"f:\\tiff\\c.tif","f:\\tiff\\m.tif","f:\\tiff\\y.tif","f:\\tiff\\
> k.tif"};
> PlanarImage[] inputs = new PlanarImage[args.length];
> for(int im=0;im<args.length;im++)
> inputs[im = JAI.create("fileload", args[im]);
> ParameterBlock pb = new ParameterBlock();
> for(int im=0;im<args.length;im++)
> pb.setSource(inputs[im], im);
> PlanarImage result = JAI.create("bandmerge",pb,null);
> JAI.create("filestore",result,"f:\\multiband.tiff","TIFF");
> that's seems reasonable, anything i've missed?
> [Message sent by forum member 'mr_empty' (mr_empty123@...)]
>
> http://forums.java.net/jive/thread.jspa?messageID=370284
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@...
> For additional commands, e-mail: interest-help@...


---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: RE: Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thanks for your reply. now i have modified my code by creating the the colorspace myself, but i can't create cmyk colorspace by using this statement:
ColorSpace cc=ColorSpace.getInstance(ColorSpace.TYPE_CMYK); since the getinstance recognise TYPE_CMYK as unknown colorspace. here 's my code:

SampleModel sm =
RasterFactory.createPixelInterleavedSampleModel(
DataBuffer.TYPE_BYTE,
TILE_WIDTH,
TILE_HEIGHT,
numBands);
ColorModel cm = PlanarImage.createColorModel(sm);




int[] bits = { 8, 8, 8, 8};


try{
ICC_Profile profileOutput = ICC_Profile.getInstance("CMYK.pf");
cc=new ICC_ColorSpace(profileOutput);


ColorModel colorModel = new ComponentColorModel(cc,
bits, false, false,
Transparency.OPAQUE,
DataBuffer.TYPE_BYTE);


ImageLayout imageLayout = new ImageLayout();
imageLayout.setColorModel(colorModel);
imageLayout.setSampleModel(sm);
RenderingHints rendHints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT,imageLayout);
ParameterBlock pb = new ParameterBlock();


for(int i = 0; i < fileNames.length; i++) {
pb.addSource(src[i]);
}
RenderedImage finalImage = JAI.create("bandmerge", pb, rendHints);
TIFFEncodeParam encodeParam = new TIFFEncodeParam();
encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
JAI.create("filestore",finalImage,"c:\\merge.tiff","TIFF",encodeParam);



the result image is white opened by photoshop. i can't think of ways to solve it, why is that?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=370422

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


RE: RE: Re: merging 4 single band images into one 4-band cmyk image

by Nidel, Mike :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

First, what happens if you skip the compression?

Second, what happens if instead of the JAI TIFF writer, you use the
JAI ImageIO tools one?

> -----Original Message-----
> From: jai-interest@... [mailto:jai-
> interest@...]
> Sent: Wednesday, November 04, 2009 1:33 AM
> To: interest@...
> Subject: Re: RE: [JAI] Re: merging 4 single band images into one 4-band
> cmyk image
>
> thanks for your reply. now i have modified my code by creating the the
> colorspace myself, but i can't create cmyk colorspace by using this
> statement:
> ColorSpace cc=ColorSpace.getInstance(ColorSpace.TYPE_CMYK); since the
> getinstance recognise TYPE_CMYK as unknown colorspace. here 's my code:
>
> SampleModel sm =
> RasterFactory.createPixelInterleavedSampleModel(
> DataBuffer.TYPE_BYTE,
> TILE_WIDTH,
> TILE_HEIGHT,
> numBands);
> ColorModel cm = PlanarImage.createColorModel(sm);
>
>
>
>
> int[] bits = { 8, 8, 8, 8};
>
>
> try{
> ICC_Profile profileOutput = ICC_Profile.getInstance("CMYK.pf");
> cc=new ICC_ColorSpace(profileOutput);
>
>
> ColorModel colorModel = new ComponentColorModel(cc,
> bits, false, false,
> Transparency.OPAQUE,
> DataBuffer.TYPE_BYTE);
>
>
> ImageLayout imageLayout = new ImageLayout();
> imageLayout.setColorModel(colorModel);
> imageLayout.setSampleModel(sm);
> RenderingHints rendHints = new
> RenderingHints(JAI.KEY_IMAGE_LAYOUT,imageLayout);
> ParameterBlock pb = new ParameterBlock();
>
>
> for(int i = 0; i < fileNames.length; i++) {
> pb.addSource(src[i]);
> }
> RenderedImage finalImage = JAI.create("bandmerge", pb, rendHints);
> TIFFEncodeParam encodeParam = new TIFFEncodeParam();
> encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
> JAI.create("filestore",finalImage,"c:\\merge.tiff","TIFF",encodeParam);
>
>
>
> the result image is white opened by photoshop. i can't think of ways to
> solve it, why is that?
> [Message sent by forum member 'mr_empty' (mr_empty123@...)]
>
> http://forums.java.net/jive/thread.jspa?messageID=370422
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@...
> For additional commands, e-mail: interest-help@...


---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Re: merging 4 single band images into one 4-band cmyk image

by Bob Deen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I have three suggestions, all in the vein of separating the problems.

a) try a different output file type, for testing (jpeg, png, whatever).

b) use "colorconvert" to take your CMYK-labeled image and convert it to
the more usual RGB colorspace.  Save or display that and see if it works.

c) hook it up to an image display rather than a file output.

Those will help nail down whether it's a problem properly attaching the
CMYK color model (b) or if the file writer is perhaps not honoring the
color model (a,c).

Method b is usable in production, if you just want to be able to view
the file properly and don't actually need it stored in cmyk for some reason.

-Bob

Nidel, Mike wrote:

> First, what happens if you skip the compression?
>
> Second, what happens if instead of the JAI TIFF writer, you use the
> JAI ImageIO tools one?
>
>> -----Original Message-----
>> From: jai-interest@... [mailto:jai-
>> interest@...]
>> Sent: Wednesday, November 04, 2009 1:33 AM
>> To: interest@...
>> Subject: Re: RE: [JAI] Re: merging 4 single band images into one 4-band
>> cmyk image
>>
>> thanks for your reply. now i have modified my code by creating the the
>> colorspace myself, but i can't create cmyk colorspace by using this
>> statement:
>> ColorSpace cc=ColorSpace.getInstance(ColorSpace.TYPE_CMYK); since the
>> getinstance recognise TYPE_CMYK as unknown colorspace. here 's my code:
>>
>> SampleModel sm =
>> RasterFactory.createPixelInterleavedSampleModel(
>> DataBuffer.TYPE_BYTE,
>> TILE_WIDTH,
>> TILE_HEIGHT,
>> numBands);
>> ColorModel cm = PlanarImage.createColorModel(sm);
>>
>>
>>
>>
>> int[] bits = { 8, 8, 8, 8};
>>
>>
>> try{
>> ICC_Profile profileOutput = ICC_Profile.getInstance("CMYK.pf");
>> cc=new ICC_ColorSpace(profileOutput);
>>
>>
>> ColorModel colorModel = new ComponentColorModel(cc,
>> bits, false, false,
>> Transparency.OPAQUE,
>> DataBuffer.TYPE_BYTE);
>>
>>
>> ImageLayout imageLayout = new ImageLayout();
>> imageLayout.setColorModel(colorModel);
>> imageLayout.setSampleModel(sm);
>> RenderingHints rendHints = new
>> RenderingHints(JAI.KEY_IMAGE_LAYOUT,imageLayout);
>> ParameterBlock pb = new ParameterBlock();
>>
>>
>> for(int i = 0; i < fileNames.length; i++) {
>> pb.addSource(src[i]);
>> }
>> RenderedImage finalImage = JAI.create("bandmerge", pb, rendHints);
>> TIFFEncodeParam encodeParam = new TIFFEncodeParam();
>> encodeParam.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
>> JAI.create("filestore",finalImage,"c:\\merge.tiff","TIFF",encodeParam);
>>
>>
>>
>> the result image is white opened by photoshop. i can't think of ways to
>> solve it, why is that?
>> [Message sent by forum member 'mr_empty' (mr_empty123@...)]
>>
>> http://forums.java.net/jive/thread.jspa?messageID=370422
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: interest-unsubscribe@...
>> For additional commands, e-mail: interest-help@...
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@...
> For additional commands, e-mail: interest-help@...
>


---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

thanks for suggestions, firstly i need to correct that my resulting image is grayscale opened by photoshop. and i 've tried your suggestions, the result image file is the same, for the suggestion c, it returns an nullpointer exception error, but i'm sure the result renderedimage is not null and is passed to the display function.

its strange that the result image is grayscale since i 've created the colorspace as cmyk, with the band number 4. do u know why?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=370607

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

now i log the colormodel and numband of the image and i found that the colormodel is null, and the numband is 12,though i just merge four 1 band image, and i 've set colormodel. does any body help me?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=371048

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

now i use the following code:


import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import javax.media.jai.*;
import javax.media.jai.widget.*;

public class BandMergeTest extends Frame {
    private static int TILE_WIDTH = 128;
    private static int TILE_HEIGHT = TILE_WIDTH;

    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("Need one or more inputs");
            System.exit(0);
        }

        new BandMergeTest(args);
    }

    BandMergeTest(String[] fileNames) {
        PlanarImage[] src = new PlanarImage[fileNames.length];

        // Load the images keeping track of the number of bands in each
        // and of the total number of bands.
        int numBands = 0;
        Rectangle bounds = null;
        int[] bands = new int[fileNames.length];
        for(int i = 0; i < fileNames.length; i++) {
            src[i] = JAI.create("fileload", fileNames[i]);
            SampleModel ssm = src[i].getSampleModel();
            if(ssm.getTransferType() != DataBuffer.TYPE_BYTE) {
                System.out.println("Accept byte data only");
                System.exit(0);
            }
            bands[i] = ssm.getNumBands();
            numBands += bands[i];
            if(bounds == null) {
                bounds = src[i].getBounds();
            } else {
                bounds = bounds.intersection(src[i].getBounds());
                if(bounds.isEmpty()) {
                   System.out.println("Null intersection");
                   System.exit(0);
                }
            }
        }

        // Create a SampleModel appropriate for the total number of bands,
        // a ColorModel appropriate for this SampleMode,l and using these
        // objects create a TiledImage of the intersected area.
        SampleModel sm =
            RasterFactory.createPixelInterleavedSampleModel(
                                                    DataBuffer.TYPE_BYTE,
                                                    TILE_WIDTH,
                                                    TILE_HEIGHT,
                                                    numBands);
        ColorModel cm = PlanarImage.createColorModel(sm);
        TiledImage dst = new TiledImage(bounds.x, bounds.y,
                                        bounds.width, bounds.height,
                                        bounds.x, bounds.y,
                                        sm, cm);

        // Loop over the tiles of the TiledImage. For each tile create a band
        // child and insert the data of the appropriate source image into
        // the tile.
        int ny = dst.getNumYTiles();
        int nx = dst.getNumXTiles();
        for(int ty = 0; ty < ny; ty++) {
            for(int tx = 0; tx < nx; tx++) {
                WritableRaster parent = dst.getWritableTile(tx, ty);
                bounds = parent.getBounds();
                int startBand = 0;
                for(int i = 0; i < src.length; i++) {
                    int[] bandList = new int[bands[i]];
                    int k = startBand;
                    for(int j = 0; j < bands[i]; j++ ) {
                        bandList[j] = k++;
                    }
                    startBand += bands[i];
                    WritableRaster child =
                    parent.createWritableChild(bounds.x, bounds.y,
                                               bounds.width, bounds.height,
                                               bounds.x, bounds.y,
                                               bandList);
                    child.setRect(src[i].getData(bounds));
                }
                dst.releaseWritableTile(tx, ty);
           }
        }
                JAI.create("filestore",dst,"c:\\cmyk.tif","TIFF");

        // Display the result.
        add(new ScrollingImagePanel(dst, 640, 480));
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) { System.exit(0); }
       });
        pack();
        show();
    }
}


the frame pop up can show the cmyk merged image with right color, but the tiff file,jpg, or png saved display a whole page white, its an odd case. can anybody analyse this?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=371112

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...


Re: Re: merging 4 single band images into one 4-band cmyk image

by jai-interest-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

now i use the following code:

import java.awt.*;
import java.awt.event.*;
import java.awt.color.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import javax.media.jai.*;
import javax.media.jai.widget.*;

public class BandMergeTest extends Frame {
    private static int TILE_WIDTH = 128;
    private static int TILE_HEIGHT = TILE_WIDTH;

    public static void main(String[] args) {
        if(args.length == 0) {
            System.out.println("Need one or more inputs");
            System.exit(0);
        }

        new BandMergeTest(args);
    }

    BandMergeTest(String[] fileNames) {
        PlanarImage[] src = new PlanarImage[fileNames.length];

        // Load the images keeping track of the number of bands in each
        // and of the total number of bands.
        int numBands = 0;
        Rectangle bounds = null;
        int[] bands = new int[fileNames.length];
        for(int i = 0; i < fileNames.length; i++) {
            src[i= JAI.create("fileload", fileNames[i]);
            SampleModel ssm = src[i.getSampleModel();
            if(ssm.getTransferType() != DataBuffer.TYPE_BYTE) {
                System.out.println("Accept byte data only");
                System.exit(0);
            }
            bands[i = ssm.getNumBands();
            numBands += bands[i;
            if(bounds == null) {
                bounds = src[i.getBounds();
            } else {
                bounds = bounds.intersection(src[i.getBounds());
                if(bounds.isEmpty()) {
                   System.out.println("Null intersection");
                   System.exit(0);
                }
            }
        }

               SampleModel sm =
            RasterFactory.createPixelInterleavedSampleModel(
                                                    DataBuffer.TYPE_BYTE,
                                                    TILE_WIDTH,
                                                    TILE_HEIGHT,
                                                    numBands);
        ColorModel cm = PlanarImage.createColorModel(sm);
        TiledImage dst = new TiledImage(bounds.x, bounds.y,
                                        bounds.width, bounds.height,
                                        bounds.x, bounds.y,
                                        sm, cm);

        int ny = dst.getNumYTiles();
        int nx = dst.getNumXTiles();
        for(int ty = 0; ty < ny; ty++) {
            for(int tx = 0; tx < nx; tx++) {
                WritableRaster parent = dst.getWritableTile(tx, ty);
                bounds = parent.getBounds();
                int startBand = 0;
                for(int i = 0; i < src.length; i++) {
                    int[] bandList = new int[bands[i];
                    int k = startBand;
                    for(int j = 0; j < bands[i; j++ ) {
                        bandList[j = k++;
                    }
                    startBand += bands[i;
                    WritableRaster child =
                    parent.createWritableChild(bounds.x, bounds.y,
                                               bounds.width, bounds.height,
                                               bounds.x, bounds.y,
                                               bandList);
                    child.setRect(src[i.getData(bounds));
                }
                dst.releaseWritableTile(tx, ty);
           }
        }


            JAI.create("filestore",dst,"c:\\cmyk.tif","TIFF");

        try{
        File out=new File("c:\\cmyk2.jpg");
        System.out.println("dst:"+dst);
        ImageIO.write(dst, "jpg", out );
        }catch(IOException e){}
       

        add(new ScrollingImagePanel(dst, 640, 480));
        addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent e) { System.exit(0); }
       });
        pack();
        show();
    }
}


the image in the frame poped up displayed with right color, but the tiff,jpeg or png saved just displayed a whole page white. its quite odd. can anybody analyse this?
[Message sent by forum member 'mr_empty' (mr_empty123@...)]

http://forums.java.net/jive/thread.jspa?messageID=371114

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@...
For additional commands, e-mail: interest-help@...