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.

Picture.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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.hwpf.usermodel;
  16. import java.io.ByteArrayInputStream;
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20. import java.util.zip.InflaterInputStream;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.util.POILogFactory;
  23. import org.apache.poi.util.POILogger;
  24. /**
  25. * Represents embedded picture extracted from Word Document
  26. * @author Dmitry Romanov
  27. */
  28. public final class Picture
  29. {
  30. private static final POILogger log = POILogFactory.getLogger(Picture.class);
  31. // public static final int FILENAME_OFFSET = 0x7C;
  32. // public static final int FILENAME_SIZE_OFFSET = 0x6C;
  33. static final int PICF_OFFSET = 0x0;
  34. static final int PICT_HEADER_OFFSET = 0x4;
  35. static final int MFPMM_OFFSET = 0x6;
  36. static final int PICF_SHAPE_OFFSET = 0xE;
  37. static final int DXAGOAL_OFFSET = 0x1C;
  38. static final int DYAGOAL_OFFSET = 0x1E;
  39. static final int MX_OFFSET = 0x20;
  40. static final int MY_OFFSET = 0x22;
  41. static final int DXACROPLEFT_OFFSET = 0x24;
  42. static final int DYACROPTOP_OFFSET = 0x26;
  43. static final int DXACROPRIGHT_OFFSET = 0x28;
  44. static final int DYACROPBOTTOM_OFFSET = 0x2A;
  45. static final int UNKNOWN_HEADER_SIZE = 0x49;
  46. public static final byte[] GIF = new byte[]{'G', 'I', 'F'};
  47. public static final byte[] PNG = new byte[]{ (byte)0x89, 0x50, 0x4E, 0x47,0x0D,0x0A,0x1A,0x0A};
  48. public static final byte[] JPG = new byte[]{(byte)0xFF, (byte)0xD8};
  49. public static final byte[] BMP = new byte[]{'B', 'M'};
  50. public static final byte[] TIFF = new byte[]{0x49, 0x49, 0x2A, 0x00};
  51. public static final byte[] TIFF1 = new byte[]{0x4D, 0x4D, 0x00, 0x2A};
  52. public static final byte[] EMF = { 0x01, 0x00, 0x00, 0x00 };
  53. public static final byte[] WMF1 = { (byte)0xD7, (byte)0xCD, (byte)0xC6, (byte)0x9A, 0x00, 0x00 };
  54. public static final byte[] WMF2 = { 0x01, 0x00, 0x09, 0x00, 0x00, 0x03 }; // Windows 3.x
  55. // TODO: DIB, PICT
  56. public static final byte[] IHDR = new byte[]{'I', 'H', 'D', 'R'};
  57. public static final byte[] COMPRESSED1 = { (byte)0xFE, 0x78, (byte)0xDA };
  58. public static final byte[] COMPRESSED2 = { (byte)0xFE, 0x78, (byte)0x9C };
  59. private int dataBlockStartOfsset;
  60. private int pictureBytesStartOffset;
  61. private int dataBlockSize;
  62. private int size;
  63. // private String fileName;
  64. private byte[] rawContent;
  65. private byte[] content;
  66. private byte[] _dataStream;
  67. private int aspectRatioX;
  68. private int aspectRatioY;
  69. private int height = -1;
  70. private int width = -1;
  71. private int dxaGoal = -1;
  72. private int dyaGoal = -1;
  73. private int dxaCropLeft = -1;
  74. private int dyaCropTop = -1;
  75. private int dxaCropRight = -1;
  76. private int dyaCropBottom = -1;
  77. public Picture(int dataBlockStartOfsset, byte[] _dataStream, boolean fillBytes)
  78. {
  79. this._dataStream = _dataStream;
  80. this.dataBlockStartOfsset = dataBlockStartOfsset;
  81. this.dataBlockSize = LittleEndian.getInt(_dataStream, dataBlockStartOfsset);
  82. this.pictureBytesStartOffset = getPictureBytesStartOffset(dataBlockStartOfsset, _dataStream, dataBlockSize);
  83. this.size = dataBlockSize - (pictureBytesStartOffset - dataBlockStartOfsset);
  84. if (size<0) {
  85. }
  86. this.dxaGoal = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DXAGOAL_OFFSET);
  87. this.dyaGoal = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DYAGOAL_OFFSET);
  88. this.aspectRatioX = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+MX_OFFSET)/10;
  89. this.aspectRatioY = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+MY_OFFSET)/10;
  90. this.dxaCropLeft = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DXACROPLEFT_OFFSET);
  91. this.dyaCropTop = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DYACROPTOP_OFFSET);
  92. this.dxaCropRight = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DXACROPRIGHT_OFFSET);
  93. this.dyaCropBottom = LittleEndian.getShort(_dataStream, dataBlockStartOfsset+DYACROPBOTTOM_OFFSET);
  94. if (fillBytes)
  95. {
  96. fillImageContent();
  97. }
  98. }
  99. public Picture(byte[] _dataStream)
  100. {
  101. this._dataStream = _dataStream;
  102. this.dataBlockStartOfsset = 0;
  103. this.dataBlockSize = _dataStream.length;
  104. this.pictureBytesStartOffset = 0;
  105. this.size = _dataStream.length;
  106. }
  107. private void fillWidthHeight()
  108. {
  109. String ext = suggestFileExtension();
  110. // trying to extract width and height from pictures content:
  111. if ("jpg".equalsIgnoreCase(ext)) {
  112. fillJPGWidthHeight();
  113. } else if ("png".equalsIgnoreCase(ext)) {
  114. fillPNGWidthHeight();
  115. }
  116. }
  117. /**
  118. * Tries to suggest a filename: hex representation of picture structure offset in "Data" stream plus extension that
  119. * is tried to determine from first byte of picture's content.
  120. *
  121. * @return suggested file name
  122. */
  123. public String suggestFullFileName()
  124. {
  125. String fileExt = suggestFileExtension();
  126. return Integer.toHexString(dataBlockStartOfsset) + (fileExt.length()>0 ? "."+fileExt : "");
  127. }
  128. /**
  129. * Writes Picture's content bytes to specified OutputStream.
  130. * Is useful when there is need to write picture bytes directly to stream, omitting its representation in
  131. * memory as distinct byte array.
  132. *
  133. * @param out a stream to write to
  134. * @throws IOException if some exception is occured while writing to specified out
  135. */
  136. public void writeImageContent(OutputStream out) throws IOException
  137. {
  138. if (rawContent!=null && rawContent.length>0) {
  139. out.write(rawContent, 0, size);
  140. } else {
  141. out.write(_dataStream, pictureBytesStartOffset, size);
  142. }
  143. }
  144. /**
  145. * @return The offset of this picture in the picture bytes, used
  146. * when matching up with {@link CharacterRun#getPicOffset()}
  147. */
  148. public int getStartOffset() {
  149. return dataBlockStartOfsset;
  150. }
  151. /**
  152. * @return picture's content as byte array
  153. */
  154. public byte[] getContent()
  155. {
  156. if (content == null || content.length<=0)
  157. {
  158. fillImageContent();
  159. }
  160. return content;
  161. }
  162. public byte[] getRawContent()
  163. {
  164. if (rawContent == null || rawContent.length <= 0)
  165. {
  166. fillRawImageContent();
  167. }
  168. return rawContent;
  169. }
  170. /**
  171. *
  172. * @return size in bytes of the picture
  173. */
  174. public int getSize()
  175. {
  176. return size;
  177. }
  178. /**
  179. * @return the horizontal aspect ratio for picture provided by user
  180. */
  181. public int getAspectRatioX() {
  182. return aspectRatioX;
  183. }
  184. /**
  185. * @retrn the vertical aspect ratio for picture provided by user
  186. */
  187. public int getAspectRatioY() {
  188. return aspectRatioY;
  189. }
  190. /**
  191. * Gets the initial width of the picture, in twips, prior to cropping or scaling.
  192. *
  193. * @return the initial width of the picture in twips
  194. */
  195. public int getDxaGoal() {
  196. return dxaGoal;
  197. }
  198. /**
  199. * Gets the initial height of the picture, in twips, prior to cropping or scaling.
  200. *
  201. * @return the initial width of the picture in twips
  202. */
  203. public int getDyaGoal() {
  204. return dyaGoal;
  205. }
  206. /**
  207. * @return The amount the picture has been cropped on the left in twips
  208. */
  209. public int getDxaCropLeft() {
  210. return dxaCropLeft;
  211. }
  212. /**
  213. * @return The amount the picture has been cropped on the top in twips
  214. */
  215. public int getDyaCropTop() {
  216. return dyaCropTop;
  217. }
  218. /**
  219. * @return The amount the picture has been cropped on the right in twips
  220. */
  221. public int getDxaCropRight() {
  222. return dxaCropRight;
  223. }
  224. /**
  225. * @return The amount the picture has been cropped on the bottom in twips
  226. */
  227. public int getDyaCropBottom() {
  228. return dyaCropBottom;
  229. }
  230. /**
  231. * tries to suggest extension for picture's file by matching signatures of popular image formats to first bytes
  232. * of picture's contents
  233. * @return suggested file extension
  234. */
  235. public String suggestFileExtension()
  236. {
  237. String extension = suggestFileExtension(_dataStream, pictureBytesStartOffset);
  238. if ("".equals(extension)) {
  239. // May be compressed. Get the uncompressed content and inspect that.
  240. extension = suggestFileExtension(getContent(), 0);
  241. }
  242. return extension;
  243. }
  244. /**
  245. * Returns the mime type for the image
  246. */
  247. public String getMimeType() {
  248. String extension = suggestFileExtension();
  249. if("jpg".equals(extension)) {
  250. return "image/jpeg";
  251. }
  252. if("png".equals(extension)) {
  253. return "image/png";
  254. }
  255. if("gif".equals(extension)) {
  256. return "image/gif";
  257. }
  258. if("bmp".equals(extension)) {
  259. return "image/bmp";
  260. }
  261. if("tiff".equals(extension)) {
  262. return "image/tiff";
  263. }
  264. if("wmf".equals(extension)) {
  265. return "image/x-wmf";
  266. }
  267. if("emf".equals(extension)) {
  268. return "image/x-emf";
  269. }
  270. return "image/unknown";
  271. }
  272. private String suggestFileExtension(byte[] _dataStream, int pictureBytesStartOffset)
  273. {
  274. if (matchSignature(_dataStream, JPG, pictureBytesStartOffset)) {
  275. return "jpg";
  276. } else if (matchSignature(_dataStream, PNG, pictureBytesStartOffset)) {
  277. return "png";
  278. } else if (matchSignature(_dataStream, GIF, pictureBytesStartOffset)) {
  279. return "gif";
  280. } else if (matchSignature(_dataStream, BMP, pictureBytesStartOffset)) {
  281. return "bmp";
  282. } else if (matchSignature(_dataStream, TIFF, pictureBytesStartOffset) ||
  283. matchSignature(_dataStream, TIFF1, pictureBytesStartOffset)) {
  284. return "tiff";
  285. } else {
  286. // Need to load the image content before we can try the following tests
  287. fillImageContent();
  288. if (matchSignature(content, WMF1, 0) || matchSignature(content, WMF2, 0)) {
  289. return "wmf";
  290. } else if (matchSignature(content, EMF, 0)) {
  291. return "emf";
  292. }
  293. }
  294. // TODO: DIB, PICT
  295. return "";
  296. }
  297. private static boolean matchSignature(byte[] dataStream, byte[] signature, int pictureBytesOffset)
  298. {
  299. boolean matched = pictureBytesOffset < dataStream.length;
  300. for (int i = 0; (i+pictureBytesOffset) < dataStream.length && i < signature.length; i++)
  301. {
  302. if (dataStream[i+pictureBytesOffset] != signature[i])
  303. {
  304. matched = false;
  305. break;
  306. }
  307. }
  308. return matched;
  309. }
  310. // public String getFileName()
  311. // {
  312. // return fileName;
  313. // }
  314. // private static String extractFileName(int blockStartIndex, byte[] dataStream) {
  315. // int fileNameStartOffset = blockStartIndex + 0x7C;
  316. // int fileNameSizeOffset = blockStartIndex + FILENAME_SIZE_OFFSET;
  317. // int fileNameSize = LittleEndian.getShort(dataStream, fileNameSizeOffset);
  318. //
  319. // int fileNameIndex = fileNameStartOffset;
  320. // char[] fileNameChars = new char[(fileNameSize-1)/2];
  321. // int charIndex = 0;
  322. // while(charIndex<fileNameChars.length) {
  323. // short aChar = LittleEndian.getShort(dataStream, fileNameIndex);
  324. // fileNameChars[charIndex] = (char)aChar;
  325. // charIndex++;
  326. // fileNameIndex += 2;
  327. // }
  328. // String fileName = new String(fileNameChars);
  329. // return fileName.trim();
  330. // }
  331. private void fillRawImageContent()
  332. {
  333. this.rawContent = new byte[size];
  334. System.arraycopy(_dataStream, pictureBytesStartOffset, rawContent, 0, size);
  335. }
  336. private void fillImageContent()
  337. {
  338. byte[] rawContent = getRawContent();
  339. // HACK: Detect compressed images. In reality there should be some way to determine
  340. // this from the first 32 bytes, but I can't see any similarity between all the
  341. // samples I have obtained, nor any similarity in the data block contents.
  342. if (matchSignature(rawContent, COMPRESSED1, 32) || matchSignature(rawContent, COMPRESSED2, 32))
  343. {
  344. try
  345. {
  346. InflaterInputStream in = new InflaterInputStream(
  347. new ByteArrayInputStream(rawContent, 33, rawContent.length - 33));
  348. ByteArrayOutputStream out = new ByteArrayOutputStream();
  349. byte[] buf = new byte[4096];
  350. int readBytes;
  351. while ((readBytes = in.read(buf)) > 0)
  352. {
  353. out.write(buf, 0, readBytes);
  354. }
  355. content = out.toByteArray();
  356. }
  357. catch (IOException e)
  358. {
  359. // Problems reading from the actual ByteArrayInputStream should never happen
  360. // so this will only ever be a ZipException.
  361. log.log(POILogger.INFO, "Possibly corrupt compression or non-compressed data", e);
  362. }
  363. } else {
  364. // Raw data is not compressed.
  365. content = rawContent;
  366. }
  367. }
  368. private static int getPictureBytesStartOffset(int dataBlockStartOffset, byte[] _dataStream, int dataBlockSize)
  369. {
  370. int realPicoffset = dataBlockStartOffset;
  371. final int dataBlockEndOffset = dataBlockSize + dataBlockStartOffset;
  372. // Skip over the PICT block
  373. int PICTFBlockSize = LittleEndian.getShort(_dataStream, dataBlockStartOffset +PICT_HEADER_OFFSET); // Should be 68 bytes
  374. // Now the PICTF1
  375. int PICTF1BlockOffset = PICTFBlockSize + PICT_HEADER_OFFSET;
  376. short MM_TYPE = LittleEndian.getShort(_dataStream, dataBlockStartOffset + PICT_HEADER_OFFSET + 2);
  377. if(MM_TYPE == 0x66) {
  378. // Skip the stPicName
  379. int cchPicName = LittleEndian.getUnsignedByte(_dataStream, PICTF1BlockOffset);
  380. PICTF1BlockOffset += 1 + cchPicName;
  381. }
  382. int PICTF1BlockSize = LittleEndian.getShort(_dataStream, dataBlockStartOffset +PICTF1BlockOffset);
  383. int unknownHeaderOffset = (PICTF1BlockSize + PICTF1BlockOffset) < dataBlockEndOffset ? (PICTF1BlockSize + PICTF1BlockOffset) : PICTF1BlockOffset;
  384. realPicoffset += (unknownHeaderOffset + UNKNOWN_HEADER_SIZE);
  385. if (realPicoffset>=dataBlockEndOffset) {
  386. realPicoffset -= UNKNOWN_HEADER_SIZE;
  387. }
  388. return realPicoffset;
  389. }
  390. private void fillJPGWidthHeight() {
  391. /*
  392. http://www.codecomments.com/archive281-2004-3-158083.html
  393. Algorhitm proposed by Patrick TJ McPhee:
  394. read 2 bytes
  395. make sure they are 'ffd8'x
  396. repeatedly:
  397. read 2 bytes
  398. make sure the first one is 'ff'x
  399. if the second one is 'd9'x stop
  400. else if the second one is c0 or c2 (or possibly other values ...)
  401. skip 2 bytes
  402. read one byte into depth
  403. read two bytes into height
  404. read two bytes into width
  405. else
  406. read two bytes into length
  407. skip forward length-2 bytes
  408. Also used Ruby code snippet from: http://www.bigbold.com/snippets/posts/show/805 for reference
  409. */
  410. int pointer = pictureBytesStartOffset+2;
  411. int firstByte = _dataStream[pointer];
  412. int secondByte = _dataStream[pointer+1];
  413. int endOfPicture = pictureBytesStartOffset + size;
  414. while(pointer<endOfPicture-1) {
  415. do {
  416. firstByte = _dataStream[pointer];
  417. secondByte = _dataStream[pointer+1];
  418. pointer += 2;
  419. } while (!(firstByte==(byte)0xFF) && pointer<endOfPicture-1);
  420. if (firstByte==((byte)0xFF) && pointer<endOfPicture-1) {
  421. if (secondByte==(byte)0xD9 || secondByte==(byte)0xDA) {
  422. break;
  423. } else if ( (secondByte & 0xF0) == 0xC0 && secondByte!=(byte)0xC4 && secondByte!=(byte)0xC8 && secondByte!=(byte)0xCC) {
  424. pointer += 5;
  425. this.height = getBigEndianShort(_dataStream, pointer);
  426. this.width = getBigEndianShort(_dataStream, pointer+2);
  427. break;
  428. } else {
  429. pointer++;
  430. pointer++;
  431. int length = getBigEndianShort(_dataStream, pointer);
  432. pointer+=length;
  433. }
  434. } else {
  435. pointer++;
  436. }
  437. }
  438. }
  439. private void fillPNGWidthHeight()
  440. {
  441. /*
  442. Used PNG file format description from http://www.wotsit.org/download.asp?f=png
  443. */
  444. int HEADER_START = pictureBytesStartOffset + PNG.length + 4;
  445. if (matchSignature(_dataStream, IHDR, HEADER_START)) {
  446. int IHDR_CHUNK_WIDTH = HEADER_START + 4;
  447. this.width = getBigEndianInt(_dataStream, IHDR_CHUNK_WIDTH);
  448. this.height = getBigEndianInt(_dataStream, IHDR_CHUNK_WIDTH + 4);
  449. }
  450. }
  451. /**
  452. * returns pixel width of the picture or -1 if dimensions determining was failed
  453. */
  454. public int getWidth()
  455. {
  456. if (width == -1)
  457. {
  458. fillWidthHeight();
  459. }
  460. return width;
  461. }
  462. /**
  463. * returns pixel height of the picture or -1 if dimensions determining was failed
  464. */
  465. public int getHeight()
  466. {
  467. if (height == -1)
  468. {
  469. fillWidthHeight();
  470. }
  471. return height;
  472. }
  473. private static int getBigEndianInt(byte[] data, int offset)
  474. {
  475. return (((data[offset] & 0xFF)<< 24) + ((data[offset +1] & 0xFF) << 16) + ((data[offset + 2] & 0xFF) << 8) + (data[offset +3] & 0xFF));
  476. }
  477. private static int getBigEndianShort(byte[] data, int offset)
  478. {
  479. return (((data[offset] & 0xFF)<< 8) + (data[offset +1] & 0xFF));
  480. }
  481. }