/* * ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2000 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" must * not be used to endorse or promote products derived from this * software without prior written permission. For written * permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * nor may "Apache" appear in their name, without prior written * permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * . */ package org.apache.poi.hpsf; import org.apache.poi.util.LittleEndian; /** *

Class to manipulate data in the Clipboard Variant ({@link * Variant#VT_CF VT_CF}) format.

* * @author Drew Varner (Drew.Varner inOrAround sc.edu) * @see SummaryInformation#getThumbnail() * @version $Id$ * @since 2002-04-29 */ public class Thumbnail { /** *

Offset in bytes where the Clipboard Format Tag starts in the * byte[] returned by {@link * SummaryInformation#getThumbnail()}

*/ public static int OFFSET_CFTAG = 4; /** *

Offset in bytes where the Clipboard Format starts in the * byte[] returned by {@link * SummaryInformation#getThumbnail()}

* *

This is only valid if the Clipboard Format Tag is {@link * #CFTAG_WINDOWS}

*/ public static int OFFSET_CF = 8; /** *

Offset in bytes where the Windows Metafile (WMF) image data * starts in the byte[] returned by {@link * SummaryInformation#getThumbnail()}

* *

There is only WMF data at this point in the * byte[] if the Clipboard Format Tag is {@link * #CFTAG_WINDOWS} and the Clipboard Format is {@link * #CF_METAFILEPICT}.

* *

Note: The byte[] that starts at * OFFSET_WMFDATA and ends at * getThumbnail().length - 1 forms a complete WMF * image. It can be saved to disk with a .wmf file * type and read using a WMF-capable image viewer.

*/ public static int OFFSET_WMFDATA = 20; /** *

Clipboard Format Tag - Windows clipboard format

* *

A DWORD indicating a built-in Windows clipboard * format value

* *

See: http://msdn.microsoft.com/library/en-us/dnolegen/html/msdn_propset.asp.

*/ public static int CFTAG_WINDOWS = -1; /** *

Clipboard Format Tag - Macintosh clipboard format

* *

A DWORD indicating a Macintosh clipboard format * value

* *

See: http://msdn.microsoft.com/library/en-us/dnolegen/html/msdn_propset.asp.

*/ public static int CFTAG_MACINTOSH = -2; /** *

Clipboard Format Tag - Format ID

* *

A GUID containing a format identifier (FMTID). This is * rarely used.

* *

See: http://msdn.microsoft.com/library/en-us/dnolegen/html/msdn_propset.asp.

*/ public static int CFTAG_FMTID = -3; /** *

Clipboard Format Tag - No Data

* *

A DWORD indicating No data. This is rarely * used.

* *

See: * http://msdn.microsoft.com/library/en-us/dnolegen/html/msdn_propset.asp.

*/ public static int CFTAG_NODATA = 0; /** *

Clipboard Format - Windows metafile format. This is the * recommended way to store thumbnails in Property Streams.

* *

Note: This is not the same format used in * regular WMF images. The clipboard version of this format has an * extra clipboard-specific header.

*/ public static int CF_METAFILEPICT = 3; /** *

Clipboard Format - Device Independent Bitmap

*/ public static int CF_DIB = 8; /** *

Clipboard Format - Enhanced Windows metafile format

*/ public static int CF_ENHMETAFILE = 14; /** *

Clipboard Format - Bitmap

* *

Obsolete, see msdn.microsoft.com/library/en-us/dnw98bk/html/clipboardoperations.asp.

*/ public static int CF_BITMAP = 2; /** *

A byte[] to hold a thumbnail image in ({@link * Variant#VT_CF VT_CF}) format.

*/ private byte[] thumbnailData = null; /** *

Default Constructor. If you use it then one you'll have to add * the thumbnail byte[] from {@link * SummaryInformation#getThumbnail()} to do any useful * manipulations, otherwise you'll get a * NullPointerException.

*/ public Thumbnail() { super(); } /** *

Creates a Thumbnail instance and initializes * with the specified image bytes.

* * @param thumbnailData The thumbnail data */ public Thumbnail(final byte[] thumbnailData) { this.thumbnailData = thumbnailData; } /** *

Returns the thumbnail as a byte[] in {@link * Variant#VT_CF VT_CF} format.

* * @return The thumbnail value * @see SummaryInformation#getThumbnail() */ public byte[] getThumbnail() { return thumbnailData; } /** *

Sets the Thumbnail's underlying byte[] in * {@link Variant#VT_CF VT_CF} format.

* * @param thumbnail The new thumbnail value * @see SummaryInformation#getThumbnail() */ public void setThumbnail(final byte[] thumbnail) { this.thumbnailData = thumbnail; } /** *

Returns an int representing the Clipboard * Format Tag

* *

Possible return values are:

* * * @return A flag indicating the Clipboard Format Tag */ public long getClipboardFormatTag() { long clipboardFormatTag = LittleEndian.getUInt(getThumbnail(), OFFSET_CFTAG); return clipboardFormatTag; } /** *

Returns an int representing the Clipboard * Format

* *

Will throw an exception if the Thumbnail's Clipboard Format * Tag is not {@link Thumbnail#CFTAG_WINDOWS CFTAG_WINDOWS}.

* *

Possible return values are:

* * * * @return a flag indicating the Clipboard Format * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS */ public long getClipboardFormat() throws HPSFException { if (!(getClipboardFormatTag() == CFTAG_WINDOWS)) throw new HPSFException("Clipboard Format Tag of Thumbnail must " + "be CFTAG_WINDOWS."); return LittleEndian.getUInt(getThumbnail(), OFFSET_CF); } /** *

Returns the Thumbnail as a byte[] of WMF data * if the Thumbnail's Clipboard Format Tag is {@link * #CFTAG_WINDOWS CFTAG_WINDOWS} and its Clipboard Format is * {@link #CF_METAFILEPICT CF_METAFILEPICT}

This * byte[] is in the traditional WMF file, not the * clipboard-specific version with special headers.

* *

See http://www.wvware.com/caolan/ora-wmf.html * for more information on the WMF image format.

* * @return A WMF image of the Thumbnail * @throws HPSFException if the Thumbnail isn't CFTAG_WINDOWS and * CF_METAFILEPICT */ public byte[] getThumbnailAsWMF() throws HPSFException { if (!(getClipboardFormatTag() == CFTAG_WINDOWS)) throw new HPSFException("Clipboard Format Tag of Thumbnail must " + "be CFTAG_WINDOWS."); if (!(getClipboardFormat() == CF_METAFILEPICT)) throw new HPSFException("Clipboard Format of Thumbnail must " + "be CF_METAFILEPICT."); else { byte[] thumbnail = getThumbnail(); int wmfImageLength = thumbnail.length - OFFSET_WMFDATA; byte[] wmfImage = new byte[wmfImageLength]; System.arraycopy(thumbnail, OFFSET_WMFDATA, wmfImage, 0, wmfImageLength); return wmfImage; } } }