SDL_bmp.c 28 KB

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