SDL_pixels.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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. /* General (mostly internal) pixel/color manipulation routines for SDL */
  20. #include "SDL_endian.h"
  21. #include "SDL_video.h"
  22. #include "SDL_sysvideo.h"
  23. #include "SDL_blit.h"
  24. #include "SDL_pixels_c.h"
  25. #include "SDL_RLEaccel_c.h"
  26. /* Lookup tables to expand partial bytes to the full 0..255 range */
  27. static Uint8 lookup_0[] = {
  28. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255
  29. };
  30. static Uint8 lookup_1[] = {
  31. 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126, 128, 130, 132, 134, 136, 138, 140, 142, 144, 146, 148, 150, 152, 154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182, 184, 186, 188, 190, 192, 194, 196, 198, 200, 202, 204, 206, 208, 210, 212, 214, 216, 218, 220, 222, 224, 226, 228, 230, 232, 234, 236, 238, 240, 242, 244, 246, 248, 250, 252, 255
  32. };
  33. static Uint8 lookup_2[] = {
  34. 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 68, 72, 76, 80, 85, 89, 93, 97, 101, 105, 109, 113, 117, 121, 125, 129, 133, 137, 141, 145, 149, 153, 157, 161, 165, 170, 174, 178, 182, 186, 190, 194, 198, 202, 206, 210, 214, 218, 222, 226, 230, 234, 238, 242, 246, 250, 255
  35. };
  36. static Uint8 lookup_3[] = {
  37. 0, 8, 16, 24, 32, 41, 49, 57, 65, 74, 82, 90, 98, 106, 115, 123, 131, 139, 148, 156, 164, 172, 180, 189, 197, 205, 213, 222, 230, 238, 246, 255
  38. };
  39. static Uint8 lookup_4[] = {
  40. 0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255
  41. };
  42. static Uint8 lookup_5[] = {
  43. 0, 36, 72, 109, 145, 182, 218, 255
  44. };
  45. static Uint8 lookup_6[] = {
  46. 0, 85, 170, 255
  47. };
  48. static Uint8 lookup_7[] = {
  49. 0, 255
  50. };
  51. static Uint8 lookup_8[] = {
  52. 255
  53. };
  54. Uint8* SDL_expand_byte[9] = {
  55. lookup_0,
  56. lookup_1,
  57. lookup_2,
  58. lookup_3,
  59. lookup_4,
  60. lookup_5,
  61. lookup_6,
  62. lookup_7,
  63. lookup_8
  64. };
  65. /* Helper functions */
  66. const char*
  67. SDL_GetPixelFormatName(Uint32 format)
  68. {
  69. switch (format) {
  70. #define CASE(X) case X: return #X;
  71. CASE(SDL_PIXELFORMAT_INDEX1LSB)
  72. CASE(SDL_PIXELFORMAT_INDEX1MSB)
  73. CASE(SDL_PIXELFORMAT_INDEX4LSB)
  74. CASE(SDL_PIXELFORMAT_INDEX4MSB)
  75. CASE(SDL_PIXELFORMAT_INDEX8)
  76. CASE(SDL_PIXELFORMAT_RGB332)
  77. CASE(SDL_PIXELFORMAT_RGB444)
  78. CASE(SDL_PIXELFORMAT_BGR444)
  79. CASE(SDL_PIXELFORMAT_RGB555)
  80. CASE(SDL_PIXELFORMAT_BGR555)
  81. CASE(SDL_PIXELFORMAT_ARGB4444)
  82. CASE(SDL_PIXELFORMAT_RGBA4444)
  83. CASE(SDL_PIXELFORMAT_ABGR4444)
  84. CASE(SDL_PIXELFORMAT_BGRA4444)
  85. CASE(SDL_PIXELFORMAT_ARGB1555)
  86. CASE(SDL_PIXELFORMAT_RGBA5551)
  87. CASE(SDL_PIXELFORMAT_ABGR1555)
  88. CASE(SDL_PIXELFORMAT_BGRA5551)
  89. CASE(SDL_PIXELFORMAT_RGB565)
  90. CASE(SDL_PIXELFORMAT_BGR565)
  91. CASE(SDL_PIXELFORMAT_RGB24)
  92. CASE(SDL_PIXELFORMAT_BGR24)
  93. CASE(SDL_PIXELFORMAT_RGB888)
  94. CASE(SDL_PIXELFORMAT_RGBX8888)
  95. CASE(SDL_PIXELFORMAT_BGR888)
  96. CASE(SDL_PIXELFORMAT_BGRX8888)
  97. CASE(SDL_PIXELFORMAT_ARGB8888)
  98. CASE(SDL_PIXELFORMAT_RGBA8888)
  99. CASE(SDL_PIXELFORMAT_ABGR8888)
  100. CASE(SDL_PIXELFORMAT_BGRA8888)
  101. CASE(SDL_PIXELFORMAT_ARGB2101010)
  102. CASE(SDL_PIXELFORMAT_YV12)
  103. CASE(SDL_PIXELFORMAT_IYUV)
  104. CASE(SDL_PIXELFORMAT_YUY2)
  105. CASE(SDL_PIXELFORMAT_UYVY)
  106. CASE(SDL_PIXELFORMAT_YVYU)
  107. CASE(SDL_PIXELFORMAT_NV12)
  108. CASE(SDL_PIXELFORMAT_NV21)
  109. #undef CASE
  110. default:
  111. return "SDL_PIXELFORMAT_UNKNOWN";
  112. }
  113. }
  114. SDL_bool
  115. SDL_PixelFormatEnumToMasks(Uint32 format, int *bpp, Uint32 * Rmask,
  116. Uint32 * Gmask, Uint32 * Bmask, Uint32 * Amask)
  117. {
  118. Uint32 masks[4];
  119. /* This function doesn't work with FourCC pixel formats */
  120. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  121. SDL_SetError("FOURCC pixel formats are not supported");
  122. return SDL_FALSE;
  123. }
  124. /* Initialize the values here */
  125. if (SDL_BYTESPERPIXEL(format) <= 2) {
  126. *bpp = SDL_BITSPERPIXEL(format);
  127. } else {
  128. *bpp = SDL_BYTESPERPIXEL(format) * 8;
  129. }
  130. *Rmask = *Gmask = *Bmask = *Amask = 0;
  131. if (format == SDL_PIXELFORMAT_RGB24) {
  132. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  133. *Rmask = 0x00FF0000;
  134. *Gmask = 0x0000FF00;
  135. *Bmask = 0x000000FF;
  136. #else
  137. *Rmask = 0x000000FF;
  138. *Gmask = 0x0000FF00;
  139. *Bmask = 0x00FF0000;
  140. #endif
  141. return SDL_TRUE;
  142. }
  143. if (format == SDL_PIXELFORMAT_BGR24) {
  144. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  145. *Rmask = 0x000000FF;
  146. *Gmask = 0x0000FF00;
  147. *Bmask = 0x00FF0000;
  148. #else
  149. *Rmask = 0x00FF0000;
  150. *Gmask = 0x0000FF00;
  151. *Bmask = 0x000000FF;
  152. #endif
  153. return SDL_TRUE;
  154. }
  155. if (SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED8 &&
  156. SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED16 &&
  157. SDL_PIXELTYPE(format) != SDL_PIXELTYPE_PACKED32) {
  158. /* Not a format that uses masks */
  159. return SDL_TRUE;
  160. }
  161. switch (SDL_PIXELLAYOUT(format)) {
  162. case SDL_PACKEDLAYOUT_332:
  163. masks[0] = 0x00000000;
  164. masks[1] = 0x000000E0;
  165. masks[2] = 0x0000001C;
  166. masks[3] = 0x00000003;
  167. break;
  168. case SDL_PACKEDLAYOUT_4444:
  169. masks[0] = 0x0000F000;
  170. masks[1] = 0x00000F00;
  171. masks[2] = 0x000000F0;
  172. masks[3] = 0x0000000F;
  173. break;
  174. case SDL_PACKEDLAYOUT_1555:
  175. masks[0] = 0x00008000;
  176. masks[1] = 0x00007C00;
  177. masks[2] = 0x000003E0;
  178. masks[3] = 0x0000001F;
  179. break;
  180. case SDL_PACKEDLAYOUT_5551:
  181. masks[0] = 0x0000F800;
  182. masks[1] = 0x000007C0;
  183. masks[2] = 0x0000003E;
  184. masks[3] = 0x00000001;
  185. break;
  186. case SDL_PACKEDLAYOUT_565:
  187. masks[0] = 0x00000000;
  188. masks[1] = 0x0000F800;
  189. masks[2] = 0x000007E0;
  190. masks[3] = 0x0000001F;
  191. break;
  192. case SDL_PACKEDLAYOUT_8888:
  193. masks[0] = 0xFF000000;
  194. masks[1] = 0x00FF0000;
  195. masks[2] = 0x0000FF00;
  196. masks[3] = 0x000000FF;
  197. break;
  198. case SDL_PACKEDLAYOUT_2101010:
  199. masks[0] = 0xC0000000;
  200. masks[1] = 0x3FF00000;
  201. masks[2] = 0x000FFC00;
  202. masks[3] = 0x000003FF;
  203. break;
  204. case SDL_PACKEDLAYOUT_1010102:
  205. masks[0] = 0xFFC00000;
  206. masks[1] = 0x003FF000;
  207. masks[2] = 0x00000FFC;
  208. masks[3] = 0x00000003;
  209. break;
  210. default:
  211. SDL_SetError("Unknown pixel format");
  212. return SDL_FALSE;
  213. }
  214. switch (SDL_PIXELORDER(format)) {
  215. case SDL_PACKEDORDER_XRGB:
  216. *Rmask = masks[1];
  217. *Gmask = masks[2];
  218. *Bmask = masks[3];
  219. break;
  220. case SDL_PACKEDORDER_RGBX:
  221. *Rmask = masks[0];
  222. *Gmask = masks[1];
  223. *Bmask = masks[2];
  224. break;
  225. case SDL_PACKEDORDER_ARGB:
  226. *Amask = masks[0];
  227. *Rmask = masks[1];
  228. *Gmask = masks[2];
  229. *Bmask = masks[3];
  230. break;
  231. case SDL_PACKEDORDER_RGBA:
  232. *Rmask = masks[0];
  233. *Gmask = masks[1];
  234. *Bmask = masks[2];
  235. *Amask = masks[3];
  236. break;
  237. case SDL_PACKEDORDER_XBGR:
  238. *Bmask = masks[1];
  239. *Gmask = masks[2];
  240. *Rmask = masks[3];
  241. break;
  242. case SDL_PACKEDORDER_BGRX:
  243. *Bmask = masks[0];
  244. *Gmask = masks[1];
  245. *Rmask = masks[2];
  246. break;
  247. case SDL_PACKEDORDER_BGRA:
  248. *Bmask = masks[0];
  249. *Gmask = masks[1];
  250. *Rmask = masks[2];
  251. *Amask = masks[3];
  252. break;
  253. case SDL_PACKEDORDER_ABGR:
  254. *Amask = masks[0];
  255. *Bmask = masks[1];
  256. *Gmask = masks[2];
  257. *Rmask = masks[3];
  258. break;
  259. default:
  260. SDL_SetError("Unknown pixel format");
  261. return SDL_FALSE;
  262. }
  263. return SDL_TRUE;
  264. }
  265. Uint32
  266. SDL_MasksToPixelFormatEnum(int bpp, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask,
  267. Uint32 Amask)
  268. {
  269. switch (bpp) {
  270. case 1:
  271. /* SDL defaults to MSB ordering */
  272. return SDL_PIXELFORMAT_INDEX1MSB;
  273. case 4:
  274. /* SDL defaults to MSB ordering */
  275. return SDL_PIXELFORMAT_INDEX4MSB;
  276. case 8:
  277. if (Rmask == 0) {
  278. return SDL_PIXELFORMAT_INDEX8;
  279. }
  280. if (Rmask == 0xE0 &&
  281. Gmask == 0x1C &&
  282. Bmask == 0x03 &&
  283. Amask == 0x00) {
  284. return SDL_PIXELFORMAT_RGB332;
  285. }
  286. break;
  287. case 12:
  288. if (Rmask == 0) {
  289. return SDL_PIXELFORMAT_RGB444;
  290. }
  291. if (Rmask == 0x0F00 &&
  292. Gmask == 0x00F0 &&
  293. Bmask == 0x000F &&
  294. Amask == 0x0000) {
  295. return SDL_PIXELFORMAT_RGB444;
  296. }
  297. if (Rmask == 0x000F &&
  298. Gmask == 0x00F0 &&
  299. Bmask == 0x0F00 &&
  300. Amask == 0x0000) {
  301. return SDL_PIXELFORMAT_BGR444;
  302. }
  303. break;
  304. case 15:
  305. if (Rmask == 0) {
  306. return SDL_PIXELFORMAT_RGB555;
  307. }
  308. /* fallthrough */
  309. case 16:
  310. if (Rmask == 0) {
  311. return SDL_PIXELFORMAT_RGB565;
  312. }
  313. if (Rmask == 0x7C00 &&
  314. Gmask == 0x03E0 &&
  315. Bmask == 0x001F &&
  316. Amask == 0x0000) {
  317. return SDL_PIXELFORMAT_RGB555;
  318. }
  319. if (Rmask == 0x001F &&
  320. Gmask == 0x03E0 &&
  321. Bmask == 0x7C00 &&
  322. Amask == 0x0000) {
  323. return SDL_PIXELFORMAT_BGR555;
  324. }
  325. if (Rmask == 0x0F00 &&
  326. Gmask == 0x00F0 &&
  327. Bmask == 0x000F &&
  328. Amask == 0xF000) {
  329. return SDL_PIXELFORMAT_ARGB4444;
  330. }
  331. if (Rmask == 0xF000 &&
  332. Gmask == 0x0F00 &&
  333. Bmask == 0x00F0 &&
  334. Amask == 0x000F) {
  335. return SDL_PIXELFORMAT_RGBA4444;
  336. }
  337. if (Rmask == 0x000F &&
  338. Gmask == 0x00F0 &&
  339. Bmask == 0x0F00 &&
  340. Amask == 0xF000) {
  341. return SDL_PIXELFORMAT_ABGR4444;
  342. }
  343. if (Rmask == 0x00F0 &&
  344. Gmask == 0x0F00 &&
  345. Bmask == 0xF000 &&
  346. Amask == 0x000F) {
  347. return SDL_PIXELFORMAT_BGRA4444;
  348. }
  349. if (Rmask == 0x7C00 &&
  350. Gmask == 0x03E0 &&
  351. Bmask == 0x001F &&
  352. Amask == 0x8000) {
  353. return SDL_PIXELFORMAT_ARGB1555;
  354. }
  355. if (Rmask == 0xF800 &&
  356. Gmask == 0x07C0 &&
  357. Bmask == 0x003E &&
  358. Amask == 0x0001) {
  359. return SDL_PIXELFORMAT_RGBA5551;
  360. }
  361. if (Rmask == 0x001F &&
  362. Gmask == 0x03E0 &&
  363. Bmask == 0x7C00 &&
  364. Amask == 0x8000) {
  365. return SDL_PIXELFORMAT_ABGR1555;
  366. }
  367. if (Rmask == 0x003E &&
  368. Gmask == 0x07C0 &&
  369. Bmask == 0xF800 &&
  370. Amask == 0x0001) {
  371. return SDL_PIXELFORMAT_BGRA5551;
  372. }
  373. if (Rmask == 0xF800 &&
  374. Gmask == 0x07E0 &&
  375. Bmask == 0x001F &&
  376. Amask == 0x0000) {
  377. return SDL_PIXELFORMAT_RGB565;
  378. }
  379. if (Rmask == 0x001F &&
  380. Gmask == 0x07E0 &&
  381. Bmask == 0xF800 &&
  382. Amask == 0x0000) {
  383. return SDL_PIXELFORMAT_BGR565;
  384. }
  385. if (Rmask == 0x003F &&
  386. Gmask == 0x07C0 &&
  387. Bmask == 0xF800 &&
  388. Amask == 0x0000) {
  389. /* Technically this would be BGR556, but Witek says this works in bug 3158 */
  390. return SDL_PIXELFORMAT_RGB565;
  391. }
  392. break;
  393. case 24:
  394. switch (Rmask) {
  395. case 0:
  396. case 0x00FF0000:
  397. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  398. return SDL_PIXELFORMAT_RGB24;
  399. #else
  400. return SDL_PIXELFORMAT_BGR24;
  401. #endif
  402. case 0x000000FF:
  403. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  404. return SDL_PIXELFORMAT_BGR24;
  405. #else
  406. return SDL_PIXELFORMAT_RGB24;
  407. #endif
  408. }
  409. case 32:
  410. if (Rmask == 0) {
  411. return SDL_PIXELFORMAT_RGB888;
  412. }
  413. if (Rmask == 0x00FF0000 &&
  414. Gmask == 0x0000FF00 &&
  415. Bmask == 0x000000FF &&
  416. Amask == 0x00000000) {
  417. return SDL_PIXELFORMAT_RGB888;
  418. }
  419. if (Rmask == 0xFF000000 &&
  420. Gmask == 0x00FF0000 &&
  421. Bmask == 0x0000FF00 &&
  422. Amask == 0x00000000) {
  423. return SDL_PIXELFORMAT_RGBX8888;
  424. }
  425. if (Rmask == 0x000000FF &&
  426. Gmask == 0x0000FF00 &&
  427. Bmask == 0x00FF0000 &&
  428. Amask == 0x00000000) {
  429. return SDL_PIXELFORMAT_BGR888;
  430. }
  431. if (Rmask == 0x0000FF00 &&
  432. Gmask == 0x00FF0000 &&
  433. Bmask == 0xFF000000 &&
  434. Amask == 0x00000000) {
  435. return SDL_PIXELFORMAT_BGRX8888;
  436. }
  437. if (Rmask == 0x00FF0000 &&
  438. Gmask == 0x0000FF00 &&
  439. Bmask == 0x000000FF &&
  440. Amask == 0xFF000000) {
  441. return SDL_PIXELFORMAT_ARGB8888;
  442. }
  443. if (Rmask == 0xFF000000 &&
  444. Gmask == 0x00FF0000 &&
  445. Bmask == 0x0000FF00 &&
  446. Amask == 0x000000FF) {
  447. return SDL_PIXELFORMAT_RGBA8888;
  448. }
  449. if (Rmask == 0x000000FF &&
  450. Gmask == 0x0000FF00 &&
  451. Bmask == 0x00FF0000 &&
  452. Amask == 0xFF000000) {
  453. return SDL_PIXELFORMAT_ABGR8888;
  454. }
  455. if (Rmask == 0x0000FF00 &&
  456. Gmask == 0x00FF0000 &&
  457. Bmask == 0xFF000000 &&
  458. Amask == 0x000000FF) {
  459. return SDL_PIXELFORMAT_BGRA8888;
  460. }
  461. if (Rmask == 0x3FF00000 &&
  462. Gmask == 0x000FFC00 &&
  463. Bmask == 0x000003FF &&
  464. Amask == 0xC0000000) {
  465. return SDL_PIXELFORMAT_ARGB2101010;
  466. }
  467. }
  468. return SDL_PIXELFORMAT_UNKNOWN;
  469. }
  470. static SDL_PixelFormat *formats;
  471. static SDL_SpinLock formats_lock = 0;
  472. SDL_PixelFormat *
  473. SDL_AllocFormat(Uint32 pixel_format)
  474. {
  475. SDL_PixelFormat *format;
  476. SDL_AtomicLock(&formats_lock);
  477. /* Look it up in our list of previously allocated formats */
  478. for (format = formats; format; format = format->next) {
  479. if (pixel_format == format->format) {
  480. ++format->refcount;
  481. SDL_AtomicUnlock(&formats_lock);
  482. return format;
  483. }
  484. }
  485. /* Allocate an empty pixel format structure, and initialize it */
  486. format = SDL_malloc(sizeof(*format));
  487. if (format == NULL) {
  488. SDL_AtomicUnlock(&formats_lock);
  489. SDL_OutOfMemory();
  490. return NULL;
  491. }
  492. if (SDL_InitFormat(format, pixel_format) < 0) {
  493. SDL_AtomicUnlock(&formats_lock);
  494. SDL_free(format);
  495. SDL_InvalidParamError("format");
  496. return NULL;
  497. }
  498. if (!SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
  499. /* Cache the RGB formats */
  500. format->next = formats;
  501. formats = format;
  502. }
  503. SDL_AtomicUnlock(&formats_lock);
  504. return format;
  505. }
  506. int
  507. SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format)
  508. {
  509. int bpp;
  510. Uint32 Rmask, Gmask, Bmask, Amask;
  511. Uint32 mask;
  512. if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
  513. &Rmask, &Gmask, &Bmask, &Amask)) {
  514. return -1;
  515. }
  516. /* Set up the format */
  517. SDL_zerop(format);
  518. format->format = pixel_format;
  519. format->BitsPerPixel = bpp;
  520. format->BytesPerPixel = (bpp + 7) / 8;
  521. format->Rmask = Rmask;
  522. format->Rshift = 0;
  523. format->Rloss = 8;
  524. if (Rmask) {
  525. for (mask = Rmask; !(mask & 0x01); mask >>= 1)
  526. ++format->Rshift;
  527. for (; (mask & 0x01); mask >>= 1)
  528. --format->Rloss;
  529. }
  530. format->Gmask = Gmask;
  531. format->Gshift = 0;
  532. format->Gloss = 8;
  533. if (Gmask) {
  534. for (mask = Gmask; !(mask & 0x01); mask >>= 1)
  535. ++format->Gshift;
  536. for (; (mask & 0x01); mask >>= 1)
  537. --format->Gloss;
  538. }
  539. format->Bmask = Bmask;
  540. format->Bshift = 0;
  541. format->Bloss = 8;
  542. if (Bmask) {
  543. for (mask = Bmask; !(mask & 0x01); mask >>= 1)
  544. ++format->Bshift;
  545. for (; (mask & 0x01); mask >>= 1)
  546. --format->Bloss;
  547. }
  548. format->Amask = Amask;
  549. format->Ashift = 0;
  550. format->Aloss = 8;
  551. if (Amask) {
  552. for (mask = Amask; !(mask & 0x01); mask >>= 1)
  553. ++format->Ashift;
  554. for (; (mask & 0x01); mask >>= 1)
  555. --format->Aloss;
  556. }
  557. format->palette = NULL;
  558. format->refcount = 1;
  559. format->next = NULL;
  560. return 0;
  561. }
  562. void
  563. SDL_FreeFormat(SDL_PixelFormat *format)
  564. {
  565. SDL_PixelFormat *prev;
  566. if (!format) {
  567. SDL_InvalidParamError("format");
  568. return;
  569. }
  570. SDL_AtomicLock(&formats_lock);
  571. if (--format->refcount > 0) {
  572. SDL_AtomicUnlock(&formats_lock);
  573. return;
  574. }
  575. /* Remove this format from our list */
  576. if (format == formats) {
  577. formats = format->next;
  578. } else if (formats) {
  579. for (prev = formats; prev->next; prev = prev->next) {
  580. if (prev->next == format) {
  581. prev->next = format->next;
  582. break;
  583. }
  584. }
  585. }
  586. SDL_AtomicUnlock(&formats_lock);
  587. if (format->palette) {
  588. SDL_FreePalette(format->palette);
  589. }
  590. SDL_free(format);
  591. }
  592. SDL_Palette *
  593. SDL_AllocPalette(int ncolors)
  594. {
  595. SDL_Palette *palette;
  596. /* Input validation */
  597. if (ncolors < 1) {
  598. SDL_InvalidParamError("ncolors");
  599. return NULL;
  600. }
  601. palette = (SDL_Palette *) SDL_malloc(sizeof(*palette));
  602. if (!palette) {
  603. SDL_OutOfMemory();
  604. return NULL;
  605. }
  606. palette->colors =
  607. (SDL_Color *) SDL_malloc(ncolors * sizeof(*palette->colors));
  608. if (!palette->colors) {
  609. SDL_free(palette);
  610. return NULL;
  611. }
  612. palette->ncolors = ncolors;
  613. palette->version = 1;
  614. palette->refcount = 1;
  615. SDL_memset(palette->colors, 0xFF, ncolors * sizeof(*palette->colors));
  616. return palette;
  617. }
  618. int
  619. SDL_SetPixelFormatPalette(SDL_PixelFormat * format, SDL_Palette *palette)
  620. {
  621. if (!format) {
  622. return SDL_SetError("SDL_SetPixelFormatPalette() passed NULL format");
  623. }
  624. if (palette && palette->ncolors > (1 << format->BitsPerPixel)) {
  625. return SDL_SetError("SDL_SetPixelFormatPalette() passed a palette that doesn't match the format");
  626. }
  627. if (format->palette == palette) {
  628. return 0;
  629. }
  630. if (format->palette) {
  631. SDL_FreePalette(format->palette);
  632. }
  633. format->palette = palette;
  634. if (format->palette) {
  635. ++format->palette->refcount;
  636. }
  637. return 0;
  638. }
  639. int
  640. SDL_SetPaletteColors(SDL_Palette * palette, const SDL_Color * colors,
  641. int firstcolor, int ncolors)
  642. {
  643. int status = 0;
  644. /* Verify the parameters */
  645. if (!palette) {
  646. return -1;
  647. }
  648. if (ncolors > (palette->ncolors - firstcolor)) {
  649. ncolors = (palette->ncolors - firstcolor);
  650. status = -1;
  651. }
  652. if (colors != (palette->colors + firstcolor)) {
  653. SDL_memcpy(palette->colors + firstcolor, colors,
  654. ncolors * sizeof(*colors));
  655. }
  656. ++palette->version;
  657. if (!palette->version) {
  658. palette->version = 1;
  659. }
  660. return status;
  661. }
  662. void
  663. SDL_FreePalette(SDL_Palette * palette)
  664. {
  665. if (!palette) {
  666. SDL_InvalidParamError("palette");
  667. return;
  668. }
  669. if (--palette->refcount > 0) {
  670. return;
  671. }
  672. SDL_free(palette->colors);
  673. SDL_free(palette);
  674. }
  675. /*
  676. * Calculate an 8-bit (3 red, 3 green, 2 blue) dithered palette of colors
  677. */
  678. void
  679. SDL_DitherColors(SDL_Color * colors, int bpp)
  680. {
  681. int i;
  682. if (bpp != 8)
  683. return; /* only 8bpp supported right now */
  684. for (i = 0; i < 256; i++) {
  685. int r, g, b;
  686. /* map each bit field to the full [0, 255] interval,
  687. so 0 is mapped to (0, 0, 0) and 255 to (255, 255, 255) */
  688. r = i & 0xe0;
  689. r |= r >> 3 | r >> 6;
  690. colors[i].r = r;
  691. g = (i << 3) & 0xe0;
  692. g |= g >> 3 | g >> 6;
  693. colors[i].g = g;
  694. b = i & 0x3;
  695. b |= b << 2;
  696. b |= b << 4;
  697. colors[i].b = b;
  698. colors[i].a = SDL_ALPHA_OPAQUE;
  699. }
  700. }
  701. /*
  702. * Match an RGB value to a particular palette index
  703. */
  704. Uint8
  705. SDL_FindColor(SDL_Palette * pal, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  706. {
  707. /* Do colorspace distance matching */
  708. unsigned int smallest;
  709. unsigned int distance;
  710. int rd, gd, bd, ad;
  711. int i;
  712. Uint8 pixel = 0;
  713. smallest = ~0;
  714. for (i = 0; i < pal->ncolors; ++i) {
  715. rd = pal->colors[i].r - r;
  716. gd = pal->colors[i].g - g;
  717. bd = pal->colors[i].b - b;
  718. ad = pal->colors[i].a - a;
  719. distance = (rd * rd) + (gd * gd) + (bd * bd) + (ad * ad);
  720. if (distance < smallest) {
  721. pixel = i;
  722. if (distance == 0) { /* Perfect match! */
  723. break;
  724. }
  725. smallest = distance;
  726. }
  727. }
  728. return (pixel);
  729. }
  730. /* Tell whether palette is opaque, and if it has an alpha_channel */
  731. void
  732. SDL_DetectPalette(SDL_Palette *pal, SDL_bool *is_opaque, SDL_bool *has_alpha_channel)
  733. {
  734. int i;
  735. {
  736. SDL_bool all_opaque = SDL_TRUE;
  737. for (i = 0; i < pal->ncolors; i++) {
  738. Uint8 alpha_value = pal->colors[i].a;
  739. if (alpha_value != SDL_ALPHA_OPAQUE) {
  740. all_opaque = SDL_FALSE;
  741. break;
  742. }
  743. }
  744. if (all_opaque) {
  745. /* Palette is opaque, with an alpha channel */
  746. *is_opaque = SDL_TRUE;
  747. *has_alpha_channel = SDL_TRUE;
  748. return;
  749. }
  750. }
  751. {
  752. SDL_bool all_transparent = SDL_TRUE;
  753. for (i = 0; i < pal->ncolors; i++) {
  754. Uint8 alpha_value = pal->colors[i].a;
  755. if (alpha_value != SDL_ALPHA_TRANSPARENT) {
  756. all_transparent = SDL_FALSE;
  757. break;
  758. }
  759. }
  760. if (all_transparent) {
  761. /* Palette is opaque, without an alpha channel */
  762. *is_opaque = SDL_TRUE;
  763. *has_alpha_channel = SDL_FALSE;
  764. return;
  765. }
  766. }
  767. /* Palette has alpha values */
  768. *is_opaque = SDL_FALSE;
  769. *has_alpha_channel = SDL_TRUE;
  770. }
  771. /* Find the opaque pixel value corresponding to an RGB triple */
  772. Uint32
  773. SDL_MapRGB(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b)
  774. {
  775. if (format->palette == NULL) {
  776. return (r >> format->Rloss) << format->Rshift
  777. | (g >> format->Gloss) << format->Gshift
  778. | (b >> format->Bloss) << format->Bshift | format->Amask;
  779. } else {
  780. return SDL_FindColor(format->palette, r, g, b, SDL_ALPHA_OPAQUE);
  781. }
  782. }
  783. /* Find the pixel value corresponding to an RGBA quadruple */
  784. Uint32
  785. SDL_MapRGBA(const SDL_PixelFormat * format, Uint8 r, Uint8 g, Uint8 b,
  786. Uint8 a)
  787. {
  788. if (format->palette == NULL) {
  789. return (r >> format->Rloss) << format->Rshift
  790. | (g >> format->Gloss) << format->Gshift
  791. | (b >> format->Bloss) << format->Bshift
  792. | ((a >> format->Aloss) << format->Ashift & format->Amask);
  793. } else {
  794. return SDL_FindColor(format->palette, r, g, b, a);
  795. }
  796. }
  797. void
  798. SDL_GetRGB(Uint32 pixel, const SDL_PixelFormat * format, Uint8 * r, Uint8 * g,
  799. Uint8 * b)
  800. {
  801. if (format->palette == NULL) {
  802. unsigned v;
  803. v = (pixel & format->Rmask) >> format->Rshift;
  804. *r = SDL_expand_byte[format->Rloss][v];
  805. v = (pixel & format->Gmask) >> format->Gshift;
  806. *g = SDL_expand_byte[format->Gloss][v];
  807. v = (pixel & format->Bmask) >> format->Bshift;
  808. *b = SDL_expand_byte[format->Bloss][v];
  809. } else {
  810. if (pixel < (unsigned)format->palette->ncolors) {
  811. *r = format->palette->colors[pixel].r;
  812. *g = format->palette->colors[pixel].g;
  813. *b = format->palette->colors[pixel].b;
  814. } else {
  815. *r = *g = *b = 0;
  816. }
  817. }
  818. }
  819. void
  820. SDL_GetRGBA(Uint32 pixel, const SDL_PixelFormat * format,
  821. Uint8 * r, Uint8 * g, Uint8 * b, Uint8 * a)
  822. {
  823. if (format->palette == NULL) {
  824. unsigned v;
  825. v = (pixel & format->Rmask) >> format->Rshift;
  826. *r = SDL_expand_byte[format->Rloss][v];
  827. v = (pixel & format->Gmask) >> format->Gshift;
  828. *g = SDL_expand_byte[format->Gloss][v];
  829. v = (pixel & format->Bmask) >> format->Bshift;
  830. *b = SDL_expand_byte[format->Bloss][v];
  831. v = (pixel & format->Amask) >> format->Ashift;
  832. *a = SDL_expand_byte[format->Aloss][v];
  833. } else {
  834. if (pixel < (unsigned)format->palette->ncolors) {
  835. *r = format->palette->colors[pixel].r;
  836. *g = format->palette->colors[pixel].g;
  837. *b = format->palette->colors[pixel].b;
  838. *a = format->palette->colors[pixel].a;
  839. } else {
  840. *r = *g = *b = *a = 0;
  841. }
  842. }
  843. }
  844. /* Map from Palette to Palette */
  845. static Uint8 *
  846. Map1to1(SDL_Palette * src, SDL_Palette * dst, int *identical)
  847. {
  848. Uint8 *map;
  849. int i;
  850. if (identical) {
  851. if (src->ncolors <= dst->ncolors) {
  852. /* If an identical palette, no need to map */
  853. if (src == dst
  854. ||
  855. (SDL_memcmp
  856. (src->colors, dst->colors,
  857. src->ncolors * sizeof(SDL_Color)) == 0)) {
  858. *identical = 1;
  859. return (NULL);
  860. }
  861. }
  862. *identical = 0;
  863. }
  864. map = (Uint8 *) SDL_malloc(src->ncolors);
  865. if (map == NULL) {
  866. SDL_OutOfMemory();
  867. return (NULL);
  868. }
  869. for (i = 0; i < src->ncolors; ++i) {
  870. map[i] = SDL_FindColor(dst,
  871. src->colors[i].r, src->colors[i].g,
  872. src->colors[i].b, src->colors[i].a);
  873. }
  874. return (map);
  875. }
  876. /* Map from Palette to BitField */
  877. static Uint8 *
  878. Map1toN(SDL_PixelFormat * src, Uint8 Rmod, Uint8 Gmod, Uint8 Bmod, Uint8 Amod,
  879. SDL_PixelFormat * dst)
  880. {
  881. Uint8 *map;
  882. int i;
  883. int bpp;
  884. SDL_Palette *pal = src->palette;
  885. bpp = ((dst->BytesPerPixel == 3) ? 4 : dst->BytesPerPixel);
  886. map = (Uint8 *) SDL_malloc(pal->ncolors * bpp);
  887. if (map == NULL) {
  888. SDL_OutOfMemory();
  889. return (NULL);
  890. }
  891. /* We memory copy to the pixel map so the endianness is preserved */
  892. for (i = 0; i < pal->ncolors; ++i) {
  893. Uint8 R = (Uint8) ((pal->colors[i].r * Rmod) / 255);
  894. Uint8 G = (Uint8) ((pal->colors[i].g * Gmod) / 255);
  895. Uint8 B = (Uint8) ((pal->colors[i].b * Bmod) / 255);
  896. Uint8 A = (Uint8) ((pal->colors[i].a * Amod) / 255);
  897. ASSEMBLE_RGBA(&map[i * bpp], dst->BytesPerPixel, dst, R, G, B, A);
  898. }
  899. return (map);
  900. }
  901. /* Map from BitField to Dithered-Palette to Palette */
  902. static Uint8 *
  903. MapNto1(SDL_PixelFormat * src, SDL_PixelFormat * dst, int *identical)
  904. {
  905. /* Generate a 256 color dither palette */
  906. SDL_Palette dithered;
  907. SDL_Color colors[256];
  908. SDL_Palette *pal = dst->palette;
  909. dithered.ncolors = 256;
  910. SDL_DitherColors(colors, 8);
  911. dithered.colors = colors;
  912. return (Map1to1(&dithered, pal, identical));
  913. }
  914. SDL_BlitMap *
  915. SDL_AllocBlitMap(void)
  916. {
  917. SDL_BlitMap *map;
  918. /* Allocate the empty map */
  919. map = (SDL_BlitMap *) SDL_calloc(1, sizeof(*map));
  920. if (map == NULL) {
  921. SDL_OutOfMemory();
  922. return (NULL);
  923. }
  924. map->info.r = 0xFF;
  925. map->info.g = 0xFF;
  926. map->info.b = 0xFF;
  927. map->info.a = 0xFF;
  928. /* It's ready to go */
  929. return (map);
  930. }
  931. void
  932. SDL_InvalidateMap(SDL_BlitMap * map)
  933. {
  934. if (!map) {
  935. return;
  936. }
  937. if (map->dst) {
  938. /* Release our reference to the surface - see the note below */
  939. if (--map->dst->refcount <= 0) {
  940. SDL_FreeSurface(map->dst);
  941. }
  942. }
  943. map->dst = NULL;
  944. map->src_palette_version = 0;
  945. map->dst_palette_version = 0;
  946. SDL_free(map->info.table);
  947. map->info.table = NULL;
  948. }
  949. int
  950. SDL_MapSurface(SDL_Surface * src, SDL_Surface * dst)
  951. {
  952. SDL_PixelFormat *srcfmt;
  953. SDL_PixelFormat *dstfmt;
  954. SDL_BlitMap *map;
  955. /* Clear out any previous mapping */
  956. map = src->map;
  957. #if SDL_HAVE_RLE
  958. if ((src->flags & SDL_RLEACCEL) == SDL_RLEACCEL) {
  959. SDL_UnRLESurface(src, 1);
  960. }
  961. #endif
  962. SDL_InvalidateMap(map);
  963. /* Figure out what kind of mapping we're doing */
  964. map->identity = 0;
  965. srcfmt = src->format;
  966. dstfmt = dst->format;
  967. if (SDL_ISPIXELFORMAT_INDEXED(srcfmt->format)) {
  968. if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
  969. /* Palette --> Palette */
  970. map->info.table =
  971. Map1to1(srcfmt->palette, dstfmt->palette, &map->identity);
  972. if (!map->identity) {
  973. if (map->info.table == NULL) {
  974. return (-1);
  975. }
  976. }
  977. if (srcfmt->BitsPerPixel != dstfmt->BitsPerPixel)
  978. map->identity = 0;
  979. } else {
  980. /* Palette --> BitField */
  981. map->info.table =
  982. Map1toN(srcfmt, src->map->info.r, src->map->info.g,
  983. src->map->info.b, src->map->info.a, dstfmt);
  984. if (map->info.table == NULL) {
  985. return (-1);
  986. }
  987. }
  988. } else {
  989. if (SDL_ISPIXELFORMAT_INDEXED(dstfmt->format)) {
  990. /* BitField --> Palette */
  991. map->info.table = MapNto1(srcfmt, dstfmt, &map->identity);
  992. if (!map->identity) {
  993. if (map->info.table == NULL) {
  994. return (-1);
  995. }
  996. }
  997. map->identity = 0; /* Don't optimize to copy */
  998. } else {
  999. /* BitField --> BitField */
  1000. if (srcfmt == dstfmt) {
  1001. map->identity = 1;
  1002. }
  1003. }
  1004. }
  1005. map->dst = dst;
  1006. if (map->dst) {
  1007. /* Keep a reference to this surface so it doesn't get deleted
  1008. while we're still pointing at it.
  1009. A better method would be for the destination surface to keep
  1010. track of surfaces that are mapped to it and automatically
  1011. invalidate them when it is freed, but this will do for now.
  1012. */
  1013. ++map->dst->refcount;
  1014. }
  1015. if (dstfmt->palette) {
  1016. map->dst_palette_version = dstfmt->palette->version;
  1017. } else {
  1018. map->dst_palette_version = 0;
  1019. }
  1020. if (srcfmt->palette) {
  1021. map->src_palette_version = srcfmt->palette->version;
  1022. } else {
  1023. map->src_palette_version = 0;
  1024. }
  1025. /* Choose your blitters wisely */
  1026. return (SDL_CalculateBlit(src));
  1027. }
  1028. void
  1029. SDL_FreeBlitMap(SDL_BlitMap * map)
  1030. {
  1031. if (map) {
  1032. SDL_InvalidateMap(map);
  1033. SDL_free(map);
  1034. }
  1035. }
  1036. void
  1037. SDL_CalculateGammaRamp(float gamma, Uint16 * ramp)
  1038. {
  1039. int i;
  1040. /* Input validation */
  1041. if (gamma < 0.0f ) {
  1042. SDL_InvalidParamError("gamma");
  1043. return;
  1044. }
  1045. if (ramp == NULL) {
  1046. SDL_InvalidParamError("ramp");
  1047. return;
  1048. }
  1049. /* 0.0 gamma is all black */
  1050. if (gamma == 0.0f) {
  1051. SDL_memset(ramp, 0, 256 * sizeof(Uint16));
  1052. return;
  1053. } else if (gamma == 1.0f) {
  1054. /* 1.0 gamma is identity */
  1055. for (i = 0; i < 256; ++i) {
  1056. ramp[i] = (i << 8) | i;
  1057. }
  1058. return;
  1059. } else {
  1060. /* Calculate a real gamma ramp */
  1061. int value;
  1062. gamma = 1.0f / gamma;
  1063. for (i = 0; i < 256; ++i) {
  1064. value =
  1065. (int) (SDL_pow((double) i / 256.0, gamma) * 65535.0 + 0.5);
  1066. if (value > 65535) {
  1067. value = 65535;
  1068. }
  1069. ramp[i] = (Uint16) value;
  1070. }
  1071. }
  1072. }
  1073. /* vi: set ts=4 sw=4 expandtab: */