| 1 | /* $Id: dib.cpp,v 1.8 2000-11-15 20:44:26 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 | #include "dib.h"
|
|---|
| 23 |
|
|---|
| 24 | #define DBG_LOCALLOG DBG_dib
|
|---|
| 25 | #include "dbglocal.h"
|
|---|
| 26 |
|
|---|
| 27 | /***********************************************************************
|
|---|
| 28 | * DIB_GetDIBWidthBytes
|
|---|
| 29 | *
|
|---|
| 30 | * Return the width of a DIB bitmap in bytes. DIB bitmap data is 32-bit aligned.
|
|---|
| 31 | * http://www.microsoft.com/msdn/sdk/platforms/doc/sdk/win32/struc/src/str01.htm
|
|---|
| 32 | */
|
|---|
| 33 | int WIN32API DIB_GetDIBWidthBytes( int width, int depth )
|
|---|
| 34 | {
|
|---|
| 35 | int words;
|
|---|
| 36 |
|
|---|
| 37 | switch(depth)
|
|---|
| 38 | {
|
|---|
| 39 | case 1: words = (width + 31) / 32; break;
|
|---|
| 40 | case 4: words = (width + 7) / 8; break;
|
|---|
| 41 | case 8: words = (width + 3) / 4; break;
|
|---|
| 42 | case 15:
|
|---|
| 43 | case 16: words = (width + 1) / 2; break;
|
|---|
| 44 | case 24: words = (width * 3 + 3)/4; break;
|
|---|
| 45 |
|
|---|
| 46 | default:
|
|---|
| 47 | dprintf(("(%d): Unsupported depth\n", depth ));
|
|---|
| 48 | /* fall through */
|
|---|
| 49 | case 32:
|
|---|
| 50 | words = width;
|
|---|
| 51 | }
|
|---|
| 52 | return 4 * words;
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | /***********************************************************************
|
|---|
| 56 | * DIB_GetDIBImageBytes
|
|---|
| 57 | *
|
|---|
| 58 | * Return the number of bytes used to hold the image in a DIB bitmap.
|
|---|
| 59 | */
|
|---|
| 60 | int DIB_GetDIBImageBytes( int width, int height, int depth )
|
|---|
| 61 | {
|
|---|
| 62 | return DIB_GetDIBWidthBytes( width, depth ) * abs( height );
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 | /***********************************************************************
|
|---|
| 67 | * DIB_BitmapInfoSize
|
|---|
| 68 | *
|
|---|
| 69 | * Return the size of the bitmap info structure including color table.
|
|---|
| 70 | */
|
|---|
| 71 | int DIB_BitmapInfoSize( BITMAPINFO * info, WORD coloruse )
|
|---|
| 72 | {
|
|---|
| 73 | int colors;
|
|---|
| 74 |
|
|---|
| 75 | if (info->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
|
|---|
| 76 | {
|
|---|
| 77 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)info;
|
|---|
| 78 | colors = (core->bcBitCount <= 8) ? 1 << core->bcBitCount : 0;
|
|---|
| 79 | return sizeof(BITMAPCOREHEADER) + colors *
|
|---|
| 80 | ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBTRIPLE) : sizeof(WORD));
|
|---|
| 81 | }
|
|---|
| 82 | else /* assume BITMAPINFOHEADER */
|
|---|
| 83 | {
|
|---|
| 84 | colors = info->bmiHeader.biClrUsed;
|
|---|
| 85 | if (!colors && (info->bmiHeader.biBitCount <= 8))
|
|---|
| 86 | colors = 1 << info->bmiHeader.biBitCount;
|
|---|
| 87 | return sizeof(BITMAPINFOHEADER) + colors *
|
|---|
| 88 | ((coloruse == DIB_RGB_COLORS) ? sizeof(RGBQUAD) : sizeof(WORD));
|
|---|
| 89 | }
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | /***********************************************************************
|
|---|
| 94 | * DIB_GetBitmapInfo
|
|---|
| 95 | *
|
|---|
| 96 | * Get the info from a bitmap header.
|
|---|
| 97 | * Return 1 for INFOHEADER, 0 for COREHEADER, -1 for error.
|
|---|
| 98 | */
|
|---|
| 99 | int DIB_GetBitmapInfo( const BITMAPINFOHEADER *header, DWORD *width,
|
|---|
| 100 | int *height, WORD *bpp, WORD *compr )
|
|---|
| 101 | {
|
|---|
| 102 | if (header->biSize == sizeof(BITMAPINFOHEADER))
|
|---|
| 103 | {
|
|---|
| 104 | *width = header->biWidth;
|
|---|
| 105 | *height = header->biHeight;
|
|---|
| 106 | *bpp = header->biBitCount;
|
|---|
| 107 | *compr = header->biCompression;
|
|---|
| 108 | return 1;
|
|---|
| 109 | }
|
|---|
| 110 | if (header->biSize == sizeof(BITMAPCOREHEADER))
|
|---|
| 111 | {
|
|---|
| 112 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)header;
|
|---|
| 113 | *width = core->bcWidth;
|
|---|
| 114 | *height = core->bcHeight;
|
|---|
| 115 | *bpp = core->bcBitCount;
|
|---|
| 116 | *compr = 0;
|
|---|
| 117 | return 0;
|
|---|
| 118 | }
|
|---|
| 119 | dprintf(("(%ld): wrong size for header\n", header->biSize ));
|
|---|
| 120 | return -1;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | /***********************************************************************
|
|---|
| 124 | * DIB_FixColorsToLoadflags
|
|---|
| 125 | *
|
|---|
| 126 | * Change color table entries when LR_LOADTRANSPARENT or LR_LOADMAP3DCOLORS
|
|---|
| 127 | * are in loadflags
|
|---|
| 128 | */
|
|---|
| 129 | void DIB_FixColorsToLoadflags(BITMAPINFO * bmi, UINT loadflags, BYTE pix)
|
|---|
| 130 | {
|
|---|
| 131 | int colors, bitcount;
|
|---|
| 132 | COLORREF c_W, c_S, c_F, c_L, c_C;
|
|---|
| 133 | int incr,i;
|
|---|
| 134 | RGBQUAD *ptr;
|
|---|
| 135 | char *colorptr;
|
|---|
| 136 |
|
|---|
| 137 | //SvL: Wine code doesn't work for OS/2 1.3 bitmaps
|
|---|
| 138 | if (bmi->bmiHeader.biSize == sizeof(BITMAPINFOHEADER))
|
|---|
| 139 | {
|
|---|
| 140 | if (bmi->bmiHeader.biBitCount > 8) return;
|
|---|
| 141 | colors = bmi->bmiHeader.biClrUsed;
|
|---|
| 142 | bitcount = bmi->bmiHeader.biBitCount;
|
|---|
| 143 | colorptr = (char*)bmi->bmiColors;
|
|---|
| 144 | incr = 4;
|
|---|
| 145 | }
|
|---|
| 146 | else
|
|---|
| 147 | if (bmi->bmiHeader.biSize == sizeof(BITMAPCOREHEADER))
|
|---|
| 148 | {
|
|---|
| 149 | BITMAPCOREHEADER *core = (BITMAPCOREHEADER *)bmi;
|
|---|
| 150 |
|
|---|
| 151 | if (core->bcBitCount > 8) return;
|
|---|
| 152 | colors = (1 << core->bcBitCount);
|
|---|
| 153 | bitcount = core->bcBitCount;
|
|---|
| 154 | colorptr = (char*)(core + 1);
|
|---|
| 155 | incr = 3;
|
|---|
| 156 | }
|
|---|
| 157 | else
|
|---|
| 158 | {
|
|---|
| 159 | dprintf(("Wrong bitmap header size!\n"));
|
|---|
| 160 | return;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | if (!colors && (bitcount <= 8))
|
|---|
| 164 | colors = 1 << bitcount;
|
|---|
| 165 |
|
|---|
| 166 | c_W = GetSysColor(COLOR_WINDOW);
|
|---|
| 167 | c_S = GetSysColor(COLOR_3DSHADOW);
|
|---|
| 168 | c_F = GetSysColor(COLOR_3DFACE);
|
|---|
| 169 | c_L = GetSysColor(COLOR_3DLIGHT);
|
|---|
| 170 | if (loadflags & LR_LOADTRANSPARENT) {
|
|---|
| 171 | switch (bitcount) {
|
|---|
| 172 | case 1: pix = pix >> 7; break;
|
|---|
| 173 | case 4: pix = pix >> 4; break;
|
|---|
| 174 | case 8: break;
|
|---|
| 175 | default:
|
|---|
| 176 | dprintf(("(%d): Unsupported depth\n", bitcount));
|
|---|
| 177 | return;
|
|---|
| 178 | }
|
|---|
| 179 | if (pix >= colors) {
|
|---|
| 180 | dprintf(("pixel has color index greater than biClrUsed!\n"));
|
|---|
| 181 | return;
|
|---|
| 182 | }
|
|---|
| 183 | if (loadflags & LR_LOADMAP3DCOLORS) c_W = c_F;
|
|---|
| 184 | ptr = (RGBQUAD*)((char*)colorptr+pix*incr);
|
|---|
| 185 |
|
|---|
| 186 | ptr->rgbBlue = GetBValue(c_W);
|
|---|
| 187 | ptr->rgbGreen = GetGValue(c_W);
|
|---|
| 188 | ptr->rgbRed = GetRValue(c_W);
|
|---|
| 189 | }
|
|---|
| 190 | if (loadflags & LR_LOADMAP3DCOLORS) {
|
|---|
| 191 | for (i=0; i<colors; i++) {
|
|---|
| 192 | ptr = (RGBQUAD*)(colorptr +i*incr);
|
|---|
| 193 | c_C = RGB(ptr->rgbRed, ptr->rgbGreen, ptr->rgbBlue);
|
|---|
| 194 | if (c_C == RGB(128, 128, 128)) {
|
|---|
| 195 | ptr->rgbRed = GetRValue(c_S);
|
|---|
| 196 | ptr->rgbGreen = GetGValue(c_S);
|
|---|
| 197 | ptr->rgbBlue = GetBValue(c_S);
|
|---|
| 198 | } else if (c_C == RGB(192, 192, 192)) {
|
|---|
| 199 | ptr->rgbRed = GetRValue(c_F);
|
|---|
| 200 | ptr->rgbGreen = GetGValue(c_F);
|
|---|
| 201 | ptr->rgbBlue = GetBValue(c_F);
|
|---|
| 202 | } else if (c_C == RGB(223, 223, 223)) {
|
|---|
| 203 | ptr->rgbRed = GetRValue(c_L);
|
|---|
| 204 | ptr->rgbGreen = GetGValue(c_L);
|
|---|
| 205 | ptr->rgbBlue = GetBValue(c_L);
|
|---|
| 206 | }
|
|---|
| 207 | }
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | /***********************************************************************
|
|---|
| 213 | * BITMAP_GetWidthBytes
|
|---|
| 214 | *
|
|---|
| 215 | * Return number of bytes taken by a scanline of 16-bit aligned Windows DDB
|
|---|
| 216 | * data.
|
|---|
| 217 | */
|
|---|
| 218 | int WIN32API BITMAP_GetWidthBytes( INT bmWidth, INT bpp )
|
|---|
| 219 | {
|
|---|
| 220 | switch(bpp)
|
|---|
| 221 | {
|
|---|
| 222 | case 1:
|
|---|
| 223 | return 2 * ((bmWidth+15) >> 4);
|
|---|
| 224 |
|
|---|
| 225 | case 24:
|
|---|
| 226 | bmWidth *= 3; /* fall through */
|
|---|
| 227 | case 8:
|
|---|
| 228 | return bmWidth + (bmWidth & 1);
|
|---|
| 229 |
|
|---|
| 230 | case 32:
|
|---|
| 231 | return bmWidth * 4;
|
|---|
| 232 |
|
|---|
| 233 | case 16:
|
|---|
| 234 | case 15:
|
|---|
| 235 | return bmWidth * 2;
|
|---|
| 236 |
|
|---|
| 237 | case 4:
|
|---|
| 238 | return 2 * ((bmWidth+3) >> 2);
|
|---|
| 239 |
|
|---|
| 240 | default:
|
|---|
| 241 | dprintf(("BITMAP_GetWidthBytes: Unknown depth %d, please report.\n", bpp ));
|
|---|
| 242 | }
|
|---|
| 243 | return -1;
|
|---|
| 244 | }
|
|---|