PageRenderTime 62ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/ddf/EscherMetafileBlip.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 298 lines | 193 code | 38 blank | 67 comment | 20 complexity | 40ae8a8dc728bbce21c8fc56dec8bb5e MD5 | raw file
Possible License(s): Apache-2.0
  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.ddf;
  16. import org.apache.poi.util.HexDump;
  17. import org.apache.poi.util.LittleEndian;
  18. import org.apache.poi.util.POILogFactory;
  19. import org.apache.poi.util.POILogger;
  20. //import java.awt.Dimension;
  21. //import java.awt.Rectangle;
  22. import java.io.ByteArrayInputStream;
  23. import java.io.ByteArrayOutputStream;
  24. import java.io.IOException;
  25. import java.util.zip.InflaterInputStream;
  26. /**
  27. * @author Daniel Noll
  28. */
  29. public final class EscherMetafileBlip extends EscherBlipRecord {
  30. private static final POILogger log = POILogFactory.getLogger(EscherMetafileBlip.class);
  31. public static final short RECORD_ID_EMF = (short) 0xF018 + 2;
  32. public static final short RECORD_ID_WMF = (short) 0xF018 + 3;
  33. public static final short RECORD_ID_PICT = (short) 0xF018 + 4;
  34. /**
  35. * BLIP signatures as defined in the escher spec
  36. */
  37. public static final short SIGNATURE_EMF = 0x3D40;
  38. public static final short SIGNATURE_WMF = 0x2160;
  39. public static final short SIGNATURE_PICT = 0x5420;
  40. private static final int HEADER_SIZE = 8;
  41. private byte[] field_1_UID;
  42. /**
  43. * The primary UID is only saved to disk if (blip_instance ^ blip_signature == 1)
  44. */
  45. private byte[] field_2_UID;
  46. private int field_2_cb;
  47. private int field_3_rcBounds_x1;
  48. private int field_3_rcBounds_y1;
  49. private int field_3_rcBounds_x2;
  50. private int field_3_rcBounds_y2;
  51. private int field_4_ptSize_w;
  52. private int field_4_ptSize_h;
  53. private int field_5_cbSave;
  54. private byte field_6_fCompression;
  55. private byte field_7_fFilter;
  56. private byte[] raw_pictureData;
  57. private byte[] remainingData;
  58. public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
  59. int bytesAfterHeader = readHeader( data, offset );
  60. int pos = offset + HEADER_SIZE;
  61. field_1_UID = new byte[16];
  62. System.arraycopy( data, pos, field_1_UID, 0, 16 ); pos += 16;
  63. if((getOptions() ^ getSignature()) == 0x10){
  64. field_2_UID = new byte[16];
  65. System.arraycopy( data, pos, field_2_UID, 0, 16 ); pos += 16;
  66. }
  67. field_2_cb = LittleEndian.getInt( data, pos ); pos += 4;
  68. field_3_rcBounds_x1 = LittleEndian.getInt( data, pos ); pos += 4;
  69. field_3_rcBounds_y1 = LittleEndian.getInt( data, pos ); pos += 4;
  70. field_3_rcBounds_x2 = LittleEndian.getInt( data, pos ); pos += 4;
  71. field_3_rcBounds_y2 = LittleEndian.getInt( data, pos ); pos += 4;
  72. field_4_ptSize_w = LittleEndian.getInt( data, pos ); pos += 4;
  73. field_4_ptSize_h = LittleEndian.getInt( data, pos ); pos += 4;
  74. field_5_cbSave = LittleEndian.getInt( data, pos ); pos += 4;
  75. field_6_fCompression = data[pos]; pos++;
  76. field_7_fFilter = data[pos]; pos++;
  77. raw_pictureData = new byte[field_5_cbSave];
  78. System.arraycopy( data, pos, raw_pictureData, 0, field_5_cbSave );
  79. pos += field_5_cbSave;
  80. // 0 means DEFLATE compression
  81. // 0xFE means no compression
  82. if (field_6_fCompression == 0) {
  83. field_pictureData = inflatePictureData(raw_pictureData);
  84. } else {
  85. field_pictureData = raw_pictureData;
  86. }
  87. int remaining = bytesAfterHeader - pos + offset + HEADER_SIZE;
  88. if(remaining > 0) {
  89. remainingData = new byte[remaining];
  90. System.arraycopy( data, pos, remainingData, 0, remaining );
  91. }
  92. return bytesAfterHeader + HEADER_SIZE;
  93. }
  94. public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
  95. listener.beforeRecordSerialize(offset, getRecordId(), this);
  96. int pos = offset;
  97. LittleEndian.putShort( data, pos, getOptions() ); pos += 2;
  98. LittleEndian.putShort( data, pos, getRecordId() ); pos += 2;
  99. LittleEndian.putInt( data, pos, getRecordSize() - HEADER_SIZE ); pos += 4;
  100. System.arraycopy( field_1_UID, 0, data, pos, field_1_UID.length ); pos += field_1_UID.length;
  101. if ((getOptions() ^ getSignature()) == 0x10) {
  102. System.arraycopy( field_2_UID, 0, data, pos, field_2_UID.length ); pos += field_2_UID.length;
  103. }
  104. LittleEndian.putInt( data, pos, field_2_cb ); pos += 4;
  105. LittleEndian.putInt( data, pos, field_3_rcBounds_x1 ); pos += 4;
  106. LittleEndian.putInt( data, pos, field_3_rcBounds_y1 ); pos += 4;
  107. LittleEndian.putInt( data, pos, field_3_rcBounds_x2 ); pos += 4;
  108. LittleEndian.putInt( data, pos, field_3_rcBounds_y2 ); pos += 4;
  109. LittleEndian.putInt( data, pos, field_4_ptSize_w ); pos += 4;
  110. LittleEndian.putInt( data, pos, field_4_ptSize_h ); pos += 4;
  111. LittleEndian.putInt( data, pos, field_5_cbSave ); pos += 4;
  112. data[pos] = field_6_fCompression; pos++;
  113. data[pos] = field_7_fFilter; pos++;
  114. System.arraycopy( raw_pictureData, 0, data, pos, raw_pictureData.length ); pos += raw_pictureData.length;
  115. if(remainingData != null) {
  116. System.arraycopy( remainingData, 0, data, pos, remainingData.length ); pos += remainingData.length;
  117. }
  118. listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
  119. return getRecordSize();
  120. }
  121. /**
  122. * Decompresses the provided data, returning the inflated result.
  123. *
  124. * @param data the deflated picture data.
  125. * @return the inflated picture data.
  126. */
  127. private static byte[] inflatePictureData(byte[] data) {
  128. try {
  129. InflaterInputStream in = new InflaterInputStream(
  130. new ByteArrayInputStream( data ) );
  131. ByteArrayOutputStream out = new ByteArrayOutputStream();
  132. byte[] buf = new byte[4096];
  133. int readBytes;
  134. while ((readBytes = in.read(buf)) > 0) {
  135. out.write(buf, 0, readBytes);
  136. }
  137. return out.toByteArray();
  138. } catch (IOException e) {
  139. log.log(POILogger.WARN, "Possibly corrupt compression or non-compressed data", e);
  140. return data;
  141. }
  142. }
  143. public int getRecordSize() {
  144. int size = 8 + 50 + raw_pictureData.length;
  145. if(remainingData != null) size += remainingData.length;
  146. if((getOptions() ^ getSignature()) == 0x10){
  147. size += field_2_UID.length;
  148. }
  149. return size;
  150. }
  151. public byte[] getUID() {
  152. return field_1_UID;
  153. }
  154. public void setUID(byte[] uid) {
  155. field_1_UID = uid;
  156. }
  157. public byte[] getPrimaryUID()
  158. {
  159. return field_2_UID;
  160. }
  161. public void setPrimaryUID(byte[] primaryUID) {
  162. field_2_UID = primaryUID;
  163. }
  164. public int getUncompressedSize() {
  165. return field_2_cb;
  166. }
  167. public void setUncompressedSize(int uncompressedSize) {
  168. field_2_cb = uncompressedSize;
  169. }
  170. /*public Rectangle getBounds() {
  171. return new Rectangle(field_3_rcBounds_x1,
  172. field_3_rcBounds_y1,
  173. field_3_rcBounds_x2 - field_3_rcBounds_x1,
  174. field_3_rcBounds_y2 - field_3_rcBounds_y1);
  175. }
  176. public void setBounds(Rectangle bounds) {
  177. field_3_rcBounds_x1 = bounds.x;
  178. field_3_rcBounds_y1 = bounds.y;
  179. field_3_rcBounds_x2 = bounds.x + bounds.width;
  180. field_3_rcBounds_y2 = bounds.y + bounds.height;
  181. }
  182. public Dimension getSizeEMU() {
  183. return new Dimension(field_4_ptSize_w, field_4_ptSize_h);
  184. }
  185. public void setSizeEMU(Dimension sizeEMU) {
  186. field_4_ptSize_w = sizeEMU.width;
  187. field_4_ptSize_h = sizeEMU.height;
  188. }*/
  189. public int getCompressedSize() {
  190. return field_5_cbSave;
  191. }
  192. public void setCompressedSize(int compressedSize) {
  193. field_5_cbSave = compressedSize;
  194. }
  195. public boolean isCompressed() {
  196. return (field_6_fCompression == 0);
  197. }
  198. public void setCompressed(boolean compressed) {
  199. field_6_fCompression = compressed ? 0 : (byte)0xFE;
  200. }
  201. public byte[] getRemainingData() {
  202. return remainingData;
  203. }
  204. // filtering is always 254 according to available docs, so no point giving it a setter method.
  205. public String toString() {
  206. String extraData = "";//HexDump.toHex(field_pictureData, 32);
  207. return getClass().getName() + ":" + '\n' +
  208. " RecordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' +
  209. " Version: 0x" + HexDump.toHex( getVersion() ) + '\n' +
  210. " Instance: 0x" + HexDump.toHex( getInstance() ) + '\n' +
  211. " UID: 0x" + HexDump.toHex( field_1_UID ) + '\n' +
  212. (field_2_UID == null ? "" : (" UID2: 0x" + HexDump.toHex( field_2_UID ) + '\n')) +
  213. " Uncompressed Size: " + HexDump.toHex( field_2_cb ) + '\n' +
  214. // " Bounds: " + getBounds() + '\n' +
  215. // " Size in EMU: " + getSizeEMU() + '\n' +
  216. " Compressed Size: " + HexDump.toHex( field_5_cbSave ) + '\n' +
  217. " Compression: " + HexDump.toHex( field_6_fCompression ) + '\n' +
  218. " Filter: " + HexDump.toHex( field_7_fFilter ) + '\n' +
  219. " Extra Data:" + '\n' + extraData +
  220. (remainingData == null ? null : ("\n" +
  221. " Remaining Data: " + HexDump.toHex(remainingData, 32)));
  222. }
  223. @Override
  224. public String toXml(String tab) {
  225. String extraData = "";
  226. StringBuilder builder = new StringBuilder();
  227. builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())))
  228. .append(tab).append("\t").append("<UID>0x").append(HexDump.toHex( field_1_UID ) + '\n' +
  229. (field_2_UID == null ? "" : (" UID2: 0x" + HexDump.toHex( field_2_UID ) + '\n'))).append("</UID>\n")
  230. .append(tab).append("\t").append("<UncompressedSize>0x").append(HexDump.toHex( field_2_cb )).append("</UncompressedSize>\n")
  231. // .append(tab).append("\t").append("<Bounds>").append(getBounds()).append("</Bounds>\n")
  232. // .append(tab).append("\t").append("<SizeInEMU>").append(getSizeEMU()).append("</SizeInEMU>\n")
  233. .append(tab).append("\t").append("<CompressedSize>0x").append(HexDump.toHex( field_5_cbSave )).append("</CompressedSize>\n")
  234. .append(tab).append("\t").append("<Compression>0x").append(HexDump.toHex( field_6_fCompression )).append("</Compression>\n")
  235. .append(tab).append("\t").append("<Filter>0x").append(HexDump.toHex( field_7_fFilter )).append("</Filter>\n")
  236. .append(tab).append("\t").append("<ExtraData>").append(extraData).append("</ExtraData>\n")
  237. .append(tab).append("\t").append("<RemainingData>0x").append(HexDump.toHex(remainingData, 32)).append("</RemainingData>\n");
  238. builder.append(tab).append("</").append(getClass().getSimpleName()).append(">\n");
  239. return builder.toString();
  240. }
  241. /**
  242. * Return the blip signature
  243. *
  244. * @return the blip signature
  245. */
  246. public short getSignature() {
  247. switch (getRecordId()) {
  248. case RECORD_ID_EMF: return SIGNATURE_EMF;
  249. case RECORD_ID_WMF: return SIGNATURE_WMF;
  250. case RECORD_ID_PICT: return SIGNATURE_PICT;
  251. }
  252. log.log(POILogger.WARN, "Unknown metafile: " + getRecordId());
  253. return 0;
  254. }
  255. }