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