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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.util.zip.InflaterInputStream;
  20. import org.apache.poi.hslf.exceptions.HSLFException;
  21. import org.apache.poi.hslf.model.Picture;
  22. import org.apache.poi.hslf.model.Shape;
  23. /**
  24. * Represents Macintosh PICT picture data.
  25. *
  26. * @author Yegor Kozlov
  27. */
  28. public final class PICT extends Metafile {
  29. /**
  30. * Extract compressed PICT data from a ppt
  31. */
  32. public byte[] getData(){
  33. byte[] rawdata = getRawData();
  34. try {
  35. byte[] macheader = new byte[512];
  36. ByteArrayOutputStream out = new ByteArrayOutputStream();
  37. out.write(macheader);
  38. int pos = CHECKSUM_SIZE*uidInstanceCount;
  39. byte[] pict;
  40. try {
  41. pict = read(rawdata, pos);
  42. } catch (IOException e){
  43. //weird MAC behaviour.
  44. //if failed to read right after the checksum - skip 16 bytes and try again
  45. pict = read(rawdata, pos + 16);
  46. }
  47. out.write(pict);
  48. return out.toByteArray();
  49. } catch (IOException e){
  50. throw new HSLFException(e);
  51. }
  52. }
  53. private byte[] read(byte[] data, int pos) throws IOException {
  54. ByteArrayOutputStream out = new ByteArrayOutputStream();
  55. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  56. Header header = new Header();
  57. header.read(data, pos);
  58. bis.skip(pos + header.getSize());
  59. InflaterInputStream inflater = new InflaterInputStream( bis );
  60. byte[] chunk = new byte[4096];
  61. int count;
  62. while ((count = inflater.read(chunk)) >=0 ) {
  63. out.write(chunk,0,count);
  64. }
  65. inflater.close();
  66. return out.toByteArray();
  67. }
  68. public void setData(byte[] data) throws IOException {
  69. int pos = 512; //skip the first 512 bytes - they are MAC specific crap
  70. byte[] compressed = compress(data, pos, data.length-pos);
  71. Header header = new Header();
  72. header.wmfsize = data.length - 512;
  73. //we don't have a PICT reader in java, have to set default image size 200x200
  74. header.bounds = new java.awt.Rectangle(0, 0, 200, 200);
  75. header.size = new java.awt.Dimension(header.bounds.width*Shape.EMU_PER_POINT,
  76. header.bounds.height*Shape.EMU_PER_POINT);
  77. header.zipsize = compressed.length;
  78. byte[] checksum = getChecksum(data);
  79. ByteArrayOutputStream out = new ByteArrayOutputStream();
  80. out.write(checksum);
  81. out.write(new byte[16]); //16-byte prefix which is safe to ignore
  82. header.write(out);
  83. out.write(compressed);
  84. setRawData(out.toByteArray());
  85. }
  86. /**
  87. * @see org.apache.poi.hslf.model.Picture#PICT
  88. */
  89. public int getType(){
  90. return Picture.PICT;
  91. }
  92. /**
  93. * PICT signature is {@code 0x5420} or {@code 0x5430}
  94. *
  95. * @return PICT signature ({@code 0x5420} or {@code 0x5430})
  96. */
  97. public int getSignature(){
  98. return (uidInstanceCount == 1 ? 0x5420 : 0x5430);
  99. }
  100. /**
  101. * Sets the PICT signature - either {@code 0x5420} or {@code 0x5430}
  102. */
  103. public void setSignature(int signature) {
  104. switch (signature) {
  105. case 0x5420:
  106. uidInstanceCount = 1;
  107. break;
  108. case 0x5430:
  109. uidInstanceCount = 2;
  110. break;
  111. default:
  112. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for PICT");
  113. }
  114. }
  115. }