Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Metafile.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.awt.Rectangle;
  18. import java.io.ByteArrayInputStream;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.IOException;
  21. import java.io.OutputStream;
  22. import java.util.zip.DeflaterOutputStream;
  23. import org.apache.poi.ddf.EscherBSERecord;
  24. import org.apache.poi.ddf.EscherContainerRecord;
  25. import org.apache.poi.hslf.usermodel.HSLFPictureData;
  26. import org.apache.poi.hslf.usermodel.HSLFSlideShow;
  27. import org.apache.poi.util.Internal;
  28. import org.apache.poi.util.LittleEndian;
  29. import org.apache.poi.util.LittleEndianInputStream;
  30. import org.apache.poi.util.LittleEndianOutputStream;
  31. import org.apache.poi.util.Removal;
  32. import org.apache.poi.util.Units;
  33. /**
  34. * Represents a metafile picture which can be one of the following types: EMF, WMF, or PICT.
  35. * A metafile is stored compressed using the ZIP deflate/inflate algorithm.
  36. */
  37. public abstract class Metafile extends HSLFPictureData {
  38. /**
  39. * @deprecated Use {@link HSLFSlideShow#addPicture(byte[], PictureType)} or one of it's overloads to create new
  40. * {@link Metafile}. This API led to detached {@link Metafile} instances (See Bugzilla
  41. * 46122) and prevented adding additional functionality.
  42. */
  43. @Deprecated
  44. @Removal(version = "5.3")
  45. public Metafile() {
  46. this(new EscherContainerRecord(), new EscherBSERecord());
  47. }
  48. /**
  49. * Creates a new instance.
  50. *
  51. * @param recordContainer Record tracking all pictures. Should be attached to the slideshow that this picture is
  52. * linked to.
  53. * @param bse Record referencing this picture. Should be attached to the slideshow that this picture is linked to.
  54. */
  55. @Internal
  56. protected Metafile(EscherContainerRecord recordContainer, EscherBSERecord bse) {
  57. super(recordContainer, bse);
  58. }
  59. /**
  60. * A structure which represents a 34-byte header preceding the compressed metafile data
  61. */
  62. public static class Header{
  63. private static final int RECORD_LENGTH = 34;
  64. /**
  65. * size of the original file
  66. */
  67. private int wmfsize;
  68. /**
  69. * Boundary of the metafile drawing commands
  70. */
  71. private final Rectangle bounds = new Rectangle();
  72. /**
  73. * Size of the metafile in EMUs
  74. */
  75. private final Dimension size = new Dimension();
  76. /**
  77. * size of the compressed metafile data
  78. */
  79. private int zipsize;
  80. /**
  81. * Reserved. Always 0.
  82. */
  83. private int compression;
  84. /**
  85. * Reserved. Always 254.
  86. */
  87. private int filter = 254;
  88. public void read(byte[] data, int offset){
  89. @SuppressWarnings("resource")
  90. LittleEndianInputStream leis = new LittleEndianInputStream(
  91. new ByteArrayInputStream(data, offset, RECORD_LENGTH));
  92. wmfsize = leis.readInt();
  93. int left = leis.readInt();
  94. int top = leis.readInt();
  95. int right = leis.readInt();
  96. int bottom = leis.readInt();
  97. bounds.setBounds(left, top, right-left, bottom-top);
  98. int width = leis.readInt();
  99. int height = leis.readInt();
  100. size.setSize(width, height);
  101. zipsize = leis.readInt();
  102. compression = leis.readUByte();
  103. filter = leis.readUByte();
  104. }
  105. public void write(OutputStream out) throws IOException {
  106. @SuppressWarnings("resource")
  107. LittleEndianOutputStream leos = new LittleEndianOutputStream(out);
  108. //hmf
  109. leos.writeInt(wmfsize);
  110. //left
  111. leos.writeInt(bounds.x);
  112. //top
  113. leos.writeInt(bounds.y);
  114. //right
  115. leos.writeInt(bounds.x + bounds.width);
  116. //bottom
  117. leos.writeInt(bounds.y + bounds.height);
  118. //inch
  119. leos.writeInt(size.width);
  120. //inch
  121. leos.writeInt(size.height);
  122. leos.writeInt(zipsize);
  123. leos.writeByte(compression);
  124. leos.writeByte(filter);
  125. }
  126. void write(byte[] destination, int offset) {
  127. //hmf
  128. LittleEndian.putInt(destination, offset, wmfsize);
  129. offset += 4;
  130. //left
  131. LittleEndian.putInt(destination, offset, bounds.x);
  132. offset += 4;
  133. //top
  134. LittleEndian.putInt(destination, offset, bounds.y);
  135. offset += 4;
  136. //right
  137. LittleEndian.putInt(destination, offset, bounds.x + bounds.width);
  138. offset += 4;
  139. //bottom
  140. LittleEndian.putInt(destination, offset, bounds.y + bounds.height);
  141. offset += 4;
  142. //inch
  143. LittleEndian.putInt(destination, offset, size.width);
  144. offset += 4;
  145. //inch
  146. LittleEndian.putInt(destination, offset, size.height);
  147. offset += 4;
  148. LittleEndian.putInt(destination, offset, zipsize);
  149. offset += 4;
  150. destination[offset] = (byte) compression;
  151. offset++;
  152. destination[offset] = (byte) filter;
  153. }
  154. public int getSize(){
  155. return 34;
  156. }
  157. public int getWmfSize() {
  158. return wmfsize;
  159. }
  160. protected void setWmfSize(int wmfSize) {
  161. this.wmfsize = wmfSize;
  162. }
  163. protected void setZipSize(int zipSize) {
  164. this.zipsize = zipSize;
  165. }
  166. public Rectangle getBounds() {
  167. return (Rectangle)bounds.clone();
  168. }
  169. protected void setBounds(Rectangle bounds) {
  170. this.bounds.setBounds(bounds);
  171. }
  172. protected void setDimension(Dimension size) {
  173. this.size.setSize(size);
  174. }
  175. }
  176. protected static byte[] compress(byte[] bytes, int offset, int length) {
  177. ByteArrayOutputStream out = new ByteArrayOutputStream();
  178. try (DeflaterOutputStream deflater = new DeflaterOutputStream(out)) {
  179. deflater.write(bytes, offset, length);
  180. } catch (IOException e) {
  181. // IOException won't get thrown by the DeflaterOutputStream in this configuration because:
  182. // 1. ByteArrayOutputStream doesn't throw an IOException during writes.
  183. // 2. The DeflaterOutputStream is not finished until we're done writing.
  184. throw new AssertionError("Won't happen", e);
  185. }
  186. return out.toByteArray();
  187. }
  188. @Override
  189. public Dimension getImageDimension() {
  190. int prefixLen = 16*getUIDInstanceCount();
  191. Header header = new Header();
  192. header.read(getRawData(), prefixLen);
  193. return new Dimension(
  194. (int)Math.round(Units.toPoints((long)header.size.getWidth())),
  195. (int)Math.round(Units.toPoints((long)header.size.getHeight()))
  196. );
  197. }
  198. }