Hello.
I wanted to turn a TIFF image into a PNG image w/ transparent background.

I came up with a way, but it involves reading the image, saving the image as PNG (w/o 
transparency), reading the image back in again, and finally processing the image and 
resaving the image w/ transparent background.


Here is my code:

// Step 1 (load tiff)
RenderedImage tiffImage = JAI.create( "fileload", "my.tiff" );
printInfoAbout( tiffImage );

try
{
   // Step 2 (convert to png & save)
   File tempPngFile = new File( "myTemp.png" );
   ImageIO.write( tiffImage, "png", tempPngFile ); // save as png.
   BufferedImageOp transparentOp = new TransparentImageOp(); // mine

   // Step 3 (reload png & make transparent)
   BufferedImage opaquePng = ImageIO.read( tempPngFile );
   BufferedImage transparentPng = transparentOp.filter( opaquePng,
                                                        null );

   // Step 4 (save transparent png)
   File pngFile = new File( "my.png" ); // with transparent background.
   ImageIO.write( transparentPng, "png", pngFile );
}
catch ( IOException e )
{
   System.out.println ( "Exception caught: " + e.getMessage () );
   e.printStackTrace();
}


Surely, there has to be a way to convert the image w/o going through so many steps.

In particular, there should be a way to manipulate the image w/o saving and reloading 
it. I think my problem is that I don't know how to make the BufferedImage out of the 
tiffImage I created. I don't know which ColorModel to use, because the ones that I do 
try end up with an IllegalArgumentException being thrown at runtime.


Here is some info I took from the TIFF image:

The image's dimensions: 1024x1024

This is the raster: ByteInterleavedRaster: width = 1024 height = 1024
#numDataElements 1
The roster is writable.
Translate X is: 0
Translate Y is: 0

This is the sample model: java.awt.image.PixelInterleavedSampleModel@1050000
data type is: 0 (which is DataBuffer.TYPE_BYTE)
transfer type is: 0 (which is DataBuffer.TYPE_BYTE)
sample model width is: 1024
sample model height is: 1024
# of samples per pixel is: 1
# of data elements is: 1
sample sizes for model are: {8}
the band offsets are: 0

Here are some pixels from getPixels( 100, 100, 3, 10, (int [])null ):
176, 176, 176, 183, 183, 176, 176, 176, 183, 176,
176, 176, 176, 176, 176, 176, 176, 176, 176, 176,
176, 176, 176, 176, 176, 176, 176, 176, 176, 176

A sample would be: 176           This corresponds to a black pixel.

Could anyone help me?
Thanks in advance.

P.S.:
When I try:

BufferedImage opaqueImage = new BufferedImage( tiffImage.getWidth(),
                                               tiffImage.getHeight(),
                                               BufferedImage.TYPE_INT_RGB );
opaqueImage.setData(tiffImage.getData()); // <-- get data from TIFF

I get the following exception:

java.lang.ArrayIndexOutOfBoundsException: 1024
 at 
java.awt.image.SinglePixelPackedSampleModel.setPixels(SinglePixelPackedSampleModel.java:670)

 at java.awt.image.WritableRaster.setPixels(WritableRaster.java:547)
 at sun.awt.image.SunWritableRaster.setPixels(SunWritableRaster.java:132)
 at java.awt.image.BufferedImage.setData(BufferedImage.java:1390)
 at ImageTransformer.transformImages(ImageTransformer.java:208)
 at ImageTransformer.main(ImageTransformer.java:34)


But the TIFF image is 1024 x 1024 pixels. That's what I don't understand.

Do you understand what is happening?

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to