2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2012 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
25 Code to load and save surfaces in Windows BMP format.
27 Why support BMP format? Well, it's a native format for Windows, and
28 most image processing programs can read and write it. It would be nice
29 to be able to have at least one image format that we can natively load
30 and save, and since PNG is so complex that it would bloat the library,
31 BMP is a good alternative.
33 This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
36 #include "SDL_video.h"
37 #include "SDL_endian.h"
39 /* Compression encodings for BMP files */
44 #define BI_BITFIELDS 3
48 SDL_Surface * SDL_LoadBMP_RW (SDL_RWops *src, int freesrc)
64 /* The Win32 BMP file header (14 bytes) */
71 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
79 Sint32 biXPelsPerMeter;
80 Sint32 biYPelsPerMeter;
82 Uint32 biClrImportant;
84 /* Make sure we are passed a valid data source */
86 was_error = SDL_FALSE;
92 /* Read in the BMP file header */
93 fp_offset = SDL_RWtell(src);
95 if ( SDL_RWread(src, magic, 1, 2) != 2 ) {
96 SDL_Error(SDL_EFREAD);
100 if ( SDL_strncmp(magic, "BM", 2) != 0 ) {
101 SDL_SetError("File is not a Windows BMP file");
102 was_error = SDL_TRUE;
105 bfSize = SDL_ReadLE32(src);
106 bfReserved1 = SDL_ReadLE16(src);
107 bfReserved2 = SDL_ReadLE16(src);
108 bfOffBits = SDL_ReadLE32(src);
110 /* Read the Win32 BITMAPINFOHEADER */
111 biSize = SDL_ReadLE32(src);
112 if ( biSize == 12 ) {
113 biWidth = (Uint32)SDL_ReadLE16(src);
114 biHeight = (Uint32)SDL_ReadLE16(src);
115 biPlanes = SDL_ReadLE16(src);
116 biBitCount = SDL_ReadLE16(src);
117 biCompression = BI_RGB;
124 biWidth = SDL_ReadLE32(src);
125 biHeight = SDL_ReadLE32(src);
126 biPlanes = SDL_ReadLE16(src);
127 biBitCount = SDL_ReadLE16(src);
128 biCompression = SDL_ReadLE32(src);
129 biSizeImage = SDL_ReadLE32(src);
130 biXPelsPerMeter = SDL_ReadLE32(src);
131 biYPelsPerMeter = SDL_ReadLE32(src);
132 biClrUsed = SDL_ReadLE32(src);
133 biClrImportant = SDL_ReadLE32(src);
136 /* stop some compiler warnings. */
142 (void) biXPelsPerMeter;
143 (void) biYPelsPerMeter;
144 (void) biClrImportant;
148 biHeight = -biHeight;
153 /* Check for read error */
154 if ( SDL_strcmp(SDL_GetError(), "") != 0 ) {
155 was_error = SDL_TRUE;
159 /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
160 switch (biBitCount) {
163 ExpandBMP = biBitCount;
171 /* We don't support any BMP compression right now */
172 Rmask = Gmask = Bmask = 0;
173 switch (biCompression) {
175 /* If there are no masks, use the defaults */
176 if ( bfOffBits == (14+biSize) ) {
177 /* Default values for the BMP format */
178 switch (biBitCount) {
186 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
202 /* Fall through -- read the RGB masks */
205 switch (biBitCount) {
209 Rmask = SDL_ReadLE32(src);
210 Gmask = SDL_ReadLE32(src);
211 Bmask = SDL_ReadLE32(src);
218 SDL_SetError("Compressed BMP files not supported");
219 was_error = SDL_TRUE;
223 /* Create a compatible surface, note that the colors are RGB ordered */
224 surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
225 biWidth, biHeight, biBitCount, Rmask, Gmask, Bmask, 0);
226 if ( surface == NULL ) {
227 was_error = SDL_TRUE;
231 /* Load the palette, if any */
232 palette = (surface->format)->palette;
234 if ( biClrUsed == 0 ) {
235 biClrUsed = 1 << biBitCount;
237 if ( biSize == 12 ) {
238 for ( i = 0; i < (int)biClrUsed; ++i ) {
239 SDL_RWread(src, &palette->colors[i].b, 1, 1);
240 SDL_RWread(src, &palette->colors[i].g, 1, 1);
241 SDL_RWread(src, &palette->colors[i].r, 1, 1);
242 palette->colors[i].unused = 0;
245 for ( i = 0; i < (int)biClrUsed; ++i ) {
246 SDL_RWread(src, &palette->colors[i].b, 1, 1);
247 SDL_RWread(src, &palette->colors[i].g, 1, 1);
248 SDL_RWread(src, &palette->colors[i].r, 1, 1);
249 SDL_RWread(src, &palette->colors[i].unused, 1, 1);
252 palette->ncolors = biClrUsed;
255 /* Read the surface pixels. Note that the bmp image is upside down */
256 if ( SDL_RWseek(src, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
257 SDL_Error(SDL_EFSEEK);
258 was_error = SDL_TRUE;
261 top = (Uint8 *)surface->pixels;
262 end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
265 bmpPitch = (biWidth + 7) >> 3;
266 pad = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
269 bmpPitch = (biWidth + 1) >> 1;
270 pad = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0);
273 pad = ((surface->pitch%4) ?
274 (4-(surface->pitch%4)) : 0);
280 bits = end - surface->pitch;
282 while ( bits >= top && bits < end ) {
287 int shift = (8-ExpandBMP);
288 for ( i=0; i<surface->w; ++i ) {
289 if ( i%(8/ExpandBMP) == 0 ) {
290 if ( !SDL_RWread(src, &pixel, 1, 1) ) {
292 "Error reading from BMP");
293 was_error = SDL_TRUE;
297 *(bits+i) = (pixel>>shift);
303 if ( SDL_RWread(src, bits, 1, surface->pitch)
304 != surface->pitch ) {
305 SDL_Error(SDL_EFREAD);
306 was_error = SDL_TRUE;
309 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
310 /* Byte-swap the pixels if needed. Note that the 24bpp
311 case has already been taken care of above. */
315 Uint16 *pix = (Uint16 *)bits;
316 for(i = 0; i < surface->w; i++)
317 pix[i] = SDL_Swap16(pix[i]);
322 Uint32 *pix = (Uint32 *)bits;
323 for(i = 0; i < surface->w; i++)
324 pix[i] = SDL_Swap32(pix[i]);
331 /* Skip padding bytes, ugh */
334 for ( i=0; i<pad; ++i ) {
335 SDL_RWread(src, &padbyte, 1, 1);
339 bits += surface->pitch;
341 bits -= surface->pitch;
347 SDL_RWseek(src, fp_offset, RW_SEEK_SET);
350 SDL_FreeSurface(surface);
354 if ( freesrc && src ) {
360 int SDL_SaveBMP_RW (SDL_Surface *saveme, SDL_RWops *dst, int freedst)
364 SDL_Surface *surface;
367 /* The Win32 BMP file header (14 bytes) */
368 char magic[2] = { 'B', 'M' };
374 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
380 Uint32 biCompression;
382 Sint32 biXPelsPerMeter;
383 Sint32 biYPelsPerMeter;
385 Uint32 biClrImportant;
387 /* Make sure we have somewhere to save */
390 if ( saveme->format->palette ) {
391 if ( saveme->format->BitsPerPixel == 8 ) {
394 SDL_SetError("%d bpp BMP files not supported",
395 saveme->format->BitsPerPixel);
398 else if ( (saveme->format->BitsPerPixel == 24) &&
399 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
400 (saveme->format->Rmask == 0x00FF0000) &&
401 (saveme->format->Gmask == 0x0000FF00) &&
402 (saveme->format->Bmask == 0x000000FF)
404 (saveme->format->Rmask == 0x000000FF) &&
405 (saveme->format->Gmask == 0x0000FF00) &&
406 (saveme->format->Bmask == 0x00FF0000)
413 /* Convert to 24 bits per pixel */
414 surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
415 saveme->w, saveme->h, 24,
416 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
417 0x00FF0000, 0x0000FF00, 0x000000FF,
419 0x000000FF, 0x0000FF00, 0x00FF0000,
422 if ( surface != NULL ) {
425 bounds.w = saveme->w;
426 bounds.h = saveme->h;
427 if ( SDL_LowerBlit(saveme, &bounds, surface,
429 SDL_FreeSurface(surface);
431 "Couldn't convert image to 24 bpp");
438 if ( surface && (SDL_LockSurface(surface) == 0) ) {
439 const int bw = surface->w*surface->format->BytesPerPixel;
441 /* Set the BMP file header values */
442 bfSize = 0; /* We'll write this when we're done */
445 bfOffBits = 0; /* We'll write this when we're done */
447 /* Write the BMP file header values */
448 fp_offset = SDL_RWtell(dst);
450 SDL_RWwrite(dst, magic, 2, 1);
451 SDL_WriteLE32(dst, bfSize);
452 SDL_WriteLE16(dst, bfReserved1);
453 SDL_WriteLE16(dst, bfReserved2);
454 SDL_WriteLE32(dst, bfOffBits);
456 /* Set the BMP info values */
458 biWidth = surface->w;
459 biHeight = surface->h;
461 biBitCount = surface->format->BitsPerPixel;
462 biCompression = BI_RGB;
463 biSizeImage = surface->h*surface->pitch;
466 if ( surface->format->palette ) {
467 biClrUsed = surface->format->palette->ncolors;
473 /* Write the BMP info values */
474 SDL_WriteLE32(dst, biSize);
475 SDL_WriteLE32(dst, biWidth);
476 SDL_WriteLE32(dst, biHeight);
477 SDL_WriteLE16(dst, biPlanes);
478 SDL_WriteLE16(dst, biBitCount);
479 SDL_WriteLE32(dst, biCompression);
480 SDL_WriteLE32(dst, biSizeImage);
481 SDL_WriteLE32(dst, biXPelsPerMeter);
482 SDL_WriteLE32(dst, biYPelsPerMeter);
483 SDL_WriteLE32(dst, biClrUsed);
484 SDL_WriteLE32(dst, biClrImportant);
486 /* Write the palette (in BGR color order) */
487 if ( surface->format->palette ) {
491 colors = surface->format->palette->colors;
492 ncolors = surface->format->palette->ncolors;
493 for ( i=0; i<ncolors; ++i ) {
494 SDL_RWwrite(dst, &colors[i].b, 1, 1);
495 SDL_RWwrite(dst, &colors[i].g, 1, 1);
496 SDL_RWwrite(dst, &colors[i].r, 1, 1);
497 SDL_RWwrite(dst, &colors[i].unused, 1, 1);
501 /* Write the bitmap offset */
502 bfOffBits = SDL_RWtell(dst)-fp_offset;
503 if ( SDL_RWseek(dst, fp_offset+10, RW_SEEK_SET) < 0 ) {
504 SDL_Error(SDL_EFSEEK);
506 SDL_WriteLE32(dst, bfOffBits);
507 if ( SDL_RWseek(dst, fp_offset+bfOffBits, RW_SEEK_SET) < 0 ) {
508 SDL_Error(SDL_EFSEEK);
511 /* Write the bitmap image upside down */
512 bits = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
513 pad = ((bw%4) ? (4-(bw%4)) : 0);
514 while ( bits > (Uint8 *)surface->pixels ) {
515 bits -= surface->pitch;
516 if ( SDL_RWwrite(dst, bits, 1, bw) != bw) {
517 SDL_Error(SDL_EFWRITE);
521 const Uint8 padbyte = 0;
522 for ( i=0; i<pad; ++i ) {
523 SDL_RWwrite(dst, &padbyte, 1, 1);
528 /* Write the BMP file size */
529 bfSize = SDL_RWtell(dst)-fp_offset;
530 if ( SDL_RWseek(dst, fp_offset+2, RW_SEEK_SET) < 0 ) {
531 SDL_Error(SDL_EFSEEK);
533 SDL_WriteLE32(dst, bfSize);
534 if ( SDL_RWseek(dst, fp_offset+bfSize, RW_SEEK_SET) < 0 ) {
535 SDL_Error(SDL_EFSEEK);
539 SDL_UnlockSurface(surface);
540 if ( surface != saveme ) {
541 SDL_FreeSurface(surface);
545 if ( freedst && dst ) {
548 return((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);