DC: use dc.DcLevel.hPal for the palette instead of using dc.PalIndexed and dc.w.hPalette
[reactos.git] / reactos / subsystems / win32 / win32k / objects / bitmaps.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$ */
20
21 #include <w32k.h>
22
23 #define NDEBUG
24 #include <debug.h>
25
26 #define IN_RECT(r,x,y) \
27 ( \
28 (x) >= (r).left && \
29 (y) >= (r).top && \
30 (x) < (r).right && \
31 (y) < (r).bottom \
32 )
33
34 HBITMAP STDCALL
35 IntGdiCreateBitmap(
36 INT Width,
37 INT Height,
38 UINT Planes,
39 UINT BitsPixel,
40 IN OPTIONAL LPBYTE pBits)
41 {
42 HBITMAP hBitmap;
43 SIZEL Size;
44 LONG WidthBytes;
45
46 /* NOTE: Windows also doesn't store nr. of planes separately! */
47 BitsPixel = BITMAPOBJ_GetRealBitsPixel(BitsPixel * Planes);
48
49 /* Check parameters */
50 if (BitsPixel == 0 || Width < 0)
51 {
52 DPRINT1("Width = %d, Height = %d BitsPixel = %d\n", Width, Height, BitsPixel);
53 SetLastWin32Error(ERROR_INVALID_PARAMETER);
54 return 0;
55 }
56
57 WidthBytes = BITMAPOBJ_GetWidthBytes(Width, Planes * BitsPixel);
58
59 Size.cx = abs(Width);
60 Size.cy = abs(Height);
61
62 /* Create the bitmap object. */
63 hBitmap = IntCreateBitmap(Size, WidthBytes,
64 BitmapFormat(BitsPixel, BI_RGB),
65 (Height < 0 ? BMF_TOPDOWN : 0) |
66 (NULL == pBits ? 0 : BMF_NOZEROINIT), NULL);
67 if (!hBitmap)
68 {
69 DPRINT("IntGdiCreateBitmap: returned 0\n");
70 return 0;
71 }
72
73 PBITMAPOBJ bmp = BITMAPOBJ_LockBitmap( hBitmap );
74 if (bmp == NULL)
75 {
76 NtGdiDeleteObject(hBitmap);
77 return NULL;
78 }
79
80 bmp->flFlags = BITMAPOBJ_IS_APIBITMAP;
81 bmp->hDC = NULL; // Fixme
82
83 if (NULL != pBits)
84 {
85 IntSetBitmapBits(bmp, bmp->SurfObj.cjBits, pBits);
86 }
87
88 BITMAPOBJ_UnlockBitmap( bmp );
89
90 DPRINT("IntGdiCreateBitmap : %dx%d, %d BPP colors, topdown %d, returning %08x\n",
91 Size.cx, Size.cy, BitsPixel, (Height < 0 ? 1 : 0), hBitmap);
92
93 return hBitmap;
94 }
95
96
97 HBITMAP STDCALL
98 NtGdiCreateBitmap(
99 INT Width,
100 INT Height,
101 UINT Planes,
102 UINT BitsPixel,
103 IN OPTIONAL LPBYTE pUnsafeBits)
104 {
105 HBITMAP hBitmap;
106
107 _SEH_TRY
108 {
109 if (pUnsafeBits)
110 {
111 UINT cjBits = BITMAPOBJ_GetWidthBytes(Width, BitsPixel) * abs(Height);
112 ProbeForRead(pUnsafeBits, cjBits, 1);
113 }
114
115 if (0 == Width || 0 == Height)
116 {
117 hBitmap = IntGdiCreateBitmap (1, 1, 1, 1, NULL);
118 }
119 else
120 {
121 hBitmap = IntGdiCreateBitmap(Width, Height, Planes, BitsPixel, pUnsafeBits);
122 }
123 }
124 _SEH_HANDLE
125 {
126 hBitmap = 0;
127 }
128 _SEH_END
129
130 return hBitmap;
131 }
132
133 BOOL INTERNAL_CALL
134 BITMAP_Cleanup(PVOID ObjectBody)
135 {
136 PBITMAPOBJ pBmp = (PBITMAPOBJ)ObjectBody;
137 if (pBmp->SurfObj.pvBits != NULL &&
138 (pBmp->flFlags & BITMAPOBJ_IS_APIBITMAP))
139 {
140 if (pBmp->dib == NULL)
141 {
142 if (pBmp->SurfObj.pvBits != NULL)
143 ExFreePool(pBmp->SurfObj.pvBits);
144 }
145 else
146 {
147 if (pBmp->SurfObj.pvBits != NULL)
148 EngFreeUserMem(pBmp->SurfObj.pvBits);
149 }
150 if (pBmp->hDIBPalette != NULL)
151 {
152 NtGdiDeleteObject(pBmp->hDIBPalette);
153 }
154 }
155
156 if (NULL != pBmp->BitsLock)
157 {
158 ExFreePoolWithTag(pBmp->BitsLock, TAG_BITMAPOBJ);
159 pBmp->BitsLock = NULL;
160 }
161
162 return TRUE;
163 }
164
165
166 HBITMAP FASTCALL
167 IntCreateCompatibleBitmap(
168 PDC Dc,
169 INT Width,
170 INT Height)
171 {
172 HBITMAP Bmp;
173
174 Bmp = NULL;
175
176 if ((Width >= 0x10000) || (Height >= 0x10000))
177 {
178 DPRINT1("got bad width %d or height %d, please look for reason\n", Width, Height);
179 return NULL;
180 }
181
182 /* MS doc says if width or height is 0, return 1-by-1 pixel, monochrome bitmap */
183 if (0 == Width || 0 == Height)
184 {
185 Bmp = IntGdiCreateBitmap (1, 1, 1, 1, NULL);
186 }
187 else
188 {
189 Bmp = IntGdiCreateBitmap(abs(Width), abs(Height), 1, Dc->w.bitsPerPixel, NULL);
190 }
191
192 return Bmp;
193 }
194
195 HBITMAP STDCALL
196 NtGdiCreateCompatibleBitmap(
197 HDC hDC,
198 INT Width,
199 INT Height)
200 {
201 HBITMAP Bmp;
202 PDC Dc;
203
204 Dc = DC_LockDc(hDC);
205
206 DPRINT("NtGdiCreateCompatibleBitmap(%04x,%d,%d, bpp:%d) = \n", hDC, Width, Height, Dc->w.bitsPerPixel);
207
208 if (NULL == Dc)
209 {
210 SetLastWin32Error(ERROR_INVALID_HANDLE);
211 return NULL;
212 }
213
214 Bmp = IntCreateCompatibleBitmap(Dc, Width, Height);
215
216 DPRINT ("\t\t%04x\n", Bmp);
217 DC_UnlockDc(Dc);
218 return Bmp;
219 }
220
221 BOOL STDCALL
222 NtGdiGetBitmapDimension(
223 HBITMAP hBitmap,
224 LPSIZE Dimension)
225 {
226 PBITMAPOBJ bmp;
227
228 bmp = BITMAPOBJ_LockBitmap(hBitmap);
229 if (bmp == NULL)
230 {
231 return FALSE;
232 }
233
234 *Dimension = bmp->dimension;
235
236 BITMAPOBJ_UnlockBitmap(bmp);
237
238 return TRUE;
239 }
240
241 COLORREF STDCALL
242 NtGdiGetPixel(HDC hDC, INT XPos, INT YPos)
243 {
244 PDC dc = NULL;
245 COLORREF Result = (COLORREF)CLR_INVALID; // default to failure
246 BOOL bInRect = FALSE;
247 BITMAPOBJ *BitmapObject;
248 SURFOBJ *SurfaceObject;
249 HPALETTE Pal = 0;
250 XLATEOBJ *XlateObj;
251 HBITMAP hBmpTmp;
252
253 dc = DC_LockDc (hDC);
254
255 if ( !dc )
256 {
257 SetLastWin32Error(ERROR_INVALID_HANDLE);
258 return Result;
259 }
260 if (dc->DC_Type == DC_TYPE_INFO)
261 {
262 DC_UnlockDc(dc);
263 return Result;
264 }
265 XPos += dc->w.DCOrgX;
266 YPos += dc->w.DCOrgY;
267 if ( IN_RECT(dc->CombinedClip->rclBounds,XPos,YPos) )
268 {
269 bInRect = TRUE;
270 BitmapObject = BITMAPOBJ_LockBitmap(dc->w.hBitmap);
271 SurfaceObject = &BitmapObject->SurfObj;
272 if ( BitmapObject )
273 {
274 if ( dc->DcLevel.hpal != 0 )
275 Pal = dc->DcLevel.hpal;
276 /* FIXME: Verify if it shouldn't be PAL_BGR! */
277 XlateObj = (XLATEOBJ*)IntEngCreateXlate ( PAL_RGB, 0, NULL, Pal );
278 if ( XlateObj )
279 {
280 // check if this DC has a DIB behind it...
281 if ( SurfaceObject->pvScan0 ) // STYPE_BITMAP == SurfaceObject->iType
282 {
283 ASSERT ( SurfaceObject->lDelta );
284 Result = XLATEOBJ_iXlate(XlateObj,
285 DibFunctionsForBitmapFormat[SurfaceObject->iBitmapFormat].DIB_GetPixel ( SurfaceObject, XPos, YPos ) );
286 }
287 EngDeleteXlate(XlateObj);
288 }
289 BITMAPOBJ_UnlockBitmap(BitmapObject);
290 }
291 }
292 DC_UnlockDc(dc);
293
294 // if Result is still CLR_INVALID, then the "quick" method above didn't work
295 if ( bInRect && Result == CLR_INVALID )
296 {
297 // FIXME: create a 1x1 32BPP DIB, and blit to it
298 HDC hDCTmp = NtGdiCreateCompatibleDC(hDC);
299 if ( hDCTmp )
300 {
301 static const BITMAPINFOHEADER bih = { sizeof(BITMAPINFOHEADER), 1, 1, 1, 32, BI_RGB, 0, 0, 0, 0, 0 };
302 BITMAPINFO bi;
303 RtlMoveMemory ( &(bi.bmiHeader), &bih, sizeof(bih) );
304 hBmpTmp = NtGdiCreateDIBitmapInternal(hDC,
305 bi.bmiHeader.biWidth,
306 bi.bmiHeader.biHeight,
307 0,
308 NULL,
309 &bi,
310 DIB_RGB_COLORS,
311 bi.bmiHeader.biBitCount,
312 bi.bmiHeader.biSizeImage,
313 0,
314 0);
315
316 //HBITMAP hBmpTmp = IntGdiCreateBitmap ( 1, 1, 1, 32, NULL);
317 if ( hBmpTmp )
318 {
319 HBITMAP hBmpOld = (HBITMAP)NtGdiSelectBitmap ( hDCTmp, hBmpTmp );
320 if ( hBmpOld )
321 {
322 PBITMAPOBJ bmpobj;
323
324 NtGdiBitBlt ( hDCTmp, 0, 0, 1, 1, hDC, XPos, YPos, SRCCOPY, 0, 0 );
325 NtGdiSelectBitmap ( hDCTmp, hBmpOld );
326
327 // our bitmap is no longer selected, so we can access it's stuff...
328 bmpobj = BITMAPOBJ_LockBitmap ( hBmpTmp );
329 if ( bmpobj )
330 {
331 Result = *(COLORREF*)bmpobj->SurfObj.pvScan0;
332 BITMAPOBJ_UnlockBitmap ( bmpobj );
333 }
334 }
335 NtGdiDeleteObject ( hBmpTmp );
336 }
337 NtGdiDeleteObjectApp ( hDCTmp );
338 }
339 }
340
341 return Result;
342 }
343
344
345 LONG STDCALL
346 IntGetBitmapBits(
347 PBITMAPOBJ bmp,
348 DWORD Bytes,
349 OUT PBYTE Bits)
350 {
351 LONG ret;
352
353 ASSERT(Bits);
354
355 /* Don't copy more bytes than the buffer has */
356 Bytes = min(Bytes, bmp->SurfObj.cjBits);
357
358 #if 0
359 /* FIXME: Call DDI CopyBits here if available */
360 if(bmp->DDBitmap)
361 {
362 DPRINT("Calling device specific BitmapBits\n");
363 if(bmp->DDBitmap->funcs->pBitmapBits)
364 {
365 ret = bmp->DDBitmap->funcs->pBitmapBits(hbitmap, bits, count, DDB_GET);
366 }
367 else
368 {
369 ERR_(bitmap)("BitmapBits == NULL??\n");
370 ret = 0;
371 }
372 }
373 else
374 #endif
375 {
376 RtlCopyMemory(Bits, bmp->SurfObj.pvBits, Bytes);
377 ret = Bytes;
378 }
379 return ret;
380 }
381
382 LONG STDCALL
383 NtGdiGetBitmapBits(HBITMAP hBitmap,
384 ULONG Bytes,
385 OUT OPTIONAL PBYTE pUnsafeBits)
386 {
387 PBITMAPOBJ bmp;
388 LONG ret;
389
390 if (pUnsafeBits != NULL && Bytes == 0)
391 {
392 return 0;
393 }
394
395 bmp = BITMAPOBJ_LockBitmap (hBitmap);
396 if (!bmp)
397 {
398 SetLastWin32Error(ERROR_INVALID_HANDLE);
399 return 0;
400 }
401
402 /* If the bits vector is null, the function should return the read size */
403 if (pUnsafeBits == NULL)
404 {
405 ret = bmp->SurfObj.cjBits;
406 BITMAPOBJ_UnlockBitmap (bmp);
407 return ret;
408 }
409
410 /* Don't copy more bytes than the buffer has */
411 Bytes = min(Bytes, bmp->SurfObj.cjBits);
412
413 _SEH_TRY
414 {
415 ProbeForWrite(pUnsafeBits, Bytes, 1);
416 ret = IntGetBitmapBits(bmp, Bytes, pUnsafeBits);
417 }
418 _SEH_HANDLE
419 {
420 ret = 0;
421 }
422 _SEH_END
423
424 BITMAPOBJ_UnlockBitmap (bmp);
425
426 return ret;
427 }
428
429
430 LONG STDCALL
431 IntSetBitmapBits(
432 PBITMAPOBJ bmp,
433 DWORD Bytes,
434 IN PBYTE Bits)
435 {
436 LONG ret;
437
438 /* Don't copy more bytes than the buffer has */
439 Bytes = min(Bytes, bmp->SurfObj.cjBits);
440
441 #if 0
442 /* FIXME: call DDI specific function here if available */
443 if(bmp->DDBitmap)
444 {
445 DPRINT ("Calling device specific BitmapBits\n");
446 if (bmp->DDBitmap->funcs->pBitmapBits)
447 {
448 ret = bmp->DDBitmap->funcs->pBitmapBits(hBitmap, (void *) Bits, Bytes, DDB_SET);
449 }
450 else
451 {
452 DPRINT ("BitmapBits == NULL??\n");
453 ret = 0;
454 }
455 }
456 else
457 #endif
458 {
459 RtlCopyMemory(bmp->SurfObj.pvBits, Bits, Bytes);
460 ret = Bytes;
461 }
462
463 return ret;
464 }
465
466
467 LONG STDCALL
468 NtGdiSetBitmapBits(
469 HBITMAP hBitmap,
470 DWORD Bytes,
471 IN PBYTE pUnsafeBits)
472 {
473 LONG ret;
474 PBITMAPOBJ bmp;
475
476 if (pUnsafeBits == NULL || Bytes == 0)
477 {
478 return 0;
479 }
480
481 bmp = BITMAPOBJ_LockBitmap(hBitmap);
482 if (bmp == NULL)
483 {
484 SetLastWin32Error(ERROR_INVALID_HANDLE);
485 return 0;
486 }
487
488 _SEH_TRY
489 {
490 ProbeForRead(pUnsafeBits, Bytes, 1);
491 ret = IntSetBitmapBits(bmp, Bytes, pUnsafeBits);
492 }
493 _SEH_HANDLE
494 {
495 ret = 0;
496 }
497 _SEH_END
498
499 BITMAPOBJ_UnlockBitmap(bmp);
500
501 return ret;
502 }
503
504 BOOL STDCALL
505 NtGdiSetBitmapDimension(
506 HBITMAP hBitmap,
507 INT Width,
508 INT Height,
509 LPSIZE Size)
510 {
511 PBITMAPOBJ bmp;
512
513 bmp = BITMAPOBJ_LockBitmap(hBitmap);
514 if (bmp == NULL)
515 {
516 return FALSE;
517 }
518
519 if (Size)
520 {
521 *Size = bmp->dimension;
522 }
523 bmp->dimension.cx = Width;
524 bmp->dimension.cy = Height;
525
526 BITMAPOBJ_UnlockBitmap (bmp);
527
528 return TRUE;
529 }
530
531 BOOL STDCALL
532 GdiSetPixelV(
533 HDC hDC,
534 INT X,
535 INT Y,
536 COLORREF Color)
537 {
538 HBRUSH NewBrush = NtGdiCreateSolidBrush(Color, NULL);
539 HGDIOBJ OldBrush;
540
541 if (NewBrush == NULL)
542 return(FALSE);
543 OldBrush = NtGdiSelectBrush(hDC, NewBrush);
544 if (OldBrush == NULL)
545 {
546 NtGdiDeleteObject(NewBrush);
547 return(FALSE);
548 }
549 NtGdiPatBlt(hDC, X, Y, 1, 1, PATCOPY);
550 NtGdiSelectBrush(hDC, OldBrush);
551 NtGdiDeleteObject(NewBrush);
552 return TRUE;
553 }
554
555 COLORREF STDCALL
556 NtGdiSetPixel(
557 HDC hDC,
558 INT X,
559 INT Y,
560 COLORREF Color)
561 {
562 DPRINT("0 NtGdiSetPixel X %ld Y %ld C %ld\n",X,Y,Color);
563
564 if (GdiSetPixelV(hDC,X,Y,Color))
565 {
566 Color = NtGdiGetPixel(hDC,X,Y);
567 DPRINT("1 NtGdiSetPixel X %ld Y %ld C %ld\n",X,Y,Color);
568 return Color;
569 }
570
571 Color = ((COLORREF) CLR_INVALID);
572 DPRINT("2 NtGdiSetPixel X %ld Y %ld C %ld\n",X,Y,Color);
573 return Color;
574 }
575
576
577 /* Internal Functions */
578
579 INT FASTCALL
580 BITMAPOBJ_GetRealBitsPixel(INT nBitsPixel)
581 {
582 if (nBitsPixel < 0)
583 return 0;
584 if (nBitsPixel <= 1)
585 return 1;
586 if (nBitsPixel <= 2)
587 return 2;
588 if (nBitsPixel <= 4)
589 return 4;
590 if (nBitsPixel <= 8)
591 return 8;
592 if (nBitsPixel <= 16)
593 return 16;
594 if (nBitsPixel <= 24)
595 return 24;
596 if (nBitsPixel <= 32)
597 return 32;
598
599 return 0;
600 }
601
602 INT FASTCALL
603 BITMAPOBJ_GetWidthBytes (INT bmWidth, INT bpp)
604 {
605 #if 0
606 switch(bpp)
607 {
608 case 1:
609 return 2 * ((bmWidth+15) >> 4);
610
611 case 24:
612 bmWidth *= 3; /* fall through */
613 case 8:
614 return bmWidth + (bmWidth & 1);
615
616 case 32:
617 return bmWidth * 4;
618
619 case 16:
620 case 15:
621 return bmWidth * 2;
622
623 case 4:
624 return 2 * ((bmWidth+3) >> 2);
625
626 default:
627 DPRINT ("stub");
628 }
629
630 return -1;
631 #endif
632
633 return ((bmWidth * bpp + 15) & ~15) >> 3;
634 }
635
636 HBITMAP FASTCALL
637 BITMAPOBJ_CopyBitmap(HBITMAP hBitmap)
638 {
639 HBITMAP res;
640 BITMAP bm;
641 BITMAPOBJ *Bitmap, *resBitmap;
642 SIZEL Size;
643
644 if (hBitmap == NULL)
645 {
646 return 0;
647 }
648
649 Bitmap = GDIOBJ_LockObj(hBitmap, GDI_OBJECT_TYPE_BITMAP);
650 if (Bitmap == NULL)
651 {
652 return 0;
653 }
654
655 BITMAP_GetObject(Bitmap, sizeof(BITMAP), &bm);
656 bm.bmBits = NULL;
657 if (Bitmap->SurfObj.lDelta >= 0)
658 bm.bmHeight = -bm.bmHeight;
659
660 Size.cx = abs(bm.bmWidth);
661 Size.cy = abs(bm.bmHeight);
662 res = IntCreateBitmap(Size,
663 bm.bmWidthBytes,
664 BitmapFormat(bm.bmBitsPixel * bm.bmPlanes, BI_RGB),
665 (bm.bmHeight < 0 ? BMF_TOPDOWN : 0) | BMF_NOZEROINIT,
666 NULL);
667
668 if(res)
669 {
670 PBYTE buf;
671
672 resBitmap = GDIOBJ_LockObj(res, GDI_OBJECT_TYPE_BITMAP);
673 if (resBitmap)
674 {
675 buf = ExAllocatePoolWithTag (PagedPool, bm.bmWidthBytes * abs(bm.bmHeight), TAG_BITMAP);
676 if (buf == NULL)
677 {
678 GDIOBJ_UnlockObjByPtr((POBJ)resBitmap);
679 GDIOBJ_UnlockObjByPtr((POBJ)Bitmap);
680 NtGdiDeleteObject(res);
681 return 0;
682 }
683 IntGetBitmapBits (Bitmap, bm.bmWidthBytes * abs(bm.bmHeight), buf);
684 IntSetBitmapBits (resBitmap, bm.bmWidthBytes * abs(bm.bmHeight), buf);
685 ExFreePool (buf);
686 resBitmap->flFlags = Bitmap->flFlags;
687 GDIOBJ_UnlockObjByPtr((POBJ)resBitmap);
688 }
689 else
690 {
691 NtGdiDeleteObject(res);
692 res = NULL;
693 }
694 }
695
696 GDIOBJ_UnlockObjByPtr((POBJ)Bitmap);
697
698 return res;
699 }
700
701 INT STDCALL
702 BITMAP_GetObject(BITMAPOBJ * bmp, INT Count, LPVOID buffer)
703 {
704 if ((UINT)Count < sizeof(BITMAP)) return 0;
705
706 if(bmp->dib)
707 {
708 if((UINT)Count < sizeof(DIBSECTION))
709 {
710 Count = sizeof(BITMAP);
711 }
712 else
713 {
714 Count = sizeof(DIBSECTION);
715 }
716 if (buffer)
717 {
718 memcpy(buffer, bmp->dib, Count);
719 }
720 return Count;
721 }
722 else
723 {
724 Count = sizeof(BITMAP);
725 if (buffer)
726 {
727 BITMAP Bitmap;
728
729 Count = sizeof(BITMAP);
730 Bitmap.bmType = 0;
731 Bitmap.bmWidth = bmp->SurfObj.sizlBitmap.cx;
732 Bitmap.bmHeight = bmp->SurfObj.sizlBitmap.cy;
733 Bitmap.bmWidthBytes = abs(bmp->SurfObj.lDelta);
734 Bitmap.bmPlanes = 1;
735 Bitmap.bmBitsPixel = BitsPerFormat(bmp->SurfObj.iBitmapFormat);
736 //Bitmap.bmBits = bmp->SurfObj.pvBits;
737 Bitmap.bmBits = NULL; /* not set accoring wine test confirm in win2k */
738 memcpy(buffer, &Bitmap, Count);
739 }
740 return Count;
741 }
742 }
743
744 /*
745 * @implemented
746 */
747 HDC
748 APIENTRY
749 NtGdiGetDCforBitmap(
750 IN HBITMAP hsurf)
751 {
752 HDC hDC = NULL;
753 PBITMAPOBJ bmp = BITMAPOBJ_LockBitmap( hsurf );
754 hDC = bmp->hDC;
755 BITMAPOBJ_UnlockBitmap( bmp );
756 return hDC;
757 }
758
759
760 /* EOF */