001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: /**
023: * <p>Class to manipulate data in the Clipboard Variant ({@link
024: * Variant#VT_CF VT_CF}) format.</p>
025: *
026: * @author Drew Varner (Drew.Varner inOrAround sc.edu)
027: * @see SummaryInformation#getThumbnail()
028: * @version $Id: Thumbnail.java 489730 2006-12-22 19:18:16Z bayard $
029: * @since 2002-04-29
030: */
031: public class Thumbnail {
032:
033: /**
034: * <p>Offset in bytes where the Clipboard Format Tag starts in the
035: * <code>byte[]</code> returned by {@link
036: * SummaryInformation#getThumbnail()}</p>
037: */
038: public static int OFFSET_CFTAG = 4;
039:
040: /**
041: * <p>Offset in bytes where the Clipboard Format starts in the
042: * <code>byte[]</code> returned by {@link
043: * SummaryInformation#getThumbnail()}</p>
044: *
045: * <p>This is only valid if the Clipboard Format Tag is {@link
046: * #CFTAG_WINDOWS}</p>
047: */
048: public static int OFFSET_CF = 8;
049:
050: /**
051: * <p>Offset in bytes where the Windows Metafile (WMF) image data
052: * starts in the <code>byte[]</code> returned by {@link
053: * SummaryInformation#getThumbnail()}</p>
054: *
055: * <p>There is only WMF data at this point in the
056: * <code>byte[]</code> if the Clipboard Format Tag is {@link
057: * #CFTAG_WINDOWS} and the Clipboard Format is {@link
058: * #CF_METAFILEPICT}.</p>
059: *
060: * <p>Note: The <code>byte[]</code> that starts at
061: * <code>OFFSET_WMFDATA</code> and ends at
062: * <code>getThumbnail().length - 1</code> forms a complete WMF
063: * image. It can be saved to disk with a <code>.wmf</code> file
064: * type and read using a WMF-capable image viewer.</p>
065: */
066: public static int OFFSET_WMFDATA = 20;
067:
068: /**
069: * <p>Clipboard Format Tag - Windows clipboard format</p>
070: *
071: * <p>A <code>DWORD</code> indicating a built-in Windows clipboard
072: * format value</p>
073: */
074: public static int CFTAG_WINDOWS = -1;
075:
076: /**
077: * <p>Clipboard Format Tag - Macintosh clipboard format</p>
078: *
079: * <p>A <code>DWORD</code> indicating a Macintosh clipboard format
080: * value</p>
081: */
082: public static int CFTAG_MACINTOSH = -2;
083:
084: /**
085: * <p>Clipboard Format Tag - Format ID</p>
086: *
087: * <p>A GUID containing a format identifier (FMTID). This is
088: * rarely used.</p>
089: */
090: public static int CFTAG_FMTID = -3;
091:
092: /**
093: * <p>Clipboard Format Tag - No Data</p>
094: *
095: * <p>A <code>DWORD</code> indicating No data. This is rarely
096: * used.</p>
097: */
098: public static int CFTAG_NODATA = 0;
099:
100: /**
101: * <p>Clipboard Format - Windows metafile format. This is the
102: * recommended way to store thumbnails in Property Streams.</p>
103: *
104: * <p><strong>Note:</strong> This is not the same format used in
105: * regular WMF images. The clipboard version of this format has an
106: * extra clipboard-specific header.</p>
107: */
108: public static int CF_METAFILEPICT = 3;
109:
110: /**
111: * <p>Clipboard Format - Device Independent Bitmap</p>
112: */
113: public static int CF_DIB = 8;
114:
115: /**
116: * <p>Clipboard Format - Enhanced Windows metafile format</p>
117: */
118: public static int CF_ENHMETAFILE = 14;
119:
120: /**
121: * <p>Clipboard Format - Bitmap</p>
122: *
123: * <p>Obsolete, see <a
124: * href="msdn.microsoft.com/library/en-us/dnw98bk/html/clipboardoperations.asp
125: * target="_blank">msdn.microsoft.com/library/en-us/dnw98bk/html/clipboardoperations.asp</a>.</p>
126: */
127: public static int CF_BITMAP = 2;
128:
129: /**
130: * <p>A <code>byte[]</code> to hold a thumbnail image in ({@link
131: * Variant#VT_CF VT_CF}) format.</p>
132: */
133: private byte[] thumbnailData = null;
134:
135: /**
136: * <p>Default Constructor. If you use it then one you'll have to add
137: * the thumbnail <code>byte[]</code> from {@link
138: * SummaryInformation#getThumbnail()} to do any useful
139: * manipulations, otherwise you'll get a
140: * <code>NullPointerException</code>.</p>
141: */
142: public Thumbnail() {
143: super ();
144: }
145:
146: /**
147: * <p>Creates a <code>Thumbnail</code> instance and initializes
148: * with the specified image bytes.</p>
149: *
150: * @param thumbnailData The thumbnail data
151: */
152: public Thumbnail(final byte[] thumbnailData) {
153: this .thumbnailData = thumbnailData;
154: }
155:
156: /**
157: * <p>Returns the thumbnail as a <code>byte[]</code> in {@link
158: * Variant#VT_CF VT_CF} format.</p>
159: *
160: * @return The thumbnail value
161: * @see SummaryInformation#getThumbnail()
162: */
163: public byte[] getThumbnail() {
164: return thumbnailData;
165: }
166:
167: /**
168: * <p>Sets the Thumbnail's underlying <code>byte[]</code> in
169: * {@link Variant#VT_CF VT_CF} format.</p>
170: *
171: * @param thumbnail The new thumbnail value
172: * @see SummaryInformation#getThumbnail()
173: */
174: public void setThumbnail(final byte[] thumbnail) {
175: this .thumbnailData = thumbnail;
176: }
177:
178: /**
179: * <p>Returns an <code>int</code> representing the Clipboard
180: * Format Tag</p>
181: *
182: * <p>Possible return values are:</p>
183: * <ul>
184: * <li>{@link #CFTAG_WINDOWS CFTAG_WINDOWS}</li>
185: * <li>{@link #CFTAG_MACINTOSH CFTAG_MACINTOSH}</li>
186: * <li>{@link #CFTAG_FMTID CFTAG_FMTID}</li>
187: * <li>{@link #CFTAG_NODATA CFTAG_NODATA}</li>
188: * </ul>
189: *
190: * @return A flag indicating the Clipboard Format Tag
191: */
192: public long getClipboardFormatTag() {
193: long clipboardFormatTag = LittleEndian.getUInt(getThumbnail(),
194: OFFSET_CFTAG);
195: return clipboardFormatTag;
196: }
197:
198: /**
199: * <p>Returns an <code>int</code> representing the Clipboard
200: * Format</p>
201: *
202: * <p>Will throw an exception if the Thumbnail's Clipboard Format
203: * Tag is not {@link Thumbnail#CFTAG_WINDOWS CFTAG_WINDOWS}.</p>
204: *
205: * <p>Possible return values are:</p>
206: *
207: * <ul>
208: * <li>{@link #CF_METAFILEPICT CF_METAFILEPICT}</li>
209: * <li>{@link #CF_DIB CF_DIB}</li>
210: * <li>{@link #CF_ENHMETAFILE CF_ENHMETAFILE}</li>
211: * <li>{@link #CF_BITMAP CF_BITMAP}</li>
212: * </ul>
213: *
214: * @return a flag indicating the Clipboard Format
215: * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS
216: */
217: public long getClipboardFormat() throws HPSFException {
218: if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
219: throw new HPSFException(
220: "Clipboard Format Tag of Thumbnail must "
221: + "be CFTAG_WINDOWS.");
222:
223: return LittleEndian.getUInt(getThumbnail(), OFFSET_CF);
224: }
225:
226: /**
227: * <p>Returns the Thumbnail as a <code>byte[]</code> of WMF data
228: * if the Thumbnail's Clipboard Format Tag is {@link
229: * #CFTAG_WINDOWS CFTAG_WINDOWS} and its Clipboard Format is
230: * {@link #CF_METAFILEPICT CF_METAFILEPICT}</p> <p>This
231: * <code>byte[]</code> is in the traditional WMF file, not the
232: * clipboard-specific version with special headers.</p>
233: *
234: * <p>See <a href="http://www.wvware.com/caolan/ora-wmf.html"
235: * target="_blank">http://www.wvware.com/caolan/ora-wmf.html</a>
236: * for more information on the WMF image format.</p>
237: *
238: * @return A WMF image of the Thumbnail
239: * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS and
240: * CF_METAFILEPICT
241: */
242: public byte[] getThumbnailAsWMF() throws HPSFException {
243: if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
244: throw new HPSFException(
245: "Clipboard Format Tag of Thumbnail must "
246: + "be CFTAG_WINDOWS.");
247: if (!(getClipboardFormat() == CF_METAFILEPICT))
248: throw new HPSFException(
249: "Clipboard Format of Thumbnail must "
250: + "be CF_METAFILEPICT.");
251: else {
252: byte[] thumbnail = getThumbnail();
253: int wmfImageLength = thumbnail.length - OFFSET_WMFDATA;
254: byte[] wmfImage = new byte[wmfImageLength];
255: System.arraycopy(thumbnail, OFFSET_WMFDATA, wmfImage, 0,
256: wmfImageLength);
257: return wmfImage;
258: }
259: }
260:
261: }
|