DIB Engine: introduction of bitmaplist structure
[wine/hacks.git] / dlls / winedib.drv / dibdrvbitmap.c
blobeb7d1e2aff0445f755408d5c6fc28f18beaf1ca9
1 /*
2 * DIB Engine DIBDRVBITMAP handling
4 * Copyright 2009 Massimo Del Fedele
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"
22 #include "wine/port.h"
24 #include "dibdrv.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(dibdrv);
28 /* gets human-readable dib format name */
29 const char *_DIBDRVBITMAP_GetFormatName(DIBDRVBITMAP const *bmp)
31 if(!bmp)
33 ERR("Null bitmap\n");
34 return "NULL BITMAP DETECTED";
36 switch(bmp->format)
38 case DIBFMT_DIB1:
39 return "DIBFMT_DIB1";
40 case DIBFMT_DIB4:
41 return "DIBFMT_DIB4";
42 case DIBFMT_DIB4_RLE:
43 return "DIBFMT_DIB4_RLE";
44 case DIBFMT_DIB8:
45 return "DIBFMT_DIB8";
46 case DIBFMT_DIB8_RLE:
47 return "DIBFMT_DIB8_RLE";
48 case DIBFMT_DIB16_RGB:
49 return "DIBFMT_DIB_RGB";
50 case DIBFMT_DIB16_BITFIELDS:
51 return "DIBFMT_DIB16_BITFIELDS";
52 case DIBFMT_DIB24:
53 return "DIBFMT_DIB24";
54 case DIBFMT_DIB32_RGB:
55 return "DIBFMT_DIB32_RGB";
56 case DIBFMT_DIB32_BITFIELDS:
57 return "DIBFMT_DIB32_BITFIELDS";
58 case DIBFMT_UNKNOWN:
59 default:
60 return "DIBFMT_UNKNOWN";
64 /* calculates shift and length given a bit mask */
65 static void CalcShiftAndLen(DWORD mask, int *shift, int *len)
67 int s, l;
69 /* FIXME----*/
70 if(mask == 0)
72 FIXME("color mask == 0 -- problem on init_dib\n");
73 *shift = 0;
74 *len = 0;
75 return;
78 /* calculates bit shift
79 (number of 0's on right of bit field */
80 s = 0;
81 while ((mask & 1) == 0)
83 mask >>= 1;
84 s++;
87 /* calculates bitfield length
88 (number of 1's in bit field */
89 l = 0;
90 while ((mask & 1) == 1)
92 mask >>= 1;
93 l++;
95 *shift = s;
96 *len = l;
99 /* initializes bit fields from bit masks */
100 static void InitBitFields(DIBDRVBITMAP *dib, const DWORD *bit_fields)
102 dib->redMask = bit_fields[0];
103 dib->greenMask = bit_fields[1];
104 dib->blueMask = bit_fields[2];
105 CalcShiftAndLen(dib->redMask, &dib->redShift, &dib->redLen);
106 CalcShiftAndLen(dib->greenMask, &dib->greenShift, &dib->greenLen);
107 CalcShiftAndLen(dib->blueMask, &dib->blueShift, &dib->blueLen);
110 /* sets/gets bits of a DIBDRVBITMAP taking in account if it's a top down
111 or a bottom-up DIB */
112 void _DIBDRVBITMAP_Set_Bits(DIBDRVBITMAP *dib, void *bits, BOOL owns)
114 /* checks whether dib is top-down or bottom-up one */
115 if(dib->stride > 0)
117 /* top-down dib */
118 dib->bits = bits;
120 else
122 /* bottom-up dib */
123 /* data->bits always points to the top-left corner and the stride is -ve */
124 dib->bits = (BYTE*)bits - (dib->height - 1) * dib->stride;
126 dib->ownsBits = owns;
129 void *_DIBDRVBITMAP_Get_Bits(DIBDRVBITMAP * dib)
131 /* checks whether dib is top-down or bottom-up one */
132 if(dib->stride > 0)
134 /* top-down dib */
135 return dib->bits;
137 else
139 /* bottom-up dib */
140 /* data->bits always points to the top-left corner and the stride is -ve */
141 return (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
145 /* calculates and sets the lightest color for monochrome bitmaps */
146 int _DIBDRVBITMAP_GetLightestColorIndex(DIBDRVBITMAP *dib)
148 DWORD foreRed, foreGreen, foreBlue;
149 DWORD backRed, backGreen, backBlue;
150 RGBQUAD *fore, *back;
152 /* zero for non-monochrome bitmaps */
153 if(dib->bitCount != 1)
154 return 0;
155 /* just in case color table hasn't been grabbed yet */
156 if(!dib->colorTableGrabbed)
157 return 1;
158 back = dib->colorTable;
159 fore = back + 1;
160 foreRed = fore->rgbRed; foreGreen = fore->rgbGreen; foreBlue = fore->rgbBlue;
161 backRed = back->rgbRed; backGreen = back->rgbGreen; backBlue = back->rgbBlue;
162 if(foreRed*foreRed + foreGreen*foreGreen + foreBlue*foreBlue >
163 backRed*backRed + backGreen*backGreen + backBlue*backBlue)
165 dib->lightColor = 1;
166 return 1;
168 dib->lightColor = 0;
169 return 0;
172 /* initializes dib from a bitmap :
173 dib dib being initialized
174 bi source BITMAPINFOHEADER with required DIB format info
175 bit_fields color masks
176 colorTable color table, if any
177 bits pointer to image data array
178 NOTE : DIBDRVBITMAP doesn't owns bits, but do own color table
180 BOOL _DIBDRVBITMAP_InitFromBMIH(DIBDRVBITMAP *dib, const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
181 const RGBQUAD *colorTable, void *bits)
183 MAYBE(TRACE("dib=%p, bi=%p, bit_fields=%p, colorTable=%p, bits=%p\n", dib, bi, bit_fields, colorTable, bits));
185 /* initializes DIB dimensions and color depth */
186 dib->bitCount = bi->biBitCount;
187 dib->width = bi->biWidth;
188 dib->height = bi->biHeight;
189 dib->stride = ((dib->width * dib->bitCount + 31) >> 3) & ~3;
191 /* initializes image data pointer */
192 dib->bits = bits;
193 dib->ownsBits = FALSE;
195 /* initializes color table */
196 dib->colorTableSize = 0;
197 dib->colorTable = NULL;
198 dib->colorTableGrabbed = FALSE;
200 /* checks whether dib is top-down or bottom-up one */
201 if(dib->height < 0)
203 /* top-down dib */
204 dib->height = -dib->height;
206 else
208 /* bottom-up dib */
209 /* data->bits always points to the top-left corner and the stride is -ve */
210 dib->bits = (BYTE*)dib->bits + (dib->height - 1) * dib->stride;
211 dib->stride = -dib->stride;
214 /* gets and stores bitmap format */
215 switch(dib->bitCount)
217 case 24:
218 dib->format = DIBFMT_DIB24;
219 dib->funcs = &DIBDRV_funcs_DIB24;
220 break;
222 case 32:
224 if(bi->biCompression == BI_RGB)
226 dib->format = DIBFMT_DIB32_RGB;
227 dib->funcs = &DIBDRV_funcs_DIB32_RGB;
229 else
231 InitBitFields(dib, bit_fields);
232 dib->format = DIBFMT_DIB32_BITFIELDS;
233 dib->funcs = &DIBDRV_funcs_DIB32_BITFIELDS;
235 break;
237 case 16:
238 if(bi->biCompression == BI_RGB)
240 dib->format = DIBFMT_DIB16_RGB;
241 dib->funcs = &DIBDRV_funcs_DIB16_RGB;
243 else
245 InitBitFields(dib, bit_fields);
246 dib->format = DIBFMT_DIB16_BITFIELDS;
247 dib->funcs = &DIBDRV_funcs_DIB16_BITFIELDS;
249 break;
251 case 8:
252 dib->format = DIBFMT_DIB8;
253 dib->funcs = &DIBDRV_funcs_DIB8;
254 dib->colorTableSize = 256;
255 if(bi->biClrUsed) dib->colorTableSize = bi->biClrUsed;
256 break;
258 case 4:
259 dib->format = DIBFMT_DIB4;
260 dib->funcs = &DIBDRV_funcs_DIB4;
261 dib->colorTableSize = 16;
262 if(bi->biClrUsed) dib->colorTableSize = bi->biClrUsed;
263 break;
265 case 1:
266 dib->format = DIBFMT_DIB1;
267 dib->funcs = &DIBDRV_funcs_DIB1;
268 dib->colorTableSize = 2;
269 if(bi->biClrUsed) dib->colorTableSize = bi->biClrUsed;
270 break;
272 default:
273 dib->format = DIBFMT_UNKNOWN;
274 dib->funcs = NULL;
275 FIXME("bpp %d not supported\n", dib->bitCount);
276 return FALSE;
278 MAYBE(TRACE("DIB FORMAT : %s\n", _DIBDRVBITMAP_GetFormatName(dib)));
280 /* allocates color table and copy it from source, *if* source is
281 not null */
282 if(dib->colorTableSize && colorTable)
284 if(!(dib->colorTable = HeapAlloc(GetProcessHeap(), 0,
285 dib->colorTableSize * sizeof(dib->colorTable[0]))
288 ERR("HeapAlloc failed\n");
289 return FALSE;
291 memcpy(dib->colorTable, colorTable,
292 dib->colorTableSize * sizeof(dib->colorTable[0]));
293 dib->colorTableGrabbed = TRUE;
295 /* for monochrome bitmaps, we need the 'lightest' color */
296 _DIBDRVBITMAP_GetLightestColorIndex(dib);
298 else if(!dib->colorTableSize)
299 /* no color table on more than 8 bits/pixel */
300 dib->colorTableGrabbed = TRUE;
302 MAYBE(TRACE("END\n"));
303 return TRUE;
306 DIBDRVBITMAP *_DIBDRVBITMAP_CreateFromBMIH(const BITMAPINFOHEADER *bi, const DWORD *bit_fields,
307 const RGBQUAD *colorTable, void *bits)
309 DIBDRVBITMAP *bmp = _DIBDRVBITMAP_New();
310 if(bmp && !_DIBDRVBITMAP_InitFromBMIH(bmp, bi, bit_fields, colorTable, bits))
312 _DIBDRVBITMAP_Free(bmp);
313 bmp = NULL;
315 return bmp;
318 /* gets a BITMAPINFOHEADER from a soure BITMAPINFO- or BITMAPCORE-header */
319 static BITMAPINFOHEADER *GetBitmapInfoHeader(BITMAPINFO const *bmi)
321 BITMAPINFOHEADER *res = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(BITMAPINFOHEADER));
323 int size = bmi->bmiHeader.biSize;
324 if(size >= sizeof(BITMAPINFOHEADER))
326 memcpy(res, bmi, sizeof(BITMAPINFOHEADER));
327 res->biSize = sizeof(BITMAPINFOHEADER);
329 else if(size == sizeof(BITMAPCOREHEADER))
331 BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)bmi;
332 res->biSize = sizeof(BITMAPINFOHEADER);
333 res->biWidth = core->bcWidth;
334 res->biHeight = core->bcHeight;
335 res->biPlanes = core->bcPlanes;
336 res->biBitCount = core->bcBitCount;
338 else
340 HeapFree(GetProcessHeap(), 0, res);
341 ERR("Bad/unknown header size %d\n", size);
342 res = NULL;
344 return res;
347 BOOL _DIBDRVBITMAP_InitFromBitmapinfo(DIBDRVBITMAP *dib, const BITMAPINFO *bmi, void *bits)
349 static const DWORD bit_fields_DIB32_RGB[3] = {0xff0000, 0x00ff00, 0x0000ff};
350 static const DWORD bit_fields_DIB16_RGB[3] = {0x7c00, 0x03e0, 0x001f};
351 const DWORD *masks = NULL;
352 RGBQUAD *colorTable = NULL;
353 BITMAPINFOHEADER *bi;
354 BYTE *ptr;
355 int num_colors;
356 BOOL res;
358 /* gets info header */
359 if(!(bi = GetBitmapInfoHeader(bmi)))
360 return FALSE;
362 ptr = (BYTE*)bmi + bmi->bmiHeader.biSize;
363 num_colors = bi->biClrUsed;
365 MAYBE(TRACE("dib=%p, bmi=%p\n", dib, bmi));
367 if(bi->biCompression == BI_BITFIELDS)
369 masks = (DWORD *)ptr;
370 ptr += 3 * sizeof(DWORD);
372 else if(bi->biBitCount == 32)
373 masks = bit_fields_DIB32_RGB;
374 else if(bi->biBitCount == 16)
375 masks = bit_fields_DIB16_RGB;
377 if(!num_colors && bi->biBitCount <= 8)
378 num_colors = 1 << bi->biBitCount;
379 if(num_colors)
380 colorTable = (RGBQUAD*)ptr;
381 ptr += num_colors * sizeof(*colorTable);
383 res = _DIBDRVBITMAP_InitFromBMIH(dib, bi, masks, colorTable, bits ? bits : ptr);
384 HeapFree(GetProcessHeap(), 0, bi);
385 MAYBE(TRACE("END\n"));
386 return res;
389 DIBDRVBITMAP *_DIBDRVBITMAP_CreateFromBitmapinfo(const BITMAPINFO *bmi, void *bits)
391 DIBDRVBITMAP *bmp = _DIBDRVBITMAP_New();
392 if(bmp && !_DIBDRVBITMAP_InitFromBitmapinfo(bmp, bmi, bits))
394 _DIBDRVBITMAP_Free(bmp);
395 bmp = NULL;
397 return bmp;
400 /* initializes a DIBRDVBITMAP copying it from a source one
401 Parameters :
402 dib destination DIBDRVBITMAP
403 src source DIBDRVBITMAP
404 copy TRUE->copy source pixel array FALSE->link to source pixel array
406 BOOL _DIBDRVBITMAP_InitFromDibdrvbitmap(DIBDRVBITMAP *dib, const DIBDRVBITMAP *src, BOOL copy)
408 MAYBE(TRACE("dib=%p, src=%p, copy=%d\n", dib, src, copy));
410 dib->format = src->format;
411 dib->width = src->width;
412 dib->height = src->height;
413 dib->stride = src->stride;
414 dib->bitCount = src->bitCount;
416 dib->redMask = src->redMask;
417 dib->greenMask = src->greenMask;
418 dib->blueMask = src->blueMask;
419 dib->redShift = src->redShift;
420 dib->greenShift = src->greenShift;
421 dib->blueShift = src->blueShift;
422 dib->redLen = src->redLen;
423 dib->greenLen = src->greenLen;
424 dib->blueLen = src->blueLen;
426 dib->funcs = src->funcs;
428 dib->lightColor = src->lightColor;
430 if(copy)
432 int size = dib->height*abs(dib->stride);
433 if(!(dib->bits = HeapAlloc(GetProcessHeap(), 0, size)))
435 ERR("Failed to allocate bits buffer\n");
436 return FALSE;
438 dib->ownsBits = TRUE;
440 /* check for bottom-up DIB */
441 if(dib->stride < 0)
443 /* copy the bitmap array */
444 memcpy(dib->bits, (BYTE *)src->bits + (src->height - 1) * src->stride, size);
446 dib->bits = (BYTE *)dib->bits - (dib->height-1) * dib->stride;
448 else
450 /* copy the bitmap array */
451 memcpy(dib->bits, src->bits, size);
454 else
456 dib->bits = src->bits;
457 dib->ownsBits = FALSE;
460 if(src->colorTable)
462 dib->colorTable = HeapAlloc(GetProcessHeap(), 0, src->colorTableSize * sizeof(src->colorTable[0]));
463 memcpy(dib->colorTable, src->colorTable, src->colorTableSize * sizeof(src->colorTable[0]));
465 else
466 dib->colorTable = NULL;
467 dib->colorTableSize = src->colorTableSize;
468 dib->colorTableGrabbed = TRUE;
469 MAYBE(TRACE("END\n"));
470 return TRUE;
474 /* creates a DIBRDVBITMAP copying format info from a source one
475 Parameters :
476 dib destination DIBDRVBITMAP
477 src source DIBDRVBITMAP
478 widht, height sizes of newly created bitmap
480 BOOL _DIBDRVBITMAP_CreateFromDibdrvbitmap(DIBDRVBITMAP *dib, const DIBDRVBITMAP *src, int width, int height)
482 MAYBE(TRACE("dib=%p, src=%p, width=%d, height=%d\n", dib, src, width, height));
484 /* grab color and format info from source DIB */
485 if(!_DIBDRVBITMAP_InitFromDibdrvbitmap(dib, src, FALSE))
487 ERR("Failed grabbing source dib format\n");
488 return FALSE;
491 /* sets up new DIB dimensions */
492 dib->width = width;
493 dib->height = height;
495 /* calculates new stride basing of new width */
496 dib->stride = ((width * dib->bitCount +31) &~31) / 8;
497 if(src->stride < 0)
498 dib->stride = -dib->stride;
500 /* allocates bits for newly created DIB */
501 if(!(dib->bits = HeapAlloc(GetProcessHeap(), 0, height*abs(dib->stride))))
503 ERR("Failed to allocate bits buffer\n");
504 return FALSE;
506 /* check for bottom-up DIB */
507 if(dib->stride < 0)
508 dib->bits = (BYTE *)dib->bits - (dib->height-1) * dib->stride;
509 dib->ownsBits = TRUE;
511 MAYBE(TRACE("END\n"));
512 return TRUE;
515 /* Clears a DIBDRVBITMAP structure data
516 WARNING : doesn't free anything */
517 void _DIBDRVBITMAP_Clear(DIBDRVBITMAP *bmp)
519 MAYBE(TRACE("bmp=%p\n", bmp));
521 if(!bmp)
522 return;
523 bmp->bits = NULL;
524 bmp->ownsBits = FALSE;
525 bmp->colorTable = NULL;
526 bmp->colorTableSize = 0;
527 bmp->colorTableGrabbed = FALSE;
529 MAYBE(TRACE("END\n"));
532 /* allocates a new DIBDTVBITMAP */
533 DIBDRVBITMAP *_DIBDRVBITMAP_New(void)
535 DIBDRVBITMAP *bmp = HeapAlloc(GetProcessHeap(), 0, sizeof(DIBDRVBITMAP));
536 if(!bmp)
537 return NULL;
538 _DIBDRVBITMAP_Clear(bmp);
539 return bmp;
542 /* Frees a DIBDRVBITMAP structure data */
543 void _DIBDRVBITMAP_Free(DIBDRVBITMAP *bmp)
545 MAYBE(TRACE("bmp=%p\n", bmp));
547 if(!bmp)
548 return;
549 /* frees bits, if needed */
550 if(bmp->bits && bmp->ownsBits)
552 /* on bottom-up dibs, bits doesn't point to starting
553 of buffer.... bad design choice */
554 if(bmp->stride < 0)
555 bmp->bits = (BYTE *)bmp->bits + bmp->stride * (bmp->height -1);
556 HeapFree(GetProcessHeap(), 0, bmp->bits);
558 /* frees color table */
559 if(bmp->colorTable)
560 HeapFree(GetProcessHeap(), 0, bmp->colorTable);
562 HeapFree(GetProcessHeap(), 0, bmp);
567 /* checks whether the format of 2 DIBs are identical
568 it checks the pixel bit count and the color table size
569 and content, if needed */
570 BOOL _DIBDRVBITMAP_FormatMatch(const DIBDRVBITMAP *d1, const DIBDRVBITMAP *d2)
572 /* checks at first the format (bit count and color masks) */
573 if(d1->format != d2->format)
574 return FALSE;
576 /* formats matches, now checks color tables if needed */
577 switch(d1->format)
579 case DIBFMT_DIB32_RGB :
580 case DIBFMT_DIB32_BITFIELDS :
581 case DIBFMT_DIB24 :
582 case DIBFMT_DIB16_RGB :
583 case DIBFMT_DIB16_BITFIELDS :
584 return TRUE;
586 case DIBFMT_DIB1 :
587 case DIBFMT_DIB4 :
588 /*case DIBFMT_DIB4_RLE :*/
589 case DIBFMT_DIB8 :
590 /*case DIBFMT_DIB8_RLE :*/
591 if(d1->colorTableSize != d2->colorTableSize)
592 return FALSE;
593 return !memcmp(d1->colorTable, d2->colorTable, d1->colorTableSize * sizeof(d1->colorTable[0]));
595 default:
596 ERR("Unexpected depth %d\n", d1->bitCount);
597 return FALSE;
601 /* convert a given dib into another format given by 'format' parameter */
602 BOOL _DIBDRVBITMAP_Convert(DIBDRVBITMAP *dst, const DIBDRVBITMAP *src, const DIBDRVBITMAP *format)
604 int width, height;
605 int iLine;
606 void *buf;
607 BOOL res;
609 MAYBE(TRACE("dst=%p, src=%p, format=%p\n", dst, src, format));
611 /* free, if needed, destination bitmap */
612 _DIBDRVBITMAP_Free(dst);
614 /* if format and source bitmaps format match,
615 just copy source on destination */
616 if(_DIBDRVBITMAP_FormatMatch(src, format))
618 res = _DIBDRVBITMAP_InitFromDibdrvbitmap(dst, src, TRUE);
619 MAYBE(TRACE("END - Identical formats\n"));
620 return res;
623 /* formats don't match, we create the dest bitmap with same format as format's one
624 but with source's one dimensions */
625 width = src->width;
626 height = src->height;
627 if(!_DIBDRVBITMAP_CreateFromDibdrvbitmap(dst, format, width, height))
629 ERR("Couldn't create destination bmp\n");
630 return FALSE;
633 /* we now copy/convert from source to dest */
634 if(!(buf = HeapAlloc(GetProcessHeap(), 0, width * 4)))
636 ERR("HeapAlloc failed\n");
637 return FALSE;
640 for(iLine = 0; iLine < height; iLine++)
642 src->funcs->GetLine(src, iLine, 0, width, buf);
643 dst->funcs->PutLine(dst, iLine, 0, width, buf);
645 HeapFree(GetProcessHeap(), 0, buf);
647 MAYBE(TRACE("END - different formats\n"));
648 return TRUE;
651 /* creates a solid-filled DIB of given color and format
652 DIB format is given by 'format' parameter */
653 BOOL _DIBDRVBITMAP_CreateSolid(DIBDRVBITMAP *bmp, DIBDRVBITMAP *format, int width, int height, DWORD Color)
655 DWORD *buf, *bufPnt;
656 int i;
658 MAYBE(TRACE("bmp=%p, format=%p, width=%d, height=%d, Color=%08x\n", bmp, format, width, height, Color));
660 /* swaps color bytes....*/
661 Color = RGB((Color >> 8) & 0xff, (Color >> 16) &0xff, Color &0xff);
663 /* creates the destination bitmap */
664 if(!_DIBDRVBITMAP_CreateFromDibdrvbitmap(bmp, format, width, height))
666 ERR("Couldn't create destination bmp\n");
667 return FALSE;
670 /* creates a temporary line filled with given color */
671 if(!(buf = HeapAlloc(GetProcessHeap(), 0, width * 4)))
673 ERR("HeapAlloc failed\n");
674 return FALSE;
677 for(i = 0, bufPnt = buf; i < width; i++)
678 *bufPnt++ = Color;
680 /* fills the bitmap */
681 for(i = 0; i < height; i++)
682 bmp->funcs->PutLine(bmp, i, 0, width, buf);
684 /* frees temporaty line */
685 HeapFree(GetProcessHeap(), 0, buf);
687 MAYBE(TRACE("END\n"));
688 return TRUE;
691 /* expands horizontally a bitmap to reach a minimum size,
692 keeping its width as a multiple of a base width
693 Used to widen brushes in order to optimize blitting */
694 BOOL _DIBDRVBITMAP_ExpandHoriz(DIBDRVBITMAP *dib, int baseWidth, int minWidth)
696 BYTE *srcBuf, *dstBuf;
697 int chunkSize;
698 int iLine, iCol;
699 DIBDRVBITMAP tmpDib;
700 void *bits;
701 BOOL ownsBits;
703 MAYBE(TRACE("dib=%p, baseWidth=%d, minWidth=%d\n", dib, baseWidth, minWidth));
705 /* if dst dib already wide enough, just do nothing */
706 if(dib->width >= minWidth)
708 MAYBE(TRACE("END - No need to expand\n"));
709 return TRUE;
712 /* source DIB can't be NULL */
713 if(!dib->bits)
715 ERR("Empty source DIB detected\n");
716 return FALSE;
719 /* round up minWidth to be a multiple of source width */
720 minWidth += (baseWidth - (minWidth % baseWidth));
722 /* creates a temporary destination bitmap with required sizes */
723 _DIBDRVBITMAP_Clear(&tmpDib);
724 if(!_DIBDRVBITMAP_CreateFromDibdrvbitmap(&tmpDib, dib, minWidth, dib->height))
726 ERR("Couldn't create the temporary DIB for brush cache\n");
727 return FALSE;
730 /* if format uses almost 1 byte/pixel, fast copy path */
731 if(dib->bitCount >= 8)
733 chunkSize = dib->width * dib->bitCount / 8;
734 for(iLine = 0; iLine < dib->height; iLine++)
736 srcBuf = (BYTE *)dib->bits + iLine * dib->stride;
737 dstBuf = (BYTE *)tmpDib.bits + iLine * tmpDib.stride;
738 for(iCol = 0; iCol < tmpDib.width; iCol += dib->width)
740 memcpy(dstBuf, srcBuf, chunkSize);
741 dstBuf += chunkSize;
745 /* otherwise slow path -- could be optimized */
746 else
748 chunkSize = dib->width * 4;
749 /* allocates a line buffer */
750 if(!(srcBuf = HeapAlloc(GetProcessHeap(), 0, tmpDib.width * 4)))
752 ERR("HeapAlloc failed\n");
753 return FALSE;
756 FIXME("dib:format=%s, funcs=%p, bits=%p, width=%d, height=%d, stride=%d\n",
757 _DIBDRVBITMAP_GetFormatName(dib), dib->funcs, dib->bits, dib->width, dib->height, dib->stride);
758 for(iLine = 0; iLine < dib->height; iLine++)
760 /* fills the line buffer repeating source's line data */
761 dib->funcs->GetLine(dib, iLine, 0, dib->width, srcBuf);
762 dstBuf = srcBuf + chunkSize;
763 for(iCol = dib->width; iCol < tmpDib.width; iCol += dib->width)
765 memcpy(dstBuf, srcBuf, chunkSize);
766 dstBuf += chunkSize;
768 /* stores the line on destination bmp */
769 tmpDib.funcs->PutLine(&tmpDib, iLine, 0, tmpDib.width, srcBuf);
771 HeapFree(GetProcessHeap(), 0, srcBuf);
774 /* swaps temp DIB and source one */
775 bits = dib->bits;
776 ownsBits = dib->ownsBits;
777 dib->bits = tmpDib.bits;
778 dib->ownsBits = tmpDib.ownsBits;
779 tmpDib.bits = bits;
780 tmpDib.ownsBits = ownsBits;
782 /* frees the temporary DIB */
783 _DIBDRVBITMAP_Free(&tmpDib);
785 MAYBE(TRACE("END\n"));
786 return TRUE;