/
dibobj.c
2176 lines (1926 loc) · 62.6 KB
/
dibobj.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROJECT: ReactOS win32 kernel mode subsystem
* LICENSE: GPL - See COPYING in the top level directory
* FILE: win32ss/gdi/ntgdi/dibobj.c
* PURPOSE: Dib object functions
* PROGRAMMER:
*/
#include <win32k.h>
#define NDEBUG
#include <debug.h>
static const RGBQUAD DefLogPaletteQuads[20] = /* Copy of Default Logical Palette */
{
/* rgbBlue, rgbGreen, rgbRed, rgbReserved */
{ 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x80, 0x00 },
{ 0x00, 0x80, 0x00, 0x00 },
{ 0x00, 0x80, 0x80, 0x00 },
{ 0x80, 0x00, 0x00, 0x00 },
{ 0x80, 0x00, 0x80, 0x00 },
{ 0x80, 0x80, 0x00, 0x00 },
{ 0xc0, 0xc0, 0xc0, 0x00 },
{ 0xc0, 0xdc, 0xc0, 0x00 },
{ 0xf0, 0xca, 0xa6, 0x00 },
{ 0xf0, 0xfb, 0xff, 0x00 },
{ 0xa4, 0xa0, 0xa0, 0x00 },
{ 0x80, 0x80, 0x80, 0x00 },
{ 0x00, 0x00, 0xff, 0x00 },
{ 0x00, 0xff, 0x00, 0x00 },
{ 0x00, 0xff, 0xff, 0x00 },
{ 0xff, 0x00, 0x00, 0x00 },
{ 0xff, 0x00, 0xff, 0x00 },
{ 0xff, 0xff, 0x00, 0x00 },
{ 0xff, 0xff, 0xff, 0x00 }
};
PPALETTE
NTAPI
CreateDIBPalette(
_In_ const BITMAPINFO *pbmi,
_In_ PDC pdc,
_In_ ULONG iUsage)
{
PPALETTE ppal;
ULONG i, cBitsPixel, cColors;
if (pbmi->bmiHeader.biSize < sizeof(BITMAPINFOHEADER))
{
PBITMAPCOREINFO pbci = (PBITMAPCOREINFO)pbmi;
cBitsPixel = pbci->bmciHeader.bcBitCount;
}
else
{
cBitsPixel = pbmi->bmiHeader.biBitCount;
}
/* Check if the colors are indexed */
if (cBitsPixel <= 8)
{
/* We create a "full" palette */
cColors = 1 << cBitsPixel;
/* Allocate the palette */
ppal = PALETTE_AllocPalette(PAL_INDEXED,
cColors,
NULL,
0,
0,
0);
/* Check if the BITMAPINFO specifies how many colors to use */
if ((pbmi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER)) &&
(pbmi->bmiHeader.biClrUsed != 0))
{
/* This is how many colors we can actually process */
cColors = min(cColors, pbmi->bmiHeader.biClrUsed);
}
/* Check how to use the colors */
if (iUsage == DIB_PAL_COLORS)
{
COLORREF crColor;
/* The colors are an array of WORD indices into the DC palette */
PWORD pwColors = (PWORD)((PCHAR)pbmi + pbmi->bmiHeader.biSize);
/* Use the DCs palette or, if no DC is given, the default one */
PPALETTE ppalDC = pdc ? pdc->dclevel.ppal : gppalDefault;
/* Loop all color indices in the DIB */
for (i = 0; i < cColors; i++)
{
/* Get the palette index and handle wraparound when exceeding
the number of colors in the DC palette */
WORD wIndex = pwColors[i] % ppalDC->NumColors;
/* USe the RGB value from the DC palette */
crColor = PALETTE_ulGetRGBColorFromIndex(ppalDC, wIndex);
PALETTE_vSetRGBColorForIndex(ppal, i, crColor);
}
}
else if (iUsage == DIB_PAL_BRUSHHACK)
{
/* The colors are an array of WORD indices into the DC palette */
PWORD pwColors = (PWORD)((PCHAR)pbmi + pbmi->bmiHeader.biSize);
/* Loop all color indices in the DIB */
for (i = 0; i < cColors; i++)
{
/* Set the index directly as the RGB color, the real palette
containing RGB values will be calculated when the brush is
realized */
PALETTE_vSetRGBColorForIndex(ppal, i, pwColors[i]);
}
/* Mark the palette as a brush hack palette */
ppal->flFlags |= PAL_BRUSHHACK;
}
// else if (iUsage == 2)
// {
// FIXME: this one is undocumented
// ASSERT(FALSE);
// }
else if (pbmi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER))
{
/* The colors are an array of RGBQUAD values */
RGBQUAD *prgb = (RGBQUAD*)((PCHAR)pbmi + pbmi->bmiHeader.biSize);
// FIXME: do we need to handle PALETTEINDEX / PALETTERGB macro?
/* Loop all color indices in the DIB */
for (i = 0; i < cColors; i++)
{
/* Get the color value and translate it to a COLORREF */
RGBQUAD rgb = prgb[i];
COLORREF crColor = RGB(rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue);
/* Set the RGB value in the palette */
PALETTE_vSetRGBColorForIndex(ppal, i, crColor);
}
}
else
{
/* The colors are an array of RGBTRIPLE values */
RGBTRIPLE *prgb = (RGBTRIPLE*)((PCHAR)pbmi + pbmi->bmiHeader.biSize);
/* Loop all color indices in the DIB */
for (i = 0; i < cColors; i++)
{
/* Get the color value and translate it to a COLORREF */
RGBTRIPLE rgb = prgb[i];
COLORREF crColor = RGB(rgb.rgbtRed, rgb.rgbtGreen, rgb.rgbtBlue);
/* Set the RGB value in the palette */
PALETTE_vSetRGBColorForIndex(ppal, i, crColor);
}
}
}
else
{
/* This is a bitfield / RGB palette */
ULONG flRedMask, flGreenMask, flBlueMask;
/* Check if the DIB contains bitfield values */
if ((pbmi->bmiHeader.biSize >= sizeof(BITMAPINFOHEADER)) &&
(pbmi->bmiHeader.biCompression == BI_BITFIELDS))
{
/* Check if we have a v4/v5 header */
if (pbmi->bmiHeader.biSize >= sizeof(BITMAPV4HEADER))
{
/* The masks are dedicated fields in the header */
PBITMAPV4HEADER pbmV4Header = (PBITMAPV4HEADER)&pbmi->bmiHeader;
flRedMask = pbmV4Header->bV4RedMask;
flGreenMask = pbmV4Header->bV4GreenMask;
flBlueMask = pbmV4Header->bV4BlueMask;
}
else
{
/* The masks are the first 3 values in the DIB color table */
PDWORD pdwColors = (PVOID)((PCHAR)pbmi + pbmi->bmiHeader.biSize);
flRedMask = pdwColors[0];
flGreenMask = pdwColors[1];
flBlueMask = pdwColors[2];
}
}
else
{
/* Check what bit depth we have. Note: optimization flags are
calculated in PALETTE_AllocPalette() */
if (cBitsPixel == 16)
{
/* This is an RGB 555 palette */
flRedMask = 0x7C00;
flGreenMask = 0x03E0;
flBlueMask = 0x001F;
}
else
{
/* This is an RGB 888 palette */
flRedMask = 0xFF0000;
flGreenMask = 0x00FF00;
flBlueMask = 0x0000FF;
}
}
/* Allocate the bitfield palette */
ppal = PALETTE_AllocPalette(PAL_BITFIELDS,
0,
NULL,
flRedMask,
flGreenMask,
flBlueMask);
}
/* We're done, return the palette */
return ppal;
}
// Converts a DIB to a device-dependent bitmap
static INT
FASTCALL
IntSetDIBits(
PDC DC,
HBITMAP hBitmap,
UINT StartScan,
UINT ScanLines,
CONST VOID *Bits,
ULONG cjMaxBits,
CONST BITMAPINFO *bmi,
UINT ColorUse)
{
HBITMAP SourceBitmap;
PSURFACE psurfDst, psurfSrc;
INT result = 0;
RECT rcDst;
POINTL ptSrc;
EXLATEOBJ exlo;
PPALETTE ppalDIB = 0;
ULONG cjSizeImage;
if (!bmi || !Bits) return 0;
/* Check for uncompressed formats */
if ((bmi->bmiHeader.biCompression == BI_RGB) ||
(bmi->bmiHeader.biCompression == BI_BITFIELDS))
{
/* Calculate the image size */
cjSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth,
ScanLines,
bmi->bmiHeader.biBitCount);
}
/* Check if the header provided an image size */
else if (bmi->bmiHeader.biSizeImage != 0)
{
/* Use the given size */
cjSizeImage = bmi->bmiHeader.biSizeImage;
}
else
{
/* Compressed format without a size. This is invalid. */
DPRINT1("Compressed format without a size!");
return 0;
}
/* Check if the size that we have is ok */
if ((cjSizeImage > cjMaxBits) || (cjSizeImage == 0))
{
DPRINT1("Invalid bitmap size! cjSizeImage = %lu, cjMaxBits = %lu\n",
cjSizeImage, cjMaxBits);
return 0;
}
SourceBitmap = GreCreateBitmapEx(bmi->bmiHeader.biWidth,
ScanLines,
0,
BitmapFormat(bmi->bmiHeader.biBitCount,
bmi->bmiHeader.biCompression),
bmi->bmiHeader.biHeight < 0 ? BMF_TOPDOWN : 0,
cjSizeImage,
(PVOID)Bits,
0);
if (!SourceBitmap)
{
DPRINT1("Error: Could not create a bitmap.\n");
EngSetLastError(ERROR_NO_SYSTEM_RESOURCES);
return 0;
}
psurfDst = SURFACE_ShareLockSurface(hBitmap);
psurfSrc = SURFACE_ShareLockSurface(SourceBitmap);
if(!(psurfSrc && psurfDst))
{
DPRINT1("Error: Could not lock surfaces\n");
goto cleanup;
}
/* Create a palette for the DIB */
ppalDIB = CreateDIBPalette(bmi, DC, ColorUse);
if (!ppalDIB)
{
EngSetLastError(ERROR_NO_SYSTEM_RESOURCES);
goto cleanup;
}
/* Initialize EXLATEOBJ */
EXLATEOBJ_vInitialize(&exlo,
ppalDIB,
psurfDst->ppal,
RGB(0xff, 0xff, 0xff),
RGB(0xff, 0xff, 0xff), //DC->pdcattr->crBackgroundClr,
0); // DC->pdcattr->crForegroundClr);
rcDst.top = StartScan;
rcDst.left = 0;
rcDst.bottom = rcDst.top + ScanLines;
rcDst.right = psurfDst->SurfObj.sizlBitmap.cx;
ptSrc.x = 0;
ptSrc.y = 0;
result = IntEngCopyBits(&psurfDst->SurfObj,
&psurfSrc->SurfObj,
NULL,
&exlo.xlo,
&rcDst,
&ptSrc);
if(result)
result = ScanLines;
EXLATEOBJ_vCleanup(&exlo);
cleanup:
if (ppalDIB) PALETTE_ShareUnlockPalette(ppalDIB);
if(psurfSrc) SURFACE_ShareUnlockSurface(psurfSrc);
if(psurfDst) SURFACE_ShareUnlockSurface(psurfDst);
GreDeleteObject(SourceBitmap);
return result;
}
static
HBITMAP
IntGdiCreateMaskFromRLE(
DWORD Width,
DWORD Height,
ULONG Compression,
const BYTE* Bits,
DWORD BitsSize)
{
HBITMAP Mask;
DWORD x, y;
SURFOBJ* SurfObj;
UINT i = 0;
BYTE Data, NumPixels, ToSkip;
ASSERT((Compression == BI_RLE8) || (Compression == BI_RLE4));
/* Create the bitmap */
Mask = GreCreateBitmapEx(Width, Height, 0, BMF_1BPP, 0, 0, NULL, 0);
if (!Mask)
return NULL;
SurfObj = EngLockSurface((HSURF)Mask);
if (!SurfObj)
{
GreDeleteObject(Mask);
return NULL;
}
ASSERT(SurfObj->pvBits != NULL);
x = y = 0;
while (i < BitsSize)
{
NumPixels = Bits[i];
Data = Bits[i + 1];
i += 2;
if (NumPixels != 0)
{
if ((x + NumPixels) > Width)
NumPixels = Width - x;
if (NumPixels == 0)
continue;
DIB_1BPP_HLine(SurfObj, x, x + NumPixels, y, 1);
x += NumPixels;
continue;
}
if (Data < 3)
{
switch (Data)
{
case 0:
/* End of line */
y++;
if (y == Height)
goto done;
x = 0;
break;
case 1:
/* End of file */
goto done;
case 2:
/* Jump */
if (i >= (BitsSize - 1))
goto done;
x += Bits[i];
if (x > Width)
x = Width;
y += Bits[i + 1];
if (y >= Height)
goto done;
i += 2;
break;
}
/* Done for this run */
continue;
}
/* Embedded data into the RLE */
NumPixels = Data;
if (Compression == BI_RLE8)
ToSkip = NumPixels;
else
ToSkip = (NumPixels / 2) + (NumPixels & 1);
if ((i + ToSkip) > BitsSize)
goto done;
ToSkip = (ToSkip + 1) & ~1;
if ((x + NumPixels) > Width)
NumPixels = Width - x;
if (NumPixels != 0)
{
DIB_1BPP_HLine(SurfObj, x, x + NumPixels, y, 1);
x += NumPixels;
}
i += ToSkip;
}
done:
EngUnlockSurface(SurfObj);
return Mask;
}
W32KAPI
INT
APIENTRY
NtGdiSetDIBitsToDeviceInternal(
IN HDC hDC,
IN INT XDest,
IN INT YDest,
IN DWORD Width,
IN DWORD Height,
IN INT XSrc,
IN INT YSrc,
IN DWORD StartScan,
IN DWORD ScanLines,
IN LPBYTE Bits,
IN LPBITMAPINFO bmi,
IN DWORD ColorUse,
IN UINT cjMaxBits,
IN UINT cjMaxInfo,
IN BOOL bTransformCoordinates,
IN OPTIONAL HANDLE hcmXform)
{
INT ret;
PDC pDC = NULL;
HBITMAP hSourceBitmap = NULL, hMaskBitmap = NULL;
SURFOBJ *pDestSurf, *pSourceSurf = NULL, *pMaskSurf = NULL;
SURFACE *pSurf;
RECTL rcDest;
POINTL ptSource;
//INT DIBWidth;
SIZEL SourceSize;
EXLATEOBJ exlo;
PPALETTE ppalDIB = NULL;
LPBITMAPINFO pbmiSafe;
BOOL bResult;
if (!Bits) return 0;
pbmiSafe = ExAllocatePoolWithTag(PagedPool, cjMaxInfo, 'pmTG');
if (!pbmiSafe) return 0;
_SEH2_TRY
{
ProbeForRead(bmi, cjMaxInfo, 1);
ProbeForRead(Bits, cjMaxBits, 1);
RtlCopyMemory(pbmiSafe, bmi, cjMaxInfo);
bmi = pbmiSafe;
}
_SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
{
ret = 0;
goto Exit;
}
_SEH2_END
ScanLines = min(ScanLines, abs(bmi->bmiHeader.biHeight) - StartScan);
if (ScanLines == 0)
{
DPRINT1("ScanLines == 0\n");
ret = 0;
goto Exit;
}
pDC = DC_LockDc(hDC);
if (!pDC)
{
EngSetLastError(ERROR_INVALID_HANDLE);
ret = 0;
goto Exit;
}
if (pDC->dctype == DC_TYPE_INFO)
{
ret = 0;
goto Exit;
}
rcDest.left = XDest;
rcDest.top = YDest;
if (bTransformCoordinates)
{
IntLPtoDP(pDC, (LPPOINT)&rcDest, 2);
}
rcDest.left += pDC->ptlDCOrig.x;
rcDest.top += pDC->ptlDCOrig.y;
rcDest.right = rcDest.left + Width;
rcDest.bottom = rcDest.top + Height;
rcDest.top += StartScan;
if (pDC->fs & (DC_ACCUM_APP|DC_ACCUM_WMGR))
{
IntUpdateBoundsRect(pDC, &rcDest);
}
ptSource.x = XSrc;
ptSource.y = YSrc;
SourceSize.cx = bmi->bmiHeader.biWidth;
SourceSize.cy = ScanLines;
//DIBWidth = WIDTH_BYTES_ALIGN32(SourceSize.cx, bmi->bmiHeader.biBitCount);
hSourceBitmap = GreCreateBitmapEx(bmi->bmiHeader.biWidth,
ScanLines,
0,
BitmapFormat(bmi->bmiHeader.biBitCount,
bmi->bmiHeader.biCompression),
bmi->bmiHeader.biHeight < 0 ? BMF_TOPDOWN : 0,
bmi->bmiHeader.biSizeImage,
Bits,
0);
if (!hSourceBitmap)
{
EngSetLastError(ERROR_NO_SYSTEM_RESOURCES);
ret = 0;
goto Exit;
}
pSourceSurf = EngLockSurface((HSURF)hSourceBitmap);
if (!pSourceSurf)
{
ret = 0;
goto Exit;
}
/* HACK: If this is a RLE bitmap, only the relevant pixels must be set. */
if ((bmi->bmiHeader.biCompression == BI_RLE8) || (bmi->bmiHeader.biCompression == BI_RLE4))
{
hMaskBitmap = IntGdiCreateMaskFromRLE(bmi->bmiHeader.biWidth,
ScanLines,
bmi->bmiHeader.biCompression,
Bits,
cjMaxBits);
if (!hMaskBitmap)
{
EngSetLastError(ERROR_NO_SYSTEM_RESOURCES);
ret = 0;
goto Exit;
}
pMaskSurf = EngLockSurface((HSURF)hMaskBitmap);
if (!pMaskSurf)
{
ret = 0;
goto Exit;
}
}
/* Create a palette for the DIB */
ppalDIB = CreateDIBPalette(bmi, pDC, ColorUse);
if (!ppalDIB)
{
EngSetLastError(ERROR_NO_SYSTEM_RESOURCES);
ret = 0;
goto Exit;
}
/* This is actually a blit */
DC_vPrepareDCsForBlit(pDC, &rcDest, NULL, NULL);
pSurf = pDC->dclevel.pSurface;
if (!pSurf)
{
DC_vFinishBlit(pDC, NULL);
ret = ScanLines;
goto Exit;
}
ASSERT(pSurf->ppal);
/* Initialize EXLATEOBJ */
EXLATEOBJ_vInitialize(&exlo,
ppalDIB,
pSurf->ppal,
RGB(0xff, 0xff, 0xff),
pDC->pdcattr->crBackgroundClr,
pDC->pdcattr->crForegroundClr);
pDestSurf = &pSurf->SurfObj;
/* Copy the bits */
DPRINT("BitsToDev with dstsurf=(%d|%d) (%d|%d), src=(%d|%d) w=%d h=%d\n",
rcDest.left, rcDest.top, rcDest.right, rcDest.bottom,
ptSource.x, ptSource.y, SourceSize.cx, SourceSize.cy);
bResult = IntEngBitBlt(pDestSurf,
pSourceSurf,
pMaskSurf,
(CLIPOBJ *)&pDC->co,
&exlo.xlo,
&rcDest,
&ptSource,
pMaskSurf ? &ptSource : NULL,
NULL,
NULL,
pMaskSurf ? ROP4_MASK : ROP4_FROM_INDEX(R3_OPINDEX_SRCCOPY));
/* Cleanup EXLATEOBJ */
EXLATEOBJ_vCleanup(&exlo);
/* We're done */
DC_vFinishBlit(pDC, NULL);
ret = bResult ? ScanLines : 0;
Exit:
if (ppalDIB) PALETTE_ShareUnlockPalette(ppalDIB);
if (pSourceSurf) EngUnlockSurface(pSourceSurf);
if (hSourceBitmap) EngDeleteSurface((HSURF)hSourceBitmap);
if (pMaskSurf) EngUnlockSurface(pMaskSurf);
if (hMaskBitmap) EngDeleteSurface((HSURF)hMaskBitmap);
if (pDC) DC_UnlockDc(pDC);
ExFreePoolWithTag(pbmiSafe, 'pmTG');
return ret;
}
/* Converts a device-dependent bitmap to a DIB */
INT
APIENTRY
GreGetDIBitsInternal(
HDC hDC,
HBITMAP hBitmap,
UINT StartScan,
UINT ScanLines,
LPBYTE Bits,
LPBITMAPINFO Info,
UINT Usage,
UINT MaxBits,
UINT MaxInfo)
{
BITMAPCOREINFO* pbmci = NULL;
PSURFACE psurf = NULL;
PDC pDC;
LONG width, height;
WORD planes, bpp;
DWORD compr, size ;
USHORT i;
int bitmap_type;
RGBQUAD* rgbQuads;
VOID* colorPtr;
DPRINT("Entered GreGetDIBitsInternal()\n");
if ((Usage && Usage != DIB_PAL_COLORS) || !Info || !hBitmap)
return 0;
pDC = DC_LockDc(hDC);
if (pDC == NULL || pDC->dctype == DC_TYPE_INFO)
{
ScanLines = 0;
goto done;
}
/* Get a pointer to the source bitmap object */
psurf = SURFACE_ShareLockSurface(hBitmap);
if (psurf == NULL)
{
ScanLines = 0;
goto done;
}
colorPtr = (LPBYTE)Info + Info->bmiHeader.biSize;
rgbQuads = colorPtr;
bitmap_type = DIB_GetBitmapInfo(&Info->bmiHeader,
&width,
&height,
&planes,
&bpp,
&compr,
&size);
if(bitmap_type == -1)
{
DPRINT("Wrong bitmap format\n");
EngSetLastError(ERROR_INVALID_PARAMETER);
ScanLines = 0;
goto done;
}
else if(bitmap_type == 0)
{
/* We need a BITMAPINFO to create a DIB, but we have to fill
* the BITMAPCOREINFO we're provided */
pbmci = (BITMAPCOREINFO*)Info;
/* fill in the the bit count, so we can calculate the right ColorsSize during the conversion */
pbmci->bmciHeader.bcBitCount = BitsPerFormat(psurf->SurfObj.iBitmapFormat);
Info = DIB_ConvertBitmapInfo((BITMAPINFO*)pbmci, Usage);
if(Info == NULL)
{
DPRINT1("Error, could not convert the BITMAPCOREINFO!\n");
ScanLines = 0;
goto done;
}
rgbQuads = Info->bmiColors;
}
/* Validate input:
- negative width is always an invalid value
- non-null Bits and zero bpp is an invalid combination
- only check the rest of the input params if either bpp is non-zero or Bits are set */
if (width < 0 || (bpp == 0 && Bits))
{
ScanLines = 0;
goto done;
}
if (Bits || bpp)
{
if ((height == 0 || width == 0) || (compr && compr != BI_BITFIELDS && compr != BI_RGB))
{
ScanLines = 0;
goto done;
}
}
Info->bmiHeader.biClrUsed = 0;
Info->bmiHeader.biClrImportant = 0;
/* Fill in the structure */
switch(bpp)
{
case 0: /* Only info */
Info->bmiHeader.biWidth = psurf->SurfObj.sizlBitmap.cx;
Info->bmiHeader.biHeight = (psurf->SurfObj.fjBitmap & BMF_TOPDOWN) ?
-psurf->SurfObj.sizlBitmap.cy :
psurf->SurfObj.sizlBitmap.cy;
Info->bmiHeader.biPlanes = 1;
Info->bmiHeader.biBitCount = BitsPerFormat(psurf->SurfObj.iBitmapFormat);
Info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes( Info->bmiHeader.biWidth,
Info->bmiHeader.biHeight,
Info->bmiHeader.biBitCount);
Info->bmiHeader.biCompression = (Info->bmiHeader.biBitCount == 16 || Info->bmiHeader.biBitCount == 32) ?
BI_BITFIELDS : BI_RGB;
Info->bmiHeader.biXPelsPerMeter = 0;
Info->bmiHeader.biYPelsPerMeter = 0;
if (Info->bmiHeader.biBitCount <= 8 && Info->bmiHeader.biClrUsed == 0)
Info->bmiHeader.biClrUsed = 1 << Info->bmiHeader.biBitCount;
ScanLines = 1;
goto done;
case 1:
case 4:
case 8:
Info->bmiHeader.biClrUsed = 1 << bpp;
/* If the bitmap is a DIB section and has the same format as what
* is requested, go ahead! */
if((psurf->hSecure) &&
(BitsPerFormat(psurf->SurfObj.iBitmapFormat) == bpp))
{
if(Usage == DIB_RGB_COLORS)
{
ULONG colors = min(psurf->ppal->NumColors, 256);
if(colors != 256) Info->bmiHeader.biClrUsed = colors;
for(i = 0; i < colors; i++)
{
rgbQuads[i].rgbRed = psurf->ppal->IndexedColors[i].peRed;
rgbQuads[i].rgbGreen = psurf->ppal->IndexedColors[i].peGreen;
rgbQuads[i].rgbBlue = psurf->ppal->IndexedColors[i].peBlue;
rgbQuads[i].rgbReserved = 0;
}
}
else
{
for(i = 0; i < 256; i++)
((WORD*)rgbQuads)[i] = i;
}
}
else
{
if(Usage == DIB_PAL_COLORS)
{
for(i = 0; i < 256; i++)
{
((WORD*)rgbQuads)[i] = i;
}
}
else if(bpp > 1 && bpp == BitsPerFormat(psurf->SurfObj.iBitmapFormat))
{
/* For color DDBs in native depth (mono DDBs always have
a black/white palette):
Generate the color map from the selected palette */
PPALETTE pDcPal = PALETTE_ShareLockPalette(pDC->dclevel.hpal);
if(!pDcPal)
{
ScanLines = 0 ;
goto done ;
}
for (i = 0; i < pDcPal->NumColors; i++)
{
rgbQuads[i].rgbRed = pDcPal->IndexedColors[i].peRed;
rgbQuads[i].rgbGreen = pDcPal->IndexedColors[i].peGreen;
rgbQuads[i].rgbBlue = pDcPal->IndexedColors[i].peBlue;
rgbQuads[i].rgbReserved = 0;
}
PALETTE_ShareUnlockPalette(pDcPal);
}
else
{
switch (bpp)
{
case 1:
rgbQuads[0].rgbRed = rgbQuads[0].rgbGreen = rgbQuads[0].rgbBlue = 0;
rgbQuads[0].rgbReserved = 0;
rgbQuads[1].rgbRed = rgbQuads[1].rgbGreen = rgbQuads[1].rgbBlue = 0xff;
rgbQuads[1].rgbReserved = 0;
break;
case 4:
/* The EGA palette is the first and last 8 colours of the default palette
with the innermost pair swapped */
RtlCopyMemory(rgbQuads, DefLogPaletteQuads, 7 * sizeof(RGBQUAD));
RtlCopyMemory(rgbQuads + 7, DefLogPaletteQuads + 12, 1 * sizeof(RGBQUAD));
RtlCopyMemory(rgbQuads + 8, DefLogPaletteQuads + 7, 1 * sizeof(RGBQUAD));
RtlCopyMemory(rgbQuads + 9, DefLogPaletteQuads + 13, 7 * sizeof(RGBQUAD));
break;
case 8:
{
INT i;
memcpy(rgbQuads, DefLogPaletteQuads, 10 * sizeof(RGBQUAD));
memcpy(rgbQuads + 246, DefLogPaletteQuads + 10, 10 * sizeof(RGBQUAD));
for (i = 10; i < 246; i++)
{
rgbQuads[i].rgbRed = (i & 0x07) << 5;
rgbQuads[i].rgbGreen = (i & 0x38) << 2;
rgbQuads[i].rgbBlue = i & 0xc0;
rgbQuads[i].rgbReserved = 0;
}
}
}
}
}
break;
case 15:
if (Info->bmiHeader.biCompression == BI_BITFIELDS)
{
((PDWORD)Info->bmiColors)[0] = 0x7c00;
((PDWORD)Info->bmiColors)[1] = 0x03e0;
((PDWORD)Info->bmiColors)[2] = 0x001f;
}
break;
case 16:
if (Info->bmiHeader.biCompression == BI_BITFIELDS)
{
if (psurf->hSecure)
{
((PDWORD)Info->bmiColors)[0] = psurf->ppal->RedMask;
((PDWORD)Info->bmiColors)[1] = psurf->ppal->GreenMask;
((PDWORD)Info->bmiColors)[2] = psurf->ppal->BlueMask;
}
else
{
((PDWORD)Info->bmiColors)[0] = 0xf800;
((PDWORD)Info->bmiColors)[1] = 0x07e0;
((PDWORD)Info->bmiColors)[2] = 0x001f;
}
}
break;
case 24:
case 32:
if (Info->bmiHeader.biCompression == BI_BITFIELDS)
{
if (psurf->hSecure)
{
((PDWORD)Info->bmiColors)[0] = psurf->ppal->RedMask;
((PDWORD)Info->bmiColors)[1] = psurf->ppal->GreenMask;
((PDWORD)Info->bmiColors)[2] = psurf->ppal->BlueMask;
}
else
{
((PDWORD)Info->bmiColors)[0] = 0xff0000;
((PDWORD)Info->bmiColors)[1] = 0x00ff00;
((PDWORD)Info->bmiColors)[2] = 0x0000ff;
}
}
break;
default:
ScanLines = 0;
goto done;
}
Info->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(width, height, bpp);
Info->bmiHeader.biPlanes = 1;
if(Bits && ScanLines)
{
/* Create a DIBSECTION, blt it, profit */
PVOID pDIBits ;
HBITMAP hBmpDest;
PSURFACE psurfDest;
EXLATEOBJ exlo;
RECT rcDest;
POINTL srcPoint;
BOOL ret ;
int newLines = -1;
if (StartScan >= abs(Info->bmiHeader.biHeight))
{
ScanLines = 1;
goto done;
}
else
{
ScanLines = min(ScanLines, abs(Info->bmiHeader.biHeight) - StartScan);
}
if (abs(Info->bmiHeader.biHeight) < psurf->SurfObj.sizlBitmap.cy)
{
StartScan += psurf->SurfObj.sizlBitmap.cy - abs(Info->bmiHeader.biHeight);
}
/* Fixup values */
Info->bmiHeader.biHeight = (height < 0) ?
-(LONG)ScanLines : ScanLines;
/* Create the DIB */
hBmpDest = DIB_CreateDIBSection(pDC, Info, Usage, &pDIBits, NULL, 0, 0);
/* Restore them */
Info->bmiHeader.biHeight = height;
if(!hBmpDest)
{
DPRINT1("Unable to create a DIB Section!\n");
EngSetLastError(ERROR_INVALID_PARAMETER);
ScanLines = 0;
goto done ;
}
psurfDest = SURFACE_ShareLockSurface(hBmpDest);
RECTL_vSetRect(&rcDest, 0, 0, Info->bmiHeader.biWidth, ScanLines);
Info->bmiHeader.biWidth = width;
srcPoint.x = 0;
if (abs(Info->bmiHeader.biHeight) <= psurf->SurfObj.sizlBitmap.cy)
{
srcPoint.y = psurf->SurfObj.sizlBitmap.cy - StartScan - ScanLines;
}
else
{
/* Determine the actual number of lines copied from the */
/* original bitmap. It might be different from ScanLines. */
newLines = abs(Info->bmiHeader.biHeight) - psurf->SurfObj.sizlBitmap.cy;