KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > ps > PSImageUtils


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: PSImageUtils.java 426576 2006-07-28 15:44:37Z jeremias $ */
19  
20 package org.apache.fop.render.ps;
21
22 import java.awt.Dimension JavaDoc;
23 import java.awt.geom.Rectangle2D JavaDoc;
24 import java.io.IOException JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.fop.image.EPSImage;
29 import org.apache.fop.image.FopImage;
30 import org.apache.fop.image.JpegImage;
31 import org.apache.xmlgraphics.ps.PSGenerator;
32
33 /**
34  * Utility code for rendering images in PostScript.
35  */

36 public class PSImageUtils extends org.apache.xmlgraphics.ps.PSImageUtils {
37
38     /** logging instance */
39     protected static Log log = LogFactory.getLog(PSImageUtils.class);
40
41     /**
42      * Renders a bitmap image to PostScript.
43      * @param img image to render
44      * @param x x position
45      * @param y y position
46      * @param w width
47      * @param h height
48      * @param gen PS generator
49      * @throws IOException In case of an I/O problem while rendering the image
50      */

51     public static void renderBitmapImage(FopImage img,
52                 float x, float y, float w, float h, PSGenerator gen)
53                     throws IOException JavaDoc {
54         if (img instanceof JpegImage) {
55             if (!img.load(FopImage.ORIGINAL_DATA)) {
56                 gen.commentln("%JPEG image could not be processed: " + img);
57                 return;
58             }
59         } else {
60             if (!img.load(FopImage.BITMAP)) {
61                 gen.commentln("%Bitmap image could not be processed: " + img);
62                 return;
63             }
64         }
65         byte[] imgmap;
66         if (img.getBitmapsSize() > 0) {
67             imgmap = img.getBitmaps();
68         } else {
69             imgmap = img.getRessourceBytes();
70         }
71         
72         String JavaDoc imgName = img.getMimeType() + " " + img.getOriginalURI();
73         Dimension JavaDoc imgDim = new Dimension JavaDoc(img.getWidth(), img.getHeight());
74         Rectangle2D JavaDoc targetRect = new Rectangle2D.Double JavaDoc(x, y, w, h);
75         boolean isJPEG = (img instanceof JpegImage);
76         writeImage(imgmap, imgDim, imgName, targetRect, isJPEG,
77                 img.getColorSpace(), gen);
78     }
79
80     public static void renderEPS(EPSImage img,
81             float x, float y, float w, float h,
82             PSGenerator gen) {
83         try {
84             if (!img.load(FopImage.ORIGINAL_DATA)) {
85                 gen.commentln("%EPS image could not be processed: " + img);
86                 return;
87             }
88             int[] bbox = img.getBBox();
89             int bboxw = bbox[2] - bbox[0];
90             int bboxh = bbox[3] - bbox[1];
91             String JavaDoc name = img.getDocName();
92             if (name == null || name.length() == 0) {
93                 name = img.getOriginalURI();
94             }
95             renderEPS(img.getEPSImage(), name,
96                 x, y, w, h,
97                 bbox[0], bbox[1], bboxw, bboxh, gen);
98
99         } catch (Exception JavaDoc e) {
100             log.error("PSRenderer.renderImageArea(): Error rendering bitmap ("
101                                    + e.getMessage() + ")", e);
102         }
103     }
104
105 }
106
Popular Tags