Sync to trunk head (35333)
[reactos.git] / reactos / subsystems / win32 / 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
20 #include <w32k.h>
21
22 #define NDEBUG
23 #include <debug.h>
24
25 VOID
26 DIB_16BPP_PutPixel(SURFOBJ *SurfObj, LONG x, LONG y, ULONG c)
27 {
28 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta;
29 PWORD addr = (PWORD)byteaddr + x;
30
31 *addr = (WORD)c;
32 }
33
34 ULONG
35 DIB_16BPP_GetPixel(SURFOBJ *SurfObj, LONG x, LONG y)
36 {
37 PBYTE byteaddr = (PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta;
38 PWORD addr = (PWORD)byteaddr + x;
39 return (ULONG)(*addr);
40 }
41
42 VOID
43 DIB_16BPP_HLine(SURFOBJ *SurfObj, LONG x1, LONG x2, LONG y, ULONG c)
44 {
45 PDWORD addr = (PDWORD)((PWORD)((PBYTE)SurfObj->pvScan0 + y * SurfObj->lDelta) + x1);
46
47 #if defined(_M_IX86) && !defined(_MSC_VER)
48 /* This is about 10% faster than the generic C code below */
49 LONG Count = x2 - x1;
50
51 __asm__ __volatile__ (
52 " cld\n"
53 " mov %0, %%eax\n"
54 " shl $16, %%eax\n"
55 " andl $0xffff, %0\n" /* If the pixel value is "abcd", put "abcdabcd" in %eax */
56 " or %0, %%eax\n"
57 " mov %2, %%edi\n"
58 " test $0x03, %%edi\n" /* Align to fullword boundary */
59 " jz 0f\n"
60 " stosw\n"
61 " dec %1\n"
62 " jz 1f\n"
63 "0:\n"
64 " mov %1,%%ecx\n" /* Setup count of fullwords to fill */
65 " shr $1,%%ecx\n"
66 " rep stosl\n" /* The actual fill */
67 " test $0x01, %1\n" /* One left to do at the right side? */
68 " jz 1f\n"
69 " stosw\n"
70 "1:\n"
71 : /* no output */
72 : "r"(c), "r"(Count), "m"(addr)
73 : "%eax", "%ecx", "%edi");
74 #else /* _M_IX86 */
75 LONG cx = x1;
76 DWORD cc;
77
78 if (0 != (cx & 0x01))
79 {
80 *((PWORD) addr) = c;
81 cx++;
82 addr = (PDWORD)((PWORD)(addr) + 1);
83 }
84 cc = ((c & 0xffff) << 16) | (c & 0xffff);
85 while(cx + 1 < x2)
86 {
87 *addr++ = cc;
88 cx += 2;
89 }
90 if (cx < x2)
91 {
92 *((PWORD) addr) = c;
93 }
94 #endif /* _M_IX86 */
95 }
96
97
98 VOID
99 DIB_16BPP_VLine(SURFOBJ *SurfObj, LONG x, LONG y1, LONG y2, ULONG c)
100 {
101 #if defined(_M_IX86) && !defined(_MSC_VER)
102 asm volatile(
103 " testl %2, %2" "\n\t"
104 " jle 2f" "\n\t"
105 " movl %2, %%ecx" "\n\t"
106 " shrl $2, %2" "\n\t"
107 " andl $3, %%ecx" "\n\t"
108 " jz 1f" "\n\t"
109 "0:" "\n\t"
110 " movw %%ax, (%0)" "\n\t"
111 " addl %1, %0" "\n\t"
112 " decl %%ecx" "\n\t"
113 " jnz 0b" "\n\t"
114 " testl %2, %2" "\n\t"
115 " jz 2f" "\n\t"
116 "1:" "\n\t"
117 " movw %%ax, (%0)" "\n\t"
118 " addl %1, %0" "\n\t"
119 " movw %%ax, (%0)" "\n\t"
120 " addl %1, %0" "\n\t"
121 " movw %%ax, (%0)" "\n\t"
122 " addl %1, %0" "\n\t"
123 " movw %%ax, (%0)" "\n\t"
124 " addl %1, %0" "\n\t"
125 " decl %2" "\n\t"
126 " jnz 1b" "\n\t"
127 "2:" "\n\t"
128 : /* no output */
129 : "r"((PBYTE)SurfObj->pvScan0 + (y1 * SurfObj->lDelta) + (x * sizeof (WORD))),
130 "r"(SurfObj->lDelta), "r"(y2 - y1), "a"(c)
131 : "cc", "memory", "%ecx");
132 #else
133 PBYTE byteaddr = (PBYTE)(ULONG_PTR)SurfObj->pvScan0 + y1 * SurfObj->lDelta;
134 PWORD addr = (PWORD)byteaddr + x;
135 LONG lDelta = SurfObj->lDelta;
136
137 byteaddr = (PBYTE)addr;
138 while(y1++ < y2)
139 {
140 *addr = (WORD)c;
141
142 byteaddr += lDelta;
143 addr = (PWORD)byteaddr;
144 }
145 #endif
146 }
147
148 BOOLEAN
149 DIB_16BPP_BitBltSrcCopy(PBLTINFO BltInfo)
150 {
151 LONG i, j, sx, sy, xColor, f1;
152 PBYTE SourceBits, DestBits, SourceLine, DestLine;
153 PBYTE SourceBits_4BPP, SourceLine_4BPP;
154 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 + (BltInfo->DestRect.top * BltInfo->DestSurface->lDelta) + 2 * BltInfo->DestRect.left;
155
156 switch(BltInfo->SourceSurface->iBitmapFormat)
157 {
158 case BMF_1BPP:
159 sx = BltInfo->SourcePoint.x;
160 sy = BltInfo->SourcePoint.y;
161 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
162 {
163 sx = BltInfo->SourcePoint.x;
164 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
165 {
166 if(DIB_1BPP_GetPixel(BltInfo->SourceSurface, sx, sy) == 0)
167 {
168 DIB_16BPP_PutPixel(BltInfo->DestSurface, i, j,
169 XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 0));
170 }
171 else
172 {
173 DIB_16BPP_PutPixel(BltInfo->DestSurface, i, j,
174 XLATEOBJ_iXlate(BltInfo->XlateSourceToDest, 1));
175 }
176 sx++;
177 }
178 sy++;
179 }
180 break;
181
182 case BMF_4BPP:
183 SourceBits_4BPP = (PBYTE)BltInfo->SourceSurface->pvScan0 +
184 (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) +
185 (BltInfo->SourcePoint.x >> 1);
186
187 for (j=BltInfo->DestRect.top; j<BltInfo->DestRect.bottom; j++)
188 {
189 SourceLine_4BPP = SourceBits_4BPP;
190 sx = BltInfo->SourcePoint.x;
191 f1 = sx & 1;
192
193 for (i=BltInfo->DestRect.left; i<BltInfo->DestRect.right; i++)
194 {
195 xColor = XLATEOBJ_iXlate(BltInfo->XlateSourceToDest,
196 (*SourceLine_4BPP & altnotmask[f1]) >> (4 * (1 - f1)));
197 DIB_16BPP_PutPixel(BltInfo->DestSurface, i, j, xColor);
198 if(f1 == 1)
199 {
200 SourceLine_4BPP++;
201 f1 = 0;
202 }
203 else
204 {
205 f1 = 1;
206 }
207 sx++;
208 }
209 SourceBits_4BPP += BltInfo->SourceSurface->lDelta;
210 }
211 break;
212
213 case BMF_8BPP:
214 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 +
215 (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) +
216 BltInfo->SourcePoint.x;
217 DestLine = DestBits;
218
219 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
220 {
221 SourceBits = SourceLine;
222 DestBits = DestLine;
223
224 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
225 {
226 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(
227 BltInfo->XlateSourceToDest, *SourceBits);
228 SourceBits += 1;
229 DestBits += 2;
230 }
231
232 SourceLine += BltInfo->SourceSurface->lDelta;
233 DestLine += BltInfo->DestSurface->lDelta;
234 }
235 break;
236
237 case BMF_16BPP:
238 if (NULL == BltInfo->XlateSourceToDest || 0 !=
239 (BltInfo->XlateSourceToDest->flXlate & XO_TRIVIAL))
240 {
241 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
242 {
243 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 +
244 (BltInfo->SourcePoint.y *
245 BltInfo->SourceSurface->lDelta) + 2 *
246 BltInfo->SourcePoint.x;
247
248 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
249 {
250 RtlMoveMemory(DestBits, SourceBits,
251 2 * (BltInfo->DestRect.right -
252 BltInfo->DestRect.left));
253
254 SourceBits += BltInfo->SourceSurface->lDelta;
255 DestBits += BltInfo->DestSurface->lDelta;
256 }
257 }
258 else
259 {
260 SourceBits = (PBYTE)BltInfo->SourceSurface->pvScan0 +
261 ((BltInfo->SourcePoint.y + BltInfo->DestRect.bottom -
262 BltInfo->DestRect.top - 1) *
263 BltInfo->SourceSurface->lDelta) + 2 *
264 BltInfo->SourcePoint.x;
265
266 DestBits = (PBYTE)BltInfo->DestSurface->pvScan0 +
267 ((BltInfo->DestRect.bottom - 1) *
268 BltInfo->DestSurface->lDelta) + 2 *
269 BltInfo->DestRect.left;
270
271 for (j = BltInfo->DestRect.bottom - 1;
272 BltInfo->DestRect.top <= j; j--)
273 {
274 RtlMoveMemory(DestBits, SourceBits, 2 *
275 (BltInfo->DestRect.right -
276 BltInfo->DestRect.left));
277
278 SourceBits -= BltInfo->SourceSurface->lDelta;
279 DestBits -= BltInfo->DestSurface->lDelta;
280 }
281 }
282 }
283 else
284 {
285 if (BltInfo->DestRect.top < BltInfo->SourcePoint.y)
286 {
287 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 +
288 (BltInfo->SourcePoint.y *
289 BltInfo->SourceSurface->lDelta) + 2 *
290 BltInfo->SourcePoint.x;
291
292 DestLine = DestBits;
293 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
294 {
295 SourceBits = SourceLine;
296 DestBits = DestLine;
297 for (i = BltInfo->DestRect.left; i <
298 BltInfo->DestRect.right; i++)
299 {
300 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(
301 BltInfo->XlateSourceToDest,
302 *((WORD *)SourceBits));
303 SourceBits += 2;
304 DestBits += 2;
305 }
306 SourceLine += BltInfo->SourceSurface->lDelta;
307 DestLine += BltInfo->DestSurface->lDelta;
308 }
309 }
310 else
311 {
312 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 +
313 ((BltInfo->SourcePoint.y +
314 BltInfo->DestRect.bottom -
315 BltInfo->DestRect.top - 1) *
316 BltInfo->SourceSurface->lDelta) + 2 *
317 BltInfo->SourcePoint.x;
318
319 DestLine = (PBYTE)BltInfo->DestSurface->pvScan0 +
320 ((BltInfo->DestRect.bottom - 1) *
321 BltInfo->DestSurface->lDelta) + 2 *
322 BltInfo->DestRect.left;
323
324 for (j = BltInfo->DestRect.bottom - 1;
325 BltInfo->DestRect.top <= j; j--)
326 {
327 SourceBits = SourceLine;
328 DestBits = DestLine;
329 for (i = BltInfo->DestRect.left; i <
330 BltInfo->DestRect.right; i++)
331 {
332 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(
333 BltInfo->XlateSourceToDest,
334 *((WORD *)SourceBits));
335 SourceBits += 2;
336 DestBits += 2;
337 }
338 SourceLine -= BltInfo->SourceSurface->lDelta;
339 DestLine -= BltInfo->DestSurface->lDelta;
340 }
341 }
342 }
343 break;
344
345 case BMF_24BPP:
346 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 +
347 (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) +
348 3 * BltInfo->SourcePoint.x;
349
350 DestLine = DestBits;
351
352 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
353 {
354 SourceBits = SourceLine;
355 DestBits = DestLine;
356
357 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
358 {
359 xColor = (*(SourceBits + 2) << 0x10) +
360 (*(SourceBits + 1) << 0x08) + (*(SourceBits));
361
362 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(
363 BltInfo->XlateSourceToDest, xColor);
364
365 SourceBits += 3;
366 DestBits += 2;
367 }
368 SourceLine += BltInfo->SourceSurface->lDelta;
369 DestLine += BltInfo->DestSurface->lDelta;
370 }
371 break;
372
373 case BMF_32BPP:
374 SourceLine = (PBYTE)BltInfo->SourceSurface->pvScan0 +
375 (BltInfo->SourcePoint.y * BltInfo->SourceSurface->lDelta) +
376 4 * BltInfo->SourcePoint.x;
377
378 DestLine = DestBits;
379
380 for (j = BltInfo->DestRect.top; j < BltInfo->DestRect.bottom; j++)
381 {
382 SourceBits = SourceLine;
383 DestBits = DestLine;
384
385 for (i = BltInfo->DestRect.left; i < BltInfo->DestRect.right; i++)
386 {
387 *((WORD *)DestBits) = (WORD)XLATEOBJ_iXlate(
388 BltInfo->XlateSourceToDest,
389 *((PDWORD) SourceBits));
390 SourceBits += 4;
391 DestBits += 2;
392 }
393
394 SourceLine += BltInfo->SourceSurface->lDelta;
395 DestLine += BltInfo->DestSurface->lDelta;
396 }
397 break;
398
399 default:
400 DPRINT1("DIB_16BPP_Bitblt: Unhandled Source BPP: %u\n",
401 BitsPerFormat(BltInfo->SourceSurface->iBitmapFormat));
402 return FALSE;
403 }
404
405 return TRUE;
406 }
407
408 /* Optimize for bitBlt */
409 BOOLEAN
410 DIB_16BPP_ColorFill(SURFOBJ* DestSurface, RECTL* DestRect, ULONG color)
411 {
412 ULONG DestY;
413
414 #if defined(_M_IX86) && !defined(_MSC_VER)
415 /* This is about 10% faster than the generic C code below */
416 ULONG delta = DestSurface->lDelta;
417 ULONG width = (DestRect->right - DestRect->left) ;
418 PULONG pos = (PULONG) ((PBYTE)DestSurface->pvScan0 + DestRect->top * delta + (DestRect->left<<1));
419 color = (color&0xffff); /* If the color value is "abcd", put "abcdabcd" into color */
420 color += (color<<16);
421
422 for (DestY = DestRect->top; DestY< DestRect->bottom; DestY++)
423 {
424 __asm__ __volatile__ (
425 " cld\n"
426 " mov %1,%%ebx\n"
427 " mov %2,%%edi\n"
428 " test $0x03, %%edi\n" /* Align to fullword boundary */
429 " jz .FL1\n"
430 " stosw\n"
431 " dec %%ebx\n"
432 " jz .FL2\n"
433 ".FL1:\n"
434 " mov %%ebx,%%ecx\n" /* Setup count of fullwords to fill */
435 " shr $1,%%ecx\n"
436 " rep stosl\n" /* The actual fill */
437 " test $0x01, %%ebx\n" /* One left to do at the right side? */
438 " jz .FL2\n"
439 " stosw\n"
440 ".FL2:\n"
441 :
442 : "a" (color), "r" (width), "m" (pos)
443 : "%ecx", "%ebx", "%edi");
444 pos =(PULONG)((ULONG_PTR)pos + delta);
445 }
446 #else /* _M_IX86 */
447
448 for (DestY = DestRect->top; DestY< DestRect->bottom; DestY++)
449 {
450 DIB_16BPP_HLine (DestSurface, DestRect->left, DestRect->right, DestY, color);
451 }
452 #endif
453 return TRUE;
454 }
455 /*
456 =======================================
457 Stretching functions goes below
458 Some parts of code are based on an
459 article "Bresenhame image scaling"
460 Dr. Dobb Journal, May 2002
461 =======================================
462 */
463
464 typedef unsigned short PIXEL;
465
466 /* 16-bit HiColor (565 format) */
467 __inline PIXEL average16(PIXEL a, PIXEL b)
468 {
469 // This one doesn't work
470 /*
471 if (a == b)
472 {
473 return a;
474 }
475 else
476 {
477 unsigned short mask = ~ (((a | b) & 0x0410) << 1);
478 return ((a & mask) + (b & mask)) >> 1;
479 }*/ /* if */
480
481 // This one should be correct, but it's too long
482 /*
483 unsigned char r1, g1, b1, r2, g2, b2, rr, gr, br;
484 unsigned short res;
485
486 r1 = (a & 0xF800) >> 11;
487 g1 = (a & 0x7E0) >> 5;
488 b1 = (a & 0x1F);
489
490 r2 = (b & 0xF800) >> 11;
491 g2 = (b & 0x7E0) >> 5;
492 b2 = (b & 0x1F);
493
494 rr = (r1+r2) / 2;
495 gr = (g1+g2) / 2;
496 br = (b1+b2) / 2;
497
498 res = (rr << 11) + (gr << 5) + br;
499
500 return res;
501 */
502 // This one is the short form of the correct one ;-)
503 return (((a ^ b) & 0xf7deU) >> 1) + (a & b);
504 }
505
506 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
507 void ScaleLineAvg16(PIXEL *Target, PIXEL *Source, int SrcWidth, int TgtWidth)
508 {
509 int NumPixels = TgtWidth;
510 int IntPart = SrcWidth / TgtWidth;
511 int FractPart = SrcWidth % TgtWidth;
512 int Mid = TgtWidth >> 1;
513 int E = 0;
514 int skip;
515 PIXEL p;
516
517 skip = (TgtWidth < SrcWidth) ? 0 : (TgtWidth / (2*SrcWidth) + 1);
518 NumPixels -= skip;
519
520 while (NumPixels-- > 0)
521 {
522 p = *Source;
523 if (E >= Mid)
524 {
525 p = average16(p, *(Source+1));
526 }
527 *Target++ = p;
528 Source += IntPart;
529 E += FractPart;
530 if (E >= TgtWidth)
531 {
532 E -= TgtWidth;
533 Source++;
534 }
535 }
536 while (skip-- > 0)
537 {
538 *Target++ = *Source;
539 }
540 }
541
542 static BOOLEAN
543 FinalCopy16(PIXEL *Target, PIXEL *Source, PSPAN ClipSpans, UINT ClipSpansCount, UINT *SpanIndex,
544 UINT DestY, RECTL *DestRect)
545 {
546 LONG Left, Right;
547
548 while ( ClipSpans[*SpanIndex].Y < DestY ||
549 (ClipSpans[*SpanIndex].Y == DestY &&
550 ClipSpans[*SpanIndex].X + ClipSpans[*SpanIndex].Width < DestRect->left))
551 {
552 (*SpanIndex)++;
553 if (ClipSpansCount <= *SpanIndex)
554 {
555 /* No more spans, everything else is clipped away, we're done */
556 return FALSE;
557 }
558 }
559 while (ClipSpans[*SpanIndex].Y == DestY)
560 {
561 if (ClipSpans[*SpanIndex].X < DestRect->right)
562 {
563 Left = max(ClipSpans[*SpanIndex].X, DestRect->left);
564
565 Right = min(ClipSpans[*SpanIndex].X + ClipSpans[*SpanIndex].Width,
566 DestRect->right);
567
568 memcpy(Target + Left - DestRect->left, Source + Left - DestRect->left,
569 (Right - Left) * sizeof(PIXEL));
570 }
571
572 (*SpanIndex)++;
573
574 if (ClipSpansCount <= *SpanIndex)
575 {
576 /* No more spans, everything else is clipped away, we're done */
577 return FALSE;
578 }
579 }
580
581 return TRUE;
582 }
583
584 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
585 BOOLEAN ScaleRectAvg16(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
586 RECTL* DestRect, RECTL *SourceRect,
587 POINTL* MaskOrigin, POINTL BrushOrigin,
588 CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
589 ULONG Mode)
590 {
591 int NumPixels = DestRect->bottom - DestRect->top;
592
593 int IntPart = (((SourceRect->bottom - SourceRect->top) /
594 (DestRect->bottom - DestRect->top)) * SourceSurf->lDelta) >> 1;
595
596 int FractPart = (SourceRect->bottom - SourceRect->top) %
597 (DestRect->bottom - DestRect->top);
598
599 int Mid = (DestRect->bottom - DestRect->top) >> 1;
600 int E = 0;
601 int skip;
602 PIXEL *ScanLine, *ScanLineAhead;
603 PIXEL *PrevSource = NULL;
604 PIXEL *PrevSourceAhead = NULL;
605
606 PIXEL *Target = (PIXEL *) ((PBYTE)DestSurf->pvScan0 + (DestRect->top *
607 DestSurf->lDelta) + 2 * DestRect->left);
608
609 PIXEL *Source = (PIXEL *) ((PBYTE)SourceSurf->pvScan0 + (SourceRect->top *
610 SourceSurf->lDelta) + 2 * SourceRect->left);
611
612 PSPAN ClipSpans;
613 UINT ClipSpansCount;
614 UINT SpanIndex;
615 LONG DestY;
616
617 if (! ClipobjToSpans(&ClipSpans, &ClipSpansCount, ClipRegion, DestRect))
618 {
619 return FALSE;
620 }
621 if (0 == ClipSpansCount)
622 {
623 /* No clip spans == empty clipping region, everything clipped away */
624 ASSERT(NULL == ClipSpans);
625 return TRUE;
626 }
627 skip = (DestRect->bottom - DestRect->top < SourceRect->bottom - SourceRect->top)
628 ? 0 : ((DestRect->bottom - DestRect->top) /
629 (2 * (SourceRect->bottom - SourceRect->top)) + 1);
630
631 NumPixels -= skip;
632
633 ScanLine = (PIXEL*)ExAllocatePool(PagedPool, (DestRect->right - DestRect->left) *
634 sizeof(PIXEL));
635
636 ScanLineAhead = (PIXEL *)ExAllocatePool(PagedPool, (DestRect->right -
637 DestRect->left) * sizeof(PIXEL));
638
639 DestY = DestRect->top;
640 SpanIndex = 0;
641 while (NumPixels-- > 0)
642 {
643 if (Source != PrevSource)
644 {
645 if (Source == PrevSourceAhead)
646 {
647 /* the next scan line has already been scaled and stored in
648 * ScanLineAhead; swap the buffers that ScanLine and ScanLineAhead
649 * point to
650 */
651 PIXEL *tmp = ScanLine;
652 ScanLine = ScanLineAhead;
653 ScanLineAhead = tmp;
654 }
655 else
656 {
657 ScaleLineAvg16(ScanLine, Source, SourceRect->right - SourceRect->left,
658 DestRect->right - DestRect->left);
659 }
660 PrevSource = Source;
661 }
662
663 if (E >= Mid && PrevSourceAhead != (PIXEL *)((BYTE *)Source +
664 SourceSurf->lDelta))
665 {
666 int x;
667
668 ScaleLineAvg16(ScanLineAhead, (PIXEL *)((BYTE *)Source +
669 SourceSurf->lDelta), SourceRect->right -
670 SourceRect->left, DestRect->right - DestRect->left);
671
672 for (x = 0; x < DestRect->right - DestRect->left; x++)
673 {
674 ScanLine[x] = average16(ScanLine[x], ScanLineAhead[x]);
675 }
676
677 PrevSourceAhead = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
678 }
679
680 if (! FinalCopy16(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex, DestY, DestRect))
681 {
682 /* No more spans, everything else is clipped away, we're done */
683 ExFreePool(ClipSpans);
684 ExFreePool(ScanLine);
685 ExFreePool(ScanLineAhead);
686 return TRUE;
687 }
688
689 DestY++;
690 Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
691 Source += IntPart;
692 E += FractPart;
693
694 if (E >= DestRect->bottom - DestRect->top)
695 {
696 E -= DestRect->bottom - DestRect->top;
697 Source = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
698 }
699 } /* while */
700
701 if (skip > 0 && Source != PrevSource)
702 {
703 ScaleLineAvg16(ScanLine, Source, SourceRect->right - SourceRect->left,
704 DestRect->right - DestRect->left);
705 }
706
707 while (skip-- > 0)
708 {
709 if (! FinalCopy16(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex,
710 DestY, DestRect))
711 {
712 /* No more spans, everything else is clipped away, we're done */
713 ExFreePool(ClipSpans);
714 ExFreePool(ScanLine);
715 ExFreePool(ScanLineAhead);
716 return TRUE;
717 }
718 DestY++;
719 Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
720 }
721
722 ExFreePool(ClipSpans);
723 ExFreePool(ScanLine);
724 ExFreePool(ScanLineAhead);
725
726 return TRUE;
727 }
728
729
730 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
731 BOOLEAN DIB_16BPP_StretchBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
732 RECTL* DestRect, RECTL *SourceRect,
733 POINTL* MaskOrigin, POINTL BrushOrigin,
734 CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
735 ULONG Mode)
736 {
737 LONG SrcSizeY;
738 LONG SrcSizeX;
739 LONG DesSizeY;
740 LONG DesSizeX;
741 LONG sx = 0;
742 LONG sy = 0;
743 LONG DesX;
744 LONG DesY;
745 PULONG DestBits;
746 LONG DifflDelta;
747
748 LONG SrcZoomXHight;
749 LONG SrcZoomXLow;
750 LONG SrcZoomYHight;
751 LONG SrcZoomYLow;
752
753 LONG sy_dec = 0;
754 LONG sy_max;
755
756 LONG sx_dec = 0;
757 LONG sx_max;
758
759 DPRINT("DIB_16BPP_StretchBlt: Source BPP: %u, srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
760 BitsPerFormat(SourceSurf->iBitmapFormat), SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
761 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
762
763 /* Calc the Zoom height of Source */
764 SrcSizeY = SourceRect->bottom - SourceRect->top;
765
766 /* Calc the Zoom Width of Source */
767 SrcSizeX = SourceRect->right - SourceRect->left;
768
769 /* Calc the Zoom height of Destions */
770 DesSizeY = DestRect->bottom - DestRect->top;
771
772 /* Calc the Zoom width of Destions */
773 DesSizeX = DestRect->right - DestRect->left;
774
775 /* Calc the zoom factor of soruce height */
776 SrcZoomYHight = SrcSizeY / DesSizeY;
777 SrcZoomYLow = SrcSizeY - (SrcZoomYHight * DesSizeY);
778
779 /* Calc the zoom factor of soruce width */
780 SrcZoomXHight = SrcSizeX / DesSizeX;
781 SrcZoomXLow = SrcSizeX - (SrcZoomXHight * DesSizeX);
782
783 sx_max = DesSizeX;
784 sy_max = DesSizeY;
785 sy = SourceRect->top;
786
787 DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 1) +
788 DestRect->top * DestSurf->lDelta);
789
790 DifflDelta = DestSurf->lDelta - (DesSizeX << 1);
791
792 switch(SourceSurf->iBitmapFormat)
793 {
794
795 case BMF_1BPP:
796 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
797 /* This is a reference implementation, it hasn't been optimized for speed */
798
799 for (DesY=0; DesY<DesSizeY; DesY++)
800 {
801 sx = SourceRect->left;
802 sx_dec = 0;
803
804 for (DesX=0; DesX<DesSizeX; DesX++)
805 {
806 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
807 DIB_1BPP_GetPixel(SourceSurf, sx, sy));
808
809 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
810
811 sx += SrcZoomXHight;
812 sx_dec += SrcZoomXLow;
813 if (sx_dec >= sx_max)
814 {
815 sx++;
816 sx_dec -= sx_max;
817 }
818 }
819
820 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
821
822 sy += SrcZoomYHight;
823 sy_dec += SrcZoomYLow;
824 if (sy_dec >= sy_max)
825 {
826 sy++;
827 sy_dec -= sy_max;
828 }
829 }
830 break;
831
832 case BMF_4BPP:
833 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
834 /* This is a reference implementation, it hasn't been optimized for speed */
835
836 for (DesY=0; DesY<DesSizeY; DesY++)
837 {
838 sx = SourceRect->left;
839 sx_dec = 0;
840
841 for (DesX=0; DesX<DesSizeX; DesX++)
842 {
843 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
844 DIB_4BPP_GetPixel(SourceSurf, sx, sy));
845
846 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
847
848 sx += SrcZoomXHight;
849 sx_dec += SrcZoomXLow;
850 if (sx_dec >= sx_max)
851 {
852 sx++;
853 sx_dec -= sx_max;
854 }
855 }
856
857 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
858
859 sy += SrcZoomYHight;
860 sy_dec += SrcZoomYLow;
861 if (sy_dec >= sy_max)
862 {
863 sy++;
864 sy_dec -= sy_max;
865 }
866 }
867 break;
868
869 case BMF_8BPP:
870 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
871 /* This is a reference implementation, it hasn't been optimized for speed */
872
873 for (DesY=0; DesY<DesSizeY; DesY++)
874 {
875 sx = SourceRect->left;
876 sx_dec = 0;
877
878 for (DesX=0; DesX<DesSizeX; DesX++)
879 {
880 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
881 DIB_8BPP_GetPixel(SourceSurf, sx, sy));
882
883 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
884
885 sx += SrcZoomXHight;
886 sx_dec += SrcZoomXLow;
887 if (sx_dec >= sx_max)
888 {
889 sx++;
890 sx_dec -= sx_max;
891 }
892 }
893
894 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
895
896 sy += SrcZoomYHight;
897 sy_dec += SrcZoomYLow;
898 if (sy_dec >= sy_max)
899 {
900 sy++;
901 sy_dec -= sy_max;
902 }
903 }
904 break;
905
906
907 case BMF_24BPP:
908 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
909 /* This is a reference implementation, it hasn't been optimized for speed */
910
911 DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 1) +
912 DestRect->top * DestSurf->lDelta);
913
914 DifflDelta = DestSurf->lDelta - (DesSizeX << 1);
915
916 for (DesY=0; DesY<DesSizeY; DesY++)
917 {
918 sx = SourceRect->left;
919 sx_dec = 0;
920
921 for (DesX=0; DesX<DesSizeX; DesX++)
922 {
923 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
924 DIB_24BPP_GetPixel(SourceSurf, sx, sy));
925
926 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
927
928 sx += SrcZoomXHight;
929 sx_dec += SrcZoomXLow;
930 if (sx_dec >= sx_max)
931 {
932 sx++;
933 sx_dec -= sx_max;
934 }
935 }
936
937 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
938
939 sy += SrcZoomYHight;
940 sy_dec += SrcZoomYLow;
941 if (sy_dec >= sy_max)
942 {
943 sy++;
944 sy_dec -= sy_max;
945 }
946 }
947 break;
948
949 case BMF_32BPP:
950 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
951 /* This is a reference implementation, it hasn't been optimized for speed */
952
953 for (DesY=0; DesY<DesSizeY; DesY++)
954 {
955 sx = SourceRect->left;
956 sx_dec = 0;
957
958 for (DesX=0; DesX<DesSizeX; DesX++)
959 {
960 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
961 DIB_32BPP_GetPixel(SourceSurf, sx, sy));
962
963 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
964
965 sx += SrcZoomXHight;
966 sx_dec += SrcZoomXLow;
967 if (sx_dec >= sx_max)
968 {
969 sx++;
970 sx_dec -= sx_max;
971 }
972 }
973 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
974
975 sy += SrcZoomYHight;
976 sy_dec += SrcZoomYLow;
977 if (sy_dec >= sy_max)
978 {
979 sy++;
980 sy_dec -= sy_max;
981 }
982 }
983 break;
984
985 case BMF_16BPP:
986 return ScaleRectAvg16(DestSurf, SourceSurf, DestRect, SourceRect, MaskOrigin, BrushOrigin,
987 ClipRegion, ColorTranslation, Mode);
988 break;
989
990 default:
991 DPRINT1("DIB_16BPP_StretchBlt: Unhandled Source BPP: %u\n", BitsPerFormat(SourceSurf->iBitmapFormat));
992 return FALSE;
993 }
994
995
996
997 return TRUE;
998 }
999
1000 BOOLEAN
1001 DIB_16BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
1002 RECTL* DestRect, POINTL *SourcePoint,
1003 XLATEOBJ *ColorTranslation, ULONG iTransColor)
1004 {
1005 ULONG RoundedRight, X, Y, SourceX, SourceY, Source, wd, Dest;
1006 ULONG *DestBits;
1007
1008 RoundedRight = DestRect->right - ((DestRect->right - DestRect->left) & 0x1);
1009 SourceY = SourcePoint->y;
1010 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 +
1011 (DestRect->left << 1) +
1012 DestRect->top * DestSurf->lDelta);
1013 wd = DestSurf->lDelta - ((DestRect->right - DestRect->left) << 1);
1014
1015 for(Y = DestRect->top; Y < DestRect->bottom; Y++)
1016 {
1017 SourceX = SourcePoint->x;
1018 for(X = DestRect->left; X < RoundedRight; X += 2, DestBits++, SourceX += 2)
1019 {
1020 Dest = *DestBits;
1021 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1022
1023 if(Source != iTransColor)
1024 {
1025 Dest &= 0xFFFF0000;
1026 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) & 0xFFFF);
1027 }
1028
1029 Source = DIB_GetSourceIndex(SourceSurf, SourceX + 1, SourceY);
1030 if(Source != iTransColor)
1031 {
1032 Dest &= 0xFFFF;
1033 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) << 16);
1034 }
1035
1036 *DestBits = Dest;
1037 }
1038
1039 if(X < DestRect->right)
1040 {
1041 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1042 if(Source != iTransColor)
1043 {
1044 *((USHORT*)DestBits) = (USHORT)XLATEOBJ_iXlate(ColorTranslation,
1045 Source);
1046 }
1047
1048 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
1049 }
1050
1051 SourceY++;
1052 DestBits = (ULONG*)((ULONG_PTR)DestBits + wd);
1053 }
1054
1055 return TRUE;
1056 }
1057
1058 typedef union
1059 {
1060 ULONG ul;
1061 struct
1062 {
1063 UCHAR red;
1064 UCHAR green;
1065 UCHAR blue;
1066 UCHAR alpha;
1067 } col;
1068 } NICEPIXEL32;
1069
1070 typedef union
1071 {
1072 USHORT us;
1073 struct
1074 {
1075 USHORT red:5,
1076 green:6,
1077 blue:5;
1078 } col;
1079 } NICEPIXEL16;
1080
1081 static __inline UCHAR
1082 Clamp5(ULONG val)
1083 {
1084 return (val > 31) ? 31 : val;
1085 }
1086
1087 static __inline UCHAR
1088 Clamp6(ULONG val)
1089 {
1090 return (val > 63) ? 63 : val;
1091 }
1092
1093 BOOLEAN
1094 DIB_16BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
1095 RECTL* SourceRect, CLIPOBJ* ClipRegion,
1096 XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
1097 {
1098 INT Rows, Cols, SrcX, SrcY;
1099 register PUSHORT Dst;
1100 ULONG DstDelta;
1101 BLENDFUNCTION BlendFunc;
1102 register NICEPIXEL16 DstPixel;
1103 register NICEPIXEL32 SrcPixel;
1104 UCHAR Alpha, SrcBpp;
1105
1106 DPRINT("DIB_16BPP_AlphaBlend: srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
1107 SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
1108 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
1109
1110 ASSERT(DestRect->bottom - DestRect->top == SourceRect->bottom - SourceRect->top &&
1111 DestRect->right - DestRect->left == SourceRect->right - SourceRect->left);
1112
1113 BlendFunc = BlendObj->BlendFunction;
1114 if (BlendFunc.BlendOp != AC_SRC_OVER)
1115 {
1116 DPRINT1("BlendOp != AC_SRC_OVER\n");
1117 return FALSE;
1118 }
1119 if (BlendFunc.BlendFlags != 0)
1120 {
1121 DPRINT1("BlendFlags != 0\n");
1122 return FALSE;
1123 }
1124 if ((BlendFunc.AlphaFormat & ~AC_SRC_ALPHA) != 0)
1125 {
1126 DPRINT1("Unsupported AlphaFormat (0x%x)\n", BlendFunc.AlphaFormat);
1127 return FALSE;
1128 }
1129 if ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0 &&
1130 BitsPerFormat(Source->iBitmapFormat) != 32)
1131 {
1132 DPRINT1("Source bitmap must be 32bpp when AC_SRC_ALPHA is set\n");
1133 return FALSE;
1134 }
1135
1136 Dst = (PUSHORT)((ULONG_PTR)Dest->pvScan0 + (DestRect->top * Dest->lDelta) +
1137 (DestRect->left << 1));
1138 DstDelta = Dest->lDelta - ((DestRect->right - DestRect->left) << 1);
1139 SrcBpp = BitsPerFormat(Source->iBitmapFormat);
1140
1141 Rows = DestRect->bottom - DestRect->top;
1142 SrcY = SourceRect->top;
1143 while (--Rows >= 0)
1144 {
1145 Cols = DestRect->right - DestRect->left;
1146 SrcX = SourceRect->left;
1147 while (--Cols >= 0)
1148 {
1149 if (SrcBpp <= 16)
1150 {
1151 DstPixel.us = DIB_GetSource(Source, SrcX++, SrcY, ColorTranslation);
1152 SrcPixel.col.red = (DstPixel.col.red << 3) | (DstPixel.col.red >> 2);
1153
1154 SrcPixel.col.green = (DstPixel.col.green << 2) |
1155 (DstPixel.col.green >> 4);
1156
1157 SrcPixel.col.blue = (DstPixel.col.blue << 3) | (DstPixel.col.blue >> 2);
1158 }
1159 else
1160 {
1161 SrcPixel.ul = DIB_GetSourceIndex(Source, SrcX++, SrcY);
1162 }
1163 SrcPixel.col.red = SrcPixel.col.red *
1164 BlendFunc.SourceConstantAlpha / 255;
1165
1166 SrcPixel.col.green = SrcPixel.col.green *
1167 BlendFunc.SourceConstantAlpha / 255;
1168
1169 SrcPixel.col.blue = SrcPixel.col.blue *
1170 BlendFunc.SourceConstantAlpha / 255;
1171
1172 SrcPixel.col.alpha = (SrcBpp == 32) ?
1173 (SrcPixel.col.alpha *
1174 BlendFunc.SourceConstantAlpha / 255) :
1175 BlendFunc.SourceConstantAlpha;
1176
1177 Alpha = ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0) ?
1178 SrcPixel.col.alpha : BlendFunc.SourceConstantAlpha;
1179
1180 DstPixel.us = *Dst;
1181 DstPixel.col.red = Clamp5(DstPixel.col.red * (255 - Alpha) / 255 +
1182 (SrcPixel.col.red >> 3));
1183
1184 DstPixel.col.green = Clamp6(DstPixel.col.green * (255 - Alpha) / 255 +
1185 (SrcPixel.col.green >> 2));
1186
1187 DstPixel.col.blue = Clamp5(DstPixel.col.blue * (255 - Alpha) / 255 +
1188 (SrcPixel.col.blue >> 3));
1189
1190 *Dst++ = DstPixel.us;
1191 }
1192
1193 Dst = (PUSHORT)((ULONG_PTR)Dst + DstDelta);
1194 SrcY++;
1195 }
1196
1197 return TRUE;
1198 }
1199
1200 /* EOF */