blob: ed6c82d4f787b7966c76cc186f4ebda8eb338d6a [file] [log] [blame]
Alexandre Julliard401710d1993-09-04 10:09:32 +00001/*
2 * GDI bitmap objects
3 *
4 * Copyright 1993 Alexandre Julliard
Huw D M Davies87f87bf1998-10-28 09:53:53 +00005 * 1998 Huw D M Davies
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliarda2f2e011995-06-06 16:40:35 +000020 */
Alexandre Julliardd90840e1996-06-11 16:02:08 +000021
Alexandre Julliard8d24ae61994-04-05 21:42:43 +000022#include <stdlib.h>
Alexandre Julliard84c70f51997-05-09 08:40:27 +000023#include <string.h>
Marcus Meissner317af321999-02-17 13:51:06 +000024
25#include "wine/winbase16.h"
Alexandre Julliard6ec42c02004-01-15 00:35:38 +000026#include "wine/winuser16.h"
Alexandre Julliard401710d1993-09-04 10:09:32 +000027#include "gdi.h"
Alexandre Julliard6ec42c02004-01-15 00:35:38 +000028#include "gdi_private.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000029#include "wine/debug.h"
Alexandre Julliard670cdc41997-08-24 16:00:30 +000030
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000031WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000032
Alexandre Julliard77b99181997-09-14 17:17:23 +000033
Alexandre Julliardd8a92442002-05-31 18:43:22 +000034static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
35static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
36static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
38
39static const struct gdi_obj_funcs bitmap_funcs =
40{
41 BITMAP_SelectObject, /* pSelectObject */
42 BITMAP_GetObject16, /* pGetObject16 */
43 BITMAP_GetObject, /* pGetObjectA */
44 BITMAP_GetObject, /* pGetObjectW */
45 NULL, /* pUnrealizeObject */
46 BITMAP_DeleteObject /* pDeleteObject */
47};
48
Alexandre Julliard77b99181997-09-14 17:17:23 +000049/***********************************************************************
Huw D M Davies82617361998-11-01 16:35:42 +000050 * BITMAP_GetWidthBytes
Alexandre Julliard77b99181997-09-14 17:17:23 +000051 *
Huw D M Davies82617361998-11-01 16:35:42 +000052 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
53 * data.
Alexandre Julliard77b99181997-09-14 17:17:23 +000054 */
Alexandre Julliardaface532002-08-28 22:35:23 +000055static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
Alexandre Julliard77b99181997-09-14 17:17:23 +000056{
57 switch(bpp)
58 {
59 case 1:
60 return 2 * ((bmWidth+15) >> 4);
61
62 case 24:
63 bmWidth *= 3; /* fall through */
64 case 8:
65 return bmWidth + (bmWidth & 1);
66
67 case 32:
68 return bmWidth * 4;
69
70 case 16:
71 case 15:
72 return bmWidth * 2;
73
74 case 4:
75 return 2 * ((bmWidth+3) >> 2);
76
77 default:
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000078 WARN("Unknown depth %d, please report.\n", bpp );
Alexandre Julliard77b99181997-09-14 17:17:23 +000079 }
80 return -1;
81}
Alexandre Julliard670cdc41997-08-24 16:00:30 +000082
Alexandre Julliard21979011997-03-05 08:22:35 +000083
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000084/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +000085 * CreateBitmap [GDI32.@]
86 *
87 * Creates a bitmap with the specified info.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000088 *
89 * PARAMS
90 * width [I] bitmap width
91 * height [I] bitmap height
92 * planes [I] Number of color planes
93 * bpp [I] Number of bits to identify a color
94 * bits [I] Pointer to array containing color data
95 *
96 * RETURNS
97 * Success: Handle to bitmap
Huw D M Davies87f87bf1998-10-28 09:53:53 +000098 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +000099 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000100HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
101 UINT bpp, LPCVOID bits )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000102{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000103 BITMAPOBJ *bmp;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000104 HBITMAP hbitmap;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000105
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000106 planes = (BYTE)planes;
107 bpp = (BYTE)bpp;
108
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000109
110 /* Check parameters */
Erik Inge Bolsø975324f2001-12-04 20:16:25 +0000111 if (!height || !width)
112 {
113 height = 1;
114 width = 1;
115 planes = 1;
116 bpp = 1;
117 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000118 if (planes != 1) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000119 FIXME("planes = %d\n", planes);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000120 return 0;
121 }
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000122 if (height < 0) height = -height;
123 if (width < 0) width = -width;
124
125 /* Create the BITMAPOBJ */
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000126 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
127 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000128 return 0;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000129
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000130 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000131
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000132 bmp->size.cx = 0;
133 bmp->size.cy = 0;
134 bmp->bitmap.bmType = 0;
Huw D M Davies82617361998-11-01 16:35:42 +0000135 bmp->bitmap.bmWidth = width;
136 bmp->bitmap.bmHeight = height;
137 bmp->bitmap.bmPlanes = planes;
138 bmp->bitmap.bmBitsPixel = bpp;
139 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000140 bmp->bitmap.bmBits = NULL;
141
Alexandre Julliarda08e2cf2000-03-28 13:37:50 +0000142 bmp->funcs = NULL;
143 bmp->physBitmap = NULL;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000144 bmp->dib = NULL;
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000145 bmp->segptr_bits = 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000146
147 if (bits) /* Set bitmap bits */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000148 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
Alexandre Julliard21979011997-03-05 08:22:35 +0000149 bits );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000150 GDI_ReleaseObj( hbitmap );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000151 return hbitmap;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000152}
153
154
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000155/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000156 * CreateCompatibleBitmap [GDI32.@]
157 *
158 * Creates a bitmap compatible with the DC.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000159 *
160 * PARAMS
161 * hdc [I] Handle to device context
162 * width [I] Width of bitmap
163 * height [I] Height of bitmap
164 *
165 * RETURNS
166 * Success: Handle to bitmap
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000167 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000168 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000169HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
Alexandre Julliard21979011997-03-05 08:22:35 +0000170{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000171 HBITMAP hbmpRet = 0;
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000172 DC *dc;
173
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000174 TRACE("(%p,%d,%d) = \n", hdc, width, height );
Alexandre Julliard21979011997-03-05 08:22:35 +0000175 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
NF Stevens16f0dfb1999-01-01 18:45:03 +0000176 if ((width >= 0x10000) || (height >= 0x10000)) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000177 FIXME("got bad width %d or height %d, please look for reason\n",
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000178 width, height );
Alexandre Julliardb89525f2004-01-18 22:20:17 +0000179 }
180 else
181 {
182 INT planes, bpp;
183
184 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
185 {
186 planes = GetDeviceCaps( hdc, PLANES );
187 bpp = GetDeviceCaps( hdc, BITSPIXEL );
188 }
189 else /* memory DC, get the depth of the bitmap */
190 {
191 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
192 planes = bmp->bitmap.bmPlanes;
193 bpp = bmp->bitmap.bmBitsPixel;
194 GDI_ReleaseObj( dc->hBitmap );
195 }
196 hbmpRet = CreateBitmap( width, height, planes, bpp, NULL );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000197 }
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000198 TRACE("\t\t%p\n", hbmpRet);
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000199 GDI_ReleaseObj(hdc);
Alexandre Julliard1e9ac791996-06-06 18:38:27 +0000200 return hbmpRet;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000201}
202
203
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000204/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000205 * CreateBitmapIndirect [GDI32.@]
206 *
207 * Creates a bitmap with the specifies info.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000208 *
209 * RETURNS
210 * Success: Handle to bitmap
211 * Failure: NULL
Alexandre Julliard401710d1993-09-04 10:09:32 +0000212 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000213HBITMAP WINAPI CreateBitmapIndirect(
214 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000215{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000216 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
Alexandre Julliard21979011997-03-05 08:22:35 +0000217 bmp->bmBitsPixel, bmp->bmBits );
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000218}
219
220
221/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000222 * GetBitmapBits [GDI32.@]
223 *
224 * Copies bitmap bits of bitmap to buffer.
Vincent Béron9a624912002-05-31 23:06:46 +0000225 *
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000226 * RETURNS
227 * Success: Number of bytes copied
228 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000229 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000230LONG WINAPI GetBitmapBits(
231 HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000232 LONG count, /* [in] Number of bytes to copy */
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000233 LPVOID bits) /* [out] Pointer to buffer to receive bits */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000234{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000235 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
236 LONG height, ret;
Vincent Béron9a624912002-05-31 23:06:46 +0000237
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000238 if (!bmp) return 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000239
Pascal Lessard8903e531999-05-22 10:39:00 +0000240 /* If the bits vector is null, the function should return the read size */
241 if(bits == NULL)
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000242 {
243 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
244 goto done;
245 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000246
Alexandre Julliardade697e1995-11-26 13:59:11 +0000247 if (count < 0) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000248 WARN("(%ld): Negative number of bytes passed???\n", count );
Alexandre Julliardade697e1995-11-26 13:59:11 +0000249 count = -count;
250 }
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000251
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000252 /* Only get entire lines */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000253 height = count / bmp->bitmap.bmWidthBytes;
254 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000255 count = height * bmp->bitmap.bmWidthBytes;
Uwe Bonnes1f43de31998-11-07 12:25:18 +0000256 if (count == 0)
257 {
Francois Gouget05fc3cd2001-01-22 02:22:26 +0000258 WARN("Less than one entire line requested\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000259 ret = 0;
260 goto done;
Uwe Bonnes1f43de31998-11-07 12:25:18 +0000261 }
262
Alexandre Julliard77b99181997-09-14 17:17:23 +0000263
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000264 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000265 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
266 1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard77b99181997-09-14 17:17:23 +0000267
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000268 if(bmp->funcs)
269 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000270 TRACE("Calling device specific BitmapBits\n");
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000271 if(bmp->funcs->pGetBitmapBits)
272 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
273 else
274 {
275 memset( bits, 0, count );
276 ret = count;
277 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000278 } else {
Alexandre Julliard03468f71998-02-15 19:40:49 +0000279
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000280 if(!bmp->bitmap.bmBits) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000281 WARN("Bitmap is empty\n");
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000282 ret = 0;
283 } else {
284 memcpy(bits, bmp->bitmap.bmBits, count);
285 ret = count;
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000286 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000287
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000288 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000289 done:
290 GDI_ReleaseObj( hbitmap );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000291 return ret;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000292}
293
294
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000295/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000296 * SetBitmapBits [GDI32.@]
297 *
298 * Sets bits of color data for a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000299 *
300 * RETURNS
301 * Success: Number of bytes used in setting the bitmap bits
302 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000303 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000304LONG WINAPI SetBitmapBits(
305 HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000306 LONG count, /* [in] Number of bytes in bitmap array */
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000307 LPCVOID bits) /* [in] Address of array with bitmap bits */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000308{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000309 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
310 LONG height, ret;
Vincent Béron9a624912002-05-31 23:06:46 +0000311
Pascal Lessard8903e531999-05-22 10:39:00 +0000312 if ((!bmp) || (!bits))
313 return 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000314
Alexandre Julliardade697e1995-11-26 13:59:11 +0000315 if (count < 0) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000316 WARN("(%ld): Negative number of bytes passed???\n", count );
Alexandre Julliardade697e1995-11-26 13:59:11 +0000317 count = -count;
318 }
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000319
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000320 /* Only get entire lines */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000321 height = count / bmp->bitmap.bmWidthBytes;
322 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000323 count = height * bmp->bitmap.bmWidthBytes;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000324
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000325 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000326 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
327 1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard77b99181997-09-14 17:17:23 +0000328
Alexandre Julliarda08e2cf2000-03-28 13:37:50 +0000329 if(bmp->funcs) {
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000330
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000331 TRACE("Calling device specific BitmapBits\n");
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000332 if(bmp->funcs->pSetBitmapBits)
333 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
334 else
335 ret = 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000336 } else {
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000337
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000338 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
339 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
340 if(!bmp->bitmap.bmBits) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000341 WARN("Unable to allocate bit buffer\n");
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000342 ret = 0;
343 } else {
344 memcpy(bmp->bitmap.bmBits, bits, count);
345 ret = count;
346 }
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000347 }
348
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000349 GDI_ReleaseObj( hbitmap );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000350 return ret;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000351}
352
Alexandre Julliard594997c1995-04-30 10:05:20 +0000353/**********************************************************************
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000354 * BITMAP_CopyBitmap
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000355 *
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000356 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000357HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000358{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000359 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000360 HBITMAP res = 0;
361 BITMAP bm;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000362
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000363 if(!bmp) return 0;
364
365 bm = bmp->bitmap;
366 bm.bmBits = NULL;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000367 res = CreateBitmapIndirect(&bm);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000368
369 if(res) {
370 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
371 bm.bmHeight );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000372 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
373 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000374 HeapFree( GetProcessHeap(), 0, buf );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000375 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000376
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000377 GDI_ReleaseObj( hbitmap );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000378 return res;
379}
380
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000381
382/***********************************************************************
383 * BITMAP_SetOwnerDC
384 *
385 * Set the type of DC that owns the bitmap. This is used when the
386 * bitmap is selected into a device to initialize the bitmap function
387 * table.
388 */
389BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
390{
391 BITMAPOBJ *bitmap;
392 BOOL ret;
393
394 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
395 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
396
397 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
398
399 ret = TRUE;
400 if (!bitmap->funcs) /* not owned by a DC yet */
401 {
402 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
403 if (ret) bitmap->funcs = dc->funcs;
404 }
405 else if (bitmap->funcs != dc->funcs)
406 {
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000407 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000408 ret = FALSE;
409 }
410 GDI_ReleaseObj( hbitmap );
411 return ret;
412}
413
414
415/***********************************************************************
416 * BITMAP_SelectObject
417 */
418static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
419{
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000420 HGDIOBJ ret;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000421 BITMAPOBJ *bitmap = obj;
422 DC *dc = DC_GetDCPtr( hdc );
423
424 if (!dc) return 0;
Alexandre Julliarde1147ba2003-05-13 23:56:12 +0000425 if (GetObjectType( hdc ) != OBJ_MEMDC)
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000426 {
427 GDI_ReleaseObj( hdc );
428 return 0;
429 }
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000430 ret = dc->hBitmap;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000431 if (handle == dc->hBitmap) goto done; /* nothing to do */
432
433 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
434 {
435 WARN( "Bitmap already selected in another DC\n" );
436 GDI_ReleaseObj( hdc );
437 return 0;
438 }
439
440 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
441 {
442 GDI_ReleaseObj( hdc );
443 return 0;
444 }
445
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000446 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000447
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000448 if (handle)
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000449 {
Alexandre Julliardd1f73182004-02-13 04:06:37 +0000450 dc->hBitmap = handle;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000451 dc->flags &= ~DC_DIRTY;
452 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
Alexandre Julliardb89525f2004-01-18 22:20:17 +0000453 DC_InitDC( dc );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000454 }
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000455 else ret = 0;
456
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000457 done:
458 GDI_ReleaseObj( hdc );
459 return ret;
460}
461
462
Alexandre Julliard401710d1993-09-04 10:09:32 +0000463/***********************************************************************
Alexandre Julliard7cbe6571995-01-09 18:21:16 +0000464 * BITMAP_DeleteObject
Alexandre Julliard401710d1993-09-04 10:09:32 +0000465 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000466static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000467{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000468 BITMAPOBJ * bmp = obj;
469
470 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
471 bmp->funcs->pDeleteBitmap( handle );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000472
473 if( bmp->bitmap.bmBits )
474 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000475
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000476 if (bmp->dib)
477 {
478 DIBSECTION *dib = bmp->dib;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000479
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000480 if (dib->dsBm.bmBits)
481 {
482 if (dib->dshSection)
483 {
484 SYSTEM_INFO SystemInfo;
485 GetSystemInfo( &SystemInfo );
486 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
487 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
488 }
489 else if (!dib->dsOffset)
490 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
491 }
492 HeapFree(GetProcessHeap(), 0, dib);
493 bmp->dib = NULL;
Andreas Mohr757e7cb2002-05-08 00:20:40 +0000494 if (bmp->segptr_bits)
495 { /* free its selector array */
496 WORD sel = SELECTOROF(bmp->segptr_bits);
497 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
498 int i;
499
500 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
501 }
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000502 }
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000503 return GDI_FreeObject( handle, obj );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000504}
505
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000506
Alexandre Julliard401710d1993-09-04 10:09:32 +0000507/***********************************************************************
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000508 * BITMAP_GetObject16
Alexandre Julliard401710d1993-09-04 10:09:32 +0000509 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000510static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000511{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000512 BITMAPOBJ *bmp = obj;
513
Alexandre Julliard642d3131998-07-12 19:29:36 +0000514 if (bmp->dib)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000515 {
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000516 if ( count <= sizeof(BITMAP16) )
517 {
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000518 BITMAP *bmp32 = &bmp->dib->dsBm;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000519 BITMAP16 bmp16;
520 bmp16.bmType = bmp32->bmType;
521 bmp16.bmWidth = bmp32->bmWidth;
522 bmp16.bmHeight = bmp32->bmHeight;
523 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
524 bmp16.bmPlanes = bmp32->bmPlanes;
525 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000526 bmp16.bmBits = (SEGPTR)0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000527 memcpy( buffer, &bmp16, count );
528 return count;
529 }
530 else
531 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000532 FIXME("not implemented for DIBs: count %d\n", count);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000533 return 0;
534 }
Alexandre Julliarda845b881998-06-01 10:44:35 +0000535 }
536 else
537 {
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000538 BITMAP16 bmp16;
539 bmp16.bmType = bmp->bitmap.bmType;
540 bmp16.bmWidth = bmp->bitmap.bmWidth;
541 bmp16.bmHeight = bmp->bitmap.bmHeight;
542 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
543 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
544 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
545 bmp16.bmBits = (SEGPTR)0;
546 if (count > sizeof(bmp16)) count = sizeof(bmp16);
547 memcpy( buffer, &bmp16, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000548 return count;
549 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000550}
Vincent Béron9a624912002-05-31 23:06:46 +0000551
Alexandre Julliard401710d1993-09-04 10:09:32 +0000552
553/***********************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +0000554 * BITMAP_GetObject
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000555 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000556static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000557{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000558 BITMAPOBJ *bmp = obj;
559
Alexandre Julliard642d3131998-07-12 19:29:36 +0000560 if (bmp->dib)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000561 {
Mike McCormack369116d2003-07-26 20:39:46 +0000562 if( !buffer )
563 return sizeof(DIBSECTION);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000564 if (count < sizeof(DIBSECTION))
565 {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000566 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000567 }
568 else
569 {
570 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
571 }
572
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000573 memcpy( buffer, bmp->dib, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000574 return count;
575 }
576 else
577 {
Mike McCormack369116d2003-07-26 20:39:46 +0000578 if( !buffer )
579 return sizeof(BITMAP);
Alexandre Julliarda3960291999-02-26 11:11:13 +0000580 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000581 memcpy( buffer, &bmp->bitmap, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000582 return count;
583 }
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000584}
Vincent Béron9a624912002-05-31 23:06:46 +0000585
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000586
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000587/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000588 * CreateDiscardableBitmap [GDI32.@]
589 *
590 * Creates a discardable bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000591 *
592 * RETURNS
593 * Success: Handle to bitmap
594 * Failure: NULL
Alexandre Julliard21979011997-03-05 08:22:35 +0000595 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000596HBITMAP WINAPI CreateDiscardableBitmap(
597 HDC hdc, /* [in] Handle to device context */
598 INT width, /* [in] Bitmap width */
599 INT height) /* [in] Bitmap height */
Alexandre Julliard21979011997-03-05 08:22:35 +0000600{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000601 return CreateCompatibleBitmap( hdc, width, height );
Alexandre Julliard36ca1361994-06-02 22:38:20 +0000602}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000603
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000604
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000605/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000606 * GetBitmapDimensionEx [GDI32.@]
607 *
608 * Retrieves dimensions of a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000609 *
610 * RETURNS
611 * Success: TRUE
612 * Failure: FALSE
Alexandre Julliard401710d1993-09-04 10:09:32 +0000613 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000614BOOL WINAPI GetBitmapDimensionEx(
615 HBITMAP hbitmap, /* [in] Handle to bitmap */
616 LPSIZE size) /* [out] Address of struct receiving dimensions */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000617{
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000618 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
619 if (!bmp) return FALSE;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000620 *size = bmp->size;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000621 GDI_ReleaseObj( hbitmap );
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000622 return TRUE;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000623}
624
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000625
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000626/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000627 * SetBitmapDimensionEx [GDI32.@]
628 *
629 * Assigns dimensions to a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000630 *
631 * RETURNS
632 * Success: TRUE
633 * Failure: FALSE
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000634 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000635BOOL WINAPI SetBitmapDimensionEx(
636 HBITMAP hbitmap, /* [in] Handle to bitmap */
637 INT x, /* [in] Bitmap width */
638 INT y, /* [in] Bitmap height */
639 LPSIZE prevSize) /* [out] Address of structure for orig dims */
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000640{
641 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
642 if (!bmp) return FALSE;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000643 if (prevSize) *prevSize = bmp->size;
644 bmp->size.cx = x;
645 bmp->size.cy = y;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000646 GDI_ReleaseObj( hbitmap );
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000647 return TRUE;
648}