src/video/SDL_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Dec 2009 08:11:06 +0000
changeset 3565 f43c8f688f77
parent 3497 74d2f44a85de
child 3697 f7b03b6838cb
permissions -rw-r--r--
Fixed bug #906

Added better error reporting for OpenGL context creation failing.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* 
slouken@0
    25
   Code to load and save surfaces in Windows BMP format.
slouken@0
    26
slouken@0
    27
   Why support BMP format?  Well, it's a native format for Windows, and
slouken@0
    28
   most image processing programs can read and write it.  It would be nice
slouken@0
    29
   to be able to have at least one image format that we can natively load
slouken@0
    30
   and save, and since PNG is so complex that it would bloat the library,
slouken@0
    31
   BMP is a good alternative. 
slouken@0
    32
slouken@0
    33
   This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
slouken@0
    34
*/
slouken@0
    35
slouken@0
    36
#include "SDL_video.h"
slouken@0
    37
#include "SDL_endian.h"
slouken@2823
    38
#include "SDL_pixels_c.h"
slouken@0
    39
slouken@0
    40
/* Compression encodings for BMP files */
slouken@0
    41
#ifndef BI_RGB
slouken@0
    42
#define BI_RGB		0
slouken@0
    43
#define BI_RLE8		1
slouken@0
    44
#define BI_RLE4		2
slouken@0
    45
#define BI_BITFIELDS	3
slouken@0
    46
#endif
slouken@0
    47
slouken@0
    48
slouken@1895
    49
SDL_Surface *
slouken@1895
    50
SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
slouken@0
    51
{
slouken@3310
    52
    SDL_bool was_error;
slouken@1895
    53
    long fp_offset;
slouken@1895
    54
    int bmpPitch;
slouken@1895
    55
    int i, pad;
slouken@1895
    56
    SDL_Surface *surface;
slouken@1895
    57
    Uint32 Rmask;
slouken@1895
    58
    Uint32 Gmask;
slouken@1895
    59
    Uint32 Bmask;
slouken@3497
    60
    Uint32 Amask;
slouken@1895
    61
    SDL_Palette *palette;
slouken@1895
    62
    Uint8 *bits;
slouken@3310
    63
    Uint8 *top, *end;
slouken@3310
    64
    SDL_bool topDown;
slouken@1895
    65
    int ExpandBMP;
slouken@0
    66
slouken@1895
    67
    /* The Win32 BMP file header (14 bytes) */
slouken@1895
    68
    char magic[2];
slouken@1895
    69
    Uint32 bfSize;
slouken@1895
    70
    Uint16 bfReserved1;
slouken@1895
    71
    Uint16 bfReserved2;
slouken@1895
    72
    Uint32 bfOffBits;
slouken@0
    73
slouken@1895
    74
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@1895
    75
    Uint32 biSize;
slouken@1895
    76
    Sint32 biWidth;
slouken@1895
    77
    Sint32 biHeight;
slouken@1895
    78
    Uint16 biPlanes;
slouken@1895
    79
    Uint16 biBitCount;
slouken@1895
    80
    Uint32 biCompression;
slouken@1895
    81
    Uint32 biSizeImage;
slouken@1895
    82
    Sint32 biXPelsPerMeter;
slouken@1895
    83
    Sint32 biYPelsPerMeter;
slouken@1895
    84
    Uint32 biClrUsed;
slouken@1895
    85
    Uint32 biClrImportant;
slouken@0
    86
slouken@1895
    87
    /* Make sure we are passed a valid data source */
slouken@1895
    88
    surface = NULL;
slouken@3310
    89
    was_error = SDL_FALSE;
slouken@1895
    90
    if (src == NULL) {
slouken@3310
    91
        was_error = SDL_TRUE;
slouken@1895
    92
        goto done;
slouken@1895
    93
    }
slouken@0
    94
slouken@1895
    95
    /* Read in the BMP file header */
slouken@1895
    96
    fp_offset = SDL_RWtell(src);
slouken@1895
    97
    SDL_ClearError();
slouken@1895
    98
    if (SDL_RWread(src, magic, 1, 2) != 2) {
slouken@1895
    99
        SDL_Error(SDL_EFREAD);
slouken@3310
   100
        was_error = SDL_TRUE;
slouken@1895
   101
        goto done;
slouken@1895
   102
    }
slouken@1895
   103
    if (SDL_strncmp(magic, "BM", 2) != 0) {
slouken@1895
   104
        SDL_SetError("File is not a Windows BMP file");
slouken@3310
   105
        was_error = SDL_TRUE;
slouken@1895
   106
        goto done;
slouken@1895
   107
    }
slouken@1895
   108
    bfSize = SDL_ReadLE32(src);
slouken@1895
   109
    bfReserved1 = SDL_ReadLE16(src);
slouken@1895
   110
    bfReserved2 = SDL_ReadLE16(src);
slouken@1895
   111
    bfOffBits = SDL_ReadLE32(src);
slouken@0
   112
slouken@1895
   113
    /* Read the Win32 BITMAPINFOHEADER */
slouken@1895
   114
    biSize = SDL_ReadLE32(src);
slouken@1895
   115
    if (biSize == 12) {
slouken@1895
   116
        biWidth = (Uint32) SDL_ReadLE16(src);
slouken@1895
   117
        biHeight = (Uint32) SDL_ReadLE16(src);
slouken@1895
   118
        biPlanes = SDL_ReadLE16(src);
slouken@1895
   119
        biBitCount = SDL_ReadLE16(src);
slouken@1895
   120
        biCompression = BI_RGB;
slouken@1895
   121
        biSizeImage = 0;
slouken@1895
   122
        biXPelsPerMeter = 0;
slouken@1895
   123
        biYPelsPerMeter = 0;
slouken@1895
   124
        biClrUsed = 0;
slouken@1895
   125
        biClrImportant = 0;
slouken@1895
   126
    } else {
slouken@1895
   127
        biWidth = SDL_ReadLE32(src);
slouken@1895
   128
        biHeight = SDL_ReadLE32(src);
slouken@1895
   129
        biPlanes = SDL_ReadLE16(src);
slouken@1895
   130
        biBitCount = SDL_ReadLE16(src);
slouken@1895
   131
        biCompression = SDL_ReadLE32(src);
slouken@1895
   132
        biSizeImage = SDL_ReadLE32(src);
slouken@1895
   133
        biXPelsPerMeter = SDL_ReadLE32(src);
slouken@1895
   134
        biYPelsPerMeter = SDL_ReadLE32(src);
slouken@1895
   135
        biClrUsed = SDL_ReadLE32(src);
slouken@1895
   136
        biClrImportant = SDL_ReadLE32(src);
slouken@1895
   137
    }
slouken@3310
   138
    if (biHeight < 0) {
slouken@3310
   139
        topDown = SDL_TRUE;
slouken@3310
   140
        biHeight = -biHeight;
slouken@3310
   141
    } else {
slouken@3310
   142
        topDown = SDL_FALSE;
slouken@3310
   143
    }
slouken@0
   144
slouken@1895
   145
    /* Check for read error */
slouken@1895
   146
    if (SDL_strcmp(SDL_GetError(), "") != 0) {
slouken@3310
   147
        was_error = SDL_TRUE;
slouken@1895
   148
        goto done;
slouken@1895
   149
    }
slouken@0
   150
slouken@1895
   151
    /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@1895
   152
    switch (biBitCount) {
slouken@1895
   153
    case 1:
slouken@1895
   154
    case 4:
slouken@1895
   155
        ExpandBMP = biBitCount;
slouken@1895
   156
        biBitCount = 8;
slouken@1895
   157
        break;
slouken@1895
   158
    default:
slouken@1895
   159
        ExpandBMP = 0;
slouken@1895
   160
        break;
slouken@1895
   161
    }
slouken@0
   162
slouken@1895
   163
    /* We don't support any BMP compression right now */
slouken@3497
   164
    Rmask = Gmask = Bmask = Amask = 0;
slouken@1895
   165
    switch (biCompression) {
slouken@1895
   166
    case BI_RGB:
slouken@1895
   167
        /* If there are no masks, use the defaults */
slouken@1895
   168
        if (bfOffBits == (14 + biSize)) {
slouken@1895
   169
            /* Default values for the BMP format */
slouken@1895
   170
            switch (biBitCount) {
slouken@1895
   171
            case 15:
slouken@1895
   172
            case 16:
slouken@1895
   173
                Rmask = 0x7C00;
slouken@1895
   174
                Gmask = 0x03E0;
slouken@1895
   175
                Bmask = 0x001F;
slouken@1895
   176
                break;
slouken@1895
   177
            case 24:
slouken@0
   178
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
   179
                Rmask = 0x000000FF;
slouken@1895
   180
                Gmask = 0x0000FF00;
slouken@1895
   181
                Bmask = 0x00FF0000;
slouken@3497
   182
#else
slouken@3497
   183
                Rmask = 0x00FF0000;
slouken@3497
   184
                Gmask = 0x0000FF00;
slouken@3497
   185
                Bmask = 0x000000FF;
slouken@3497
   186
#endif
slouken@1895
   187
                break;
slouken@1895
   188
            case 32:
slouken@3497
   189
                Amask = 0xFF000000;
slouken@1895
   190
                Rmask = 0x00FF0000;
slouken@1895
   191
                Gmask = 0x0000FF00;
slouken@1895
   192
                Bmask = 0x000000FF;
slouken@1895
   193
                break;
slouken@1895
   194
            default:
slouken@1895
   195
                break;
slouken@1895
   196
            }
slouken@1895
   197
            break;
slouken@1895
   198
        }
slouken@1895
   199
        /* Fall through -- read the RGB masks */
slouken@0
   200
slouken@1895
   201
    case BI_BITFIELDS:
slouken@1895
   202
        switch (biBitCount) {
slouken@1895
   203
        case 15:
slouken@1895
   204
        case 16:
slouken@3497
   205
            Rmask = SDL_ReadLE32(src);
slouken@3497
   206
            Gmask = SDL_ReadLE32(src);
slouken@3497
   207
            Bmask = SDL_ReadLE32(src);
slouken@3497
   208
            break;
slouken@1895
   209
        case 32:
slouken@1895
   210
            Rmask = SDL_ReadLE32(src);
slouken@1895
   211
            Gmask = SDL_ReadLE32(src);
slouken@1895
   212
            Bmask = SDL_ReadLE32(src);
slouken@3497
   213
            Amask = SDL_ReadLE32(src);
slouken@1895
   214
            break;
slouken@1895
   215
        default:
slouken@1895
   216
            break;
slouken@1895
   217
        }
slouken@1895
   218
        break;
slouken@1895
   219
    default:
slouken@1895
   220
        SDL_SetError("Compressed BMP files not supported");
slouken@3310
   221
        was_error = SDL_TRUE;
slouken@1895
   222
        goto done;
slouken@1895
   223
    }
slouken@0
   224
slouken@1895
   225
    /* Create a compatible surface, note that the colors are RGB ordered */
slouken@1895
   226
    surface =
slouken@1895
   227
        SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
slouken@3497
   228
                             Bmask, Amask);
slouken@1895
   229
    if (surface == NULL) {
slouken@3310
   230
        was_error = SDL_TRUE;
slouken@1895
   231
        goto done;
slouken@1895
   232
    }
slouken@0
   233
slouken@1895
   234
    /* Load the palette, if any */
slouken@1895
   235
    palette = (surface->format)->palette;
slouken@1895
   236
    if (palette) {
slouken@1895
   237
        if (biClrUsed == 0) {
slouken@1895
   238
            biClrUsed = 1 << biBitCount;
slouken@1895
   239
        }
slouken@2920
   240
        if ((int) biClrUsed > palette->ncolors) {
slouken@2851
   241
            palette->ncolors = biClrUsed;
slouken@2851
   242
            palette->colors =
slouken@2851
   243
                (SDL_Color *) SDL_realloc(palette->colors,
slouken@2851
   244
                                          palette->ncolors *
slouken@2851
   245
                                          sizeof(*palette->colors));
slouken@2851
   246
            if (!palette->colors) {
slouken@2851
   247
                SDL_OutOfMemory();
slouken@3310
   248
                was_error = SDL_TRUE;
slouken@2851
   249
                goto done;
slouken@2851
   250
            }
slouken@2920
   251
        } else if ((int) biClrUsed < palette->ncolors) {
slouken@2851
   252
            palette->ncolors = biClrUsed;
slouken@2851
   253
        }
slouken@1895
   254
        if (biSize == 12) {
slouken@1895
   255
            for (i = 0; i < (int) biClrUsed; ++i) {
slouken@1895
   256
                SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@1895
   257
                SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@1895
   258
                SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@1895
   259
                palette->colors[i].unused = SDL_ALPHA_OPAQUE;
slouken@1895
   260
            }
slouken@1895
   261
        } else {
slouken@1895
   262
            for (i = 0; i < (int) biClrUsed; ++i) {
slouken@1895
   263
                SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@1895
   264
                SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@1895
   265
                SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@1895
   266
                SDL_RWread(src, &palette->colors[i].unused, 1, 1);
slouken@1895
   267
            }
slouken@1895
   268
        }
slouken@1895
   269
    }
slouken@0
   270
slouken@1895
   271
    /* Read the surface pixels.  Note that the bmp image is upside down */
slouken@1895
   272
    if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
slouken@1895
   273
        SDL_Error(SDL_EFSEEK);
slouken@3310
   274
        was_error = SDL_TRUE;
slouken@1895
   275
        goto done;
slouken@1895
   276
    }
slouken@3310
   277
    top = (Uint8 *)surface->pixels;
slouken@3310
   278
    end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@1895
   279
    switch (ExpandBMP) {
slouken@1895
   280
    case 1:
slouken@1895
   281
        bmpPitch = (biWidth + 7) >> 3;
slouken@1895
   282
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
slouken@1895
   283
        break;
slouken@1895
   284
    case 4:
slouken@1895
   285
        bmpPitch = (biWidth + 1) >> 1;
slouken@1895
   286
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
slouken@1895
   287
        break;
slouken@1895
   288
    default:
slouken@1895
   289
        pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
slouken@1895
   290
        break;
slouken@1895
   291
    }
slouken@3310
   292
    if (topDown) {
slouken@3310
   293
        bits = top;
slouken@3310
   294
    } else {
slouken@3310
   295
        bits = end - surface->pitch;
slouken@3310
   296
    }
slouken@3310
   297
    while (bits >= top && bits < end) {
slouken@1895
   298
        switch (ExpandBMP) {
slouken@1895
   299
        case 1:
slouken@3310
   300
        case 4:{
slouken@1895
   301
                Uint8 pixel = 0;
slouken@1895
   302
                int shift = (8 - ExpandBMP);
slouken@1895
   303
                for (i = 0; i < surface->w; ++i) {
slouken@1895
   304
                    if (i % (8 / ExpandBMP) == 0) {
slouken@1895
   305
                        if (!SDL_RWread(src, &pixel, 1, 1)) {
slouken@1895
   306
                            SDL_SetError("Error reading from BMP");
slouken@3310
   307
                            was_error = SDL_TRUE;
slouken@1895
   308
                            goto done;
slouken@1895
   309
                        }
slouken@1895
   310
                    }
slouken@1895
   311
                    *(bits + i) = (pixel >> shift);
slouken@1895
   312
                    pixel <<= ExpandBMP;
slouken@1895
   313
                }
slouken@1895
   314
            }
slouken@1895
   315
            break;
slouken@0
   316
slouken@1895
   317
        default:
slouken@1895
   318
            if (SDL_RWread(src, bits, 1, surface->pitch)
slouken@1895
   319
                != surface->pitch) {
slouken@1895
   320
                SDL_Error(SDL_EFREAD);
slouken@3310
   321
                was_error = SDL_TRUE;
slouken@1895
   322
                goto done;
slouken@1895
   323
            }
slouken@0
   324
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
   325
            /* Byte-swap the pixels if needed. Note that the 24bpp
slouken@1895
   326
               case has already been taken care of above. */
slouken@1895
   327
            switch (biBitCount) {
slouken@1895
   328
            case 15:
slouken@3310
   329
            case 16:{
slouken@1895
   330
                    Uint16 *pix = (Uint16 *) bits;
slouken@1895
   331
                    for (i = 0; i < surface->w; i++)
slouken@1895
   332
                        pix[i] = SDL_Swap16(pix[i]);
slouken@1895
   333
                    break;
slouken@1895
   334
                }
slouken@0
   335
slouken@3310
   336
            case 32:{
slouken@1895
   337
                    Uint32 *pix = (Uint32 *) bits;
slouken@1895
   338
                    for (i = 0; i < surface->w; i++)
slouken@1895
   339
                        pix[i] = SDL_Swap32(pix[i]);
slouken@1895
   340
                    break;
slouken@1895
   341
                }
slouken@1895
   342
            }
slouken@0
   343
#endif
slouken@1895
   344
            break;
slouken@1895
   345
        }
slouken@1895
   346
        /* Skip padding bytes, ugh */
slouken@1895
   347
        if (pad) {
slouken@1895
   348
            Uint8 padbyte;
slouken@1895
   349
            for (i = 0; i < pad; ++i) {
slouken@1895
   350
                SDL_RWread(src, &padbyte, 1, 1);
slouken@1895
   351
            }
slouken@1895
   352
        }
slouken@3310
   353
        if (topDown) {
slouken@3310
   354
            bits += surface->pitch;
slouken@3310
   355
        } else {
slouken@3310
   356
            bits -= surface->pitch;
slouken@3310
   357
        }
slouken@1895
   358
    }
slouken@1895
   359
  done:
slouken@1895
   360
    if (was_error) {
slouken@1895
   361
        if (src) {
slouken@1895
   362
            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
slouken@1895
   363
        }
slouken@1895
   364
        if (surface) {
slouken@1895
   365
            SDL_FreeSurface(surface);
slouken@1895
   366
        }
slouken@1895
   367
        surface = NULL;
slouken@1895
   368
    }
slouken@1895
   369
    if (freesrc && src) {
slouken@1895
   370
        SDL_RWclose(src);
slouken@1895
   371
    }
slouken@1895
   372
    return (surface);
slouken@0
   373
}
slouken@0
   374
slouken@1895
   375
int
slouken@1895
   376
SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
slouken@0
   377
{
slouken@1895
   378
    long fp_offset;
slouken@1895
   379
    int i, pad;
slouken@1895
   380
    SDL_Surface *surface;
slouken@1895
   381
    Uint8 *bits;
slouken@0
   382
slouken@1895
   383
    /* The Win32 BMP file header (14 bytes) */
slouken@1895
   384
    char magic[2] = { 'B', 'M' };
slouken@1895
   385
    Uint32 bfSize;
slouken@1895
   386
    Uint16 bfReserved1;
slouken@1895
   387
    Uint16 bfReserved2;
slouken@1895
   388
    Uint32 bfOffBits;
slouken@0
   389
slouken@1895
   390
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@1895
   391
    Uint32 biSize;
slouken@1895
   392
    Sint32 biWidth;
slouken@1895
   393
    Sint32 biHeight;
slouken@1895
   394
    Uint16 biPlanes;
slouken@1895
   395
    Uint16 biBitCount;
slouken@1895
   396
    Uint32 biCompression;
slouken@1895
   397
    Uint32 biSizeImage;
slouken@1895
   398
    Sint32 biXPelsPerMeter;
slouken@1895
   399
    Sint32 biYPelsPerMeter;
slouken@1895
   400
    Uint32 biClrUsed;
slouken@1895
   401
    Uint32 biClrImportant;
slouken@0
   402
slouken@1895
   403
    /* Make sure we have somewhere to save */
slouken@1895
   404
    surface = NULL;
slouken@1895
   405
    if (dst) {
slouken@2969
   406
        SDL_bool save32bit = SDL_FALSE;
slouken@2969
   407
#ifdef SAVE_32BIT_BMP
slouken@2969
   408
        /* We can save alpha information in a 32-bit BMP */
slouken@2969
   409
        if (saveme->map->info.flags & SDL_COPY_COLORKEY ||
slouken@2969
   410
            saveme->format->Amask) {
slouken@2969
   411
            save32bit = SDL_TRUE;
slouken@2969
   412
        }
slouken@2969
   413
#endif /* SAVE_32BIT_BMP */
slouken@2969
   414
slouken@2969
   415
        if (saveme->format->palette && !save32bit) {
slouken@1895
   416
            if (saveme->format->BitsPerPixel == 8) {
slouken@1895
   417
                surface = saveme;
slouken@1895
   418
            } else {
slouken@1895
   419
                SDL_SetError("%d bpp BMP files not supported",
slouken@1895
   420
                             saveme->format->BitsPerPixel);
slouken@1895
   421
            }
slouken@1895
   422
        } else if ((saveme->format->BitsPerPixel == 24) &&
slouken@0
   423
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1895
   424
                   (saveme->format->Rmask == 0x00FF0000) &&
slouken@1895
   425
                   (saveme->format->Gmask == 0x0000FF00) &&
slouken@1895
   426
                   (saveme->format->Bmask == 0x000000FF)
slouken@0
   427
#else
slouken@1895
   428
                   (saveme->format->Rmask == 0x000000FF) &&
slouken@1895
   429
                   (saveme->format->Gmask == 0x0000FF00) &&
slouken@1895
   430
                   (saveme->format->Bmask == 0x00FF0000)
slouken@0
   431
#endif
slouken@1895
   432
            ) {
slouken@1895
   433
            surface = saveme;
slouken@1895
   434
        } else {
slouken@2967
   435
            SDL_PixelFormat format;
slouken@0
   436
slouken@2969
   437
            /* If the surface has a colorkey or alpha channel we'll save a
slouken@2969
   438
               32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
slouken@2969
   439
            if (save32bit) {
slouken@2969
   440
                SDL_InitFormat(&format, 32,
slouken@2990
   441
                               0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@2990
   442
                               0xFF000000);
slouken@2969
   443
            } else {
slouken@2969
   444
                SDL_InitFormat(&format, 24,
slouken@0
   445
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@2969
   446
                               0x00FF0000, 0x0000FF00, 0x000000FF,
slouken@0
   447
#else
slouken@2969
   448
                               0x000000FF, 0x0000FF00, 0x00FF0000,
slouken@0
   449
#endif
slouken@2969
   450
                               0);
slouken@2969
   451
            }
slouken@2967
   452
            surface = SDL_ConvertSurface(saveme, &format, 0);
slouken@2967
   453
            if (!surface) {
slouken@2990
   454
                SDL_SetError("Couldn't convert image to %d bpp",
slouken@2990
   455
                             format.BitsPerPixel);
slouken@1895
   456
            }
slouken@1895
   457
        }
slouken@1895
   458
    }
slouken@0
   459
slouken@1895
   460
    if (surface && (SDL_LockSurface(surface) == 0)) {
slouken@1895
   461
        const int bw = surface->w * surface->format->BytesPerPixel;
slouken@1012
   462
slouken@1895
   463
        /* Set the BMP file header values */
slouken@1895
   464
        bfSize = 0;             /* We'll write this when we're done */
slouken@1895
   465
        bfReserved1 = 0;
slouken@1895
   466
        bfReserved2 = 0;
slouken@1895
   467
        bfOffBits = 0;          /* We'll write this when we're done */
slouken@0
   468
slouken@1895
   469
        /* Write the BMP file header values */
slouken@1895
   470
        fp_offset = SDL_RWtell(dst);
slouken@1895
   471
        SDL_ClearError();
slouken@1895
   472
        SDL_RWwrite(dst, magic, 2, 1);
slouken@1895
   473
        SDL_WriteLE32(dst, bfSize);
slouken@1895
   474
        SDL_WriteLE16(dst, bfReserved1);
slouken@1895
   475
        SDL_WriteLE16(dst, bfReserved2);
slouken@1895
   476
        SDL_WriteLE32(dst, bfOffBits);
slouken@0
   477
slouken@1895
   478
        /* Set the BMP info values */
slouken@1895
   479
        biSize = 40;
slouken@1895
   480
        biWidth = surface->w;
slouken@1895
   481
        biHeight = surface->h;
slouken@1895
   482
        biPlanes = 1;
slouken@1895
   483
        biBitCount = surface->format->BitsPerPixel;
slouken@1895
   484
        biCompression = BI_RGB;
slouken@1895
   485
        biSizeImage = surface->h * surface->pitch;
slouken@1895
   486
        biXPelsPerMeter = 0;
slouken@1895
   487
        biYPelsPerMeter = 0;
slouken@1895
   488
        if (surface->format->palette) {
slouken@1895
   489
            biClrUsed = surface->format->palette->ncolors;
slouken@1895
   490
        } else {
slouken@1895
   491
            biClrUsed = 0;
slouken@1895
   492
        }
slouken@1895
   493
        biClrImportant = 0;
slouken@0
   494
slouken@1895
   495
        /* Write the BMP info values */
slouken@1895
   496
        SDL_WriteLE32(dst, biSize);
slouken@1895
   497
        SDL_WriteLE32(dst, biWidth);
slouken@1895
   498
        SDL_WriteLE32(dst, biHeight);
slouken@1895
   499
        SDL_WriteLE16(dst, biPlanes);
slouken@1895
   500
        SDL_WriteLE16(dst, biBitCount);
slouken@1895
   501
        SDL_WriteLE32(dst, biCompression);
slouken@1895
   502
        SDL_WriteLE32(dst, biSizeImage);
slouken@1895
   503
        SDL_WriteLE32(dst, biXPelsPerMeter);
slouken@1895
   504
        SDL_WriteLE32(dst, biYPelsPerMeter);
slouken@1895
   505
        SDL_WriteLE32(dst, biClrUsed);
slouken@1895
   506
        SDL_WriteLE32(dst, biClrImportant);
slouken@0
   507
slouken@1895
   508
        /* Write the palette (in BGR color order) */
slouken@1895
   509
        if (surface->format->palette) {
slouken@1895
   510
            SDL_Color *colors;
slouken@1895
   511
            int ncolors;
slouken@0
   512
slouken@1895
   513
            colors = surface->format->palette->colors;
slouken@1895
   514
            ncolors = surface->format->palette->ncolors;
slouken@1895
   515
            for (i = 0; i < ncolors; ++i) {
slouken@1895
   516
                SDL_RWwrite(dst, &colors[i].b, 1, 1);
slouken@1895
   517
                SDL_RWwrite(dst, &colors[i].g, 1, 1);
slouken@1895
   518
                SDL_RWwrite(dst, &colors[i].r, 1, 1);
slouken@1895
   519
                SDL_RWwrite(dst, &colors[i].unused, 1, 1);
slouken@1895
   520
            }
slouken@1895
   521
        }
slouken@0
   522
slouken@1895
   523
        /* Write the bitmap offset */
slouken@1895
   524
        bfOffBits = SDL_RWtell(dst) - fp_offset;
slouken@1895
   525
        if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
slouken@1895
   526
            SDL_Error(SDL_EFSEEK);
slouken@1895
   527
        }
slouken@1895
   528
        SDL_WriteLE32(dst, bfOffBits);
slouken@1895
   529
        if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
slouken@1895
   530
            SDL_Error(SDL_EFSEEK);
slouken@1895
   531
        }
slouken@0
   532
slouken@1895
   533
        /* Write the bitmap image upside down */
slouken@1895
   534
        bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
slouken@1895
   535
        pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
slouken@1895
   536
        while (bits > (Uint8 *) surface->pixels) {
slouken@1895
   537
            bits -= surface->pitch;
slouken@1895
   538
            if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
slouken@1895
   539
                SDL_Error(SDL_EFWRITE);
slouken@1895
   540
                break;
slouken@1895
   541
            }
slouken@1895
   542
            if (pad) {
slouken@1895
   543
                const Uint8 padbyte = 0;
slouken@1895
   544
                for (i = 0; i < pad; ++i) {
slouken@1895
   545
                    SDL_RWwrite(dst, &padbyte, 1, 1);
slouken@1895
   546
                }
slouken@1895
   547
            }
slouken@1895
   548
        }
slouken@0
   549
slouken@1895
   550
        /* Write the BMP file size */
slouken@1895
   551
        bfSize = SDL_RWtell(dst) - fp_offset;
slouken@1895
   552
        if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
slouken@1895
   553
            SDL_Error(SDL_EFSEEK);
slouken@1895
   554
        }
slouken@1895
   555
        SDL_WriteLE32(dst, bfSize);
slouken@1895
   556
        if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
slouken@1895
   557
            SDL_Error(SDL_EFSEEK);
slouken@1895
   558
        }
slouken@0
   559
slouken@1895
   560
        /* Close it up.. */
slouken@1895
   561
        SDL_UnlockSurface(surface);
slouken@1895
   562
        if (surface != saveme) {
slouken@1895
   563
            SDL_FreeSurface(surface);
slouken@1895
   564
        }
slouken@1895
   565
    }
slouken@0
   566
slouken@1895
   567
    if (freedst && dst) {
slouken@1895
   568
        SDL_RWclose(dst);
slouken@1895
   569
    }
slouken@1895
   570
    return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
slouken@0
   571
}
slouken@1895
   572
slouken@1895
   573
/* vi: set ts=4 sw=4 expandtab: */