Skip to main content

Missing Tag BitsPerSample when writing TIFF-Image

1 reply [Last post]
hveit
Offline
Joined: 2006-02-17
Points: 0

Hello,

when using ImageIO (JDK 1.4.2) and JAI ImageIO Tools 1.0 on Win32 for scaling TIFF images, all output TIFF images lack the BitsPerSample tag although the source image had this tag. Trying to adjust the IIOMetadata object via IIOMetadata.setFromTree() causes my application to hang (function does not return at all, 100% CPU consumption).
What might be the problem? How can the BitsPerSample tag be set?

My code:

static void testDimension(String srcPath, String dstPath)
throws IOException
{
// load the image
ImageInputStream imageInput =
ImageIO.createImageInputStream(new FileInputStream(srcPath));
Iterator it = ImageIO.getImageReaders(imageInput);
ImageReader reader = (ImageReader) it.next();
reader.setInput(imageInput);

// get the DPI of the image (if it has it - may not)
int xDPI = -1;
int yDPI = -1;
IIOMetadata srcMeta = reader.getImageMetadata(0);
IIOMetadataNode srcNodes = (IIOMetadataNode)
srcMeta.getAsTree("javax_imageio_1.0");
NodeList nl =
srcNodes.getElementsByTagName("HorizontalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
xDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
nl = srcNodes.getElementsByTagName("VerticalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
yDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
System.out.println("input resolution = " + xDPI + "x" +
yDPI);

// save it as a tiff
it = ImageIO.getImageWritersByMIMEType("image/tiff");
ImageWriter writer = (ImageWriter)it.next();
ImageWriteParam writeParam = writer.getDefaultWriteParam();
RenderedImage ri = reader.read(0);
File dstFile = new File(dstPath);
dstFile.delete();
FileImageOutputStream fios = new FileImageOutputStream(dstFile);
writer.setOutput(fios);

// set the DPI
IIOMetadata destMeta = writer.convertImageMetadata(srcMeta,
new ImageTypeSpecifier(ri), writeParam);
IIOMetadataNode destNodes = (IIOMetadataNode)
destMeta.getAsTree("javax_imageio_1.0");
nl = destNodes.getElementsByTagName("Dimension");
IIOMetadataNode dim;
if ((nl != null) && (nl.getLength() > 0))
dim = (IIOMetadataNode) nl.item(0);
else {
dim = new IIOMetadataNode("Dimension");
destNodes.appendChild(dim);
}
nl = destNodes.getElementsByTagName("HorizontalPixelSize");
if ((nl == null) || (nl.getLength() == 0)) {
IIOMetadataNode horz = new
IIOMetadataNode("HorizontalPixelSize");
dim.appendChild(horz);
horz.setAttribute("value", Float.toString(xDPI/25.4f));//XXX 25.4f / (float)xDPI));
}
nl = destNodes.getElementsByTagName("VerticalPixelSize");
if ((nl == null) || (nl.getLength() == 0)) {
IIOMetadataNode horz = new
IIOMetadataNode("VerticalPixelSize");
dim.appendChild(horz);
horz.setAttribute("value", Float.toString(yDPI/25.4f));//XXX 25.4f / (float)yDPI));
}

// display from nodes built
xDPI = -1;
yDPI = -1;
nl = destNodes.getElementsByTagName("HorizontalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
xDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
nl = destNodes.getElementsByTagName("VerticalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
yDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
System.out.println("set resolution = " + xDPI + "x" + yDPI);

//method hangs...
destMeta.setFromTree("javax_imageio_1.0", destNodes);

// display from new meta
xDPI = -1;
yDPI = -1;
destNodes = (IIOMetadataNode)
destMeta.getAsTree("javax_imageio_1.0");
nl = destNodes.getElementsByTagName("HorizontalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
xDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
nl = destNodes.getElementsByTagName("VerticalPixelSize");
if ((nl != null) && (nl.getLength() > 0))
yDPI = Math.round(25.4f /
Float.parseFloat(nl.item(0).getAttributes().item(0).getNodeValue()));
System.out.println("output resolution = " + xDPI + "x" +
yDPI);

writer.write(null, new IIOImage(ri, null, destMeta),
writeParam);

// close everything
reader.dispose();
imageInput.close();
writer.dispose();
fios.close();
}

Thanks in advance

Holger

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bpb
Offline
Joined: 2004-06-23
Points: 0

Holger,

Are your images perchange bilevel images, i.e., 1-bit per sample? Note that as the default value of the BitsPerSample field is 1 the presence of the field is not required in this case.

Nevertheless that would not explain any hanging of the application. Is the code you posted complete? In any case it would be more convenient if no editing were required to run it.

Regards,

Brian