KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > image > PNGImage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: PNGImage.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.image;
21
22 import java.io.IOException JavaDoc;
23
24 import org.apache.xmlgraphics.image.codec.png.PNGRed;
25 import org.apache.xmlgraphics.image.codec.png.PNGDecodeParam;
26 import org.apache.xmlgraphics.image.codec.util.SeekableStream;
27 import org.apache.xmlgraphics.image.rendered.CachableRed;
28 import org.apache.commons.io.IOUtils;
29
30 /**
31  * FopImage object using PNG
32  * @author Eric SCHAEFFER
33  * @see AbstractFopImage
34  * @see FopImage
35  */

36 public class PNGImage extends XmlGraphicsCommonsImage {
37
38     /**
39      * Constructs a new PNGImage instance.
40      * @param imgReader basic metadata for the image
41      */

42     public PNGImage(FopImage.ImageInfo imgReader) {
43         super(imgReader);
44         this.loaded = 0; //TODO The PNGReader cannot read the resolution, yet.
45
}
46
47     /**
48      * @see org.apache.fop.image.XmlGraphicsCommonsImage#decodeImage(
49      * org.apache.xmlgraphics.image.codec.util.SeekableStream)
50      */

51     protected CachableRed decodeImage(SeekableStream stream) throws IOException JavaDoc {
52         PNGDecodeParam param = new PNGDecodeParam();
53         param.setPerformGammaCorrection(true);
54         param.setDisplayExponent(2.2f); // sRGB gamma
55
PNGRed red = new PNGRed(stream, param);
56         String JavaDoc unit = (String JavaDoc)red.getProperty("pixel_units");
57         if ("Meters".equals(unit)) {
58             this.dpiHorizontal = ((Integer JavaDoc)red.getProperty("x_pixels_per_unit")).intValue()
59                 * 25.4f / 1000f;
60             this.dpiVertical = ((Integer JavaDoc)red.getProperty("y_pixels_per_unit")).intValue()
61                 * 25.4f / 1000f;
62         }
63         return red;
64     }
65     
66     /**
67      * Load the original PNG data.
68      * This loads the original PNG data as is into memory.
69      *
70      * @return true if loaded false for any error
71      */

72     protected boolean loadOriginalData() {
73         try {
74             seekableInput.seek(0);
75             this.raw = IOUtils.toByteArray(seekableInput);
76         
77         } catch (java.io.IOException JavaDoc ex) {
78             log.error("Error while loading raw image: " + ex.getMessage(), ex);
79             return false;
80         } finally {
81             IOUtils.closeQuietly(inputStream);
82             inputStream = null;
83         }
84
85         return true;
86     }
87     
88 }
89
Popular Tags