SDL_surface.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127
  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. #include "SDL_sysvideo.h"
  20. #include "SDL_video_c.h"
  21. #include "SDL_blit.h"
  22. #include "SDL_RLEaccel_c.h"
  23. #include "SDL_pixels_c.h"
  24. #include "SDL_yuv_c.h"
  25. #include "../render/SDL_sysrender.h"
  26. #include "SDL_surface_c.h"
  27. /* Check to make sure we can safely check multiplication of surface w and pitch and it won't overflow size_t */
  28. SDL_COMPILE_TIME_ASSERT(surface_size_assumptions,
  29. sizeof(int) == sizeof(Sint32) && sizeof(size_t) >= sizeof(Sint32));
  30. SDL_COMPILE_TIME_ASSERT(can_indicate_overflow, SDL_SIZE_MAX > SDL_MAX_SINT32);
  31. /* Public routines */
  32. SDL_bool SDL_SurfaceValid(SDL_Surface *surface)
  33. {
  34. return surface && surface->internal;
  35. }
  36. void SDL_UpdateSurfaceLockFlag(SDL_Surface *surface)
  37. {
  38. if (SDL_SurfaceHasRLE(surface)) {
  39. surface->flags |= SDL_SURFACE_LOCK_NEEDED;
  40. } else {
  41. surface->flags &= ~SDL_SURFACE_LOCK_NEEDED;
  42. }
  43. }
  44. /*
  45. * Calculate the pad-aligned scanline width of a surface.
  46. * Return SDL_SIZE_MAX on overflow.
  47. *
  48. * for FOURCC, use SDL_CalculateYUVSize()
  49. */
  50. static int SDL_CalculateRGBSize(Uint32 format, size_t width, size_t height, size_t *size, size_t *pitch, SDL_bool minimal)
  51. {
  52. if (SDL_BITSPERPIXEL(format) >= 8) {
  53. if (SDL_size_mul_overflow(width, SDL_BYTESPERPIXEL(format), pitch)) {
  54. return SDL_SetError("width * bpp would overflow");
  55. }
  56. } else {
  57. if (SDL_size_mul_overflow(width, SDL_BITSPERPIXEL(format), pitch)) {
  58. return SDL_SetError("width * bpp would overflow");
  59. }
  60. if (SDL_size_add_overflow(*pitch, 7, pitch)) {
  61. return SDL_SetError("aligning pitch would overflow");
  62. }
  63. *pitch /= 8;
  64. }
  65. if (!minimal) {
  66. /* 4-byte aligning for speed */
  67. if (SDL_size_add_overflow(*pitch, 3, pitch)) {
  68. return SDL_SetError("aligning pitch would overflow");
  69. }
  70. *pitch &= ~3;
  71. }
  72. if (SDL_size_mul_overflow(height, *pitch, size)) {
  73. return SDL_SetError("height * pitch would overflow");
  74. }
  75. return 0;
  76. }
  77. int SDL_CalculateSurfaceSize(SDL_PixelFormat format, int width, int height, size_t *size, size_t *pitch, SDL_bool minimalPitch)
  78. {
  79. size_t p = 0, sz = 0;
  80. if (size) {
  81. *size = 0;
  82. }
  83. if (pitch) {
  84. *pitch = 0;
  85. }
  86. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  87. if (SDL_CalculateYUVSize(format, width, height, &sz, &p) < 0) {
  88. /* Overflow... */
  89. return -1;
  90. }
  91. } else {
  92. if (SDL_CalculateRGBSize(format, width, height, &sz, &p, minimalPitch) < 0) {
  93. /* Overflow... */
  94. return -1;
  95. }
  96. }
  97. if (size) {
  98. *size = sz;
  99. }
  100. if (pitch) {
  101. *pitch = p;
  102. }
  103. return 0;
  104. }
  105. static SDL_Surface *SDL_InitializeSurface(SDL_InternalSurface *mem, int width, int height, SDL_PixelFormat format, SDL_Colorspace colorspace, SDL_PropertiesID props, void *pixels, int pitch, SDL_bool onstack)
  106. {
  107. SDL_Surface *surface = &mem->surface;
  108. SDL_zerop(mem);
  109. surface->flags = SDL_SURFACE_PREALLOCATED;
  110. surface->format = format;
  111. surface->w = width;
  112. surface->h = height;
  113. surface->pixels = pixels;
  114. surface->pitch = pitch;
  115. surface->internal = &mem->internal;
  116. if (onstack) {
  117. surface->internal->flags |= SDL_INTERNAL_SURFACE_STACK;
  118. }
  119. surface->internal->format = SDL_GetPixelFormatDetails(format);
  120. if (!surface->internal->format) {
  121. SDL_DestroySurface(surface);
  122. return NULL;
  123. }
  124. /* Initialize the clip rect */
  125. surface->internal->clip_rect.w = width;
  126. surface->internal->clip_rect.h = height;
  127. /* Allocate an empty mapping */
  128. surface->internal->map.info.r = 0xFF;
  129. surface->internal->map.info.g = 0xFF;
  130. surface->internal->map.info.b = 0xFF;
  131. surface->internal->map.info.a = 0xFF;
  132. if (colorspace != SDL_COLORSPACE_UNKNOWN &&
  133. colorspace != SDL_GetDefaultColorspaceForFormat(format)) {
  134. SDL_SetSurfaceColorspace(surface, colorspace);
  135. }
  136. if (props) {
  137. if (SDL_CopyProperties(props, SDL_GetSurfaceProperties(surface)) < 0) {
  138. return NULL;
  139. }
  140. }
  141. /* By default surfaces with an alpha mask are set up for blending */
  142. if (SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  143. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  144. }
  145. /* The surface is ready to go */
  146. surface->refcount = 1;
  147. return surface;
  148. }
  149. /*
  150. * Create an empty surface of the appropriate depth using the given format
  151. */
  152. SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
  153. {
  154. size_t pitch, size;
  155. SDL_InternalSurface *mem;
  156. SDL_Surface *surface;
  157. if (width < 0) {
  158. SDL_InvalidParamError("width");
  159. return NULL;
  160. }
  161. if (height < 0) {
  162. SDL_InvalidParamError("height");
  163. return NULL;
  164. }
  165. if (SDL_CalculateSurfaceSize(format, width, height, &size, &pitch, SDL_FALSE /* not minimal pitch */) < 0) {
  166. /* Overflow... */
  167. return NULL;
  168. }
  169. /* Allocate and initialize the surface */
  170. mem = (SDL_InternalSurface *)SDL_malloc(sizeof(*mem));
  171. if (!mem) {
  172. return NULL;
  173. }
  174. surface = SDL_InitializeSurface(mem, width, height, format, SDL_COLORSPACE_UNKNOWN, 0, NULL, (int)pitch, SDL_FALSE);
  175. if (surface) {
  176. if (surface->w && surface->h) {
  177. surface->flags &= ~SDL_SURFACE_PREALLOCATED;
  178. surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size);
  179. if (!surface->pixels) {
  180. SDL_DestroySurface(surface);
  181. return NULL;
  182. }
  183. surface->flags |= SDL_SURFACE_SIMD_ALIGNED;
  184. /* This is important for bitmaps */
  185. SDL_memset(surface->pixels, 0, size);
  186. }
  187. }
  188. return surface;
  189. }
  190. /*
  191. * Create an RGB surface from an existing memory buffer using the given
  192. * enum SDL_PIXELFORMAT_* format
  193. */
  194. SDL_Surface *SDL_CreateSurfaceFrom(int width, int height, SDL_PixelFormat format, void *pixels, int pitch)
  195. {
  196. SDL_InternalSurface *mem;
  197. if (width < 0) {
  198. SDL_InvalidParamError("width");
  199. return NULL;
  200. }
  201. if (height < 0) {
  202. SDL_InvalidParamError("height");
  203. return NULL;
  204. }
  205. if (pitch == 0 && !pixels) {
  206. /* The application will fill these in later with valid values */
  207. } else {
  208. size_t minimalPitch;
  209. if (SDL_CalculateSurfaceSize(format, width, height, NULL, &minimalPitch, SDL_TRUE /* minimal pitch */) < 0) {
  210. /* Overflow... */
  211. return NULL;
  212. }
  213. if (pitch < 0 || (size_t)pitch < minimalPitch) {
  214. SDL_InvalidParamError("pitch");
  215. return NULL;
  216. }
  217. }
  218. /* Allocate and initialize the surface */
  219. mem = (SDL_InternalSurface *)SDL_malloc(sizeof(*mem));
  220. if (!mem) {
  221. return NULL;
  222. }
  223. return SDL_InitializeSurface(mem, width, height, format, SDL_COLORSPACE_UNKNOWN, 0, pixels, pitch, SDL_FALSE);
  224. }
  225. SDL_PropertiesID SDL_GetSurfaceProperties(SDL_Surface *surface)
  226. {
  227. if (!SDL_SurfaceValid(surface)) {
  228. SDL_InvalidParamError("surface");
  229. return 0;
  230. }
  231. if (!surface->internal->props) {
  232. surface->internal->props = SDL_CreateProperties();
  233. }
  234. return surface->internal->props;
  235. }
  236. int SDL_SetSurfaceColorspace(SDL_Surface *surface, SDL_Colorspace colorspace)
  237. {
  238. if (!SDL_SurfaceValid(surface)) {
  239. return SDL_InvalidParamError("surface");
  240. }
  241. if (colorspace == SDL_GetDefaultColorspaceForFormat(surface->format)) {
  242. return 0;
  243. }
  244. return SDL_SetNumberProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_COLORSPACE_NUMBER, colorspace);
  245. }
  246. SDL_Colorspace SDL_GetSurfaceColorspace(SDL_Surface *surface)
  247. {
  248. SDL_Colorspace colorspace;
  249. if (!SDL_SurfaceValid(surface)) {
  250. return SDL_COLORSPACE_UNKNOWN;
  251. }
  252. colorspace = (SDL_Colorspace)SDL_GetNumberProperty(surface->internal->props, SDL_PROP_SURFACE_COLORSPACE_NUMBER, SDL_COLORSPACE_UNKNOWN);
  253. if (colorspace == SDL_COLORSPACE_UNKNOWN) {
  254. colorspace = SDL_GetDefaultColorspaceForFormat(surface->format);
  255. }
  256. return colorspace;
  257. }
  258. float SDL_GetDefaultSDRWhitePoint(SDL_Colorspace colorspace)
  259. {
  260. return SDL_GetSurfaceSDRWhitePoint(NULL, colorspace);
  261. }
  262. float SDL_GetSurfaceSDRWhitePoint(SDL_Surface *surface, SDL_Colorspace colorspace)
  263. {
  264. SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
  265. if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
  266. transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  267. SDL_PropertiesID props;
  268. float default_value = 1.0f;
  269. if (SDL_SurfaceValid(surface)) {
  270. props = surface->internal->props;
  271. } else {
  272. props = 0;
  273. }
  274. if (transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  275. /* The older standards use an SDR white point of 100 nits.
  276. * ITU-R BT.2408-6 recommends using an SDR white point of 203 nits.
  277. * This is the default Chrome uses, and what a lot of game content
  278. * assumes, so we'll go with that.
  279. */
  280. const float DEFAULT_PQ_SDR_WHITE_POINT = 203.0f;
  281. default_value = DEFAULT_PQ_SDR_WHITE_POINT;
  282. }
  283. return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT, default_value);
  284. }
  285. return 1.0f;
  286. }
  287. float SDL_GetDefaultHDRHeadroom(SDL_Colorspace colorspace)
  288. {
  289. return SDL_GetSurfaceHDRHeadroom(NULL, colorspace);
  290. }
  291. float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace)
  292. {
  293. SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
  294. if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
  295. transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  296. SDL_PropertiesID props;
  297. float default_value = 0.0f;
  298. if (SDL_SurfaceValid(surface)) {
  299. props = surface->internal->props;
  300. } else {
  301. props = 0;
  302. }
  303. return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, default_value);
  304. }
  305. return 1.0f;
  306. }
  307. SDL_Palette *SDL_CreateSurfacePalette(SDL_Surface *surface)
  308. {
  309. SDL_Palette *palette;
  310. if (!SDL_SurfaceValid(surface)) {
  311. SDL_InvalidParamError("surface");
  312. return NULL;
  313. }
  314. if (!SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
  315. SDL_SetError("The surface is not indexed format");
  316. return NULL;
  317. }
  318. palette = SDL_CreatePalette((1 << SDL_BITSPERPIXEL(surface->format)));
  319. if (!palette) {
  320. return NULL;
  321. }
  322. if (palette->ncolors == 2) {
  323. /* Create a black and white bitmap palette */
  324. palette->colors[0].r = 0xFF;
  325. palette->colors[0].g = 0xFF;
  326. palette->colors[0].b = 0xFF;
  327. palette->colors[1].r = 0x00;
  328. palette->colors[1].g = 0x00;
  329. palette->colors[1].b = 0x00;
  330. }
  331. if (SDL_SetSurfacePalette(surface, palette) < 0) {
  332. SDL_DestroyPalette(palette);
  333. return NULL;
  334. }
  335. /* The surface has retained the palette, we can remove the reference here */
  336. SDL_assert(palette->refcount == 2);
  337. SDL_DestroyPalette(palette);
  338. return palette;
  339. }
  340. int SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
  341. {
  342. if (!SDL_SurfaceValid(surface)) {
  343. return SDL_InvalidParamError("surface");
  344. }
  345. if (palette && palette->ncolors > (1 << SDL_BITSPERPIXEL(surface->format))) {
  346. return SDL_SetError("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
  347. }
  348. if (palette != surface->internal->palette) {
  349. if (surface->internal->palette) {
  350. SDL_DestroyPalette(surface->internal->palette);
  351. }
  352. surface->internal->palette = palette;
  353. if (surface->internal->palette) {
  354. ++surface->internal->palette->refcount;
  355. }
  356. }
  357. SDL_InvalidateMap(&surface->internal->map);
  358. return 0;
  359. }
  360. SDL_Palette *SDL_GetSurfacePalette(SDL_Surface *surface)
  361. {
  362. if (!SDL_SurfaceValid(surface)) {
  363. return NULL;
  364. }
  365. return surface->internal->palette;
  366. }
  367. int SDL_SetSurfaceRLE(SDL_Surface *surface, SDL_bool enabled)
  368. {
  369. int flags;
  370. if (!SDL_SurfaceValid(surface)) {
  371. return SDL_InvalidParamError("surface");
  372. }
  373. flags = surface->internal->map.info.flags;
  374. if (enabled) {
  375. surface->internal->map.info.flags |= SDL_COPY_RLE_DESIRED;
  376. } else {
  377. surface->internal->map.info.flags &= ~SDL_COPY_RLE_DESIRED;
  378. }
  379. if (surface->internal->map.info.flags != flags) {
  380. SDL_InvalidateMap(&surface->internal->map);
  381. }
  382. SDL_UpdateSurfaceLockFlag(surface);
  383. return 0;
  384. }
  385. SDL_bool SDL_SurfaceHasRLE(SDL_Surface *surface)
  386. {
  387. if (!SDL_SurfaceValid(surface)) {
  388. return SDL_FALSE;
  389. }
  390. if (!(surface->internal->map.info.flags & SDL_COPY_RLE_DESIRED)) {
  391. return SDL_FALSE;
  392. }
  393. return SDL_TRUE;
  394. }
  395. int SDL_SetSurfaceColorKey(SDL_Surface *surface, SDL_bool enabled, Uint32 key)
  396. {
  397. int flags;
  398. if (!SDL_SurfaceValid(surface)) {
  399. return SDL_InvalidParamError("surface");
  400. }
  401. if (surface->internal->palette && key >= ((Uint32)surface->internal->palette->ncolors)) {
  402. return SDL_InvalidParamError("key");
  403. }
  404. flags = surface->internal->map.info.flags;
  405. if (enabled) {
  406. surface->internal->map.info.flags |= SDL_COPY_COLORKEY;
  407. surface->internal->map.info.colorkey = key;
  408. } else {
  409. surface->internal->map.info.flags &= ~SDL_COPY_COLORKEY;
  410. }
  411. if (surface->internal->map.info.flags != flags) {
  412. SDL_InvalidateMap(&surface->internal->map);
  413. }
  414. return 0;
  415. }
  416. SDL_bool SDL_SurfaceHasColorKey(SDL_Surface *surface)
  417. {
  418. if (!SDL_SurfaceValid(surface)) {
  419. return SDL_FALSE;
  420. }
  421. if (!(surface->internal->map.info.flags & SDL_COPY_COLORKEY)) {
  422. return SDL_FALSE;
  423. }
  424. return SDL_TRUE;
  425. }
  426. int SDL_GetSurfaceColorKey(SDL_Surface *surface, Uint32 *key)
  427. {
  428. if (key) {
  429. *key = 0;
  430. }
  431. if (!SDL_SurfaceValid(surface)) {
  432. return SDL_InvalidParamError("surface");
  433. }
  434. if (!(surface->internal->map.info.flags & SDL_COPY_COLORKEY)) {
  435. return SDL_SetError("Surface doesn't have a colorkey");
  436. }
  437. if (key) {
  438. *key = surface->internal->map.info.colorkey;
  439. }
  440. return 0;
  441. }
  442. /* This is a fairly slow function to switch from colorkey to alpha
  443. NB: it doesn't handle bpp 1 or 3, because they have no alpha channel */
  444. static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface, SDL_bool ignore_alpha)
  445. {
  446. int x, y, bpp;
  447. if (!SDL_SurfaceValid(surface)) {
  448. return;
  449. }
  450. if (!(surface->internal->map.info.flags & SDL_COPY_COLORKEY) ||
  451. !SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  452. return;
  453. }
  454. bpp = SDL_BYTESPERPIXEL(surface->format);
  455. SDL_LockSurface(surface);
  456. if (bpp == 2) {
  457. Uint16 *row, *spot;
  458. Uint16 ckey = (Uint16)surface->internal->map.info.colorkey;
  459. Uint16 mask = (Uint16)(~surface->internal->format->Amask);
  460. /* Ignore, or not, alpha in colorkey comparison */
  461. if (ignore_alpha) {
  462. ckey &= mask;
  463. row = (Uint16 *)surface->pixels;
  464. for (y = surface->h; y--;) {
  465. spot = row;
  466. for (x = surface->w; x--;) {
  467. if ((*spot & mask) == ckey) {
  468. *spot &= mask;
  469. }
  470. ++spot;
  471. }
  472. row += surface->pitch / 2;
  473. }
  474. } else {
  475. row = (Uint16 *)surface->pixels;
  476. for (y = surface->h; y--;) {
  477. spot = row;
  478. for (x = surface->w; x--;) {
  479. if (*spot == ckey) {
  480. *spot &= mask;
  481. }
  482. ++spot;
  483. }
  484. row += surface->pitch / 2;
  485. }
  486. }
  487. } else if (bpp == 4) {
  488. Uint32 *row, *spot;
  489. Uint32 ckey = surface->internal->map.info.colorkey;
  490. Uint32 mask = ~surface->internal->format->Amask;
  491. /* Ignore, or not, alpha in colorkey comparison */
  492. if (ignore_alpha) {
  493. ckey &= mask;
  494. row = (Uint32 *)surface->pixels;
  495. for (y = surface->h; y--;) {
  496. spot = row;
  497. for (x = surface->w; x--;) {
  498. if ((*spot & mask) == ckey) {
  499. *spot &= mask;
  500. }
  501. ++spot;
  502. }
  503. row += surface->pitch / 4;
  504. }
  505. } else {
  506. row = (Uint32 *)surface->pixels;
  507. for (y = surface->h; y--;) {
  508. spot = row;
  509. for (x = surface->w; x--;) {
  510. if (*spot == ckey) {
  511. *spot &= mask;
  512. }
  513. ++spot;
  514. }
  515. row += surface->pitch / 4;
  516. }
  517. }
  518. }
  519. SDL_UnlockSurface(surface);
  520. SDL_SetSurfaceColorKey(surface, 0, 0);
  521. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  522. }
  523. int SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
  524. {
  525. int flags;
  526. if (!SDL_SurfaceValid(surface)) {
  527. return SDL_InvalidParamError("surface");
  528. }
  529. surface->internal->map.info.r = r;
  530. surface->internal->map.info.g = g;
  531. surface->internal->map.info.b = b;
  532. flags = surface->internal->map.info.flags;
  533. if (r != 0xFF || g != 0xFF || b != 0xFF) {
  534. surface->internal->map.info.flags |= SDL_COPY_MODULATE_COLOR;
  535. } else {
  536. surface->internal->map.info.flags &= ~SDL_COPY_MODULATE_COLOR;
  537. }
  538. if (surface->internal->map.info.flags != flags) {
  539. SDL_InvalidateMap(&surface->internal->map);
  540. }
  541. return 0;
  542. }
  543. int SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
  544. {
  545. if (!SDL_SurfaceValid(surface)) {
  546. if (r) {
  547. *r = 255;
  548. }
  549. if (g) {
  550. *g = 255;
  551. }
  552. if (b) {
  553. *b = 255;
  554. }
  555. return SDL_InvalidParamError("surface");
  556. }
  557. if (r) {
  558. *r = surface->internal->map.info.r;
  559. }
  560. if (g) {
  561. *g = surface->internal->map.info.g;
  562. }
  563. if (b) {
  564. *b = surface->internal->map.info.b;
  565. }
  566. return 0;
  567. }
  568. int SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
  569. {
  570. int flags;
  571. if (!SDL_SurfaceValid(surface)) {
  572. return SDL_InvalidParamError("surface");
  573. }
  574. surface->internal->map.info.a = alpha;
  575. flags = surface->internal->map.info.flags;
  576. if (alpha != 0xFF) {
  577. surface->internal->map.info.flags |= SDL_COPY_MODULATE_ALPHA;
  578. } else {
  579. surface->internal->map.info.flags &= ~SDL_COPY_MODULATE_ALPHA;
  580. }
  581. if (surface->internal->map.info.flags != flags) {
  582. SDL_InvalidateMap(&surface->internal->map);
  583. }
  584. return 0;
  585. }
  586. int SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
  587. {
  588. if (!SDL_SurfaceValid(surface)) {
  589. if (alpha) {
  590. *alpha = 255;
  591. }
  592. return SDL_InvalidParamError("surface");
  593. }
  594. if (alpha) {
  595. *alpha = surface->internal->map.info.a;
  596. }
  597. return 0;
  598. }
  599. int SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
  600. {
  601. int flags, status;
  602. if (!SDL_SurfaceValid(surface)) {
  603. return SDL_InvalidParamError("surface");
  604. }
  605. if (blendMode == SDL_BLENDMODE_INVALID) {
  606. return SDL_InvalidParamError("blendMode");
  607. }
  608. status = 0;
  609. flags = surface->internal->map.info.flags;
  610. surface->internal->map.info.flags &= ~(SDL_COPY_BLEND | SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL);
  611. switch (blendMode) {
  612. case SDL_BLENDMODE_NONE:
  613. break;
  614. case SDL_BLENDMODE_BLEND:
  615. surface->internal->map.info.flags |= SDL_COPY_BLEND;
  616. break;
  617. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  618. surface->internal->map.info.flags |= SDL_COPY_BLEND_PREMULTIPLIED;
  619. break;
  620. case SDL_BLENDMODE_ADD:
  621. surface->internal->map.info.flags |= SDL_COPY_ADD;
  622. break;
  623. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  624. surface->internal->map.info.flags |= SDL_COPY_ADD_PREMULTIPLIED;
  625. break;
  626. case SDL_BLENDMODE_MOD:
  627. surface->internal->map.info.flags |= SDL_COPY_MOD;
  628. break;
  629. case SDL_BLENDMODE_MUL:
  630. surface->internal->map.info.flags |= SDL_COPY_MUL;
  631. break;
  632. default:
  633. status = SDL_Unsupported();
  634. break;
  635. }
  636. if (surface->internal->map.info.flags != flags) {
  637. SDL_InvalidateMap(&surface->internal->map);
  638. }
  639. return status;
  640. }
  641. int SDL_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode)
  642. {
  643. if (blendMode) {
  644. *blendMode = SDL_BLENDMODE_INVALID;
  645. }
  646. if (!SDL_SurfaceValid(surface)) {
  647. return SDL_InvalidParamError("surface");
  648. }
  649. if (!blendMode) {
  650. return 0;
  651. }
  652. switch (surface->internal->map.info.flags & (SDL_COPY_BLEND | SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL)) {
  653. case SDL_COPY_BLEND:
  654. *blendMode = SDL_BLENDMODE_BLEND;
  655. break;
  656. case SDL_COPY_BLEND_PREMULTIPLIED:
  657. *blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
  658. break;
  659. case SDL_COPY_ADD:
  660. *blendMode = SDL_BLENDMODE_ADD;
  661. break;
  662. case SDL_COPY_ADD_PREMULTIPLIED:
  663. *blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
  664. break;
  665. case SDL_COPY_MOD:
  666. *blendMode = SDL_BLENDMODE_MOD;
  667. break;
  668. case SDL_COPY_MUL:
  669. *blendMode = SDL_BLENDMODE_MUL;
  670. break;
  671. default:
  672. *blendMode = SDL_BLENDMODE_NONE;
  673. break;
  674. }
  675. return 0;
  676. }
  677. SDL_bool SDL_SetSurfaceClipRect(SDL_Surface *surface, const SDL_Rect *rect)
  678. {
  679. SDL_Rect full_rect;
  680. /* Don't do anything if there's no surface to act on */
  681. if (!SDL_SurfaceValid(surface)) {
  682. return SDL_FALSE;
  683. }
  684. /* Set up the full surface rectangle */
  685. full_rect.x = 0;
  686. full_rect.y = 0;
  687. full_rect.w = surface->w;
  688. full_rect.h = surface->h;
  689. /* Set the clipping rectangle */
  690. if (!rect) {
  691. surface->internal->clip_rect = full_rect;
  692. return SDL_TRUE;
  693. }
  694. return SDL_GetRectIntersection(rect, &full_rect, &surface->internal->clip_rect);
  695. }
  696. int SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
  697. {
  698. if (!SDL_SurfaceValid(surface)) {
  699. if (rect) {
  700. SDL_zerop(rect);
  701. }
  702. return SDL_InvalidParamError("surface");
  703. }
  704. if (!rect) {
  705. return SDL_InvalidParamError("rect");
  706. }
  707. *rect = surface->internal->clip_rect;
  708. return 0;
  709. }
  710. /*
  711. * Set up a blit between two surfaces -- split into three parts:
  712. * The upper part, SDL_BlitSurface(), performs clipping and rectangle
  713. * verification. The lower part is a pointer to a low level
  714. * accelerated blitting function.
  715. *
  716. * These parts are separated out and each used internally by this
  717. * library in the optimum places. They are exported so that if
  718. * you know exactly what you are doing, you can optimize your code
  719. * by calling the one(s) you need.
  720. */
  721. int SDL_BlitSurfaceUnchecked(SDL_Surface *src, const SDL_Rect *srcrect,
  722. SDL_Surface *dst, const SDL_Rect *dstrect)
  723. {
  724. /* Check to make sure the blit mapping is valid */
  725. if ((src->internal->map.dst != dst) ||
  726. (dst->internal->palette &&
  727. src->internal->map.dst_palette_version != dst->internal->palette->version) ||
  728. (src->internal->palette &&
  729. src->internal->map.src_palette_version != src->internal->palette->version)) {
  730. if (SDL_MapSurface(src, dst) < 0) {
  731. return -1;
  732. }
  733. /* just here for debugging */
  734. /* printf */
  735. /* ("src = 0x%08X src->flags = %08X src->internal->map.info.flags = %08x\ndst = 0x%08X dst->flags = %08X dst->internal->map.info.flags = %08X\nsrc->internal->map.blit = 0x%08x\n", */
  736. /* src, dst->flags, src->internal->map.info.flags, dst, dst->flags, */
  737. /* dst->internal->map.info.flags, src->internal->map.blit); */
  738. }
  739. return src->internal->map.blit(src, srcrect, dst, dstrect);
  740. }
  741. int SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect,
  742. SDL_Surface *dst, SDL_Rect *dstrect)
  743. {
  744. SDL_Rect r_src, r_dst;
  745. /* Make sure the surfaces aren't locked */
  746. if (!SDL_SurfaceValid(src)) {
  747. return SDL_InvalidParamError("src");
  748. } else if (!SDL_SurfaceValid(dst)) {
  749. return SDL_InvalidParamError("dst");
  750. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  751. return SDL_SetError("Surfaces must not be locked during blit");
  752. }
  753. /* Full src surface */
  754. r_src.x = 0;
  755. r_src.y = 0;
  756. r_src.w = src->w;
  757. r_src.h = src->h;
  758. if (dstrect) {
  759. r_dst.x = dstrect->x;
  760. r_dst.y = dstrect->y;
  761. } else {
  762. r_dst.x = 0;
  763. r_dst.y = 0;
  764. }
  765. /* clip the source rectangle to the source surface */
  766. if (srcrect) {
  767. SDL_Rect tmp;
  768. if (SDL_GetRectIntersection(srcrect, &r_src, &tmp) == SDL_FALSE) {
  769. goto end;
  770. }
  771. /* Shift dstrect, if srcrect origin has changed */
  772. r_dst.x += tmp.x - srcrect->x;
  773. r_dst.y += tmp.y - srcrect->y;
  774. /* Update srcrect */
  775. r_src = tmp;
  776. }
  777. /* There're no dstrect.w/h parameters. It's the same as srcrect */
  778. r_dst.w = r_src.w;
  779. r_dst.h = r_src.h;
  780. /* clip the destination rectangle against the clip rectangle */
  781. {
  782. SDL_Rect tmp;
  783. if (SDL_GetRectIntersection(&r_dst, &dst->internal->clip_rect, &tmp) == SDL_FALSE) {
  784. goto end;
  785. }
  786. /* Shift srcrect, if dstrect has changed */
  787. r_src.x += tmp.x - r_dst.x;
  788. r_src.y += tmp.y - r_dst.y;
  789. r_src.w = tmp.w;
  790. r_src.h = tmp.h;
  791. /* Update dstrect */
  792. r_dst = tmp;
  793. }
  794. /* Switch back to a fast blit if we were previously stretching */
  795. if (src->internal->map.info.flags & SDL_COPY_NEAREST) {
  796. src->internal->map.info.flags &= ~SDL_COPY_NEAREST;
  797. SDL_InvalidateMap(&src->internal->map);
  798. }
  799. if (r_dst.w > 0 && r_dst.h > 0) {
  800. if (dstrect) { /* update output parameter */
  801. *dstrect = r_dst;
  802. }
  803. return SDL_BlitSurfaceUnchecked(src, &r_src, dst, &r_dst);
  804. }
  805. end:
  806. if (dstrect) { /* update output parameter */
  807. dstrect->w = dstrect->h = 0;
  808. }
  809. return 0;
  810. }
  811. int SDL_BlitSurfaceScaled(SDL_Surface *src, const SDL_Rect *srcrect,
  812. SDL_Surface *dst, SDL_Rect *dstrect,
  813. SDL_ScaleMode scaleMode)
  814. {
  815. SDL_Rect *clip_rect;
  816. double src_x0, src_y0, src_x1, src_y1;
  817. double dst_x0, dst_y0, dst_x1, dst_y1;
  818. SDL_Rect final_src, final_dst;
  819. double scaling_w, scaling_h;
  820. int src_w, src_h;
  821. int dst_w, dst_h;
  822. /* Make sure the surfaces aren't locked */
  823. if (!SDL_SurfaceValid(src)) {
  824. return SDL_InvalidParamError("src");
  825. } else if (!SDL_SurfaceValid(dst)) {
  826. return SDL_InvalidParamError("dst");
  827. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  828. return SDL_SetError("Surfaces must not be locked during blit");
  829. }
  830. if (!srcrect) {
  831. src_w = src->w;
  832. src_h = src->h;
  833. } else {
  834. src_w = srcrect->w;
  835. src_h = srcrect->h;
  836. }
  837. if (!dstrect) {
  838. dst_w = dst->w;
  839. dst_h = dst->h;
  840. } else {
  841. dst_w = dstrect->w;
  842. dst_h = dstrect->h;
  843. }
  844. if (dst_w == src_w && dst_h == src_h) {
  845. /* No scaling, defer to regular blit */
  846. return SDL_BlitSurface(src, srcrect, dst, dstrect);
  847. }
  848. scaling_w = (double)dst_w / src_w;
  849. scaling_h = (double)dst_h / src_h;
  850. if (!dstrect) {
  851. dst_x0 = 0;
  852. dst_y0 = 0;
  853. dst_x1 = dst_w;
  854. dst_y1 = dst_h;
  855. } else {
  856. dst_x0 = dstrect->x;
  857. dst_y0 = dstrect->y;
  858. dst_x1 = dst_x0 + dst_w;
  859. dst_y1 = dst_y0 + dst_h;
  860. }
  861. if (!srcrect) {
  862. src_x0 = 0;
  863. src_y0 = 0;
  864. src_x1 = src_w;
  865. src_y1 = src_h;
  866. } else {
  867. src_x0 = srcrect->x;
  868. src_y0 = srcrect->y;
  869. src_x1 = src_x0 + src_w;
  870. src_y1 = src_y0 + src_h;
  871. /* Clip source rectangle to the source surface */
  872. if (src_x0 < 0) {
  873. dst_x0 -= src_x0 * scaling_w;
  874. src_x0 = 0;
  875. }
  876. if (src_x1 > src->w) {
  877. dst_x1 -= (src_x1 - src->w) * scaling_w;
  878. src_x1 = src->w;
  879. }
  880. if (src_y0 < 0) {
  881. dst_y0 -= src_y0 * scaling_h;
  882. src_y0 = 0;
  883. }
  884. if (src_y1 > src->h) {
  885. dst_y1 -= (src_y1 - src->h) * scaling_h;
  886. src_y1 = src->h;
  887. }
  888. }
  889. /* Clip destination rectangle to the clip rectangle */
  890. clip_rect = &dst->internal->clip_rect;
  891. /* Translate to clip space for easier calculations */
  892. dst_x0 -= clip_rect->x;
  893. dst_x1 -= clip_rect->x;
  894. dst_y0 -= clip_rect->y;
  895. dst_y1 -= clip_rect->y;
  896. if (dst_x0 < 0) {
  897. src_x0 -= dst_x0 / scaling_w;
  898. dst_x0 = 0;
  899. }
  900. if (dst_x1 > clip_rect->w) {
  901. src_x1 -= (dst_x1 - clip_rect->w) / scaling_w;
  902. dst_x1 = clip_rect->w;
  903. }
  904. if (dst_y0 < 0) {
  905. src_y0 -= dst_y0 / scaling_h;
  906. dst_y0 = 0;
  907. }
  908. if (dst_y1 > clip_rect->h) {
  909. src_y1 -= (dst_y1 - clip_rect->h) / scaling_h;
  910. dst_y1 = clip_rect->h;
  911. }
  912. /* Translate back to surface coordinates */
  913. dst_x0 += clip_rect->x;
  914. dst_x1 += clip_rect->x;
  915. dst_y0 += clip_rect->y;
  916. dst_y1 += clip_rect->y;
  917. final_src.x = (int)SDL_round(src_x0);
  918. final_src.y = (int)SDL_round(src_y0);
  919. final_src.w = (int)SDL_round(src_x1 - src_x0);
  920. final_src.h = (int)SDL_round(src_y1 - src_y0);
  921. final_dst.x = (int)SDL_round(dst_x0);
  922. final_dst.y = (int)SDL_round(dst_y0);
  923. final_dst.w = (int)SDL_round(dst_x1 - dst_x0);
  924. final_dst.h = (int)SDL_round(dst_y1 - dst_y0);
  925. /* Clip again */
  926. {
  927. SDL_Rect tmp;
  928. tmp.x = 0;
  929. tmp.y = 0;
  930. tmp.w = src->w;
  931. tmp.h = src->h;
  932. SDL_GetRectIntersection(&tmp, &final_src, &final_src);
  933. }
  934. /* Clip again */
  935. SDL_GetRectIntersection(clip_rect, &final_dst, &final_dst);
  936. if (dstrect) {
  937. *dstrect = final_dst;
  938. }
  939. if (final_dst.w == 0 || final_dst.h == 0 ||
  940. final_src.w <= 0 || final_src.h <= 0) {
  941. /* No-op. */
  942. return 0;
  943. }
  944. return SDL_BlitSurfaceUncheckedScaled(src, &final_src, dst, &final_dst, scaleMode);
  945. }
  946. /**
  947. * This is a semi-private blit function and it performs low-level surface
  948. * scaled blitting only.
  949. */
  950. int SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect,
  951. SDL_Surface *dst, const SDL_Rect *dstrect,
  952. SDL_ScaleMode scaleMode)
  953. {
  954. static const Uint32 complex_copy_flags = (SDL_COPY_MODULATE_COLOR | SDL_COPY_MODULATE_ALPHA |
  955. SDL_COPY_BLEND | SDL_COPY_BLEND_PREMULTIPLIED | SDL_COPY_ADD | SDL_COPY_ADD_PREMULTIPLIED | SDL_COPY_MOD | SDL_COPY_MUL |
  956. SDL_COPY_COLORKEY);
  957. if (scaleMode != SDL_SCALEMODE_NEAREST && scaleMode != SDL_SCALEMODE_LINEAR && scaleMode != SDL_SCALEMODE_BEST) {
  958. return SDL_InvalidParamError("scaleMode");
  959. }
  960. if (scaleMode != SDL_SCALEMODE_NEAREST) {
  961. scaleMode = SDL_SCALEMODE_LINEAR;
  962. }
  963. if (srcrect->w > SDL_MAX_UINT16 || srcrect->h > SDL_MAX_UINT16 ||
  964. dstrect->w > SDL_MAX_UINT16 || dstrect->h > SDL_MAX_UINT16) {
  965. return SDL_SetError("Size too large for scaling");
  966. }
  967. if (!(src->internal->map.info.flags & SDL_COPY_NEAREST)) {
  968. src->internal->map.info.flags |= SDL_COPY_NEAREST;
  969. SDL_InvalidateMap(&src->internal->map);
  970. }
  971. if (scaleMode == SDL_SCALEMODE_NEAREST) {
  972. if (!(src->internal->map.info.flags & complex_copy_flags) &&
  973. src->format == dst->format &&
  974. !SDL_ISPIXELFORMAT_INDEXED(src->format)) {
  975. return SDL_SoftStretch(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST);
  976. } else {
  977. return SDL_BlitSurfaceUnchecked(src, srcrect, dst, dstrect);
  978. }
  979. } else {
  980. if (!(src->internal->map.info.flags & complex_copy_flags) &&
  981. src->format == dst->format &&
  982. !SDL_ISPIXELFORMAT_INDEXED(src->format) &&
  983. SDL_BYTESPERPIXEL(src->format) == 4 &&
  984. src->format != SDL_PIXELFORMAT_ARGB2101010) {
  985. /* fast path */
  986. return SDL_SoftStretch(src, srcrect, dst, dstrect, SDL_SCALEMODE_LINEAR);
  987. } else {
  988. /* Use intermediate surface(s) */
  989. SDL_Surface *tmp1 = NULL;
  990. int ret;
  991. SDL_Rect srcrect2;
  992. int is_complex_copy_flags = (src->internal->map.info.flags & complex_copy_flags);
  993. Uint8 r, g, b;
  994. Uint8 alpha;
  995. SDL_BlendMode blendMode;
  996. /* Save source infos */
  997. SDL_GetSurfaceColorMod(src, &r, &g, &b);
  998. SDL_GetSurfaceAlphaMod(src, &alpha);
  999. SDL_GetSurfaceBlendMode(src, &blendMode);
  1000. srcrect2.x = srcrect->x;
  1001. srcrect2.y = srcrect->y;
  1002. srcrect2.w = srcrect->w;
  1003. srcrect2.h = srcrect->h;
  1004. /* Change source format if not appropriate for scaling */
  1005. if (SDL_BYTESPERPIXEL(src->format) != 4 || src->format == SDL_PIXELFORMAT_ARGB2101010) {
  1006. SDL_Rect tmprect;
  1007. SDL_PixelFormat fmt;
  1008. tmprect.x = 0;
  1009. tmprect.y = 0;
  1010. tmprect.w = src->w;
  1011. tmprect.h = src->h;
  1012. if (SDL_BYTESPERPIXEL(dst->format) == 4 && dst->format != SDL_PIXELFORMAT_ARGB2101010) {
  1013. fmt = dst->format;
  1014. } else {
  1015. fmt = SDL_PIXELFORMAT_ARGB8888;
  1016. }
  1017. tmp1 = SDL_CreateSurface(src->w, src->h, fmt);
  1018. SDL_BlitSurfaceUnchecked(src, srcrect, tmp1, &tmprect);
  1019. srcrect2.x = 0;
  1020. srcrect2.y = 0;
  1021. SDL_SetSurfaceColorMod(tmp1, r, g, b);
  1022. SDL_SetSurfaceAlphaMod(tmp1, alpha);
  1023. SDL_SetSurfaceBlendMode(tmp1, blendMode);
  1024. src = tmp1;
  1025. }
  1026. /* Intermediate scaling */
  1027. if (is_complex_copy_flags || src->format != dst->format) {
  1028. SDL_Rect tmprect;
  1029. SDL_Surface *tmp2 = SDL_CreateSurface(dstrect->w, dstrect->h, src->format);
  1030. SDL_SoftStretch(src, &srcrect2, tmp2, NULL, SDL_SCALEMODE_LINEAR);
  1031. SDL_SetSurfaceColorMod(tmp2, r, g, b);
  1032. SDL_SetSurfaceAlphaMod(tmp2, alpha);
  1033. SDL_SetSurfaceBlendMode(tmp2, blendMode);
  1034. tmprect.x = 0;
  1035. tmprect.y = 0;
  1036. tmprect.w = dstrect->w;
  1037. tmprect.h = dstrect->h;
  1038. ret = SDL_BlitSurfaceUnchecked(tmp2, &tmprect, dst, dstrect);
  1039. SDL_DestroySurface(tmp2);
  1040. } else {
  1041. ret = SDL_SoftStretch(src, &srcrect2, dst, dstrect, SDL_SCALEMODE_LINEAR);
  1042. }
  1043. SDL_DestroySurface(tmp1);
  1044. return ret;
  1045. }
  1046. }
  1047. }
  1048. /*
  1049. * Lock a surface to directly access the pixels
  1050. */
  1051. int SDL_LockSurface(SDL_Surface *surface)
  1052. {
  1053. if (!SDL_SurfaceValid(surface)) {
  1054. return SDL_InvalidParamError("surface");
  1055. }
  1056. if (!surface->internal->locked) {
  1057. #if SDL_HAVE_RLE
  1058. /* Perform the lock */
  1059. if (surface->internal->flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1060. SDL_UnRLESurface(surface, SDL_TRUE);
  1061. surface->internal->flags |= SDL_INTERNAL_SURFACE_RLEACCEL; /* save accel'd state */
  1062. }
  1063. #endif
  1064. }
  1065. /* Increment the surface lock count, for recursive locks */
  1066. ++surface->internal->locked;
  1067. surface->flags |= SDL_SURFACE_LOCKED;
  1068. /* Ready to go.. */
  1069. return 0;
  1070. }
  1071. /*
  1072. * Unlock a previously locked surface
  1073. */
  1074. void SDL_UnlockSurface(SDL_Surface *surface)
  1075. {
  1076. if (!SDL_SurfaceValid(surface)) {
  1077. return;
  1078. }
  1079. /* Only perform an unlock if we are locked */
  1080. if (!surface->internal->locked || (--surface->internal->locked > 0)) {
  1081. return;
  1082. }
  1083. #if SDL_HAVE_RLE
  1084. /* Update RLE encoded surface with new data */
  1085. if (surface->internal->flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1086. surface->internal->flags &= ~SDL_INTERNAL_SURFACE_RLEACCEL; /* stop lying */
  1087. SDL_RLESurface(surface);
  1088. }
  1089. #endif
  1090. surface->flags &= ~SDL_SURFACE_LOCKED;
  1091. }
  1092. static int SDL_FlipSurfaceHorizontal(SDL_Surface *surface)
  1093. {
  1094. SDL_bool isstack;
  1095. Uint8 *row, *a, *b, *tmp;
  1096. int i, j, bpp;
  1097. if (SDL_BITSPERPIXEL(surface->format) < 8) {
  1098. /* We could implement this if needed, but we'd have to flip sets of bits within a byte */
  1099. return SDL_Unsupported();
  1100. }
  1101. if (surface->h <= 0) {
  1102. return 0;
  1103. }
  1104. if (surface->w <= 1) {
  1105. return 0;
  1106. }
  1107. bpp = SDL_BYTESPERPIXEL(surface->format);
  1108. row = (Uint8 *)surface->pixels;
  1109. tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
  1110. for (i = surface->h; i--; ) {
  1111. a = row;
  1112. b = a + (surface->w - 1) * bpp;
  1113. for (j = surface->w / 2; j--; ) {
  1114. SDL_memcpy(tmp, a, bpp);
  1115. SDL_memcpy(a, b, bpp);
  1116. SDL_memcpy(b, tmp, bpp);
  1117. a += bpp;
  1118. b -= bpp;
  1119. }
  1120. row += surface->pitch;
  1121. }
  1122. SDL_small_free(tmp, isstack);
  1123. return 0;
  1124. }
  1125. static int SDL_FlipSurfaceVertical(SDL_Surface *surface)
  1126. {
  1127. SDL_bool isstack;
  1128. Uint8 *a, *b, *tmp;
  1129. int i;
  1130. if (surface->h <= 1) {
  1131. return 0;
  1132. }
  1133. a = (Uint8 *)surface->pixels;
  1134. b = a + (surface->h - 1) * surface->pitch;
  1135. tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
  1136. for (i = surface->h / 2; i--; ) {
  1137. SDL_memcpy(tmp, a, surface->pitch);
  1138. SDL_memcpy(a, b, surface->pitch);
  1139. SDL_memcpy(b, tmp, surface->pitch);
  1140. a += surface->pitch;
  1141. b -= surface->pitch;
  1142. }
  1143. SDL_small_free(tmp, isstack);
  1144. return 0;
  1145. }
  1146. int SDL_FlipSurface(SDL_Surface *surface, SDL_FlipMode flip)
  1147. {
  1148. if (!SDL_SurfaceValid(surface)) {
  1149. return SDL_InvalidParamError("surface");
  1150. }
  1151. if (!surface->pixels) {
  1152. return 0;
  1153. }
  1154. switch (flip) {
  1155. case SDL_FLIP_HORIZONTAL:
  1156. return SDL_FlipSurfaceHorizontal(surface);
  1157. case SDL_FLIP_VERTICAL:
  1158. return SDL_FlipSurfaceVertical(surface);
  1159. default:
  1160. return SDL_InvalidParamError("flip");
  1161. }
  1162. }
  1163. SDL_Surface *SDL_ConvertSurfaceAndColorspace(SDL_Surface *surface, SDL_PixelFormat format, SDL_Palette *palette, SDL_Colorspace colorspace, SDL_PropertiesID props)
  1164. {
  1165. SDL_Palette *temp_palette = NULL;
  1166. SDL_Surface *convert = NULL;
  1167. SDL_Colorspace src_colorspace;
  1168. SDL_PropertiesID src_properties;
  1169. Uint32 copy_flags;
  1170. SDL_Color copy_color;
  1171. SDL_Rect bounds;
  1172. int ret;
  1173. SDL_bool palette_ck_transform = SDL_FALSE;
  1174. Uint8 palette_ck_value = 0;
  1175. Uint8 *palette_saved_alpha = NULL;
  1176. int palette_saved_alpha_ncolors = 0;
  1177. if (!SDL_SurfaceValid(surface)) {
  1178. SDL_InvalidParamError("surface");
  1179. goto error;
  1180. }
  1181. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1182. SDL_InvalidParamError("format");
  1183. goto error;
  1184. }
  1185. /* Check for empty destination palette! (results in empty image) */
  1186. if (palette) {
  1187. int i;
  1188. for (i = 0; i < palette->ncolors; ++i) {
  1189. if ((palette->colors[i].r != 0xFF) || (palette->colors[i].g != 0xFF) || (palette->colors[i].b != 0xFF)) {
  1190. break;
  1191. }
  1192. }
  1193. if (i == palette->ncolors) {
  1194. SDL_SetError("Empty destination palette");
  1195. goto error;
  1196. }
  1197. } else if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1198. // Create a dither palette for conversion
  1199. temp_palette = SDL_CreatePalette(1 << SDL_BITSPERPIXEL(format));
  1200. if (temp_palette) {
  1201. SDL_DitherPalette(temp_palette);
  1202. palette = temp_palette;
  1203. }
  1204. }
  1205. src_colorspace = SDL_GetSurfaceColorspace(surface);
  1206. src_properties = surface->internal->props;
  1207. /* Create a new surface with the desired format */
  1208. convert = SDL_CreateSurface(surface->w, surface->h, format);
  1209. if (!convert) {
  1210. goto error;
  1211. }
  1212. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1213. SDL_SetSurfacePalette(convert, palette);
  1214. }
  1215. if (colorspace == SDL_COLORSPACE_UNKNOWN) {
  1216. colorspace = src_colorspace;
  1217. }
  1218. SDL_SetSurfaceColorspace(convert, colorspace);
  1219. if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  1220. if (SDL_ConvertPixelsAndColorspace(surface->w, surface->h, surface->format, src_colorspace, src_properties, surface->pixels, surface->pitch, convert->format, colorspace, props, convert->pixels, convert->pitch) < 0) {
  1221. goto error;
  1222. }
  1223. /* Save the original copy flags */
  1224. copy_flags = surface->internal->map.info.flags;
  1225. goto end;
  1226. }
  1227. /* Save the original copy flags */
  1228. copy_flags = surface->internal->map.info.flags;
  1229. copy_color.r = surface->internal->map.info.r;
  1230. copy_color.g = surface->internal->map.info.g;
  1231. copy_color.b = surface->internal->map.info.b;
  1232. copy_color.a = surface->internal->map.info.a;
  1233. surface->internal->map.info.r = 0xFF;
  1234. surface->internal->map.info.g = 0xFF;
  1235. surface->internal->map.info.b = 0xFF;
  1236. surface->internal->map.info.a = 0xFF;
  1237. surface->internal->map.info.flags = (copy_flags & (SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY));
  1238. SDL_InvalidateMap(&surface->internal->map);
  1239. /* Copy over the image data */
  1240. bounds.x = 0;
  1241. bounds.y = 0;
  1242. bounds.w = surface->w;
  1243. bounds.h = surface->h;
  1244. /* Source surface has a palette with no real alpha (0 or OPAQUE).
  1245. * Destination format has alpha.
  1246. * -> set alpha channel to be opaque */
  1247. if (surface->internal->palette && SDL_ISPIXELFORMAT_ALPHA(format)) {
  1248. SDL_bool set_opaque = SDL_FALSE;
  1249. SDL_bool is_opaque, has_alpha_channel;
  1250. SDL_DetectPalette(surface->internal->palette, &is_opaque, &has_alpha_channel);
  1251. if (is_opaque) {
  1252. if (!has_alpha_channel) {
  1253. set_opaque = SDL_TRUE;
  1254. }
  1255. }
  1256. /* Set opaque and backup palette alpha values */
  1257. if (set_opaque) {
  1258. int i;
  1259. palette_saved_alpha_ncolors = surface->internal->palette->ncolors;
  1260. if (palette_saved_alpha_ncolors > 0) {
  1261. palette_saved_alpha = SDL_stack_alloc(Uint8, palette_saved_alpha_ncolors);
  1262. for (i = 0; i < palette_saved_alpha_ncolors; i++) {
  1263. palette_saved_alpha[i] = surface->internal->palette->colors[i].a;
  1264. surface->internal->palette->colors[i].a = SDL_ALPHA_OPAQUE;
  1265. }
  1266. }
  1267. }
  1268. }
  1269. /* Transform colorkey to alpha. for cases where source palette has duplicate values, and colorkey is one of them */
  1270. if (copy_flags & SDL_COPY_COLORKEY) {
  1271. if (surface->internal->palette && !palette) {
  1272. palette_ck_transform = SDL_TRUE;
  1273. palette_ck_value = surface->internal->palette->colors[surface->internal->map.info.colorkey].a;
  1274. surface->internal->palette->colors[surface->internal->map.info.colorkey].a = SDL_ALPHA_TRANSPARENT;
  1275. }
  1276. }
  1277. ret = SDL_BlitSurfaceUnchecked(surface, &bounds, convert, &bounds);
  1278. /* Restore colorkey alpha value */
  1279. if (palette_ck_transform) {
  1280. surface->internal->palette->colors[surface->internal->map.info.colorkey].a = palette_ck_value;
  1281. }
  1282. /* Restore palette alpha values */
  1283. if (palette_saved_alpha) {
  1284. int i;
  1285. for (i = 0; i < palette_saved_alpha_ncolors; i++) {
  1286. surface->internal->palette->colors[i].a = palette_saved_alpha[i];
  1287. }
  1288. SDL_stack_free(palette_saved_alpha);
  1289. }
  1290. /* Clean up the original surface, and update converted surface */
  1291. convert->internal->map.info.r = copy_color.r;
  1292. convert->internal->map.info.g = copy_color.g;
  1293. convert->internal->map.info.b = copy_color.b;
  1294. convert->internal->map.info.a = copy_color.a;
  1295. convert->internal->map.info.flags =
  1296. (copy_flags &
  1297. ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
  1298. SDL_COPY_RLE_ALPHAKEY));
  1299. surface->internal->map.info.r = copy_color.r;
  1300. surface->internal->map.info.g = copy_color.g;
  1301. surface->internal->map.info.b = copy_color.b;
  1302. surface->internal->map.info.a = copy_color.a;
  1303. surface->internal->map.info.flags = copy_flags;
  1304. SDL_InvalidateMap(&surface->internal->map);
  1305. /* SDL_BlitSurfaceUnchecked failed, and so the conversion */
  1306. if (ret < 0) {
  1307. goto error;
  1308. }
  1309. if (copy_flags & SDL_COPY_COLORKEY) {
  1310. SDL_bool set_colorkey_by_color = SDL_FALSE;
  1311. SDL_bool convert_colorkey = SDL_TRUE;
  1312. if (surface->internal->palette) {
  1313. if (palette &&
  1314. surface->internal->palette->ncolors <= palette->ncolors &&
  1315. (SDL_memcmp(surface->internal->palette->colors, palette->colors,
  1316. surface->internal->palette->ncolors * sizeof(SDL_Color)) == 0)) {
  1317. /* The palette is identical, just set the same colorkey */
  1318. SDL_SetSurfaceColorKey(convert, 1, surface->internal->map.info.colorkey);
  1319. } else if (!palette) {
  1320. if (SDL_ISPIXELFORMAT_ALPHA(format)) {
  1321. /* No need to add the colorkey, transparency is in the alpha channel*/
  1322. } else {
  1323. /* Only set the colorkey information */
  1324. set_colorkey_by_color = SDL_TRUE;
  1325. convert_colorkey = SDL_FALSE;
  1326. }
  1327. } else {
  1328. set_colorkey_by_color = SDL_TRUE;
  1329. }
  1330. } else {
  1331. set_colorkey_by_color = SDL_TRUE;
  1332. }
  1333. if (set_colorkey_by_color) {
  1334. SDL_Surface *tmp;
  1335. SDL_Surface *tmp2;
  1336. int converted_colorkey = 0;
  1337. /* Create a dummy surface to get the colorkey converted */
  1338. tmp = SDL_CreateSurface(1, 1, surface->format);
  1339. if (!tmp) {
  1340. goto error;
  1341. }
  1342. /* Share the palette, if any */
  1343. if (surface->internal->palette) {
  1344. SDL_SetSurfacePalette(tmp, surface->internal->palette);
  1345. }
  1346. SDL_FillSurfaceRect(tmp, NULL, surface->internal->map.info.colorkey);
  1347. tmp->internal->map.info.flags &= ~SDL_COPY_COLORKEY;
  1348. /* Conversion of the colorkey */
  1349. tmp2 = SDL_ConvertSurfaceAndColorspace(tmp, format, palette, colorspace, props);
  1350. if (!tmp2) {
  1351. SDL_DestroySurface(tmp);
  1352. goto error;
  1353. }
  1354. /* Get the converted colorkey */
  1355. SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->internal->format->bytes_per_pixel);
  1356. SDL_DestroySurface(tmp);
  1357. SDL_DestroySurface(tmp2);
  1358. /* Set the converted colorkey on the new surface */
  1359. SDL_SetSurfaceColorKey(convert, 1, converted_colorkey);
  1360. /* This is needed when converting for 3D texture upload */
  1361. if (convert_colorkey) {
  1362. SDL_ConvertColorkeyToAlpha(convert, SDL_TRUE);
  1363. }
  1364. }
  1365. }
  1366. end:
  1367. if (temp_palette) {
  1368. SDL_DestroyPalette(temp_palette);
  1369. }
  1370. SDL_SetSurfaceClipRect(convert, &surface->internal->clip_rect);
  1371. /* Enable alpha blending by default if the new surface has an
  1372. * alpha channel or alpha modulation */
  1373. if (SDL_ISPIXELFORMAT_ALPHA(format) ||
  1374. (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
  1375. SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
  1376. }
  1377. if (copy_flags & SDL_COPY_RLE_DESIRED) {
  1378. SDL_SetSurfaceRLE(convert, SDL_TRUE);
  1379. }
  1380. /* We're ready to go! */
  1381. return convert;
  1382. error:
  1383. if (temp_palette) {
  1384. SDL_DestroyPalette(temp_palette);
  1385. }
  1386. if (convert) {
  1387. SDL_DestroySurface(convert);
  1388. }
  1389. return NULL;
  1390. }
  1391. SDL_Surface *SDL_DuplicateSurface(SDL_Surface *surface)
  1392. {
  1393. if (!SDL_SurfaceValid(surface)) {
  1394. SDL_InvalidParamError("surface");
  1395. return NULL;
  1396. }
  1397. return SDL_ConvertSurfaceAndColorspace(surface, surface->format, surface->internal->palette, SDL_GetSurfaceColorspace(surface), surface->internal->props);
  1398. }
  1399. SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, SDL_PixelFormat format)
  1400. {
  1401. if (!SDL_SurfaceValid(surface)) {
  1402. SDL_InvalidParamError("surface");
  1403. return NULL;
  1404. }
  1405. return SDL_ConvertSurfaceAndColorspace(surface, format, NULL, SDL_GetDefaultColorspaceForFormat(format), surface->internal->props);
  1406. }
  1407. SDL_Surface *SDL_DuplicatePixels(int width, int height, SDL_PixelFormat format, SDL_Colorspace colorspace, void *pixels, int pitch)
  1408. {
  1409. SDL_Surface *surface = SDL_CreateSurface(width, height, format);
  1410. if (surface) {
  1411. int length = width * SDL_BYTESPERPIXEL(format);
  1412. Uint8 *src = (Uint8 *)pixels;
  1413. Uint8 *dst = (Uint8 *)surface->pixels;
  1414. int rows = height;
  1415. while (rows--) {
  1416. SDL_memcpy(dst, src, length);
  1417. dst += surface->pitch;
  1418. src += pitch;
  1419. }
  1420. SDL_SetSurfaceColorspace(surface, colorspace);
  1421. }
  1422. return surface;
  1423. }
  1424. int SDL_ConvertPixelsAndColorspace(int width, int height,
  1425. SDL_PixelFormat src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
  1426. SDL_PixelFormat dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
  1427. {
  1428. SDL_InternalSurface src_data, dst_data;
  1429. SDL_Surface *src_surface;
  1430. SDL_Surface *dst_surface;
  1431. SDL_Rect rect;
  1432. void *nonconst_src = (void *)src;
  1433. int ret;
  1434. if (!src) {
  1435. return SDL_InvalidParamError("src");
  1436. }
  1437. if (!src_pitch) {
  1438. return SDL_InvalidParamError("src_pitch");
  1439. }
  1440. if (!dst) {
  1441. return SDL_InvalidParamError("dst");
  1442. }
  1443. if (!dst_pitch) {
  1444. return SDL_InvalidParamError("dst_pitch");
  1445. }
  1446. if (src_colorspace == SDL_COLORSPACE_UNKNOWN) {
  1447. src_colorspace = SDL_GetDefaultColorspaceForFormat(src_format);
  1448. }
  1449. if (dst_colorspace == SDL_COLORSPACE_UNKNOWN) {
  1450. dst_colorspace = SDL_GetDefaultColorspaceForFormat(dst_format);
  1451. }
  1452. #if SDL_HAVE_YUV
  1453. if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1454. return SDL_ConvertPixels_YUV_to_YUV(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
  1455. } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
  1456. return SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
  1457. } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1458. return SDL_ConvertPixels_RGB_to_YUV(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
  1459. }
  1460. #else
  1461. if (SDL_ISPIXELFORMAT_FOURCC(src_format) || SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1462. return SDL_SetError("SDL not built with YUV support");
  1463. }
  1464. #endif
  1465. /* Fast path for same format copy */
  1466. if (src_format == dst_format && src_colorspace == dst_colorspace) {
  1467. int i;
  1468. const int bpp = SDL_BYTESPERPIXEL(src_format);
  1469. width *= bpp;
  1470. for (i = height; i--;) {
  1471. SDL_memcpy(dst, src, width);
  1472. src = (const Uint8 *)src + src_pitch;
  1473. dst = (Uint8 *)dst + dst_pitch;
  1474. }
  1475. return 0;
  1476. }
  1477. src_surface = SDL_InitializeSurface(&src_data, width, height, src_format, src_colorspace, src_properties, nonconst_src, src_pitch, SDL_TRUE);
  1478. if (!src_surface) {
  1479. return -1;
  1480. }
  1481. SDL_SetSurfaceBlendMode(src_surface, SDL_BLENDMODE_NONE);
  1482. dst_surface = SDL_InitializeSurface(&dst_data, width, height, dst_format, dst_colorspace, dst_properties, dst, dst_pitch, SDL_TRUE);
  1483. if (!dst_surface) {
  1484. return -1;
  1485. }
  1486. /* Set up the rect and go! */
  1487. rect.x = 0;
  1488. rect.y = 0;
  1489. rect.w = width;
  1490. rect.h = height;
  1491. ret = SDL_BlitSurfaceUnchecked(src_surface, &rect, dst_surface, &rect);
  1492. SDL_DestroySurface(src_surface);
  1493. SDL_DestroySurface(dst_surface);
  1494. return ret;
  1495. }
  1496. int SDL_ConvertPixels(int width, int height,
  1497. SDL_PixelFormat src_format, const void *src, int src_pitch,
  1498. SDL_PixelFormat dst_format, void *dst, int dst_pitch)
  1499. {
  1500. return SDL_ConvertPixelsAndColorspace(width, height,
  1501. src_format, SDL_COLORSPACE_UNKNOWN, 0, src, src_pitch,
  1502. dst_format, SDL_COLORSPACE_UNKNOWN, 0, dst, dst_pitch);
  1503. }
  1504. /*
  1505. * Premultiply the alpha on a block of pixels
  1506. *
  1507. * Here are some ideas for optimization:
  1508. * https://github.com/Wizermil/premultiply_alpha/tree/master/premultiply_alpha
  1509. * https://developer.arm.com/documentation/101964/0201/Pre-multiplied-alpha-channel-data
  1510. */
  1511. static void SDL_PremultiplyAlpha_AXYZ8888(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1512. {
  1513. int c;
  1514. Uint32 srcpixel;
  1515. Uint32 srcR, srcG, srcB, srcA;
  1516. Uint32 dstpixel;
  1517. Uint32 dstR, dstG, dstB, dstA;
  1518. while (height--) {
  1519. const Uint32 *src_px = (const Uint32 *)src;
  1520. Uint32 *dst_px = (Uint32 *)dst;
  1521. for (c = width; c; --c) {
  1522. /* Component bytes extraction. */
  1523. srcpixel = *src_px++;
  1524. RGBA_FROM_ARGB8888(srcpixel, srcR, srcG, srcB, srcA);
  1525. /* Alpha pre-multiplication of each component. */
  1526. dstA = srcA;
  1527. dstR = (srcA * srcR) / 255;
  1528. dstG = (srcA * srcG) / 255;
  1529. dstB = (srcA * srcB) / 255;
  1530. /* ARGB8888 pixel recomposition. */
  1531. ARGB8888_FROM_RGBA(dstpixel, dstR, dstG, dstB, dstA);
  1532. *dst_px++ = dstpixel;
  1533. }
  1534. src = (const Uint8 *)src + src_pitch;
  1535. dst = (Uint8 *)dst + dst_pitch;
  1536. }
  1537. }
  1538. static void SDL_PremultiplyAlpha_XYZA8888(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1539. {
  1540. int c;
  1541. Uint32 srcpixel;
  1542. Uint32 srcR, srcG, srcB, srcA;
  1543. Uint32 dstpixel;
  1544. Uint32 dstR, dstG, dstB, dstA;
  1545. while (height--) {
  1546. const Uint32 *src_px = (const Uint32 *)src;
  1547. Uint32 *dst_px = (Uint32 *)dst;
  1548. for (c = width; c; --c) {
  1549. /* Component bytes extraction. */
  1550. srcpixel = *src_px++;
  1551. RGBA_FROM_RGBA8888(srcpixel, srcR, srcG, srcB, srcA);
  1552. /* Alpha pre-multiplication of each component. */
  1553. dstA = srcA;
  1554. dstR = (srcA * srcR) / 255;
  1555. dstG = (srcA * srcG) / 255;
  1556. dstB = (srcA * srcB) / 255;
  1557. /* RGBA8888 pixel recomposition. */
  1558. RGBA8888_FROM_RGBA(dstpixel, dstR, dstG, dstB, dstA);
  1559. *dst_px++ = dstpixel;
  1560. }
  1561. src = (const Uint8 *)src + src_pitch;
  1562. dst = (Uint8 *)dst + dst_pitch;
  1563. }
  1564. }
  1565. static void SDL_PremultiplyAlpha_AXYZ128(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1566. {
  1567. int c;
  1568. float flR, flG, flB, flA;
  1569. while (height--) {
  1570. const float *src_px = (const float *)src;
  1571. float *dst_px = (float *)dst;
  1572. for (c = width; c; --c) {
  1573. flA = *src_px++;
  1574. flR = *src_px++;
  1575. flG = *src_px++;
  1576. flB = *src_px++;
  1577. /* Alpha pre-multiplication of each component. */
  1578. flR *= flA;
  1579. flG *= flA;
  1580. flB *= flA;
  1581. *dst_px++ = flA;
  1582. *dst_px++ = flR;
  1583. *dst_px++ = flG;
  1584. *dst_px++ = flB;
  1585. }
  1586. src = (const Uint8 *)src + src_pitch;
  1587. dst = (Uint8 *)dst + dst_pitch;
  1588. }
  1589. }
  1590. static int SDL_PremultiplyAlphaPixelsAndColorspace(int width, int height, SDL_PixelFormat src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch, SDL_PixelFormat dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch, SDL_bool linear)
  1591. {
  1592. SDL_Surface *convert = NULL;
  1593. void *final_dst = dst;
  1594. int final_dst_pitch = dst_pitch;
  1595. SDL_PixelFormat format;
  1596. SDL_Colorspace colorspace;
  1597. int result = -1;
  1598. if (!src) {
  1599. return SDL_InvalidParamError("src");
  1600. }
  1601. if (!src_pitch) {
  1602. return SDL_InvalidParamError("src_pitch");
  1603. }
  1604. if (!dst) {
  1605. return SDL_InvalidParamError("dst");
  1606. }
  1607. if (!dst_pitch) {
  1608. return SDL_InvalidParamError("dst_pitch");
  1609. }
  1610. // Use a high precision format if we're converting to linear colorspace or using high precision pixel formats
  1611. if (linear ||
  1612. SDL_ISPIXELFORMAT_10BIT(src_format) || SDL_BITSPERPIXEL(src_format) > 32 ||
  1613. SDL_ISPIXELFORMAT_10BIT(dst_format) || SDL_BITSPERPIXEL(dst_format) > 32) {
  1614. if (src_format == SDL_PIXELFORMAT_ARGB128_FLOAT ||
  1615. src_format == SDL_PIXELFORMAT_ABGR128_FLOAT) {
  1616. format = src_format;
  1617. } else {
  1618. format = SDL_PIXELFORMAT_ARGB128_FLOAT;
  1619. }
  1620. } else {
  1621. if (src_format == SDL_PIXELFORMAT_ARGB8888 ||
  1622. src_format == SDL_PIXELFORMAT_ABGR8888 ||
  1623. src_format == SDL_PIXELFORMAT_RGBA8888 ||
  1624. src_format == SDL_PIXELFORMAT_BGRA8888) {
  1625. format = src_format;
  1626. } else {
  1627. format = SDL_PIXELFORMAT_ARGB8888;
  1628. }
  1629. }
  1630. if (linear) {
  1631. colorspace = SDL_COLORSPACE_SRGB_LINEAR;
  1632. } else {
  1633. colorspace = SDL_COLORSPACE_SRGB;
  1634. }
  1635. if (src_format != format || src_colorspace != colorspace) {
  1636. convert = SDL_CreateSurface(width, height, format);
  1637. if (!convert) {
  1638. goto done;
  1639. }
  1640. if (SDL_ConvertPixelsAndColorspace(width, height, src_format, src_colorspace, src_properties, src, src_pitch, format, colorspace, 0, convert->pixels, convert->pitch) < 0) {
  1641. goto done;
  1642. }
  1643. src = convert->pixels;
  1644. src_pitch = convert->pitch;
  1645. dst = convert->pixels;
  1646. dst_pitch = convert->pitch;
  1647. } else if (dst_format != format || dst_colorspace != colorspace) {
  1648. convert = SDL_CreateSurface(width, height, format);
  1649. if (!convert) {
  1650. goto done;
  1651. }
  1652. dst = convert->pixels;
  1653. dst_pitch = convert->pitch;
  1654. }
  1655. switch (format) {
  1656. case SDL_PIXELFORMAT_ARGB8888:
  1657. case SDL_PIXELFORMAT_ABGR8888:
  1658. SDL_PremultiplyAlpha_AXYZ8888(width, height, src, src_pitch, dst, dst_pitch);
  1659. break;
  1660. case SDL_PIXELFORMAT_RGBA8888:
  1661. case SDL_PIXELFORMAT_BGRA8888:
  1662. SDL_PremultiplyAlpha_XYZA8888(width, height, src, src_pitch, dst, dst_pitch);
  1663. break;
  1664. case SDL_PIXELFORMAT_ARGB128_FLOAT:
  1665. case SDL_PIXELFORMAT_ABGR128_FLOAT:
  1666. SDL_PremultiplyAlpha_AXYZ128(width, height, src, src_pitch, dst, dst_pitch);
  1667. break;
  1668. default:
  1669. SDL_SetError("Unexpected internal pixel format");
  1670. goto done;
  1671. }
  1672. if (dst != final_dst) {
  1673. if (SDL_ConvertPixelsAndColorspace(width, height, format, colorspace, 0, convert->pixels, convert->pitch, dst_format, dst_colorspace, dst_properties, final_dst, final_dst_pitch) < 0) {
  1674. goto done;
  1675. }
  1676. }
  1677. result = 0;
  1678. done:
  1679. if (convert) {
  1680. SDL_DestroySurface(convert);
  1681. }
  1682. return result;
  1683. }
  1684. int SDL_PremultiplyAlpha(int width, int height,
  1685. SDL_PixelFormat src_format, const void *src, int src_pitch,
  1686. SDL_PixelFormat dst_format, void *dst, int dst_pitch, SDL_bool linear)
  1687. {
  1688. SDL_Colorspace src_colorspace = SDL_GetDefaultColorspaceForFormat(src_format);
  1689. SDL_Colorspace dst_colorspace = SDL_GetDefaultColorspaceForFormat(dst_format);
  1690. return SDL_PremultiplyAlphaPixelsAndColorspace(width, height, src_format, src_colorspace, 0, src, src_pitch, dst_format, dst_colorspace, 0, dst, dst_pitch, linear);
  1691. }
  1692. int SDL_PremultiplySurfaceAlpha(SDL_Surface *surface, SDL_bool linear)
  1693. {
  1694. SDL_Colorspace colorspace;
  1695. if (!SDL_SurfaceValid(surface)) {
  1696. return SDL_InvalidParamError("surface");
  1697. }
  1698. colorspace = SDL_GetSurfaceColorspace(surface);
  1699. return SDL_PremultiplyAlphaPixelsAndColorspace(surface->w, surface->h, surface->format, colorspace, surface->internal->props, surface->pixels, surface->pitch, surface->format, colorspace, surface->internal->props, surface->pixels, surface->pitch, linear);
  1700. }
  1701. Uint32 SDL_MapSurfaceRGB(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
  1702. {
  1703. return SDL_MapSurfaceRGBA(surface, r, g, b, SDL_ALPHA_OPAQUE);
  1704. }
  1705. Uint32 SDL_MapSurfaceRGBA(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  1706. {
  1707. if (!SDL_SurfaceValid(surface)) {
  1708. SDL_InvalidParamError("surface");
  1709. return 0;
  1710. }
  1711. return SDL_MapRGBA(surface->internal->format, surface->internal->palette, r, g, b, a);
  1712. }
  1713. /* This function Copyright 2023 Collabora Ltd., contributed to SDL under the ZLib license */
  1714. int SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
  1715. {
  1716. Uint32 pixel = 0;
  1717. size_t bytes_per_pixel;
  1718. Uint8 unused;
  1719. Uint8 *p;
  1720. int result = -1;
  1721. if (!surface || !surface->format || !surface->pixels) {
  1722. return SDL_InvalidParamError("surface");
  1723. }
  1724. if (x < 0 || x >= surface->w) {
  1725. return SDL_InvalidParamError("x");
  1726. }
  1727. if (y < 0 || y >= surface->h) {
  1728. return SDL_InvalidParamError("y");
  1729. }
  1730. if (!r) {
  1731. r = &unused;
  1732. }
  1733. if (!g) {
  1734. g = &unused;
  1735. }
  1736. if (!b) {
  1737. b = &unused;
  1738. }
  1739. if (!a) {
  1740. a = &unused;
  1741. }
  1742. bytes_per_pixel = SDL_BYTESPERPIXEL(surface->format);
  1743. if (SDL_MUSTLOCK(surface)) {
  1744. if (SDL_LockSurface(surface) < 0) {
  1745. return -1;
  1746. }
  1747. }
  1748. p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel;
  1749. if (bytes_per_pixel > sizeof(pixel)) {
  1750. /* This is really slow, but it gets the job done */
  1751. Uint8 rgba[4];
  1752. SDL_Colorspace colorspace = SDL_GetSurfaceColorspace(surface);
  1753. if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, colorspace, surface->internal->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba)) == 0) {
  1754. *r = rgba[0];
  1755. *g = rgba[1];
  1756. *b = rgba[2];
  1757. *a = rgba[3];
  1758. result = 0;
  1759. }
  1760. } else {
  1761. /* Fill the appropriate number of least-significant bytes of pixel,
  1762. * leaving the most-significant bytes set to zero */
  1763. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  1764. SDL_memcpy(((Uint8 *)&pixel) + (sizeof(pixel) - bytes_per_pixel), p, bytes_per_pixel);
  1765. #else
  1766. SDL_memcpy(&pixel, p, bytes_per_pixel);
  1767. #endif
  1768. SDL_GetRGBA(pixel, surface->internal->format, surface->internal->palette, r, g, b, a);
  1769. result = 0;
  1770. }
  1771. if (SDL_MUSTLOCK(surface)) {
  1772. SDL_UnlockSurface(surface);
  1773. }
  1774. return result;
  1775. }
  1776. /*
  1777. * Free a surface created by the above function.
  1778. */
  1779. void SDL_DestroySurface(SDL_Surface *surface)
  1780. {
  1781. if (!SDL_SurfaceValid(surface)) {
  1782. return;
  1783. }
  1784. if (surface->internal->flags & SDL_INTERNAL_SURFACE_DONTFREE) {
  1785. return;
  1786. }
  1787. if (--surface->refcount > 0) {
  1788. return;
  1789. }
  1790. SDL_DestroyProperties(surface->internal->props);
  1791. SDL_InvalidateMap(&surface->internal->map);
  1792. SDL_InvalidateAllBlitMap(surface);
  1793. while (surface->internal->locked > 0) {
  1794. SDL_UnlockSurface(surface);
  1795. }
  1796. #if SDL_HAVE_RLE
  1797. if (surface->internal->flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1798. SDL_UnRLESurface(surface, SDL_FALSE);
  1799. }
  1800. #endif
  1801. SDL_SetSurfacePalette(surface, NULL);
  1802. if (surface->flags & SDL_SURFACE_PREALLOCATED) {
  1803. /* Don't free */
  1804. } else if (surface->flags & SDL_SURFACE_SIMD_ALIGNED) {
  1805. /* Free aligned */
  1806. SDL_aligned_free(surface->pixels);
  1807. } else {
  1808. /* Normal */
  1809. SDL_free(surface->pixels);
  1810. }
  1811. if (!(surface->internal->flags & SDL_INTERNAL_SURFACE_STACK)) {
  1812. SDL_free(surface);
  1813. }
  1814. }