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.

ImageObject.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.modca;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import org.apache.commons.io.output.ByteArrayOutputStream;
  22. import org.apache.xmlgraphics.util.MimeConstants;
  23. import org.apache.fop.afp.AFPDataObjectInfo;
  24. import org.apache.fop.afp.AFPImageObjectInfo;
  25. import org.apache.fop.afp.Factory;
  26. import org.apache.fop.afp.ioca.ImageSegment;
  27. /**
  28. * An IOCA Image Data Object
  29. */
  30. public class ImageObject extends AbstractDataObject {
  31. private static final int MAX_DATA_LEN = 8192;
  32. /** the image segment */
  33. private ImageSegment imageSegment;
  34. /**
  35. * Constructor for the image object with the specified name,
  36. * the name must be a fixed length of eight characters.
  37. *
  38. * @param name The name of the image.
  39. * @param factory the resource manager
  40. */
  41. public ImageObject(Factory factory, String name) {
  42. super(factory, name);
  43. }
  44. /**
  45. * Returns the image segment object associated with this image object.
  46. * @return the image segment
  47. */
  48. public ImageSegment getImageSegment() {
  49. if (imageSegment == null) {
  50. this.imageSegment = factory.createImageSegment();
  51. }
  52. return imageSegment;
  53. }
  54. /** {@inheritDoc} */
  55. public void setViewport(AFPDataObjectInfo dataObjectInfo) {
  56. super.setViewport(dataObjectInfo);
  57. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  58. int dataWidth = imageObjectInfo.getDataWidth();
  59. int dataHeight = imageObjectInfo.getDataHeight();
  60. int dataWidthRes = imageObjectInfo.getDataWidthRes();
  61. int dataHeightRes = imageObjectInfo.getDataWidthRes();
  62. ImageDataDescriptor imageDataDescriptor
  63. = factory.createImageDataDescriptor(dataWidth, dataHeight, dataWidthRes, dataHeightRes);
  64. if (MimeConstants.MIME_AFP_IOCA_FS45.equals(imageObjectInfo.getMimeType())) {
  65. imageDataDescriptor.setFunctionSet(ImageDataDescriptor.FUNCTION_SET_FS45);
  66. } else if (imageObjectInfo.getBitsPerPixel() == 1) {
  67. imageDataDescriptor.setFunctionSet(ImageDataDescriptor.FUNCTION_SET_FS10);
  68. }
  69. getObjectEnvironmentGroup().setDataDescriptor(imageDataDescriptor);
  70. getObjectEnvironmentGroup().setMapImageObject(
  71. new MapImageObject(dataObjectInfo.getMappingOption()));
  72. getImageSegment().setImageSize(dataWidth, dataHeight, dataWidthRes, dataHeightRes);
  73. }
  74. /**
  75. * Sets the image encoding.
  76. *
  77. * @param encoding The image encoding.
  78. */
  79. public void setEncoding(byte encoding) {
  80. getImageSegment().setEncoding(encoding);
  81. }
  82. /**
  83. * Sets the image compression.
  84. *
  85. * @param compression The image compression.
  86. */
  87. public void setCompression(byte compression) {
  88. getImageSegment().setCompression(compression);
  89. }
  90. /**
  91. * Sets the image IDE size.
  92. *
  93. * @param size The IDE size.
  94. */
  95. public void setIDESize(byte size) {
  96. getImageSegment().setIDESize(size);
  97. }
  98. /**
  99. * Sets the image IDE color model.
  100. *
  101. * @param colorModel the IDE color model.
  102. * @deprecated Use {@link org.apache.fop.afp.ioca.IDEStructureParameter#setColorModel(byte)}
  103. * instead.
  104. */
  105. public void setIDEColorModel(byte colorModel) {
  106. getImageSegment().setIDEColorModel(colorModel);
  107. }
  108. /**
  109. * Set either additive or subtractive mode (used for ASFLAG).
  110. * @param subtractive true for subtractive mode, false for additive mode
  111. * @deprecated Use {@link org.apache.fop.afp.ioca.IDEStructureParameter#setSubtractive(boolean)}
  112. * instead.
  113. */
  114. public void setSubtractive(boolean subtractive) {
  115. getImageSegment().setSubtractive(subtractive);
  116. }
  117. /**
  118. * Set the data of the image.
  119. *
  120. * @param imageData the image data
  121. */
  122. public void setData(byte[] imageData) {
  123. getImageSegment().setData(imageData);
  124. }
  125. /** {@inheritDoc} */
  126. protected void writeStart(OutputStream os) throws IOException {
  127. byte[] data = new byte[17];
  128. copySF(data, Type.BEGIN, Category.IMAGE);
  129. os.write(data);
  130. }
  131. /** {@inheritDoc} */
  132. protected void writeContent(OutputStream os) throws IOException {
  133. super.writeContent(os);
  134. if (imageSegment != null) {
  135. final byte[] dataHeader = new byte[9];
  136. copySF(dataHeader, SF_CLASS, Type.DATA, Category.IMAGE);
  137. final int lengthOffset = 1;
  138. // TODO save memory!
  139. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  140. imageSegment.writeToStream(baos);
  141. byte[] data = baos.toByteArray();
  142. writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
  143. }
  144. }
  145. /** {@inheritDoc} */
  146. protected void writeEnd(OutputStream os) throws IOException {
  147. byte[] data = new byte[17];
  148. copySF(data, Type.END, Category.IMAGE);
  149. os.write(data);
  150. }
  151. }