Back to index

lightning-sunbird  0.9+nobinonly
nsImageClipboard.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Mike Pinkerton <pinkerton@netscape.com>
00024  *   Gus Verdun <gustavoverdun@aol.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #ifndef WINCE
00041 
00042 /* Things To Do 11/8/00
00043 
00044 Check image metrics, can we support them? Do we need to?
00045 Any other render format? HTML?
00046 
00047 */
00048 
00049 #include "nsError.h"
00050 #include <windows.h>
00051 
00052 #include "nsCOMPtr.h"
00053 #include "nsIImage.h"
00054 #include "nsIInputStream.h"
00055 
00056 
00057 //
00058 // nsImageToClipboard
00059 //
00060 // A utility class that takes an nsIImage and does all the bitmap magic
00061 // to allow us to put it on the clipboard
00062 //
00063 class nsImageToClipboard
00064 {
00065 public:
00066   nsImageToClipboard ( nsIImage* inImage );
00067   ~nsImageToClipboard();
00068 
00069     // Call to get the actual bits that go on the clipboard. If |nsnull|, the
00070     // setup operations have failed.
00071     //
00072     // NOTE: The caller owns the handle and must delete it with ::GlobalRelease()
00073   nsresult GetPicture ( HANDLE* outBits ) ;
00074 
00075 private:
00076 
00077     // Computes # of bytes needed by a bitmap with the specified attributes.
00078   PRInt32 CalcSize(PRInt32 aHeight, PRInt32 aColors, WORD aBitsPerPixel, PRInt32 aSpanBytes);
00079   PRInt32 CalcSpanLength(PRUint32 aWidth, PRUint32 aBitCount);
00080 
00081     // Do the work
00082   nsresult CreateFromImage ( nsIImage* inImage, HANDLE* outBitmap );
00083 
00084   nsCOMPtr<nsIImage> mImage;            // the image we're working with
00085 
00086 }; // class nsImageToClipboard
00087 
00088 
00089 struct bitFields {
00090     PRUint32 red;
00091     PRUint32 green;
00092     PRUint32 blue;
00093     PRUint8 redLeftShift;
00094     PRUint8 redRightShift;
00095     PRUint8 greenLeftShift;
00096     PRUint8 greenRightShift;
00097     PRUint8 blueLeftShift;
00098     PRUint8 blueRightShift;
00099 };
00100 
00101 //
00102 // nsImageFromClipboard
00103 //
00104 // A utility class that takes a DIB from the win32 clipboard and does
00105 // all the bitmap magic to convert it to a PNG or a JPEG in the form of a nsIInputStream
00106 //
00107 class nsImageFromClipboard
00108 {
00109 public:
00110   nsImageFromClipboard () ;
00111   ~nsImageFromClipboard ( ) ;
00112   
00113     // Retrieve the newly created image
00114   nsresult GetEncodedImageStream (unsigned char * aClipboardData, nsIInputStream** outImage);
00115 
00116 private:
00117 
00118   void InvertRows(unsigned char * aInitialBuffer, PRUint32 aSizeOfBuffer, PRUint32 aNumBytesPerRow);
00119   nsresult ConvertColorBitMap(unsigned char * aInputBuffer, PBITMAPINFO pBitMapInfo, unsigned char * aOutBuffer);
00120   void CalcBitmask(PRUint32 aMask, PRUint8& aBegin, PRUint8& aLength);
00121   void CalcBitShift(bitFields * aColorMask);
00122 
00123 }; // nsImageFromClipboard
00124 
00125 #endif //WINCE