SDL_bmp.c 26 KB

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