Skip to main content

conversion of tiff to png

2 replies [Last post]
question13
Offline
Joined: 2006-02-14

Hi @ all
I'm a beginner of working with JAI an i tried to convert a tiff file that i load from database to an byte array to an png: I don't know what Bad endianess tag error or unable zo render RenderedOp mean and how to solve this errors!
Has someone a solution to this problem?
Thanks in advance

Here is the source code:

public static void main(String []args) {
TestConvert tc = new TestConvert();
String result = tc.ConvTifPNG();
System.out.println(result);
}

private String ConvTifPNG(){

String sourcefilename = "4711_4.tif";
String targetfilename = "4711_2b.PNG";
File dstFile = new File("d:\\4711_2c.png/");
File srcFile = new File (sourcefilename);
byte picBytes [] = new byte [(int)srcFile.length() ];
ByteArrayOutputStream bs = new ByteArrayOutputStream();

try {

ByteArraySeekableStream bass = new ByteArraySeekableStream(picBytes);

ImageDecoder dec = ImageCodec.createImageDecoder("tiff",bass, null);
RenderedImage image =
dec.decodeAsRenderedImage(0);
System.out.println("Starte JAICreate!");
JAI.create("encode",in,bs,"PNG",null
bass.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}

byte [] bytes = new byte[(int)bs.size()];
try {

bs.write(bytes);
FileOutputStream fos = new FileOutputStream(dstFile);
fos.write(bytes);
bs.close();
fos.close();

} catch (IOException e1) {

e1.printStackTrace();
}

return "conversion successfully";

and here the exception:

java.lang.RuntimeException: - Unable to render RenderedOp for this operation.
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:838)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
at javax.media.jai.RenderedOp.getSampleModel(RenderedOp.java:2244)
at com.sun.media.jai.codecimpl.PNGCodec.canEncodeImage(PNGCodec.java:62)
at javax.media.jai.operator.EncodeDescriptor.validateArguments(EncodeDescriptor.java:172)
at javax.media.jai.JAI.createNS(JAI.java:1090)
at javax.media.jai.JAI.create(JAI.java:977)
at javax.media.jai.JAI.create(JAI.java:1672)
at TestConvert.ConvTifJpg(TestConvert.java:57)
at TestConvert.main(TestConvert.java:32)
java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
at com.sun.media.jai.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:588)
at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:105)
at com.sun.media.jai.codecimpl.TIFFImageDecoder.decodeAsRenderedImage(TIFFImageDecoder.java:109)
at TestConvert.ConvTifJpg(TestConvert.java:52)
at TestConvert.main(TestConvert.java:32)
Exception in thread "main"

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

The "bad endianness" message means your TIFF stream is not compliant with the
TIFF 6.0 specification which may be found listed in the references here:

http://java.sun.com/products/java-media/jai/forDevelopers/jai-imageio-1_...

Brian

On Thu, 23 Jun 2005 jai-interest@javadesktop.org wrote:

> Hi @ all
> I'm a beginner of working with JAI an i tried to convert a tiff file that i load from database to an byte array to an png: I don't know what Bad endianess tag error or unable zo render RenderedOp mean and how to solve this errors!
> Has someone a solution to this problem?
> Thanks in advance
>
> Here is the source code:
>
> public static void main(String []args) {
> TestConvert tc = new TestConvert();
> String result = tc.ConvTifPNG();
> System.out.println(result);
> }
>
>
> private String ConvTifPNG(){
>
> String sourcefilename = "4711_4.tif";
> String targetfilename = "4711_2b.PNG";
> File dstFile = new File("d:\\4711_2c.png/");
> File srcFile = new File (sourcefilename);
> byte picBytes [] = new byte [(int)srcFile.length() ];
> ByteArrayOutputStream bs = new ByteArrayOutputStream();
>
>
>
> try {
>
> ByteArraySeekableStream bass = new ByteArraySeekableStream(picBytes);
>
> ImageDecoder dec = ImageCodec.createImageDecoder("tiff",bass, null);
> RenderedImage image =
> dec.decodeAsRenderedImage(0);
> System.out.println("Starte JAICreate!");
> JAI.create("encode",in,bs,"PNG",null
> bass.close();
> } catch (IOException e) {
> e.printStackTrace();
> System.exit(0);
> }
>
> byte [] bytes = new byte[(int)bs.size()];
> try {
>
> bs.write(bytes);
> FileOutputStream fos = new FileOutputStream(dstFile);
> fos.write(bytes);
> bs.close();
> fos.close();
>
> } catch (IOException e1) {
>
> e1.printStackTrace();
> }
>
> return "conversion successfully";
>
> and here the exception:
>
> java.lang.RuntimeException: - Unable to render RenderedOp for this operation.
> at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:838)
> at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
> at javax.media.jai.RenderedOp.getSampleModel(RenderedOp.java:2244)
> at com.sun.media.jai.codecimpl.PNGCodec.canEncodeImage(PNGCodec.java:62)
> at javax.media.jai.operator.EncodeDescriptor.validateArguments(EncodeDescriptor.java:172)
> at javax.media.jai.JAI.createNS(JAI.java:1090)
> at javax.media.jai.JAI.create(JAI.java:977)
> at javax.media.jai.JAI.create(JAI.java:1672)
> at TestConvert.ConvTifJpg(TestConvert.java:57)
> at TestConvert.main(TestConvert.java:32)
> java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or 0x4d4d).
> at com.sun.media.jai.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:588)
> at com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.java:105)
> at com.sun.media.jai.codecimpl.TIFFImageDecoder.decodeAsRenderedImage(TIFFImageDecoder.java:109)
> at TestConvert.ConvTifJpg(TestConvert.java:52)
> at TestConvert.main(TestConvert.java:32)
> Exception in thread "main"
> ---
> [Message sent by forum member 'question13' (hahn)]
>
> http://www.javadesktop.org/forums/thread.jspa?messageID=94450&#94450
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

----------------
Brian Burkhalter
Advanced Development/Media & Entertainment
Market Development Engineering
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

Aaron Bruegl

I tried testing you're code but had some problems - like, where is the field
"in"? Anyway maybe the following will help.

I use JAI to convert multi-page tiffs to PNG. Here is a class that you might
find helpful, you can give it the stream to the TIFF and it will convert it
to PNGs and scale it by the tiff's resolution. Some information I got from
the TIFF 6.0 standards document which you can find by internet search.

Just provide a "input.tif" in the same directory for the main to work.

// From the TIFF 6.0 Standard 1992
import com.sun.media.jai.codec.SeekableStream;
import com.sun.media.jai.codec.TIFFDecodeParam;
import com.sun.media.jai.codec.TIFFDirectory;

import java.awt.RenderingHints;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.ArrayList;

import javax.imageio.ImageIO;

import javax.media.jai.Interpolation;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.media.jai.TiledImage;

public class TIFF60 {
// TIFF Tag names associated with their Decimal value, from the TIFF 6.0
Standard
public static final int IMAGE_WIDTH = 256;
public static final int IMAGE_LENGTH = 257;
public static final int BITS_PER_SAMPLE = 258;
public static final int COMPRESSION = 259;
public static final int SAMPLES_PER_PIXEL = 277;
public static final int X_RESOLUTION = 282;
public static final int Y_RESOLUTION = 283;
public static final int RESOLUTION_UNIT = 296;
private static final int DEFAULT_RESOLUTION = 200;

private TIFF60() {
}

/**
* Takes an inputstream to a tiff (can be multipage) and converts to
* Arraylist of PlanarImages formatting - scale the tiff to its proper
aspect by its resolution
*
* @param Input stream of the TIFF image
* @return ArrayList of PlanarImages
*/
public static synchronized ArrayList
readAndFormat(InputStream in) {
ArrayList images = new ArrayList();

SeekableStream s = SeekableStream.wrapInputStream(in, true);
ParameterBlock pb = new ParameterBlock();
pb.add(s);

TIFFDecodeParam param = new TIFFDecodeParam();
pb.add(param);

long nextOffset = 0;
int count = 1;

do {
PlanarImage pi = JAI.create("tiff", pb);
TIFFDirectory dir = (TIFFDirectory)
pi.getProperty("tiff_directory");

PlanarImage formattedPi = formatTiffImage(pi, null);
images.add(formattedPi);
nextOffset = dir.getNextIFDOffset();

if (nextOffset != 0) {
param.setIFDOffset(nextOffset);
}

count++;
} while (nextOffset != 0);

return images;
}

/**
* Scales properly to a given size for a tiff image using Nearest
Neighbor to the correct aspect ratio
*/
public static synchronized RenderedOp formatTiffImage(PlanarImage
source, int width, int height, RenderingHints hints) {
float hRatio = ((float) height) / ((float) source.getHeight());
float wRatio = ((float) width) / ((float) source.getWidth());
float scale = Math.min(hRatio, wRatio);

ParameterBlock scale_pb = new ParameterBlock();
scale_pb.addSource(source).add(scale).add(scale);

return JAI.create("SubsampleBinaryToGray", scale_pb, hints);
}

/**
* Scales the tiff image to its viewable size using Nearest Neighbor
*/
public static synchronized PlanarImage formatTiffImage(PlanarImage
source, RenderingHints hints) {
TIFFDirectory dir = (TIFFDirectory)
source.getProperty("tiff_directory");

try {
// Its does not have the tiff dierectory
if (dir != null) {
// these r from the TIFF 6.0 standard in 1992
float xRes = dir.getFieldAsFloat(X_RESOLUTION);
float yRes = dir.getFieldAsFloat(Y_RESOLUTION);
//int imageHeight = dir.getField(IMAGE_LENGTH).getAsInt(0);
//int imageWidth = dir.getField(IMAGE_WIDTH).getAsInt(0);
//int resUnit = dir.getField(RESOLUTION_UNIT).getAsInt(0);

float xScaleFactor = (float) DEFAULT_RESOLUTION / xRes;
float yScaleFactor = (float) DEFAULT_RESOLUTION / yRes;
ParameterBlock pb = new ParameterBlock();
pb.addSource(source);
pb.add(xScaleFactor); // x scale
pb.add(yScaleFactor); // y scale
pb.add(0.0F); // x translation
pb.add(0.0F); // y translation

pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));

RenderedOp rop = JAI.create("scale", pb, hints);
//System.out.println("@rop.getWidth() " + rop.getWidth());
//System.out.println("@rop.getHeight() " + rop.getHeight());

return rop;
}
} catch (Exception e) {
System.out.println("Looks like the tiff images does not have
fields");
e.printStackTrace();
}

return source;
}

public static void main(String[] args) {
try {
// Parse the Multipage tiff into multiple PlanarImages
ArrayList imagesPng = TIFF60.readAndFormat(new
FileInputStream("input.tif"));

// Convert the png images to byte[]'s and add them to the
ArrayList to return
int count = 1;
for(PlanarImage pi : imagesPng) {
ImageIO.write(pi, "png", new FileOutputStream("output" +
count + ".png"));
count++;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}

Aaron Bruegl

-----Original Message-----
From: jai-interest@javadesktop.org [mailto:jai-interest@javadesktop.org]
Sent: Thursday, June 23, 2005 4:59 AM
To: interest@jai.dev.java.net
Subject: [JAI] conversion of tiff to png

Hi @ all
I'm a beginner of working with JAI an i tried to convert a tiff file that i
load from database to an byte array to an png: I don't know what Bad
endianess tag error or unable zo render RenderedOp mean and how to solve
this errors!
Has someone a solution to this problem?
Thanks in advance

Here is the source code:

public static void main(String []args) {
TestConvert tc = new TestConvert();
String result = tc.ConvTifPNG();
System.out.println(result);
}

private String ConvTifPNG(){

String sourcefilename = "4711_4.tif";
String targetfilename = "4711_2b.PNG";
File dstFile = new File("d:\\4711_2c.png/");
File srcFile = new File (sourcefilename);
byte picBytes [] = new byte [(int)srcFile.length() ];
ByteArrayOutputStream bs = new ByteArrayOutputStream();

try {

ByteArraySeekableStream bass = new
ByteArraySeekableStream(picBytes);

ImageDecoder dec =
ImageCodec.createImageDecoder("tiff",bass, null);
RenderedImage image =
dec.decodeAsRenderedImage(0);
System.out.println("Starte JAICreate!");
JAI.create("encode",in,bs,"PNG",null
bass.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}

byte [] bytes = new byte[(int)bs.size()];
try {

bs.write(bytes);
FileOutputStream fos = new FileOutputStream(dstFile);
fos.write(bytes);
bs.close();
fos.close();

} catch (IOException e1) {

e1.printStackTrace();
}

return "conversion successfully";

and here the exception:

java.lang.RuntimeException: - Unable to render RenderedOp for this
operation.
at javax.media.jai.RenderedOp.createInstance(RenderedOp.java:838)
at javax.media.jai.RenderedOp.createRendering(RenderedOp.java:878)
at javax.media.jai.RenderedOp.getSampleModel(RenderedOp.java:2244)
at
com.sun.media.jai.codecimpl.PNGCodec.canEncodeImage(PNGCodec.java:62)
at
javax.media.jai.operator.EncodeDescriptor.validateArguments(EncodeDescriptor
.java:172)
at javax.media.jai.JAI.createNS(JAI.java:1090)
at javax.media.jai.JAI.create(JAI.java:977)
at javax.media.jai.JAI.create(JAI.java:1672)
at TestConvert.ConvTifJpg(TestConvert.java:57)
at TestConvert.main(TestConvert.java:32)
java.lang.IllegalArgumentException: Bad endianness tag (not 0x4949 or
0x4d4d).
at
com.sun.media.jai.codec.TIFFDirectory.getNumDirectories(TIFFDirectory.java:5
88)
at
com.sun.media.jai.codecimpl.TIFFImageDecoder.getNumPages(TIFFImageDecoder.ja
va:105)
at
com.sun.media.jai.codecimpl.TIFFImageDecoder.decodeAsRenderedImage(TIFFImage
Decoder.java:109)
at TestConvert.ConvTifJpg(TestConvert.java:52)
at TestConvert.main(TestConvert.java:32)
Exception in thread "main"
---
[Message sent by forum member 'question13' (hahn)]

http://www.javadesktop.org/forums/thread.jspa?messageID=94450&#94450

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
For additional commands, e-mail: interest-help@jai.dev.java.net