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 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. /** JPEG algorithms (usually baseline DCT). */
  48. public static final byte COMPID_JPEG = (byte)0x83;
  49. /** the image size parameter */
  50. private ImageSizeParameter imageSizeParameter;
  51. /** the IDE Structure parameter */
  52. private IDEStructureParameter ideStructureParameter;
  53. /** the image encoding */
  54. private byte encoding = (byte)0x03;
  55. /** the image IDE (Image Data Element, Sample) size */
  56. private byte ideSize = 1;
  57. /** the image compression */
  58. private byte compression = (byte)0xC0; //Baseline DCT in case of JPEG compression
  59. /** the image data */
  60. private byte[] data;
  61. /**
  62. * Main Constructor
  63. */
  64. public ImageContent() {
  65. }
  66. /**
  67. * Sets the image size parameter
  68. *
  69. * @param imageSizeParameter the image size parameter.
  70. */
  71. public void setImageSizeParameter(ImageSizeParameter imageSizeParameter) {
  72. this.imageSizeParameter = imageSizeParameter;
  73. }
  74. /**
  75. * Sets the IDE Structure parameter.
  76. * @param parameter the IDE Structure parameter
  77. */
  78. public void setIDEStructureParameter(IDEStructureParameter parameter) {
  79. this.ideStructureParameter = parameter;
  80. }
  81. /**
  82. * Returns the (optional) IDE Structure parameter
  83. * @return the IDE Structure parameter or null if none is set
  84. */
  85. public IDEStructureParameter getIDEStructureParameter() {
  86. return this.ideStructureParameter;
  87. }
  88. /**
  89. * Returns the (optional) IDE Structure parameter. If none is set an instance is prepared
  90. * with defaults for a bi-level image.
  91. * @return the IDE Structure parameter
  92. */
  93. public IDEStructureParameter needIDEStructureParameter() {
  94. if (this.ideStructureParameter == null) {
  95. setIDEStructureParameter(new IDEStructureParameter());
  96. }
  97. return getIDEStructureParameter();
  98. }
  99. /**
  100. * Sets the image encoding.
  101. *
  102. * @param enc The image encoding.
  103. */
  104. public void setImageEncoding(byte enc) {
  105. this.encoding = enc;
  106. }
  107. /**
  108. * Sets the image compression.
  109. *
  110. * @param comp The image compression.
  111. */
  112. public void setImageCompression(byte comp) {
  113. this.compression = comp;
  114. }
  115. /**
  116. * Sets the image IDE size.
  117. *
  118. * @param s The IDE size.
  119. */
  120. public void setImageIDESize(byte s) {
  121. this.ideSize = s;
  122. }
  123. /**
  124. * Sets the image IDE color model.
  125. *
  126. * @param color the IDE color model.
  127. * @deprecated use {@link #setIDEStructureParameter(IDEStructureParameter)} instead
  128. */
  129. @Deprecated
  130. public void setImageIDEColorModel(byte color) {
  131. needIDEStructureParameter().setColorModel(color);
  132. }
  133. /**
  134. * Set either additive or subtractive mode (used for ASFLAG).
  135. * @param subtractive true for subtractive mode, false for additive mode
  136. * @deprecated use {@link #setIDEStructureParameter(IDEStructureParameter)} instead
  137. */
  138. @Deprecated
  139. public void setSubtractive(boolean subtractive) {
  140. needIDEStructureParameter().setSubtractive(subtractive);
  141. }
  142. /**
  143. * Set the image data (can be byte array or inputstream)
  144. *
  145. * @param imageData the image data
  146. */
  147. public void setImageData(byte[] imageData) {
  148. this.data = imageData;
  149. }
  150. private static final int MAX_DATA_LEN = 65535;
  151. /** {@inheritDoc} */
  152. @Override
  153. protected void writeContent(OutputStream os) throws IOException {
  154. if (imageSizeParameter != null) {
  155. imageSizeParameter.writeToStream(os);
  156. }
  157. // TODO convert to triplet/parameter class
  158. os.write(getImageEncodingParameter());
  159. os.write(getImageIDESizeParameter());
  160. if (getIDEStructureParameter() != null) {
  161. getIDEStructureParameter().writeToStream(os);
  162. }
  163. boolean useFS10 = (this.ideSize == 1);
  164. if (!useFS10) {
  165. os.write(getExternalAlgorithmParameter());
  166. }
  167. final byte[] dataHeader = new byte[] {
  168. (byte)0xFE, // ID
  169. (byte)0x92, // ID
  170. 0x00, // length
  171. 0x00 // length
  172. };
  173. final int lengthOffset = 2;
  174. // Image Data
  175. if (data != null) {
  176. writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
  177. }
  178. }
  179. /** {@inheritDoc} */
  180. @Override
  181. protected void writeStart(OutputStream os) throws IOException {
  182. final byte[] startData = new byte[] {
  183. (byte)0x91, // ID
  184. 0x01, // Length
  185. (byte)0xff, // Object Type = IOCA Image Object
  186. };
  187. os.write(startData);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. protected void writeEnd(OutputStream os) throws IOException {
  192. final byte[] endData = new byte[] {
  193. (byte)0x93, // ID
  194. 0x00, // Length
  195. };
  196. os.write(endData);
  197. }
  198. /**
  199. * Helper method to return the image encoding parameter.
  200. *
  201. * @return byte[] The data stream.
  202. */
  203. private byte[] getImageEncodingParameter() {
  204. final byte[] encodingData = new byte[] {
  205. (byte)0x95, // ID
  206. 0x02, // Length
  207. encoding,
  208. (byte)(encoding == COMPID_JPEG ? 0xFE : 0x01), // RECID
  209. };
  210. return encodingData;
  211. }
  212. /**
  213. * Helper method to return the external algorithm parameter.
  214. *
  215. * @return byte[] The data stream.
  216. */
  217. private byte[] getExternalAlgorithmParameter() {
  218. if (encoding == COMPID_JPEG && compression != 0) {
  219. final byte[] extAlgData = new byte[] {
  220. (byte)0x9F, // ID
  221. 0x00, // Length
  222. 0x10, // ALGTYPE = Compression Algorithm
  223. 0x00, // Reserved
  224. COMPID_JPEG, // COMPRID = JPEG
  225. 0x00, // Reserved
  226. 0x00, // Reserved
  227. 0x00, // Reserved
  228. compression, // MARKER
  229. 0x00, // Reserved
  230. 0x00, // Reserved
  231. 0x00, // Reserved
  232. };
  233. extAlgData[1] = (byte)(extAlgData.length - 2);
  234. return extAlgData;
  235. }
  236. return new byte[0];
  237. }
  238. /**
  239. * Helper method to return the image encoding parameter.
  240. *
  241. * @return byte[] The data stream.
  242. */
  243. private byte[] getImageIDESizeParameter() {
  244. if (ideSize != 1) {
  245. final byte[] ideSizeData = new byte[] {
  246. (byte)0x96, // ID
  247. 0x01, // Length
  248. ideSize};
  249. return ideSizeData;
  250. } else {
  251. return new byte[0];
  252. }
  253. }
  254. }