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.

EMF.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 org.apache.poi.hslf.model.Picture;
  17. import org.apache.poi.hslf.model.Shape;
  18. import org.apache.poi.hslf.exceptions.HSLFException;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.InputStream;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import java.util.zip.InflaterInputStream;
  24. /**
  25. * Represents EMF (Windows Enhanced Metafile) picture data.
  26. *
  27. * @author Yegor Kozlov
  28. */
  29. public final class EMF extends Metafile {
  30. /**
  31. * Extract compressed EMF data from a ppt
  32. */
  33. public byte[] getData(){
  34. try {
  35. byte[] rawdata = getRawData();
  36. ByteArrayOutputStream out = new ByteArrayOutputStream();
  37. InputStream is = new ByteArrayInputStream( rawdata );
  38. Header header = new Header();
  39. header.read(rawdata, CHECKSUM_SIZE);
  40. is.skip(header.getSize() + CHECKSUM_SIZE);
  41. InflaterInputStream inflater = new InflaterInputStream( is );
  42. byte[] chunk = new byte[4096];
  43. int count;
  44. while ((count = inflater.read(chunk)) >=0 ) {
  45. out.write(chunk,0,count);
  46. }
  47. inflater.close();
  48. return out.toByteArray();
  49. } catch (IOException e){
  50. throw new HSLFException(e);
  51. }
  52. }
  53. public void setData(byte[] data) throws IOException {
  54. byte[] compressed = compress(data, 0, data.length);
  55. Header header = new Header();
  56. header.wmfsize = data.length;
  57. //we don't have a EMF reader in java, have to set default image size 200x200
  58. header.bounds = new java.awt.Rectangle(0, 0, 200, 200);
  59. header.size = new java.awt.Dimension(header.bounds.width*Shape.EMU_PER_POINT, header.bounds.height*Shape.EMU_PER_POINT);
  60. header.zipsize = compressed.length;
  61. byte[] checksum = getChecksum(data);
  62. ByteArrayOutputStream out = new ByteArrayOutputStream();
  63. out.write(checksum);
  64. header.write(out);
  65. out.write(compressed);
  66. setRawData(out.toByteArray());
  67. }
  68. public int getType(){
  69. return Picture.EMF;
  70. }
  71. /**
  72. * EMF signature is {@code 0x3D40} or {@code 0x3D50}
  73. *
  74. * @return EMF signature ({@code 0x3D40} or {@code 0x3D50})
  75. */
  76. public int getSignature() {
  77. return (uidInstanceCount == 1 ? 0x3D40 : 0x3D50);
  78. }
  79. /**
  80. * Sets the EMF signature - either {@code 0x3D40} or {@code 0x3D50}
  81. */
  82. public void setSignature(int signature) {
  83. switch (signature) {
  84. case 0x3D40:
  85. uidInstanceCount = 1;
  86. break;
  87. case 0x3D50:
  88. uidInstanceCount = 2;
  89. break;
  90. default:
  91. throw new IllegalArgumentException(signature+" is not a valid instance/signature value for EMF");
  92. }
  93. }
  94. }