- Make sure memory allocation succeeded
[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 PULONG DestBits;
744 LONG DifflDelta;
745
746 LONG SrcZoomXHight;
747 LONG SrcZoomXLow;
748 LONG SrcZoomYHight;
749 LONG SrcZoomYLow;
750
751 LONG sy_dec = 0;
752 LONG sy_max;
753
754 LONG sx_dec = 0;
755 LONG sx_max;
756
757 DPRINT("DIB_16BPP_StretchBlt: Source BPP: %u, srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
758 BitsPerFormat(SourceSurf->iBitmapFormat), SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
759 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
760
761 /* Calc the Zoom height of Source */
762 SrcSizeY = SourceRect->bottom - SourceRect->top;
763
764 /* Calc the Zoom Width of Source */
765 SrcSizeX = SourceRect->right - SourceRect->left;
766
767 /* Calc the Zoom height of Destions */
768 DesSizeY = DestRect->bottom - DestRect->top;
769
770 /* Calc the Zoom width of Destions */
771 DesSizeX = DestRect->right - DestRect->left;
772
773 /* Calc the zoom factor of soruce height */
774 SrcZoomYHight = SrcSizeY / DesSizeY;
775 SrcZoomYLow = SrcSizeY - (SrcZoomYHight * DesSizeY);
776
777 /* Calc the zoom factor of soruce width */
778 SrcZoomXHight = SrcSizeX / DesSizeX;
779 SrcZoomXLow = SrcSizeX - (SrcZoomXHight * DesSizeX);
780
781 sx_max = DesSizeX;
782 sy_max = DesSizeY;
783 sy = SourceRect->top;
784
785 DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 1) +
786 DestRect->top * DestSurf->lDelta);
787
788 DifflDelta = DestSurf->lDelta - (DesSizeX << 1);
789
790 switch(SourceSurf->iBitmapFormat)
791 {
792
793 case BMF_1BPP:
794 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
795 /* This is a reference implementation, it hasn't been optimized for speed */
796
797 for (DesY=0; DesY<DesSizeY; DesY++)
798 {
799 sx = SourceRect->left;
800 sx_dec = 0;
801
802 for (DesX=0; DesX<DesSizeX; DesX++)
803 {
804 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
805 DIB_1BPP_GetPixel(SourceSurf, sx, sy));
806
807 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
808
809 sx += SrcZoomXHight;
810 sx_dec += SrcZoomXLow;
811 if (sx_dec >= sx_max)
812 {
813 sx++;
814 sx_dec -= sx_max;
815 }
816 }
817
818 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
819
820 sy += SrcZoomYHight;
821 sy_dec += SrcZoomYLow;
822 if (sy_dec >= sy_max)
823 {
824 sy++;
825 sy_dec -= sy_max;
826 }
827 }
828 break;
829
830 case BMF_4BPP:
831 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
832 /* This is a reference implementation, it hasn't been optimized for speed */
833
834 for (DesY=0; DesY<DesSizeY; DesY++)
835 {
836 sx = SourceRect->left;
837 sx_dec = 0;
838
839 for (DesX=0; DesX<DesSizeX; DesX++)
840 {
841 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
842 DIB_4BPP_GetPixel(SourceSurf, sx, sy));
843
844 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
845
846 sx += SrcZoomXHight;
847 sx_dec += SrcZoomXLow;
848 if (sx_dec >= sx_max)
849 {
850 sx++;
851 sx_dec -= sx_max;
852 }
853 }
854
855 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
856
857 sy += SrcZoomYHight;
858 sy_dec += SrcZoomYLow;
859 if (sy_dec >= sy_max)
860 {
861 sy++;
862 sy_dec -= sy_max;
863 }
864 }
865 break;
866
867 case BMF_8BPP:
868 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
869 /* This is a reference implementation, it hasn't been optimized for speed */
870
871 for (DesY=0; DesY<DesSizeY; DesY++)
872 {
873 sx = SourceRect->left;
874 sx_dec = 0;
875
876 for (DesX=0; DesX<DesSizeX; DesX++)
877 {
878 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
879 DIB_8BPP_GetPixel(SourceSurf, sx, sy));
880
881 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
882
883 sx += SrcZoomXHight;
884 sx_dec += SrcZoomXLow;
885 if (sx_dec >= sx_max)
886 {
887 sx++;
888 sx_dec -= sx_max;
889 }
890 }
891
892 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
893
894 sy += SrcZoomYHight;
895 sy_dec += SrcZoomYLow;
896 if (sy_dec >= sy_max)
897 {
898 sy++;
899 sy_dec -= sy_max;
900 }
901 }
902 break;
903
904
905 case BMF_24BPP:
906 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
907 /* This is a reference implementation, it hasn't been optimized for speed */
908
909 DestBits = (PULONG)((PBYTE)DestSurf->pvScan0 + (DestRect->left << 1) +
910 DestRect->top * DestSurf->lDelta);
911
912 DifflDelta = DestSurf->lDelta - (DesSizeX << 1);
913
914 for (DesY=0; DesY<DesSizeY; DesY++)
915 {
916 sx = SourceRect->left;
917 sx_dec = 0;
918
919 for (DesX=0; DesX<DesSizeX; DesX++)
920 {
921 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
922 DIB_24BPP_GetPixel(SourceSurf, sx, sy));
923
924 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
925
926 sx += SrcZoomXHight;
927 sx_dec += SrcZoomXLow;
928 if (sx_dec >= sx_max)
929 {
930 sx++;
931 sx_dec -= sx_max;
932 }
933 }
934
935 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
936
937 sy += SrcZoomYHight;
938 sy_dec += SrcZoomYLow;
939 if (sy_dec >= sy_max)
940 {
941 sy++;
942 sy_dec -= sy_max;
943 }
944 }
945 break;
946
947 case BMF_32BPP:
948 /* FIXME : MaskOrigin, BrushOrigin, ClipRegion, Mode ? */
949 /* This is a reference implementation, it hasn't been optimized for speed */
950
951 for (DesY=0; DesY<DesSizeY; DesY++)
952 {
953 sx = SourceRect->left;
954 sx_dec = 0;
955
956 for (DesX=0; DesX<DesSizeX; DesX++)
957 {
958 *DestBits = XLATEOBJ_iXlate(ColorTranslation,
959 DIB_32BPP_GetPixel(SourceSurf, sx, sy));
960
961 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
962
963 sx += SrcZoomXHight;
964 sx_dec += SrcZoomXLow;
965 if (sx_dec >= sx_max)
966 {
967 sx++;
968 sx_dec -= sx_max;
969 }
970 }
971 DestBits = (PULONG)((ULONG_PTR)DestBits + DifflDelta);
972
973 sy += SrcZoomYHight;
974 sy_dec += SrcZoomYLow;
975 if (sy_dec >= sy_max)
976 {
977 sy++;
978 sy_dec -= sy_max;
979 }
980 }
981 break;
982
983 case BMF_16BPP:
984 return ScaleRectAvg16(DestSurf, SourceSurf, DestRect, SourceRect, MaskOrigin, BrushOrigin,
985 ClipRegion, ColorTranslation, Mode);
986 break;
987
988 default:
989 DPRINT1("DIB_16BPP_StretchBlt: Unhandled Source BPP: %u\n", BitsPerFormat(SourceSurf->iBitmapFormat));
990 return FALSE;
991 }
992
993
994
995 return TRUE;
996 }
997
998 BOOLEAN
999 DIB_16BPP_TransparentBlt(SURFOBJ *DestSurf, SURFOBJ *SourceSurf,
1000 RECTL* DestRect, POINTL *SourcePoint,
1001 XLATEOBJ *ColorTranslation, ULONG iTransColor)
1002 {
1003 ULONG RoundedRight, X, Y, SourceX, SourceY, Source, wd, Dest;
1004 ULONG *DestBits;
1005
1006 RoundedRight = DestRect->right - ((DestRect->right - DestRect->left) & 0x1);
1007 SourceY = SourcePoint->y;
1008 DestBits = (ULONG*)((PBYTE)DestSurf->pvScan0 +
1009 (DestRect->left << 1) +
1010 DestRect->top * DestSurf->lDelta);
1011 wd = DestSurf->lDelta - ((DestRect->right - DestRect->left) << 1);
1012
1013 for(Y = DestRect->top; Y < DestRect->bottom; Y++)
1014 {
1015 SourceX = SourcePoint->x;
1016 for(X = DestRect->left; X < RoundedRight; X += 2, DestBits++, SourceX += 2)
1017 {
1018 Dest = *DestBits;
1019 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1020
1021 if(Source != iTransColor)
1022 {
1023 Dest &= 0xFFFF0000;
1024 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) & 0xFFFF);
1025 }
1026
1027 Source = DIB_GetSourceIndex(SourceSurf, SourceX + 1, SourceY);
1028 if(Source != iTransColor)
1029 {
1030 Dest &= 0xFFFF;
1031 Dest |= (XLATEOBJ_iXlate(ColorTranslation, Source) << 16);
1032 }
1033
1034 *DestBits = Dest;
1035 }
1036
1037 if(X < DestRect->right)
1038 {
1039 Source = DIB_GetSourceIndex(SourceSurf, SourceX, SourceY);
1040 if(Source != iTransColor)
1041 {
1042 *((USHORT*)DestBits) = (USHORT)XLATEOBJ_iXlate(ColorTranslation,
1043 Source);
1044 }
1045
1046 DestBits = (PULONG)((ULONG_PTR)DestBits + 2);
1047 }
1048
1049 SourceY++;
1050 DestBits = (ULONG*)((ULONG_PTR)DestBits + wd);
1051 }
1052
1053 return TRUE;
1054 }
1055
1056 typedef union
1057 {
1058 ULONG ul;
1059 struct
1060 {
1061 UCHAR red;
1062 UCHAR green;
1063 UCHAR blue;
1064 UCHAR alpha;
1065 } col;
1066 } NICEPIXEL32;
1067
1068 typedef union
1069 {
1070 USHORT us;
1071 struct
1072 {
1073 USHORT red:5,
1074 green:6,
1075 blue:5;
1076 } col;
1077 } NICEPIXEL16;
1078
1079 static __inline UCHAR
1080 Clamp5(ULONG val)
1081 {
1082 return (val > 31) ? 31 : val;
1083 }
1084
1085 static __inline UCHAR
1086 Clamp6(ULONG val)
1087 {
1088 return (val > 63) ? 63 : val;
1089 }
1090
1091 BOOLEAN
1092 DIB_16BPP_AlphaBlend(SURFOBJ* Dest, SURFOBJ* Source, RECTL* DestRect,
1093 RECTL* SourceRect, CLIPOBJ* ClipRegion,
1094 XLATEOBJ* ColorTranslation, BLENDOBJ* BlendObj)
1095 {
1096 INT Rows, Cols, SrcX, SrcY;
1097 register PUSHORT Dst;
1098 ULONG DstDelta;
1099 BLENDFUNCTION BlendFunc;
1100 register NICEPIXEL16 DstPixel;
1101 register NICEPIXEL32 SrcPixel;
1102 UCHAR Alpha, SrcBpp;
1103
1104 DPRINT("DIB_16BPP_AlphaBlend: srcRect: (%d,%d)-(%d,%d), dstRect: (%d,%d)-(%d,%d)\n",
1105 SourceRect->left, SourceRect->top, SourceRect->right, SourceRect->bottom,
1106 DestRect->left, DestRect->top, DestRect->right, DestRect->bottom);
1107
1108 ASSERT(DestRect->bottom - DestRect->top == SourceRect->bottom - SourceRect->top &&
1109 DestRect->right - DestRect->left == SourceRect->right - SourceRect->left);
1110
1111 BlendFunc = BlendObj->BlendFunction;
1112 if (BlendFunc.BlendOp != AC_SRC_OVER)
1113 {
1114 DPRINT1("BlendOp != AC_SRC_OVER\n");
1115 return FALSE;
1116 }
1117 if (BlendFunc.BlendFlags != 0)
1118 {
1119 DPRINT1("BlendFlags != 0\n");
1120 return FALSE;
1121 }
1122 if ((BlendFunc.AlphaFormat & ~AC_SRC_ALPHA) != 0)
1123 {
1124 DPRINT1("Unsupported AlphaFormat (0x%x)\n", BlendFunc.AlphaFormat);
1125 return FALSE;
1126 }
1127 if ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0 &&
1128 BitsPerFormat(Source->iBitmapFormat) != 32)
1129 {
1130 DPRINT1("Source bitmap must be 32bpp when AC_SRC_ALPHA is set\n");
1131 return FALSE;
1132 }
1133
1134 Dst = (PUSHORT)((ULONG_PTR)Dest->pvScan0 + (DestRect->top * Dest->lDelta) +
1135 (DestRect->left << 1));
1136 DstDelta = Dest->lDelta - ((DestRect->right - DestRect->left) << 1);
1137 SrcBpp = BitsPerFormat(Source->iBitmapFormat);
1138
1139 Rows = DestRect->bottom - DestRect->top;
1140 SrcY = SourceRect->top;
1141 while (--Rows >= 0)
1142 {
1143 Cols = DestRect->right - DestRect->left;
1144 SrcX = SourceRect->left;
1145 while (--Cols >= 0)
1146 {
1147 if (SrcBpp <= 16)
1148 {
1149 DstPixel.us = DIB_GetSource(Source, SrcX++, SrcY, ColorTranslation);
1150 SrcPixel.col.red = (DstPixel.col.red << 3) | (DstPixel.col.red >> 2);
1151
1152 SrcPixel.col.green = (DstPixel.col.green << 2) |
1153 (DstPixel.col.green >> 4);
1154
1155 SrcPixel.col.blue = (DstPixel.col.blue << 3) | (DstPixel.col.blue >> 2);
1156 }
1157 else
1158 {
1159 SrcPixel.ul = DIB_GetSourceIndex(Source, SrcX++, SrcY);
1160 }
1161 SrcPixel.col.red = SrcPixel.col.red *
1162 BlendFunc.SourceConstantAlpha / 255;
1163
1164 SrcPixel.col.green = SrcPixel.col.green *
1165 BlendFunc.SourceConstantAlpha / 255;
1166
1167 SrcPixel.col.blue = SrcPixel.col.blue *
1168 BlendFunc.SourceConstantAlpha / 255;
1169
1170 SrcPixel.col.alpha = (SrcBpp == 32) ?
1171 (SrcPixel.col.alpha *
1172 BlendFunc.SourceConstantAlpha / 255) :
1173 BlendFunc.SourceConstantAlpha;
1174
1175 Alpha = ((BlendFunc.AlphaFormat & AC_SRC_ALPHA) != 0) ?
1176 SrcPixel.col.alpha : BlendFunc.SourceConstantAlpha;
1177
1178 DstPixel.us = *Dst;
1179 DstPixel.col.red = Clamp5(DstPixel.col.red * (255 - Alpha) / 255 +
1180 (SrcPixel.col.red >> 3));
1181
1182 DstPixel.col.green = Clamp6(DstPixel.col.green * (255 - Alpha) / 255 +
1183 (SrcPixel.col.green >> 2));
1184
1185 DstPixel.col.blue = Clamp5(DstPixel.col.blue * (255 - Alpha) / 255 +
1186 (SrcPixel.col.blue >> 3));
1187
1188 *Dst++ = DstPixel.us;
1189 }
1190
1191 Dst = (PUSHORT)((ULONG_PTR)Dst + DstDelta);
1192 SrcY++;
1193 }
1194
1195 return TRUE;
1196 }
1197
1198 /* EOF */