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

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