src/video/SDL_bmp.c
changeset 3026 69ab1117dd3b
parent 3024 1a08749aebce
child 3310 b907e83deb88
     1.1 --- a/src/video/SDL_bmp.c	Mon Jan 12 06:19:05 2009 +0000
     1.2 +++ b/src/video/SDL_bmp.c	Mon Jan 12 06:21:34 2009 +0000
     1.3 @@ -46,288 +46,6 @@
     1.4  #endif
     1.5  
     1.6  
     1.7 -static Uint8
     1.8 -SDL_Read8(SDL_RWops * src)
     1.9 -{
    1.10 -    Uint8 value;
    1.11 -
    1.12 -    SDL_RWread(src, &value, 1, 1);
    1.13 -    return (value);
    1.14 -}
    1.15 -
    1.16 -SDL_Surface *
    1.17 -SDL_LoadICO_RW(SDL_RWops * src, int freesrc)
    1.18 -{
    1.19 -    int was_error;
    1.20 -    long fp_offset;
    1.21 -    int bmpPitch;
    1.22 -    int i, pad;
    1.23 -    SDL_Surface *surface;
    1.24 -    Uint32 Rmask;
    1.25 -    Uint32 Gmask;
    1.26 -    Uint32 Bmask;
    1.27 -    Uint8 *bits;
    1.28 -    int ExpandBMP;
    1.29 -    int maxCol = 0;
    1.30 -    int icoOfs = 0;
    1.31 -    Uint32 palette[256];
    1.32 -
    1.33 -    /* The Win32 ICO file header (14 bytes) */
    1.34 -    Uint16 bfReserved;
    1.35 -    Uint16 bfType;
    1.36 -    Uint16 bfCount;
    1.37 -
    1.38 -    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
    1.39 -    Uint32 biSize;
    1.40 -    Sint32 biWidth;
    1.41 -    Sint32 biHeight;
    1.42 -    Uint16 biPlanes;
    1.43 -    Uint16 biBitCount;
    1.44 -    Uint32 biCompression;
    1.45 -    Uint32 biSizeImage;
    1.46 -    Sint32 biXPelsPerMeter;
    1.47 -    Sint32 biYPelsPerMeter;
    1.48 -    Uint32 biClrUsed;
    1.49 -    Uint32 biClrImportant;
    1.50 -
    1.51 -    /* Make sure we are passed a valid data source */
    1.52 -    surface = NULL;
    1.53 -    was_error = 0;
    1.54 -    if (src == NULL) {
    1.55 -        was_error = 1;
    1.56 -        goto done;
    1.57 -    }
    1.58 -
    1.59 -    /* Read in the ICO file header */
    1.60 -    fp_offset = SDL_RWtell(src);
    1.61 -    SDL_ClearError();
    1.62 -
    1.63 -    bfReserved = SDL_ReadLE16(src);
    1.64 -    bfType = SDL_ReadLE16(src);
    1.65 -    bfCount = SDL_ReadLE16(src);
    1.66 -    if ((bfType != 1 && bfType != 2) || (bfCount == 0)) {
    1.67 -        SDL_SetError("File is not a Windows ICO file");
    1.68 -        was_error = 1;
    1.69 -        goto done;
    1.70 -    }
    1.71 -
    1.72 -    /* Read the Win32 Icon Directory */
    1.73 -    for (i = 0; i < bfCount; i++) {
    1.74 -        /* Icon Directory Entries */
    1.75 -        int bWidth = SDL_Read8(src);    /* Uint8, but 0 = 256 ! */
    1.76 -        int bHeight = SDL_Read8(src);   /* Uint8, but 0 = 256 ! */
    1.77 -        int bColorCount = SDL_Read8(src);       /* Uint8, but 0 = 256 ! */
    1.78 -        Uint8 bReserved = SDL_Read8(src);
    1.79 -        Uint16 wPlanes = SDL_ReadLE16(src);
    1.80 -        Uint16 wBitCount = SDL_ReadLE16(src);
    1.81 -        Uint32 dwBytesInRes = SDL_ReadLE32(src);
    1.82 -        Uint32 dwImageOffset = SDL_ReadLE32(src);
    1.83 -
    1.84 -        if (!bWidth)
    1.85 -            bWidth = 256;
    1.86 -        if (!bHeight)
    1.87 -            bHeight = 256;
    1.88 -        if (!bColorCount)
    1.89 -            bColorCount = 256;
    1.90 -
    1.91 -        //printf("%dx%d@%d - %08x\n", bWidth, bHeight, bColorCount, dwImageOffset);
    1.92 -        if (bColorCount > maxCol) {
    1.93 -            maxCol = bColorCount;
    1.94 -            icoOfs = dwImageOffset;
    1.95 -            //printf("marked\n");
    1.96 -        }
    1.97 -    }
    1.98 -
    1.99 -    /* Advance to the DIB Data */
   1.100 -    if (SDL_RWseek(src, icoOfs, RW_SEEK_SET) < 0) {
   1.101 -        SDL_Error(SDL_EFSEEK);
   1.102 -        was_error = 1;
   1.103 -        goto done;
   1.104 -    }
   1.105 -
   1.106 -    /* Read the Win32 BITMAPINFOHEADER */
   1.107 -    biSize = SDL_ReadLE32(src);
   1.108 -    if (biSize == 40) {
   1.109 -        biWidth = SDL_ReadLE32(src);
   1.110 -        biHeight = SDL_ReadLE32(src);
   1.111 -        biPlanes = SDL_ReadLE16(src);
   1.112 -        biBitCount = SDL_ReadLE16(src);
   1.113 -        biCompression = SDL_ReadLE32(src);
   1.114 -        biSizeImage = SDL_ReadLE32(src);
   1.115 -        biXPelsPerMeter = SDL_ReadLE32(src);
   1.116 -        biYPelsPerMeter = SDL_ReadLE32(src);
   1.117 -        biClrUsed = SDL_ReadLE32(src);
   1.118 -        biClrImportant = SDL_ReadLE32(src);
   1.119 -    } else {
   1.120 -        SDL_SetError("Unsupported ICO bitmap format");
   1.121 -        was_error = 1;
   1.122 -        goto done;
   1.123 -    }
   1.124 -
   1.125 -    /* Check for read error */
   1.126 -    if (SDL_strcmp(SDL_GetError(), "") != 0) {
   1.127 -        was_error = 1;
   1.128 -        goto done;
   1.129 -    }
   1.130 -
   1.131 -    /* We don't support any BMP compression right now */
   1.132 -    switch (biCompression) {
   1.133 -    case BI_RGB:
   1.134 -        /* Default values for the BMP format */
   1.135 -        switch (biBitCount) {
   1.136 -        case 1:
   1.137 -        case 4:
   1.138 -            ExpandBMP = biBitCount;
   1.139 -            biBitCount = 8;
   1.140 -            break;
   1.141 -        case 8:
   1.142 -            ExpandBMP = 8;
   1.143 -            break;
   1.144 -        case 32:
   1.145 -            Rmask = 0x00FF0000;
   1.146 -            Gmask = 0x0000FF00;
   1.147 -            Bmask = 0x000000FF;
   1.148 -            ExpandBMP = 0;
   1.149 -            break;
   1.150 -        default:
   1.151 -            SDL_SetError("ICO file with unsupported bit count");
   1.152 -            was_error = 1;
   1.153 -            goto done;
   1.154 -        }
   1.155 -        break;
   1.156 -    default:
   1.157 -        SDL_SetError("Compressed ICO files not supported");
   1.158 -        was_error = 1;
   1.159 -        goto done;
   1.160 -    }
   1.161 -
   1.162 -    /* Create a RGBA surface */
   1.163 -    biHeight = biHeight >> 1;
   1.164 -    //printf("%d x %d\n", biWidth, biHeight);
   1.165 -    surface =
   1.166 -        SDL_CreateRGBSurface(0, biWidth, biHeight, 32, 0x00FF0000,
   1.167 -                             0x0000FF00, 0x000000FF, 0xFF000000);
   1.168 -    if (surface == NULL) {
   1.169 -        was_error = 1;
   1.170 -        goto done;
   1.171 -    }
   1.172 -
   1.173 -    /* Load the palette, if any */
   1.174 -    //printf("bc %d bused %d\n", biBitCount, biClrUsed);
   1.175 -    if (biBitCount <= 8) {
   1.176 -        if (biClrUsed == 0) {
   1.177 -            biClrUsed = 1 << biBitCount;
   1.178 -        }
   1.179 -        for (i = 0; i < (int) biClrUsed; ++i) {
   1.180 -            SDL_RWread(src, &palette[i], 4, 1);
   1.181 -        }
   1.182 -    }
   1.183 -
   1.184 -    /* Read the surface pixels.  Note that the bmp image is upside down */
   1.185 -    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.186 -    switch (ExpandBMP) {
   1.187 -    case 1:
   1.188 -        bmpPitch = (biWidth + 7) >> 3;
   1.189 -        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.190 -        break;
   1.191 -    case 4:
   1.192 -        bmpPitch = (biWidth + 1) >> 1;
   1.193 -        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.194 -        break;
   1.195 -    case 8:
   1.196 -        bmpPitch = biWidth;
   1.197 -        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.198 -        break;
   1.199 -    default:
   1.200 -        bmpPitch = biWidth * 4;
   1.201 -        pad = 0;
   1.202 -        break;
   1.203 -    }
   1.204 -    while (bits > (Uint8 *) surface->pixels) {
   1.205 -        bits -= surface->pitch;
   1.206 -        switch (ExpandBMP) {
   1.207 -        case 1:
   1.208 -        case 4:
   1.209 -        case 8:
   1.210 -            {
   1.211 -                Uint8 pixel = 0;
   1.212 -                int shift = (8 - ExpandBMP);
   1.213 -                for (i = 0; i < surface->w; ++i) {
   1.214 -                    if (i % (8 / ExpandBMP) == 0) {
   1.215 -                        if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.216 -                            SDL_SetError("Error reading from ICO");
   1.217 -                            was_error = 1;
   1.218 -                            goto done;
   1.219 -                        }
   1.220 -                    }
   1.221 -                    *((Uint32 *) bits + i) = (palette[pixel >> shift]);
   1.222 -                    pixel <<= ExpandBMP;
   1.223 -                }
   1.224 -            }
   1.225 -            break;
   1.226 -
   1.227 -        default:
   1.228 -            if (SDL_RWread(src, bits, 1, surface->pitch)
   1.229 -                != surface->pitch) {
   1.230 -                SDL_Error(SDL_EFREAD);
   1.231 -                was_error = 1;
   1.232 -                goto done;
   1.233 -            }
   1.234 -            break;
   1.235 -        }
   1.236 -        /* Skip padding bytes, ugh */
   1.237 -        if (pad) {
   1.238 -            Uint8 padbyte;
   1.239 -            for (i = 0; i < pad; ++i) {
   1.240 -                SDL_RWread(src, &padbyte, 1, 1);
   1.241 -            }
   1.242 -        }
   1.243 -    }
   1.244 -    /* Read the mask pixels.  Note that the bmp image is upside down */
   1.245 -    bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
   1.246 -    ExpandBMP = 1;
   1.247 -    bmpPitch = (biWidth + 7) >> 3;
   1.248 -    pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
   1.249 -    while (bits > (Uint8 *) surface->pixels) {
   1.250 -        Uint8 pixel = 0;
   1.251 -        int shift = (8 - ExpandBMP);
   1.252 -
   1.253 -        bits -= surface->pitch;
   1.254 -        for (i = 0; i < surface->w; ++i) {
   1.255 -            if (i % (8 / ExpandBMP) == 0) {
   1.256 -                if (!SDL_RWread(src, &pixel, 1, 1)) {
   1.257 -                    SDL_SetError("Error reading from ICO");
   1.258 -                    was_error = 1;
   1.259 -                    goto done;
   1.260 -                }
   1.261 -            }
   1.262 -            *((Uint32 *) bits + i) |= ((pixel >> shift) ? 0 : 0xFF000000);
   1.263 -            pixel <<= ExpandBMP;
   1.264 -        }
   1.265 -        /* Skip padding bytes, ugh */
   1.266 -        if (pad) {
   1.267 -            Uint8 padbyte;
   1.268 -            for (i = 0; i < pad; ++i) {
   1.269 -                SDL_RWread(src, &padbyte, 1, 1);
   1.270 -            }
   1.271 -        }
   1.272 -    }
   1.273 -  done:
   1.274 -    if (was_error) {
   1.275 -        if (src) {
   1.276 -            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
   1.277 -        }
   1.278 -        if (surface) {
   1.279 -            SDL_FreeSurface(surface);
   1.280 -        }
   1.281 -        surface = NULL;
   1.282 -    }
   1.283 -    if (freesrc && src) {
   1.284 -        SDL_RWclose(src);
   1.285 -    }
   1.286 -    return (surface);
   1.287 -}
   1.288 -
   1.289  SDL_Surface *
   1.290  SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
   1.291  {