You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageContent.java 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. /* $Id$ */
  18. package org.apache.fop.afp.ioca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.fop.afp.modca.AbstractStructuredObject;
  22. /**
  23. * An IOCA Image Content
  24. */
  25. public class ImageContent extends AbstractStructuredObject {
  26. /**
  27. * The CCITT T.4 Group 3 Coding Standard (G3 MH-Modified Huffman) is a
  28. * compression method standardized by the International Telegraph and
  29. * Telephone Consultative Committee (CCITT) for facsimile. It enables
  30. * one-dimensional compression.
  31. */
  32. public static final byte COMPID_G3_MH = (byte)0x80;
  33. /**
  34. * The CCITT T.4 Group 3 Coding Option (G3 MR-Modified READ) is a
  35. * compression method standardized by the International Telegraph and
  36. * Telephone Consultative Committee (CCITT) for facsimile. It enables
  37. * two-dimensional compression.
  38. */
  39. public static final byte COMPID_G3_MR = (byte)0x81;
  40. /**
  41. * The CCITT T.6 Group 4 Coding Standard (G4 MMR-Modified Modified READ) is a
  42. * compression method standardized by the International Telegraph and
  43. * Telephone Consultative Committee (CCITT) for facsimile. It enables
  44. * two-dimensional compression.
  45. */
  46. public static final byte COMPID_G3_MMR = (byte)0x82;
  47. /** the image size parameter */
  48. private ImageSizeParameter imageSizeParameter = null;
  49. /** the image encoding */
  50. private byte encoding = (byte)0x03;
  51. /** the image ide size */
  52. private byte size = 1;
  53. /** the image compression */
  54. private byte compression = (byte)0xC0;
  55. /** the image color model */
  56. private byte colorModel = (byte)0x01;
  57. /** the image data */
  58. private byte[] data;
  59. /**
  60. * Main Constructor
  61. */
  62. public ImageContent() {
  63. }
  64. /**
  65. * Sets the image size parameter
  66. *
  67. * @param imageSizeParameter the image size parameter.
  68. */
  69. public void setImageSizeParameter(ImageSizeParameter imageSizeParameter) {
  70. this.imageSizeParameter = imageSizeParameter;
  71. }
  72. /**
  73. * Sets the image encoding.
  74. *
  75. * @param enc The image encoding.
  76. */
  77. public void setImageEncoding(byte enc) {
  78. this.encoding = enc;
  79. }
  80. /**
  81. * Sets the image compression.
  82. *
  83. * @param comp The image compression.
  84. */
  85. public void setImageCompression(byte comp) {
  86. this.compression = comp;
  87. }
  88. /**
  89. * Sets the image IDE size.
  90. *
  91. * @param s The IDE size.
  92. */
  93. public void setImageIDESize(byte s) {
  94. this.size = s;
  95. }
  96. /**
  97. * Sets the image IDE color model.
  98. *
  99. * @param color the IDE color model.
  100. */
  101. public void setImageIDEColorModel(byte color) {
  102. this.colorModel = color;
  103. }
  104. /**
  105. * Set the image data (can be byte array or inputstream)
  106. *
  107. * @param imageData the image data
  108. */
  109. public void setImageData(byte[] imageData) {
  110. this.data = imageData;
  111. }
  112. private static final int MAX_DATA_LEN = 65535;
  113. /** {@inheritDoc} */
  114. protected void writeContent(OutputStream os) throws IOException {
  115. if (imageSizeParameter != null) {
  116. imageSizeParameter.writeToStream(os);
  117. }
  118. // TODO convert to triplet/parameter class
  119. os.write(getImageEncodingParameter());
  120. os.write(getImageIDESizeParameter());
  121. os.write(getIDEStructureParameter());
  122. os.write(getExternalAlgorithmParameter());
  123. final byte[] dataHeader = new byte[] {
  124. (byte)0xFE, // ID
  125. (byte)0x92, // ID
  126. 0x00, // length
  127. 0x00 // length
  128. };
  129. final int lengthOffset = 2;
  130. // Image Data
  131. if (data != null) {
  132. writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
  133. }
  134. }
  135. /** {@inheritDoc} */
  136. protected void writeStart(OutputStream os) throws IOException {
  137. final byte[] startData = new byte[] {
  138. (byte)0x91, // ID
  139. 0x01, // Length
  140. (byte)0xff, // Object Type = IOCA Image Object
  141. };
  142. os.write(startData);
  143. }
  144. /** {@inheritDoc} */
  145. protected void writeEnd(OutputStream os) throws IOException {
  146. final byte[] endData = new byte[] {
  147. (byte)0x93, // ID
  148. 0x00, // Length
  149. };
  150. os.write(endData);
  151. }
  152. /**
  153. * Helper method to return the image encoding parameter.
  154. *
  155. * @return byte[] The data stream.
  156. */
  157. private byte[] getImageEncodingParameter() {
  158. final byte[] encodingData = new byte[] {
  159. (byte)0x95, // ID
  160. 0x02, // Length
  161. encoding,
  162. 0x01, // RECID
  163. };
  164. return encodingData;
  165. }
  166. /**
  167. * Helper method to return the external algorithm parameter.
  168. *
  169. * @return byte[] The data stream.
  170. */
  171. private byte[] getExternalAlgorithmParameter() {
  172. if (encoding == (byte)0x83 && compression != 0) {
  173. final byte[] extAlgData = new byte[] {
  174. (byte)0x95, // ID
  175. 0x00, // Length
  176. 0x10, // ALGTYPE = Compression Algorithm
  177. 0x00, // Reserved
  178. (byte)0x83, // COMPRID = JPEG
  179. 0x00, // Reserved
  180. 0x00, // Reserved
  181. 0x00, // Reserved
  182. compression, // MARKER
  183. 0x00, // Reserved
  184. 0x00, // Reserved
  185. 0x00, // Reserved
  186. };
  187. extAlgData[1] = (byte)(extAlgData.length - 2);
  188. return extAlgData;
  189. }
  190. return new byte[0];
  191. }
  192. /**
  193. * Helper method to return the image encoding parameter.
  194. *
  195. * @return byte[] The data stream.
  196. */
  197. private byte[] getImageIDESizeParameter() {
  198. final byte[] ideSizeData = new byte[] {
  199. (byte)0x96, // ID
  200. 0x01, // Length
  201. size,
  202. };
  203. return ideSizeData;
  204. }
  205. /**
  206. * Helper method to return the external algorithm parameter.
  207. *
  208. * @return byte[] The data stream.
  209. */
  210. private byte[] getIDEStructureParameter() {
  211. if (colorModel != 0 && size == 24) {
  212. final byte bits = (byte)(size / 3);
  213. final byte[] ideStructData = new byte[] {
  214. (byte)0x9B, // ID
  215. 0x00, // Length
  216. 0x00, // FLAGS
  217. 0x00, // Reserved
  218. colorModel, // COLOR MODEL
  219. 0x00, // Reserved
  220. 0x00, // Reserved
  221. 0x00, // Reserved
  222. bits,
  223. bits,
  224. bits,
  225. };
  226. ideStructData[1] = (byte)(ideStructData.length - 2);
  227. return ideStructData;
  228. }
  229. return new byte[0];
  230. }
  231. }