KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > Thumbnail


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hpsf;
19
20 import org.apache.poi.util.LittleEndian;
21 /**
22  * <p>Class to manipulate data in the Clipboard Variant ({@link
23  * Variant#VT_CF VT_CF}) format.</p>
24  *
25  * @author Drew Varner (Drew.Varner inOrAround sc.edu)
26  * @see SummaryInformation#getThumbnail()
27  * @version $Id: Thumbnail.java,v 1.8 2004/12/28 20:20:23 klute Exp $
28  * @since 2002-04-29
29  */

30 public class Thumbnail
31 {
32
33     /**
34      * <p>Offset in bytes where the Clipboard Format Tag starts in the
35      * <code>byte[]</code> returned by {@link
36      * SummaryInformation#getThumbnail()}</p>
37      */

38     public static int OFFSET_CFTAG = 4;
39
40     /**
41      * <p>Offset in bytes where the Clipboard Format starts in the
42      * <code>byte[]</code> returned by {@link
43      * SummaryInformation#getThumbnail()}</p>
44      *
45      * <p>This is only valid if the Clipboard Format Tag is {@link
46      * #CFTAG_WINDOWS}</p>
47      */

48     public static int OFFSET_CF = 8;
49
50     /**
51      * <p>Offset in bytes where the Windows Metafile (WMF) image data
52      * starts in the <code>byte[]</code> returned by {@link
53      * SummaryInformation#getThumbnail()}</p>
54      *
55      * <p>There is only WMF data at this point in the
56      * <code>byte[]</code> if the Clipboard Format Tag is {@link
57      * #CFTAG_WINDOWS} and the Clipboard Format is {@link
58      * #CF_METAFILEPICT}.</p>
59      *
60      * <p>Note: The <code>byte[]</code> that starts at
61      * <code>OFFSET_WMFDATA</code> and ends at
62      * <code>getThumbnail().length - 1</code> forms a complete WMF
63      * image. It can be saved to disk with a <code>.wmf</code> file
64      * type and read using a WMF-capable image viewer.</p>
65      */

66     public static int OFFSET_WMFDATA = 20;
67
68     /**
69      * <p>Clipboard Format Tag - Windows clipboard format</p>
70      *
71      * <p>A <code>DWORD</code> indicating a built-in Windows clipboard
72      * format value</p>
73      */

74     public static int CFTAG_WINDOWS = -1;
75
76     /**
77      * <p>Clipboard Format Tag - Macintosh clipboard format</p>
78      *
79      * <p>A <code>DWORD</code> indicating a Macintosh clipboard format
80      * value</p>
81      */

82     public static int CFTAG_MACINTOSH = -2;
83
84     /**
85      * <p>Clipboard Format Tag - Format ID</p>
86      *
87      * <p>A GUID containing a format identifier (FMTID). This is
88      * rarely used.</p>
89      */

90     public static int CFTAG_FMTID = -3;
91
92     /**
93      * <p>Clipboard Format Tag - No Data</p>
94      *
95      * <p>A <code>DWORD</code> indicating No data. This is rarely
96      * used.</p>
97      */

98     public static int CFTAG_NODATA = 0;
99
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
137     /**
138      * <p>Default Constructor. If you use it then one you'll have to add
139      * the thumbnail <code>byte[]</code> from {@link
140      * SummaryInformation#getThumbnail()} to do any useful
141      * manipulations, otherwise you'll get a
142      * <code>NullPointerException</code>.</p>
143      */

144     public Thumbnail()
145     {
146         super();
147     }
148
149
150
151     /**
152      * <p>Creates a <code>Thumbnail</code> instance and initializes
153      * with the specified image bytes.</p>
154      *
155      * @param thumbnailData The thumbnail data
156      */

157     public Thumbnail(final byte[] thumbnailData)
158     {
159         this.thumbnailData = thumbnailData;
160     }
161
162
163
164     /**
165      * <p>Returns the thumbnail as a <code>byte[]</code> in {@link
166      * Variant#VT_CF VT_CF} format.</p>
167      *
168      * @return The thumbnail value
169      * @see SummaryInformation#getThumbnail()
170      */

171     public byte[] getThumbnail()
172     {
173         return thumbnailData;
174     }
175
176
177
178     /**
179      * <p>Sets the Thumbnail's underlying <code>byte[]</code> in
180      * {@link Variant#VT_CF VT_CF} format.</p>
181      *
182      * @param thumbnail The new thumbnail value
183      * @see SummaryInformation#getThumbnail()
184      */

185     public void setThumbnail(final byte[] thumbnail)
186     {
187         this.thumbnailData = thumbnail;
188     }
189
190
191
192     /**
193      * <p>Returns an <code>int</code> representing the Clipboard
194      * Format Tag</p>
195      *
196      * <p>Possible return values are:</p>
197      * <ul>
198      * <li>{@link #CFTAG_WINDOWS CFTAG_WINDOWS}</li>
199      * <li>{@link #CFTAG_MACINTOSH CFTAG_MACINTOSH}</li>
200      * <li>{@link #CFTAG_FMTID CFTAG_FMTID}</li>
201      * <li>{@link #CFTAG_NODATA CFTAG_NODATA}</li>
202      * </ul>
203      *
204      * @return A flag indicating the Clipboard Format Tag
205      */

206     public long getClipboardFormatTag()
207     {
208         long clipboardFormatTag = LittleEndian.getUInt(getThumbnail(),
209                                                        OFFSET_CFTAG);
210         return clipboardFormatTag;
211     }
212
213
214
215     /**
216      * <p>Returns an <code>int</code> representing the Clipboard
217      * Format</p>
218      *
219      * <p>Will throw an exception if the Thumbnail's Clipboard Format
220      * Tag is not {@link Thumbnail#CFTAG_WINDOWS CFTAG_WINDOWS}.</p>
221      *
222      * <p>Possible return values are:</p>
223      *
224      * <ul>
225      * <li>{@link #CF_METAFILEPICT CF_METAFILEPICT}</li>
226      * <li>{@link #CF_DIB CF_DIB}</li>
227      * <li>{@link #CF_ENHMETAFILE CF_ENHMETAFILE}</li>
228      * <li>{@link #CF_BITMAP CF_BITMAP}</li>
229      * </ul>
230      *
231      * @return a flag indicating the Clipboard Format
232      * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS
233      */

234     public long getClipboardFormat() throws HPSFException
235     {
236         if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
237             throw new HPSFException("Clipboard Format Tag of Thumbnail must " +
238                                     "be CFTAG_WINDOWS.");
239
240         return LittleEndian.getUInt(getThumbnail(), OFFSET_CF);
241     }
242
243
244
245     /**
246      * <p>Returns the Thumbnail as a <code>byte[]</code> of WMF data
247      * if the Thumbnail's Clipboard Format Tag is {@link
248      * #CFTAG_WINDOWS CFTAG_WINDOWS} and its Clipboard Format is
249      * {@link #CF_METAFILEPICT CF_METAFILEPICT}</p> <p>This
250      * <code>byte[]</code> is in the traditional WMF file, not the
251      * clipboard-specific version with special headers.</p>
252      *
253      * <p>See <a HREF="http://www.wvware.com/caolan/ora-wmf.html"
254      * target="_blank">http://www.wvware.com/caolan/ora-wmf.html</a>
255      * for more information on the WMF image format.</p>
256      *
257      * @return A WMF image of the Thumbnail
258      * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS and
259      * CF_METAFILEPICT
260      */

261     public byte[] getThumbnailAsWMF() throws HPSFException
262     {
263         if (!(getClipboardFormatTag() == CFTAG_WINDOWS))
264             throw new HPSFException("Clipboard Format Tag of Thumbnail must " +
265                                     "be CFTAG_WINDOWS.");
266         if (!(getClipboardFormat() == CF_METAFILEPICT))
267             throw new HPSFException("Clipboard Format of Thumbnail must " +
268                                     "be CF_METAFILEPICT.");
269         else
270         {
271             byte[] thumbnail = getThumbnail();
272             int wmfImageLength = thumbnail.length - OFFSET_WMFDATA;
273             byte[] wmfImage = new byte[wmfImageLength];
274             System.arraycopy(thumbnail,
275                              OFFSET_WMFDATA,
276                              wmfImage,
277                              0,
278                              wmfImageLength);
279             return wmfImage;
280         }
281     }
282
283 }
284
Popular Tags