| 1 | /* $Id: cvtcursor.cpp,v 1.10 2000-02-16 14:25:37 sandervl Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * PE2LX cursor conversion code | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright 1999 Daniela Engert (dani@ngrt.de) | 
|---|
| 8 | * | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 | #define INCL_GPIBITMAPS | 
|---|
| 14 | #define INCL_BITMAPFILEFORMAT | 
|---|
| 15 | #define INCL_DOSFILEMGR          /* File Manager values      */ | 
|---|
| 16 | #define INCL_DOSERRORS           /* DOS Error values         */ | 
|---|
| 17 | #define INCL_DOSPROCESS          /* DOS Process values       */ | 
|---|
| 18 | #define INCL_DOSMISC             /* DOS Miscellanous values  */ | 
|---|
| 19 | #define INCL_WIN | 
|---|
| 20 | #include <os2wrap.h> | 
|---|
| 21 | #include <stdio.h> | 
|---|
| 22 | #include <string.h> | 
|---|
| 23 | #include <stdlib.h> | 
|---|
| 24 | #include <string.h> | 
|---|
| 25 | #include <win32type.h> | 
|---|
| 26 | #include <winicon.h> | 
|---|
| 27 | #include <wincursor.h> | 
|---|
| 28 | #include <misc.h> | 
|---|
| 29 |  | 
|---|
| 30 | #define DBG_LOCALLOG    DBG_cvtcursor | 
|---|
| 31 | #include "dbglocal.h" | 
|---|
| 32 |  | 
|---|
| 33 | //****************************************************************************** | 
|---|
| 34 | //****************************************************************************** | 
|---|
| 35 | ULONG QueryConvertedCursorSize(CursorComponent *curHdr, int size) | 
|---|
| 36 | { | 
|---|
| 37 | WINBITMAPINFOHEADER *bmpHdr = (WINBITMAPINFOHEADER *)(curHdr+1); | 
|---|
| 38 | int bwsize, colorsize, rgbsize, cursorsize; | 
|---|
| 39 |  | 
|---|
| 40 | bwsize   = (bmpHdr->biWidth*(bmpHdr->biHeight/2))/8; | 
|---|
| 41 | colorsize = bmpHdr->biWidth*(bmpHdr->biHeight/2); | 
|---|
| 42 |  | 
|---|
| 43 | if(bmpHdr->biBitCount <= 8) | 
|---|
| 44 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB2); | 
|---|
| 45 | else  rgbsize = 0; | 
|---|
| 46 |  | 
|---|
| 47 | switch(bmpHdr->biBitCount) { | 
|---|
| 48 | case 1: | 
|---|
| 49 | colorsize /= 8; | 
|---|
| 50 | break; | 
|---|
| 51 | case 4: | 
|---|
| 52 | colorsize /= 2; | 
|---|
| 53 | break; | 
|---|
| 54 | case 8: | 
|---|
| 55 | break; | 
|---|
| 56 | case 16: | 
|---|
| 57 | colorsize *= 2; | 
|---|
| 58 | break; | 
|---|
| 59 | case 24: | 
|---|
| 60 | colorsize *= 3; | 
|---|
| 61 | break; | 
|---|
| 62 | case 32: | 
|---|
| 63 | colorsize *= 4; | 
|---|
| 64 | break; | 
|---|
| 65 | } | 
|---|
| 66 | if(bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) { | 
|---|
| 67 | bmpHdr->biSizeImage = bwsize + colorsize; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | //SvL: 28-09-'98: cllngenu.dll has an incorrect size in the header | 
|---|
| 71 | if(bmpHdr->biSizeImage < colorsize) { | 
|---|
| 72 | bmpHdr->biSizeImage = colorsize; | 
|---|
| 73 | } | 
|---|
| 74 | if(bmpHdr->biBitCount > 1) { | 
|---|
| 75 | //And mask, xor mask (0) + color image | 
|---|
| 76 | cursorsize = 2*sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) + | 
|---|
| 77 | rgbsize + 2*bwsize + bmpHdr->biSizeImage; | 
|---|
| 78 | } | 
|---|
| 79 | else { | 
|---|
| 80 | //And + xor mask | 
|---|
| 81 | cursorsize = sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) + 2*bwsize; | 
|---|
| 82 | } | 
|---|
| 83 |  | 
|---|
| 84 | return cursorsize; | 
|---|
| 85 | } | 
|---|
| 86 | //****************************************************************************** | 
|---|
| 87 | //NOTE: offsetBits is the value added to the offBits bitmap structure members | 
|---|
| 88 | //      (handy for converting cursor groups) | 
|---|
| 89 | //****************************************************************************** | 
|---|
| 90 | void *ConvertCursor(CursorComponent *curHdr, int size, int *os2size, int offsetBits) | 
|---|
| 91 | { | 
|---|
| 92 | RGBQUAD   *rgb; | 
|---|
| 93 | RGB2      *os2rgb; | 
|---|
| 94 | WINBITMAPINFOHEADER *bmpHdr = (WINBITMAPINFOHEADER *)(curHdr+1); | 
|---|
| 95 | BITMAPFILEHEADER2   *cursorhdr, *cursorhdr2; | 
|---|
| 96 | int        i, bwsize, bmpsize, cursorsize, rgbsize, colorsize; | 
|---|
| 97 |  | 
|---|
| 98 | dprintf(("ConvertCursor: Cursor size %d", size)); | 
|---|
| 99 | bwsize   = (bmpHdr->biWidth*(bmpHdr->biHeight/2))/8; | 
|---|
| 100 | colorsize = bmpHdr->biWidth*(bmpHdr->biHeight/2); | 
|---|
| 101 |  | 
|---|
| 102 | if(bmpHdr->biBitCount <= 8) | 
|---|
| 103 | rgbsize = (1<<bmpHdr->biBitCount)*sizeof(RGB2); | 
|---|
| 104 | else  rgbsize = 0; | 
|---|
| 105 |  | 
|---|
| 106 | switch(bmpHdr->biBitCount) { | 
|---|
| 107 | case 1: | 
|---|
| 108 | colorsize /= 8; | 
|---|
| 109 | break; | 
|---|
| 110 | case 4: | 
|---|
| 111 | colorsize /= 2; | 
|---|
| 112 | break; | 
|---|
| 113 | case 8: | 
|---|
| 114 | break; | 
|---|
| 115 | case 16: | 
|---|
| 116 | colorsize *= 2; | 
|---|
| 117 | break; | 
|---|
| 118 | case 24: | 
|---|
| 119 | colorsize *= 3; | 
|---|
| 120 | break; | 
|---|
| 121 | case 32: | 
|---|
| 122 | colorsize *= 4; | 
|---|
| 123 | break; | 
|---|
| 124 | } | 
|---|
| 125 | if(bmpHdr->biSizeImage == 0 && bmpHdr->biCompression == 0) { | 
|---|
| 126 | bmpHdr->biSizeImage = bwsize + colorsize; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | //SvL: 28-09-'98: cllngenu.dll has an incorrect size in the header | 
|---|
| 130 | if(bmpHdr->biSizeImage < colorsize) { | 
|---|
| 131 | bmpHdr->biSizeImage = colorsize; | 
|---|
| 132 | } | 
|---|
| 133 | if(bmpHdr->biBitCount == 1) { | 
|---|
| 134 | //And + xor mask | 
|---|
| 135 | cursorsize = sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) + 2*bwsize; | 
|---|
| 136 | } | 
|---|
| 137 | else { | 
|---|
| 138 | //And mask, xor mask (0) + color image | 
|---|
| 139 | cursorsize = 2*sizeof(BITMAPFILEHEADER2) + 2*sizeof(RGB2) + | 
|---|
| 140 | rgbsize + 2*bwsize + bmpHdr->biSizeImage; | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | cursorhdr  = (BITMAPFILEHEADER2 *)malloc(cursorsize); | 
|---|
| 144 | memset(cursorhdr, 0, cursorsize); | 
|---|
| 145 | cursorhdr->usType        = BFT_POINTER; | 
|---|
| 146 | cursorhdr->cbSize        = sizeof(BITMAPFILEHEADER2); | 
|---|
| 147 | cursorhdr->xHotspot      = curHdr->xHotspot; | 
|---|
| 148 |  | 
|---|
| 149 | /* @@@PH y-hotspot is upside down ! */ | 
|---|
| 150 | cursorhdr->yHotspot      = (bmpHdr->biHeight >> 1)       /* height div 2 */ | 
|---|
| 151 | - curHdr->yHotspot;         /* subtract hot.y */ | 
|---|
| 152 |  | 
|---|
| 153 | dprintf2(("Cursor Hot.x   : %d", curHdr->xHotspot)); | 
|---|
| 154 | dprintf2(("Cursor Hot.y   : %d", curHdr->yHotspot)); | 
|---|
| 155 |  | 
|---|
| 156 | if(bmpHdr->biBitCount == 1) { | 
|---|
| 157 | cursorhdr->offBits = sizeof(BITMAPFILEHEADER2) + | 
|---|
| 158 | 2*sizeof(RGB2) + offsetBits; | 
|---|
| 159 | } | 
|---|
| 160 | else { | 
|---|
| 161 | cursorhdr->offBits = 2*sizeof(BITMAPFILEHEADER2) + | 
|---|
| 162 | 2*sizeof(RGB2) + rgbsize + offsetBits; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | cursorhdr->bmp2.cbFix     = sizeof(BITMAPINFOHEADER2); | 
|---|
| 166 | cursorhdr->bmp2.cx        = (USHORT)bmpHdr->biWidth; | 
|---|
| 167 | cursorhdr->bmp2.cy        = (USHORT)(bmpHdr->biHeight); | 
|---|
| 168 | cursorhdr->bmp2.cPlanes   = bmpHdr->biPlanes; | 
|---|
| 169 | cursorhdr->bmp2.cBitCount = 1; | 
|---|
| 170 | cursorhdr->bmp2.ulCompression   = BCA_UNCOMP; | 
|---|
| 171 | cursorhdr->bmp2.ulColorEncoding = BCE_RGB; | 
|---|
| 172 | dprintf2(("Cursor size    : %d", bmpHdr->biSizeImage)); | 
|---|
| 173 | dprintf2(("Cursor Width   : %d", bmpHdr->biWidth)); | 
|---|
| 174 | //height for both the XOR and AND bitmap (color & BW) | 
|---|
| 175 | dprintf2(("Height         : %d", bmpHdr->biHeight)); | 
|---|
| 176 | dprintf2(("Cursor Bitcount: %d", bmpHdr->biBitCount)); | 
|---|
| 177 | dprintf2(("Cursor Compress: %d", bmpHdr->biCompression)); | 
|---|
| 178 |  | 
|---|
| 179 | os2rgb                   = (RGB2 *)(cursorhdr+1); | 
|---|
| 180 | rgb                      = (RGBQUAD *)(bmpHdr+1); | 
|---|
| 181 | if(bmpHdr->biBitCount == 1) { | 
|---|
| 182 | for(i=0;i<2;i++) { | 
|---|
| 183 | os2rgb->bRed   = rgb->red; | 
|---|
| 184 | os2rgb->bBlue  = rgb->blue; | 
|---|
| 185 | os2rgb->bGreen = rgb->green; | 
|---|
| 186 | os2rgb++; | 
|---|
| 187 | rgb++; | 
|---|
| 188 | } | 
|---|
| 189 | //write XOR and AND mask | 
|---|
| 190 | memcpy((char *)os2rgb, (char *)rgb, bwsize*2); | 
|---|
| 191 | } | 
|---|
| 192 | else { | 
|---|
| 193 | memset(os2rgb, 0, sizeof(RGB2)); | 
|---|
| 194 | memset(os2rgb+1, 0xff, sizeof(RGB)); //not reserved byte! | 
|---|
| 195 | cursorhdr2               = (BITMAPFILEHEADER2 *)(os2rgb+2); | 
|---|
| 196 | cursorhdr2->usType       = BFT_COLORICON; | 
|---|
| 197 | cursorhdr2->cbSize       = sizeof(BITMAPFILEHEADER2); | 
|---|
| 198 | cursorhdr2->xHotspot     = curHdr->xHotspot; | 
|---|
| 199 | cursorhdr2->yHotspot     = (bmpHdr->biHeight >> 1)       /* height div 2 */ | 
|---|
| 200 | - curHdr->yHotspot;         /* subtract hot.y */ | 
|---|
| 201 | cursorhdr2->offBits      = 2*sizeof(BITMAPFILEHEADER2) + | 
|---|
| 202 | 2*sizeof(RGB2) + rgbsize + 2*bwsize + offsetBits; | 
|---|
| 203 | cursorhdr2->bmp2.cbFix   = sizeof(BITMAPINFOHEADER2); | 
|---|
| 204 | cursorhdr2->bmp2.cx      = (USHORT)bmpHdr->biWidth; | 
|---|
| 205 | cursorhdr2->bmp2.cy      = (USHORT)(bmpHdr->biHeight/2); | 
|---|
| 206 | cursorhdr2->bmp2.cPlanes = bmpHdr->biPlanes; | 
|---|
| 207 | cursorhdr2->bmp2.cBitCount= bmpHdr->biBitCount; | 
|---|
| 208 | cursorhdr2->bmp2.ulCompression   = BCA_UNCOMP; | 
|---|
| 209 | cursorhdr2->bmp2.ulColorEncoding = BCE_RGB; | 
|---|
| 210 | os2rgb                 = (RGB2 *)(cursorhdr2+1); | 
|---|
| 211 | rgb                    = (RGBQUAD *)(bmpHdr+1); | 
|---|
| 212 | if(bmpHdr->biBitCount <= 8) { | 
|---|
| 213 | for(i=0;i<(1<<bmpHdr->biBitCount);i++) { | 
|---|
| 214 | os2rgb->bRed   = rgb->red; | 
|---|
| 215 | os2rgb->bBlue  = rgb->blue; | 
|---|
| 216 | os2rgb->bGreen = rgb->green; | 
|---|
| 217 | os2rgb++; | 
|---|
| 218 | rgb++; | 
|---|
| 219 | } | 
|---|
| 220 | } | 
|---|
| 221 | //write XOR and AND mask | 
|---|
| 222 | char *pXor = (char *)os2rgb; | 
|---|
| 223 | char *pAnd = (char *)os2rgb + bwsize; | 
|---|
| 224 |  | 
|---|
| 225 | memcpy (pAnd, (char *)rgb + colorsize, bwsize); | 
|---|
| 226 | memset (pXor, 0, bwsize); | 
|---|
| 227 | memcpy((char *)os2rgb+2*bwsize, (char *)rgb, colorsize); | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | *os2size = cursorsize; | 
|---|
| 231 | return cursorhdr; | 
|---|
| 232 | } | 
|---|
| 233 | //****************************************************************************** | 
|---|
| 234 | //****************************************************************************** | 
|---|