You are viewing a plain text version of this content. The canonical link for it is here.
Posted to general@xmlgraphics.apache.org by Jean-François El Fouly <je...@elfouly.fr> on 2008/09/17 06:39:12 UTC

Using PNGEncoder and PNGDecoder

Since we use and embed xmlgraphics-commons for the sake of FOP anyway, I 
tried to use the PNG encoding / decoding classes in another context of 
our application (thus, not at all related to FOP or Batik).
In two different places at least we need to write or process (read - 
process - write) PNG images with their PHYS metadata.
Since the documentation on using xmlgraphics-commons in general is 
rather scarce, I tried to figure out myself how to use these classes by 
reading the code and tried this.

            PNGDecodeParam decodeParam = new PNGDecodeParam();
            decodeParam.setGenerateEncodeParam(true);

            PNGImageDecoder decoder = new PNGImageDecoder(new 
FileInputStream(sourceFile), decodeParam);
            RenderedImage srcImage = decoder.decodeAsRenderedImage();
           
            // Some irrelevant code removed here

            BufferedImage dstImage = new 
BufferedImage(srcImage.getWidth(), srcImage.getHeight(), 
BufferedImage.TYPE_3BYTE_BGR);

            // Some processing here...


            PNGEncodeParam encodeParam = decodeParam.getEncodeParam();
            System.out.println("Physical dimensions set ?" + 
encodeParam.isPhysicalDimensionSet());
            System.out.println("Physical dimensions:" + 
encodeParam.getPhysicalDimension()[0]);
            PNGImageEncoder encoder = new PNGImageEncoder(new 
FileOutputStream(destinationFile), encodeParam);
            encoder.encode(dstImage);

Hoping this would propagate the PNG metadata (among which ICCProfile and 
physical dimensions) from source to destination.
Well the image processing code works well and the output file is 
properly encoded, but without any metadata.
What did I miss in the process ?

Thanks for helping !

Jean-Francois






---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org


Re: Using PNGEncoder and PNGDecoder

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
On 19.09.2008 15:38:21 Jean-François El Fouly wrote:
> Jeremias Maerki a écrit :
> > Right, that's the downside of Image I/O. But then, the XML-based approach
> > to metadata is also very powerful.
> >
> >   
> I'm not sure I understand. Am I missing something ?
> The images I (pre) process will be used by FOP somewhere downstream. 
> And, as far as I understand, FOP uses the PHYS metadata for its layout. 

Indirectly, yes.

> That's why I have been through considerable effort to give these 
> metadata sensible values.

Ok, makes sense.

Here's an example that uses Image I/O to produce a PNG including
resolution information:
https://svn.eu.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/writer/imageio/ImageIOImageWriter.java?view=markup
Maybe that helps to understand how Image I/O does this.

Another alternative: If you only need the resolution right, you can just
as well use Common's ImageWriter which is great for just quickly save an
image with control over the most important features:
https://svn.eu.apache.org/viewvc/xmlgraphics/commons/trunk/examples/java/image/writer/ImageWriterExample1.java?view=markup


> > Another option that would give you a lot of control but possibly with a
> > simpler API is Apache Sanselan (incubating).
> > http://incubator.apache.org/sanselan
> >
> >   
> Interesting, did'nt know of this project.
> But the project I work on uses and embeds FOP and its dependencies, so I 
> guess it makes sense to use xmlgraphics-commons whenever possible.

Sure, whatever you think works best for you. Just listing alternatives.


Jeremias Maerki


---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org


Re: Using PNGEncoder and PNGDecoder

Posted by Jean-François El Fouly <je...@elfouly.fr>.
Jeremias Maerki a écrit :
> Right, that's the downside of Image I/O. But then, the XML-based approach
> to metadata is also very powerful.
>
>   
I'm not sure I understand. Am I missing something ?
The images I (pre) process will be used by FOP somewhere downstream. 
And, as far as I understand, FOP uses the PHYS metadata for its layout. 
That's why I have been through considerable effort to give these 
metadata sensible values.
> Another option that would give you a lot of control but possibly with a
> simpler API is Apache Sanselan (incubating).
> http://incubator.apache.org/sanselan
>
>   
Interesting, did'nt know of this project.
But the project I work on uses and embeds FOP and its dependencies, so I 
guess it makes sense to use xmlgraphics-commons whenever possible.

---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org


Re: Using PNGEncoder and PNGDecoder

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
On 19.09.2008 15:10:05 Jean-François El Fouly wrote:
> Jeremias Maerki a écrit :
> > I'd recommend you use Image I/O instead. The codecs here are nice for
> > certain use cases but there are too few people around here to help
> > maintain and support the code. With Image I/O you have a much bigger
> > pool of people that can help you if you run into problems. And Image I/O
> > is also already available to your application if you're using Java 1.4
> > or later.
> >
> >   
> Thanks !
> The previous version used ImageIO indeed. But I never figured how to 
> handle PNG metadata with ImageIO -- though I spent a few days at it 
> earlier this year, and got out with the feeling that ImageIO and JAI are 
> among the most cryptic and ill-documented parts of the Java Platform.
> And at this point I need precise control, what I need is really 
> PNG-specific and rather tricky BTW.

Right, that's the downside of Image I/O. But then, the XML-based approach
to metadata is also very powerful.

Another option that would give you a lot of control but possibly with a
simpler API is Apache Sanselan (incubating).
http://incubator.apache.org/sanselan

> Never mind, it works well now; though I'm not 100% sure of what I 
> changed to make it work.
>
> I'll use it in another place where I have shortly used another open 
> source PNG encoder that is probably getting old by now.
> But let me say thank you once more.
> 
> Jean-Francois El Fouly



Jeremias Maerki


---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org


Re: Using PNGEncoder and PNGDecoder

Posted by Jean-François El Fouly <je...@elfouly.fr>.
Jeremias Maerki a écrit :
> I'd recommend you use Image I/O instead. The codecs here are nice for
> certain use cases but there are too few people around here to help
> maintain and support the code. With Image I/O you have a much bigger
> pool of people that can help you if you run into problems. And Image I/O
> is also already available to your application if you're using Java 1.4
> or later.
>
>   
Thanks !
The previous version used ImageIO indeed. But I never figured how to 
handle PNG metadata with ImageIO -- though I spent a few days at it 
earlier this year, and got out with the feeling that ImageIO and JAI are 
among the most cryptic and ill-documented parts of the Java Platform.
And at this point I need precise control, what I need is really 
PNG-specific and rather tricky BTW.

Never mind, it works well now; though I'm not 100% sure of what I 
changed to make it work.
I'll use it in another place where I have shortly used another open 
source PNG encoder that is probably getting old by now.
But let me say thank you once more.

Jean-Francois El Fouly


---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org


Re: Using PNGEncoder and PNGDecoder

Posted by Jeremias Maerki <de...@jeremias-maerki.ch>.
I'd recommend you use Image I/O instead. The codecs here are nice for
certain use cases but there are too few people around here to help
maintain and support the code. With Image I/O you have a much bigger
pool of people that can help you if you run into problems. And Image I/O
is also already available to your application if you're using Java 1.4
or later.

On 17.09.2008 06:39:12 Jean-François El Fouly wrote:
> Since we use and embed xmlgraphics-commons for the sake of FOP anyway, I 
> tried to use the PNG encoding / decoding classes in another context of 
> our application (thus, not at all related to FOP or Batik).
> In two different places at least we need to write or process (read - 
> process - write) PNG images with their PHYS metadata.
> Since the documentation on using xmlgraphics-commons in general is 
> rather scarce, I tried to figure out myself how to use these classes by 
> reading the code and tried this.
> 
>             PNGDecodeParam decodeParam = new PNGDecodeParam();
>             decodeParam.setGenerateEncodeParam(true);
> 
>             PNGImageDecoder decoder = new PNGImageDecoder(new 
> FileInputStream(sourceFile), decodeParam);
>             RenderedImage srcImage = decoder.decodeAsRenderedImage();
>            
>             // Some irrelevant code removed here
> 
>             BufferedImage dstImage = new 
> BufferedImage(srcImage.getWidth(), srcImage.getHeight(), 
> BufferedImage.TYPE_3BYTE_BGR);
> 
>             // Some processing here...
> 
> 
>             PNGEncodeParam encodeParam = decodeParam.getEncodeParam();
>             System.out.println("Physical dimensions set ?" + 
> encodeParam.isPhysicalDimensionSet());
>             System.out.println("Physical dimensions:" + 
> encodeParam.getPhysicalDimension()[0]);
>             PNGImageEncoder encoder = new PNGImageEncoder(new 
> FileOutputStream(destinationFile), encodeParam);
>             encoder.encode(dstImage);
> 
> Hoping this would propagate the PNG metadata (among which ICCProfile and 
> physical dimensions) from source to destination.
> Well the image processing code works well and the output file is 
> properly encoded, but without any metadata.
> What did I miss in the process ?
> 
> Thanks for helping !
> 
> Jean-Francois
> 
> 
> 



Jeremias Maerki


---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: general-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: general-help@xmlgraphics.apache.org