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.

Thumbnail.java 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.hpsf;
  16. import org.apache.poi.util.LittleEndian;
  17. /**
  18. * <p>Class to manipulate data in the Clipboard Variant ({@link
  19. * Variant#VT_CF VT_CF}) format.</p>
  20. *
  21. * @author Drew Varner (Drew.Varner inOrAround sc.edu)
  22. * @see SummaryInformation#getThumbnail()
  23. */
  24. public final class Thumbnail {
  25. /**
  26. * <p>Offset in bytes where the Clipboard Format Tag starts in the
  27. * <code>byte[]</code> returned by {@link
  28. * SummaryInformation#getThumbnail()}</p>
  29. */
  30. public static final int OFFSET_CFTAG = 4;
  31. /**
  32. * <p>Offset in bytes where the Clipboard Format starts in the
  33. * <code>byte[]</code> returned by {@link
  34. * SummaryInformation#getThumbnail()}</p>
  35. *
  36. * <p>This is only valid if the Clipboard Format Tag is {@link
  37. * #CFTAG_WINDOWS}</p>
  38. */
  39. public static final int OFFSET_CF = 8;
  40. /**
  41. * <p>Offset in bytes where the Windows Metafile (WMF) image data
  42. * starts in the <code>byte[]</code> returned by {@link
  43. * SummaryInformation#getThumbnail()}</p>
  44. *
  45. * <p>There is only WMF data at this point in the
  46. * <code>byte[]</code> if the Clipboard Format Tag is {@link
  47. * #CFTAG_WINDOWS} and the Clipboard Format is {@link
  48. * #CF_METAFILEPICT}.</p>
  49. *
  50. * <p>Note: The <code>byte[]</code> that starts at
  51. * <code>OFFSET_WMFDATA</code> and ends at
  52. * <code>getThumbnail().length - 1</code> forms a complete WMF
  53. * image. It can be saved to disk with a <code>.wmf</code> file
  54. * type and read using a WMF-capable image viewer.</p>
  55. */
  56. public static final int OFFSET_WMFDATA = 20;
  57. /**
  58. * <p>Clipboard Format Tag - Windows clipboard format</p>
  59. *
  60. * <p>A <code>DWORD</code> indicating a built-in Windows clipboard
  61. * format value</p>
  62. */
  63. public static final int CFTAG_WINDOWS = -1;
  64. /**
  65. * <p>Clipboard Format Tag - Macintosh clipboard format</p>
  66. *
  67. * <p>A <code>DWORD</code> indicating a Macintosh clipboard format
  68. * value</p>
  69. */
  70. public static final int CFTAG_MACINTOSH = -2;
  71. /**
  72. * <p>Clipboard Format Tag - Format ID</p>
  73. *
  74. * <p>A GUID containing a format identifier (FMTID). This is
  75. * rarely used.</p>
  76. */
  77. public static final int CFTAG_FMTID = -3;
  78. /**
  79. * <p>Clipboard Format Tag - No Data</p>
  80. *
  81. * <p>A <code>DWORD</code> indicating No data. This is rarely
  82. * used.</p>
  83. */
  84. public static final int CFTAG_NODATA = 0;
  85. /**
  86. * <p>Clipboard Format - Windows metafile format. This is the
  87. * recommended way to store thumbnails in Property Streams.</p>
  88. *
  89. * <p><strong>Note:</strong> This is not the same format used in
  90. * regular WMF images. The clipboard version of this format has an
  91. * extra clipboard-specific header.</p>
  92. */
  93. public static final int CF_METAFILEPICT = 3;
  94. /**
  95. * <p>Clipboard Format - Device Independent Bitmap</p>
  96. */
  97. public static final int CF_DIB = 8;
  98. /**
  99. * <p>Clipboard Format - Enhanced Windows metafile format</p>
  100. */
  101. public static final int CF_ENHMETAFILE = 14;
  102. /**
  103. * <p>Clipboard Format - Bitmap</p>
  104. *
  105. * <p>Obsolete, see <a
  106. * href="msdn.microsoft.com/library/en-us/dnw98bk/html/clipboardoperations.asp
  107. * target="_blank">msdn.microsoft.com/library/en-us/dnw98bk/html/clipboardoperations.asp</a>.</p>
  108. */
  109. public static final int CF_BITMAP = 2;
  110. /**
  111. * <p>A <code>byte[]</code> to hold a thumbnail image in ({@link
  112. * Variant#VT_CF VT_CF}) format.</p>
  113. */
  114. private byte[] _thumbnailData = null;
  115. /**
  116. * <p>Default Constructor. If you use it then one you'll have to add
  117. * the thumbnail <code>byte[]</code> from {@link
  118. * SummaryInformation#getThumbnail()} to do any useful
  119. * manipulations, otherwise you'll get a
  120. * <code>NullPointerException</code>.</p>
  121. */
  122. public Thumbnail()
  123. {
  124. super();
  125. }
  126. /**
  127. * <p>Creates a <code>Thumbnail</code> instance and initializes
  128. * with the specified image bytes.</p>
  129. *
  130. * @param thumbnailData The thumbnail data
  131. */
  132. public Thumbnail(final byte[] thumbnailData)
  133. {
  134. this._thumbnailData = thumbnailData;
  135. }
  136. /**
  137. * <p>Returns the thumbnail as a <code>byte[]</code> in {@link
  138. * Variant#VT_CF VT_CF} format.</p>
  139. *
  140. * @return The thumbnail value
  141. * @see SummaryInformation#getThumbnail()
  142. */
  143. public byte[] getThumbnail()
  144. {
  145. return _thumbnailData;
  146. }
  147. /**
  148. * <p>Sets the Thumbnail's underlying <code>byte[]</code> in
  149. * {@link Variant#VT_CF VT_CF} format.</p>
  150. *
  151. * @param thumbnail The new thumbnail value
  152. * @see SummaryInformation#getThumbnail()
  153. */
  154. public void setThumbnail(final byte[] thumbnail)
  155. {
  156. this._thumbnailData = thumbnail;
  157. }
  158. /**
  159. * <p>Returns an <code>int</code> representing the Clipboard
  160. * Format Tag</p>
  161. *
  162. * <p>Possible return values are:</p>
  163. * <ul>
  164. * <li>{@link #CFTAG_WINDOWS CFTAG_WINDOWS}</li>
  165. * <li>{@link #CFTAG_MACINTOSH CFTAG_MACINTOSH}</li>
  166. * <li>{@link #CFTAG_FMTID CFTAG_FMTID}</li>
  167. * <li>{@link #CFTAG_NODATA CFTAG_NODATA}</li>
  168. * </ul>
  169. *
  170. * @return A flag indicating the Clipboard Format Tag
  171. */
  172. public long getClipboardFormatTag()
  173. {
  174. long clipboardFormatTag = LittleEndian.getInt(getThumbnail(),
  175. OFFSET_CFTAG);
  176. return clipboardFormatTag;
  177. }
  178. /**
  179. * <p>Returns an <code>int</code> representing the Clipboard
  180. * Format</p>
  181. *
  182. * <p>Will throw an exception if the Thumbnail's Clipboard Format
  183. * Tag is not {@link Thumbnail#CFTAG_WINDOWS CFTAG_WINDOWS}.</p>
  184. *
  185. * <p>Possible return values are:</p>
  186. *
  187. * <ul>
  188. * <li>{@link #CF_METAFILEPICT CF_METAFILEPICT}</li>
  189. * <li>{@link #CF_DIB CF_DIB}</li>
  190. * <li>{@link #CF_ENHMETAFILE CF_ENHMETAFILE}</li>
  191. * <li>{@link #CF_BITMAP CF_BITMAP}</li>
  192. * </ul>
  193. *
  194. * @return a flag indicating the Clipboard Format
  195. * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS
  196. */
  197. public long getClipboardFormat() throws HPSFException
  198. {
  199. if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
  200. throw new HPSFException("Clipboard Format Tag of Thumbnail must " +
  201. "be CFTAG_WINDOWS.");
  202. return LittleEndian.getInt(getThumbnail(), OFFSET_CF);
  203. }
  204. /**
  205. * <p>Returns the Thumbnail as a <code>byte[]</code> of WMF data
  206. * if the Thumbnail's Clipboard Format Tag is {@link
  207. * #CFTAG_WINDOWS CFTAG_WINDOWS} and its Clipboard Format is
  208. * {@link #CF_METAFILEPICT CF_METAFILEPICT}</p> <p>This
  209. * <code>byte[]</code> is in the traditional WMF file, not the
  210. * clipboard-specific version with special headers.</p>
  211. *
  212. * <p>See <a href="http://www.wvware.com/caolan/ora-wmf.html"
  213. * target="_blank">http://www.wvware.com/caolan/ora-wmf.html</a>
  214. * for more information on the WMF image format.</p>
  215. *
  216. * @return A WMF image of the Thumbnail
  217. * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS and
  218. * CF_METAFILEPICT
  219. */
  220. public byte[] getThumbnailAsWMF() throws HPSFException
  221. {
  222. if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
  223. throw new HPSFException("Clipboard Format Tag of Thumbnail must " +
  224. "be CFTAG_WINDOWS.");
  225. if (!(getClipboardFormat() == CF_METAFILEPICT)) {
  226. throw new HPSFException("Clipboard Format of Thumbnail must " +
  227. "be CF_METAFILEPICT.");
  228. }
  229. byte[] thumbnail = getThumbnail();
  230. int wmfImageLength = thumbnail.length - OFFSET_WMFDATA;
  231. byte[] wmfImage = new byte[wmfImageLength];
  232. System.arraycopy(thumbnail,
  233. OFFSET_WMFDATA,
  234. wmfImage,
  235. 0,
  236. wmfImageLength);
  237. return wmfImage;
  238. }
  239. }