SDL_bmp.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /*
  20. Code to load and save surfaces in Windows BMP format.
  21. Why support BMP format? Well, it's a native format for Windows, and
  22. most image processing programs can read and write it. It would be nice
  23. to be able to have at least one image format that we can natively load
  24. and save, and since PNG is so complex that it would bloat the library,
  25. BMP is a good alternative.
  26. This code currently supports Win32 DIBs in uncompressed 8 and 24 bpp.
  27. */
  28. #include "SDL_hints.h"
  29. #include "SDL_video.h"
  30. #include "SDL_assert.h"
  31. #include "SDL_endian.h"
  32. #include "SDL_pixels_c.h"
  33. #define SAVE_32BIT_BMP
  34. /* Compression encodings for BMP files */
  35. #ifndef BI_RGB
  36. #define BI_RGB 0
  37. #define BI_RLE8 1
  38. #define BI_RLE4 2
  39. #define BI_BITFIELDS 3
  40. #endif
  41. /* Logical color space values for BMP files */
  42. #ifndef LCS_WINDOWS_COLOR_SPACE
  43. /* 0x57696E20 == "Win " */
  44. #define LCS_WINDOWS_COLOR_SPACE 0x57696E20
  45. #endif
  46. static int readRlePixels(SDL_Surface * surface, SDL_RWops * src, int isRle8)
  47. {
  48. /*
  49. | Sets the surface pixels from src. A bmp image is upside down.
  50. */
  51. int pitch = surface->pitch;
  52. int height = surface->h;
  53. Uint8 *start = (Uint8 *)surface->pixels;
  54. Uint8 *end = start + (height*pitch);
  55. Uint8 *bits = end-pitch, *spot;
  56. int ofs = 0;
  57. Uint8 ch;
  58. Uint8 needsPad;
  59. #define COPY_PIXEL(x) spot = &bits[ofs++]; if(spot >= start && spot < end) *spot = (x)
  60. for (;;) {
  61. if (!SDL_RWread(src, &ch, 1, 1)) return 1;
  62. /*
  63. | encoded mode starts with a run length, and then a byte
  64. | with two colour indexes to alternate between for the run
  65. */
  66. if (ch) {
  67. Uint8 pixel;
  68. if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
  69. if (isRle8) { /* 256-color bitmap, compressed */
  70. do {
  71. COPY_PIXEL(pixel);
  72. } while (--ch);
  73. } else { /* 16-color bitmap, compressed */
  74. Uint8 pixel0 = pixel >> 4;
  75. Uint8 pixel1 = pixel & 0x0F;
  76. for (;;) {
  77. COPY_PIXEL(pixel0); /* even count, high nibble */
  78. if (!--ch) break;
  79. COPY_PIXEL(pixel1); /* odd count, low nibble */
  80. if (!--ch) break;
  81. }
  82. }
  83. } else {
  84. /*
  85. | A leading zero is an escape; it may signal the end of the bitmap,
  86. | a cursor move, or some absolute data.
  87. | zero tag may be absolute mode or an escape
  88. */
  89. if (!SDL_RWread(src, &ch, 1, 1)) return 1;
  90. switch (ch) {
  91. case 0: /* end of line */
  92. ofs = 0;
  93. bits -= pitch; /* go to previous */
  94. break;
  95. case 1: /* end of bitmap */
  96. return 0; /* success! */
  97. case 2: /* delta */
  98. if (!SDL_RWread(src, &ch, 1, 1)) return 1;
  99. ofs += ch;
  100. if (!SDL_RWread(src, &ch, 1, 1)) return 1;
  101. bits -= (ch * pitch);
  102. break;
  103. default: /* no compression */
  104. if (isRle8) {
  105. needsPad = (ch & 1);
  106. do {
  107. Uint8 pixel;
  108. if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
  109. COPY_PIXEL(pixel);
  110. } while (--ch);
  111. } else {
  112. needsPad = (((ch+1)>>1) & 1); /* (ch+1)>>1: bytes size */
  113. for (;;) {
  114. Uint8 pixel;
  115. if (!SDL_RWread(src, &pixel, 1, 1)) return 1;
  116. COPY_PIXEL(pixel >> 4);
  117. if (!--ch) break;
  118. COPY_PIXEL(pixel & 0x0F);
  119. if (!--ch) break;
  120. }
  121. }
  122. /* pad at even boundary */
  123. if (needsPad && !SDL_RWread(src, &ch, 1, 1)) return 1;
  124. break;
  125. }
  126. }
  127. }
  128. }
  129. static void CorrectAlphaChannel(SDL_Surface *surface)
  130. {
  131. /* Check to see if there is any alpha channel data */
  132. SDL_bool hasAlpha = SDL_FALSE;
  133. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  134. int alphaChannelOffset = 0;
  135. #else
  136. int alphaChannelOffset = 3;
  137. #endif
  138. Uint8 *alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
  139. Uint8 *end = alpha + surface->h * surface->pitch;
  140. while (alpha < end) {
  141. if (*alpha != 0) {
  142. hasAlpha = SDL_TRUE;
  143. break;
  144. }
  145. alpha += 4;
  146. }
  147. if (!hasAlpha) {
  148. alpha = ((Uint8*)surface->pixels) + alphaChannelOffset;
  149. while (alpha < end) {
  150. *alpha = SDL_ALPHA_OPAQUE;
  151. alpha += 4;
  152. }
  153. }
  154. }
  155. SDL_Surface *
  156. SDL_LoadBMP_RW(SDL_RWops * src, int freesrc)
  157. {
  158. SDL_bool was_error;
  159. Sint64 fp_offset = 0;
  160. int bmpPitch;
  161. int i, pad;
  162. SDL_Surface *surface;
  163. Uint32 Rmask = 0;
  164. Uint32 Gmask = 0;
  165. Uint32 Bmask = 0;
  166. Uint32 Amask = 0;
  167. SDL_Palette *palette;
  168. Uint8 *bits;
  169. Uint8 *top, *end;
  170. SDL_bool topDown;
  171. int ExpandBMP;
  172. SDL_bool haveRGBMasks = SDL_FALSE;
  173. SDL_bool haveAlphaMask = SDL_FALSE;
  174. SDL_bool correctAlpha = SDL_FALSE;
  175. /* The Win32 BMP file header (14 bytes) */
  176. char magic[2];
  177. /* Uint32 bfSize; */
  178. /* Uint16 bfReserved1; */
  179. /* Uint16 bfReserved2; */
  180. Uint32 bfOffBits;
  181. /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
  182. Uint32 biSize;
  183. Sint32 biWidth = 0;
  184. Sint32 biHeight = 0;
  185. /* Uint16 biPlanes; */
  186. Uint16 biBitCount = 0;
  187. Uint32 biCompression = 0;
  188. /* Uint32 biSizeImage; */
  189. /* Sint32 biXPelsPerMeter; */
  190. /* Sint32 biYPelsPerMeter; */
  191. Uint32 biClrUsed = 0;
  192. /* Uint32 biClrImportant; */
  193. /* Make sure we are passed a valid data source */
  194. surface = NULL;
  195. was_error = SDL_FALSE;
  196. if (src == NULL) {
  197. was_error = SDL_TRUE;
  198. goto done;
  199. }
  200. /* Read in the BMP file header */
  201. fp_offset = SDL_RWtell(src);
  202. SDL_ClearError();
  203. if (SDL_RWread(src, magic, 1, 2) != 2) {
  204. SDL_Error(SDL_EFREAD);
  205. was_error = SDL_TRUE;
  206. goto done;
  207. }
  208. if (SDL_strncmp(magic, "BM", 2) != 0) {
  209. SDL_SetError("File is not a Windows BMP file");
  210. was_error = SDL_TRUE;
  211. goto done;
  212. }
  213. /* bfSize = */ SDL_ReadLE32(src);
  214. /* bfReserved1 = */ SDL_ReadLE16(src);
  215. /* bfReserved2 = */ SDL_ReadLE16(src);
  216. bfOffBits = SDL_ReadLE32(src);
  217. /* Read the Win32 BITMAPINFOHEADER */
  218. biSize = SDL_ReadLE32(src);
  219. if (biSize == 12) { /* really old BITMAPCOREHEADER */
  220. biWidth = (Uint32) SDL_ReadLE16(src);
  221. biHeight = (Uint32) SDL_ReadLE16(src);
  222. /* biPlanes = */ SDL_ReadLE16(src);
  223. biBitCount = SDL_ReadLE16(src);
  224. biCompression = BI_RGB;
  225. /* biSizeImage = 0; */
  226. /* biXPelsPerMeter = 0; */
  227. /* biYPelsPerMeter = 0; */
  228. biClrUsed = 0;
  229. /* biClrImportant = 0; */
  230. } else if (biSize >= 40) { /* some version of BITMAPINFOHEADER */
  231. Uint32 headerSize;
  232. biWidth = SDL_ReadLE32(src);
  233. biHeight = SDL_ReadLE32(src);
  234. /* biPlanes = */ SDL_ReadLE16(src);
  235. biBitCount = SDL_ReadLE16(src);
  236. biCompression = SDL_ReadLE32(src);
  237. /* biSizeImage = */ SDL_ReadLE32(src);
  238. /* biXPelsPerMeter = */ SDL_ReadLE32(src);
  239. /* biYPelsPerMeter = */ SDL_ReadLE32(src);
  240. biClrUsed = SDL_ReadLE32(src);
  241. /* biClrImportant = */ SDL_ReadLE32(src);
  242. /* 64 == BITMAPCOREHEADER2, an incompatible OS/2 2.x extension. Skip this stuff for now. */
  243. if (biSize != 64) {
  244. /* This is complicated. If compression is BI_BITFIELDS, then
  245. we have 3 DWORDS that specify the RGB masks. This is either
  246. stored here in an BITMAPV2INFOHEADER (which only differs in
  247. that it adds these RGB masks) and biSize >= 52, or we've got
  248. these masks stored in the exact same place, but strictly
  249. speaking, this is the bmiColors field in BITMAPINFO immediately
  250. following the legacy v1 info header, just past biSize. */
  251. if (biCompression == BI_BITFIELDS) {
  252. haveRGBMasks = SDL_TRUE;
  253. Rmask = SDL_ReadLE32(src);
  254. Gmask = SDL_ReadLE32(src);
  255. Bmask = SDL_ReadLE32(src);
  256. /* ...v3 adds an alpha mask. */
  257. if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
  258. haveAlphaMask = SDL_TRUE;
  259. Amask = SDL_ReadLE32(src);
  260. }
  261. } else {
  262. /* the mask fields are ignored for v2+ headers if not BI_BITFIELD. */
  263. if (biSize >= 52) { /* BITMAPV2INFOHEADER; adds RGB masks */
  264. /*Rmask = */ SDL_ReadLE32(src);
  265. /*Gmask = */ SDL_ReadLE32(src);
  266. /*Bmask = */ SDL_ReadLE32(src);
  267. }
  268. if (biSize >= 56) { /* BITMAPV3INFOHEADER; adds alpha mask */
  269. /*Amask = */ SDL_ReadLE32(src);
  270. }
  271. }
  272. /* Insert other fields here; Wikipedia and MSDN say we're up to
  273. v5 of this header, but we ignore those for now (they add gamma,
  274. color spaces, etc). Ignoring the weird OS/2 2.x format, we
  275. currently parse up to v3 correctly (hopefully!). */
  276. }
  277. /* skip any header bytes we didn't handle... */
  278. headerSize = (Uint32) (SDL_RWtell(src) - (fp_offset + 14));
  279. if (biSize > headerSize) {
  280. SDL_RWseek(src, (biSize - headerSize), RW_SEEK_CUR);
  281. }
  282. }
  283. if (biWidth <= 0 || biHeight == 0) {
  284. SDL_SetError("BMP file with bad dimensions (%dx%d)", biWidth, biHeight);
  285. was_error = SDL_TRUE;
  286. goto done;
  287. }
  288. if (biHeight < 0) {
  289. topDown = SDL_TRUE;
  290. biHeight = -biHeight;
  291. } else {
  292. topDown = SDL_FALSE;
  293. }
  294. /* Check for read error */
  295. if (SDL_strcmp(SDL_GetError(), "") != 0) {
  296. was_error = SDL_TRUE;
  297. goto done;
  298. }
  299. /* Expand 1 and 4 bit bitmaps to 8 bits per pixel */
  300. switch (biBitCount) {
  301. case 1:
  302. case 4:
  303. ExpandBMP = biBitCount;
  304. biBitCount = 8;
  305. break;
  306. case 0:
  307. case 2:
  308. case 3:
  309. case 5:
  310. case 6:
  311. case 7:
  312. SDL_SetError("%d-bpp BMP images are not supported", biBitCount);
  313. was_error = SDL_TRUE;
  314. goto done;
  315. default:
  316. ExpandBMP = 0;
  317. break;
  318. }
  319. /* RLE4 and RLE8 BMP compression is supported */
  320. switch (biCompression) {
  321. case BI_RGB:
  322. /* If there are no masks, use the defaults */
  323. SDL_assert(!haveRGBMasks);
  324. SDL_assert(!haveAlphaMask);
  325. /* Default values for the BMP format */
  326. switch (biBitCount) {
  327. case 15:
  328. case 16:
  329. Rmask = 0x7C00;
  330. Gmask = 0x03E0;
  331. Bmask = 0x001F;
  332. break;
  333. case 24:
  334. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  335. Rmask = 0x000000FF;
  336. Gmask = 0x0000FF00;
  337. Bmask = 0x00FF0000;
  338. #else
  339. Rmask = 0x00FF0000;
  340. Gmask = 0x0000FF00;
  341. Bmask = 0x000000FF;
  342. #endif
  343. break;
  344. case 32:
  345. /* We don't know if this has alpha channel or not */
  346. correctAlpha = SDL_TRUE;
  347. Amask = 0xFF000000;
  348. Rmask = 0x00FF0000;
  349. Gmask = 0x0000FF00;
  350. Bmask = 0x000000FF;
  351. break;
  352. default:
  353. break;
  354. }
  355. break;
  356. case BI_BITFIELDS:
  357. break; /* we handled this in the info header. */
  358. default:
  359. break;
  360. }
  361. /* Create a compatible surface, note that the colors are RGB ordered */
  362. surface =
  363. SDL_CreateRGBSurface(0, biWidth, biHeight, biBitCount, Rmask, Gmask,
  364. Bmask, Amask);
  365. if (surface == NULL) {
  366. was_error = SDL_TRUE;
  367. goto done;
  368. }
  369. /* Load the palette, if any */
  370. palette = (surface->format)->palette;
  371. if (palette) {
  372. if (SDL_RWseek(src, fp_offset+14+biSize, RW_SEEK_SET) < 0) {
  373. SDL_Error(SDL_EFSEEK);
  374. was_error = SDL_TRUE;
  375. goto done;
  376. }
  377. /*
  378. | guich: always use 1<<bpp b/c some bitmaps can bring wrong information
  379. | for colorsUsed
  380. */
  381. /* if (biClrUsed == 0) { */
  382. biClrUsed = 1 << biBitCount;
  383. /* } */
  384. if (biSize == 12) {
  385. for (i = 0; i < (int) biClrUsed; ++i) {
  386. SDL_RWread(src, &palette->colors[i].b, 1, 1);
  387. SDL_RWread(src, &palette->colors[i].g, 1, 1);
  388. SDL_RWread(src, &palette->colors[i].r, 1, 1);
  389. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  390. }
  391. } else {
  392. for (i = 0; i < (int) biClrUsed; ++i) {
  393. SDL_RWread(src, &palette->colors[i].b, 1, 1);
  394. SDL_RWread(src, &palette->colors[i].g, 1, 1);
  395. SDL_RWread(src, &palette->colors[i].r, 1, 1);
  396. SDL_RWread(src, &palette->colors[i].a, 1, 1);
  397. /* According to Microsoft documentation, the fourth element
  398. is reserved and must be zero, so we shouldn't treat it as
  399. alpha.
  400. */
  401. palette->colors[i].a = SDL_ALPHA_OPAQUE;
  402. }
  403. }
  404. palette->ncolors = biClrUsed;
  405. }
  406. /* Read the surface pixels. Note that the bmp image is upside down */
  407. if (SDL_RWseek(src, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
  408. SDL_Error(SDL_EFSEEK);
  409. was_error = SDL_TRUE;
  410. goto done;
  411. }
  412. if ((biCompression == BI_RLE4) || (biCompression == BI_RLE8)) {
  413. was_error = (SDL_bool)readRlePixels(surface, src, biCompression == BI_RLE8);
  414. if (was_error) SDL_SetError("Error reading from BMP");
  415. goto done;
  416. }
  417. top = (Uint8 *)surface->pixels;
  418. end = (Uint8 *)surface->pixels+(surface->h*surface->pitch);
  419. switch (ExpandBMP) {
  420. case 1:
  421. bmpPitch = (biWidth + 7) >> 3;
  422. pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
  423. break;
  424. case 4:
  425. bmpPitch = (biWidth + 1) >> 1;
  426. pad = (((bmpPitch) % 4) ? (4 - ((bmpPitch) % 4)) : 0);
  427. break;
  428. default:
  429. pad = ((surface->pitch % 4) ? (4 - (surface->pitch % 4)) : 0);
  430. break;
  431. }
  432. if (topDown) {
  433. bits = top;
  434. } else {
  435. bits = end - surface->pitch;
  436. }
  437. while (bits >= top && bits < end) {
  438. switch (ExpandBMP) {
  439. case 1:
  440. case 4:{
  441. Uint8 pixel = 0;
  442. int shift = (8 - ExpandBMP);
  443. for (i = 0; i < surface->w; ++i) {
  444. if (i % (8 / ExpandBMP) == 0) {
  445. if (!SDL_RWread(src, &pixel, 1, 1)) {
  446. SDL_SetError("Error reading from BMP");
  447. was_error = SDL_TRUE;
  448. goto done;
  449. }
  450. }
  451. bits[i] = (pixel >> shift);
  452. if (bits[i] >= biClrUsed) {
  453. SDL_SetError("A BMP image contains a pixel with a color out of the palette");
  454. was_error = SDL_TRUE;
  455. goto done;
  456. }
  457. pixel <<= ExpandBMP;
  458. }
  459. }
  460. break;
  461. default:
  462. if (SDL_RWread(src, bits, 1, surface->pitch) != surface->pitch) {
  463. SDL_Error(SDL_EFREAD);
  464. was_error = SDL_TRUE;
  465. goto done;
  466. }
  467. if (biBitCount == 8 && palette && biClrUsed < (1u << biBitCount)) {
  468. for (i = 0; i < surface->w; ++i) {
  469. if (bits[i] >= biClrUsed) {
  470. SDL_SetError("A BMP image contains a pixel with a color out of the palette");
  471. was_error = SDL_TRUE;
  472. goto done;
  473. }
  474. }
  475. }
  476. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  477. /* Byte-swap the pixels if needed. Note that the 24bpp
  478. case has already been taken care of above. */
  479. switch (biBitCount) {
  480. case 15:
  481. case 16:{
  482. Uint16 *pix = (Uint16 *) bits;
  483. for (i = 0; i < surface->w; i++)
  484. pix[i] = SDL_Swap16(pix[i]);
  485. break;
  486. }
  487. case 32:{
  488. Uint32 *pix = (Uint32 *) bits;
  489. for (i = 0; i < surface->w; i++)
  490. pix[i] = SDL_Swap32(pix[i]);
  491. break;
  492. }
  493. }
  494. #endif
  495. break;
  496. }
  497. /* Skip padding bytes, ugh */
  498. if (pad) {
  499. Uint8 padbyte;
  500. for (i = 0; i < pad; ++i) {
  501. SDL_RWread(src, &padbyte, 1, 1);
  502. }
  503. }
  504. if (topDown) {
  505. bits += surface->pitch;
  506. } else {
  507. bits -= surface->pitch;
  508. }
  509. }
  510. if (correctAlpha) {
  511. CorrectAlphaChannel(surface);
  512. }
  513. done:
  514. if (was_error) {
  515. if (src) {
  516. SDL_RWseek(src, fp_offset, RW_SEEK_SET);
  517. }
  518. if (surface) {
  519. SDL_FreeSurface(surface);
  520. }
  521. surface = NULL;
  522. }
  523. if (freesrc && src) {
  524. SDL_RWclose(src);
  525. }
  526. return (surface);
  527. }
  528. int
  529. SDL_SaveBMP_RW(SDL_Surface * saveme, SDL_RWops * dst, int freedst)
  530. {
  531. Sint64 fp_offset;
  532. int i, pad;
  533. SDL_Surface *surface;
  534. Uint8 *bits;
  535. SDL_bool save32bit = SDL_FALSE;
  536. SDL_bool saveLegacyBMP = SDL_FALSE;
  537. /* The Win32 BMP file header (14 bytes) */
  538. char magic[2] = { 'B', 'M' };
  539. Uint32 bfSize;
  540. Uint16 bfReserved1;
  541. Uint16 bfReserved2;
  542. Uint32 bfOffBits;
  543. /* The Win32 BITMAPINFOHEADER struct (40 bytes) */
  544. Uint32 biSize;
  545. Sint32 biWidth;
  546. Sint32 biHeight;
  547. Uint16 biPlanes;
  548. Uint16 biBitCount;
  549. Uint32 biCompression;
  550. Uint32 biSizeImage;
  551. Sint32 biXPelsPerMeter;
  552. Sint32 biYPelsPerMeter;
  553. Uint32 biClrUsed;
  554. Uint32 biClrImportant;
  555. /* The additional header members from the Win32 BITMAPV4HEADER struct (108 bytes in total) */
  556. Uint32 bV4RedMask = 0;
  557. Uint32 bV4GreenMask = 0;
  558. Uint32 bV4BlueMask = 0;
  559. Uint32 bV4AlphaMask = 0;
  560. Uint32 bV4CSType = 0;
  561. Sint32 bV4Endpoints[3 * 3] = {0};
  562. Uint32 bV4GammaRed = 0;
  563. Uint32 bV4GammaGreen = 0;
  564. Uint32 bV4GammaBlue = 0;
  565. /* Make sure we have somewhere to save */
  566. surface = NULL;
  567. if (dst) {
  568. #ifdef SAVE_32BIT_BMP
  569. /* We can save alpha information in a 32-bit BMP */
  570. if (saveme->format->BitsPerPixel >= 8 && (saveme->format->Amask ||
  571. saveme->map->info.flags & SDL_COPY_COLORKEY)) {
  572. save32bit = SDL_TRUE;
  573. }
  574. #endif /* SAVE_32BIT_BMP */
  575. if (saveme->format->palette && !save32bit) {
  576. if (saveme->format->BitsPerPixel == 8) {
  577. surface = saveme;
  578. } else {
  579. SDL_SetError("%d bpp BMP files not supported",
  580. saveme->format->BitsPerPixel);
  581. }
  582. } else if ((saveme->format->BitsPerPixel == 24) && !save32bit &&
  583. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  584. (saveme->format->Rmask == 0x00FF0000) &&
  585. (saveme->format->Gmask == 0x0000FF00) &&
  586. (saveme->format->Bmask == 0x000000FF)
  587. #else
  588. (saveme->format->Rmask == 0x000000FF) &&
  589. (saveme->format->Gmask == 0x0000FF00) &&
  590. (saveme->format->Bmask == 0x00FF0000)
  591. #endif
  592. ) {
  593. surface = saveme;
  594. } else {
  595. SDL_PixelFormat format;
  596. /* If the surface has a colorkey or alpha channel we'll save a
  597. 32-bit BMP with alpha channel, otherwise save a 24-bit BMP. */
  598. if (save32bit) {
  599. SDL_InitFormat(&format, SDL_PIXELFORMAT_BGRA32);
  600. } else {
  601. SDL_InitFormat(&format, SDL_PIXELFORMAT_BGR24);
  602. }
  603. surface = SDL_ConvertSurface(saveme, &format, 0);
  604. if (!surface) {
  605. SDL_SetError("Couldn't convert image to %d bpp",
  606. format.BitsPerPixel);
  607. }
  608. }
  609. } else {
  610. /* Set no error here because it may overwrite a more useful message from
  611. SDL_RWFromFile() if SDL_SaveBMP_RW() is called from SDL_SaveBMP(). */
  612. return -1;
  613. }
  614. if (save32bit) {
  615. saveLegacyBMP = SDL_GetHintBoolean(SDL_HINT_BMP_SAVE_LEGACY_FORMAT, SDL_FALSE);
  616. }
  617. if (surface && (SDL_LockSurface(surface) == 0)) {
  618. const int bw = surface->w * surface->format->BytesPerPixel;
  619. /* Set the BMP file header values */
  620. bfSize = 0; /* We'll write this when we're done */
  621. bfReserved1 = 0;
  622. bfReserved2 = 0;
  623. bfOffBits = 0; /* We'll write this when we're done */
  624. /* Write the BMP file header values */
  625. fp_offset = SDL_RWtell(dst);
  626. SDL_ClearError();
  627. SDL_RWwrite(dst, magic, 2, 1);
  628. SDL_WriteLE32(dst, bfSize);
  629. SDL_WriteLE16(dst, bfReserved1);
  630. SDL_WriteLE16(dst, bfReserved2);
  631. SDL_WriteLE32(dst, bfOffBits);
  632. /* Set the BMP info values */
  633. biSize = 40;
  634. biWidth = surface->w;
  635. biHeight = surface->h;
  636. biPlanes = 1;
  637. biBitCount = surface->format->BitsPerPixel;
  638. biCompression = BI_RGB;
  639. biSizeImage = surface->h * surface->pitch;
  640. biXPelsPerMeter = 0;
  641. biYPelsPerMeter = 0;
  642. if (surface->format->palette) {
  643. biClrUsed = surface->format->palette->ncolors;
  644. } else {
  645. biClrUsed = 0;
  646. }
  647. biClrImportant = 0;
  648. /* Set the BMP info values for the version 4 header */
  649. if (save32bit && !saveLegacyBMP) {
  650. biSize = 108;
  651. biCompression = BI_BITFIELDS;
  652. /* The BMP format is always little endian, these masks stay the same */
  653. bV4RedMask = 0x00ff0000;
  654. bV4GreenMask = 0x0000ff00;
  655. bV4BlueMask = 0x000000ff;
  656. bV4AlphaMask = 0xff000000;
  657. bV4CSType = LCS_WINDOWS_COLOR_SPACE;
  658. bV4GammaRed = 0;
  659. bV4GammaGreen = 0;
  660. bV4GammaBlue = 0;
  661. }
  662. /* Write the BMP info values */
  663. SDL_WriteLE32(dst, biSize);
  664. SDL_WriteLE32(dst, biWidth);
  665. SDL_WriteLE32(dst, biHeight);
  666. SDL_WriteLE16(dst, biPlanes);
  667. SDL_WriteLE16(dst, biBitCount);
  668. SDL_WriteLE32(dst, biCompression);
  669. SDL_WriteLE32(dst, biSizeImage);
  670. SDL_WriteLE32(dst, biXPelsPerMeter);
  671. SDL_WriteLE32(dst, biYPelsPerMeter);
  672. SDL_WriteLE32(dst, biClrUsed);
  673. SDL_WriteLE32(dst, biClrImportant);
  674. /* Write the BMP info values for the version 4 header */
  675. if (save32bit && !saveLegacyBMP) {
  676. SDL_WriteLE32(dst, bV4RedMask);
  677. SDL_WriteLE32(dst, bV4GreenMask);
  678. SDL_WriteLE32(dst, bV4BlueMask);
  679. SDL_WriteLE32(dst, bV4AlphaMask);
  680. SDL_WriteLE32(dst, bV4CSType);
  681. for (i = 0; i < 3 * 3; i++) {
  682. SDL_WriteLE32(dst, bV4Endpoints[i]);
  683. }
  684. SDL_WriteLE32(dst, bV4GammaRed);
  685. SDL_WriteLE32(dst, bV4GammaGreen);
  686. SDL_WriteLE32(dst, bV4GammaBlue);
  687. }
  688. /* Write the palette (in BGR color order) */
  689. if (surface->format->palette) {
  690. SDL_Color *colors;
  691. int ncolors;
  692. colors = surface->format->palette->colors;
  693. ncolors = surface->format->palette->ncolors;
  694. for (i = 0; i < ncolors; ++i) {
  695. SDL_RWwrite(dst, &colors[i].b, 1, 1);
  696. SDL_RWwrite(dst, &colors[i].g, 1, 1);
  697. SDL_RWwrite(dst, &colors[i].r, 1, 1);
  698. SDL_RWwrite(dst, &colors[i].a, 1, 1);
  699. }
  700. }
  701. /* Write the bitmap offset */
  702. bfOffBits = (Uint32)(SDL_RWtell(dst) - fp_offset);
  703. if (SDL_RWseek(dst, fp_offset + 10, RW_SEEK_SET) < 0) {
  704. SDL_Error(SDL_EFSEEK);
  705. }
  706. SDL_WriteLE32(dst, bfOffBits);
  707. if (SDL_RWseek(dst, fp_offset + bfOffBits, RW_SEEK_SET) < 0) {
  708. SDL_Error(SDL_EFSEEK);
  709. }
  710. /* Write the bitmap image upside down */
  711. bits = (Uint8 *) surface->pixels + (surface->h * surface->pitch);
  712. pad = ((bw % 4) ? (4 - (bw % 4)) : 0);
  713. while (bits > (Uint8 *) surface->pixels) {
  714. bits -= surface->pitch;
  715. if (SDL_RWwrite(dst, bits, 1, bw) != bw) {
  716. SDL_Error(SDL_EFWRITE);
  717. break;
  718. }
  719. if (pad) {
  720. const Uint8 padbyte = 0;
  721. for (i = 0; i < pad; ++i) {
  722. SDL_RWwrite(dst, &padbyte, 1, 1);
  723. }
  724. }
  725. }
  726. /* Write the BMP file size */
  727. bfSize = (Uint32)(SDL_RWtell(dst) - fp_offset);
  728. if (SDL_RWseek(dst, fp_offset + 2, RW_SEEK_SET) < 0) {
  729. SDL_Error(SDL_EFSEEK);
  730. }
  731. SDL_WriteLE32(dst, bfSize);
  732. if (SDL_RWseek(dst, fp_offset + bfSize, RW_SEEK_SET) < 0) {
  733. SDL_Error(SDL_EFSEEK);
  734. }
  735. /* Close it up.. */
  736. SDL_UnlockSurface(surface);
  737. if (surface != saveme) {
  738. SDL_FreeSurface(surface);
  739. }
  740. }
  741. if (freedst && dst) {
  742. SDL_RWclose(dst);
  743. }
  744. return ((SDL_strcmp(SDL_GetError(), "") == 0) ? 0 : -1);
  745. }
  746. /* vi: set ts=4 sw=4 expandtab: */