aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/apache/fop/pdf/PDFXObject.java
blob: 8a3b9ad46dd71648e75180a756b70c7cb77b4026 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

/* modified by JKT to integrate with 0.12.0 */
/* modified by Eric SCHAEFFER to integrate with 0.13.0 */

package org.apache.fop.pdf;

// Java
import java.io.IOException;
import java.io.OutputStream;
import java.io.ByteArrayOutputStream;

/**
 * PDF XObject
 *
 * A derivative of the PDF Object, is a PDF Stream that has not only a
 * dictionary but a stream of image data.
 * the dictionary just provides information like the stream length
 */
public class PDFXObject extends PDFObject {
    PDFImage pdfimage;
    int Xnum;

    /**
     * create an XObject with the given number and name and load the
     * image in the object
     */
    public PDFXObject(int number, int Xnumber, PDFImage img) {
        super(number);
        this.Xnum = Xnumber;
        pdfimage = img;
    }

    /**
     * @return the PDF XObject number
     */
    public int getXNumber() {
        return this.Xnum;
    }

    /**
     * represent as PDF
     */
    protected int output(OutputStream stream) throws IOException {
        int length = 0;
        int i = 0;

        if (pdfimage.isPS()) {
            length = outputEPSImage(stream);
        } else {

            PDFStream imgStream = pdfimage.getDataStream();

            String dictEntries = imgStream.applyFilters();

            String p = this.number + " " + this.generation + " obj\n";
            p = p + "<</Type /XObject\n";
            p = p + "/Subtype /Image\n";
            p = p + "/Name /Im" + Xnum + "\n";
            p = p + "/Length " + imgStream.getDataLength() + "\n";
            p = p + "/Width " + pdfimage.getWidth() + "\n";
            p = p + "/Height " + pdfimage.getHeight() + "\n";
            p = p + "/BitsPerComponent " + pdfimage.getBitsPerPixel() +
                "\n";

            PDFICCStream pdfICCStream = pdfimage.getICCStream();
            if (pdfICCStream != null) {
                p = p + "/ColorSpace [/ICCBased " +
                    pdfICCStream.referencePDF() + "]\n";
            } else {
                PDFColorSpace cs = pdfimage.getColorSpace();
                p = p + "/ColorSpace /" + cs.getColorSpacePDFString() +
                    "\n";
            }

            /* PhotoShop generates CMYK values that's inverse,
               this will invert the values - too bad if it's not
               a PhotoShop image...
             */
            if (pdfimage.getColorSpace().getColorSpace() ==
                    PDFColorSpace.DEVICE_CMYK) {
                p = p + "/Decode [ 1.0 0.0 1.0 0.0 1.0 0.0 1.1 0.0 ]\n";
            }

            if (pdfimage.isTransparent()) {
                PDFColor transp = pdfimage.getTransparentColor();
                p = p + "/Mask [" + transp.red255() + " " +
                    transp.red255() + " " + transp.green255() +
                    " " + transp.green255() + " " +
                    transp.blue255() + " " + transp.blue255() + "]\n";
            }
            String ref = pdfimage.getSoftMask();
            if (ref != null) {
                p = p + "/SMask " + ref + "\n";
            }

            p = p + dictEntries;
            p = p + ">>\n";

            // push the pdf dictionary on the writer
            byte[] pdfBytes = p.getBytes();
            stream.write(pdfBytes);
            length += pdfBytes.length;
            // push all the image data on the writer
            // and takes care of length for trailer
            length += imgStream.outputStreamData(stream);

            pdfBytes = ("endobj\n").getBytes();
            stream.write(pdfBytes);
            length += pdfBytes.length;
        }
        // let it gc
        pdfimage = null;
        return length;
    }

    byte[] toPDF() {
        return null;
    }

    private int outputEPSImage(OutputStream stream) throws IOException {
        int length = 0;
        int i = 0;

        PDFStream imgStream = pdfimage.getDataStream();
        String dictEntries = imgStream.applyFilters();

        String p = this.number + " " + this.generation + " obj\n";
        p = p + "<</Type /XObject\n";
        p = p + "/Subtype /PS\n";
        p = p + "/Length " + imgStream.getDataLength();

        p = p + dictEntries;
        p = p + ">>\n";

        // push the pdf dictionary on the writer
        byte[] pdfBytes = p.getBytes();
        stream.write(pdfBytes);
        length += pdfBytes.length;
        // push all the image data on  the writer and takes care of length for trailer
        length += imgStream.outputStreamData(stream);

        pdfBytes = ("endobj\n").getBytes();
        stream.write(pdfBytes);
        length += pdfBytes.length;

        return length;
    }

}