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