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.

PICT.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hslf.blip;
  16. import java.awt.Dimension;
  17. import java.io.ByteArrayInputStream;
  18. import java.io.ByteArrayOutputStream;
  19. import java.io.EOFException;
  20. import java.io.IOException;
  21. import java.util.zip.InflaterInputStream;
  22. import org.apache.logging.log4j.LogManager;
  23. import org.apache.logging.log4j.Logger;
  24. import org.apache.poi.ddf.EscherBSERecord;
  25. import org.apache.poi.ddf.EscherContainerRecord;
  26. import org.apache.poi.hslf.exceptions.HSLFException;
  27. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  28. import org.apache.poi.sl.image.ImageHeaderPICT;
  29. import org.apache.poi.util.IOUtils;
  30. import org.apache.poi.util.Internal;
  31. import org.apache.poi.util.Removal;
  32. import org.apache.poi.util.Units;
  33. import static org.apache.logging.log4j.util.Unbox.box;
  34. /**
  35. * Represents Macintosh PICT picture data.
  36. */
  37. public final class PICT extends Metafile {
  38. private static final Logger LOG = LogManager.getLogger(PICT.class);
  39. /**
  40. * @deprecated Use {@link HSLFSlideShow#addPicture(byte[], PictureType)} or one of it's overloads to create new
  41. * {@link PICT}. This API led to detached {@link PICT} instances (See Bugzilla
  42. * 46122) and prevented adding additional functionality.
  43. */
  44. @Deprecated
  45. @Removal(version = "5.3")
  46. public PICT() {
  47. this(new EscherContainerRecord(), new EscherBSERecord());
  48. }
  49. /**
  50. * Creates a new instance.
  51. *
  52. * @param recordContainer Record tracking all pictures. Should be attached to the slideshow that this picture is
  53. * linked to.
  54. * @param bse Record referencing this picture. Should be attached to the slideshow that this picture is linked to.
  55. */
  56. @Internal
  57. public PICT(EscherContainerRecord recordContainer, EscherBSERecord bse) {
  58. super(recordContainer, bse);
  59. }
  60. @Override
  61. public byte[] getData(){
  62. byte[] rawdata = getRawData();
  63. try {
  64. byte[] macheader = new byte[512];
  65. ByteArrayOutputStream out = new ByteArrayOutputStream();
  66. out.write(macheader);
  67. int pos = CHECKSUM_SIZE*getUIDInstanceCount();
  68. byte[] pict = read(rawdata, pos);
  69. out.write(pict);
  70. return out.toByteArray();
  71. } catch (IOException e){
  72. throw new HSLFException(e);
  73. }
  74. }
  75. private byte[] read(byte[] data, int pos) throws IOException {
  76. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  77. Header header = new Header();
  78. header.read(data, pos);
  79. long bs_exp = (long)pos + header.getSize();
  80. long bs_act = IOUtils.skipFully(bis, bs_exp);
  81. if (bs_exp != bs_act) {
  82. throw new EOFException();
  83. }
  84. byte[] chunk = new byte[4096];
  85. ByteArrayOutputStream out = new ByteArrayOutputStream(header.getWmfSize());
  86. try (InflaterInputStream inflater = new InflaterInputStream(bis)) {
  87. int count;
  88. while ((count = inflater.read(chunk)) >= 0) {
  89. out.write(chunk, 0, count);
  90. // PICT zip-stream can be erroneous, so we clear the array to determine
  91. // the maximum of read bytes, after the inflater crashed
  92. bytefill(chunk, (byte) 0);
  93. }
  94. } catch (Exception e) {
  95. int lastLen;
  96. for (lastLen = chunk.length - 1; lastLen >= 0 && chunk[lastLen] == 0; lastLen--) ;
  97. if (++lastLen > 0) {
  98. if (header.getWmfSize() > out.size()) {
  99. // sometimes the wmfsize is smaller than the amount of already successfully read bytes
  100. // in this case we take the lastLen as-is, otherwise we truncate it to the given size
  101. lastLen = Math.min(lastLen, header.getWmfSize() - out.size());
  102. }
  103. out.write(chunk, 0, lastLen);
  104. }
  105. // End of picture marker for PICT is 0x00 0xFF
  106. LOG.atError().withThrowable(e).log("PICT zip-stream is invalid, read as much as possible. Uncompressed length of header: {} / Read bytes: {}", box(header.getWmfSize()),box(out.size()));
  107. }
  108. return out.toByteArray();
  109. }
  110. @Override
  111. protected byte[] formatImageForSlideshow(byte[] data) {
  112. // skip the first 512 bytes - they are MAC specific crap
  113. final int nOffset = ImageHeaderPICT.PICT_HEADER_OFFSET;
  114. ImageHeaderPICT nHeader = new ImageHeaderPICT(data, nOffset);
  115. Header header = new Header();
  116. int wmfSize = data.length - nOffset;
  117. header.setWmfSize(wmfSize);
  118. byte[] compressed = compress(data, nOffset, wmfSize);
  119. header.setZipSize(compressed.length);
  120. header.setBounds(nHeader.getBounds());
  121. Dimension nDim = nHeader.getSize();
  122. header.setDimension(new Dimension(Units.toEMU(nDim.getWidth()), Units.toEMU(nDim.getHeight())));
  123. byte[] checksum = getChecksum(data);
  124. byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + header.getSize() + compressed.length];
  125. int offset = 0;
  126. System.arraycopy(checksum, 0, rawData, offset, checksum.length);
  127. offset += checksum.length;
  128. if (getUIDInstanceCount() == 2) {
  129. System.arraycopy(checksum, 0, rawData, offset, checksum.length);
  130. offset += checksum.length;
  131. }
  132. header.write(rawData, offset);
  133. offset += header.getSize();
  134. System.arraycopy(compressed, 0, rawData, offset, compressed.length);
  135. return rawData;
  136. }
  137. @Override
  138. public PictureType getType(){
  139. return PictureType.PICT;
  140. }
  141. /**
  142. * PICT signature is {@code 0x5420} or {@code 0x5430}
  143. *
  144. * @return PICT signature ({@code 0x5420} or {@code 0x5430})
  145. */
  146. public int getSignature(){
  147. return (getUIDInstanceCount() == 1 ? 0x5420 : 0x5430);
  148. }
  149. /**
  150. * Sets the PICT signature - either {@code 0x5420} or {@code 0x5430}
  151. */
  152. public void setSignature(int signature) {
  153. switch (signature) {
  154. case 0x5420:
  155. setUIDInstanceCount(1);
  156. break;
  157. case 0x5430:
  158. setUIDInstanceCount(2);
  159. break;
  160. default:
  161. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT");
  162. }
  163. }
  164. /*
  165. * initialize a smaller piece of the array and use the System.arraycopy
  166. * call to fill in the rest of the array in an expanding binary fashion
  167. */
  168. private static void bytefill(byte[] array, byte value) {
  169. // http://stackoverflow.com/questions/9128737/fastest-way-to-set-all-values-of-an-array
  170. int len = array.length;
  171. if (len > 0){
  172. array[0] = value;
  173. }
  174. for (int i = 1; i < len; i += i) {
  175. System.arraycopy(array, 0, array, i, Math.min(len - i, i));
  176. }
  177. }
  178. }