StretchBlt:
[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 should be correct, but it's too long
470 /*
471 unsigned char r1, g1, b1, r2, g2, b2, rr, gr, br;
472 unsigned short res;
473
474 r1 = (a & 0xF800) >> 11;
475 g1 = (a & 0x7E0) >> 5;
476 b1 = (a & 0x1F);
477
478 r2 = (b & 0xF800) >> 11;
479 g2 = (b & 0x7E0) >> 5;
480 b2 = (b & 0x1F);
481
482 rr = (r1+r2) / 2;
483 gr = (g1+g2) / 2;
484 br = (b1+b2) / 2;
485
486 res = (rr << 11) + (gr << 5) + br;
487
488 return res;
489 */
490 // This one is the short form of the correct one, but does not work for QEMU (expects 555 format)
491 //return (((a ^ b) & 0xf7deU) >> 1) + (a & b);
492
493 //hack until short version works properly
494 return a;
495 }
496
497 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
498 void ScaleLineAvg16(PIXEL *Target, PIXEL *Source, int SrcWidth, int TgtWidth)
499 {
500 int NumPixels = TgtWidth;
501 int IntPart = SrcWidth / TgtWidth;
502 int FractPart = SrcWidth % TgtWidth;
503 int Mid = TgtWidth >> 1;
504 int E = 0;
505 int skip;
506 PIXEL p;
507
508 skip = (TgtWidth < SrcWidth) ? 0 : (TgtWidth / (2*SrcWidth) + 1);
509 NumPixels -= skip;
510
511 while (NumPixels-- > 0)
512 {
513 p = *Source;
514 if (E >= Mid)
515 {
516 p = average16(p, *(Source+1));
517 }
518 *Target++ = p;
519 Source += IntPart;
520 E += FractPart;
521 if (E >= TgtWidth)
522 {
523 E -= TgtWidth;
524 Source++;
525 }
526 }
527 while (skip-- > 0)
528 {
529 *Target++ = *Source;
530 }
531 }
532
533 static BOOLEAN
534 FinalCopy16(PIXEL *Target, PIXEL *Source, PSPAN ClipSpans, UINT ClipSpansCount, UINT *SpanIndex,
535 UINT DestY, RECTL *DestRect)
536 {
537 LONG Left, Right;
538
539 while ( ClipSpans[*SpanIndex].Y < DestY ||
540 (ClipSpans[*SpanIndex].Y == DestY &&
541 ClipSpans[*SpanIndex].X + ClipSpans[*SpanIndex].Width < DestRect->left))
542 {
543 (*SpanIndex)++;
544 if (ClipSpansCount <= *SpanIndex)
545 {
546 /* No more spans, everything else is clipped away, we're done */
547 return FALSE;
548 }
549 }
550 while (ClipSpans[*SpanIndex].Y == DestY)
551 {
552 if (ClipSpans[*SpanIndex].X < DestRect->right)
553 {
554 Left = max(ClipSpans[*SpanIndex].X, DestRect->left);
555
556 Right = min(ClipSpans[*SpanIndex].X + ClipSpans[*SpanIndex].Width,
557 DestRect->right);
558
559 memcpy(Target + Left - DestRect->left, Source + Left - DestRect->left,
560 (Right - Left) * sizeof(PIXEL));
561 }
562
563 (*SpanIndex)++;
564
565 if (ClipSpansCount <= *SpanIndex)
566 {
567 /* No more spans, everything else is clipped away, we're done */
568 return FALSE;
569 }
570 }
571
572 return TRUE;
573 }
574
575 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
576 BOOLEAN ScaleRectAvg16(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
577 RECTL* DestRect, RECTL *SourceRect,
578 POINTL* MaskOrigin, POINTL BrushOrigin,
579 CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
580 ULONG Mode)
581 {
582 int NumPixels = DestRect->bottom - DestRect->top;
583
584 int IntPart = (((SourceRect->bottom - SourceRect->top) /
585 (DestRect->bottom - DestRect->top)) * SourceSurf->lDelta) >> 1;
586
587 int FractPart = (SourceRect->bottom - SourceRect->top) %
588 (DestRect->bottom - DestRect->top);
589
590 int Mid = (DestRect->bottom - DestRect->top) >> 1;
591 int E = 0;
592 int skip;
593 PIXEL *ScanLine, *ScanLineAhead;
594 PIXEL *PrevSource = NULL;
595 PIXEL *PrevSourceAhead = NULL;
596
597 PIXEL *Target = (PIXEL *) ((PBYTE)DestSurf->pvScan0 + (DestRect->top *
598 DestSurf->lDelta) + 2 * DestRect->left);
599
600 PIXEL *Source = (PIXEL *) ((PBYTE)SourceSurf->pvScan0 + (SourceRect->top *
601 SourceSurf->lDelta) + 2 * SourceRect->left);
602
603 PSPAN ClipSpans;
604 UINT ClipSpansCount;
605 UINT SpanIndex;
606 LONG DestY;
607
608 if (! ClipobjToSpans(&ClipSpans, &ClipSpansCount, ClipRegion, DestRect))
609 {
610 return FALSE;
611 }
612 if (0 == ClipSpansCount)
613 {
614 /* No clip spans == empty clipping region, everything clipped away */
615 ASSERT(NULL == ClipSpans);
616 return TRUE;
617 }
618 skip = (DestRect->bottom - DestRect->top < SourceRect->bottom - SourceRect->top)
619 ? 0 : ((DestRect->bottom - DestRect->top) /
620 (2 * (SourceRect->bottom - SourceRect->top)) + 1);
621
622 NumPixels -= skip;
623
624 ScanLine = (PIXEL*)ExAllocatePool(PagedPool, (DestRect->right - DestRect->left) *
625 sizeof(PIXEL));
626
627 ScanLineAhead = (PIXEL *)ExAllocatePool(PagedPool, (DestRect->right -
628 DestRect->left) * sizeof(PIXEL));
629
630 if (!ScanLine || !ScanLineAhead)
631 {
632 if (ScanLine) ExFreePool(ScanLine);
633 if (ScanLineAhead) ExFreePool(ScanLineAhead);
634 return FALSE;
635 }
636
637 DestY = DestRect->top;
638 SpanIndex = 0;
639 while (NumPixels-- > 0)
640 {
641 if (Source != PrevSource)
642 {
643 if (Source == PrevSourceAhead)
644 {
645 /* the next scan line has already been scaled and stored in
646 * ScanLineAhead; swap the buffers that ScanLine and ScanLineAhead
647 * point to
648 */
649 PIXEL *tmp = ScanLine;
650 ScanLine = ScanLineAhead;
651 ScanLineAhead = tmp;
652 }
653 else
654 {
655 ScaleLineAvg16(ScanLine, Source, SourceRect->right - SourceRect->left,
656 DestRect->right - DestRect->left);
657 }
658 PrevSource = Source;
659 }
660
661 if (E >= Mid && PrevSourceAhead != (PIXEL *)((BYTE *)Source +
662 SourceSurf->lDelta))
663 {
664 int x;
665
666 ScaleLineAvg16(ScanLineAhead, (PIXEL *)((BYTE *)Source +
667 SourceSurf->lDelta), SourceRect->right -
668 SourceRect->left, DestRect->right - DestRect->left);
669
670 for (x = 0; x < DestRect->right - DestRect->left; x++)
671 {
672 ScanLine[x] = average16(ScanLine[x], ScanLineAhead[x]);
673 }
674
675 PrevSourceAhead = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
676 }
677
678 if (! FinalCopy16(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex, DestY, DestRect))
679 {
680 /* No more spans, everything else is clipped away, we're done */
681 ExFreePool(ClipSpans);
682 ExFreePool(ScanLine);
683 ExFreePool(ScanLineAhead);
684 return TRUE;
685 }
686
687 DestY++;
688 Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
689 Source += IntPart;
690 E += FractPart;
691
692 if (E >= DestRect->bottom - DestRect->top)
693 {
694 E -= DestRect->bottom - DestRect->top;
695 Source = (PIXEL *)((BYTE *)Source + SourceSurf->lDelta);
696 }
697 } /* while */
698
699 if (skip > 0 && Source != PrevSource)
700 {
701 ScaleLineAvg16(ScanLine, Source, SourceRect->right - SourceRect->left,
702 DestRect->right - DestRect->left);
703 }
704
705 while (skip-- > 0)
706 {
707 if (! FinalCopy16(Target, ScanLine, ClipSpans, ClipSpansCount, &SpanIndex,
708 DestY, DestRect))
709 {
710 /* No more spans, everything else is clipped away, we're done */
711 ExFreePool(ClipSpans);
712 ExFreePool(ScanLine);
713 ExFreePool(ScanLineAhead);
714 return TRUE;
715 }
716 DestY++;
717 Target = (PIXEL *)((BYTE *)Target + DestSurf->lDelta);
718 }
719
720 ExFreePool(ClipSpans);
721 ExFreePool(ScanLine);
722 ExFreePool(ScanLineAhead);
723
724 return TRUE;
725 }
726
727
728 //NOTE: If you change something here, please do the same in other dibXXbpp.c files!
729 BOOLEAN DIB_16BPP_StretchBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
730 RECTL* DestRect, RECTL *SourceRect,
731 POINTL* MaskOrigin, POINTL BrushOrigin,
732 CLIPOBJ *ClipRegion, XLATEOBJ *ColorTranslation,
733 ULONG Mode)
734 {
735 LONG SrcSizeY;
736 LONG SrcSizeX;
737 LONG DesSizeY;
738 LONG DesSizeX;
739 LONG sx = 0;
740 LONG sy = 0;
741 LONG DesX;
742 LONG DesY;
743
744 LONG SrcZoomXHight;
745 LONG SrcZoomXLow;
746 LONG SrcZoomYHight;
747 LONG SrcZoomYLow;
748
749 LONG sy_dec = 0;
750 LONG sy_max;
751
752 LONG sx_dec = 0;
753 LONG sx_max;
754 ULONG color;
755
756 DPRINT("DIB_16BPP_StretchBlt: Source BPP: %u, srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
757 BitsPerFormat(SourceSurf->iBitmapFormat), SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
758 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
759
760 /* Calc the Zoom height of Source */
761 SrcSizeY = SourceRect->bottom - SourceRect->top;
762
763 /* Calc the Zoom Width of Source */
764 SrcSizeX = SourceRect->right - SourceRect->left;
765
766 /* Calc the Zoom height of Destinations */
767 DesSizeY = DestRect->bottom - DestRect->top;
768
769 /* Calc the Zoom width of Destinations */
770 DesSizeX = DestRect->right - DestRect->left;
771
772 /* Calc the zoom factor of source height */
773 SrcZoomYHight = SrcSizeY / DesSizeY;
774 SrcZoomYLow = SrcSizeY - (SrcZoomYHight * DesSizeY);
775
776 /* Calc the zoom factor of source width */
777 SrcZoomXHight = SrcSizeX / DesSizeX;
778 SrcZoomXLow = SrcSizeX - (SrcZoomXHight * DesSizeX);
779
780 sx_max = DesSizeX;
781 sy_max = DesSizeY;
782 sy = SourceRect->top;
783
784 switch(SourceSurf->iBitmapFormat)
785 {
786
787 case BMF_1BPP:
788 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
789 /* This is a reference implementation, it hasn't been optimized for speed */
790
791 for (DesY=0; DesY<DesSizeY; DesY++)
792 {
793 sx = SourceRect->left;
794 sx_dec = 0;
795
796 for (DesX=0; DesX<DesSizeX; DesX++)
797 {
798 color = XLATEOBJ_iXlate(ColorTranslation,
799 DIB_1BPP_GetPixel(SourceSurf, sx, sy));
800
801 DIB_16BPP_PutPixel(DestSurf, DesX, DesY, color);
802
803 sx += SrcZoomXHight;
804 sx_dec += SrcZoomXLow;
805 if (sx_dec >= sx_max)
806 {
807 sx++;
808 sx_dec -= sx_max;
809 }
810 }
811
812 sy += SrcZoomYHight;
813 sy_dec += SrcZoomYLow;
814 if (sy_dec >= sy_max)
815 {
816 sy++;
817 sy_dec -= sy_max;
818 }
819 }
820 break;
821
822 case BMF_4BPP:
823 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
824 /* This is a reference implementation, it hasn't been optimized for speed */
825
826 for (DesY=0; DesY<DesSizeY; DesY++)
827 {
828 sx = SourceRect->left;
829 sx_dec = 0;
830
831 for (DesX=0; DesX<DesSizeX; DesX++)
832 {
833 color = XLATEOBJ_iXlate(ColorTranslation,
834 DIB_4BPP_GetPixel(SourceSurf, sx, sy));
835
836 DIB_16BPP_PutPixel(DestSurf, DesX, DesY, color);
837
838 sx += SrcZoomXHight;
839 sx_dec += SrcZoomXLow;
840 if (sx_dec >= sx_max)
841 {
842 sx++;
843 sx_dec -= sx_max;
844 }
845 }
846
847 sy += SrcZoomYHight;
848 sy_dec += SrcZoomYLow;
849 if (sy_dec >= sy_max)
850 {
851 sy++;
852 sy_dec -= sy_max;
853 }
854 }
855 break;
856
857 case BMF_8BPP:
858 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
859 /* This is a reference implementation, it hasn't been optimized for speed */
860
861 for (DesY=0; DesY<DesSizeY; DesY++)
862 {
863 sx = SourceRect->left;
864 sx_dec = 0;
865
866 for (DesX=0; DesX<DesSizeX; DesX++)
867 {
868 color = XLATEOBJ_iXlate(ColorTranslation,
869 DIB_8BPP_GetPixel(SourceSurf, sx, sy));
870
871 DIB_16BPP_PutPixel(DestSurf, DesX, DesY, color);
872
873 sx += SrcZoomXHight;
874 sx_dec += SrcZoomXLow;
875 if (sx_dec >= sx_max)
876 {
877 sx++;
878 sx_dec -= sx_max;
879 }
880 }
881
882 sy += SrcZoomYHight;
883 sy_dec += SrcZoomYLow;
884 if (sy_dec >= sy_max)
885 {
886 sy++;
887 sy_dec -= sy_max;
888 }
889 }
890 break;
891
892
893 case BMF_24BPP:
894 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
895 /* This is a reference implementation, it hasn't been optimized for speed */
896
897 for (DesY=0; DesY<DesSizeY; DesY++)
898 {
899 sx = SourceRect->left;
900 sx_dec = 0;
901
902 for (DesX=0; DesX<DesSizeX; DesX++)
903 {
904 color = XLATEOBJ_iXlate(ColorTranslation,
905 DIB_24BPP_GetPixel(SourceSurf, sx, sy));
906
907 DIB_16BPP_PutPixel(DestSurf, DesX, DesY, color);
908
909 sx += SrcZoomXHight;
910 sx_dec += SrcZoomXLow;
911 if (sx_dec >= sx_max)
912 {
913 sx++;
914 sx_dec -= sx_max;
915 }
916 }
917
918 sy += SrcZoomYHight;
919 sy_dec += SrcZoomYLow;
920 if (sy_dec >= sy_max)
921 {
922 sy++;
923 sy_dec -= sy_max;
924 }
925 }
926 break;
927
928 case BMF_32BPP:
929 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
930 /* This is a reference implementation, it hasn't been optimized for speed */
931
932 for (DesY=0; DesY<DesSizeY; DesY++)
933 {
934 sx = SourceRect->left;
935 sx_dec = 0;
936
937 for (DesX=0; DesX<DesSizeX; DesX++)
938 {
939 color = XLATEOBJ_iXlate(ColorTranslation,
940 DIB_32BPP_GetPixel(SourceSurf, sx, sy));
941
942 DIB_16BPP_PutPixel(DestSurf, DesX, DesY, color);
943
944 sx += SrcZoomXHight;
945 sx_dec += SrcZoomXLow;
946 if (sx_dec >= sx_max)
947 {
948 sx++;
949 sx_dec -= sx_max;
950 }
951 }
952
953 sy += SrcZoomYHight;
954 sy_dec += SrcZoomYLow;
955 if (sy_dec >= sy_max)
956 {
957 sy++;
958 sy_dec -= sy_max;
959 }
960 }
961 break;
962
963 case BMF_16BPP:
964 return ScaleRectAvg16(DestSurf, SourceSurf, DestRect, SourceRect, MaskOrigin, BrushOrigin,
965 ClipRegion, ColorTranslation, Mode);
966 break;
967
968 default:
969 DPRINT1("DIB_16BPP_StretchBlt: Unhandled Source BPP: %u\n", BitsPerFormat(SourceSurf->iBitmapFormat));
970 return FALSE;
971 }
972
973
974
975 return TRUE;
976 }
977
978 BOOLEAN
979 DIB_16BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
980 RECTL* DestRect, POINTL *SourcePoint,
981 XLATEOBJ *ColorTranslation, ULONG iTransColor)
982 {
983 ULONG RoundedRight, X, Y, SourceX, SourceY, Source, wd, Dest;
984 ULONG *DestBits;
985
986 RoundedRight = DestRect->right - ((DestRect->right - DestRect->left) & 0x1);
987 SourceY = SourcePoint->y;
988 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 +
989 (DestRect->left << 1) +
990 DestRect->top * DestSurf->lDelta);
991 wd = DestSurf->lDelta - ((DestRect->right - DestRect->left) << 1);
992
993 for(Y = DestRect->top; Y < DestRect->bottom; Y++)
994 {
995 SourceX = SourcePoint->x;
996 for(X = DestRect->left; X < RoundedRight; X += 2, DestBits++, SourceX += 2)
997 {
998 Dest = *DestBits;
999 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1000
1001 if(Source != iTransColor)
1002 {
1003 Dest &= 0xFFFF0000;
1004 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) & 0xFFFF);
1005 }
1006
1007 Source = DIB_GetSourceIndex(SourceSurf, SourceX + 1, SourceY);
1008 if(Source != iTransColor)
1009 {
1010 Dest &= 0xFFFF;
1011 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) << 16);
1012 }
1013
1014 *DestBits = Dest;
1015 }
1016
1017 if(X < DestRect->right)
1018 {
1019 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1020 if(Source != iTransColor)
1021 {
1022 *((USHORT*)DestBits) = (USHORT)XLATEOBJ_iXlate(ColorTranslation,
1023 Source);
1024 }
1025
1026 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
1027 }
1028
1029 SourceY++;
1030 DestBits = (ULONG*)((ULONG_PTR)DestBits + wd);
1031 }
1032
1033 return TRUE;
1034 }
1035
1036 typedef union
1037 {
1038 ULONG ul;
1039 struct
1040 {
1041 UCHAR red;
1042 UCHAR green;
1043 UCHAR blue;
1044 UCHAR alpha;
1045 } col;
1046 } NICEPIXEL32;
1047
1048 typedef union
1049 {
1050 USHORT us;
1051 struct
1052 {
1053 USHORT red:5,
1054 green:6,
1055 blue:5;
1056 } col;
1057 } NICEPIXEL16;
1058
1059 static __inline UCHAR
1060 Clamp5(ULONG val)
1061 {
1062 return (val > 31) ? 31 : val;
1063 }
1064
1065 static __inline UCHAR
1066 Clamp6(ULONG val)
1067 {
1068 return (val > 63) ? 63 : val;
1069 }
1070
1071 BOOLEAN
1072 DIB_16BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
1073 RECTL* SourceRect, CLIPOBJ* ClipRegion,
1074 XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
1075 {
1076 INT Rows, Cols, SrcX, SrcY;
1077 register PUSHORT Dst;
1078 ULONG DstDelta;
1079 BLENDFUNCTION BlendFunc;
1080 register NICEPIXEL16 DstPixel;
1081 register NICEPIXEL32 SrcPixel;
1082 UCHAR Alpha, SrcBpp;
1083
1084 DPRINT("DIB_16BPP_AlphaBlend: srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
1085 SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
1086 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
1087
1088 ASSERT(DestRect->bottom - DestRect->top == SourceRect->bottom - SourceRect->top &&
1089 DestRect->right - DestRect->left == SourceRect->right - SourceRect->left);
1090
1091 BlendFunc = BlendObj->BlendFunction;
1092 if (BlendFunc.BlendOp != AC_SRC_OVER)
1093 {
1094 DPRINT1("BlendOp != AC_SRC_OVER\n");
1095 return FALSE;
1096 }
1097 if (BlendFunc.BlendFlags != 0)
1098 {
1099 DPRINT1("BlendFlags != 0\n");
1100 return FALSE;
1101 }
1102 if ((BlendFunc.AlphaFormat & ~AC_SRC_ALPHA) != 0)
1103 {
1104 DPRINT1("Unsupported AlphaFormat (0x%x)\n", BlendFunc.AlphaFormat);
1105 return FALSE;
1106 }
1107 if ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0 &&
1108 BitsPerFormat(Source->iBitmapFormat) != 32)
1109 {
1110 DPRINT1("Source bitmap must be 32bpp when AC_SRC_ALPHA is set\n");
1111 return FALSE;
1112 }
1113
1114 Dst = (PUSHORT)((ULONG_PTR)Dest->pvScan0 + (DestRect->top * Dest->lDelta) +
1115 (DestRect->left << 1));
1116 DstDelta = Dest->lDelta - ((DestRect->right - DestRect->left) << 1);
1117 SrcBpp = BitsPerFormat(Source->iBitmapFormat);
1118
1119 Rows = DestRect->bottom - DestRect->top;
1120 SrcY = SourceRect->top;
1121 while (--Rows >= 0)
1122 {
1123 Cols = DestRect->right - DestRect->left;
1124 SrcX = SourceRect->left;
1125 while (--Cols >= 0)
1126 {
1127 if (SrcBpp <= 16)
1128 {
1129 DstPixel.us = DIB_GetSource(Source, SrcX++, SrcY, ColorTranslation);
1130 SrcPixel.col.red = (DstPixel.col.red << 3) | (DstPixel.col.red >> 2);
1131
1132 SrcPixel.col.green = (DstPixel.col.green << 2) |
1133 (DstPixel.col.green >> 4);
1134
1135 SrcPixel.col.blue = (DstPixel.col.blue << 3) | (DstPixel.col.blue >> 2);
1136 }
1137 else
1138 {
1139 SrcPixel.ul = DIB_GetSourceIndex(Source, SrcX++, SrcY);
1140 }
1141 SrcPixel.col.red = SrcPixel.col.red *
1142 BlendFunc.SourceConstantAlpha / 255;
1143
1144 SrcPixel.col.green = SrcPixel.col.green *
1145 BlendFunc.SourceConstantAlpha / 255;
1146
1147 SrcPixel.col.blue = SrcPixel.col.blue *
1148 BlendFunc.SourceConstantAlpha / 255;
1149
1150 SrcPixel.col.alpha = (SrcBpp == 32) ?
1151 (SrcPixel.col.alpha *
1152 BlendFunc.SourceConstantAlpha / 255) :
1153 BlendFunc.SourceConstantAlpha;
1154
1155 Alpha = ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0) ?
1156 SrcPixel.col.alpha : BlendFunc.SourceConstantAlpha;
1157
1158 DstPixel.us = *Dst;
1159 DstPixel.col.red = Clamp5(DstPixel.col.red * (255 - Alpha) / 255 +
1160 (SrcPixel.col.red >> 3));
1161
1162 DstPixel.col.green = Clamp6(DstPixel.col.green * (255 - Alpha) / 255 +
1163 (SrcPixel.col.green >> 2));
1164
1165 DstPixel.col.blue = Clamp5(DstPixel.col.blue * (255 - Alpha) / 255 +
1166 (SrcPixel.col.blue >> 3));
1167
1168 *Dst++ = DstPixel.us;
1169 }
1170
1171 Dst = (PUSHORT)((ULONG_PTR)Dst + DstDelta);
1172 SrcY++;
1173 }
1174
1175 return TRUE;
1176 }
1177
1178 /* EOF */