aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/afp/ioca/ImageContent.java
blob: 9c06589e0e69a42d5a500907d5198b91098f72f1 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.afp.ioca;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.fop.afp.modca.AbstractStructuredObject;

/**
 * An IOCA Image Content
 */
public class ImageContent extends AbstractStructuredObject {

    /**
     * The CCITT T.4 Group 3 Coding Standard (G3 MH-Modified Huffman) is a
     * compression method standardized by the International Telegraph and
     * Telephone Consultative Committee (CCITT) for facsimile.  It enables
     * one-dimensional compression.
     */
    public static final byte COMPID_G3_MH = (byte)0x80;

    /**
     * The CCITT T.4 Group 3 Coding Option (G3 MR-Modified READ) is a
     * compression method standardized by the International Telegraph and
     * Telephone Consultative Committee (CCITT) for facsimile. It enables
     * two-dimensional compression.
     */
    public static final byte COMPID_G3_MR = (byte)0x81;

    /**
     * The CCITT T.6 Group 4 Coding Standard (G4 MMR-Modified Modified READ) is a
     * compression method standardized by the International Telegraph and
     * Telephone Consultative Committee (CCITT) for facsimile.  It enables
     * two-dimensional compression.
     */
    public static final byte COMPID_G3_MMR = (byte)0x82;

    /** the image size parameter */
    private ImageSizeParameter imageSizeParameter = null;

    /** the IDE Structure parameter */
    private IDEStructureParameter ideStructureParameter = null;

    /** the image encoding */
    private byte encoding = (byte)0x03;

    /** the image IDE (Image Data Element, Sample) size */
    private byte ideSize = 1;

    /** the image compression */
    private byte compression = (byte)0xC0;

    /** the image data */
    private byte[] data;

    /**
     * Main Constructor
     */
    public ImageContent() {
    }

    /**
     * Sets the image size parameter
     *
     * @param imageSizeParameter the image size parameter.
     */
    public void setImageSizeParameter(ImageSizeParameter imageSizeParameter) {
        this.imageSizeParameter = imageSizeParameter;
    }

    /**
     * Sets the IDE Structure parameter.
     * @param parameter the IDE Structure parameter
     */
    public void setIDEStructureParameter(IDEStructureParameter parameter) {
        this.ideStructureParameter = parameter;
    }

    /**
     * Returns the (optional) IDE Structure parameter
     * @return the IDE Structure parameter or null if none is set
     */
    public IDEStructureParameter getIDEStructureParameter() {
        return this.ideStructureParameter;
    }

    /**
     * Returns the (optional) IDE Structure parameter. If none is set an instance is prepared
     * with defaults for a bi-level image.
     * @return the IDE Structure parameter
     */
    public IDEStructureParameter needIDEStructureParameter() {
        if (this.ideStructureParameter == null) {
            setIDEStructureParameter(new IDEStructureParameter());
        }
        return getIDEStructureParameter();
    }

    /**
     * Sets the image encoding.
     *
     * @param enc The image encoding.
     */
    public void setImageEncoding(byte enc) {
        this.encoding = enc;
    }

    /**
     * Sets the image compression.
     *
     * @param comp The image compression.
     */
    public void setImageCompression(byte comp) {
        this.compression = comp;
    }

    /**
     * Sets the image IDE size.
     *
     * @param s The IDE size.
     */
    public void setImageIDESize(byte s) {
        this.ideSize = s;
    }

    /**
     * Sets the image IDE color model.
     *
     * @param color    the IDE color model.
     * @deprecated use {@link #setIDEStructureParameter(IDEStructureParameter)} instead
     */
    public void setImageIDEColorModel(byte color) {
        needIDEStructureParameter().setColorModel(color);
    }

    /**
     * Set either additive or subtractive mode (used for ASFLAG).
     * @param subtractive true for subtractive mode, false for additive mode
     * @deprecated use {@link #setIDEStructureParameter(IDEStructureParameter)} instead
     */
    public void setSubtractive(boolean subtractive) {
        needIDEStructureParameter().setSubtractive(subtractive);
    }

    /**
     * Set the image data (can be byte array or inputstream)
     *
     * @param imageData the image data
     */
    public void setImageData(byte[] imageData) {
        this.data = imageData;
    }

    private static final int MAX_DATA_LEN = 65535;

    /** {@inheritDoc} */
    protected void writeContent(OutputStream os) throws IOException {
        if (imageSizeParameter != null) {
            imageSizeParameter.writeToStream(os);
        }

        // TODO convert to triplet/parameter class
        os.write(getImageEncodingParameter());

        os.write(getImageIDESizeParameter());

        if (getIDEStructureParameter() != null) {
            getIDEStructureParameter().writeToStream(os);
        }

        boolean useFS10 = (this.ideSize == 1);
        if (!useFS10) {
            os.write(getExternalAlgorithmParameter());
        }

        final byte[] dataHeader = new byte[] {
                (byte)0xFE, // ID
                (byte)0x92, // ID
                0x00, // length
                0x00  // length
            };
        final int lengthOffset = 2;

        // Image Data
        if (data != null) {
            writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
        }
    }

    /** {@inheritDoc} */
    protected void writeStart(OutputStream os) throws IOException {
        final byte[] startData = new byte[] {
            (byte)0x91, // ID
            0x01, // Length
            (byte)0xff, // Object Type = IOCA Image Object
        };
        os.write(startData);
    }

    /** {@inheritDoc} */
    protected void writeEnd(OutputStream os) throws IOException {
        final byte[] endData = new byte[] {
            (byte)0x93, // ID
            0x00, // Length
        };
        os.write(endData);
    }

    /**
     * Helper method to return the image encoding parameter.
     *
     * @return byte[] The data stream.
     */
    private byte[] getImageEncodingParameter() {
        final byte[] encodingData = new byte[] {
            (byte)0x95, // ID
            0x02, // Length
            encoding,
            0x01, // RECID
        };
        return encodingData;
    }

    /**
     * Helper method to return the external algorithm parameter.
     *
     * @return byte[] The data stream.
     */
    private byte[] getExternalAlgorithmParameter() {
        if (encoding == (byte)0x83 && compression != 0) {
            final byte[] extAlgData = new byte[] {
                (byte)0x95, // ID
                      0x00, // Length
                      0x10, // ALGTYPE = Compression Algorithm
                      0x00, // Reserved
                (byte)0x83, // COMPRID = JPEG
                      0x00, // Reserved
                      0x00, // Reserved
                      0x00, // Reserved
              compression, // MARKER
                      0x00, // Reserved
                      0x00, // Reserved
                      0x00, // Reserved
            };
            extAlgData[1] = (byte)(extAlgData.length - 2);
            return extAlgData;
        }
        return new byte[0];
    }

    /**
     * Helper method to return the image encoding parameter.
     *
     * @return byte[] The data stream.
     */
    private byte[] getImageIDESizeParameter() {
        if (ideSize != 1) {
            final byte[] ideSizeData = new byte[] {
                    (byte)0x96, // ID
                    0x01, // Length
                    ideSize};
            return ideSizeData;
        } else {
            return new byte[0];
        }
    }

}