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