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.

ImageHeaderWMF.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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.sl.image;
  16. import java.awt.Dimension;
  17. import java.awt.Rectangle;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import org.apache.logging.log4j.LogManager;
  21. import org.apache.logging.log4j.Logger;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.LittleEndianConsts;
  25. import org.apache.poi.util.Units;
  26. /**
  27. * Aldus Placeable Metafile header - 22 byte structure before WMF data.
  28. * <ul>
  29. * <li>int Key; Magic number (always 9AC6CDD7h)
  30. * <li>short Handle; Metafile HANDLE number (always 0)
  31. * <li>short Left; Left coordinate in metafile units
  32. * <li>short Top; Top coordinate in metafile units
  33. * <li>short Right; Right coordinate in metafile units
  34. * <li>short Bottom; Bottom coordinate in metafile units
  35. * <li>short Inch; Number of metafile units per inch
  36. * <li>int Reserved; Reserved (always 0)
  37. * <li>short Checksum; Checksum value for previous 10 shorts
  38. * </ul>
  39. */
  40. @Internal
  41. public class ImageHeaderWMF {
  42. public static final int APMHEADER_KEY = 0x9AC6CDD7;
  43. private static final Logger LOG = LogManager.getLogger(ImageHeaderWMF.class);
  44. @SuppressWarnings("unused")
  45. private final int handle;
  46. private final int left, top, right, bottom;
  47. /**
  48. * The number of logical units per inch used to represent the image.
  49. * This value can be used to scale an image. By convention, an image is
  50. * considered to be recorded at 1440 logical units (twips) per inch.
  51. * Thus, a value of 720 specifies that the image SHOULD be rendered at
  52. * twice its normal size, and a value of 2880 specifies that the image
  53. * SHOULD be rendered at half its normal size.
  54. */
  55. private final int inch;
  56. @SuppressWarnings("unused")
  57. private final int reserved;
  58. private int checksum;
  59. public ImageHeaderWMF(Rectangle dim) {
  60. handle = 0;
  61. left = dim.x;
  62. top = dim.y;
  63. right = dim.x + dim.width;
  64. bottom = dim.y + dim.height;
  65. inch = Units.POINT_DPI; //default resolution is 72 dpi
  66. reserved = 0;
  67. }
  68. public ImageHeaderWMF(byte[] data, final int off) {
  69. int offset = off;
  70. int key = LittleEndian.getInt(data, offset); offset += LittleEndianConsts.INT_SIZE; //header key
  71. if (key != APMHEADER_KEY) {
  72. LOG.atWarn().log("WMF file doesn't contain a placeable header - ignore parsing");
  73. handle = 0;
  74. left = 0;
  75. top = 0;
  76. right = 200;
  77. bottom = 200;
  78. inch = Units.POINT_DPI; //default resolution is 72 dpi
  79. reserved = 0;
  80. return;
  81. }
  82. handle = LittleEndian.getUShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  83. left = LittleEndian.getShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  84. top = LittleEndian.getShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  85. right = LittleEndian.getShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  86. bottom = LittleEndian.getShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  87. inch = LittleEndian.getUShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  88. reserved = LittleEndian.getInt(data, offset); offset += LittleEndianConsts.INT_SIZE;
  89. checksum = LittleEndian.getShort(data, offset); offset += LittleEndianConsts.SHORT_SIZE;
  90. if (checksum != getChecksum()){
  91. LOG.atWarn().log("WMF checksum does not match the header data");
  92. }
  93. }
  94. /**
  95. * Returns a checksum value for the previous 10 shorts in the header.
  96. * The checksum is calculated by XORing each short value to an initial value of 0:
  97. */
  98. public int getChecksum(){
  99. int cs = 0;
  100. cs ^= (APMHEADER_KEY & 0x0000FFFF);
  101. cs ^= ((APMHEADER_KEY & 0xFFFF0000) >> 16);
  102. cs ^= left;
  103. cs ^= top;
  104. cs ^= right;
  105. cs ^= bottom;
  106. cs ^= inch;
  107. return cs;
  108. }
  109. public void write(OutputStream out) throws IOException {
  110. byte[] header = new byte[22];
  111. int pos = 0;
  112. LittleEndian.putInt(header, pos, APMHEADER_KEY); pos += LittleEndianConsts.INT_SIZE; //header key
  113. LittleEndian.putUShort(header, pos, 0); pos += LittleEndianConsts.SHORT_SIZE; //hmf
  114. LittleEndian.putUShort(header, pos, left); pos += LittleEndianConsts.SHORT_SIZE; //left
  115. LittleEndian.putUShort(header, pos, top); pos += LittleEndianConsts.SHORT_SIZE; //top
  116. LittleEndian.putUShort(header, pos, right); pos += LittleEndianConsts.SHORT_SIZE; //right
  117. LittleEndian.putUShort(header, pos, bottom); pos += LittleEndianConsts.SHORT_SIZE; //bottom
  118. LittleEndian.putUShort(header, pos, inch); pos += LittleEndianConsts.SHORT_SIZE; //inch
  119. LittleEndian.putInt(header, pos, 0); pos += LittleEndianConsts.INT_SIZE; //reserved
  120. checksum = getChecksum();
  121. LittleEndian.putUShort(header, pos, checksum);
  122. out.write(header);
  123. }
  124. public Dimension getSize() {
  125. //coefficient to translate from WMF dpi to 72dpi
  126. double coeff = ((double)Units.POINT_DPI)/inch;
  127. return new Dimension((int)Math.round((right-left)*coeff), (int)Math.round((bottom-top)*coeff));
  128. }
  129. public Rectangle getBounds() {
  130. return new Rectangle(left, top, right-left, bottom-top);
  131. }
  132. public int getLength(){
  133. return 22;
  134. }
  135. }