- Changed the internal brush object to be (almost) Win32k compatible.
[reactos.git] / reactos / subsys / win32k / dib / dib16bpp.c
1 /*
2 * ReactOS W32 Subsystem
3 * Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id: dib16bpp.c,v 1.22 2004/04/05 21:26:24 navaraf Exp $ */
20 #undef WIN32_LEAN_AND_MEAN
21 #include <windows.h>
22 #include <stdlib.h>
23 #include <math.h>
24 #include <debug.h>
25 #include <ddk/winddi.h>
26 #include <win32k/bitmaps.h>
27 #include <win32k/brush.h>
28 #include <win32k/debug.h>
29 #include <include/object.h>
30 #include "../eng/objects.h"
31 #include "dib.h"
32
33 VOID
34 DIB_16BPP_PutPixel(PSURFOBJ SurfObj, LONG x, LONG y, ULONG c)
35 {
36 PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
37 PWORD addr = (PWORD)byteaddr + x;
38
39 *addr = (WORD)c;
40 }
41
42 ULONG
43 DIB_16BPP_GetPixel(PSURFOBJ SurfObj, LONG x, LONG y)
44 {
45 PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
46 PWORD addr = (PWORD)byteaddr + x;
47
48 return (ULONG)(*addr);
49 }
50
51 VOID
52 DIB_16BPP_HLine(PSURFOBJ SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
53 {
54 PBYTE byteaddr = SurfObj->pvScan0 + y * SurfObj->lDelta;
55 PWORD addr = (PWORD)byteaddr + x1;
56 LONG cx = x1;
57
58 while(cx < x2) {
59 *addr = (WORD)c;
60 ++addr;
61 ++cx;
62 }
63 }
64
65 VOID
66 DIB_16BPP_VLine(PSURFOBJ SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
67 {
68 PBYTE byteaddr = SurfObj->pvScan0 + y1 * SurfObj->lDelta;
69 PWORD addr = (PWORD)byteaddr + x;
70 LONG lDelta = SurfObj->lDelta;
71
72 byteaddr = (PBYTE)addr;
73 while(y1++ < y2) {
74 *addr = (WORD)c;
75
76 byteaddr += lDelta;
77 addr = (PWORD)byteaddr;
78 }
79 }
80
81 BOOLEAN STATIC
82 DIB_16BPP_BitBltSrcCopy(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
83 SURFGDI *DestGDI, SURFGDI *SourceGDI,
84 PRECTL DestRect, POINTL *SourcePoint,
85 XLATEOBJ *ColorTranslation)
86 {
87 LONG i, j, sx, sy, xColor, f1;
88 PBYTE SourceBits, DestBits, SourceLine, DestLine;
89 PBYTE SourceBits_4BPP, SourceLine_4BPP;
90 DestBits = DestSurf->pvScan0 + (DestRect->top * DestSurf->lDelta) + 2 * DestRect->left;
91
92 switch(SourceGDI->BitsPerPixel)
93 {
94 case 1:
95 sx = SourcePoint->x;
96 sy = SourcePoint->y;
97
98 for (j=DestRect->top; j<DestRect->bottom; j++)
99 {
100 sx = SourcePoint->x;
101 for (i=DestRect->left; i<DestRect->right; i++)
102 {
103 if(DIB_1BPP_GetPixel(SourceSurf, sx, sy) == 0)
104 {
105 DIB_16BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 0));
106 } else {
107 DIB_16BPP_PutPixel(DestSurf, i, j, XLATEOBJ_iXlate(ColorTranslation, 1));
108 }
109 sx++;
110 }
111 sy++;
112 }
113 break;
114
115 case 4:
116 SourceBits_4BPP = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + (SourcePoint->x >> 1);
117
118 for (j=DestRect->top; j<DestRect->bottom; j++)
119 {
120 SourceLine_4BPP = SourceBits_4BPP;
121 sx = SourcePoint->x;
122 f1 = sx & 1;
123
124 for (i=DestRect->left; i<DestRect->right; i++)
125 {
126 xColor = XLATEOBJ_iXlate(ColorTranslation,
127 (*SourceLine_4BPP & altnotmask[f1]) >> (4 * (1 - f1)));
128 DIB_16BPP_PutPixel(DestSurf, i, j, xColor);
129 if(f1 == 1) { SourceLine_4BPP++; f1 = 0; } else { f1 = 1; }
130 sx++;
131 }
132
133 SourceBits_4BPP += SourceSurf->lDelta;
134 }
135 break;
136
137 case 8:
138 SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + SourcePoint->x;
139 DestLine = DestBits;
140
141 for (j = DestRect->top; j < DestRect->bottom; j++)
142 {
143 SourceBits = SourceLine;
144 DestBits = DestLine;
145
146 for (i = DestRect->left; i < DestRect->right; i++)
147 {
148 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, *SourceBits);
149 SourceBits += 1;
150 DestBits += 2;
151 }
152
153 SourceLine += SourceSurf->lDelta;
154 DestLine += DestSurf->lDelta;
155 }
156 break;
157
158 case 16:
159 if (NULL == ColorTranslation || 0 != (ColorTranslation->flXlate & XO_TRIVIAL))
160 {
161 if (DestRect->top < SourcePoint->y)
162 {
163 SourceBits = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
164 for (j = DestRect->top; j < DestRect->bottom; j++)
165 {
166 RtlMoveMemory(DestBits, SourceBits, 2 * (DestRect->right - DestRect->left));
167 SourceBits += SourceSurf->lDelta;
168 DestBits += DestSurf->lDelta;
169 }
170 }
171 else
172 {
173 SourceBits = SourceSurf->pvScan0 + ((SourcePoint->y + DestRect->bottom - DestRect->top - 1) * SourceSurf->lDelta) + 2 * SourcePoint->x;
174 DestBits = DestSurf->pvScan0 + ((DestRect->bottom - 1) * DestSurf->lDelta) + 2 * DestRect->left;
175 for (j = DestRect->bottom - 1; DestRect->top <= j; j--)
176 {
177 RtlMoveMemory(DestBits, SourceBits, 2 * (DestRect->right - DestRect->left));
178 SourceBits -= SourceSurf->lDelta;
179 DestBits -= DestSurf->lDelta;
180 }
181 }
182 }
183 else
184 {
185 if (DestRect->top < SourcePoint->y)
186 {
187 SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 2 * SourcePoint->x;
188 DestLine = DestBits;
189 for (j = DestRect->top; j < DestRect->bottom; j++)
190 {
191 SourceBits = SourceLine;
192 DestBits = DestLine;
193 for (i = DestRect->left; i < DestRect->right; i++)
194 {
195 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, *((WORD *)SourceBits));
196 SourceBits += 2;
197 DestBits += 2;
198 }
199 SourceLine += SourceSurf->lDelta;
200 DestLine += DestSurf->lDelta;
201 }
202 }
203 else
204 {
205 SourceLine = SourceSurf->pvScan0 + ((SourcePoint->y + DestRect->bottom - DestRect->top - 1) * SourceSurf->lDelta) + 2 * SourcePoint->x;
206 DestLine = DestSurf->pvScan0 + ((DestRect->bottom - 1) * DestSurf->lDelta) + 2 * DestRect->left;
207 for (j = DestRect->bottom - 1; DestRect->top <= j; j--)
208 {
209 SourceBits = SourceLine;
210 DestBits = DestLine;
211 for (i = DestRect->left; i < DestRect->right; i++)
212 {
213 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, *((WORD *)SourceBits));
214 SourceBits += 2;
215 DestBits += 2;
216 }
217 SourceLine -= SourceSurf->lDelta;
218 DestLine -= DestSurf->lDelta;
219 }
220 }
221 }
222 break;
223
224 case 24:
225 SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 3 * SourcePoint->x;
226 DestLine = DestBits;
227
228 for (j = DestRect->top; j < DestRect->bottom; j++)
229 {
230 SourceBits = SourceLine;
231 DestBits = DestLine;
232
233 for (i = DestRect->left; i < DestRect->right; i++)
234 {
235 xColor = (*(SourceBits + 2) << 0x10) +
236 (*(SourceBits + 1) << 0x08) +
237 (*(SourceBits));
238 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, xColor);
239 SourceBits += 3;
240 DestBits += 2;
241 }
242
243 SourceLine += SourceSurf->lDelta;
244 DestLine += DestSurf->lDelta;
245 }
246 break;
247
248 case 32:
249 SourceLine = SourceSurf->pvScan0 + (SourcePoint->y * SourceSurf->lDelta) + 4 * SourcePoint->x;
250 DestLine = DestBits;
251
252 for (j = DestRect->top; j < DestRect->bottom; j++)
253 {
254 SourceBits = SourceLine;
255 DestBits = DestLine;
256
257 for (i = DestRect->left; i < DestRect->right; i++)
258 {
259 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(ColorTranslation, *((PDWORD) SourceBits));
260 SourceBits += 4;
261 DestBits += 2;
262 }
263
264 SourceLine += SourceSurf->lDelta;
265 DestLine += DestSurf->lDelta;
266 }
267 break;
268
269 default:
270 DbgPrint("DIB_16BPP_Bitblt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
271 return FALSE;
272 }
273
274 return TRUE;
275 }
276
277 BOOLEAN
278 DIB_16BPP_BitBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
279 SURFGDI *DestGDI, SURFGDI *SourceGDI,
280 PRECTL DestRect, POINTL *SourcePoint,
281 PBRUSHOBJ Brush, PPOINTL BrushOrigin,
282 XLATEOBJ *ColorTranslation, ULONG Rop4)
283 {
284 ULONG X, Y;
285 ULONG SourceX, SourceY;
286 ULONG Dest, Source, Pattern = 0;
287 PULONG DestBits;
288 BOOL UsesSource;
289 BOOL UsesPattern;
290 LONG RoundedRight;
291 /* Pattern brushes */
292 PGDIBRUSHOBJ GdiBrush;
293 HBITMAP PatternSurface = NULL;
294 PSURFOBJ PatternObj;
295 ULONG PatternWidth, PatternHeight;
296
297 if (Rop4 == SRCCOPY)
298 {
299 return DIB_16BPP_BitBltSrcCopy(
300 DestSurf,
301 SourceSurf,
302 DestGDI,
303 SourceGDI,
304 DestRect,
305 SourcePoint,
306 ColorTranslation);
307 }
308
309 UsesSource = ((Rop4 & 0xCC0000) >> 2) != (Rop4 & 0x330000);
310 UsesPattern = ((Rop4 & 0xF00000) >> 4) != (Rop4 & 0x0F0000);
311
312 if (UsesPattern)
313 {
314 if (Brush == NULL)
315 {
316 UsesPattern = FALSE;
317 } else
318 if (Brush->iSolidColor == 0xFFFFFFFF)
319 {
320 PBITMAPOBJ PatternBitmap;
321
322 GdiBrush = CONTAINING_RECORD(
323 Brush,
324 GDIBRUSHOBJ,
325 BrushObject);
326
327 PatternBitmap = BITMAPOBJ_LockBitmap(GdiBrush->hbmPattern);
328 PatternSurface = BitmapToSurf(PatternBitmap, NULL);
329 BITMAPOBJ_UnlockBitmap(GdiBrush->hbmPattern);
330
331 PatternObj = (PSURFOBJ)AccessUserObject((ULONG)PatternSurface);
332 PatternWidth = PatternObj->sizlBitmap.cx;
333 PatternHeight = PatternObj->sizlBitmap.cy;
334 }
335 }
336
337 RoundedRight = DestRect->right - ((DestRect->right - DestRect->left) & 0x1);
338 SourceY = SourcePoint->y;
339 DestBits = (PULONG)(
340 DestSurf->pvScan0 +
341 (DestRect->left << 1) +
342 DestRect->top * DestSurf->lDelta);
343
344 for (Y = DestRect->top; Y < DestRect->bottom; Y++)
345 {
346 SourceX = SourcePoint->x;
347 for (X = DestRect->left; X < RoundedRight; X += 2, DestBits++, SourceX += 2)
348 {
349 Dest = *DestBits;
350
351 if (UsesSource)
352 {
353 Source = DIB_GetSource(SourceSurf, SourceGDI, SourceX, SourceY, ColorTranslation);
354 Source |= DIB_GetSource(SourceSurf, SourceGDI, SourceX + 1, SourceY, ColorTranslation) << 16;
355 }
356
357 if (UsesPattern)
358 {
359 if (Brush->iSolidColor == 0xFFFFFFFF)
360 {
361 Pattern = (DIB_1BPP_GetPixel(PatternObj, X % PatternWidth, Y % PatternHeight) ? GdiBrush->crFore : GdiBrush->crBack);
362 Pattern |= (DIB_1BPP_GetPixel(PatternObj, (X + 1) % PatternWidth, Y % PatternHeight) ? GdiBrush->crFore : GdiBrush->crBack) << 16;
363 }
364 else
365 {
366 Pattern = (Brush->iSolidColor & 0xFFFF) |
367 ((Brush->iSolidColor & 0xFFFF) << 16);
368 }
369 }
370
371 *DestBits = DIB_DoRop(Rop4, Dest, Source, Pattern);
372 }
373
374 if (X < DestRect->right)
375 {
376 Dest = *((PUSHORT)DestBits);
377
378 if (UsesSource)
379 {
380 Source = DIB_GetSource(SourceSurf, SourceGDI, SourceX, SourceY, ColorTranslation);
381 }
382
383 if (UsesPattern)
384 {
385 if (Brush->iSolidColor == 0xFFFFFFFF)
386 Pattern = DIB_1BPP_GetPixel(PatternObj, X % PatternWidth, Y % PatternHeight) ? GdiBrush->crFore : GdiBrush->crBack;
387 else
388 Pattern = Brush->iSolidColor & 0xFFFF;
389 }
390
391 DIB_16BPP_PutPixel(DestSurf, X, Y, DIB_DoRop(Rop4, Dest, Source, Pattern) & 0xFFFF);
392 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
393 }
394
395 SourceY++;
396 DestBits = (PULONG)(
397 (ULONG_PTR)DestBits -
398 ((DestRect->right - DestRect->left) << 1) +
399 DestSurf->lDelta);
400 }
401
402 if (PatternSurface != NULL)
403 EngDeleteSurface(PatternSurface);
404
405 return TRUE;
406 }
407
408
409 /*
410 =======================================
411 Stretching functions goes below
412 Some parts of code are based on an
413 article "Bresenhame image scaling"
414 Dr. Dobb Journal, May 2002
415 =======================================
416 */
417
418 typedef unsigned short PIXEL;
419
420 /* 16-bit HiColor (565 format) */
421 inline PIXEL average16(PIXEL a, PIXEL b)
422 {
423 // This one doesn't work
424 /*
425 if (a == b) {
426 return a;
427 } else {
428 unsigned short mask = ~ (((a | b) & 0x0410) << 1);
429 return ((a & mask) + (b & mask)) >> 1;
430 }*/ /* if */
431
432 // This one should be correct, but it's too long
433 /*
434 unsigned char r1, g1, b1, r2, g2, b2, rr, gr, br;
435 unsigned short res;
436
437 r1 = (a & 0xF800) >> 11;
438 g1 = (a & 0x7E0) >> 5;
439 b1 = (a & 0x1F);
440
441 r2 = (b & 0xF800) >> 11;
442 g2 = (b & 0x7E0) >> 5;
443 b2 = (b & 0x1F);
444
445 rr = (r1+r2) / 2;
446 gr = (g1+g2) / 2;
447 br = (b1+b2) / 2;
448
449 res = (rr << 11) + (gr << 5) + br;
450
451 return res;
452 */
453 return a; // FIXME: Depend on SetStretchMode
454 }
455
456 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
457 void ScaleLineAvg16(PIXEL *Target, PIXEL *Source, int SrcWidth, int TgtWidth)
458 {
459 int NumPixels = TgtWidth;
460 int IntPart = SrcWidth / TgtWidth;
461 int FractPart = SrcWidth % TgtWidth;
462 int Mid = TgtWidth >> 1;
463 int E = 0;
464 int skip;
465 PIXEL p;
466
467 skip = (TgtWidth < SrcWidth) ? 0 : (TgtWidth / (2*SrcWidth) + 1);
468 NumPixels -= skip;
469
470 while (NumPixels-- > 0) {
471 p = *Source;
472 if (E >= Mid)
473 p = average16(p, *(Source+1));
474 *Target++ = p;
475 Source += IntPart;
476 E += FractPart;
477 if (E >= TgtWidth) {
478 E -= TgtWidth;
479 Source++;
480 } /* if */
481 } /* while */
482 while (skip-- > 0)
483 *Target++ = *Source;
484 }
485
486 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
487 void ScaleRectAvg16(PIXEL *Target, PIXEL *Source, int SrcWidth, int SrcHeight,
488 int TgtWidth, int TgtHeight, int srcPitch, int dstPitch)
489 {
490 int NumPixels = TgtHeight;
491 int IntPart = ((SrcHeight / TgtHeight) * srcPitch) >> 1; //(SrcHeight / TgtHeight) * SrcWidth;
492 int FractPart = SrcHeight % TgtHeight;
493 int Mid = TgtHeight >> 1;
494 int E = 0;
495 int skip;
496 PIXEL *ScanLine, *ScanLineAhead;
497 PIXEL *PrevSource = NULL;
498 PIXEL *PrevSourceAhead = NULL;
499
500 skip = (TgtHeight < SrcHeight) ? 0 : (TgtHeight / (2*SrcHeight) + 1);
501 NumPixels -= skip;
502
503 ScanLine = (PIXEL*)ExAllocatePool(NonPagedPool, TgtWidth*sizeof(PIXEL)); // FIXME: Should we use PagedPool here?
504 ScanLineAhead = (PIXEL *)ExAllocatePool(NonPagedPool, TgtWidth*sizeof(PIXEL));
505
506 while (NumPixels-- > 0) {
507 if (Source != PrevSource) {
508 if (Source == PrevSourceAhead) {
509 /* the next scan line has already been scaled and stored in
510 * ScanLineAhead; swap the buffers that ScanLine and ScanLineAhead
511 * point to
512 */
513 PIXEL *tmp = ScanLine;
514 ScanLine = ScanLineAhead;
515 ScanLineAhead = tmp;
516 } else {
517 ScaleLineAvg16(ScanLine, Source, SrcWidth, TgtWidth);
518 } /* if */
519 PrevSource = Source;
520 } /* if */
521
522 if (E >= Mid && PrevSourceAhead != (PIXEL *)((BYTE *)Source + srcPitch)) {
523 int x;
524 ScaleLineAvg16(ScanLineAhead, (PIXEL *)((BYTE *)Source + srcPitch), SrcWidth, TgtWidth);
525 for (x = 0; x < TgtWidth; x++)
526 ScanLine[x] = average16(ScanLine[x], ScanLineAhead[x]);
527 PrevSourceAhead = (PIXEL *)((BYTE *)Source + srcPitch);
528 } /* if */
529
530 memcpy(Target, ScanLine, TgtWidth*sizeof(PIXEL));
531 Target = (PIXEL *)((BYTE *)Target + dstPitch);
532 Source += IntPart;
533 E += FractPart;
534 if (E >= TgtHeight) {
535 E -= TgtHeight;
536 Source = (PIXEL *)((BYTE *)Source + srcPitch);
537 } /* if */
538 } /* while */
539
540 if (skip > 0 && Source != PrevSource)
541 ScaleLineAvg16(ScanLine, Source, SrcWidth, TgtWidth);
542 while (skip-- > 0) {
543 memcpy(Target, ScanLine, TgtWidth*sizeof(PIXEL));
544 Target = (PIXEL *)((BYTE *)Target + dstPitch);
545 } /* while */
546
547 ExFreePool(ScanLine);
548 ExFreePool(ScanLineAhead);
549 }
550
551 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
552 BOOLEAN DIB_16BPP_StretchBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
553 SURFGDI *DestGDI, SURFGDI *SourceGDI,
554 RECTL* DestRect, RECTL *SourceRect,
555 POINTL* MaskOrigin, POINTL* BrushOrigin,
556 XLATEOBJ *ColorTranslation, ULONG Mode)
557 {
558 BYTE *SourceLine, *DestLine;
559
560 DbgPrint("DIB_16BPP_StretchBlt: Source BPP: %u, srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
561 SourceGDI->BitsPerPixel, SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
562 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
563
564 switch(SourceGDI->BitsPerPixel)
565 {
566 case 1:
567 return FALSE;
568 break;
569
570 case 4:
571 return FALSE;
572 break;
573
574 case 8:
575 return FALSE;
576 break;
577
578 case 16:
579 SourceLine = SourceSurf->pvScan0 + (SourceRect->top * SourceSurf->lDelta) + 2 * SourceRect->left;
580 DestLine = DestSurf->pvScan0 + (DestRect->top * DestSurf->lDelta) + 2 * DestRect->left;
581
582 ScaleRectAvg16((PIXEL *)DestLine, (PIXEL *)SourceLine,
583 SourceRect->right-SourceRect->left, SourceRect->bottom-SourceRect->top,
584 DestRect->right-DestRect->left, DestRect->bottom-DestRect->top, SourceSurf->lDelta, DestSurf->lDelta);
585 break;
586
587 case 24:
588 return FALSE;
589 break;
590
591 case 32:
592 return FALSE;
593 break;
594
595 default:
596 DbgPrint("DIB_16BPP_StretchBlt: Unhandled Source BPP: %u\n", SourceGDI->BitsPerPixel);
597 return FALSE;
598 }
599
600
601
602 return TRUE;
603 }
604
605 /* EOF */