src/video/SDL_bmp.c
author Ryan C. Gordon
Mon, 23 Jan 2017 12:06:10 -0500
changeset 10837 c2f241c2f6ad
parent 10737 3406a0f8b041
child 10926 97c829825e0e
permissions -rw-r--r--
audio: Fix same bug as last commit, but for _mm_bslli_si128 vs _mm_slli_si128.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 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@10404
    35
#include "SDL_hints.h"
slouken@0
    36
#include "SDL_video.h"
icculus@8675
    37
#include "SDL_assert.h"
slouken@0
    38
#include "SDL_endian.h"
slouken@2823
    39
#include "SDL_pixels_c.h"
slouken@0
    40
slouken@6094
    41
#define SAVE_32BIT_BMP
slouken@6094
    42
slouken@0
    43
/* Compression encodings for BMP files */
slouken@0
    44
#ifndef BI_RGB
slouken@7191
    45
#define BI_RGB      0
slouken@10615
    46
#define BI_RLE8     1
slouken@10615
    47
#define BI_RLE4     2
slouken@7191
    48
#define BI_BITFIELDS    3
slouken@0
    49
#endif
slouken@0
    50
slouken@10404
    51
/* Logical color space values for BMP files */
slouken@10404
    52
#ifndef LCS_WINDOWS_COLOR_SPACE
slouken@10404
    53
/* 0x57696E20 == "Win " */
slouken@10404
    54
#define LCS_WINDOWS_COLOR_SPACE    0x57696E20
slouken@10404
    55
#endif
slouken@0
    56
slouken@7378
    57
static void CorrectAlphaChannel(SDL_Surface *surface)
slouken@7378
    58
{
slouken@7378
    59
    /* Check to see if there is any alpha channel data */
slouken@7378
    60
    SDL_bool hasAlpha = SDL_FALSE;
slouken@7378
    61
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@7378
    62
    int alphaChannelOffset = 0;
slouken@7378
    63
#else
slouken@7378
    64
    int alphaChannelOffset = 3;
slouken@7378
    65
#endif
slouken@7378
    66
    Uint8 *alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
slouken@7378
    67
    Uint8 *end = alpha + surface->h * surface->pitch;
slouken@7378
    68
slouken@7378
    69
    while (alpha < end) {
slouken@7378
    70
        if (*alpha != 0) {
slouken@7378
    71
            hasAlpha = SDL_TRUE;
slouken@7378
    72
            break;
slouken@7378
    73
        }
slouken@7378
    74
        alpha += 4;
slouken@7378
    75
    }
slouken@7378
    76
slouken@7378
    77
    if (!hasAlpha) {
slouken@7378
    78
        alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
slouken@7378
    79
        while (alpha < end) {
slouken@7378
    80
            *alpha = SDL_ALPHA_OPAQUE;
slouken@7378
    81
            alpha += 4;
slouken@7378
    82
        }
slouken@7378
    83
    }
slouken@7378
    84
}
slouken@7378
    85
slouken@1895
    86
SDL_Surface *
slouken@1895
    87
SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
slouken@0
    88
{
slouken@3310
    89
    SDL_bool was_error;
slouken@6666
    90
    Sint64 fp_offset = 0;
slouken@1895
    91
    int bmpPitch;
slouken@1895
    92
    int i, pad;
slouken@1895
    93
    SDL_Surface *surface;
icculus@9126
    94
    Uint32 Rmask = 0;
icculus@9126
    95
    Uint32 Gmask = 0;
icculus@9126
    96
    Uint32 Bmask = 0;
icculus@9126
    97
    Uint32 Amask = 0;
slouken@1895
    98
    SDL_Palette *palette;
slouken@1895
    99
    Uint8 *bits;
slouken@3310
   100
    Uint8 *top, *end;
slouken@3310
   101
    SDL_bool topDown;
slouken@1895
   102
    int ExpandBMP;
icculus@9126
   103
    SDL_bool haveRGBMasks = SDL_FALSE;
icculus@9126
   104
    SDL_bool haveAlphaMask = SDL_FALSE;
slouken@7378
   105
    SDL_bool correctAlpha = SDL_FALSE;
slouken@0
   106
slouken@1895
   107
    /* The Win32 BMP file header (14 bytes) */
slouken@1895
   108
    char magic[2];
gabomdq@7678
   109
    /* Uint32 bfSize = 0; */
gabomdq@7678
   110
    /* Uint16 bfReserved1 = 0; */
gabomdq@7678
   111
    /* Uint16 bfReserved2 = 0; */
icculus@6389
   112
    Uint32 bfOffBits = 0;
slouken@0
   113
slouken@1895
   114
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
icculus@6389
   115
    Uint32 biSize = 0;
icculus@6389
   116
    Sint32 biWidth = 0;
icculus@6389
   117
    Sint32 biHeight = 0;
gabomdq@7678
   118
    /* Uint16 biPlanes = 0; */
icculus@6389
   119
    Uint16 biBitCount = 0;
icculus@6389
   120
    Uint32 biCompression = 0;
gabomdq@7678
   121
    /* Uint32 biSizeImage = 0; */
gabomdq@7678
   122
    /* Sint32 biXPelsPerMeter = 0; */
gabomdq@7678
   123
    /* Sint32 biYPelsPerMeter = 0; */
icculus@6389
   124
    Uint32 biClrUsed = 0;
gabomdq@7678
   125
    /* Uint32 biClrImportant = 0; */
slouken@0
   126
slouken@1895
   127
    /* Make sure we are passed a valid data source */
slouken@1895
   128
    surface = NULL;
slouken@3310
   129
    was_error = SDL_FALSE;
slouken@1895
   130
    if (src == NULL) {
slouken@3310
   131
        was_error = SDL_TRUE;
slouken@1895
   132
        goto done;
slouken@1895
   133
    }
slouken@0
   134
slouken@1895
   135
    /* Read in the BMP file header */
slouken@1895
   136
    fp_offset = SDL_RWtell(src);
slouken@1895
   137
    SDL_ClearError();
slouken@1895
   138
    if (SDL_RWread(src, magic, 1, 2) != 2) {
slouken@1895
   139
        SDL_Error(SDL_EFREAD);
slouken@3310
   140
        was_error = SDL_TRUE;
slouken@1895
   141
        goto done;
slouken@1895
   142
    }
slouken@1895
   143
    if (SDL_strncmp(magic, "BM", 2) != 0) {
slouken@1895
   144
        SDL_SetError("File is not a Windows BMP file");
slouken@3310
   145
        was_error = SDL_TRUE;
slouken@1895
   146
        goto done;
slouken@1895
   147
    }
gabomdq@7678
   148
    /* bfSize = */ SDL_ReadLE32(src);
gabomdq@7678
   149
    /* bfReserved1 = */ SDL_ReadLE16(src);
gabomdq@7678
   150
    /* bfReserved2 = */ SDL_ReadLE16(src);
slouken@1895
   151
    bfOffBits = SDL_ReadLE32(src);
slouken@0
   152
slouken@1895
   153
    /* Read the Win32 BITMAPINFOHEADER */
slouken@1895
   154
    biSize = SDL_ReadLE32(src);
icculus@9126
   155
    if (biSize == 12) {   /* really old BITMAPCOREHEADER */
slouken@1895
   156
        biWidth = (Uint32) SDL_ReadLE16(src);
slouken@1895
   157
        biHeight = (Uint32) SDL_ReadLE16(src);
gabomdq@7678
   158
        /* biPlanes = */ SDL_ReadLE16(src);
slouken@1895
   159
        biBitCount = SDL_ReadLE16(src);
slouken@1895
   160
        biCompression = BI_RGB;
icculus@9126
   161
    } else if (biSize >= 40) {  /* some version of BITMAPINFOHEADER */
icculus@9126
   162
        Uint32 headerSize;
slouken@1895
   163
        biWidth = SDL_ReadLE32(src);
slouken@1895
   164
        biHeight = SDL_ReadLE32(src);
gabomdq@7678
   165
        /* biPlanes = */ SDL_ReadLE16(src);
slouken@1895
   166
        biBitCount = SDL_ReadLE16(src);
slouken@1895
   167
        biCompression = SDL_ReadLE32(src);
gabomdq@7678
   168
        /* biSizeImage = */ SDL_ReadLE32(src);
gabomdq@7678
   169
        /* biXPelsPerMeter = */ SDL_ReadLE32(src);
gabomdq@7678
   170
        /* biYPelsPerMeter = */ SDL_ReadLE32(src);
slouken@1895
   171
        biClrUsed = SDL_ReadLE32(src);
gabomdq@7678
   172
        /* biClrImportant = */ SDL_ReadLE32(src);
slouken@7913
   173
icculus@9126
   174
        /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
icculus@9126
   175
        if (biSize == 64) {
icculus@9126
   176
            /* ignore these extra fields. */
icculus@9126
   177
            if (biCompression == BI_BITFIELDS) {
icculus@9126
   178
                /* this value is actually huffman compression in this variant. */
icculus@9126
   179
                SDL_SetError("Compressed BMP files not supported");
icculus@9126
   180
                was_error = SDL_TRUE;
icculus@9126
   181
                goto done;
icculus@9126
   182
            }
icculus@9126
   183
        } else {
icculus@9126
   184
            /* This is complicated. If compression is BI_BITFIELDS, then
icculus@9126
   185
               we have 3 DWORDS that specify the RGB masks. This is either
icculus@9126
   186
               stored here in an BITMAPV2INFOHEADER (which only differs in
icculus@9126
   187
               that it adds these RGB masks) and biSize >= 52, or we've got
icculus@9126
   188
               these masks stored in the exact same place, but strictly
icculus@9126
   189
               speaking, this is the bmiColors field in BITMAPINFO immediately
icculus@9126
   190
               following the legacy v1 info header, just past biSize. */
icculus@9126
   191
            if (biCompression == BI_BITFIELDS) {
icculus@9126
   192
                haveRGBMasks = SDL_TRUE;
icculus@9126
   193
                Rmask = SDL_ReadLE32(src);
icculus@9126
   194
                Gmask = SDL_ReadLE32(src);
icculus@9126
   195
                Bmask = SDL_ReadLE32(src);
icculus@9126
   196
icculus@9126
   197
                /* ...v3 adds an alpha mask. */
icculus@9126
   198
                if (biSize >= 56) {  /* BITMAPV3INFOHEADER; adds alpha mask */
icculus@9126
   199
                    haveAlphaMask = SDL_TRUE;
icculus@9126
   200
                    Amask = SDL_ReadLE32(src);
icculus@9126
   201
                }
icculus@9126
   202
            } else {
icculus@9126
   203
                /* the mask fields are ignored for v2+ headers if not BI_BITFIELD. */
icculus@9126
   204
                if (biSize >= 52) {  /* BITMAPV2INFOHEADER; adds RGB masks */
icculus@9126
   205
                    /*Rmask = */ SDL_ReadLE32(src);
icculus@9126
   206
                    /*Gmask = */ SDL_ReadLE32(src);
icculus@9126
   207
                    /*Bmask = */ SDL_ReadLE32(src);
icculus@9126
   208
                }
icculus@9126
   209
                if (biSize >= 56) {  /* BITMAPV3INFOHEADER; adds alpha mask */
icculus@9126
   210
                    /*Amask = */ SDL_ReadLE32(src);
icculus@9126
   211
                }
icculus@9126
   212
            }
icculus@9126
   213
icculus@9126
   214
            /* Insert other fields here; Wikipedia and MSDN say we're up to
icculus@9126
   215
               v5 of this header, but we ignore those for now (they add gamma,
icculus@9126
   216
               color spaces, etc). Ignoring the weird OS/2 2.x format, we
icculus@9126
   217
               currently parse up to v3 correctly (hopefully!). */
icculus@9126
   218
        }
icculus@9126
   219
icculus@9126
   220
        /* skip any header bytes we didn't handle... */
icculus@9126
   221
        headerSize = (Uint32) (SDL_RWtell(src) - (fp_offset + 14));
slouken@7913
   222
        if (biSize > headerSize) {
slouken@7913
   223
            SDL_RWseek(src, (biSize - headerSize), RW_SEEK_CUR);
slouken@7913
   224
        }
slouken@1895
   225
    }
slouken@3310
   226
    if (biHeight < 0) {
slouken@3310
   227
        topDown = SDL_TRUE;
slouken@3310
   228
        biHeight = -biHeight;
slouken@3310
   229
    } else {
slouken@3310
   230
        topDown = SDL_FALSE;
slouken@3310
   231
    }
slouken@0
   232
slouken@1895
   233
    /* Check for read error */
slouken@1895
   234
    if (SDL_strcmp(SDL_GetError(), "") != 0) {
slouken@3310
   235
        was_error = SDL_TRUE;
slouken@1895
   236
        goto done;
slouken@1895
   237
    }
slouken@0
   238
slouken@1895
   239
    /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
slouken@1895
   240
    switch (biBitCount) {
slouken@1895
   241
    case 1:
slouken@1895
   242
    case 4:
slouken@1895
   243
        ExpandBMP = biBitCount;
slouken@1895
   244
        biBitCount = 8;
slouken@1895
   245
        break;
slouken@1895
   246
    default:
slouken@1895
   247
        ExpandBMP = 0;
slouken@1895
   248
        break;
slouken@1895
   249
    }
slouken@0
   250
slouken@1895
   251
    /* We don't support any BMP compression right now */
slouken@1895
   252
    switch (biCompression) {
slouken@1895
   253
    case BI_RGB:
slouken@1895
   254
        /* If there are no masks, use the defaults */
icculus@9126
   255
        SDL_assert(!haveRGBMasks);
icculus@9126
   256
        SDL_assert(!haveAlphaMask);
icculus@9126
   257
        /* Default values for the BMP format */
slouken@1895
   258
        switch (biBitCount) {
slouken@1895
   259
        case 15:
slouken@1895
   260
        case 16:
icculus@9126
   261
            Rmask = 0x7C00;
icculus@9126
   262
            Gmask = 0x03E0;
icculus@9126
   263
            Bmask = 0x001F;
icculus@9126
   264
            break;
icculus@9126
   265
        case 24:
icculus@9126
   266
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
icculus@9126
   267
            Rmask = 0x000000FF;
icculus@9126
   268
            Gmask = 0x0000FF00;
icculus@9126
   269
            Bmask = 0x00FF0000;
icculus@9126
   270
#else
icculus@9126
   271
            Rmask = 0x00FF0000;
icculus@9126
   272
            Gmask = 0x0000FF00;
icculus@9126
   273
            Bmask = 0x000000FF;
icculus@9126
   274
#endif
slouken@3497
   275
            break;
slouken@1895
   276
        case 32:
icculus@9126
   277
            /* We don't know if this has alpha channel or not */
icculus@9126
   278
            correctAlpha = SDL_TRUE;
icculus@9126
   279
            Amask = 0xFF000000;
icculus@9126
   280
            Rmask = 0x00FF0000;
icculus@9126
   281
            Gmask = 0x0000FF00;
icculus@9126
   282
            Bmask = 0x000000FF;
slouken@1895
   283
            break;
slouken@1895
   284
        default:
slouken@1895
   285
            break;
slouken@1895
   286
        }
slouken@1895
   287
        break;
icculus@9126
   288
icculus@9126
   289
    case BI_BITFIELDS:
icculus@9126
   290
        break;  /* we handled this in the info header. */
icculus@9126
   291
slouken@1895
   292
    default:
slouken@1895
   293
        SDL_SetError("Compressed BMP files not supported");
slouken@3310
   294
        was_error = SDL_TRUE;
slouken@1895
   295
        goto done;
slouken@1895
   296
    }
slouken@0
   297
slouken@1895
   298
    /* Create a compatible surface, note that the colors are RGB ordered */
slouken@1895
   299
    surface =
slouken@1895
   300
        SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
slouken@3497
   301
                             Bmask, Amask);
slouken@1895
   302
    if (surface == NULL) {
slouken@3310
   303
        was_error = SDL_TRUE;
slouken@1895
   304
        goto done;
slouken@1895
   305
    }
slouken@0
   306
slouken@1895
   307
    /* Load the palette, if any */
slouken@1895
   308
    palette = (surface->format)->palette;
slouken@1895
   309
    if (palette) {
icculus@8675
   310
        SDL_assert(biBitCount <= 8);
slouken@1895
   311
        if (biClrUsed == 0) {
slouken@1895
   312
            biClrUsed = 1 << biBitCount;
slouken@1895
   313
        }
slouken@2920
   314
        if ((int) biClrUsed > palette->ncolors) {
philipp@9334
   315
            SDL_Color *colors;
philipp@9336
   316
            int ncolors = biClrUsed;
philipp@9334
   317
            colors =
slouken@2851
   318
                (SDL_Color *) SDL_realloc(palette->colors,
philipp@9336
   319
                                          ncolors *
slouken@2851
   320
                                          sizeof(*palette->colors));
philipp@9334
   321
            if (!colors) {
slouken@2851
   322
                SDL_OutOfMemory();
slouken@3310
   323
                was_error = SDL_TRUE;
slouken@2851
   324
                goto done;
slouken@2851
   325
            }
philipp@9336
   326
            palette->ncolors = ncolors;
philipp@9334
   327
            palette->colors = colors;
slouken@2920
   328
        } else if ((int) biClrUsed < palette->ncolors) {
slouken@2851
   329
            palette->ncolors = biClrUsed;
slouken@2851
   330
        }
slouken@1895
   331
        if (biSize == 12) {
slouken@1895
   332
            for (i = 0; i < (int) biClrUsed; ++i) {
slouken@1895
   333
                SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@1895
   334
                SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@1895
   335
                SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@7024
   336
                palette->colors[i].a = SDL_ALPHA_OPAQUE;
slouken@1895
   337
            }
slouken@1895
   338
        } else {
slouken@1895
   339
            for (i = 0; i < (int) biClrUsed; ++i) {
slouken@1895
   340
                SDL_RWread(src, &palette->colors[i].b, 1, 1);
slouken@1895
   341
                SDL_RWread(src, &palette->colors[i].g, 1, 1);
slouken@1895
   342
                SDL_RWread(src, &palette->colors[i].r, 1, 1);
slouken@7024
   343
                SDL_RWread(src, &palette->colors[i].a, 1, 1);
slouken@7068
   344
slouken@7068
   345
                /* According to Microsoft documentation, the fourth element
slouken@7068
   346
                   is reserved and must be zero, so we shouldn't treat it as
slouken@7068
   347
                   alpha.
slouken@7068
   348
                */
slouken@7068
   349
                palette->colors[i].a = SDL_ALPHA_OPAQUE;
slouken@1895
   350
            }
slouken@1895
   351
        }
slouken@1895
   352
    }
slouken@0
   353
slouken@1895
   354
    /* Read the surface pixels.  Note that the bmp image is upside down */
slouken@1895
   355
    if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
slouken@1895
   356
        SDL_Error(SDL_EFSEEK);
slouken@3310
   357
        was_error = SDL_TRUE;
slouken@1895
   358
        goto done;
slouken@1895
   359
    }
slouken@3310
   360
    top = (Uint8 *)surface->pixels;
slouken@3310
   361
    end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
slouken@1895
   362
    switch (ExpandBMP) {
slouken@1895
   363
    case 1:
slouken@1895
   364
        bmpPitch = (biWidth + 7) >> 3;
slouken@1895
   365
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
slouken@1895
   366
        break;
slouken@1895
   367
    case 4:
slouken@1895
   368
        bmpPitch = (biWidth + 1) >> 1;
slouken@1895
   369
        pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
slouken@1895
   370
        break;
slouken@1895
   371
    default:
slouken@1895
   372
        pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
slouken@1895
   373
        break;
slouken@1895
   374
    }
slouken@3310
   375
    if (topDown) {
slouken@3310
   376
        bits = top;
slouken@3310
   377
    } else {
slouken@3310
   378
        bits = end - surface->pitch;
slouken@3310
   379
    }
slouken@3310
   380
    while (bits >= top && bits < end) {
slouken@1895
   381
        switch (ExpandBMP) {
slouken@1895
   382
        case 1:
slouken@3310
   383
        case 4:{
slouken@1895
   384
                Uint8 pixel = 0;
slouken@1895
   385
                int shift = (8 - ExpandBMP);
slouken@1895
   386
                for (i = 0; i < surface->w; ++i) {
slouken@1895
   387
                    if (i % (8 / ExpandBMP) == 0) {
slouken@1895
   388
                        if (!SDL_RWread(src, &pixel, 1, 1)) {
slouken@1895
   389
                            SDL_SetError("Error reading from BMP");
slouken@3310
   390
                            was_error = SDL_TRUE;
slouken@1895
   391
                            goto done;
slouken@1895
   392
                        }
slouken@1895
   393
                    }
slouken@1895
   394
                    *(bits + i) = (pixel >> shift);
slouken@1895
   395
                    pixel <<= ExpandBMP;
slouken@1895
   396
                }
slouken@1895
   397
            }
slouken@1895
   398
            break;
slouken@0
   399
slouken@1895
   400
        default:
slouken@1895
   401
            if (SDL_RWread(src, bits, 1, surface->pitch)
slouken@1895
   402
                != surface->pitch) {
slouken@1895
   403
                SDL_Error(SDL_EFREAD);
slouken@3310
   404
                was_error = SDL_TRUE;
slouken@1895
   405
                goto done;
slouken@1895
   406
            }
slouken@0
   407
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
slouken@1895
   408
            /* Byte-swap the pixels if needed. Note that the 24bpp
slouken@1895
   409
               case has already been taken care of above. */
slouken@1895
   410
            switch (biBitCount) {
slouken@1895
   411
            case 15:
slouken@3310
   412
            case 16:{
slouken@1895
   413
                    Uint16 *pix = (Uint16 *) bits;
slouken@1895
   414
                    for (i = 0; i < surface->w; i++)
slouken@1895
   415
                        pix[i] = SDL_Swap16(pix[i]);
slouken@1895
   416
                    break;
slouken@1895
   417
                }
slouken@0
   418
slouken@3310
   419
            case 32:{
slouken@1895
   420
                    Uint32 *pix = (Uint32 *) bits;
slouken@1895
   421
                    for (i = 0; i < surface->w; i++)
slouken@1895
   422
                        pix[i] = SDL_Swap32(pix[i]);
slouken@1895
   423
                    break;
slouken@1895
   424
                }
slouken@1895
   425
            }
slouken@0
   426
#endif
slouken@1895
   427
            break;
slouken@1895
   428
        }
slouken@1895
   429
        /* Skip padding bytes, ugh */
slouken@1895
   430
        if (pad) {
slouken@1895
   431
            Uint8 padbyte;
slouken@1895
   432
            for (i = 0; i < pad; ++i) {
slouken@1895
   433
                SDL_RWread(src, &padbyte, 1, 1);
slouken@1895
   434
            }
slouken@1895
   435
        }
slouken@3310
   436
        if (topDown) {
slouken@3310
   437
            bits += surface->pitch;
slouken@3310
   438
        } else {
slouken@3310
   439
            bits -= surface->pitch;
slouken@3310
   440
        }
slouken@1895
   441
    }
slouken@7378
   442
    if (correctAlpha) {
slouken@7378
   443
        CorrectAlphaChannel(surface);
slouken@7378
   444
    }
slouken@1895
   445
  done:
slouken@1895
   446
    if (was_error) {
slouken@1895
   447
        if (src) {
slouken@1895
   448
            SDL_RWseek(src, fp_offset, RW_SEEK_SET);
slouken@1895
   449
        }
slouken@7720
   450
        SDL_FreeSurface(surface);
slouken@1895
   451
        surface = NULL;
slouken@1895
   452
    }
slouken@1895
   453
    if (freesrc && src) {
slouken@1895
   454
        SDL_RWclose(src);
slouken@1895
   455
    }
slouken@1895
   456
    return (surface);
slouken@0
   457
}
slouken@0
   458
slouken@1895
   459
int
slouken@1895
   460
SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
slouken@0
   461
{
slouken@6666
   462
    Sint64 fp_offset;
slouken@1895
   463
    int i, pad;
slouken@1895
   464
    SDL_Surface *surface;
slouken@1895
   465
    Uint8 *bits;
slouken@10404
   466
    SDL_bool save32bit = SDL_FALSE;
slouken@10404
   467
    SDL_bool saveLegacyBMP = SDL_FALSE;
slouken@0
   468
slouken@1895
   469
    /* The Win32 BMP file header (14 bytes) */
slouken@1895
   470
    char magic[2] = { 'B', 'M' };
slouken@1895
   471
    Uint32 bfSize;
slouken@1895
   472
    Uint16 bfReserved1;
slouken@1895
   473
    Uint16 bfReserved2;
slouken@1895
   474
    Uint32 bfOffBits;
slouken@0
   475
slouken@1895
   476
    /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
slouken@1895
   477
    Uint32 biSize;
slouken@1895
   478
    Sint32 biWidth;
slouken@1895
   479
    Sint32 biHeight;
slouken@1895
   480
    Uint16 biPlanes;
slouken@1895
   481
    Uint16 biBitCount;
slouken@1895
   482
    Uint32 biCompression;
slouken@1895
   483
    Uint32 biSizeImage;
slouken@1895
   484
    Sint32 biXPelsPerMeter;
slouken@1895
   485
    Sint32 biYPelsPerMeter;
slouken@1895
   486
    Uint32 biClrUsed;
slouken@1895
   487
    Uint32 biClrImportant;
slouken@0
   488
slouken@10404
   489
    /* The additional header members from the Win32 BITMAPV4HEADER struct (108 bytes in total) */
slouken@10404
   490
    Uint32 bV4RedMask = 0;
slouken@10404
   491
    Uint32 bV4GreenMask = 0;
slouken@10404
   492
    Uint32 bV4BlueMask = 0;
slouken@10404
   493
    Uint32 bV4AlphaMask = 0;
slouken@10404
   494
    Uint32 bV4CSType = 0;
slouken@10404
   495
    Sint32 bV4Endpoints[3 * 3] = {0};
slouken@10404
   496
    Uint32 bV4GammaRed = 0;
slouken@10404
   497
    Uint32 bV4GammaGreen = 0;
slouken@10404
   498
    Uint32 bV4GammaBlue = 0;
slouken@10404
   499
slouken@1895
   500
    /* Make sure we have somewhere to save */
slouken@1895
   501
    surface = NULL;
slouken@1895
   502
    if (dst) {
slouken@2969
   503
#ifdef SAVE_32BIT_BMP
slouken@2969
   504
        /* We can save alpha information in a 32-bit BMP */
slouken@10404
   505
        if (saveme->format->BitsPerPixel >= 8 && (saveme->format->Amask ||
slouken@10404
   506
            saveme->map->info.flags & SDL_COPY_COLORKEY)) {
slouken@2969
   507
            save32bit = SDL_TRUE;
slouken@2969
   508
        }
slouken@2969
   509
#endif /* SAVE_32BIT_BMP */
slouken@2969
   510
slouken@2969
   511
        if (saveme->format->palette && !save32bit) {
slouken@1895
   512
            if (saveme->format->BitsPerPixel == 8) {
slouken@1895
   513
                surface = saveme;
slouken@1895
   514
            } else {
slouken@1895
   515
                SDL_SetError("%d bpp BMP files not supported",
slouken@1895
   516
                             saveme->format->BitsPerPixel);
slouken@1895
   517
            }
slouken@10404
   518
        } else if ((saveme->format->BitsPerPixel == 24) && !save32bit &&
slouken@0
   519
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1895
   520
                   (saveme->format->Rmask == 0x00FF0000) &&
slouken@1895
   521
                   (saveme->format->Gmask == 0x0000FF00) &&
slouken@1895
   522
                   (saveme->format->Bmask == 0x000000FF)
slouken@0
   523
#else
slouken@1895
   524
                   (saveme->format->Rmask == 0x000000FF) &&
slouken@1895
   525
                   (saveme->format->Gmask == 0x0000FF00) &&
slouken@1895
   526
                   (saveme->format->Bmask == 0x00FF0000)
slouken@0
   527
#endif
slouken@1895
   528
            ) {
slouken@1895
   529
            surface = saveme;
slouken@1895
   530
        } else {
slouken@2967
   531
            SDL_PixelFormat format;
slouken@0
   532
slouken@2969
   533
            /* If the surface has a colorkey or alpha channel we'll save a
slouken@2969
   534
               32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
slouken@2969
   535
            if (save32bit) {
slouken@10512
   536
                SDL_InitFormat(&format, SDL_PIXELFORMAT_BGRA32);
slouken@5288
   537
            } else {
slouken@5288
   538
                SDL_InitFormat(&format, SDL_PIXELFORMAT_BGR24);
slouken@2969
   539
            }
slouken@2967
   540
            surface = SDL_ConvertSurface(saveme, &format, 0);
slouken@2967
   541
            if (!surface) {
slouken@2990
   542
                SDL_SetError("Couldn't convert image to %d bpp",
slouken@2990
   543
                             format.BitsPerPixel);
slouken@1895
   544
            }
slouken@1895
   545
        }
philipp@9563
   546
    } else {
philipp@9563
   547
        /* Set no error here because it may overwrite a more useful message from
philipp@9563
   548
           SDL_RWFromFile() if SDL_SaveBMP_RW() is called from SDL_SaveBMP(). */
philipp@9563
   549
        return -1;
slouken@1895
   550
    }
slouken@0
   551
slouken@10404
   552
    if (save32bit) {
slouken@10499
   553
        saveLegacyBMP = SDL_GetHintBoolean(SDL_HINT_BMP_SAVE_LEGACY_FORMAT, SDL_FALSE);
slouken@10404
   554
    }
slouken@10404
   555
slouken@1895
   556
    if (surface && (SDL_LockSurface(surface) == 0)) {
slouken@1895
   557
        const int bw = surface->w * surface->format->BytesPerPixel;
slouken@1895
   558
slouken@1895
   559
        /* Set the BMP file header values */
slouken@1895
   560
        bfSize = 0;             /* We'll write this when we're done */
slouken@1895
   561
        bfReserved1 = 0;
slouken@1895
   562
        bfReserved2 = 0;
slouken@1895
   563
        bfOffBits = 0;          /* We'll write this when we're done */
slouken@1012
   564
slouken@1895
   565
        /* Write the BMP file header values */
slouken@1895
   566
        fp_offset = SDL_RWtell(dst);
slouken@1895
   567
        SDL_ClearError();
slouken@1895
   568
        SDL_RWwrite(dst, magic, 2, 1);
slouken@1895
   569
        SDL_WriteLE32(dst, bfSize);
slouken@1895
   570
        SDL_WriteLE16(dst, bfReserved1);
slouken@1895
   571
        SDL_WriteLE16(dst, bfReserved2);
slouken@1895
   572
        SDL_WriteLE32(dst, bfOffBits);
slouken@0
   573
slouken@1895
   574
        /* Set the BMP info values */
slouken@1895
   575
        biSize = 40;
slouken@1895
   576
        biWidth = surface->w;
slouken@1895
   577
        biHeight = surface->h;
slouken@1895
   578
        biPlanes = 1;
slouken@1895
   579
        biBitCount = surface->format->BitsPerPixel;
slouken@1895
   580
        biCompression = BI_RGB;
slouken@1895
   581
        biSizeImage = surface->h * surface->pitch;
slouken@1895
   582
        biXPelsPerMeter = 0;
slouken@1895
   583
        biYPelsPerMeter = 0;
slouken@1895
   584
        if (surface->format->palette) {
slouken@1895
   585
            biClrUsed = surface->format->palette->ncolors;
slouken@1895
   586
        } else {
slouken@1895
   587
            biClrUsed = 0;
slouken@1895
   588
        }
slouken@1895
   589
        biClrImportant = 0;
slouken@0
   590
slouken@10404
   591
        /* Set the BMP info values for the version 4 header */
slouken@10404
   592
        if (save32bit && !saveLegacyBMP) {
slouken@10404
   593
            biSize = 108;
slouken@10404
   594
            biCompression = BI_BITFIELDS;
slouken@10404
   595
            /* The BMP format is always little endian, these masks stay the same */
slouken@10404
   596
            bV4RedMask   = 0x00ff0000;
slouken@10404
   597
            bV4GreenMask = 0x0000ff00;
slouken@10404
   598
            bV4BlueMask  = 0x000000ff;
slouken@10404
   599
            bV4AlphaMask = 0xff000000;
slouken@10404
   600
            bV4CSType = LCS_WINDOWS_COLOR_SPACE;
slouken@10404
   601
            bV4GammaRed = 0;
slouken@10404
   602
            bV4GammaGreen = 0;
slouken@10404
   603
            bV4GammaBlue = 0;
slouken@10404
   604
        }
slouken@10404
   605
slouken@1895
   606
        /* Write the BMP info values */
slouken@1895
   607
        SDL_WriteLE32(dst, biSize);
slouken@1895
   608
        SDL_WriteLE32(dst, biWidth);
slouken@1895
   609
        SDL_WriteLE32(dst, biHeight);
slouken@1895
   610
        SDL_WriteLE16(dst, biPlanes);
slouken@1895
   611
        SDL_WriteLE16(dst, biBitCount);
slouken@1895
   612
        SDL_WriteLE32(dst, biCompression);
slouken@1895
   613
        SDL_WriteLE32(dst, biSizeImage);
slouken@1895
   614
        SDL_WriteLE32(dst, biXPelsPerMeter);
slouken@1895
   615
        SDL_WriteLE32(dst, biYPelsPerMeter);
slouken@1895
   616
        SDL_WriteLE32(dst, biClrUsed);
slouken@1895
   617
        SDL_WriteLE32(dst, biClrImportant);
slouken@0
   618
slouken@10404
   619
        /* Write the BMP info values for the version 4 header */
slouken@10404
   620
        if (save32bit && !saveLegacyBMP) {
slouken@10404
   621
            SDL_WriteLE32(dst, bV4RedMask);
slouken@10404
   622
            SDL_WriteLE32(dst, bV4GreenMask);
slouken@10404
   623
            SDL_WriteLE32(dst, bV4BlueMask);
slouken@10404
   624
            SDL_WriteLE32(dst, bV4AlphaMask);
slouken@10404
   625
            SDL_WriteLE32(dst, bV4CSType);
slouken@10404
   626
            for (i = 0; i < 3 * 3; i++) {
slouken@10404
   627
                SDL_WriteLE32(dst, bV4Endpoints[i]);
slouken@10404
   628
            }
slouken@10404
   629
            SDL_WriteLE32(dst, bV4GammaRed);
slouken@10404
   630
            SDL_WriteLE32(dst, bV4GammaGreen);
slouken@10404
   631
            SDL_WriteLE32(dst, bV4GammaBlue);
slouken@10404
   632
        }
slouken@10404
   633
slouken@1895
   634
        /* Write the palette (in BGR color order) */
slouken@1895
   635
        if (surface->format->palette) {
slouken@1895
   636
            SDL_Color *colors;
slouken@1895
   637
            int ncolors;
slouken@0
   638
slouken@1895
   639
            colors = surface->format->palette->colors;
slouken@1895
   640
            ncolors = surface->format->palette->ncolors;
slouken@1895
   641
            for (i = 0; i < ncolors; ++i) {
slouken@1895
   642
                SDL_RWwrite(dst, &colors[i].b, 1, 1);
slouken@1895
   643
                SDL_RWwrite(dst, &colors[i].g, 1, 1);
slouken@1895
   644
                SDL_RWwrite(dst, &colors[i].r, 1, 1);
slouken@7024
   645
                SDL_RWwrite(dst, &colors[i].a, 1, 1);
slouken@1895
   646
            }
slouken@1895
   647
        }
slouken@0
   648
slouken@1895
   649
        /* Write the bitmap offset */
slouken@6666
   650
        bfOffBits = (Uint32)(SDL_RWtell(dst) - fp_offset);
slouken@1895
   651
        if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
slouken@1895
   652
            SDL_Error(SDL_EFSEEK);
slouken@1895
   653
        }
slouken@1895
   654
        SDL_WriteLE32(dst, bfOffBits);
slouken@1895
   655
        if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
slouken@1895
   656
            SDL_Error(SDL_EFSEEK);
slouken@1895
   657
        }
slouken@0
   658
slouken@1895
   659
        /* Write the bitmap image upside down */
slouken@1895
   660
        bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
slouken@1895
   661
        pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
slouken@1895
   662
        while (bits > (Uint8 *) surface->pixels) {
slouken@1895
   663
            bits -= surface->pitch;
slouken@1895
   664
            if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
slouken@1895
   665
                SDL_Error(SDL_EFWRITE);
slouken@1895
   666
                break;
slouken@1895
   667
            }
slouken@1895
   668
            if (pad) {
slouken@1895
   669
                const Uint8 padbyte = 0;
slouken@1895
   670
                for (i = 0; i < pad; ++i) {
slouken@1895
   671
                    SDL_RWwrite(dst, &padbyte, 1, 1);
slouken@1895
   672
                }
slouken@1895
   673
            }
slouken@1895
   674
        }
slouken@0
   675
slouken@1895
   676
        /* Write the BMP file size */
slouken@6666
   677
        bfSize = (Uint32)(SDL_RWtell(dst) - fp_offset);
slouken@1895
   678
        if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
slouken@1895
   679
            SDL_Error(SDL_EFSEEK);
slouken@1895
   680
        }
slouken@1895
   681
        SDL_WriteLE32(dst, bfSize);
slouken@1895
   682
        if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
slouken@1895
   683
            SDL_Error(SDL_EFSEEK);
slouken@1895
   684
        }
slouken@0
   685
slouken@1895
   686
        /* Close it up.. */
slouken@1895
   687
        SDL_UnlockSurface(surface);
slouken@1895
   688
        if (surface != saveme) {
slouken@1895
   689
            SDL_FreeSurface(surface);
slouken@1895
   690
        }
slouken@1895
   691
    }
slouken@0
   692
slouken@1895
   693
    if (freedst && dst) {
slouken@1895
   694
        SDL_RWclose(dst);
slouken@1895
   695
    }
slouken@1895
   696
    return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
slouken@0
   697
}
slouken@1895
   698
slouken@1895
   699
/* vi: set ts=4 sw=4 expandtab: */