1 | /* $Id: icon.cpp,v 1.11 2001-08-08 10:07:18 sandervl Exp $ */
|
---|
2 |
|
---|
3 | /*
|
---|
4 | * Win32 icon conversion functions for OS/2
|
---|
5 | *
|
---|
6 | * Copyright 1998 Sander van Leeuwen
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 | #define INCL_GPIBITMAPS
|
---|
13 | #define INCL_BITMAPFILEFORMAT
|
---|
14 | #define INCL_DOSFILEMGR /* File Manager values */
|
---|
15 | #define INCL_DOSERRORS /* DOS Error values */
|
---|
16 | #define INCL_DOSPROCESS /* DOS Process values */
|
---|
17 | #define INCL_DOSMISC /* DOS Miscellanous values */
|
---|
18 | #define INCL_WIN
|
---|
19 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <string.h>
|
---|
22 | #include <stdlib.h>
|
---|
23 | #include <iostream.h>
|
---|
24 | #include <string.h>
|
---|
25 |
|
---|
26 | #include <win32api.h>
|
---|
27 | #include <win32type.h>
|
---|
28 | #include "dib.h"
|
---|
29 | #include <winicon.h>
|
---|
30 | #include <misc.h>
|
---|
31 |
|
---|
32 | #define DBG_LOCALLOG DBG_icon
|
---|
33 | #include "dbglocal.h"
|
---|
34 |
|
---|
35 | #define DIB_RGB_COLORS_W 0
|
---|
36 | #define DIB_PAL_COLORS_W 1
|
---|
37 | #define CBM_INIT_W 4
|
---|
38 |
|
---|
39 | #if 0
|
---|
40 | //******************************************************************************
|
---|
41 | //******************************************************************************
|
---|
42 | PBYTE ConvertWin32Icon(PBYTE presbits, DWORD dwResSize, DWORD *OS2ResSize)
|
---|
43 | {
|
---|
44 | WINBITMAPINFOHEADER *bmpHdr = (WINBITMAPINFOHEADER *)presbits;
|
---|
45 | BITMAPFILEHEADER *iconhdr;
|
---|
46 | BITMAPFILEHEADER *iconhdr2;
|
---|
47 | RGBQUAD *rgb;
|
---|
48 | RGB *os2rgb;
|
---|
49 | int bwsize, i, colorsize, rgbsize, iconsize;
|
---|
50 |
|
---|
51 | bwsize = (bmpHdr->biWidth*(bmpHdr->biHeight/2))/8;
|
---|
52 | colorsize = bmpHdr->biWidth*(bmpHdr->biHeight/2);
|
---|
53 | if(bmpHdr->biBitCount < 24)
|
---|
54 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB);
|
---|
55 | else rgbsize = 0;
|
---|
56 |
|
---|
57 | switch(bmpHdr->biBitCount) {
|
---|
58 | case 1:
|
---|
59 | colorsize /= 8;
|
---|
60 | break;
|
---|
61 | case 4:
|
---|
62 | colorsize /= 2;
|
---|
63 | break;
|
---|
64 | case 8:
|
---|
65 | break;
|
---|
66 | case 16:
|
---|
67 | colorsize *= 2;
|
---|
68 | break;
|
---|
69 | case 24:
|
---|
70 | colorsize *= 3;
|
---|
71 | break;
|
---|
72 | case 32:
|
---|
73 | colorsize *= 4;
|
---|
74 | break;
|
---|
75 | }
|
---|
76 | if(bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) {
|
---|
77 | bmpHdr->biSizeImage = bwsize + colorsize;
|
---|
78 | }
|
---|
79 | #if 0
|
---|
80 | cout << "Icon size : " << bmpHdr->biSizeImage << endl;
|
---|
81 | cout << "Icon Width : " << bmpHdr->biWidth << endl;
|
---|
82 | //height for both the XOR and AND bitmap (color & BW)
|
---|
83 | cout << "Height : " << bmpHdr->biHeight << endl;
|
---|
84 | cout << "Icon Bitcount: " << bmpHdr->biBitCount << endl;
|
---|
85 | cout << "Icon Compress: " << bmpHdr->biCompression << endl;
|
---|
86 | #endif
|
---|
87 |
|
---|
88 | //bitmapfileheader for AndXor mask + 2 RGB structs + bitmapfileheader
|
---|
89 | //for color bitmap + RGB structs for all the colors
|
---|
90 | //SvL, 15-3-98: 2*bwsize
|
---|
91 | iconsize = 2*sizeof(BITMAPFILEHEADER) + 2*sizeof(RGB) +
|
---|
92 | rgbsize + 2*bwsize + bmpHdr->biSizeImage;
|
---|
93 | //There are icons without an XOR mask, so check for it
|
---|
94 | if(bmpHdr->biSizeImage == colorsize) {
|
---|
95 | iconsize += bwsize;
|
---|
96 | }
|
---|
97 | iconhdr = (BITMAPFILEHEADER *)malloc(iconsize);
|
---|
98 | iconhdr->usType = BFT_COLORICON;
|
---|
99 | iconhdr->cbSize = sizeof(BITMAPFILEHEADER);
|
---|
100 | iconhdr->xHotspot = 0;
|
---|
101 | iconhdr->yHotspot = 0;
|
---|
102 | iconhdr->offBits = 2*sizeof(BITMAPFILEHEADER) +
|
---|
103 | 2*sizeof(RGB) + rgbsize;
|
---|
104 | iconhdr->bmp.cbFix = sizeof(BITMAPINFOHEADER);
|
---|
105 | iconhdr->bmp.cx = (USHORT)bmpHdr->biWidth;
|
---|
106 | iconhdr->bmp.cy = (USHORT)bmpHdr->biHeight;
|
---|
107 | iconhdr->bmp.cPlanes = 1;
|
---|
108 | iconhdr->bmp.cBitCount = 1;
|
---|
109 | os2rgb = (RGB *)(iconhdr+1);
|
---|
110 | memset(os2rgb, 0, sizeof(RGB));
|
---|
111 | memset(os2rgb+1, 0xff, sizeof(RGB));
|
---|
112 | iconhdr2 = (BITMAPFILEHEADER *)(os2rgb+2);
|
---|
113 | iconhdr2->usType = BFT_COLORICON;
|
---|
114 | iconhdr2->cbSize = sizeof(BITMAPFILEHEADER);
|
---|
115 | iconhdr2->xHotspot = 0;
|
---|
116 | iconhdr2->yHotspot = 0;
|
---|
117 | iconhdr2->offBits = 2*sizeof(BITMAPFILEHEADER) +
|
---|
118 | 2*sizeof(RGB) + rgbsize + 2*bwsize;
|
---|
119 | iconhdr2->bmp.cbFix = sizeof(BITMAPINFOHEADER);
|
---|
120 | iconhdr2->bmp.cx = (USHORT)bmpHdr->biWidth;
|
---|
121 | iconhdr2->bmp.cy = (USHORT)(bmpHdr->biHeight/2);
|
---|
122 | iconhdr2->bmp.cPlanes = bmpHdr->biPlanes;
|
---|
123 | iconhdr2->bmp.cBitCount= bmpHdr->biBitCount;
|
---|
124 | os2rgb = (RGB *)(iconhdr2+1);
|
---|
125 | rgb = (RGBQUAD *)(bmpHdr+1);
|
---|
126 | if(bmpHdr->biBitCount < 24) {
|
---|
127 | for(i=0;i<(1<<bmpHdr->biBitCount);i++) {
|
---|
128 | os2rgb->bRed = rgb->red;
|
---|
129 | os2rgb->bBlue = rgb->blue;
|
---|
130 | os2rgb->bGreen = rgb->green;
|
---|
131 | os2rgb++;
|
---|
132 | rgb++;
|
---|
133 | }
|
---|
134 | }
|
---|
135 | //write 2*mono pixels + color pixels
|
---|
136 | //There are icons without an XOR mask, so check for it
|
---|
137 | if(bmpHdr->biSizeImage == colorsize) {
|
---|
138 | memset((char *)os2rgb, 0, bwsize);
|
---|
139 | memset((char *)os2rgb+bwsize, 0, bwsize);
|
---|
140 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize);
|
---|
141 | }
|
---|
142 | else {
|
---|
143 | memcpy((char *)os2rgb, (char *)rgb+colorsize, bwsize);
|
---|
144 | memcpy((char *)os2rgb+bwsize, (char *)rgb+colorsize, bwsize);
|
---|
145 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize);
|
---|
146 | }
|
---|
147 | *OS2ResSize = iconsize;
|
---|
148 | return((PBYTE)iconhdr);
|
---|
149 | }
|
---|
150 | //******************************************************************************
|
---|
151 | //******************************************************************************
|
---|
152 | void FreeIcon(void *os2icon)
|
---|
153 | {
|
---|
154 | free(os2icon);
|
---|
155 | }
|
---|
156 | //******************************************************************************
|
---|
157 | //******************************************************************************
|
---|
158 | #endif
|
---|
159 |
|
---|
160 | //******************************************************************************
|
---|
161 | //******************************************************************************
|
---|
162 | ULONG QueryConvertedIconSize(WINBITMAPINFOHEADER *bmpHdr, int size)
|
---|
163 | {
|
---|
164 | int bwsize, colorsize, rgbsize, iconsize;
|
---|
165 |
|
---|
166 | bwsize = DIB_GetDIBImageBytes(bmpHdr->biWidth, (bmpHdr->biHeight/2), 1);
|
---|
167 | colorsize = DIB_GetDIBImageBytes(bmpHdr->biWidth, (bmpHdr->biHeight/2), bmpHdr->biBitCount);
|
---|
168 |
|
---|
169 | //SvL: 28-09-'98: only for <= 8
|
---|
170 | if(bmpHdr->biBitCount <= 8)
|
---|
171 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB2);
|
---|
172 | else rgbsize = 0;
|
---|
173 |
|
---|
174 | if(bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) {
|
---|
175 | bmpHdr->biSizeImage = bwsize + colorsize;
|
---|
176 | }
|
---|
177 |
|
---|
178 | //SvL: 28-09-'98: cllngenu.dll has an incorrect size in the header
|
---|
179 | if(bmpHdr->biSizeImage < colorsize) {
|
---|
180 | bmpHdr->biSizeImage = colorsize;
|
---|
181 | }
|
---|
182 | //bitmapfileheader for AndXor mask + 2 RGB structs + bitmapfileheader
|
---|
183 | //for color bitmap + RGB structs for all the colors
|
---|
184 | //SvL, 3-3-98: 2*bwsize
|
---|
185 | iconsize = 2*sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) +
|
---|
186 | rgbsize + 2*bwsize + bmpHdr->biSizeImage;
|
---|
187 |
|
---|
188 | return iconsize;
|
---|
189 | }
|
---|
190 | //******************************************************************************
|
---|
191 | //NOTE: offsetBits is the value added to the offBits bitmap structure members
|
---|
192 | // (handy for converting icon groups)
|
---|
193 | //******************************************************************************
|
---|
194 | void *ConvertIcon(WINBITMAPINFOHEADER *bmpHdr, int size, int *os2size, int offsetBits)
|
---|
195 | {
|
---|
196 | RGBQUAD *rgb;
|
---|
197 | RGB2 *os2rgb;
|
---|
198 | int bwsize, i, colorsize, rgbsize, iconsize;
|
---|
199 | BITMAPFILEHEADER2 *iconhdr;
|
---|
200 | BITMAPFILEHEADER2 *iconhdr2;
|
---|
201 | char *pAnd, *pXor;
|
---|
202 |
|
---|
203 | bwsize = DIB_GetDIBImageBytes(bmpHdr->biWidth, (bmpHdr->biHeight/2), 1);
|
---|
204 | colorsize = DIB_GetDIBImageBytes(bmpHdr->biWidth, (bmpHdr->biHeight/2), bmpHdr->biBitCount);
|
---|
205 | //SvL: 28-09-'98: only for <= 8
|
---|
206 | if(bmpHdr->biBitCount <= 8)
|
---|
207 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB2);
|
---|
208 | else rgbsize = 0;
|
---|
209 |
|
---|
210 | if(bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) {
|
---|
211 | bmpHdr->biSizeImage = bwsize + colorsize;
|
---|
212 | }
|
---|
213 | dprintf(("Icon size : %d", bmpHdr->biSizeImage));
|
---|
214 | dprintf(("Icon Width : %d", bmpHdr->biWidth));
|
---|
215 | //height for both the XOR and AND bitmap (color & BW)
|
---|
216 | dprintf(("Height : %d", bmpHdr->biHeight));
|
---|
217 | dprintf(("Icon Bitcount: %d", bmpHdr->biBitCount));
|
---|
218 | dprintf(("Icon Compress: %d", bmpHdr->biCompression));
|
---|
219 |
|
---|
220 | //SvL: 28-09-'98: cllngenu.dll has an incorrect size in the header
|
---|
221 | if(bmpHdr->biSizeImage < colorsize) {
|
---|
222 | bmpHdr->biSizeImage = colorsize;
|
---|
223 | }
|
---|
224 | //bitmapfileheader for AndXor mask + 2 RGB structs + bitmapfileheader
|
---|
225 | //for color bitmap + RGB structs for all the colors
|
---|
226 | //SvL, 3-3-98: 2*bwsize
|
---|
227 | iconsize = 2*sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) +
|
---|
228 | rgbsize + 2*bwsize + bmpHdr->biSizeImage;
|
---|
229 |
|
---|
230 | iconhdr = (BITMAPFILEHEADER2 *)malloc(iconsize);
|
---|
231 | memset(iconhdr, 0, iconsize);
|
---|
232 | iconhdr->usType = BFT_COLORICON;
|
---|
233 | iconhdr->cbSize = sizeof(BITMAPFILEHEADER2);
|
---|
234 | iconhdr->xHotspot = 0;
|
---|
235 | iconhdr->yHotspot = 0;
|
---|
236 | iconhdr->offBits = 2*sizeof(BITMAPFILEHEADER2) +
|
---|
237 | 2*sizeof(RGB2) + rgbsize + offsetBits;
|
---|
238 | iconhdr->bmp2.cbFix = sizeof(BITMAPINFOHEADER2);
|
---|
239 | iconhdr->bmp2.cx = (USHORT)bmpHdr->biWidth;
|
---|
240 | iconhdr->bmp2.cy = (USHORT)bmpHdr->biHeight;
|
---|
241 | iconhdr->bmp2.cPlanes = 1;
|
---|
242 | iconhdr->bmp2.cBitCount= 1;
|
---|
243 | iconhdr->bmp2.ulCompression = BCA_UNCOMP;
|
---|
244 | iconhdr->bmp2.ulColorEncoding = BCE_RGB;
|
---|
245 | os2rgb = (RGB2 *)(iconhdr+1);
|
---|
246 | memset(os2rgb, 0, sizeof(RGB2));
|
---|
247 | memset(os2rgb+1, 0xff, sizeof(RGB)); //not reserved byte!
|
---|
248 | iconhdr2 = (BITMAPFILEHEADER2 *)(os2rgb+2);
|
---|
249 | iconhdr2->usType = BFT_COLORICON;
|
---|
250 | iconhdr2->cbSize = sizeof(BITMAPFILEHEADER2);
|
---|
251 | iconhdr2->xHotspot = 0;
|
---|
252 | iconhdr2->yHotspot = 0;
|
---|
253 | iconhdr2->offBits = 2*sizeof(BITMAPFILEHEADER2) +
|
---|
254 | 2*sizeof(RGB2) + rgbsize + 2*bwsize + offsetBits;
|
---|
255 | iconhdr2->bmp2.cbFix = sizeof(BITMAPINFOHEADER2);
|
---|
256 | iconhdr2->bmp2.cx = (USHORT)bmpHdr->biWidth;
|
---|
257 | iconhdr2->bmp2.cy = (USHORT)(bmpHdr->biHeight/2);
|
---|
258 | iconhdr2->bmp2.cPlanes = bmpHdr->biPlanes;
|
---|
259 | iconhdr2->bmp2.cBitCount= bmpHdr->biBitCount;
|
---|
260 | iconhdr2->bmp2.ulCompression = BCA_UNCOMP;
|
---|
261 | iconhdr2->bmp2.ulColorEncoding = BCE_RGB;
|
---|
262 | os2rgb = (RGB2 *)(iconhdr2+1);
|
---|
263 | rgb = (RGBQUAD *)(bmpHdr+1);
|
---|
264 | if(bmpHdr->biBitCount <= 8) {
|
---|
265 | for(i=0;i<(1<<bmpHdr->biBitCount);i++) {
|
---|
266 | os2rgb->bRed = rgb->red;
|
---|
267 | os2rgb->bBlue = rgb->blue;
|
---|
268 | os2rgb->bGreen = rgb->green;
|
---|
269 | os2rgb++;
|
---|
270 | rgb++;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | pXor = (char *)os2rgb;
|
---|
275 | pAnd = (char *)os2rgb + bwsize;
|
---|
276 |
|
---|
277 | if ((size - (bmpHdr->biSize + rgbsize + colorsize + bwsize)) == bwsize)
|
---|
278 | {//this means an AND and XOR mask is present (interleaved; and/xor)
|
---|
279 | char *q;
|
---|
280 | int i, linesize;
|
---|
281 |
|
---|
282 | linesize = bmpHdr->biWidth / 8;
|
---|
283 | q = (char *)rgb + colorsize;
|
---|
284 | for (i = 0; i < (bmpHdr->biHeight/2); i++) {
|
---|
285 | memcpy (pAnd, q, linesize);
|
---|
286 | pAnd += linesize;
|
---|
287 | q += linesize;
|
---|
288 |
|
---|
289 | memcpy (pXor, q, linesize);
|
---|
290 | pXor += linesize;
|
---|
291 | q += linesize;
|
---|
292 | }
|
---|
293 | } else {
|
---|
294 | memcpy (pAnd, (char *)rgb + colorsize, bwsize);
|
---|
295 | memset (pXor, 0, bwsize);
|
---|
296 | }
|
---|
297 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize);
|
---|
298 |
|
---|
299 | *os2size = iconsize;
|
---|
300 | return (void *)iconhdr;
|
---|
301 | }
|
---|
302 | //******************************************************************************
|
---|
303 | //******************************************************************************
|
---|
304 | void * WIN32API ConvertIconGroup(void *hdr, HINSTANCE hInstance, DWORD *ressize)
|
---|
305 | {
|
---|
306 | IconHeader *ihdr = (IconHeader *)hdr;
|
---|
307 | ResourceDirectory *rdir = (ResourceDirectory *)(ihdr + 1);
|
---|
308 | int i, groupsize = 0, os2iconsize;
|
---|
309 | BITMAPARRAYFILEHEADER2 *bafh, *orgbafh;
|
---|
310 | WINBITMAPINFOHEADER *iconhdr;
|
---|
311 | void *os2icon;
|
---|
312 | HRSRC hRes;
|
---|
313 |
|
---|
314 | dprintf(("Icon Group type :%d", ihdr->wType));
|
---|
315 | dprintf(("Icon Group count:%d", ihdr->wCount));
|
---|
316 | for(i=0;i<ihdr->wCount;i++) {
|
---|
317 | dprintf2(("Icon : %d", rdir->wNameOrdinal));
|
---|
318 | dprintf2(("Width : %d", (int)rdir->bWidth));
|
---|
319 | dprintf2(("Height : %d", (int)rdir->bHeight));
|
---|
320 | dprintf2(("Colors : %d", (int)rdir->bColorCount));
|
---|
321 | dprintf2(("Bits : %d", rdir->wBitCount));
|
---|
322 | dprintf2(("ResBytes: %d", rdir->lBytesInRes));
|
---|
323 | hRes = FindResourceA(hInstance,
|
---|
324 | (LPCSTR)rdir->wNameOrdinal, (LPSTR)NTRT_ICON);
|
---|
325 |
|
---|
326 | groupsize += QueryConvertedIconSize((WINBITMAPINFOHEADER *)LockResource(LoadResource(hInstance, hRes)),
|
---|
327 | SizeofResource(hInstance, hRes));
|
---|
328 | rdir++;
|
---|
329 | }
|
---|
330 | groupsize = groupsize+ihdr->wCount*sizeof(BITMAPARRAYFILEHEADER2);
|
---|
331 | bafh = (BITMAPARRAYFILEHEADER2 *)malloc(groupsize);
|
---|
332 | memset(bafh, 0, groupsize);
|
---|
333 | orgbafh = bafh;
|
---|
334 |
|
---|
335 | rdir = (ResourceDirectory *)(ihdr + 1);
|
---|
336 | for(i=0;i<ihdr->wCount;i++) {
|
---|
337 | bafh->usType = BFT_BITMAPARRAY;
|
---|
338 | bafh->cbSize = sizeof(BITMAPARRAYFILEHEADER2);
|
---|
339 | bafh->cxDisplay = 0;
|
---|
340 | bafh->cyDisplay = 0;
|
---|
341 | hRes = FindResourceA(hInstance,
|
---|
342 | (LPCSTR)rdir->wNameOrdinal, (LPSTR)NTRT_ICON);
|
---|
343 | if(hRes == NULL) {
|
---|
344 | dprintf(("Can't find icon!"));
|
---|
345 | rdir++;
|
---|
346 | continue;
|
---|
347 | }
|
---|
348 | iconhdr = (WINBITMAPINFOHEADER *)LockResource(LoadResource(hInstance, hRes));
|
---|
349 | os2icon = ConvertIcon(iconhdr, SizeofResource(hInstance, hRes), &os2iconsize, (ULONG)bafh - (ULONG)orgbafh + sizeof(BITMAPARRAYFILEHEADER2)-sizeof(BITMAPFILEHEADER2));
|
---|
350 | if(os2icon == NULL) {
|
---|
351 | dprintf(("Can't convert icon!"));
|
---|
352 | rdir++;
|
---|
353 | continue;
|
---|
354 | }
|
---|
355 |
|
---|
356 | if(i != ihdr->wCount -1) {
|
---|
357 | bafh->offNext = (ULONG)&bafh->bfh2 - (ULONG)orgbafh + os2iconsize;
|
---|
358 | }
|
---|
359 | else bafh->offNext = 0;
|
---|
360 |
|
---|
361 | memcpy((char *)&bafh->bfh2, os2icon, os2iconsize);
|
---|
362 | free(os2icon);
|
---|
363 |
|
---|
364 | bafh = (BITMAPARRAYFILEHEADER2 *)((ULONG)&bafh->bfh2 + os2iconsize);
|
---|
365 |
|
---|
366 | rdir++;
|
---|
367 | }
|
---|
368 | *ressize = groupsize;
|
---|
369 | return (void *)orgbafh;
|
---|
370 | }
|
---|
371 | //******************************************************************************
|
---|
372 | //******************************************************************************
|
---|