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