KickJava   Java API By Example, From Geeks To Geeks.

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


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: ImageObject.java 426576 2006-07-28 15:44:37Z jeremias $ */
19
20 package org.apache.fop.render.afp.modca;
21
22 import java.io.ByteArrayOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import org.apache.fop.render.afp.tools.BinaryUtils;
27
28 /**
29  * An IOCA Image Data Object
30  */

31 public class ImageObject extends AbstractNamedAFPObject {
32
33     /**
34      * The object environment group
35      */

36     private ObjectEnvironmentGroup _objectEnvironmentGroup = null;
37
38     /**
39      * The image segment
40      */

41     private ImageSegment _imageSegment = null;
42
43     /**
44      * Constructor for the image object with the specified name,
45      * the name must be a fixed length of eight characters.
46      * @param name The name of the image.
47      */

48     public ImageObject(String JavaDoc name) {
49
50         super(name);
51
52     }
53
54     /**
55      * Sets the image display area position and size.
56      *
57      * @param x
58      * the x position of the image
59      * @param y
60      * the y position of the image
61      * @param w
62      * the width of the image
63      * @param h
64      * the height of the image
65      * @param r
66      * the rotation of the image
67      */

68     public void setImageViewport(int x, int y, int w, int h, int r) {
69         if (_objectEnvironmentGroup == null) {
70             _objectEnvironmentGroup = new ObjectEnvironmentGroup();
71         }
72         _objectEnvironmentGroup.setObjectArea(x, y, w, h, r);
73     }
74
75     /**
76      * Set the dimensions of the image.
77      * @param xresol the x resolution of the image
78      * @param yresol the y resolution of the image
79      * @param width the image width
80      * @param height the image height
81      */

82     public void setImageParameters(int xresol, int yresol, int width, int height) {
83         if (_objectEnvironmentGroup == null) {
84             _objectEnvironmentGroup = new ObjectEnvironmentGroup();
85         }
86         _objectEnvironmentGroup.setImageData(xresol, yresol, width, height);
87         if (_imageSegment == null) {
88             _imageSegment = new ImageSegment();
89         }
90         _imageSegment.setImageSize(xresol, yresol, width, height);
91     }
92
93     /**
94      * Sets the image encoding.
95      * @param encoding The image encoding.
96      */

97     public void setImageEncoding(byte encoding) {
98         if (_imageSegment == null) {
99             _imageSegment = new ImageSegment();
100         }
101         _imageSegment.setImageEncoding(encoding);
102     }
103
104     /**
105      * Sets the image compression.
106      * @param compression The image compression.
107      */

108     public void setImageCompression(byte compression) {
109         if (_imageSegment == null) {
110             _imageSegment = new ImageSegment();
111         }
112         _imageSegment.setImageCompression(compression);
113     }
114
115     /**
116      * Sets the image IDE size.
117      * @param size The IDE size.
118      */

119     public void setImageIDESize(byte size) {
120         if (_imageSegment == null) {
121             _imageSegment = new ImageSegment();
122         }
123         _imageSegment.setImageIDESize(size);
124     }
125
126     /**
127      * Sets the image IDE color model.
128      * @param size The IDE color model.
129      */

130     public void setImageIDEColorModel(byte colorModel) {
131         if (_imageSegment == null) {
132             _imageSegment = new ImageSegment();
133         }
134         _imageSegment.setImageIDEColorModel(colorModel);
135     }
136
137     /**
138      * Set the data of the image.
139      * @param data The image data
140      */

141     public void setImageData(byte data[]) {
142         if (_imageSegment == null) {
143             _imageSegment = new ImageSegment();
144         }
145         _imageSegment.setImageData(data);
146     }
147
148     /**
149      * Sets the ObjectEnvironmentGroup.
150      * @param objectEnvironmentGroup The objectEnvironmentGroup to set
151      */

152     public void setObjectEnvironmentGroup(ObjectEnvironmentGroup objectEnvironmentGroup) {
153         _objectEnvironmentGroup = objectEnvironmentGroup;
154     }
155
156     /**
157      * Helper method to return the start of the image object.
158      * @return byte[] The data stream.
159      */

160     private byte[] getIPDStart(int len) {
161
162         byte[] data = new byte[] {
163
164             0x5A, // Structured field identifier
165
0x00, // Length byte 1
166
0x10, // Length byte 2
167
(byte) 0xD3, // Structured field id byte 1
168
(byte) 0xEE, // Structured field id byte 2
169
(byte) 0xFB, // Structured field id byte 3
170
0x00, // Flags
171
0x00, // Reserved
172
0x00, // Reserved
173
};
174
175         byte[] l = BinaryUtils.convert(len + 8, 2);
176         data[1] = l[0];
177         data[2] = l[1];
178
179         return data;
180
181     }
182
183     /**
184      * Accessor method to write the AFP datastream for the Image Object
185      * @param os The stream to write to
186      * @throws java.io.IOException
187      */

188     public void writeDataStream(OutputStream JavaDoc os)
189         throws IOException JavaDoc {
190
191         writeStart(os);
192
193         if (_objectEnvironmentGroup != null) {
194             _objectEnvironmentGroup.writeDataStream(os);
195         }
196
197         if (_imageSegment != null) {
198             ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
199             _imageSegment.writeDataStream(baos);
200             byte b[] = baos.toByteArray();
201             int off = 0;
202             while (off < b.length) {
203                 int len = Math.min(30000, b.length - off);
204                 os.write(getIPDStart(len));
205                 os.write(b, off, len);
206                 off += len;
207             }
208         }
209
210         writeEnd(os);
211
212     }
213
214     /**
215      * Helper method to write the start of the Image Object.
216      * @param os The stream to write to
217      */

218     private void writeStart(OutputStream JavaDoc os)
219         throws IOException JavaDoc {
220
221         byte[] data = new byte[17];
222
223         data[0] = 0x5A; // Structured field identifier
224
data[1] = 0x00; // Length byte 1
225
data[2] = 0x10; // Length byte 2
226
data[3] = (byte) 0xD3; // Structured field id byte 1
227
data[4] = (byte) 0xA8; // Structured field id byte 2
228
data[5] = (byte) 0xFB; // Structured field id byte 3
229
data[6] = 0x00; // Flags
230
data[7] = 0x00; // Reserved
231
data[8] = 0x00; // Reserved
232

233         for (int i = 0; i < _nameBytes.length; i++) {
234
235             data[9 + i] = _nameBytes[i];
236
237         }
238
239         os.write(data);
240
241     }
242
243     /**
244      * Helper method to write the end of the Image Object.
245      * @param os The stream to write to
246      */

247     private void writeEnd(OutputStream JavaDoc os)
248         throws IOException JavaDoc {
249
250         byte[] data = new byte[17];
251
252         data[0] = 0x5A; // Structured field identifier
253
data[1] = 0x00; // Length byte 1
254
data[2] = 0x10; // Length byte 2
255
data[3] = (byte) 0xD3; // Structured field id byte 1
256
data[4] = (byte) 0xA9; // Structured field id byte 2
257
data[5] = (byte) 0xFB; // Structured field id byte 3
258
data[6] = 0x00; // Flags
259
data[7] = 0x00; // Reserved
260
data[8] = 0x00; // Reserved
261

262         for (int i = 0; i < _nameBytes.length; i++) {
263
264             data[9 + i] = _nameBytes[i];
265
266         }
267
268         os.write(data);
269
270     }
271
272 }
273
Popular Tags