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: PSImageHandlerRawJPEG.java 746664 2009-02-22 12:40:44Z jeremias $ */
19 
20 package org.apache.fop.render.ps;
21 
22 import java.awt.Rectangle;
23 import java.awt.geom.Rectangle2D;
24 import java.io.IOException;
25 
26 import org.apache.xmlgraphics.image.loader.Image;
27 import org.apache.xmlgraphics.image.loader.ImageFlavor;
28 import org.apache.xmlgraphics.image.loader.ImageInfo;
29 import org.apache.xmlgraphics.image.loader.impl.ImageRawJPEG;
30 import org.apache.xmlgraphics.ps.FormGenerator;
31 import org.apache.xmlgraphics.ps.ImageEncoder;
32 import org.apache.xmlgraphics.ps.ImageFormGenerator;
33 import org.apache.xmlgraphics.ps.PSGenerator;
34 import org.apache.xmlgraphics.ps.PSImageUtils;
35 
36 import org.apache.fop.render.RenderingContext;
37 
38 /**
39  * Image handler implementation which handles undecoded JPEG images for PostScript output.
40  */
41 public class PSImageHandlerRawJPEG implements PSImageHandler {
42 
43     private static final ImageFlavor[] FLAVORS = new ImageFlavor[] {
44         ImageFlavor.RAW_JPEG
45     };
46 
47     /** {@inheritDoc} */
handleImage(RenderingContext context, Image image, Rectangle pos)48     public void handleImage(RenderingContext context, Image image, Rectangle pos)
49                 throws IOException {
50         PSRenderingContext psContext = (PSRenderingContext)context;
51         PSGenerator gen = psContext.getGenerator();
52         ImageRawJPEG jpeg = (ImageRawJPEG)image;
53 
54         float x = (float)pos.getX() / 1000f;
55         float y = (float)pos.getY() / 1000f;
56         float w = (float)pos.getWidth() / 1000f;
57         float h = (float)pos.getHeight() / 1000f;
58         Rectangle2D targetRect = new Rectangle2D.Float(
59                 x, y, w, h);
60 
61         ImageInfo info = image.getInfo();
62 
63         ImageEncoder encoder = new ImageEncoderJPEG(jpeg);
64         PSImageUtils.writeImage(encoder, info.getSize().getDimensionPx(),
65                 info.getOriginalURI(), targetRect,
66                 jpeg.getColorSpace(), 8, jpeg.isInverted(), gen);
67     }
68 
69     /** {@inheritDoc} */
generateForm(RenderingContext context, Image image, PSImageFormResource form)70     public void generateForm(RenderingContext context, Image image, PSImageFormResource form)
71             throws IOException {
72         PSRenderingContext psContext = (PSRenderingContext)context;
73         PSGenerator gen = psContext.getGenerator();
74         ImageRawJPEG jpeg = (ImageRawJPEG)image;
75         ImageInfo info = image.getInfo();
76         String imageDescription = info.getMimeType() + " " + info.getOriginalURI();
77 
78         ImageEncoder encoder = new ImageEncoderJPEG(jpeg);
79         FormGenerator formGen = new ImageFormGenerator(
80                 form.getName(), imageDescription,
81                 info.getSize().getDimensionPt(),
82                 info.getSize().getDimensionPx(),
83                 encoder,
84                 jpeg.getColorSpace(), jpeg.isInverted());
85         formGen.generate(gen);
86     }
87 
88     /** {@inheritDoc} */
getPriority()89     public int getPriority() {
90         return 200;
91     }
92 
93     /** {@inheritDoc} */
getSupportedImageClass()94     public Class getSupportedImageClass() {
95         return ImageRawJPEG.class;
96     }
97 
98     /** {@inheritDoc} */
getSupportedImageFlavors()99     public ImageFlavor[] getSupportedImageFlavors() {
100         return FLAVORS;
101     }
102 
103     /** {@inheritDoc} */
isCompatible(RenderingContext targetContext, Image image)104     public boolean isCompatible(RenderingContext targetContext, Image image) {
105         if (targetContext instanceof PSRenderingContext) {
106             PSRenderingContext psContext = (PSRenderingContext)targetContext;
107             //The filters required for this implementation need PS level 2 or higher
108             if (psContext.getGenerator().getPSLevel() >= 2) {
109                 return (image == null || image instanceof ImageRawJPEG);
110             }
111         }
112         return false;
113     }
114 
115 }
116