src/video/SDL_bmp.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 8675 7680f784d850
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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