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