KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > afp > modca > ImageContent


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: ImageContent.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23 import org.apache.fop.render.afp.tools.BinaryUtils;
24
25 /**
26  */

27 public class ImageContent extends AbstractAFPObject {
28
29     /**
30      * The image size parameter
31      */

32     private ImageSizeParameter _imageSizeParameter = null;
33
34     /**
35      * The image encoding
36      */

37     private byte _encoding = 0x03;
38
39     /**
40      * The image ide size
41      */

42     private byte _size = 1;
43
44     /**
45      * The image compression
46      */

47     private byte _compression = (byte)0xC0;
48
49     /**
50      * The image color model
51      */

52     private byte _colorModel = 0x01;
53
54     /**
55      * The image data
56      */

57     private byte _data[] = null;
58
59     /**
60      * Constructor for the image content
61      */

62     public ImageContent() {
63
64     }
65
66     /**
67      * Sets the image size parameters
68      * resolution, hsize and vsize.
69      * @param hresol The horizontal resolution of the image.
70      * @param vresol The vertical resolution of the image.
71      * @param hsize The horizontal size of the image.
72      * @param vsize The vertival size of the image.
73      */

74     public void setImageSize(int hresol, int vresol, int hsize, int vsize) {
75         _imageSizeParameter = new ImageSizeParameter(hresol, vresol, hsize, vsize);
76     }
77
78     /**
79      * Sets the image encoding.
80      * @param encoding The image encoding.
81      */

82     public void setImageEncoding(byte encoding) {
83         _encoding = encoding;
84     }
85
86     /**
87      * Sets the image compression.
88      * @param compression The image compression.
89      */

90     public void setImageCompression(byte compression) {
91         _compression = compression;
92     }
93
94     /**
95      * Sets the image IDE size.
96      * @param size The IDE size.
97      */

98     public void setImageIDESize(byte size) {
99         _size = size;
100     }
101
102     /**
103      * Sets the image IDE color model.
104      * @param size The IDE color model.
105      */

106     public void setImageIDEColorModel(byte colorModel) {
107         _colorModel = colorModel;
108     }
109
110     /**
111      * Set the data of the image.
112      */

113     public void setImageData(byte data[]) {
114         _data = data;
115     }
116
117     /**
118      * Accessor method to write the AFP datastream for the Image Content
119      * @param os The stream to write to
120      * @throws java.io.IOException
121      */

122     public void writeDataStream(OutputStream JavaDoc os)
123         throws IOException JavaDoc {
124
125         writeStart(os);
126
127         if (_imageSizeParameter != null) {
128             _imageSizeParameter.writeDataStream(os);
129         }
130
131         os.write(getImageEncodingParameter());
132
133         os.write(getImageIDESizeParameter());
134
135         os.write(getIDEStructureParameter());
136
137         os.write(getExternalAlgorithmParameter());
138
139         if (_data != null) {
140             int off = 0;
141             while (off < _data.length) {
142                 int len = Math.min(30000, _data.length - off);
143                 os.write(getImageDataStart(len));
144                 os.write(_data, off, len);
145                 off += len;
146             }
147         }
148
149         writeEnd(os);
150
151     }
152
153     /**
154      * Helper method to write the start of the Image Content.
155      * @param os The stream to write to
156      */

157     private void writeStart(OutputStream JavaDoc os)
158         throws IOException JavaDoc {
159
160         byte[] data = new byte[] {
161             (byte)0x91, // ID
162
0x01, // Length
163
(byte)0xff, // Object Type = IOCA Image Object
164
};
165
166         os.write(data);
167
168     }
169
170     /**
171      * Helper method to write the end of the Image Content.
172      * @param os The stream to write to
173      */

174     private void writeEnd(OutputStream JavaDoc os)
175         throws IOException JavaDoc {
176
177         byte[] data = new byte[] {
178             (byte)0x93, // ID
179
0x00, // Length
180
};
181
182         os.write(data);
183
184     }
185
186     /**
187      * Helper method to return the start of the image segment.
188      * @return byte[] The data stream.
189      */

190     private byte[] getImageDataStart(int len) {
191
192         byte[] data = new byte[] {
193             (byte)0xFE, // ID
194
(byte)0x92, // ID
195
0x00, // Length
196
0x00, // Length
197
};
198
199         byte[] l = BinaryUtils.convert(len, 2);
200         data[2] = l[0];
201         data[3] = l[1];
202
203
204         return data;
205
206     }
207
208     /**
209      * Helper method to return the image encoding parameter.
210      * @return byte[] The data stream.
211      */

212     private byte[] getImageEncodingParameter() {
213
214         byte[] data = new byte[] {
215             (byte)0x95, // ID
216
0x02, // Length
217
_encoding,
218                   0x01, // RECID
219
};
220
221         return data;
222
223     }
224
225     /**
226      * Helper method to return the external algorithm parameter.
227      * @return byte[] The data stream.
228      */

229     private byte[] getExternalAlgorithmParameter() {
230
231         if (_encoding == (byte)0x83 && _compression != 0) {
232             byte[] data = new byte[] {
233                 (byte)0x95, // ID
234
0x00, // Length
235
0x10, // ALGTYPE = Compression Algorithm
236
0x00, // Reserved
237
(byte)0x83, // COMPRID = JPEG
238
0x00, // Reserved
239
0x00, // Reserved
240
0x00, // Reserved
241
_compression, // MARKER
242
0x00, // Reserved
243
0x00, // Reserved
244
0x00, // Reserved
245
};
246             data[1] = (byte)(data.length - 2);
247             return data;
248         }
249         return new byte[0];
250     }
251
252     /**
253      * Helper method to return the image encoding parameter.
254      * @return byte[] The data stream.
255      */

256     private byte[] getImageIDESizeParameter() {
257
258         byte[] data = new byte[] {
259             (byte)0x96, // ID
260
0x01, // Length
261
_size,
262         };
263
264         return data;
265
266     }
267
268     /**
269      * Helper method to return the external algorithm parameter.
270      * @return byte[] The data stream.
271      */

272     private byte[] getIDEStructureParameter() {
273
274         if (_colorModel != 0 && _size == 24) {
275             byte bits = (byte)(_size / 3);
276             byte[] data = new byte[] {
277                 (byte)0x9B, // ID
278
0x00, // Length
279
0x00, // FLAGS
280
0x00, // Reserved
281
_colorModel, // COLOR MODEL
282
0x00, // Reserved
283
0x00, // Reserved
284
0x00, // Reserved
285
bits,
286                       bits,
287                       bits,
288             };
289             data[1] = (byte)(data.length - 2);
290             return data;
291         }
292         return new byte[0];
293     }
294
295 }
296
Popular Tags