| 1 | /* $Id: icon.cpp,v 1.1 1999-09-06 02:20:04 bird Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * PE2LX icons | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | /******************************************************************************* | 
|---|
| 14 | *   Header Files                                                               * | 
|---|
| 15 | *******************************************************************************/ | 
|---|
| 16 | #include <pe2lx.h> | 
|---|
| 17 | #include "icon.h" | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | //****************************************************************************** | 
|---|
| 22 | //****************************************************************************** | 
|---|
| 23 | OS2Icon::OS2Icon(int id, WINBITMAPINFOHEADER *bmpHdr, int size) : | 
|---|
| 24 | id(0), next(NULL), iconhdr(NULL), iconsize(0), prevoffset(0) | 
|---|
| 25 | { | 
|---|
| 26 | RGBQUAD *rgb; | 
|---|
| 27 | RGB     *os2rgb; | 
|---|
| 28 | int     bwsize, i, colorsize, rgbsize; | 
|---|
| 29 | OS2Icon *icon = OS2Icon::icons; | 
|---|
| 30 |  | 
|---|
| 31 | vltassert((ULONG)bmpHdr > MINPTR && (ULONG)bmpHdr+size < MAXPTR); | 
|---|
| 32 |  | 
|---|
| 33 | if (icon != NULL) | 
|---|
| 34 | { | 
|---|
| 35 | while (icon->next != NULL) | 
|---|
| 36 | icon = icon->next; | 
|---|
| 37 |  | 
|---|
| 38 | icon->next = this; | 
|---|
| 39 | } | 
|---|
| 40 | else | 
|---|
| 41 | icons         = this; | 
|---|
| 42 |  | 
|---|
| 43 | this->id = id; | 
|---|
| 44 | bwsize   = (bmpHdr->biWidth*(bmpHdr->biHeight/2))/8; | 
|---|
| 45 | colorsize = bmpHdr->biWidth*(bmpHdr->biHeight/2); | 
|---|
| 46 | if (bmpHdr->biBitCount <= 8) | 
|---|
| 47 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB); | 
|---|
| 48 | else | 
|---|
| 49 | rgbsize = 0; | 
|---|
| 50 |  | 
|---|
| 51 | switch (bmpHdr->biBitCount) | 
|---|
| 52 | { | 
|---|
| 53 | case 1: | 
|---|
| 54 | colorsize /= 8; | 
|---|
| 55 | break; | 
|---|
| 56 | case 4: | 
|---|
| 57 | colorsize /= 2; | 
|---|
| 58 | break; | 
|---|
| 59 | case 8: | 
|---|
| 60 | break; | 
|---|
| 61 | case 16: | 
|---|
| 62 | colorsize *= 2; | 
|---|
| 63 | break; | 
|---|
| 64 | case 24: | 
|---|
| 65 | colorsize *= 3; | 
|---|
| 66 | break; | 
|---|
| 67 | case 32: | 
|---|
| 68 | colorsize *= 4; | 
|---|
| 69 | break; | 
|---|
| 70 | } | 
|---|
| 71 | if (bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) | 
|---|
| 72 | { | 
|---|
| 73 | bmpHdr->biSizeImage = bwsize + colorsize; | 
|---|
| 74 | } | 
|---|
| 75 | cout << "Icon size    : " << bmpHdr->biSizeImage << endl; | 
|---|
| 76 | cout << "Icon Width   : " << bmpHdr->biWidth << endl; | 
|---|
| 77 | //height for both the XOR and AND bitmap (color & BW) | 
|---|
| 78 | cout << "Height       : " << bmpHdr->biHeight << endl; | 
|---|
| 79 | cout << "Icon Bitcount: " << bmpHdr->biBitCount << endl; | 
|---|
| 80 | cout << "Icon Compress: " << bmpHdr->biCompression << endl; | 
|---|
| 81 |  | 
|---|
| 82 | //SvL: 28-09-'98: cllngenu.dll has an incorrect size in the header | 
|---|
| 83 | if(bmpHdr->biSizeImage < colorsize) { | 
|---|
| 84 | bmpHdr->biSizeImage = colorsize; | 
|---|
| 85 | } | 
|---|
| 86 | //bitmapfileheader for AndXor mask + 2 RGB structs + bitmapfileheader | 
|---|
| 87 | //for color bitmap + RGB structs for all the colors | 
|---|
| 88 | //SvL, 3-3-98: 2*bwsize | 
|---|
| 89 | iconsize = 2*sizeof(BITMAPFILEHEADER) + 2*sizeof(RGB) + | 
|---|
| 90 | rgbsize + 2*bwsize + bmpHdr->biSizeImage; | 
|---|
| 91 | //There are icons without an XOR mask, so check for it | 
|---|
| 92 | if (bmpHdr->biSizeImage == colorsize) | 
|---|
| 93 | { | 
|---|
| 94 | iconsize += bwsize; | 
|---|
| 95 | } | 
|---|
| 96 | iconhdr  = (BITMAPFILEHEADER *)malloc(iconsize); | 
|---|
| 97 | if (iconhdr == NULL) | 
|---|
| 98 | { | 
|---|
| 99 | if (icons == this) | 
|---|
| 100 | icons = NULL; | 
|---|
| 101 | else | 
|---|
| 102 | icon->next = NULL; | 
|---|
| 103 | vltassert(iconhdr != NULL); | 
|---|
| 104 | } | 
|---|
| 105 | memset((void*)iconhdr, 0xaa, iconsize);     //@KSO: this makes comparing converteded files easier | 
|---|
| 106 |  | 
|---|
| 107 | iconhdr->usType     = BFT_COLORICON; | 
|---|
| 108 | iconhdr->cbSize     = sizeof(BITMAPFILEHEADER); | 
|---|
| 109 | iconhdr->xHotspot   = 0; | 
|---|
| 110 | iconhdr->yHotspot   = 0; | 
|---|
| 111 | iconhdr->offBits    = 2*sizeof(BITMAPFILEHEADER) + | 
|---|
| 112 | 2*sizeof(RGB) + rgbsize; | 
|---|
| 113 | iconhdr->bmp.cbFix  = sizeof(BITMAPINFOHEADER); | 
|---|
| 114 | iconhdr->bmp.cx     = (USHORT)bmpHdr->biWidth; | 
|---|
| 115 | iconhdr->bmp.cy     = (USHORT)bmpHdr->biHeight; | 
|---|
| 116 | iconhdr->bmp.cPlanes   = 1; | 
|---|
| 117 | iconhdr->bmp.cBitCount = 1; | 
|---|
| 118 | os2rgb                 = (RGB *)(iconhdr+1); | 
|---|
| 119 | memset(os2rgb, 0, sizeof(RGB)); | 
|---|
| 120 | memset(os2rgb+1, 0xff, sizeof(RGB)); | 
|---|
| 121 | iconhdr2               = (BITMAPFILEHEADER *)(os2rgb+2); | 
|---|
| 122 | iconhdr2->usType       = BFT_COLORICON; | 
|---|
| 123 | iconhdr2->cbSize    = sizeof(BITMAPFILEHEADER); | 
|---|
| 124 | iconhdr2->xHotspot  = 0; | 
|---|
| 125 | iconhdr2->yHotspot  = 0; | 
|---|
| 126 | iconhdr2->offBits   = 2*sizeof(BITMAPFILEHEADER) + | 
|---|
| 127 | 2*sizeof(RGB) + rgbsize + 2*bwsize; | 
|---|
| 128 | iconhdr2->bmp.cbFix    = sizeof(BITMAPINFOHEADER); | 
|---|
| 129 | iconhdr2->bmp.cx    = (USHORT)bmpHdr->biWidth; | 
|---|
| 130 | iconhdr2->bmp.cy    = (USHORT)(bmpHdr->biHeight/2); | 
|---|
| 131 | iconhdr2->bmp.cPlanes  = bmpHdr->biPlanes; | 
|---|
| 132 | iconhdr2->bmp.cBitCount= bmpHdr->biBitCount; | 
|---|
| 133 | os2rgb                 = (RGB *)(iconhdr2+1); | 
|---|
| 134 | rgb           = (RGBQUAD *)(bmpHdr+1); | 
|---|
| 135 | if (bmpHdr->biBitCount < 24) | 
|---|
| 136 | { | 
|---|
| 137 | for (i=0;i<(1<<bmpHdr->biBitCount);i++) | 
|---|
| 138 | { | 
|---|
| 139 | //cout << "(R,G,B) = (" << (int)rgb->red << ", " << (int)rgb->green << ", " << (int)rgb->blue << ")" << endl; | 
|---|
| 140 | os2rgb->bRed   = rgb->red; | 
|---|
| 141 | os2rgb->bBlue  = rgb->blue; | 
|---|
| 142 | os2rgb->bGreen = rgb->green; | 
|---|
| 143 | os2rgb++; | 
|---|
| 144 | rgb++; | 
|---|
| 145 | } | 
|---|
| 146 | } | 
|---|
| 147 | //write 2*mono pixels + color pixels | 
|---|
| 148 | //There are icons without an XOR mask, so check for it | 
|---|
| 149 | if (bmpHdr->biSizeImage == colorsize) | 
|---|
| 150 | { | 
|---|
| 151 | memset((char *)os2rgb, 0, bwsize); | 
|---|
| 152 | memset((char *)os2rgb+bwsize, 0, bwsize); | 
|---|
| 153 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize); | 
|---|
| 154 | } | 
|---|
| 155 | else | 
|---|
| 156 | { | 
|---|
| 157 | memcpy((char *)os2rgb, (char *)rgb+colorsize, bwsize); | 
|---|
| 158 | memcpy((char *)os2rgb+bwsize, (char *)rgb+colorsize, bwsize); | 
|---|
| 159 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize); | 
|---|
| 160 |  | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | //****************************************************************************** | 
|---|
| 164 | //****************************************************************************** | 
|---|
| 165 | void OS2Icon::SetIconHdrOffset(int offset) | 
|---|
| 166 | { | 
|---|
| 167 | iconhdr->offBits  += offset - prevoffset; | 
|---|
| 168 | iconhdr2->offBits += offset - prevoffset; | 
|---|
| 169 | //remember in case icons are used in multiple groups | 
|---|
| 170 | //(can't imagine this ever happening, but you never know) | 
|---|
| 171 | prevoffset         = offset; | 
|---|
| 172 | } | 
|---|
| 173 | //****************************************************************************** | 
|---|
| 174 | //****************************************************************************** | 
|---|
| 175 | OS2Icon::~OS2Icon() | 
|---|
| 176 | { | 
|---|
| 177 | if (iconhdr)  free(iconhdr); | 
|---|
| 178 | } | 
|---|
| 179 | //****************************************************************************** | 
|---|
| 180 | //****************************************************************************** | 
|---|
| 181 | int OS2Icon::QueryIconSize() | 
|---|
| 182 | { | 
|---|
| 183 | return (iconsize); | 
|---|
| 184 | } | 
|---|
| 185 | //****************************************************************************** | 
|---|
| 186 | //****************************************************************************** | 
|---|
| 187 | BITMAPFILEHEADER *OS2Icon::GetIconHeader() | 
|---|
| 188 | { | 
|---|
| 189 | return (iconhdr); | 
|---|
| 190 | } | 
|---|
| 191 | //****************************************************************************** | 
|---|
| 192 | //****************************************************************************** | 
|---|
| 193 | /*static*/ OS2Icon *OS2Icon::GetIcon(int id) | 
|---|
| 194 | { | 
|---|
| 195 | OS2Icon *icon = OS2Icon::icons; | 
|---|
| 196 |  | 
|---|
| 197 | while (icon != NULL) | 
|---|
| 198 | { | 
|---|
| 199 | if (icon->id == id)  return (icon); | 
|---|
| 200 | icon = icon->next; | 
|---|
| 201 | } | 
|---|
| 202 | return (NULL); | 
|---|
| 203 | } | 
|---|
| 204 | //****************************************************************************** | 
|---|
| 205 | //****************************************************************************** | 
|---|
| 206 | /*static*/ void OS2Icon::DestroyAll() | 
|---|
| 207 | { | 
|---|
| 208 | OS2Icon *icon = OS2Icon::icons, *next; | 
|---|
| 209 |  | 
|---|
| 210 | while (icon != NULL) | 
|---|
| 211 | { | 
|---|
| 212 | next = icon->next; | 
|---|
| 213 | delete(icon); | 
|---|
| 214 | icon = next; | 
|---|
| 215 | } | 
|---|
| 216 | OS2Icon::icons = NULL; | 
|---|
| 217 | } | 
|---|
| 218 | //****************************************************************************** | 
|---|
| 219 | OS2Icon *OS2Icon::icons = NULL; | 
|---|