Support processors made up of different commands.
[wine/hacks.git] / objects / bitmap.c
blobed6c82d4f787b7966c76cc186f4ebda8eb338d6a
1 /*
2 * GDI bitmap objects
4 * Copyright 1993 Alexandre Julliard
5 * 1998 Huw D M Davies
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.
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.
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
22 #include <stdlib.h>
23 #include <string.h>
25 #include "wine/winbase16.h"
26 #include "wine/winuser16.h"
27 #include "gdi.h"
28 #include "gdi_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(bitmap);
34 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
35 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
36 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
37 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj );
39 static const struct gdi_obj_funcs bitmap_funcs =
41 BITMAP_SelectObject, /* pSelectObject */
42 BITMAP_GetObject16, /* pGetObject16 */
43 BITMAP_GetObject, /* pGetObjectA */
44 BITMAP_GetObject, /* pGetObjectW */
45 NULL, /* pUnrealizeObject */
46 BITMAP_DeleteObject /* pDeleteObject */
49 /***********************************************************************
50 * BITMAP_GetWidthBytes
52 * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
53 * data.
55 static INT BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
57 switch(bpp)
59 case 1:
60 return 2 * ((bmWidth+15) >> 4);
62 case 24:
63 bmWidth *= 3; /* fall through */
64 case 8:
65 return bmWidth + (bmWidth & 1);
67 case 32:
68 return bmWidth * 4;
70 case 16:
71 case 15:
72 return bmWidth * 2;
74 case 4:
75 return 2 * ((bmWidth+3) >> 2);
77 default:
78 WARN("Unknown depth %d, please report.\n", bpp );
80 return -1;
84 /******************************************************************************
85 * CreateBitmap [GDI32.@]
87 * Creates a bitmap with the specified info.
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
96 * RETURNS
97 * Success: Handle to bitmap
98 * Failure: 0
100 HBITMAP WINAPI CreateBitmap( INT width, INT height, UINT planes,
101 UINT bpp, LPCVOID bits )
103 BITMAPOBJ *bmp;
104 HBITMAP hbitmap;
106 planes = (BYTE)planes;
107 bpp = (BYTE)bpp;
110 /* Check parameters */
111 if (!height || !width)
113 height = 1;
114 width = 1;
115 planes = 1;
116 bpp = 1;
118 if (planes != 1) {
119 FIXME("planes = %d\n", planes);
120 return 0;
122 if (height < 0) height = -height;
123 if (width < 0) width = -width;
125 /* Create the BITMAPOBJ */
126 if (!(bmp = GDI_AllocObject( sizeof(BITMAPOBJ), BITMAP_MAGIC,
127 (HGDIOBJ *)&hbitmap, &bitmap_funcs )))
128 return 0;
130 TRACE("%dx%d, %d colors returning %p\n", width, height, 1 << (planes*bpp), hbitmap);
132 bmp->size.cx = 0;
133 bmp->size.cy = 0;
134 bmp->bitmap.bmType = 0;
135 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 );
140 bmp->bitmap.bmBits = NULL;
142 bmp->funcs = NULL;
143 bmp->physBitmap = NULL;
144 bmp->dib = NULL;
145 bmp->segptr_bits = 0;
147 if (bits) /* Set bitmap bits */
148 SetBitmapBits( hbitmap, height * bmp->bitmap.bmWidthBytes,
149 bits );
150 GDI_ReleaseObj( hbitmap );
151 return hbitmap;
155 /******************************************************************************
156 * CreateCompatibleBitmap [GDI32.@]
158 * Creates a bitmap compatible with the DC.
160 * PARAMS
161 * hdc [I] Handle to device context
162 * width [I] Width of bitmap
163 * height [I] Height of bitmap
165 * RETURNS
166 * Success: Handle to bitmap
167 * Failure: 0
169 HBITMAP WINAPI CreateCompatibleBitmap( HDC hdc, INT width, INT height)
171 HBITMAP hbmpRet = 0;
172 DC *dc;
174 TRACE("(%p,%d,%d) = \n", hdc, width, height );
175 if (!(dc = DC_GetDCPtr( hdc ))) return 0;
176 if ((width >= 0x10000) || (height >= 0x10000)) {
177 FIXME("got bad width %d or height %d, please look for reason\n",
178 width, height );
180 else
182 INT planes, bpp;
184 if (GDIMAGIC( dc->header.wMagic ) != MEMORY_DC_MAGIC)
186 planes = GetDeviceCaps( hdc, PLANES );
187 bpp = GetDeviceCaps( hdc, BITSPIXEL );
189 else /* memory DC, get the depth of the bitmap */
191 BITMAPOBJ *bmp = GDI_GetObjPtr( dc->hBitmap, BITMAP_MAGIC );
192 planes = bmp->bitmap.bmPlanes;
193 bpp = bmp->bitmap.bmBitsPixel;
194 GDI_ReleaseObj( dc->hBitmap );
196 hbmpRet = CreateBitmap( width, height, planes, bpp, NULL );
198 TRACE("\t\t%p\n", hbmpRet);
199 GDI_ReleaseObj(hdc);
200 return hbmpRet;
204 /******************************************************************************
205 * CreateBitmapIndirect [GDI32.@]
207 * Creates a bitmap with the specifies info.
209 * RETURNS
210 * Success: Handle to bitmap
211 * Failure: NULL
213 HBITMAP WINAPI CreateBitmapIndirect(
214 const BITMAP * bmp) /* [in] Pointer to the bitmap data */
216 return CreateBitmap( bmp->bmWidth, bmp->bmHeight, bmp->bmPlanes,
217 bmp->bmBitsPixel, bmp->bmBits );
221 /***********************************************************************
222 * GetBitmapBits [GDI32.@]
224 * Copies bitmap bits of bitmap to buffer.
226 * RETURNS
227 * Success: Number of bytes copied
228 * Failure: 0
230 LONG WINAPI GetBitmapBits(
231 HBITMAP hbitmap, /* [in] Handle to bitmap */
232 LONG count, /* [in] Number of bytes to copy */
233 LPVOID bits) /* [out] Pointer to buffer to receive bits */
235 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
236 LONG height, ret;
238 if (!bmp) return 0;
240 /* If the bits vector is null, the function should return the read size */
241 if(bits == NULL)
243 ret = bmp->bitmap.bmWidthBytes * bmp->bitmap.bmHeight;
244 goto done;
247 if (count < 0) {
248 WARN("(%ld): Negative number of bytes passed???\n", count );
249 count = -count;
252 /* Only get entire lines */
253 height = count / bmp->bitmap.bmWidthBytes;
254 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
255 count = height * bmp->bitmap.bmWidthBytes;
256 if (count == 0)
258 WARN("Less than one entire line requested\n");
259 ret = 0;
260 goto done;
264 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
265 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
266 1 << bmp->bitmap.bmBitsPixel, height );
268 if(bmp->funcs)
270 TRACE("Calling device specific BitmapBits\n");
271 if(bmp->funcs->pGetBitmapBits)
272 ret = bmp->funcs->pGetBitmapBits(hbitmap, bits, count);
273 else
275 memset( bits, 0, count );
276 ret = count;
278 } else {
280 if(!bmp->bitmap.bmBits) {
281 WARN("Bitmap is empty\n");
282 ret = 0;
283 } else {
284 memcpy(bits, bmp->bitmap.bmBits, count);
285 ret = count;
289 done:
290 GDI_ReleaseObj( hbitmap );
291 return ret;
295 /******************************************************************************
296 * SetBitmapBits [GDI32.@]
298 * Sets bits of color data for a bitmap.
300 * RETURNS
301 * Success: Number of bytes used in setting the bitmap bits
302 * Failure: 0
304 LONG WINAPI SetBitmapBits(
305 HBITMAP hbitmap, /* [in] Handle to bitmap */
306 LONG count, /* [in] Number of bytes in bitmap array */
307 LPCVOID bits) /* [in] Address of array with bitmap bits */
309 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
310 LONG height, ret;
312 if ((!bmp) || (!bits))
313 return 0;
315 if (count < 0) {
316 WARN("(%ld): Negative number of bytes passed???\n", count );
317 count = -count;
320 /* Only get entire lines */
321 height = count / bmp->bitmap.bmWidthBytes;
322 if (height > bmp->bitmap.bmHeight) height = bmp->bitmap.bmHeight;
323 count = height * bmp->bitmap.bmWidthBytes;
325 TRACE("(%p, %ld, %p) %dx%d %d colors fetched height: %ld\n",
326 hbitmap, count, bits, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
327 1 << bmp->bitmap.bmBitsPixel, height );
329 if(bmp->funcs) {
331 TRACE("Calling device specific BitmapBits\n");
332 if(bmp->funcs->pSetBitmapBits)
333 ret = bmp->funcs->pSetBitmapBits(hbitmap, bits, count);
334 else
335 ret = 0;
336 } else {
338 if(!bmp->bitmap.bmBits) /* Alloc enough for entire bitmap */
339 bmp->bitmap.bmBits = HeapAlloc( GetProcessHeap(), 0, count );
340 if(!bmp->bitmap.bmBits) {
341 WARN("Unable to allocate bit buffer\n");
342 ret = 0;
343 } else {
344 memcpy(bmp->bitmap.bmBits, bits, count);
345 ret = count;
349 GDI_ReleaseObj( hbitmap );
350 return ret;
353 /**********************************************************************
354 * BITMAP_CopyBitmap
357 HBITMAP BITMAP_CopyBitmap(HBITMAP hbitmap)
359 BITMAPOBJ *bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
360 HBITMAP res = 0;
361 BITMAP bm;
363 if(!bmp) return 0;
365 bm = bmp->bitmap;
366 bm.bmBits = NULL;
367 res = CreateBitmapIndirect(&bm);
369 if(res) {
370 char *buf = HeapAlloc( GetProcessHeap(), 0, bm.bmWidthBytes *
371 bm.bmHeight );
372 GetBitmapBits (hbitmap, bm.bmWidthBytes * bm.bmHeight, buf);
373 SetBitmapBits (res, bm.bmWidthBytes * bm.bmHeight, buf);
374 HeapFree( GetProcessHeap(), 0, buf );
377 GDI_ReleaseObj( hbitmap );
378 return res;
382 /***********************************************************************
383 * BITMAP_SetOwnerDC
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.
389 BOOL BITMAP_SetOwnerDC( HBITMAP hbitmap, DC *dc )
391 BITMAPOBJ *bitmap;
392 BOOL ret;
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;
397 if (!(bitmap = GDI_GetObjPtr( hbitmap, BITMAP_MAGIC ))) return FALSE;
399 ret = TRUE;
400 if (!bitmap->funcs) /* not owned by a DC yet */
402 if (dc->funcs->pCreateBitmap) ret = dc->funcs->pCreateBitmap( dc->physDev, hbitmap );
403 if (ret) bitmap->funcs = dc->funcs;
405 else if (bitmap->funcs != dc->funcs)
407 FIXME( "Trying to select bitmap %p in different DC type\n", hbitmap );
408 ret = FALSE;
410 GDI_ReleaseObj( hbitmap );
411 return ret;
415 /***********************************************************************
416 * BITMAP_SelectObject
418 static HGDIOBJ BITMAP_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
420 HGDIOBJ ret;
421 BITMAPOBJ *bitmap = obj;
422 DC *dc = DC_GetDCPtr( hdc );
424 if (!dc) return 0;
425 if (GetObjectType( hdc ) != OBJ_MEMDC)
427 GDI_ReleaseObj( hdc );
428 return 0;
430 ret = dc->hBitmap;
431 if (handle == dc->hBitmap) goto done; /* nothing to do */
433 if (bitmap->header.dwCount && (handle != GetStockObject(DEFAULT_BITMAP)))
435 WARN( "Bitmap already selected in another DC\n" );
436 GDI_ReleaseObj( hdc );
437 return 0;
440 if (!bitmap->funcs && !BITMAP_SetOwnerDC( handle, dc ))
442 GDI_ReleaseObj( hdc );
443 return 0;
446 if (dc->funcs->pSelectBitmap) handle = dc->funcs->pSelectBitmap( dc->physDev, handle );
448 if (handle)
450 dc->hBitmap = handle;
451 dc->flags &= ~DC_DIRTY;
452 SetRectRgn( dc->hVisRgn, 0, 0, bitmap->bitmap.bmWidth, bitmap->bitmap.bmHeight);
453 DC_InitDC( dc );
455 else ret = 0;
457 done:
458 GDI_ReleaseObj( hdc );
459 return ret;
463 /***********************************************************************
464 * BITMAP_DeleteObject
466 static BOOL BITMAP_DeleteObject( HGDIOBJ handle, void *obj )
468 BITMAPOBJ * bmp = obj;
470 if (bmp->funcs && bmp->funcs->pDeleteBitmap)
471 bmp->funcs->pDeleteBitmap( handle );
473 if( bmp->bitmap.bmBits )
474 HeapFree( GetProcessHeap(), 0, bmp->bitmap.bmBits );
476 if (bmp->dib)
478 DIBSECTION *dib = bmp->dib;
480 if (dib->dsBm.bmBits)
482 if (dib->dshSection)
484 SYSTEM_INFO SystemInfo;
485 GetSystemInfo( &SystemInfo );
486 UnmapViewOfFile( (char *)dib->dsBm.bmBits -
487 (dib->dsOffset % SystemInfo.dwAllocationGranularity) );
489 else if (!dib->dsOffset)
490 VirtualFree(dib->dsBm.bmBits, 0L, MEM_RELEASE );
492 HeapFree(GetProcessHeap(), 0, dib);
493 bmp->dib = NULL;
494 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;
500 for (i = 0; i < count; i++) FreeSelector16(sel + (i << __AHSHIFT));
503 return GDI_FreeObject( handle, obj );
507 /***********************************************************************
508 * BITMAP_GetObject16
510 static INT BITMAP_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
512 BITMAPOBJ *bmp = obj;
514 if (bmp->dib)
516 if ( count <= sizeof(BITMAP16) )
518 BITMAP *bmp32 = &bmp->dib->dsBm;
519 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;
526 bmp16.bmBits = (SEGPTR)0;
527 memcpy( buffer, &bmp16, count );
528 return count;
530 else
532 FIXME("not implemented for DIBs: count %d\n", count);
533 return 0;
536 else
538 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 );
548 return count;
553 /***********************************************************************
554 * BITMAP_GetObject
556 static INT BITMAP_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
558 BITMAPOBJ *bmp = obj;
560 if (bmp->dib)
562 if( !buffer )
563 return sizeof(DIBSECTION);
564 if (count < sizeof(DIBSECTION))
566 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
568 else
570 if (count > sizeof(DIBSECTION)) count = sizeof(DIBSECTION);
573 memcpy( buffer, bmp->dib, count );
574 return count;
576 else
578 if( !buffer )
579 return sizeof(BITMAP);
580 if (count > sizeof(BITMAP)) count = sizeof(BITMAP);
581 memcpy( buffer, &bmp->bitmap, count );
582 return count;
587 /******************************************************************************
588 * CreateDiscardableBitmap [GDI32.@]
590 * Creates a discardable bitmap.
592 * RETURNS
593 * Success: Handle to bitmap
594 * Failure: NULL
596 HBITMAP WINAPI CreateDiscardableBitmap(
597 HDC hdc, /* [in] Handle to device context */
598 INT width, /* [in] Bitmap width */
599 INT height) /* [in] Bitmap height */
601 return CreateCompatibleBitmap( hdc, width, height );
605 /******************************************************************************
606 * GetBitmapDimensionEx [GDI32.@]
608 * Retrieves dimensions of a bitmap.
610 * RETURNS
611 * Success: TRUE
612 * Failure: FALSE
614 BOOL WINAPI GetBitmapDimensionEx(
615 HBITMAP hbitmap, /* [in] Handle to bitmap */
616 LPSIZE size) /* [out] Address of struct receiving dimensions */
618 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
619 if (!bmp) return FALSE;
620 *size = bmp->size;
621 GDI_ReleaseObj( hbitmap );
622 return TRUE;
626 /******************************************************************************
627 * SetBitmapDimensionEx [GDI32.@]
629 * Assigns dimensions to a bitmap.
631 * RETURNS
632 * Success: TRUE
633 * Failure: FALSE
635 BOOL 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 */
641 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
642 if (!bmp) return FALSE;
643 if (prevSize) *prevSize = bmp->size;
644 bmp->size.cx = x;
645 bmp->size.cy = y;
646 GDI_ReleaseObj( hbitmap );
647 return TRUE;