blob: d7b5c8d99ce2e74e4e922e9eb3ed8b5904237e0a [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 Julliard8d24ae61994-04-05 21:42:43 +000028#include "bitmap.h"
Alexandre Julliard6ec42c02004-01-15 00:35:38 +000029#include "gdi_private.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000030#include "wine/debug.h"
Alexandre Julliard670cdc41997-08-24 16:00:30 +000031
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000032WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000033
Alexandre Julliard77b99181997-09-14 17:17:23 +000034
Alexandre Julliardd8a92442002-05-31 18:43:22 +000035static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
36static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
38static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
39
40static const struct gdi_obj_funcs bitmap_funcs =
41{
42 BITMAP_SelectObject, /* pSelectObject */
43 BITMAP_GetObject16, /* pGetObject16 */
44 BITMAP_GetObject, /* pGetObjectA */
45 BITMAP_GetObject, /* pGetObjectW */
46 NULL, /* pUnrealizeObject */
47 BITMAP_DeleteObject /* pDeleteObject */
48};
49
Alexandre Julliard77b99181997-09-14 17:17:23 +000050/***********************************************************************
Huw D M Davies82617361998-11-01 16:35:42 +000051 * BITMAP_GetWidthBytes
Alexandre Julliard77b99181997-09-14 17:17:23 +000052 *
Huw D M Davies82617361998-11-01 16:35:42 +000053 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
54 * data.
Alexandre Julliard77b99181997-09-14 17:17:23 +000055 */
Alexandre Julliardaface532002-08-28 22:35:23 +000056static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
Alexandre Julliard77b99181997-09-14 17:17:23 +000057{
58 switch(bpp)
59 {
60 case 1:
61 return 2 * ((bmWidth+15) >> 4);
62
63 case 24:
64 bmWidth *= 3; /* fall through */
65 case 8:
66 return bmWidth + (bmWidth & 1);
67
68 case 32:
69 return bmWidth * 4;
70
71 case 16:
72 case 15:
73 return bmWidth * 2;
74
75 case 4:
76 return 2 * ((bmWidth+3) >> 2);
77
78 default:
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +000079 WARN("Unknown depth %d, please report.\n", bpp );
Alexandre Julliard77b99181997-09-14 17:17:23 +000080 }
81 return -1;
82}
Alexandre Julliard670cdc41997-08-24 16:00:30 +000083
Alexandre Julliard21979011997-03-05 08:22:35 +000084
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000085/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +000086 * CreateBitmap [GDI32.@]
87 *
88 * Creates a bitmap with the specified info.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +000089 *
90 * PARAMS
91 * width [I] bitmap width
92 * height [I] bitmap height
93 * planes [I] Number of color planes
94 * bpp [I] Number of bits to identify a color
95 * bits [I] Pointer to array containing color data
96 *
97 * RETURNS
98 * Success: Handle to bitmap
Huw D M Davies87f87bf1998-10-28 09:53:53 +000099 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000100 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000101HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
102 UINT bpp, LPCVOID bits )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000103{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000104 BITMAPOBJ *bmp;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000105 HBITMAP hbitmap;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000106
Alexandre Julliard7ebe1a41996-12-22 18:27:48 +0000107 planes = (BYTE)planes;
108 bpp = (BYTE)bpp;
109
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000110
111 /* Check parameters */
Erik Inge Bolsø975324f2001-12-04 20:16:25 +0000112 if (!height || !width)
113 {
114 height = 1;
115 width = 1;
116 planes = 1;
117 bpp = 1;
118 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000119 if (planes != 1) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000120 FIXME("planes = %d\n", planes);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000121 return 0;
122 }
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000123 if (height < 0) height = -height;
124 if (width < 0) width = -width;
125
126 /* Create the BITMAPOBJ */
Michael Stefaniuc28a632a2002-11-21 21:50:04 +0000127 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
128 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000129 return 0;
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000130
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000131 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000132
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000133 bmp->size.cx = 0;
134 bmp->size.cy = 0;
135 bmp->bitmap.bmType = 0;
Huw D M Davies82617361998-11-01 16:35:42 +0000136 bmp->bitmap.bmWidth = width;
137 bmp->bitmap.bmHeight = height;
138 bmp->bitmap.bmPlanes = planes;
139 bmp->bitmap.bmBitsPixel = bpp;
140 bmp->bitmap.bmWidthBytes = BITMAP_GetWidthBytes( width, bpp );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000141 bmp->bitmap.bmBits = NULL;
142
Alexandre Julliarda08e2cf2000-03-28 13:37:50 +0000143 bmp->funcs = NULL;
144 bmp->physBitmap = NULL;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000145 bmp->dib = NULL;
Alexandre Julliard6bbc7452001-07-22 23:13:08 +0000146 bmp->segptr_bits = 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000147
148 if (bits) /* Set bitmap bits */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000149 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
Alexandre Julliard21979011997-03-05 08:22:35 +0000150 bits );
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000151 GDI_ReleaseObj( hbitmap );
Alexandre Julliarda2f2e011995-06-06 16:40:35 +0000152 return hbitmap;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000153}
154
155
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000156/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000157 * CreateCompatibleBitmap [GDI32.@]
158 *
159 * Creates a bitmap compatible with the DC.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000160 *
161 * PARAMS
162 * hdc [I] Handle to device context
163 * width [I] Width of bitmap
164 * height [I] Height of bitmap
165 *
166 * RETURNS
167 * Success: Handle to bitmap
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000168 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000169 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000170HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
Alexandre Julliard21979011997-03-05 08:22:35 +0000171{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000172 HBITMAP hbmpRet = 0;
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000173 DC *dc;
174
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000175 TRACE("(%p,%d,%d) = \n", hdc, width, height );
Alexandre Julliard21979011997-03-05 08:22:35 +0000176 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
NF Stevens16f0dfb1999-01-01 18:45:03 +0000177 if ((width >= 0x10000) || (height >= 0x10000)) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000178 FIXME("got bad width %d or height %d, please look for reason\n",
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000179 width, height );
Alexandre Julliardb89525f2004-01-18 22:20:17 +0000180 }
181 else
182 {
183 INT planes, bpp;
184
185 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
186 {
187 planes = GetDeviceCaps( hdc, PLANES );
188 bpp = GetDeviceCaps( hdc, BITSPIXEL );
189 }
190 else /* memory DC, get the depth of the bitmap */
191 {
192 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
193 planes = bmp->bitmap.bmPlanes;
194 bpp = bmp->bitmap.bmBitsPixel;
195 GDI_ReleaseObj( dc->hBitmap );
196 }
197 hbmpRet = CreateBitmap( width, height, planes, bpp, NULL );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000198 }
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000199 TRACE("\t\t%p\n", hbmpRet);
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000200 GDI_ReleaseObj(hdc);
Alexandre Julliard1e9ac791996-06-06 18:38:27 +0000201 return hbmpRet;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000202}
203
204
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000205/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000206 * CreateBitmapIndirect [GDI32.@]
207 *
208 * Creates a bitmap with the specifies info.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000209 *
210 * RETURNS
211 * Success: Handle to bitmap
212 * Failure: NULL
Alexandre Julliard401710d1993-09-04 10:09:32 +0000213 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000214HBITMAP WINAPI CreateBitmapIndirect(
215 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000216{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000217 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
Alexandre Julliard21979011997-03-05 08:22:35 +0000218 bmp->bmBitsPixel, bmp->bmBits );
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000219}
220
221
222/***********************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000223 * GetBitmapBits [GDI32.@]
224 *
225 * Copies bitmap bits of bitmap to buffer.
Vincent Béron9a624912002-05-31 23:06:46 +0000226 *
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000227 * RETURNS
228 * Success: Number of bytes copied
229 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000230 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000231LONG WINAPI GetBitmapBits(
232 HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000233 LONG count, /* [in] Number of bytes to copy */
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000234 LPVOID bits) /* [out] Pointer to buffer to receive bits */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000235{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000236 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
237 LONG height, ret;
Vincent Béron9a624912002-05-31 23:06:46 +0000238
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000239 if (!bmp) return 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000240
Pascal Lessard8903e531999-05-22 10:39:00 +0000241 /* If the bits vector is null, the function should return the read size */
242 if(bits == NULL)
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000243 {
244 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
245 goto done;
246 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000247
Alexandre Julliardade697e1995-11-26 13:59:11 +0000248 if (count < 0) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000249 WARN("(%ld): Negative number of bytes passed???\n", count );
Alexandre Julliardade697e1995-11-26 13:59:11 +0000250 count = -count;
251 }
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000252
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000253 /* Only get entire lines */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000254 height = count / bmp->bitmap.bmWidthBytes;
255 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000256 count = height * bmp->bitmap.bmWidthBytes;
Uwe Bonnes1f43de31998-11-07 12:25:18 +0000257 if (count == 0)
258 {
Francois Gouget05fc3cd2001-01-22 02:22:26 +0000259 WARN("Less than one entire line requested\n");
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000260 ret = 0;
261 goto done;
Uwe Bonnes1f43de31998-11-07 12:25:18 +0000262 }
263
Alexandre Julliard77b99181997-09-14 17:17:23 +0000264
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000265 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000266 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
267 1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard77b99181997-09-14 17:17:23 +0000268
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000269 if(bmp->funcs)
270 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000271 TRACE("Calling device specific BitmapBits\n");
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000272 if(bmp->funcs->pGetBitmapBits)
273 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
274 else
275 {
276 memset( bits, 0, count );
277 ret = count;
278 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000279 } else {
Alexandre Julliard03468f71998-02-15 19:40:49 +0000280
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000281 if(!bmp->bitmap.bmBits) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000282 WARN("Bitmap is empty\n");
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000283 ret = 0;
284 } else {
285 memcpy(bits, bmp->bitmap.bmBits, count);
286 ret = count;
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000287 }
Alexandre Julliard85ed45e1998-08-22 19:03:56 +0000288
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000289 }
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000290 done:
291 GDI_ReleaseObj( hbitmap );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000292 return ret;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000293}
294
295
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000296/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000297 * SetBitmapBits [GDI32.@]
298 *
299 * Sets bits of color data for a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000300 *
301 * RETURNS
302 * Success: Number of bytes used in setting the bitmap bits
303 * Failure: 0
Alexandre Julliard21979011997-03-05 08:22:35 +0000304 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000305LONG WINAPI SetBitmapBits(
306 HBITMAP hbitmap, /* [in] Handle to bitmap */
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000307 LONG count, /* [in] Number of bytes in bitmap array */
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000308 LPCVOID bits) /* [in] Address of array with bitmap bits */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000309{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000310 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
311 LONG height, ret;
Vincent Béron9a624912002-05-31 23:06:46 +0000312
Pascal Lessard8903e531999-05-22 10:39:00 +0000313 if ((!bmp) || (!bits))
314 return 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000315
Alexandre Julliardade697e1995-11-26 13:59:11 +0000316 if (count < 0) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000317 WARN("(%ld): Negative number of bytes passed???\n", count );
Alexandre Julliardade697e1995-11-26 13:59:11 +0000318 count = -count;
319 }
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000320
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000321 /* Only get entire lines */
Alexandre Julliard8d24ae61994-04-05 21:42:43 +0000322 height = count / bmp->bitmap.bmWidthBytes;
323 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000324 count = height * bmp->bitmap.bmWidthBytes;
Alexandre Julliard77b99181997-09-14 17:17:23 +0000325
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000326 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000327 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
328 1 << bmp->bitmap.bmBitsPixel, height );
Alexandre Julliard77b99181997-09-14 17:17:23 +0000329
Alexandre Julliarda08e2cf2000-03-28 13:37:50 +0000330 if(bmp->funcs) {
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000331
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000332 TRACE("Calling device specific BitmapBits\n");
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000333 if(bmp->funcs->pSetBitmapBits)
334 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
335 else
336 ret = 0;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000337 } else {
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000338
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000339 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
340 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
341 if(!bmp->bitmap.bmBits) {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000342 WARN("Unable to allocate bit buffer\n");
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000343 ret = 0;
344 } else {
345 memcpy(bmp->bitmap.bmBits, bits, count);
346 ret = count;
347 }
Alexandre Julliard84c70f51997-05-09 08:40:27 +0000348 }
349
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000350 GDI_ReleaseObj( hbitmap );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000351 return ret;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000352}
353
Alexandre Julliard594997c1995-04-30 10:05:20 +0000354/**********************************************************************
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000355 * BITMAP_CopyBitmap
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000356 *
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000357 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000358HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000359{
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000360 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000361 HBITMAP res = 0;
362 BITMAP bm;
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000363
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000364 if(!bmp) return 0;
365
366 bm = bmp->bitmap;
367 bm.bmBits = NULL;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000368 res = CreateBitmapIndirect(&bm);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000369
370 if(res) {
371 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
372 bm.bmHeight );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000373 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
374 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000375 HeapFree( GetProcessHeap(), 0, buf );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000376 }
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000377
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000378 GDI_ReleaseObj( hbitmap );
Alexandre Julliarda0b2b1d1997-11-16 17:38:29 +0000379 return res;
380}
381
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000382
383/***********************************************************************
384 * BITMAP_SetOwnerDC
385 *
386 * Set the type of DC that owns the bitmap. This is used when the
387 * bitmap is selected into a device to initialize the bitmap function
388 * table.
389 */
390BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
391{
392 BITMAPOBJ *bitmap;
393 BOOL ret;
394
395 /* never set the owner of the stock bitmap since it can be selected in multiple DCs */
396 if (hbitmap == GetStockObject(DEFAULT_BITMAP)) return TRUE;
397
398 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
399
400 ret = TRUE;
401 if (!bitmap->funcs) /* not owned by a DC yet */
402 {
403 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
404 if (ret) bitmap->funcs = dc->funcs;
405 }
406 else if (bitmap->funcs != dc->funcs)
407 {
Alexandre Julliard547cdc22002-11-22 22:16:53 +0000408 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000409 ret = FALSE;
410 }
411 GDI_ReleaseObj( hbitmap );
412 return ret;
413}
414
415
416/***********************************************************************
417 * BITMAP_SelectObject
418 */
419static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
420{
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000421 HGDIOBJ ret;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000422 BITMAPOBJ *bitmap = obj;
423 DC *dc = DC_GetDCPtr( hdc );
424
425 if (!dc) return 0;
Alexandre Julliarde1147ba2003-05-13 23:56:12 +0000426 if (GetObjectType( hdc ) != OBJ_MEMDC)
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000427 {
428 GDI_ReleaseObj( hdc );
429 return 0;
430 }
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000431 ret = dc->hBitmap;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000432 if (handle == dc->hBitmap) goto done; /* nothing to do */
433
434 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
435 {
436 WARN( "Bitmap already selected in another DC\n" );
437 GDI_ReleaseObj( hdc );
438 return 0;
439 }
440
441 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
442 {
443 GDI_ReleaseObj( hdc );
444 return 0;
445 }
446
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000447 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000448
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000449 if (handle)
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000450 {
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000451 dc->hBitmap = handle;
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000452 dc->totalExtent.left = 0;
453 dc->totalExtent.top = 0;
454 dc->totalExtent.right = bitmap->bitmap.bmWidth;
455 dc->totalExtent.bottom = bitmap->bitmap.bmHeight;
456 dc->flags &= ~DC_DIRTY;
457 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
Alexandre Julliardb89525f2004-01-18 22:20:17 +0000458 DC_InitDC( dc );
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000459 }
Alexandre Julliard4227bf42002-06-28 23:33:26 +0000460 else ret = 0;
461
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000462 done:
463 GDI_ReleaseObj( hdc );
464 return ret;
465}
466
467
Alexandre Julliard401710d1993-09-04 10:09:32 +0000468/***********************************************************************
Alexandre Julliard7cbe6571995-01-09 18:21:16 +0000469 * BITMAP_DeleteObject
Alexandre Julliard401710d1993-09-04 10:09:32 +0000470 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000471static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000472{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000473 BITMAPOBJ * bmp = obj;
474
475 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
476 bmp->funcs->pDeleteBitmap( handle );
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000477
478 if( bmp->bitmap.bmBits )
479 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000480
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000481 if (bmp->dib)
482 {
483 DIBSECTION *dib = bmp->dib;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000484
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000485 if (dib->dsBm.bmBits)
486 {
487 if (dib->dshSection)
488 {
489 SYSTEM_INFO SystemInfo;
490 GetSystemInfo( &SystemInfo );
491 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
492 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
493 }
494 else if (!dib->dsOffset)
495 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
496 }
497 HeapFree(GetProcessHeap(), 0, dib);
498 bmp->dib = NULL;
Andreas Mohr757e7cb2002-05-08 00:20:40 +0000499 if (bmp->segptr_bits)
500 { /* free its selector array */
501 WORD sel = SELECTOROF(bmp->segptr_bits);
502 WORD count = (GetSelectorLimit16(sel) / 0x10000) + 1;
503 int i;
504
505 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
506 }
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000507 }
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000508 return GDI_FreeObject( handle, obj );
Alexandre Julliard401710d1993-09-04 10:09:32 +0000509}
510
Alexandre Julliarde21c15e2002-03-28 22:22:05 +0000511
Alexandre Julliard401710d1993-09-04 10:09:32 +0000512/***********************************************************************
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000513 * BITMAP_GetObject16
Alexandre Julliard401710d1993-09-04 10:09:32 +0000514 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000515static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliard401710d1993-09-04 10:09:32 +0000516{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000517 BITMAPOBJ *bmp = obj;
518
Alexandre Julliard642d3131998-07-12 19:29:36 +0000519 if (bmp->dib)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000520 {
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000521 if ( count <= sizeof(BITMAP16) )
522 {
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000523 BITMAP *bmp32 = &bmp->dib->dsBm;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000524 BITMAP16 bmp16;
525 bmp16.bmType = bmp32->bmType;
526 bmp16.bmWidth = bmp32->bmWidth;
527 bmp16.bmHeight = bmp32->bmHeight;
528 bmp16.bmWidthBytes = bmp32->bmWidthBytes;
529 bmp16.bmPlanes = bmp32->bmPlanes;
530 bmp16.bmBitsPixel = bmp32->bmBitsPixel;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000531 bmp16.bmBits = (SEGPTR)0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000532 memcpy( buffer, &bmp16, count );
533 return count;
534 }
535 else
536 {
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000537 FIXME("not implemented for DIBs: count %d\n", count);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000538 return 0;
539 }
Alexandre Julliarda845b881998-06-01 10:44:35 +0000540 }
541 else
542 {
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000543 BITMAP16 bmp16;
544 bmp16.bmType = bmp->bitmap.bmType;
545 bmp16.bmWidth = bmp->bitmap.bmWidth;
546 bmp16.bmHeight = bmp->bitmap.bmHeight;
547 bmp16.bmWidthBytes = bmp->bitmap.bmWidthBytes;
548 bmp16.bmPlanes = bmp->bitmap.bmPlanes;
549 bmp16.bmBitsPixel = bmp->bitmap.bmBitsPixel;
550 bmp16.bmBits = (SEGPTR)0;
551 if (count > sizeof(bmp16)) count = sizeof(bmp16);
552 memcpy( buffer, &bmp16, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000553 return count;
554 }
Alexandre Julliard401710d1993-09-04 10:09:32 +0000555}
Vincent Béron9a624912002-05-31 23:06:46 +0000556
Alexandre Julliard401710d1993-09-04 10:09:32 +0000557
558/***********************************************************************
Patrik Stridvall2d6457c2000-03-28 20:22:59 +0000559 * BITMAP_GetObject
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000560 */
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000561static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000562{
Alexandre Julliardd8a92442002-05-31 18:43:22 +0000563 BITMAPOBJ *bmp = obj;
564
Alexandre Julliard642d3131998-07-12 19:29:36 +0000565 if (bmp->dib)
Alexandre Julliarda845b881998-06-01 10:44:35 +0000566 {
Mike McCormack369116d2003-07-26 20:39:46 +0000567 if( !buffer )
568 return sizeof(DIBSECTION);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000569 if (count < sizeof(DIBSECTION))
570 {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000571 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
Alexandre Julliarda845b881998-06-01 10:44:35 +0000572 }
573 else
574 {
575 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
576 }
577
Patrik Stridvallb87fe2e1999-04-01 08:16:08 +0000578 memcpy( buffer, bmp->dib, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000579 return count;
580 }
581 else
582 {
Mike McCormack369116d2003-07-26 20:39:46 +0000583 if( !buffer )
584 return sizeof(BITMAP);
Alexandre Julliarda3960291999-02-26 11:11:13 +0000585 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000586 memcpy( buffer, &bmp->bitmap, count );
Alexandre Julliarda845b881998-06-01 10:44:35 +0000587 return count;
588 }
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000589}
Vincent Béron9a624912002-05-31 23:06:46 +0000590
Alexandre Julliardd90840e1996-06-11 16:02:08 +0000591
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000592/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000593 * CreateDiscardableBitmap [GDI32.@]
594 *
595 * Creates a discardable bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000596 *
597 * RETURNS
598 * Success: Handle to bitmap
599 * Failure: NULL
Alexandre Julliard21979011997-03-05 08:22:35 +0000600 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000601HBITMAP WINAPI CreateDiscardableBitmap(
602 HDC hdc, /* [in] Handle to device context */
603 INT width, /* [in] Bitmap width */
604 INT height) /* [in] Bitmap height */
Alexandre Julliard21979011997-03-05 08:22:35 +0000605{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000606 return CreateCompatibleBitmap( hdc, width, height );
Alexandre Julliard36ca1361994-06-02 22:38:20 +0000607}
Alexandre Julliard401710d1993-09-04 10:09:32 +0000608
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000609
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000610/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000611 * GetBitmapDimensionEx [GDI32.@]
612 *
613 * Retrieves dimensions of a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000614 *
615 * RETURNS
616 * Success: TRUE
617 * Failure: FALSE
Alexandre Julliard401710d1993-09-04 10:09:32 +0000618 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000619BOOL WINAPI GetBitmapDimensionEx(
620 HBITMAP hbitmap, /* [in] Handle to bitmap */
621 LPSIZE size) /* [out] Address of struct receiving dimensions */
Alexandre Julliard401710d1993-09-04 10:09:32 +0000622{
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000623 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
624 if (!bmp) return FALSE;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000625 *size = bmp->size;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000626 GDI_ReleaseObj( hbitmap );
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000627 return TRUE;
Alexandre Julliard401710d1993-09-04 10:09:32 +0000628}
629
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000630
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000631/******************************************************************************
Jon Griffiths783a3952004-02-09 20:47:42 +0000632 * SetBitmapDimensionEx [GDI32.@]
633 *
634 * Assigns dimensions to a bitmap.
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000635 *
636 * RETURNS
637 * Success: TRUE
638 * Failure: FALSE
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000639 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000640BOOL WINAPI SetBitmapDimensionEx(
641 HBITMAP hbitmap, /* [in] Handle to bitmap */
642 INT x, /* [in] Bitmap width */
643 INT y, /* [in] Bitmap height */
644 LPSIZE prevSize) /* [out] Address of structure for orig dims */
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000645{
646 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
647 if (!bmp) return FALSE;
Huw D M Davies87f87bf1998-10-28 09:53:53 +0000648 if (prevSize) *prevSize = bmp->size;
649 bmp->size.cx = x;
650 bmp->size.cy = y;
Alexandre Julliard2a2321b2000-08-19 21:38:55 +0000651 GDI_ReleaseObj( hbitmap );
Alexandre Julliarde2bfa4c1996-05-16 18:21:06 +0000652 return TRUE;
653}