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