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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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.fop.afp.AFPDataObjectInfo;
  23. import org.apache.fop.afp.AFPImageObjectInfo;
  24. import org.apache.fop.afp.Factory;
  25. import org.apache.fop.afp.ioca.ImageSegment;
  26. /**
  27. * An IOCA Image Data Object
  28. */
  29. public class ImageObject extends AbstractDataObject {
  30. private static final int MAX_DATA_LEN = 32759;
  31. /** the image segment */
  32. private ImageSegment imageSegment = null;
  33. /**
  34. * Constructor for the image object with the specified name,
  35. * the name must be a fixed length of eight characters.
  36. *
  37. * @param name The name of the image.
  38. * @param factory the resource manager
  39. */
  40. public ImageObject(Factory factory, String name) {
  41. super(factory, name);
  42. }
  43. private ImageSegment getImageSegment() {
  44. if (imageSegment == null) {
  45. this.imageSegment = factory.createImageSegment();
  46. }
  47. return imageSegment;
  48. }
  49. /** {@inheritDoc} */
  50. public void setViewport(AFPDataObjectInfo dataObjectInfo) {
  51. super.setViewport(dataObjectInfo);
  52. AFPImageObjectInfo imageObjectInfo = (AFPImageObjectInfo)dataObjectInfo;
  53. int dataWidth = imageObjectInfo.getDataWidth();
  54. int dataHeight = imageObjectInfo.getDataHeight();
  55. // AFPObjectAreaInfo objectAreaInfo = dataObjectInfo.getObjectAreaInfo();
  56. // int widthRes = objectAreaInfo.getWidthRes();
  57. // int heightRes = objectAreaInfo.getHeightRes();
  58. int dataWidthRes = imageObjectInfo.getDataWidthRes();
  59. int dataHeightRes = imageObjectInfo.getDataWidthRes();
  60. ImageDataDescriptor imageDataDescriptor
  61. = factory.createImageDataDescriptor(dataWidth, dataHeight, dataWidthRes, dataHeightRes);
  62. getObjectEnvironmentGroup().setDataDescriptor(imageDataDescriptor);
  63. getImageSegment().setImageSize(dataWidth, dataHeight, dataWidthRes, dataHeightRes);
  64. }
  65. /**
  66. * Sets the image encoding.
  67. *
  68. * @param encoding The image encoding.
  69. */
  70. public void setEncoding(byte encoding) {
  71. getImageSegment().setEncoding(encoding);
  72. }
  73. /**
  74. * Sets the image compression.
  75. *
  76. * @param compression The image compression.
  77. */
  78. public void setCompression(byte compression) {
  79. getImageSegment().setCompression(compression);
  80. }
  81. /**
  82. * Sets the image IDE size.
  83. *
  84. * @param size The IDE size.
  85. */
  86. public void setIDESize(byte size) {
  87. getImageSegment().setIDESize(size);
  88. }
  89. /**
  90. * Sets the image IDE color model.
  91. *
  92. * @param colorModel the IDE color model.
  93. */
  94. public void setIDEColorModel(byte colorModel) {
  95. getImageSegment().setIDEColorModel(colorModel);
  96. }
  97. /**
  98. * Set the data of the image.
  99. *
  100. * @param data the image data
  101. */
  102. public void setData(byte[] imageData) {
  103. getImageSegment().setData(imageData);
  104. }
  105. /** {@inheritDoc} */
  106. protected void writeStart(OutputStream os) throws IOException {
  107. byte[] data = new byte[17];
  108. copySF(data, Type.BEGIN, Category.IMAGE);
  109. os.write(data);
  110. }
  111. /** {@inheritDoc} */
  112. protected void writeContent(OutputStream os) throws IOException {
  113. super.writeContent(os);
  114. if (imageSegment != null) {
  115. final byte[] dataHeader = new byte[9];
  116. copySF(dataHeader, SF_CLASS, Type.DATA, Category.IMAGE);
  117. final int lengthOffset = 1;
  118. // TODO save memory!
  119. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  120. imageSegment.writeToStream(baos);
  121. byte[] data = baos.toByteArray();
  122. writeChunksToStream(data, dataHeader, lengthOffset, MAX_DATA_LEN, os);
  123. }
  124. }
  125. /** {@inheritDoc} */
  126. protected void writeEnd(OutputStream os) throws IOException {
  127. byte[] data = new byte[17];
  128. copySF(data, Type.END, Category.IMAGE);
  129. os.write(data);
  130. }
  131. }