KickJava   Java API By Example, From Geeks To Geeks.

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


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: ImageSegment.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 java.io.UnsupportedEncodingException JavaDoc;
24
25 /**
26  * An Image Segment is represented by a set of self-defining fields, fields
27  * that describe their own contents. It starts with a Begin Segment, and
28  * ends with an End Segment.
29  *
30  * Between the Begin Segment and End Segment is the image information to
31  * be processed, called the Image Content.
32  *
33  * Only one Image Content can exist within a single IOCA Image Segment.
34  */

35 public class ImageSegment extends AbstractAFPObject {
36
37     /**
38      * Default name for the object environment group
39      */

40     private static final String JavaDoc DEFAULT_NAME = "IS01";
41
42     /**
43      * The name of the image segment
44      */

45     private String JavaDoc _name;
46
47     /**
48      * The name of the image segment as EBCIDIC bytes
49      */

50     private byte[] _nameBytes;
51
52     /**
53      * The ImageContent for the image segment
54      */

55     private ImageContent _imageContent = null;
56
57     /**
58      * Default constructor for the ImageSegment.
59      */

60     public ImageSegment() {
61
62         this(DEFAULT_NAME);
63
64     }
65
66     /**
67      * Constructor for the image segment with the specified name,
68      * the name must be a fixed length of eight characters.
69      * @param name The name of the image.
70      */

71     public ImageSegment(String JavaDoc name) {
72
73         if (name.length() != 4) {
74             String JavaDoc msg = "Image segment name must be 4 characters long " + name;
75             log.error("Constructor:: " + msg);
76             throw new IllegalArgumentException JavaDoc(msg);
77         }
78
79         _name = name;
80
81         try {
82
83             _nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
84
85         } catch (UnsupportedEncodingException JavaDoc usee) {
86
87             _nameBytes = name.getBytes();
88             log.warn(
89                 "Constructor:: UnsupportedEncodingException translating the name "
90                 + name);
91
92         }
93
94     }
95
96     /**
97      * Sets the image size parameters
98      * resolution, hsize and vsize.
99      * @param hresol The horizontal resolution of the image.
100      * @param vresol The vertical resolution of the image.
101      * @param hsize The horizontal size of the image.
102      * @param vsize The vertival size of the image.
103      */

104     public void setImageSize(int hresol, int vresol, int hsize, int vsize) {
105         if (_imageContent == null) {
106             _imageContent = new ImageContent();
107         }
108         _imageContent.setImageSize(hresol, vresol, hsize, vsize);
109     }
110
111     /**
112      * Sets the image encoding.
113      * @param encoding The image encoding.
114      */

115     public void setImageEncoding(byte encoding) {
116         if (_imageContent == null) {
117             _imageContent = new ImageContent();
118         }
119         _imageContent.setImageEncoding(encoding);
120     }
121
122     /**
123      * Sets the image compression.
124      * @param compression The image compression.
125      */

126     public void setImageCompression(byte compression) {
127         if (_imageContent == null) {
128             _imageContent = new ImageContent();
129         }
130         _imageContent.setImageCompression(compression);
131     }
132
133     /**
134      * Sets the image IDE size.
135      * @param size The IDE size.
136      */

137     public void setImageIDESize(byte size) {
138         if (_imageContent == null) {
139             _imageContent = new ImageContent();
140         }
141         _imageContent.setImageIDESize(size);
142     }
143
144     /**
145      * Sets the image IDE color model.
146      * @param size The IDE color model.
147      */

148     public void setImageIDEColorModel(byte colorModel) {
149         if (_imageContent == null) {
150             _imageContent = new ImageContent();
151         }
152         _imageContent.setImageIDEColorModel(colorModel);
153     }
154
155     /**
156      * Set the data of the image.
157      * @param data the image data
158      */

159     public void setImageData(byte data[]) {
160         if (_imageContent == null) {
161             _imageContent = new ImageContent();
162         }
163         _imageContent.setImageData(data);
164     }
165
166     /**
167      * Accessor method to write the AFP datastream for the Image Segment
168      * @param os The stream to write to
169      * @throws java.io.IOException
170      */

171     public void writeDataStream(OutputStream JavaDoc os)
172         throws IOException JavaDoc {
173
174         writeStart(os);
175
176         if (_imageContent != null) {
177             _imageContent.writeDataStream(os);
178         }
179
180         writeEnd(os);
181
182     }
183
184     /**
185      * Helper method to write the start of the Image Segment.
186      * @param os The stream to write to
187      */

188     private void writeStart(OutputStream JavaDoc os)
189         throws IOException JavaDoc {
190
191         byte[] data = new byte[] {
192             0x70, // ID
193
0x04, // Length
194
0x00, // Name byte 1
195
0x00, // Name byte 2
196
0x00, // Name byte 3
197
0x00, // Name byte 4
198
};
199
200         for (int i = 0; i < _nameBytes.length; i++) {
201
202             data[2 + i] = _nameBytes[i];
203
204         }
205
206         os.write(data);
207
208     }
209
210     /**
211      * Helper method to write the end of the Image Segment.
212      * @param os The stream to write to
213      */

214     private void writeEnd(OutputStream JavaDoc os)
215         throws IOException JavaDoc {
216
217         byte[] data = new byte[] {
218             0x71, // ID
219
0x00, // Length
220
};
221
222         os.write(data);
223
224     }
225
226 }
227
Popular Tags