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