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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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.Factory;
  22. import org.apache.fop.afp.modca.AbstractNamedAFPObject;
  23. /**
  24. * An Image Segment is represented by a set of self-defining fields, fields
  25. * that describe their own contents. It starts with a Begin Segment, and
  26. * ends with an End Segment.
  27. *
  28. * Between the Begin Segment and End Segment is the image information to
  29. * be processed, called the Image Content.
  30. *
  31. * Only one Image Content can exist within a single IOCA Image Segment.
  32. */
  33. public class ImageSegment extends AbstractNamedAFPObject {
  34. /**
  35. * The ImageContent for the image segment
  36. */
  37. private ImageContent imageContent = null;
  38. private final Factory factory;
  39. /**
  40. * Constructor for the image segment with the specified name,
  41. * the name must be a fixed length of eight characters.
  42. * @param factory the object factory
  43. *
  44. * @param name the name of the image.
  45. */
  46. public ImageSegment(Factory factory, String name) {
  47. super(name);
  48. this.factory = factory;
  49. }
  50. private ImageContent getImageContent() {
  51. if (imageContent == null) {
  52. this.imageContent = factory.createImageContent();
  53. }
  54. return imageContent;
  55. }
  56. /**
  57. * Sets the image size parameters resolution, hsize and vsize.
  58. *
  59. * @param hsize The horizontal size of the image.
  60. * @param vsize The vertical size of the image.
  61. * @param hresol The horizontal resolution of the image.
  62. * @param vresol The vertical resolution of the image.
  63. */
  64. public void setImageSize(int hsize, int vsize, int hresol, int vresol) {
  65. ImageSizeParameter imageSizeParameter
  66. = factory.createImageSizeParameter(hsize, vsize, hresol, vresol);
  67. getImageContent().setImageSizeParameter(imageSizeParameter);
  68. }
  69. /**
  70. * Sets the image encoding.
  71. *
  72. * @param encoding The image encoding.
  73. */
  74. public void setEncoding(byte encoding) {
  75. getImageContent().setImageEncoding(encoding);
  76. }
  77. /**
  78. * Sets the image compression.
  79. *
  80. * @param compression The image compression.
  81. */
  82. public void setCompression(byte compression) {
  83. getImageContent().setImageCompression(compression);
  84. }
  85. /**
  86. * Sets the image IDE size.
  87. *
  88. * @param size The IDE size.
  89. */
  90. public void setIDESize(byte size) {
  91. getImageContent().setImageIDESize(size);
  92. }
  93. /**
  94. * Sets the image IDE color model.
  95. *
  96. * @param colorModel the IDE color model.
  97. */
  98. public void setIDEColorModel(byte colorModel) {
  99. getImageContent().setImageIDEColorModel(colorModel);
  100. }
  101. /**
  102. * Set the data image data.
  103. *
  104. * @param data the image data
  105. */
  106. public void setData(byte[] imageData) {
  107. getImageContent().setImageData(imageData);
  108. }
  109. /** {@inheritDoc} */
  110. public void writeContent(OutputStream os) throws IOException {
  111. if (imageContent != null) {
  112. imageContent.writeToStream(os);
  113. }
  114. }
  115. private static final int NAME_LENGTH = 4;
  116. /** {@inheritDoc} */
  117. protected int getNameLength() {
  118. return NAME_LENGTH;
  119. }
  120. /** {@inheritDoc} */
  121. protected void writeStart(OutputStream os) throws IOException {
  122. byte[] nameBytes = getNameBytes();
  123. byte[] data = new byte[] {
  124. 0x70, // ID
  125. 0x04, // Length
  126. nameBytes[0], // Name byte 1
  127. nameBytes[1], // Name byte 2
  128. nameBytes[2], // Name byte 3
  129. nameBytes[3], // Name byte 4
  130. };
  131. os.write(data);
  132. }
  133. /** {@inheritDoc} */
  134. protected void writeEnd(OutputStream os) throws IOException {
  135. byte[] data = new byte[] {
  136. 0x71, // ID
  137. 0x00, // Length
  138. };
  139. os.write(data);
  140. }
  141. }