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.

HemfHeader.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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.hemf.record;
  16. import java.awt.Rectangle;
  17. import java.io.IOException;
  18. import org.apache.poi.util.IOUtils;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.poi.util.LittleEndian;
  21. import org.apache.poi.util.LittleEndianInputStream;
  22. /**
  23. * Extracts the full header from EMF files.
  24. * @see org.apache.poi.sl.image.ImageHeaderEMF
  25. */
  26. @Internal
  27. public class HemfHeader implements HemfRecord {
  28. private static final int MAX_RECORD_LENGTH = 1_000_000;
  29. private Rectangle boundsRectangle;
  30. private Rectangle frameRectangle;
  31. private long bytes;
  32. private long records;
  33. private int handles;
  34. private long nDescription;
  35. private long offDescription;
  36. private long nPalEntries;
  37. private boolean hasExtension1;
  38. private long cbPixelFormat;
  39. private long offPixelFormat;
  40. private long bOpenGL;
  41. private boolean hasExtension2;
  42. private long micrometersX;
  43. private long micrometersY;
  44. public Rectangle getBoundsRectangle() {
  45. return boundsRectangle;
  46. }
  47. public Rectangle getFrameRectangle() {
  48. return frameRectangle;
  49. }
  50. public long getBytes() {
  51. return bytes;
  52. }
  53. public long getRecords() {
  54. return records;
  55. }
  56. public int getHandles() {
  57. return handles;
  58. }
  59. public long getnDescription() {
  60. return nDescription;
  61. }
  62. public long getOffDescription() {
  63. return offDescription;
  64. }
  65. public long getnPalEntries() {
  66. return nPalEntries;
  67. }
  68. public boolean isHasExtension1() {
  69. return hasExtension1;
  70. }
  71. public long getCbPixelFormat() {
  72. return cbPixelFormat;
  73. }
  74. public long getOffPixelFormat() {
  75. return offPixelFormat;
  76. }
  77. public long getbOpenGL() {
  78. return bOpenGL;
  79. }
  80. public boolean isHasExtension2() {
  81. return hasExtension2;
  82. }
  83. public long getMicrometersX() {
  84. return micrometersX;
  85. }
  86. public long getMicrometersY() {
  87. return micrometersY;
  88. }
  89. @Override
  90. public String toString() {
  91. return "HemfHeader{" +
  92. "boundsRectangle=" + boundsRectangle +
  93. ", frameRectangle=" + frameRectangle +
  94. ", bytes=" + bytes +
  95. ", records=" + records +
  96. ", handles=" + handles +
  97. ", nDescription=" + nDescription +
  98. ", offDescription=" + offDescription +
  99. ", nPalEntries=" + nPalEntries +
  100. ", hasExtension1=" + hasExtension1 +
  101. ", cbPixelFormat=" + cbPixelFormat +
  102. ", offPixelFormat=" + offPixelFormat +
  103. ", bOpenGL=" + bOpenGL +
  104. ", hasExtension2=" + hasExtension2 +
  105. ", micrometersX=" + micrometersX +
  106. ", micrometersY=" + micrometersY +
  107. '}';
  108. }
  109. @Override
  110. public HemfRecordType getRecordType() {
  111. return HemfRecordType.header;
  112. }
  113. @Override
  114. public long init(LittleEndianInputStream leis, long recordId, long recordSize) throws IOException {
  115. if (recordId != 1L) {
  116. throw new IOException("Not a valid EMF header. Record type:"+recordId);
  117. }
  118. //read the record--id and size (2 bytes) have already been read
  119. byte[] data = IOUtils.safelyAllocate(recordSize, MAX_RECORD_LENGTH);
  120. IOUtils.readFully(leis, data);
  121. int offset = 0;
  122. //bounds
  123. int boundsLeft = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  124. int boundsTop = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  125. int boundsRight = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  126. int boundsBottom = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  127. boundsRectangle = new Rectangle(boundsLeft, boundsTop,
  128. boundsRight - boundsLeft, boundsBottom - boundsTop);
  129. int frameLeft = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  130. int frameTop = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  131. int frameRight = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  132. int frameBottom = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  133. frameRectangle = new Rectangle(frameLeft, frameTop,
  134. frameRight - frameLeft, frameBottom - frameTop);
  135. long recordSignature = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  136. if (recordSignature != 0x464D4520) {
  137. throw new IOException("bad record signature: " + recordSignature);
  138. }
  139. long version = LittleEndian.getInt(data, offset); offset += LittleEndian.INT_SIZE;
  140. //According to the spec, MSOffice doesn't pay attention to this value.
  141. //It _should_ be 0x00010000
  142. bytes = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  143. records = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  144. handles = LittleEndian.getUShort(data, offset);offset += LittleEndian.SHORT_SIZE;
  145. offset += LittleEndian.SHORT_SIZE;//reserved
  146. nDescription = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  147. offDescription = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  148. nPalEntries = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  149. //should be skips
  150. offset += 8;//device
  151. offset += 8;//millimeters
  152. if (recordSize+8 >= 100) {
  153. hasExtension1 = true;
  154. cbPixelFormat = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  155. offPixelFormat = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  156. bOpenGL= LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  157. }
  158. if (recordSize+8 >= 108) {
  159. hasExtension2 = true;
  160. micrometersX = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  161. micrometersY = LittleEndian.getUInt(data, offset); offset += LittleEndian.INT_SIZE;
  162. }
  163. return recordSize;
  164. }
  165. }