1 | /* $Id: dib.cpp,v 1.4 2000-01-18 20:10:35 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 DIB functions for OS/2
|
---|
5 | *
|
---|
6 | * Copyright 1999 Sander van Leeuwen (OS/2 Port)
|
---|
7 | *
|
---|
8 | * Based on Wine code (objects\dib.c):
|
---|
9 | *
|
---|
10 | * GDI device-independent bitmaps
|
---|
11 | *
|
---|
12 | * Copyright 1993,1994 Alexandre Julliard
|
---|
13 | *
|
---|
14 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
15 | *
|
---|
16 | *
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include <os2win.h>
|
---|
20 | #include <stdlib.h>
|
---|
21 | #include <misc.h>
|
---|
22 |
|
---|
23 | /***********************************************************************
|
---|
24 | * DIB_GetDIBWidthBytes
|
---|
25 | *
|
---|
26 | * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
|
---|
27 | * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
|
---|
28 | */
|
---|
29 | int DIB_GetDIBWidthBytes( int width, int depth )
|
---|
30 | {
|
---|
31 | int words;
|
---|
32 |
|
---|
33 | switch(depth)
|
---|
34 | {
|
---|
35 | case 1: words = (width + 31) / 32; break;
|
---|
36 | case 4: words = (width + 7) / 8; break;
|
---|
37 | case 8: words = (width + 3) / 4; break;
|
---|
38 | case 15:
|
---|
39 | case 16: words = (width + 1) / 2; break;
|
---|
40 | case 24: words = (width * 3 + 3)/4; break;
|
---|
41 |
|
---|
42 | default:
|
---|
43 | dprintf(("(%d): Unsupported depth\n", depth ));
|
---|
44 | /* fall through */
|
---|
45 | case 32:
|
---|
46 | words = width;
|
---|
47 | }
|
---|
48 | return 4 * words;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /***********************************************************************
|
---|
52 | * DIB_GetDIBImageBytes
|
---|
53 | *
|
---|
54 | * Return the number of bytes used to hold the image in a DIB bitmap.
|
---|
55 | */
|
---|
56 | int DIB_GetDIBImageBytes( int width, int height, int depth )
|
---|
57 | {
|
---|
58 | return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
|
---|
59 | }
|
---|
60 |
|
---|
61 |
|
---|
62 | /***********************************************************************
|
---|
63 | * DIB_BitmapInfoSize
|
---|
64 | *
|
---|
65 | * Return the size of the bitmap info structure including color table.
|
---|
66 | */
|
---|
67 | int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
|
---|
68 | {
|
---|
69 | int colors;
|
---|
70 |
|
---|
71 | if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
|
---|
72 | {
|
---|
73 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
|
---|
74 | colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
|
---|
75 | return sizeof(BITMAPCOREHEADER) + colors *
|
---|
76 | ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
|
---|
77 | }
|
---|
78 | else /* assume BITMAPINFOHEADER */
|
---|
79 | {
|
---|
80 | colors = info->bmiHeader.biClrUsed;
|
---|
81 | if (!colors && (info->bmiHeader.biBitCount <= 8))
|
---|
82 | colors = 1 << info->bmiHeader.biBitCount;
|
---|
83 | return sizeof(BITMAPINFOHEADER) + colors *
|
---|
84 | ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
|
---|
85 | }
|
---|
86 | }
|
---|
87 |
|
---|
88 |
|
---|
89 | /***********************************************************************
|
---|
90 | * DIB_GetBitmapInfo
|
---|
91 | *
|
---|
92 | * Get the info from a bitmap header.
|
---|
93 | * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
|
---|
94 | */
|
---|
95 | int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
|
---|
96 | int *height, WORD *bpp, WORD *compr )
|
---|
97 | {
|
---|
98 | if (header->biSize == sizeof(BITMAPINFOHEADER))
|
---|
99 | {
|
---|
100 | *width = header->biWidth;
|
---|
101 | *height = header->biHeight;
|
---|
102 | *bpp = header->biBitCount;
|
---|
103 | *compr = header->biCompression;
|
---|
104 | return 1;
|
---|
105 | }
|
---|
106 | if (header->biSize == sizeof(BITMAPCOREHEADER))
|
---|
107 | {
|
---|
108 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
|
---|
109 | *width = core->bcWidth;
|
---|
110 | *height = core->bcHeight;
|
---|
111 | *bpp = core->bcBitCount;
|
---|
112 | *compr = 0;
|
---|
113 | return 0;
|
---|
114 | }
|
---|
115 | dprintf(("(%ld): wrong size for header\n", header->biSize ));
|
---|
116 | return -1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /***********************************************************************
|
---|
120 | * DIB_FixColorsToLoadflags
|
---|
121 | *
|
---|
122 | * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
|
---|
123 | * are in loadflags
|
---|
124 | */
|
---|
125 | void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
|
---|
126 | {
|
---|
127 | int colors, bitcount;
|
---|
128 | COLORREF c_W, c_S, c_F, c_L, c_C;
|
---|
129 | int incr,i;
|
---|
130 | RGBQUAD *ptr;
|
---|
131 | char *colorptr;
|
---|
132 |
|
---|
133 | //SvL: Wine code doesn't work for OS/2 1.3 bitmaps
|
---|
134 | if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
|
---|
135 | {
|
---|
136 | if (bmi->bmiHeader.biBitCount > 8) return;
|
---|
137 | colors = bmi->bmiHeader.biClrUsed;
|
---|
138 | bitcount = bmi->bmiHeader.biBitCount;
|
---|
139 | colorptr = (char*)bmi->bmiColors;
|
---|
140 | incr = 4;
|
---|
141 | }
|
---|
142 | else
|
---|
143 | if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
|
---|
144 | {
|
---|
145 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)bmi;
|
---|
146 |
|
---|
147 | if (core->bcBitCount > 8) return;
|
---|
148 | colors = (1 << core->bcBitCount);
|
---|
149 | bitcount = core->bcBitCount;
|
---|
150 | colorptr = (char*)(core + 1);
|
---|
151 | incr = 3;
|
---|
152 | }
|
---|
153 | else
|
---|
154 | {
|
---|
155 | dprintf(("Wrong bitmap header size!\n"));
|
---|
156 | return;
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (!colors && (bitcount <= 8))
|
---|
160 | colors = 1 << bitcount;
|
---|
161 |
|
---|
162 | c_W = GetSysColor(COLOR_WINDOW);
|
---|
163 | c_S = GetSysColor(COLOR_3DSHADOW);
|
---|
164 | c_F = GetSysColor(COLOR_3DFACE);
|
---|
165 | c_L = GetSysColor(COLOR_3DLIGHT);
|
---|
166 | if (loadflags & LR_LOADTRANSPARENT) {
|
---|
167 | switch (bitcount) {
|
---|
168 | case 1: pix = pix >> 7; break;
|
---|
169 | case 4: pix = pix >> 4; break;
|
---|
170 | case 8: break;
|
---|
171 | default:
|
---|
172 | dprintf(("(%d): Unsupported depth\n", bitcount));
|
---|
173 | return;
|
---|
174 | }
|
---|
175 | if (pix >= colors) {
|
---|
176 | dprintf(("pixel has color index greater than biClrUsed!\n"));
|
---|
177 | return;
|
---|
178 | }
|
---|
179 | if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
|
---|
180 | ptr = (RGBQUAD*)((char*)colorptr+pix*incr);
|
---|
181 |
|
---|
182 | ptr->rgbBlue = GetBValue(c_W);
|
---|
183 | ptr->rgbGreen = GetGValue(c_W);
|
---|
184 | ptr->rgbRed = GetRValue(c_W);
|
---|
185 | }
|
---|
186 | if (loadflags & LR_LOADMAP3DCOLORS) {
|
---|
187 | for (i=0; i<colors; i++) {
|
---|
188 | ptr = (RGBQUAD*)(colorptr +i*incr);
|
---|
189 | c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
|
---|
190 | if (c_C == RGB(128, 128, 128)) {
|
---|
191 | ptr->rgbRed = GetRValue(c_S);
|
---|
192 | ptr->rgbGreen = GetGValue(c_S);
|
---|
193 | ptr->rgbBlue = GetBValue(c_S);
|
---|
194 | } else if (c_C == RGB(192, 192, 192)) {
|
---|
195 | ptr->rgbRed = GetRValue(c_F);
|
---|
196 | ptr->rgbGreen = GetGValue(c_F);
|
---|
197 | ptr->rgbBlue = GetBValue(c_F);
|
---|
198 | } else if (c_C == RGB(223, 223, 223)) {
|
---|
199 | ptr->rgbRed = GetRValue(c_L);
|
---|
200 | ptr->rgbGreen = GetGValue(c_L);
|
---|
201 | ptr->rgbBlue = GetBValue(c_L);
|
---|
202 | }
|
---|
203 | }
|
---|
204 | }
|
---|
205 |
|
---|
206 | }
|
---|