0

When I try to copy the contents of a “.jpg” file to a new “.tif” file, the IIOMetadata is not preserved. The main metadata tags that I need to remain are: compressionTypeName , horizontalPixelSize and VerticalPixelSize.
This is the simplified code of the copying process:

public void stackOverflowQuestion(File fileToRead) {
    ImageReader reader; 
    ImageWriter writer=null;
    FileImageInputStream fileImageInputStream;
    BufferedImage bufferedImageReadImage;
    IIOMetadata iioMetadataReadImage;
    IIOMetadata iioMetaDataImageToWrite;
    IIOImage iioImage;
    ImageWriteParam imageWriteParams;
    ImageReadParam imageReadParam;
    File newFile;
    ImageTypeSpecifier imageTypeSpecifierDestino;

    reader = this.createImageReaderBySuffix("jpg");//(parameter’s extension)
    try {
        fileImageInputStream=new FileImageInputStream(fileToRead);
        reader.setInput(fileImageInputStream);
        writer = ImageIO.getImageWritersBySuffix("tif").next();
        imageReadParam=reader.getDefaultReadParam();
        bufferedImageReadImage = reader.read(0,imageReadParam);
        iioMetadataReadImage = reader.getImageMetadata(0);
        if (bufferedImageReadImage!=null) {
            newFile =getNewFile();//simply gets a new empty file.
            writer.setOutput(new FileImageOutputStream(newFile));
            writer.prepareWriteSequence(null);
            imageTypeSpecifierDestino = ImageTypeSpecifier.createFromBufferedImageType(bufferedImageReadImage.getType());
            imageWriteParams = (ImageWriteParam) writer.getDefaultWriteParam();
            imageWriteParams.setDestinationType( ImageTypeSpecifier.createFromBufferedImageType( bufferedImageReadImage.getType()) ) ;
            imageWriteParams.setCompressionMode( ImageWriteParam.MODE_COPY_FROM_METADATA );
            iioMetaDataImageToWrite= writer.convertImageMetadata(iioMetadataReadImage, imageTypeSpecifierDestino, imageWriteParams);
            //at this point, the metadata is spoiled
            iioImage=new IIOImage(bufferedImageReadImage, null, iioMetaDataImageToWrite);
            writer.writeToSequence(iioImage, imageWriteParams);
        }
    } catch (Exception e) {
}

This is the metadata included in the format "javax_imageio_1.0" included in the read .jpg file:

<javax_imageio_1.0>
<Chroma>
    <ColorSpaceType name="YCbCr"/>
    <NumChannels value="3"/>    
</Chroma>
<Compression>
    <CompressionTypeName value="JPEG"/>
    <Lossless value="FALSE"/>
    <NumProgressiveScans value="1"/>
</Compression>
<Dimension>
    <PixelAspectRatio value="1.0"/>
    <ImageOrientation value="normal"/>
    <HorizontalPixelSize value="0.16933332"/>
    <VerticalPixelSize value="0.16933332"/>
</Dimension>

This is the metadata included in the same formatname "javax_imageio_1.0" written by my program in the .tif file

<javax_imageio_1.0>
<Chroma>
    <ColorSpaceType name="RGB"/>
    <NumChannels value="1"/>
    <BlackIsZero/>
</Chroma>
<Compression>
    <CompressionTypeName value="None"/>
</Compression>
<Data>
    <PlanarConfiguration value="PixelInterleaved"/>
    <SampleFormat value="UnsignedIntegral"/>
    <BitsPerSample value="1"/>
     <SampleMSB value="0"/>
</Data>
<Dimension>
    <PixelAspectRatio value="1.0"/>
        <HorizontalPixelSize value="5.905523436069756"/>
        <VerticalPixelSize value="5.905523436069756"/>
</Dimension>    
<Document>
    <FormatVersion value="6.0"/>
</Document>

CompressionTypeName, HorizontalPixelSize and VerticalPixelSize values were lost or changed.

radrow
  • 6,419
  • 4
  • 26
  • 53
JNacho
  • 1
  • 1
  • There are currently some open issues about what parts of the TIFF metadata that is preserved and written (see the [TwelveMonkeys issue tacker for details](https://github.com/haraldk/TwelveMonkeys/issues)). Also, the compression `MODE_COPY_FROM_METADATA` does not currently work. File a bug if you like this implemented. For now, you can work around it by specifying JPEG compression from the `ImageWriteParam`. – Harald K May 23 '19 at 09:23
  • Sadly my question is just a part of my problem… I need support for other formats beyond jpg, (gif, png, bmp, etc…) So I need a way to preserve/copy the IIOMetadata of “any” original image format. The real solution could be “something” that could make the translation from the original image metadata to the “tif” final file, reading all the metadata from the origin and trying to transform and remain as much information as possible. Is there any pre-made solution “out there”? – JNacho May 24 '19 at 13:03
  • A less convenient solution which could be enough, could transform and preserve NOT all the metadata information, but the standard metadata, (the metadata included in the standard metadata named “javax_imageio_1.0"”. Is there any pre-made component in twelvemonkeys which could make this transformation? Is there any other implementations of the Interface ImageTranscoder which do such conversion? – JNacho May 24 '19 at 13:03
  • Unfortunately, metadata is often very format-specific. So, converting from one to another, on a general basis, is difficult. For example, while TIFF can contain JPEG-compressed data, PNG, GIF and BMP can not. PNG and GIF is always compressed, BMP can be in some cases. But the BMP RLE compression can't be carried over to TIFF. Some formats have rich support for metadata (TIFF), others do not (GIF, BMP). Etc. – Harald K May 24 '19 at 13:25
  • In theory, you could implement your own `ImageTranscoder`s for each combination of formats, but I have never tried. Falling back to converting *from* "standard" metadata is the default, but even then how this maps to the new format is plugin-specific. – Harald K May 24 '19 at 13:25

0 Answers0