1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsImageClipboard_h
7 #define nsImageClipboard_h
8 
9 /* Things To Do 11/8/00
10 
11 Check image metrics, can we support them? Do we need to?
12 Any other render format? HTML?
13 
14 */
15 
16 #include "nsError.h"
17 #include <windows.h>
18 
19 #include "nsCOMPtr.h"
20 #include "imgIContainer.h"
21 #include "nsIInputStream.h"
22 
23 //
24 // nsImageToClipboard
25 //
26 // A utility class that takes an imgIContainer and does all the bitmap magic
27 // to allow us to put it on the clipboard
28 //
29 class nsImageToClipboard {
30  public:
31   explicit nsImageToClipboard(imgIContainer* aInImage, bool aWantDIBV5 = true);
32   ~nsImageToClipboard();
33 
34   // Call to get the actual bits that go on the clipboard. If |nullptr|, the
35   // setup operations have failed.
36   //
37   // NOTE: The caller owns the handle and must delete it with ::GlobalRelease()
38   nsresult GetPicture(HANDLE* outBits);
39 
40  private:
41   // Computes # of bytes needed by a bitmap with the specified attributes.
42   int32_t CalcSize(int32_t aHeight, int32_t aColors, WORD aBitsPerPixel,
43                    int32_t aSpanBytes);
44   int32_t CalcSpanLength(uint32_t aWidth, uint32_t aBitCount);
45 
46   // Do the work
47   nsresult CreateFromImage(imgIContainer* inImage, HANDLE* outBitmap);
48 
49   nsCOMPtr<imgIContainer> mImage;  // the image we're working with
50   bool mWantDIBV5;
51 
52 };  // class nsImageToClipboard
53 
54 struct bitFields {
55   uint32_t red;
56   uint32_t green;
57   uint32_t blue;
58   uint8_t redLeftShift;
59   uint8_t redRightShift;
60   uint8_t greenLeftShift;
61   uint8_t greenRightShift;
62   uint8_t blueLeftShift;
63   uint8_t blueRightShift;
64 };
65 
66 //
67 // nsImageFromClipboard
68 //
69 // A utility class that takes a DIB from the win32 clipboard and does
70 // all the bitmap magic to convert it to a PNG or a JPEG in the form of a
71 // nsIInputStream
72 //
73 class nsImageFromClipboard {
74  public:
75   nsImageFromClipboard();
76   ~nsImageFromClipboard();
77 
78   // Retrieve the newly created image
79   nsresult GetEncodedImageStream(unsigned char* aClipboardData,
80                                  const char* aMIMEFormat,
81                                  nsIInputStream** outImage);
82 
83  private:
84   void InvertRows(unsigned char* aInitialBuffer, uint32_t aSizeOfBuffer,
85                   uint32_t aNumBytesPerRow);
86   nsresult ConvertColorBitMap(unsigned char* aInputBuffer,
87                               PBITMAPINFO pBitMapInfo,
88                               unsigned char* aOutBuffer);
89   void CalcBitmask(uint32_t aMask, uint8_t& aBegin, uint8_t& aLength);
90   void CalcBitShift(bitFields* aColorMask);
91 
92 };  // nsImageFromClipboard
93 
94 #endif
95