<commit_before>/*
 * Copyright 1999-2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */
 
package org.apache.fop.svg;

import org.apache.batik.bridge.SVGImageElementBridge;

import java.awt.Shape;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedInputStream;
import java.io.InputStream;

import org.w3c.dom.Element;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.AbstractGraphicsNode;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.ParsedURL;

import org.apache.fop.image.JpegImage;
import org.apache.fop.image.FopImage;
import org.apache.fop.image.analyser.ImageReaderFactory;

/**
 * Bridge class for the &lt;image> element when jpeg images.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class PDFImageElementBridge extends SVGImageElementBridge {

    /**
     * Constructs a new bridge for the &lt;image> element.
     */
    public PDFImageElementBridge() { }

    /**
     * Create the raster image node.
     * THis checks if it is a jpeg file and creates a jpeg node
     * so the jpeg can be inserted directly into the pdf document.
     * @param ctx the bridge context
     * @param imageElement the svg element for the image
     * @param purl the parsed url for the image resource
     * @return a new graphics node
     */
    protected GraphicsNode createImageGraphicsNode
        (BridgeContext ctx, Element imageElement, ParsedURL purl) {
        try {
            InputStream is = purl.openStream();
            if (!is.markSupported()) {
                is = new BufferedInputStream(is, 1024);
            }
            
            is.mark(3);
            byte [] data = new byte[3];
            is.read(data);
            is.reset();
            if ((data[0] == (byte)0xFF) 
                    && (data[1] == (byte)0xD8) 
                    && (data[2] == (byte)0xFF)) {
                FopImage.ImageInfo ii = ImageReaderFactory.make
                        (purl.toString(), is, null);
                JpegImage jpeg = new JpegImage(ii);
                jpeg.load(FopImage.ORIGINAL_DATA);
                PDFJpegNode node = new PDFJpegNode(jpeg, ctx, e, purl);
    
                Rectangle2D imgBounds = getImageBounds(ctx, e);
                Rectangle2D bounds = node.getPrimitiveBounds();
                float [] vb = new float[4];
                vb[0] = 0; // x
                vb[1] = 0; // y
                vb[2] = (float) bounds.getWidth(); // width
                vb[3] = (float) bounds.getHeight(); // height
    
                // handles the 'preserveAspectRatio', 'overflow' and 'clip' 
                // and sets the appropriate AffineTransform to the image node
                initializeViewport(ctx, e, node, vb, imgBounds);
                return node;
            }
        } catch (Exception ex) {
            //TODO Handle this exception
        }

        return superCreateGraphicsNode(ctx, e, purl);
    }

    /**
     * @see org.apache.batik.bridge.SVGImageElementBridge
     */
    protected GraphicsNode superCreateGraphicsNode
        (BridgeContext ctx, Element imageElement, ParsedURL purl) {
        return super.createImageGraphicsNode(ctx, imageElement, purl);
    }


    /**
     * A PDF jpeg node.
     * This holds a jpeg image so that it can be drawn into
     * the PDFGraphics2D.
     */
    public class PDFJpegNode extends AbstractGraphicsNode {
        
        private JpegImage jpeg;
        private BridgeContext ctx;
        private Element imageElement;
        private ParsedURL purl;<commit_msg>Automated parameter rename failed because there was an instance variable called "e" which caused an NPE in examples/fo/build.xml on target "newPDF".<commit_after>/*
 * Copyright 1999-2005 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/* $Id$ */
 
package org.apache.fop.svg;

import org.apache.batik.bridge.SVGImageElementBridge;

import java.awt.Shape;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.io.BufferedInputStream;
import java.io.InputStream;

import org.w3c.dom.Element;

import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.gvt.AbstractGraphicsNode;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.ParsedURL;

import org.apache.fop.image.JpegImage;
import org.apache.fop.image.FopImage;
import org.apache.fop.image.analyser.ImageReaderFactory;

/**
 * Bridge class for the &lt;image> element when jpeg images.
 *
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class PDFImageElementBridge extends SVGImageElementBridge {

    /**
     * Constructs a new bridge for the &lt;image> element.
     */
    public PDFImageElementBridge() { }

    /**
     * Create the raster image node.
     * THis checks if it is a jpeg file and creates a jpeg node
     * so the jpeg can be inserted directly into the pdf document.
     * @param ctx the bridge context
     * @param imageElement the svg element for the image
     * @param purl the parsed url for the image resource
     * @return a new graphics node
     */
    protected GraphicsNode createImageGraphicsNode
        (BridgeContext ctx, Element imageElement, ParsedURL purl) {
        try {
            InputStream is = purl.openStream();
            if (!is.markSupported()) {
                is = new BufferedInputStream(is, 1024);
            }
            
            is.mark(3);
            byte [] data = new byte[3];
            is.read(data);
            is.reset();
            if ((data[0] == (byte)0xFF) 
                    && (data[1] == (byte)0xD8) 
                    && (data[2] == (byte)0xFF)) {
                FopImage.ImageInfo ii = ImageReaderFactory.make
                        (purl.toString(), is, null);
                JpegImage jpeg = new JpegImage(ii);
                jpeg.load(FopImage.ORIGINAL_DATA);
                PDFJpegNode node = new PDFJpegNode(jpeg, ctx, imageElement, purl);
    
                Rectangle2D imgBounds = getImageBounds(ctx, imageElement);
                Rectangle2D bounds = node.getPrimitiveBounds();
                float [] vb = new float[4];
                vb[0] = 0; // x
                vb[1] = 0; // y
                vb[2] = (float) bounds.getWidth(); // width
                vb[3] = (float) bounds.getHeight(); // height
    
                // handles the 'preserveAspectRatio', 'overflow' and 'clip' 
                // and sets the appropriate AffineTransform to the image node
                initializeViewport(ctx, imageElement, node, vb, imgBounds);
                return node;
            }
        } catch (Exception ex) {
            //TODO Handle this exception
        }

        return superCreateGraphicsNode(ctx, imageElement, purl);
    }

    /**
     * @see org.apache.batik.bridge.SVGImageElementBridge
     */
    protected GraphicsNode superCreateGraphicsNode
        (BridgeContext ctx, Element imageElement, ParsedURL purl) {
        return super.createImageGraphicsNode(ctx, imageElement, purl);
    }


    /**
     * A PDF jpeg node.
     * This holds a jpeg image so that it can be drawn into
     * the PDFGraphics2D.
     */
    public class PDFJpegNode extends AbstractGraphicsNode {
        
        private JpegImage jpeg;
        private BridgeContext ctx;
        private Element imageElement;
        private ParsedURL purl;
{'fraction_non_alphanumeric': 0.09474305965741288, 'fraction_numerical': 0.00897814530419374, 'mean_word_length': 2.2725164282953227, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1213162', 'n_tokens_mistral': 2453, 'n_tokens_neox': 2326, 'n_words': 735}