IMG_bmp.c
changeset 189 39d5ac4ec0f8
parent 186 5fbbeabf647f
child 202 8e1d88c68bda
     1.1 --- a/IMG_bmp.c	Sun Jan 04 02:43:39 2009 +0000
     1.2 +++ b/IMG_bmp.c	Tue Jan 13 01:21:00 2009 +0000
     1.3 @@ -21,6 +21,8 @@
     1.4  */
     1.5  
     1.6  /* This is a BMP image file loading framework */
     1.7 +/* ICO/CUR file support is here as well since it uses similar internal
     1.8 + * representation */
     1.9  
    1.10  #include <stdio.h>
    1.11  #include <string.h>
    1.12 @@ -49,6 +51,40 @@
    1.13  	return(is_BMP);
    1.14  }
    1.15  
    1.16 +static int IMG_isICOCUR(SDL_RWops *src, int type)
    1.17 +{
    1.18 +	int start;
    1.19 +	int is_ICOCUR;
    1.20 +
    1.21 +	/* The Win32 ICO file header (14 bytes) */
    1.22 +    Uint16 bfReserved;
    1.23 +    Uint16 bfType;
    1.24 +    Uint16 bfCount;
    1.25 +
    1.26 +	if ( !src )
    1.27 +		return 0;
    1.28 +	start = SDL_RWtell(src);
    1.29 +	is_ICOCUR = 0;
    1.30 +    bfReserved = SDL_ReadLE16(src);
    1.31 +    bfType = SDL_ReadLE16(src);
    1.32 +    bfCount = SDL_ReadLE16(src);
    1.33 +    if ((bfReserved == 0) && (bfType == type) && (bfCount != 0)) 
    1.34 +    	is_ICOCUR = 1;
    1.35 +	SDL_RWseek(src, start, SEEK_SET);
    1.36 +
    1.37 +	return (is_ICOCUR);
    1.38 +}
    1.39 +
    1.40 +int IMG_isICO(SDL_RWops *src)
    1.41 +{
    1.42 +	return IMG_isICOCUR(src, 1);
    1.43 +}
    1.44 +
    1.45 +int IMG_isCUR(SDL_RWops *src)
    1.46 +{
    1.47 +	return IMG_isICOCUR(src, 2);
    1.48 +}
    1.49 +
    1.50  #include "SDL_error.h"
    1.51  #include "SDL_video.h"
    1.52  #include "SDL_endian.h"
    1.53 @@ -448,12 +484,306 @@
    1.54  	return(surface);
    1.55  }
    1.56  
    1.57 +static Uint8
    1.58 +SDL_Read8(SDL_RWops * src)
    1.59 +{
    1.60 +    Uint8 value;
    1.61 +
    1.62 +    SDL_RWread(src, &value, 1, 1);
    1.63 +    return (value);
    1.64 +}
    1.65 +
    1.66 +static SDL_Surface *
    1.67 +LoadICOCUR_RW(SDL_RWops * src, int type, int freesrc)
    1.68 +{
    1.69 +    int was_error;
    1.70 +    long fp_offset;
    1.71 +    int bmpPitch;
    1.72 +    int i, pad;
    1.73 +    SDL_Surface *surface;
    1.74 +    Uint32 Rmask;
    1.75 +    Uint32 Gmask;
    1.76 +    Uint32 Bmask;
    1.77 +    Uint8 *bits;
    1.78 +    int ExpandBMP;
    1.79 +    int maxCol = 0;
    1.80 +    int icoOfs = 0;
    1.81 +    Uint32 palette[256];
    1.82 +
    1.83 +    /* The Win32 ICO file header (14 bytes) */
    1.84 +    Uint16 bfReserved;
    1.85 +    Uint16 bfType;
    1.86 +    Uint16 bfCount;
    1.87 +
    1.88 +    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
    1.89 +    Uint32 biSize;
    1.90 +    Sint32 biWidth;
    1.91 +    Sint32 biHeight;
    1.92 +    Uint16 biPlanes;
    1.93 +    Uint16 biBitCount;
    1.94 +    Uint32 biCompression;
    1.95 +    Uint32 biSizeImage;
    1.96 +    Sint32 biXPelsPerMeter;
    1.97 +    Sint32 biYPelsPerMeter;
    1.98 +    Uint32 biClrUsed;
    1.99 +    Uint32 biClrImportant;
   1.100 +
   1.101 +    /* Make sure we are passed a valid data source */
   1.102 +    surface = NULL;
   1.103 +    was_error = 0;
   1.104 +    if (src == NULL) {
   1.105 +        was_error = 1;
   1.106 +        goto done;
   1.107 +    }
   1.108 +
   1.109 +    /* Read in the ICO file header */
   1.110 +    fp_offset = SDL_RWtell(src);
   1.111 +    SDL_ClearError();
   1.112 +
   1.113 +    bfReserved = SDL_ReadLE16(src);
   1.114 +    bfType = SDL_ReadLE16(src);
   1.115 +    bfCount = SDL_ReadLE16(src);
   1.116 +    if ((bfReserved != 0) || (bfType != type) || (bfCount == 0)) {
   1.117 +        SDL_SetError("File is not a Windows %s file", type == 1 ? "ICO" : "CUR");
   1.118 +        was_error = 1;
   1.119 +        goto done;
   1.120 +    }
   1.121 +
   1.122 +    /* Read the Win32 Icon Directory */
   1.123 +    for (i = 0; i < bfCount; i++) {
   1.124 +        /* Icon Directory Entries */
   1.125 +        int bWidth = SDL_Read8(src);    /* Uint8, but 0 = 256 ! */
   1.126 +        int bHeight = SDL_Read8(src);   /* Uint8, but 0 = 256 ! */
   1.127 +        int bColorCount = SDL_Read8(src);       /* Uint8, but 0 = 256 ! */
   1.128 +        Uint8 bReserved = SDL_Read8(src);
   1.129 +        Uint16 wPlanes = SDL_ReadLE16(src);
   1.130 +        Uint16 wBitCount = SDL_ReadLE16(src);
   1.131 +        Uint32 dwBytesInRes = SDL_ReadLE32(src);
   1.132 +        Uint32 dwImageOffset = SDL_ReadLE32(src);
   1.133 +
   1.134 +        if (!bWidth)
   1.135 +            bWidth = 256;
   1.136 +        if (!bHeight)
   1.137 +            bHeight = 256;
   1.138 +        if (!bColorCount)
   1.139 +            bColorCount = 256;
   1.140 +
   1.141 +        //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
   1.142 +        if (bColorCount > maxCol) {
   1.143 +            maxCol = bColorCount;
   1.144 +            icoOfs = dwImageOffset;
   1.145 +            //printf("marked\n");
   1.146 +        }
   1.147 +    }
   1.148 +
   1.149 +    /* Advance to the DIB Data */
   1.150 +    if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
   1.151 +        SDL_Error(SDL_EFSEEK);
   1.152 +        was_error = 1;
   1.153 +        goto done;
   1.154 +    }
   1.155 +
   1.156 +    /* Read the Win32 BITMAPINFOHEADER */
   1.157 +    biSize = SDL_ReadLE32(src);
   1.158 +    if (biSize == 40) {
   1.159 +        biWidth = SDL_ReadLE32(src);
   1.160 +        biHeight = SDL_ReadLE32(src);
   1.161 +        biPlanes = SDL_ReadLE16(src);
   1.162 +        biBitCount = SDL_ReadLE16(src);
   1.163 +        biCompression = SDL_ReadLE32(src);
   1.164 +        biSizeImage = SDL_ReadLE32(src);
   1.165 +        biXPelsPerMeter = SDL_ReadLE32(src);
   1.166 +        biYPelsPerMeter = SDL_ReadLE32(src);
   1.167 +        biClrUsed = SDL_ReadLE32(src);
   1.168 +        biClrImportant = SDL_ReadLE32(src);
   1.169 +    } else {
   1.170 +        SDL_SetError("Unsupported ICO bitmap format");
   1.171 +        was_error = 1;
   1.172 +        goto done;
   1.173 +    }
   1.174 +
   1.175 +    /* Check for read error */
   1.176 +    if (SDL_strcmp(SDL_GetError(), "") != 0) {
   1.177 +        was_error = 1;
   1.178 +        goto done;
   1.179 +    }
   1.180 +
   1.181 +    /* We don't support any BMP compression right now */
   1.182 +    switch (biCompression) {
   1.183 +    case BI_RGB:
   1.184 +        /* Default values for the BMP format */
   1.185 +        switch (biBitCount) {
   1.186 +        case 1:
   1.187 +        case 4:
   1.188 +            ExpandBMP = biBitCount;
   1.189 +            biBitCount = 8;
   1.190 +            break;
   1.191 +        case 8:
   1.192 +            ExpandBMP = 8;
   1.193 +            break;
   1.194 +        case 32:
   1.195 +            Rmask = 0x00FF0000;
   1.196 +            Gmask = 0x0000FF00;
   1.197 +            Bmask = 0x000000FF;
   1.198 +            ExpandBMP = 0;
   1.199 +            break;
   1.200 +        default:
   1.201 +            SDL_SetError("ICO file with unsupported bit count");
   1.202 +            was_error = 1;
   1.203 +            goto done;
   1.204 +        }
   1.205 +        break;
   1.206 +    default:
   1.207 +        SDL_SetError("Compressed ICO files not supported");
   1.208 +        was_error = 1;
   1.209 +        goto done;
   1.210 +    }
   1.211 +
   1.212 +    /* Create a RGBA surface */
   1.213 +    biHeight = biHeight >> 1;
   1.214 +    //printf("%d x %d\n", biWidth, biHeight);
   1.215 +    surface =
   1.216 +        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
   1.217 +                             0x0000FF00, 0x000000FF, 0xFF000000);
   1.218 +    if (surface == NULL) {
   1.219 +        was_error = 1;
   1.220 +        goto done;
   1.221 +    }
   1.222 +
   1.223 +    /* Load the palette, if any */
   1.224 +    //printf("bc %d bused %d\n", biBitCount, biClrUsed);
   1.225 +    if (biBitCount <= 8) {
   1.226 +        if (biClrUsed == 0) {
   1.227 +            biClrUsed = 1 << biBitCount;
   1.228 +        }
   1.229 +        for (i = 0; i < (int) biClrUsed; ++i) {
   1.230 +            SDL_RWread(src, &palette[i], 4, 1);
   1.231 +        }
   1.232 +    }
   1.233 +
   1.234 +    /* Read the surface pixels.  Note that the bmp image is upside down */
   1.235 +    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.236 +    switch (ExpandBMP) {
   1.237 +    case 1:
   1.238 +        bmpPitch = (biWidth + 7) >> 3;
   1.239 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.240 +        break;
   1.241 +    case 4:
   1.242 +        bmpPitch = (biWidth + 1) >> 1;
   1.243 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.244 +        break;
   1.245 +    case 8:
   1.246 +        bmpPitch = biWidth;
   1.247 +        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.248 +        break;
   1.249 +    default:
   1.250 +        bmpPitch = biWidth * 4;
   1.251 +        pad = 0;
   1.252 +        break;
   1.253 +    }
   1.254 +    while (bits > (Uint8 *) surface->pixels) {
   1.255 +        bits -= surface->pitch;
   1.256 +        switch (ExpandBMP) {
   1.257 +        case 1:
   1.258 +        case 4:
   1.259 +        case 8:
   1.260 +            {
   1.261 +                Uint8 pixel = 0;
   1.262 +                int shift = (8 - ExpandBMP);
   1.263 +                for (i = 0; i < surface->w; ++i) {
   1.264 +                    if (i % (8 / ExpandBMP) == 0) {
   1.265 +                        if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.266 +                            SDL_SetError("Error reading from ICO");
   1.267 +                            was_error = 1;
   1.268 +                            goto done;
   1.269 +                        }
   1.270 +                    }
   1.271 +                    *((Uint32 *) bits + i) = (palette[pixel >> shift]);
   1.272 +                    pixel <<= ExpandBMP;
   1.273 +                }
   1.274 +            }
   1.275 +            break;
   1.276 +
   1.277 +        default:
   1.278 +            if (SDL_RWread(src, bits, 1, surface->pitch)
   1.279 +                != surface->pitch) {
   1.280 +                SDL_Error(SDL_EFREAD);
   1.281 +                was_error = 1;
   1.282 +                goto done;
   1.283 +            }
   1.284 +            break;
   1.285 +        }
   1.286 +        /* Skip padding bytes, ugh */
   1.287 +        if (pad) {
   1.288 +            Uint8 padbyte;
   1.289 +            for (i = 0; i < pad; ++i) {
   1.290 +                SDL_RWread(src, &padbyte, 1, 1);
   1.291 +            }
   1.292 +        }
   1.293 +    }
   1.294 +    /* Read the mask pixels.  Note that the bmp image is upside down */
   1.295 +    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.296 +    ExpandBMP = 1;
   1.297 +    bmpPitch = (biWidth + 7) >> 3;
   1.298 +    pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.299 +    while (bits > (Uint8 *) surface->pixels) {
   1.300 +        Uint8 pixel = 0;
   1.301 +        int shift = (8 - ExpandBMP);
   1.302 +
   1.303 +        bits -= surface->pitch;
   1.304 +        for (i = 0; i < surface->w; ++i) {
   1.305 +            if (i % (8 / ExpandBMP) == 0) {
   1.306 +                if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.307 +                    SDL_SetError("Error reading from ICO");
   1.308 +                    was_error = 1;
   1.309 +                    goto done;
   1.310 +                }
   1.311 +            }
   1.312 +            *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
   1.313 +            pixel <<= ExpandBMP;
   1.314 +        }
   1.315 +        /* Skip padding bytes, ugh */
   1.316 +        if (pad) {
   1.317 +            Uint8 padbyte;
   1.318 +            for (i = 0; i < pad; ++i) {
   1.319 +                SDL_RWread(src, &padbyte, 1, 1);
   1.320 +            }
   1.321 +        }
   1.322 +    }
   1.323 +  done:
   1.324 +    if (was_error) {
   1.325 +        if (src) {
   1.326 +            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
   1.327 +        }
   1.328 +        if (surface) {
   1.329 +            SDL_FreeSurface(surface);
   1.330 +        }
   1.331 +        surface = NULL;
   1.332 +    }
   1.333 +    if (freesrc && src) {
   1.334 +        SDL_RWclose(src);
   1.335 +    }
   1.336 +    return (surface);
   1.337 +}
   1.338 +
   1.339  /* Load a BMP type image from an SDL datasource */
   1.340  SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
   1.341  {
   1.342  	return(LoadBMP_RW(src, 0));
   1.343  }
   1.344  
   1.345 +/* Load a ICO type image from an SDL datasource */
   1.346 +SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
   1.347 +{
   1.348 +	return(LoadICOCUR_RW(src, 1, 0));
   1.349 +}
   1.350 +
   1.351 +/* Load a CUR type image from an SDL datasource */
   1.352 +SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
   1.353 +{
   1.354 +	return(LoadICOCUR_RW(src, 2, 0));
   1.355 +}
   1.356 +
   1.357  #else
   1.358  
   1.359  /* See if an image is contained in a data source */
   1.360 @@ -462,10 +792,32 @@
   1.361  	return(0);
   1.362  }
   1.363  
   1.364 +int IMG_isICO(SDL_RWops *src)
   1.365 +{
   1.366 +	return(0);
   1.367 +}
   1.368 +
   1.369 +int IMG_isCUR(SDL_RWops *src)
   1.370 +{
   1.371 +	return(0);
   1.372 +}
   1.373 +
   1.374  /* Load a BMP type image from an SDL datasource */
   1.375  SDL_Surface *IMG_LoadBMP_RW(SDL_RWops *src)
   1.376  {
   1.377  	return(NULL);
   1.378  }
   1.379  
   1.380 +/* Load a BMP type image from an SDL datasource */
   1.381 +SDL_Surface *IMG_LoadCUR_RW(SDL_RWops *src)
   1.382 +{
   1.383 +	return(NULL);
   1.384 +}
   1.385 +
   1.386 +/* Load a BMP type image from an SDL datasource */
   1.387 +SDL_Surface *IMG_LoadICO_RW(SDL_RWops *src)
   1.388 +{
   1.389 +	return(NULL);
   1.390 +}
   1.391 +
   1.392  #endif /* LOAD_BMP */