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.

Metafile.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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.ByteArrayOutputStream;
  19. import java.io.IOException;
  20. import java.io.OutputStream;
  21. import java.util.zip.DeflaterOutputStream;
  22. import org.apache.poi.hslf.usermodel.HSLFPictureData;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.Units;
  25. /**
  26. * Represents a metafile picture which can be one of the following types: EMF, WMF, or PICT.
  27. * A metafile is stored compressed using the ZIP deflate/inflate algorithm.
  28. *
  29. * @author Yegor Kozlov
  30. */
  31. public abstract class Metafile extends HSLFPictureData {
  32. /**
  33. * A structure which represents a 34-byte header preceding the compressed metafile data
  34. *
  35. * @author Yegor Kozlov
  36. */
  37. public static class Header{
  38. /**
  39. * size of the original file
  40. */
  41. public int wmfsize;
  42. /**
  43. * Boundary of the metafile drawing commands
  44. */
  45. public Rectangle bounds;
  46. /**
  47. * Size of the metafile in EMUs
  48. */
  49. public Dimension size;
  50. /**
  51. * size of the compressed metafile data
  52. */
  53. public int zipsize;
  54. /**
  55. * Reserved. Always 0.
  56. */
  57. public int compression = 0;
  58. /**
  59. * Reserved. Always 254.
  60. */
  61. public int filter = 254;
  62. public void read(byte[] data, int offset){
  63. int pos = offset;
  64. wmfsize = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  65. int left = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  66. int top = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  67. int right = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  68. int bottom = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  69. bounds = new Rectangle(left, top, right-left, bottom-top);
  70. int width = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  71. int height = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  72. size = new Dimension(width, height);
  73. zipsize = LittleEndian.getInt(data, pos); pos += LittleEndian.INT_SIZE;
  74. compression = LittleEndian.getUByte(data, pos); pos++;
  75. filter = LittleEndian.getUByte(data, pos); pos++;
  76. }
  77. public void write(OutputStream out) throws IOException {
  78. byte[] header = new byte[34];
  79. int pos = 0;
  80. LittleEndian.putInt(header, pos, wmfsize); pos += LittleEndian.INT_SIZE; //hmf
  81. LittleEndian.putInt(header, pos, bounds.x); pos += LittleEndian.INT_SIZE; //left
  82. LittleEndian.putInt(header, pos, bounds.y); pos += LittleEndian.INT_SIZE; //top
  83. LittleEndian.putInt(header, pos, bounds.x + bounds.width); pos += LittleEndian.INT_SIZE; //right
  84. LittleEndian.putInt(header, pos, bounds.y + bounds.height); pos += LittleEndian.INT_SIZE; //bottom
  85. LittleEndian.putInt(header, pos, size.width); pos += LittleEndian.INT_SIZE; //inch
  86. LittleEndian.putInt(header, pos, size.height); pos += LittleEndian.INT_SIZE; //inch
  87. LittleEndian.putInt(header, pos, zipsize); pos += LittleEndian.INT_SIZE; //inch
  88. header[pos] = 0; pos ++;
  89. header[pos] = (byte)filter; pos ++;
  90. out.write(header);
  91. }
  92. public int getSize(){
  93. return 34;
  94. }
  95. public int getWmfSize() {
  96. return wmfsize;
  97. }
  98. }
  99. protected static byte[] compress(byte[] bytes, int offset, int length) throws IOException {
  100. ByteArrayOutputStream out = new ByteArrayOutputStream();
  101. DeflaterOutputStream deflater = new DeflaterOutputStream( out );
  102. deflater.write(bytes, offset, length);
  103. deflater.close();
  104. return out.toByteArray();
  105. }
  106. @Override
  107. public Dimension getImageDimension() {
  108. int prefixLen = 16*uidInstanceCount;
  109. Header header = new Header();
  110. header.read(getRawData(), prefixLen);
  111. return new Dimension(
  112. (int)Math.round(Units.toPoints((long)header.size.getWidth())),
  113. (int)Math.round(Units.toPoints((long)header.size.getHeight()))
  114. );
  115. }
  116. }