001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ImageContent.java 426576 2006-07-28 15:44:37Z jeremias $ */
019:
020: package org.apache.fop.render.afp.modca;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024: import org.apache.fop.render.afp.tools.BinaryUtils;
025:
026: /**
027: */
028: public class ImageContent extends AbstractAFPObject {
029:
030: /**
031: * The image size parameter
032: */
033: private ImageSizeParameter _imageSizeParameter = null;
034:
035: /**
036: * The image encoding
037: */
038: private byte _encoding = 0x03;
039:
040: /**
041: * The image ide size
042: */
043: private byte _size = 1;
044:
045: /**
046: * The image compression
047: */
048: private byte _compression = (byte) 0xC0;
049:
050: /**
051: * The image color model
052: */
053: private byte _colorModel = 0x01;
054:
055: /**
056: * The image data
057: */
058: private byte _data[] = null;
059:
060: /**
061: * Constructor for the image content
062: */
063: public ImageContent() {
064:
065: }
066:
067: /**
068: * Sets the image size parameters
069: * resolution, hsize and vsize.
070: * @param hresol The horizontal resolution of the image.
071: * @param vresol The vertical resolution of the image.
072: * @param hsize The horizontal size of the image.
073: * @param vsize The vertival size of the image.
074: */
075: public void setImageSize(int hresol, int vresol, int hsize,
076: int vsize) {
077: _imageSizeParameter = new ImageSizeParameter(hresol, vresol,
078: hsize, vsize);
079: }
080:
081: /**
082: * Sets the image encoding.
083: * @param encoding The image encoding.
084: */
085: public void setImageEncoding(byte encoding) {
086: _encoding = encoding;
087: }
088:
089: /**
090: * Sets the image compression.
091: * @param compression The image compression.
092: */
093: public void setImageCompression(byte compression) {
094: _compression = compression;
095: }
096:
097: /**
098: * Sets the image IDE size.
099: * @param size The IDE size.
100: */
101: public void setImageIDESize(byte size) {
102: _size = size;
103: }
104:
105: /**
106: * Sets the image IDE color model.
107: * @param size The IDE color model.
108: */
109: public void setImageIDEColorModel(byte colorModel) {
110: _colorModel = colorModel;
111: }
112:
113: /**
114: * Set the data of the image.
115: */
116: public void setImageData(byte data[]) {
117: _data = data;
118: }
119:
120: /**
121: * Accessor method to write the AFP datastream for the Image Content
122: * @param os The stream to write to
123: * @throws java.io.IOException
124: */
125: public void writeDataStream(OutputStream os) throws IOException {
126:
127: writeStart(os);
128:
129: if (_imageSizeParameter != null) {
130: _imageSizeParameter.writeDataStream(os);
131: }
132:
133: os.write(getImageEncodingParameter());
134:
135: os.write(getImageIDESizeParameter());
136:
137: os.write(getIDEStructureParameter());
138:
139: os.write(getExternalAlgorithmParameter());
140:
141: if (_data != null) {
142: int off = 0;
143: while (off < _data.length) {
144: int len = Math.min(30000, _data.length - off);
145: os.write(getImageDataStart(len));
146: os.write(_data, off, len);
147: off += len;
148: }
149: }
150:
151: writeEnd(os);
152:
153: }
154:
155: /**
156: * Helper method to write the start of the Image Content.
157: * @param os The stream to write to
158: */
159: private void writeStart(OutputStream os) throws IOException {
160:
161: byte[] data = new byte[] { (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 os) throws IOException {
175:
176: byte[] data = new byte[] { (byte) 0x93, // ID
177: 0x00, // Length
178: };
179:
180: os.write(data);
181:
182: }
183:
184: /**
185: * Helper method to return the start of the image segment.
186: * @return byte[] The data stream.
187: */
188: private byte[] getImageDataStart(int len) {
189:
190: byte[] data = new byte[] { (byte) 0xFE, // ID
191: (byte) 0x92, // ID
192: 0x00, // Length
193: 0x00, // Length
194: };
195:
196: byte[] l = BinaryUtils.convert(len, 2);
197: data[2] = l[0];
198: data[3] = l[1];
199:
200: return data;
201:
202: }
203:
204: /**
205: * Helper method to return the image encoding parameter.
206: * @return byte[] The data stream.
207: */
208: private byte[] getImageEncodingParameter() {
209:
210: byte[] data = new byte[] { (byte) 0x95, // ID
211: 0x02, // Length
212: _encoding, 0x01, // RECID
213: };
214:
215: return data;
216:
217: }
218:
219: /**
220: * Helper method to return the external algorithm parameter.
221: * @return byte[] The data stream.
222: */
223: private byte[] getExternalAlgorithmParameter() {
224:
225: if (_encoding == (byte) 0x83 && _compression != 0) {
226: byte[] data = new byte[] { (byte) 0x95, // ID
227: 0x00, // Length
228: 0x10, // ALGTYPE = Compression Algorithm
229: 0x00, // Reserved
230: (byte) 0x83, // COMPRID = JPEG
231: 0x00, // Reserved
232: 0x00, // Reserved
233: 0x00, // Reserved
234: _compression, // MARKER
235: 0x00, // Reserved
236: 0x00, // Reserved
237: 0x00, // Reserved
238: };
239: data[1] = (byte) (data.length - 2);
240: return data;
241: }
242: return new byte[0];
243: }
244:
245: /**
246: * Helper method to return the image encoding parameter.
247: * @return byte[] The data stream.
248: */
249: private byte[] getImageIDESizeParameter() {
250:
251: byte[] data = new byte[] { (byte) 0x96, // ID
252: 0x01, // Length
253: _size, };
254:
255: return data;
256:
257: }
258:
259: /**
260: * Helper method to return the external algorithm parameter.
261: * @return byte[] The data stream.
262: */
263: private byte[] getIDEStructureParameter() {
264:
265: if (_colorModel != 0 && _size == 24) {
266: byte bits = (byte) (_size / 3);
267: byte[] data = new byte[] { (byte) 0x9B, // ID
268: 0x00, // Length
269: 0x00, // FLAGS
270: 0x00, // Reserved
271: _colorModel, // COLOR MODEL
272: 0x00, // Reserved
273: 0x00, // Reserved
274: 0x00, // Reserved
275: bits, bits, bits, };
276: data[1] = (byte) (data.length - 2);
277: return data;
278: }
279: return new byte[0];
280: }
281:
282: }
|