2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2006 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
)
62 /* The Win32 BMP file header (14 bytes) */
69 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
77 Sint32 biXPelsPerMeter
;
78 Sint32 biYPelsPerMeter
;
80 Uint32 biClrImportant
;
82 /* Make sure we are passed a valid data source */
90 /* Read in the BMP file header */
91 fp_offset
= SDL_RWtell(src
);
93 if ( SDL_RWread(src
, magic
, 1, 2) != 2 ) {
94 SDL_Error(SDL_EFREAD
);
98 if ( SDL_strncmp(magic
, "BM", 2) != 0 ) {
99 SDL_SetError("File is not a Windows BMP file");
103 bfSize
= SDL_ReadLE32(src
);
104 bfReserved1
= SDL_ReadLE16(src
);
105 bfReserved2
= SDL_ReadLE16(src
);
106 bfOffBits
= SDL_ReadLE32(src
);
108 /* Read the Win32 BITMAPINFOHEADER */
109 biSize
= SDL_ReadLE32(src
);
110 if ( biSize
== 12 ) {
111 biWidth
= (Uint32
)SDL_ReadLE16(src
);
112 biHeight
= (Uint32
)SDL_ReadLE16(src
);
113 biPlanes
= SDL_ReadLE16(src
);
114 biBitCount
= SDL_ReadLE16(src
);
115 biCompression
= BI_RGB
;
122 biWidth
= SDL_ReadLE32(src
);
123 biHeight
= SDL_ReadLE32(src
);
124 biPlanes
= SDL_ReadLE16(src
);
125 biBitCount
= SDL_ReadLE16(src
);
126 biCompression
= SDL_ReadLE32(src
);
127 biSizeImage
= SDL_ReadLE32(src
);
128 biXPelsPerMeter
= SDL_ReadLE32(src
);
129 biYPelsPerMeter
= SDL_ReadLE32(src
);
130 biClrUsed
= SDL_ReadLE32(src
);
131 biClrImportant
= SDL_ReadLE32(src
);
134 /* Check for read error */
135 if ( SDL_strcmp(SDL_GetError(), "") != 0 ) {
140 /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
141 switch (biBitCount
) {
144 ExpandBMP
= biBitCount
;
152 /* We don't support any BMP compression right now */
153 Rmask
= Gmask
= Bmask
= 0;
154 switch (biCompression
) {
156 /* If there are no masks, use the defaults */
157 if ( bfOffBits
== (14+biSize
) ) {
158 /* Default values for the BMP format */
159 switch (biBitCount
) {
167 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
183 /* Fall through -- read the RGB masks */
186 switch (biBitCount
) {
190 Rmask
= SDL_ReadLE32(src
);
191 Gmask
= SDL_ReadLE32(src
);
192 Bmask
= SDL_ReadLE32(src
);
199 SDL_SetError("Compressed BMP files not supported");
204 /* Create a compatible surface, note that the colors are RGB ordered */
205 surface
= SDL_CreateRGBSurface(SDL_SWSURFACE
,
206 biWidth
, biHeight
, biBitCount
, Rmask
, Gmask
, Bmask
, 0);
207 if ( surface
== NULL
) {
212 /* Load the palette, if any */
213 palette
= (surface
->format
)->palette
;
215 if ( biClrUsed
== 0 ) {
216 biClrUsed
= 1 << biBitCount
;
218 if ( biSize
== 12 ) {
219 for ( i
= 0; i
< (int)biClrUsed
; ++i
) {
220 SDL_RWread(src
, &palette
->colors
[i
].b
, 1, 1);
221 SDL_RWread(src
, &palette
->colors
[i
].g
, 1, 1);
222 SDL_RWread(src
, &palette
->colors
[i
].r
, 1, 1);
223 palette
->colors
[i
].unused
= 0;
226 for ( i
= 0; i
< (int)biClrUsed
; ++i
) {
227 SDL_RWread(src
, &palette
->colors
[i
].b
, 1, 1);
228 SDL_RWread(src
, &palette
->colors
[i
].g
, 1, 1);
229 SDL_RWread(src
, &palette
->colors
[i
].r
, 1, 1);
230 SDL_RWread(src
, &palette
->colors
[i
].unused
, 1, 1);
233 palette
->ncolors
= biClrUsed
;
236 /* Read the surface pixels. Note that the bmp image is upside down */
237 if ( SDL_RWseek(src
, fp_offset
+bfOffBits
, RW_SEEK_SET
) < 0 ) {
238 SDL_Error(SDL_EFSEEK
);
242 bits
= (Uint8
*)surface
->pixels
+(surface
->h
*surface
->pitch
);
245 bmpPitch
= (biWidth
+ 7) >> 3;
246 pad
= (((bmpPitch
)%4) ? (4-((bmpPitch
)%4)) : 0);
249 bmpPitch
= (biWidth
+ 1) >> 1;
250 pad
= (((bmpPitch
)%4) ? (4-((bmpPitch
)%4)) : 0);
253 pad
= ((surface
->pitch
%4) ?
254 (4-(surface
->pitch
%4)) : 0);
257 while ( bits
> (Uint8
*)surface
->pixels
) {
258 bits
-= surface
->pitch
;
263 int shift
= (8-ExpandBMP
);
264 for ( i
=0; i
<surface
->w
; ++i
) {
265 if ( i
%(8/ExpandBMP
) == 0 ) {
266 if ( !SDL_RWread(src
, &pixel
, 1, 1) ) {
268 "Error reading from BMP");
273 *(bits
+i
) = (pixel
>>shift
);
279 if ( SDL_RWread(src
, bits
, 1, surface
->pitch
)
280 != surface
->pitch
) {
281 SDL_Error(SDL_EFREAD
);
285 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
286 /* Byte-swap the pixels if needed. Note that the 24bpp
287 case has already been taken care of above. */
291 Uint16
*pix
= (Uint16
*)bits
;
292 for(i
= 0; i
< surface
->w
; i
++)
293 pix
[i
] = SDL_Swap16(pix
[i
]);
298 Uint32
*pix
= (Uint32
*)bits
;
299 for(i
= 0; i
< surface
->w
; i
++)
300 pix
[i
] = SDL_Swap32(pix
[i
]);
307 /* Skip padding bytes, ugh */
310 for ( i
=0; i
<pad
; ++i
) {
311 SDL_RWread(src
, &padbyte
, 1, 1);
318 SDL_RWseek(src
, fp_offset
, RW_SEEK_SET
);
321 SDL_FreeSurface(surface
);
325 if ( freesrc
&& src
) {
331 int SDL_SaveBMP_RW (SDL_Surface
*saveme
, SDL_RWops
*dst
, int freedst
)
335 SDL_Surface
*surface
;
338 /* The Win32 BMP file header (14 bytes) */
339 char magic
[2] = { 'B', 'M' };
345 /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
351 Uint32 biCompression
;
353 Sint32 biXPelsPerMeter
;
354 Sint32 biYPelsPerMeter
;
356 Uint32 biClrImportant
;
358 /* Make sure we have somewhere to save */
361 if ( saveme
->format
->palette
) {
362 if ( saveme
->format
->BitsPerPixel
== 8 ) {
365 SDL_SetError("%d bpp BMP files not supported",
366 saveme
->format
->BitsPerPixel
);
369 else if ( (saveme
->format
->BitsPerPixel
== 24) &&
370 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
371 (saveme
->format
->Rmask
== 0x00FF0000) &&
372 (saveme
->format
->Gmask
== 0x0000FF00) &&
373 (saveme
->format
->Bmask
== 0x000000FF)
375 (saveme
->format
->Rmask
== 0x000000FF) &&
376 (saveme
->format
->Gmask
== 0x0000FF00) &&
377 (saveme
->format
->Bmask
== 0x00FF0000)
384 /* Convert to 24 bits per pixel */
385 surface
= SDL_CreateRGBSurface(SDL_SWSURFACE
,
386 saveme
->w
, saveme
->h
, 24,
387 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
388 0x00FF0000, 0x0000FF00, 0x000000FF,
390 0x000000FF, 0x0000FF00, 0x00FF0000,
393 if ( surface
!= NULL
) {
396 bounds
.w
= saveme
->w
;
397 bounds
.h
= saveme
->h
;
398 if ( SDL_LowerBlit(saveme
, &bounds
, surface
,
400 SDL_FreeSurface(surface
);
402 "Couldn't convert image to 24 bpp");
409 if ( surface
&& (SDL_LockSurface(surface
) == 0) ) {
410 const int bw
= surface
->w
*surface
->format
->BytesPerPixel
;
412 /* Set the BMP file header values */
413 bfSize
= 0; /* We'll write this when we're done */
416 bfOffBits
= 0; /* We'll write this when we're done */
418 /* Write the BMP file header values */
419 fp_offset
= SDL_RWtell(dst
);
421 SDL_RWwrite(dst
, magic
, 2, 1);
422 SDL_WriteLE32(dst
, bfSize
);
423 SDL_WriteLE16(dst
, bfReserved1
);
424 SDL_WriteLE16(dst
, bfReserved2
);
425 SDL_WriteLE32(dst
, bfOffBits
);
427 /* Set the BMP info values */
429 biWidth
= surface
->w
;
430 biHeight
= surface
->h
;
432 biBitCount
= surface
->format
->BitsPerPixel
;
433 biCompression
= BI_RGB
;
434 biSizeImage
= surface
->h
*surface
->pitch
;
437 if ( surface
->format
->palette
) {
438 biClrUsed
= surface
->format
->palette
->ncolors
;
444 /* Write the BMP info values */
445 SDL_WriteLE32(dst
, biSize
);
446 SDL_WriteLE32(dst
, biWidth
);
447 SDL_WriteLE32(dst
, biHeight
);
448 SDL_WriteLE16(dst
, biPlanes
);
449 SDL_WriteLE16(dst
, biBitCount
);
450 SDL_WriteLE32(dst
, biCompression
);
451 SDL_WriteLE32(dst
, biSizeImage
);
452 SDL_WriteLE32(dst
, biXPelsPerMeter
);
453 SDL_WriteLE32(dst
, biYPelsPerMeter
);
454 SDL_WriteLE32(dst
, biClrUsed
);
455 SDL_WriteLE32(dst
, biClrImportant
);
457 /* Write the palette (in BGR color order) */
458 if ( surface
->format
->palette
) {
462 colors
= surface
->format
->palette
->colors
;
463 ncolors
= surface
->format
->palette
->ncolors
;
464 for ( i
=0; i
<ncolors
; ++i
) {
465 SDL_RWwrite(dst
, &colors
[i
].b
, 1, 1);
466 SDL_RWwrite(dst
, &colors
[i
].g
, 1, 1);
467 SDL_RWwrite(dst
, &colors
[i
].r
, 1, 1);
468 SDL_RWwrite(dst
, &colors
[i
].unused
, 1, 1);
472 /* Write the bitmap offset */
473 bfOffBits
= SDL_RWtell(dst
)-fp_offset
;
474 if ( SDL_RWseek(dst
, fp_offset
+10, RW_SEEK_SET
) < 0 ) {
475 SDL_Error(SDL_EFSEEK
);
477 SDL_WriteLE32(dst
, bfOffBits
);
478 if ( SDL_RWseek(dst
, fp_offset
+bfOffBits
, RW_SEEK_SET
) < 0 ) {
479 SDL_Error(SDL_EFSEEK
);
482 /* Write the bitmap image upside down */
483 bits
= (Uint8
*)surface
->pixels
+(surface
->h
*surface
->pitch
);
484 pad
= ((bw
%4) ? (4-(bw
%4)) : 0);
485 while ( bits
> (Uint8
*)surface
->pixels
) {
486 bits
-= surface
->pitch
;
487 if ( SDL_RWwrite(dst
, bits
, 1, bw
) != bw
) {
488 SDL_Error(SDL_EFWRITE
);
492 const Uint8 padbyte
= 0;
493 for ( i
=0; i
<pad
; ++i
) {
494 SDL_RWwrite(dst
, &padbyte
, 1, 1);
499 /* Write the BMP file size */
500 bfSize
= SDL_RWtell(dst
)-fp_offset
;
501 if ( SDL_RWseek(dst
, fp_offset
+2, RW_SEEK_SET
) < 0 ) {
502 SDL_Error(SDL_EFSEEK
);
504 SDL_WriteLE32(dst
, bfSize
);
505 if ( SDL_RWseek(dst
, fp_offset
+bfSize
, RW_SEEK_SET
) < 0 ) {
506 SDL_Error(SDL_EFSEEK
);
510 SDL_UnlockSurface(surface
);
511 if ( surface
!= saveme
) {
512 SDL_FreeSurface(surface
);
516 if ( freedst
&& dst
) {
519 return((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);