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