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.

ImageSegment.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.afp.modca;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.io.UnsupportedEncodingException;
  21. /**
  22. * An Image Segment is represented by a set of self-defining fields, fields
  23. * that describe their own contents. It starts with a Begin Segment, and
  24. * ends with an End Segment.
  25. *
  26. * Between the Begin Segment and End Segment is the image information to
  27. * be processed, called the Image Content.
  28. *
  29. * Only one Image Content can exist within a single IOCA Image Segment.
  30. */
  31. public class ImageSegment extends AbstractAFPObject {
  32. /**
  33. * Default name for the object environment group
  34. */
  35. private static final String DEFAULT_NAME = "IS01";
  36. /**
  37. * The name of the image segment
  38. */
  39. private String _name;
  40. /**
  41. * The name of the image segment as EBCIDIC bytes
  42. */
  43. private byte[] _nameBytes;
  44. /**
  45. * The ImageContent for the image segment
  46. */
  47. private ImageContent _imageContent = null;
  48. /**
  49. * Default constructor for the ImageSegment.
  50. */
  51. public ImageSegment() {
  52. this(DEFAULT_NAME);
  53. }
  54. /**
  55. * Constructor for the image segment with the specified name,
  56. * the name must be a fixed length of eight characters.
  57. * @param name The name of the image.
  58. */
  59. public ImageSegment(String name) {
  60. if (name.length() != 4) {
  61. String msg = "Image segment name must be 4 characters long " + name;
  62. log.error("Constructor:: " + msg);
  63. throw new IllegalArgumentException(msg);
  64. }
  65. _name = name;
  66. try {
  67. _nameBytes = name.getBytes(AFPConstants.EBCIDIC_ENCODING);
  68. } catch (UnsupportedEncodingException usee) {
  69. _nameBytes = name.getBytes();
  70. log.warn(
  71. "Constructor:: UnsupportedEncodingException translating the name "
  72. + name);
  73. }
  74. }
  75. /**
  76. * Sets the image size parameters
  77. * resolution, hsize and vsize.
  78. * @param hresol The horizontal resolution of the image.
  79. * @param vresol The vertical resolution of the image.
  80. * @param hsize The horizontal size of the image.
  81. * @param vsize The vertival size of the image.
  82. */
  83. public void setImageSize(int hresol, int vresol, int hsize, int vsize) {
  84. if (_imageContent == null) {
  85. _imageContent = new ImageContent();
  86. }
  87. _imageContent.setImageSize(hresol, vresol, hsize, vsize);
  88. }
  89. /**
  90. * Sets the image encoding.
  91. * @param encoding The image encoding.
  92. */
  93. public void setImageEncoding(byte encoding) {
  94. if (_imageContent == null) {
  95. _imageContent = new ImageContent();
  96. }
  97. _imageContent.setImageEncoding(encoding);
  98. }
  99. /**
  100. * Sets the image compression.
  101. * @param compression The image compression.
  102. */
  103. public void setImageCompression(byte compression) {
  104. if (_imageContent == null) {
  105. _imageContent = new ImageContent();
  106. }
  107. _imageContent.setImageCompression(compression);
  108. }
  109. /**
  110. * Sets the image IDE size.
  111. * @param size The IDE size.
  112. */
  113. public void setImageIDESize(byte size) {
  114. if (_imageContent == null) {
  115. _imageContent = new ImageContent();
  116. }
  117. _imageContent.setImageIDESize(size);
  118. }
  119. /**
  120. * Sets the image IDE color model.
  121. * @param size The IDE color model.
  122. */
  123. public void setImageIDEColorModel(byte colorModel) {
  124. if (_imageContent == null) {
  125. _imageContent = new ImageContent();
  126. }
  127. _imageContent.setImageIDEColorModel(colorModel);
  128. }
  129. /**
  130. * Set the data of the image.
  131. * @param data the image data
  132. */
  133. public void setImageData(byte data[]) {
  134. if (_imageContent == null) {
  135. _imageContent = new ImageContent();
  136. }
  137. _imageContent.setImageData(data);
  138. }
  139. /**
  140. * Accessor method to write the AFP datastream for the Image Segment
  141. * @param os The stream to write to
  142. * @throws java.io.IOException
  143. */
  144. public void writeDataStream(OutputStream os)
  145. throws IOException {
  146. writeStart(os);
  147. if (_imageContent != null) {
  148. _imageContent.writeDataStream(os);
  149. }
  150. writeEnd(os);
  151. }
  152. /**
  153. * Helper method to write the start of the Image Segment.
  154. * @param os The stream to write to
  155. */
  156. private void writeStart(OutputStream os)
  157. throws IOException {
  158. byte[] data = new byte[] {
  159. 0x70, // ID
  160. 0x04, // Length
  161. 0x00, // Name byte 1
  162. 0x00, // Name byte 2
  163. 0x00, // Name byte 3
  164. 0x00, // Name byte 4
  165. };
  166. for (int i = 0; i < _nameBytes.length; i++) {
  167. data[2 + i] = _nameBytes[i];
  168. }
  169. os.write(data);
  170. }
  171. /**
  172. * Helper method to write the end of the Image Segment.
  173. * @param os The stream to write to
  174. */
  175. private void writeEnd(OutputStream os)
  176. throws IOException {
  177. byte[] data = new byte[] {
  178. 0x71, // ID
  179. 0x00, // Length
  180. };
  181. os.write(data);
  182. }
  183. }