nsImageToBitmap.cpp
Go to the documentation of this file.
1 /* vim: fileencoding=utf-8 shiftwidth=2 */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org widget code.
16  *
17  * The Initial Developer of the Original Code is
18  * Matthew Gertner <matthew@allpeers.com>.
19  * Portions created by the Initial Developer are Copyright (C) 2007
20  * AllPeers Ltd. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37 
38 #include "nsStringGlue.h"
39 #include "nsImageToBitmap.h"
40 #include "gfxImageSurface.h"
41 
42 #include <windows.h>
43 
45 
47 {
48 }
49 
51 {
52 }
53 
54 NS_IMETHODIMP nsImageToBitmap::ConvertImageToBitmap(gfxImageSurface* aImage,
55  HBITMAP& outBitmap)
56 {
57  NS_ENSURE_ARG(aImage);
58  outBitmap = NULL;
59 
60  BITMAPV4HEADER infoHeader;
61  ::ZeroMemory(&infoHeader, sizeof(infoHeader));
62  infoHeader.bV4Size = sizeof(infoHeader);
63  infoHeader.bV4Width = aImage->Width();
64  infoHeader.bV4Height = -aImage->Height(); // top-to-bottom
65  infoHeader.bV4Planes = 1;
66  infoHeader.bV4BitCount = 32; // RGBA
67  infoHeader.bV4V4Compression = BI_BITFIELDS;
68  infoHeader.bV4SizeImage = aImage->Height() * aImage->Stride();
69  infoHeader.bV4XPelsPerMeter = 0;
70  infoHeader.bV4YPelsPerMeter = 0;
71  infoHeader.bV4ClrUsed = 0;
72  infoHeader.bV4ClrImportant = 0;
73  infoHeader.bV4RedMask = 0x00FF0000;
74  infoHeader.bV4GreenMask = 0x0000FF00;
75  infoHeader.bV4BlueMask = 0x000000FF;
76  infoHeader.bV4AlphaMask = 0xFF000000;
77 
78  HBITMAP tBitmap = NULL, oldbits = NULL, bitmap = NULL;
79  HDC dc = ::CreateCompatibleDC(NULL);
80  if (!dc) return NS_ERROR_FAILURE;
81  // we have to select a dummy bitmap in first;
82  // otherwise we just end up with a boring black icon
83  tBitmap = ::CreateBitmap(1, 1, 1, 32, NULL);
84  if (!tBitmap) goto loser;
85  oldbits = (HBITMAP)::SelectObject(dc, tBitmap);
86  if (!oldbits) goto loser;
87 
88  PRUint8 *bits = aImage->Data();
89 
90  bitmap = ::CreateDIBitmap(dc,
91  reinterpret_cast<CONST BITMAPINFOHEADER*>(&infoHeader),
92  CBM_INIT,
93  bits,
94  reinterpret_cast<CONST BITMAPINFO*>(&infoHeader),
95  DIB_RGB_COLORS);
96 
97  if (bitmap) {
98  // we were successful
99  outBitmap = bitmap;
100  }
101 
102 loser:
103  // cleanup
104  if (oldbits)
105  ::SelectObject(dc, oldbits);
106  if (tBitmap)
107  ::DeleteObject(tBitmap);
108  ::DeleteObject(dc);
109 
110  return outBitmap ? NS_OK : NS_ERROR_FAILURE;
111 }
112 
113 NS_IMETHODIMP nsImageToBitmap::ConvertImageToIcon(gfxImageSurface* aImage,
114  HICON& outIcon)
115 {
116  return ImageToIcon(aImage, PR_TRUE, 0, 0, outIcon);
117 }
118 
119 NS_IMETHODIMP nsImageToBitmap::ConvertImageToCursor(gfxImageSurface* aImage,
120  PRUint32 aHotspotX, PRUint32 aHotspotY, HCURSOR& outCursor)
121 {
122  return ImageToIcon(aImage, PR_FALSE, aHotspotX, aHotspotY, outCursor);
123 }
124 
125 nsresult nsImageToBitmap::ImageToIcon(gfxImageSurface* aImage,
126  PRBool aIcon, PRUint32 aHotspotX, PRUint32 aHotspotY,
127  HICON& _retval)
128 {
129  nsresult rv;
130 
131  HBITMAP hBMP;
132  rv = ConvertImageToBitmap(aImage, hBMP);
133  if (NS_FAILED(rv))
134  return rv;
135 
136  ICONINFO info = {0};
137  info.fIcon = aIcon;
138  info.xHotspot = aHotspotX;
139  info.yHotspot = aHotspotY;
140  info.hbmMask = hBMP;
141  info.hbmColor = hBMP;
142 
143  _retval = ::CreateIconIndirect(&info);
144  ::DeleteObject(hBMP);
145 
146  if (_retval == NULL) {
147  return NS_ERROR_FAILURE;
148  }
149 
150  return NS_OK;
151 }
return NS_OK
NS_IMETHOD ConvertImageToBitmap(gfxImageSurface *aImage, HBITMAP &outBitmap)
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
struct HICON__ * HICON
struct HBITMAP__ * HBITMAP
NS_IMETHOD ConvertImageToCursor(gfxImageSurface *aImage, PRUint32 aHotspotX, PRUint32 aHotspotY, HCURSOR &outCursor)
NS_IMETHOD ConvertImageToIcon(gfxImageSurface *aImage, HICON &outIcon)
nsresult ImageToIcon(gfxImageSurface *aImage, PRBool aIcon, PRUint32 aHotspotX, PRUint32 aHotspotY, HICON &_retval)
HICON HCURSOR