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.

WMF.java 5.5KB

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. 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.IOException;
  20. import java.io.InputStream;
  21. import java.util.zip.InflaterInputStream;
  22. import org.apache.poi.ddf.EscherBSERecord;
  23. import org.apache.poi.ddf.EscherContainerRecord;
  24. import org.apache.poi.hslf.exceptions.HSLFException;
  25. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  26. import org.apache.poi.sl.image.ImageHeaderWMF;
  27. import org.apache.poi.util.IOUtils;
  28. import org.apache.poi.util.Internal;
  29. import org.apache.poi.util.Removal;
  30. import org.apache.poi.util.Units;
  31. /**
  32. * Represents a WMF (Windows Metafile) picture data.
  33. */
  34. public final class WMF extends Metafile {
  35. /**
  36. * @deprecated Use {@link HSLFSlideShow#addPicture(byte[], PictureType)} or one of it's overloads to create new
  37. * {@link WMF}. This API led to detached {@link WMF} instances (See Bugzilla
  38. * 46122) and prevented adding additional functionality.
  39. */
  40. @Deprecated
  41. @Removal(version = "5.3")
  42. public WMF() {
  43. this(new EscherContainerRecord(), new EscherBSERecord());
  44. }
  45. /**
  46. * Creates a new instance.
  47. *
  48. * @param recordContainer Record tracking all pictures. Should be attached to the slideshow that this picture is
  49. * linked to.
  50. * @param bse Record referencing this picture. Should be attached to the slideshow that this picture is linked to.
  51. */
  52. @Internal
  53. public WMF(EscherContainerRecord recordContainer, EscherBSERecord bse) {
  54. super(recordContainer, bse);
  55. }
  56. @Override
  57. public byte[] getData(){
  58. try {
  59. byte[] rawdata = getRawData();
  60. ByteArrayOutputStream out = new ByteArrayOutputStream();
  61. InputStream is = new ByteArrayInputStream( rawdata );
  62. Header header = new Header();
  63. header.read(rawdata, CHECKSUM_SIZE*getUIDInstanceCount());
  64. long skipLen = header.getSize() + (long)CHECKSUM_SIZE*getUIDInstanceCount();
  65. long skipped = IOUtils.skipFully(is, skipLen);
  66. assert(skipped == skipLen);
  67. ImageHeaderWMF aldus = new ImageHeaderWMF(header.getBounds());
  68. aldus.write(out);
  69. InflaterInputStream inflater = new InflaterInputStream( is );
  70. byte[] chunk = new byte[4096];
  71. int count;
  72. while ((count = inflater.read(chunk)) >=0 ) {
  73. out.write(chunk,0,count);
  74. }
  75. inflater.close();
  76. return out.toByteArray();
  77. } catch (IOException e){
  78. throw new HSLFException(e);
  79. }
  80. }
  81. @Override
  82. protected byte[] formatImageForSlideshow(byte[] data) {
  83. int pos = 0;
  84. ImageHeaderWMF nHeader = new ImageHeaderWMF(data, pos);
  85. pos += nHeader.getLength();
  86. byte[] compressed = compress(data, pos, data.length-pos);
  87. Header header = new Header();
  88. header.setWmfSize(data.length - nHeader.getLength());
  89. header.setBounds(nHeader.getBounds());
  90. Dimension nDim = nHeader.getSize();
  91. header.setDimension(new Dimension(Units.toEMU(nDim.getWidth()), Units.toEMU(nDim.getHeight())));
  92. header.setZipSize(compressed.length);
  93. byte[] checksum = getChecksum(data);
  94. byte[] rawData = new byte[checksum.length * getUIDInstanceCount() + header.getSize() + compressed.length];
  95. int offset = 0;
  96. System.arraycopy(checksum, 0, rawData, offset, checksum.length);
  97. offset += checksum.length;
  98. if (getUIDInstanceCount() == 2) {
  99. System.arraycopy(checksum, 0, rawData, offset, checksum.length);
  100. offset += checksum.length;
  101. }
  102. header.write(rawData, offset);
  103. offset += header.getSize();
  104. System.arraycopy(compressed, 0, rawData, offset, compressed.length);
  105. return rawData;
  106. }
  107. @Override
  108. public PictureType getType(){
  109. return PictureType.WMF;
  110. }
  111. /**
  112. * WMF signature is either {@code 0x2160} or {@code 0x2170}
  113. */
  114. public int getSignature(){
  115. return (getUIDInstanceCount() == 1 ? 0x2160 : 0x2170);
  116. }
  117. /**
  118. * Sets the WMF signature - either {@code 0x2160} or {@code 0x2170}
  119. */
  120. public void setSignature(int signature) {
  121. switch (signature) {
  122. case 0x2160:
  123. setUIDInstanceCount(1);
  124. break;
  125. case 0x2170:
  126. setUIDInstanceCount(2);
  127. break;
  128. default:
  129. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for WMF");
  130. }
  131. }
  132. }