gdi32: Remove the nb_colors fields in the bitmap object, we always allocate a full...
[wine/multimedia.git] / dlls / gdi32 / brush.c
blob34d3a572be22b29b46fc452172e93c948fdc64f6
1 /*
2 * GDI brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
23 #include <stdarg.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 /* GDI logical brush object */
35 typedef struct
37 GDIOBJHDR header;
38 LOGBRUSH logbrush;
39 HBITMAP bitmap; /* bitmap handle for DDB pattern brushes */
40 BITMAPINFO *info; /* DIB info for pattern brushes */
41 struct gdi_image_bits bits; /* DIB bits for pattern brushes */
42 UINT usage; /* color usage for DIB info */
43 } BRUSHOBJ;
45 #define NB_HATCH_STYLES 6
47 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc );
48 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
49 static BOOL BRUSH_DeleteObject( HGDIOBJ handle );
51 static const struct gdi_obj_funcs brush_funcs =
53 BRUSH_SelectObject, /* pSelectObject */
54 BRUSH_GetObject, /* pGetObjectA */
55 BRUSH_GetObject, /* pGetObjectW */
56 NULL, /* pUnrealizeObject */
57 BRUSH_DeleteObject /* pDeleteObject */
61 /* fetch the contents of the brush bitmap and cache them in the brush object */
62 static BOOL store_bitmap_bits( BRUSHOBJ *brush, BITMAPOBJ *bmp )
64 const struct gdi_dc_funcs *funcs = get_bitmap_funcs( bmp );
65 struct gdi_image_bits bits;
66 struct bitblt_coords src;
67 BITMAPINFO *info;
69 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
70 return FALSE;
72 src.visrect.left = src.x = 0;
73 src.visrect.top = src.y = 0;
74 src.visrect.right = src.width = bmp->bitmap.bmWidth;
75 src.visrect.bottom = src.height = bmp->bitmap.bmHeight;
76 if (funcs->pGetImage( NULL, brush->bitmap, info, &bits, &src ))
78 HeapFree( GetProcessHeap(), 0, info );
79 return FALSE;
82 if (info->bmiHeader.biBitCount <= 8 && !info->bmiHeader.biClrUsed) fill_default_color_table( info );
84 /* release the unneeded space */
85 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info,
86 bitmap_info_size( info, DIB_RGB_COLORS ));
87 brush->info = info;
88 brush->bits = bits;
89 brush->usage = DIB_RGB_COLORS;
90 return TRUE;
93 static BOOL copy_bitmap( BRUSHOBJ *brush, HBITMAP bitmap )
95 BITMAPINFO *info;
96 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
98 if (!bmp) return FALSE;
100 if (!bmp->dib)
102 if ((brush->bitmap = CreateBitmap( bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
103 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel, NULL )))
105 if (bmp->funcs->pCopyBitmap( bitmap, brush->bitmap ))
107 BITMAPOBJ *copy = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
108 copy->funcs = bmp->funcs;
109 GDI_ReleaseObj( copy );
111 else
113 DeleteObject( brush->bitmap );
114 brush->bitmap = 0;
117 GDI_ReleaseObj( bitmap );
118 return brush->bitmap != 0;
121 info = HeapAlloc( GetProcessHeap(), 0,
122 bitmap_info_size( (BITMAPINFO *)&bmp->dib->dsBmih, DIB_RGB_COLORS ));
123 if (!info) goto done;
124 info->bmiHeader = bmp->dib->dsBmih;
125 if (info->bmiHeader.biCompression == BI_BITFIELDS)
126 memcpy( &info->bmiHeader + 1, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
127 else if (info->bmiHeader.biClrUsed)
128 memcpy( &info->bmiHeader + 1, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
129 if (!(brush->bits.ptr = HeapAlloc( GetProcessHeap(), 0, get_dib_image_size( info ))))
131 HeapFree( GetProcessHeap(), 0, info );
132 goto done;
134 memcpy( brush->bits.ptr, bmp->dib->dsBm.bmBits, get_dib_image_size( info ));
135 brush->bits.is_copy = TRUE;
136 brush->bits.free = free_heap_bits;
137 brush->info = info;
138 brush->usage = DIB_RGB_COLORS;
140 done:
141 GDI_ReleaseObj( bitmap );
142 return brush->info != NULL;
145 BOOL get_brush_bitmap_info( HBRUSH handle, BITMAPINFO *info, void **bits, UINT *usage )
147 BRUSHOBJ *brush;
148 BOOL ret = FALSE;
150 if (!(brush = GDI_GetObjPtr( handle, OBJ_BRUSH ))) return FALSE;
152 if (!brush->info)
154 BITMAPOBJ *bmp = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
156 if (bmp)
158 store_bitmap_bits( brush, bmp );
159 GDI_ReleaseObj( brush->bitmap );
162 if (brush->info)
164 memcpy( info, brush->info, bitmap_info_size( brush->info, brush->usage ));
165 *bits = brush->bits.ptr;
166 *usage = brush->usage;
167 ret = TRUE;
169 GDI_ReleaseObj( handle );
170 return ret;
174 /***********************************************************************
175 * CreateBrushIndirect (GDI32.@)
177 * Create a logical brush with a given style, color or pattern.
179 * PARAMS
180 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
182 * RETURNS
183 * A handle to the created brush, or a NULL handle if the brush cannot be
184 * created.
186 * NOTES
187 * - The brush returned should be freed by the caller using DeleteObject()
188 * when it is no longer required.
189 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
190 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
191 * is used.
193 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
195 BRUSHOBJ * ptr;
196 HBRUSH hbrush;
197 HGLOBAL hmem = 0;
199 if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) ))) return 0;
201 ptr->logbrush = *brush;
203 switch (ptr->logbrush.lbStyle)
205 case BS_SOLID:
206 case BS_HOLLOW:
207 case BS_HATCHED:
208 break;
210 case BS_PATTERN8X8:
211 ptr->logbrush.lbStyle = BS_PATTERN;
212 /* fall through */
213 case BS_PATTERN:
214 if (!copy_bitmap( ptr, (HBITMAP)ptr->logbrush.lbHatch )) goto error;
215 ptr->logbrush.lbColor = 0;
216 break;
218 case BS_DIBPATTERN:
219 hmem = (HGLOBAL)ptr->logbrush.lbHatch;
220 if (!(ptr->logbrush.lbHatch = (ULONG_PTR)GlobalLock( hmem ))) goto error;
221 /* fall through */
222 case BS_DIBPATTERNPT:
223 ptr->usage = ptr->logbrush.lbColor;
224 ptr->info = copy_packed_dib( (BITMAPINFO *)ptr->logbrush.lbHatch, ptr->usage );
225 if (hmem) GlobalUnlock( hmem );
226 if (!ptr->info) goto error;
227 ptr->bits.ptr = (char *)ptr->info + bitmap_info_size( ptr->info, ptr->usage );
228 ptr->logbrush.lbStyle = BS_DIBPATTERN;
229 ptr->logbrush.lbColor = 0;
230 break;
232 case BS_DIBPATTERN8X8:
233 case BS_MONOPATTERN:
234 case BS_INDEXED:
235 default:
236 WARN( "invalid brush style %u\n", ptr->logbrush.lbStyle );
237 goto error;
240 if ((hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
242 TRACE("%p\n", hbrush);
243 return hbrush;
246 error:
247 if (ptr->bitmap) DeleteObject( ptr->bitmap );
248 HeapFree( GetProcessHeap(), 0, ptr->info );
249 HeapFree( GetProcessHeap(), 0, ptr );
250 return 0;
254 /***********************************************************************
255 * CreateHatchBrush (GDI32.@)
257 * Create a logical brush with a hatched pattern.
259 * PARAMS
260 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
261 * color [I] Colour of the hatched pattern
263 * RETURNS
264 * A handle to the created brush, or a NULL handle if the brush cannot
265 * be created.
267 * NOTES
268 * - This function uses CreateBrushIndirect() to create the brush.
269 * - The brush returned should be freed by the caller using DeleteObject()
270 * when it is no longer required.
272 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
274 LOGBRUSH logbrush;
276 TRACE("%d %06x\n", style, color );
278 logbrush.lbStyle = BS_HATCHED;
279 logbrush.lbColor = color;
280 logbrush.lbHatch = style;
282 return CreateBrushIndirect( &logbrush );
286 /***********************************************************************
287 * CreatePatternBrush (GDI32.@)
289 * Create a logical brush with a pattern from a bitmap.
291 * PARAMS
292 * hbitmap [I] Bitmap containing pattern for the brush
294 * RETURNS
295 * A handle to the created brush, or a NULL handle if the brush cannot
296 * be created.
298 * NOTES
299 * - This function uses CreateBrushIndirect() to create the brush.
300 * - The brush returned should be freed by the caller using DeleteObject()
301 * when it is no longer required.
303 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
305 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
306 TRACE("%p\n", hbitmap );
308 logbrush.lbHatch = (ULONG_PTR)hbitmap;
309 return CreateBrushIndirect( &logbrush );
313 /***********************************************************************
314 * CreateDIBPatternBrush (GDI32.@)
316 * Create a logical brush with a pattern from a DIB.
318 * PARAMS
319 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
320 * coloruse [I] Specifies color format, if provided
322 * RETURNS
323 * A handle to the created brush, or a NULL handle if the brush cannot
324 * be created.
326 * NOTES
327 * - This function uses CreateBrushIndirect() to create the brush.
328 * - The brush returned should be freed by the caller using DeleteObject()
329 * when it is no longer required.
330 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
331 * be used instead.
333 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
335 LOGBRUSH logbrush;
337 TRACE("%p\n", hbitmap );
339 logbrush.lbStyle = BS_DIBPATTERN;
340 logbrush.lbColor = coloruse;
342 logbrush.lbHatch = (ULONG_PTR)hbitmap;
344 return CreateBrushIndirect( &logbrush );
348 /***********************************************************************
349 * CreateDIBPatternBrushPt (GDI32.@)
351 * Create a logical brush with a pattern from a DIB.
353 * PARAMS
354 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
355 * coloruse [I] Specifies color format, if provided
357 * RETURNS
358 * A handle to the created brush, or a NULL handle if the brush cannot
359 * be created.
361 * NOTES
362 * - This function uses CreateBrushIndirect() to create the brush.
363 * - The brush returned should be freed by the caller using DeleteObject()
364 * when it is no longer required.
366 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
368 const BITMAPINFO *info=data;
369 LOGBRUSH logbrush;
371 if (!data)
372 return NULL;
374 TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
375 info->bmiHeader.biHeight, info->bmiHeader.biBitCount);
377 logbrush.lbStyle = BS_DIBPATTERNPT;
378 logbrush.lbColor = coloruse;
379 logbrush.lbHatch = (ULONG_PTR)data;
381 return CreateBrushIndirect( &logbrush );
385 /***********************************************************************
386 * CreateSolidBrush (GDI32.@)
388 * Create a logical brush consisting of a single colour.
390 * PARAMS
391 * color [I] Colour to make the solid brush
393 * RETURNS
394 * A handle to the newly created brush, or a NULL handle if the brush cannot
395 * be created.
397 * NOTES
398 * - This function uses CreateBrushIndirect() to create the brush.
399 * - The brush returned should be freed by the caller using DeleteObject()
400 * when it is no longer required.
402 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
404 LOGBRUSH logbrush;
406 TRACE("%06x\n", color );
408 logbrush.lbStyle = BS_SOLID;
409 logbrush.lbColor = color;
410 logbrush.lbHatch = 0;
412 return CreateBrushIndirect( &logbrush );
416 /***********************************************************************
417 * SetBrushOrgEx (GDI32.@)
419 * Set the brush origin for a device context.
421 * PARAMS
422 * hdc [I] Device context to set the brush origin for
423 * x [I] New x origin
424 * y [I] New y origin
425 * oldorg [O] If non NULL, destination for previously set brush origin.
427 * RETURNS
428 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
430 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
432 DC *dc = get_dc_ptr( hdc );
434 if (!dc) return FALSE;
435 if (oldorg)
437 oldorg->x = dc->brushOrgX;
438 oldorg->y = dc->brushOrgY;
440 dc->brushOrgX = x;
441 dc->brushOrgY = y;
442 release_dc_ptr( dc );
443 return TRUE;
446 /***********************************************************************
447 * FixBrushOrgEx (GDI32.@)
449 * See SetBrushOrgEx.
451 * NOTES
452 * This function is no longer documented by MSDN, but in Win95 GDI32 it
453 * is the same as SetBrushOrgEx().
455 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
457 return SetBrushOrgEx(hdc,x,y,oldorg);
461 /***********************************************************************
462 * BRUSH_SelectObject
464 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
466 BRUSHOBJ *brush;
467 HGDIOBJ ret = 0;
468 DC *dc = get_dc_ptr( hdc );
470 if (!dc)
472 SetLastError( ERROR_INVALID_HANDLE );
473 return 0;
476 if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
478 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
479 HBITMAP bitmap = brush->bitmap;
480 BITMAPINFO *info;
481 void *bits;
482 UINT usage;
484 if (bitmap && !brush->info)
486 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
487 /* fetch the bitmap bits if we are selecting into a different type of DC */
488 if (bmp && bmp->funcs != physdev->funcs) store_bitmap_bits( brush, bmp );
489 GDI_ReleaseObj( bitmap );
492 info = brush->info;
493 bits = brush->bits.ptr;
494 usage = brush->usage;
495 GDI_inc_ref_count( handle );
496 GDI_ReleaseObj( handle );
498 if (!physdev->funcs->pSelectBrush( physdev, handle, bitmap, info, bits, usage ))
500 GDI_dec_ref_count( handle );
502 else
504 ret = dc->hBrush;
505 dc->hBrush = handle;
506 GDI_dec_ref_count( ret );
509 release_dc_ptr( dc );
510 return ret;
514 /***********************************************************************
515 * BRUSH_DeleteObject
517 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
519 BRUSHOBJ *brush = free_gdi_handle( handle );
521 if (!brush) return FALSE;
522 if (brush->bits.free) brush->bits.free( &brush->bits );
523 if (brush->bitmap) DeleteObject( brush->bitmap );
524 HeapFree( GetProcessHeap(), 0, brush->info );
525 return HeapFree( GetProcessHeap(), 0, brush );
529 /***********************************************************************
530 * BRUSH_GetObject
532 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
534 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
536 if (!brush) return 0;
537 if (buffer)
539 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
540 memcpy( buffer, &brush->logbrush, count );
542 else count = sizeof(brush->logbrush);
543 GDI_ReleaseObj( handle );
544 return count;