2
0

SDL_surface.c 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <[email protected]>
  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_RLEaccel_c.h"
  22. #include "SDL_pixels_c.h"
  23. #include "SDL_stb_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. // Magic!
  32. static char SDL_surface_magic;
  33. // Public routines
  34. bool SDL_SurfaceValid(SDL_Surface *surface)
  35. {
  36. return (surface && surface->reserved == &SDL_surface_magic);
  37. }
  38. void SDL_UpdateSurfaceLockFlag(SDL_Surface *surface)
  39. {
  40. if (SDL_SurfaceHasRLE(surface)) {
  41. surface->flags |= SDL_SURFACE_LOCK_NEEDED;
  42. } else {
  43. surface->flags &= ~SDL_SURFACE_LOCK_NEEDED;
  44. }
  45. }
  46. /*
  47. * Calculate the pad-aligned scanline width of a surface.
  48. *
  49. * for FOURCC, use SDL_CalculateYUVSize()
  50. */
  51. static bool SDL_CalculateRGBSize(Uint32 format, size_t width, size_t height, size_t *size, size_t *pitch, bool minimal)
  52. {
  53. if (SDL_BITSPERPIXEL(format) >= 8) {
  54. if (!SDL_size_mul_check_overflow(width, SDL_BYTESPERPIXEL(format), pitch)) {
  55. return SDL_SetError("width * bpp would overflow");
  56. }
  57. } else {
  58. if (!SDL_size_mul_check_overflow(width, SDL_BITSPERPIXEL(format), pitch)) {
  59. return SDL_SetError("width * bpp would overflow");
  60. }
  61. if (!SDL_size_add_check_overflow(*pitch, 7, pitch)) {
  62. return SDL_SetError("aligning pitch would overflow");
  63. }
  64. *pitch /= 8;
  65. }
  66. if (!minimal) {
  67. // 4-byte aligning for speed
  68. if (!SDL_size_add_check_overflow(*pitch, 3, pitch)) {
  69. return SDL_SetError("aligning pitch would overflow");
  70. }
  71. *pitch &= ~3;
  72. }
  73. if (!SDL_size_mul_check_overflow(height, *pitch, size)) {
  74. return SDL_SetError("height * pitch would overflow");
  75. }
  76. return true;
  77. }
  78. bool SDL_CalculateSurfaceSize(SDL_PixelFormat format, int width, int height, size_t *size, size_t *pitch, bool minimalPitch)
  79. {
  80. size_t p = 0, sz = 0;
  81. if (size) {
  82. *size = 0;
  83. }
  84. if (pitch) {
  85. *pitch = 0;
  86. }
  87. if (SDL_ISPIXELFORMAT_FOURCC(format)) {
  88. if (format == SDL_PIXELFORMAT_MJPG) {
  89. // We don't know in advance what it will be, we'll figure it out later.
  90. return true;
  91. }
  92. if (!SDL_CalculateYUVSize(format, width, height, &sz, &p)) {
  93. // Overflow...
  94. return false;
  95. }
  96. } else {
  97. if (!SDL_CalculateRGBSize(format, width, height, &sz, &p, minimalPitch)) {
  98. // Overflow...
  99. return false;
  100. }
  101. }
  102. if (size) {
  103. *size = sz;
  104. }
  105. if (pitch) {
  106. *pitch = p;
  107. }
  108. return true;
  109. }
  110. static bool SDL_InitializeSurface(SDL_Surface *surface, int width, int height, SDL_PixelFormat format, SDL_Colorspace colorspace, SDL_PropertiesID props, void *pixels, int pitch, bool onstack)
  111. {
  112. SDL_zerop(surface);
  113. surface->flags = SDL_SURFACE_PREALLOCATED;
  114. surface->format = format;
  115. surface->w = width;
  116. surface->h = height;
  117. surface->pixels = pixels;
  118. surface->pitch = pitch;
  119. surface->reserved = &SDL_surface_magic;
  120. if (onstack) {
  121. surface->internal_flags |= SDL_INTERNAL_SURFACE_STACK;
  122. }
  123. surface->fmt = SDL_GetPixelFormatDetails(format);
  124. if (!surface->fmt) {
  125. SDL_DestroySurface(surface);
  126. return false;
  127. }
  128. // Initialize the clip rect
  129. surface->clip_rect.w = width;
  130. surface->clip_rect.h = height;
  131. // Allocate an empty mapping
  132. surface->map.info.r = 0xFF;
  133. surface->map.info.g = 0xFF;
  134. surface->map.info.b = 0xFF;
  135. surface->map.info.a = 0xFF;
  136. if (colorspace == SDL_COLORSPACE_UNKNOWN) {
  137. surface->colorspace = SDL_GetDefaultColorspaceForFormat(format);
  138. } else {
  139. surface->colorspace = colorspace;
  140. }
  141. if (props) {
  142. if (!SDL_CopyProperties(props, SDL_GetSurfaceProperties(surface))) {
  143. SDL_DestroySurface(surface);
  144. return false;
  145. }
  146. }
  147. // By default surfaces with an alpha mask are set up for blending
  148. if (SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  149. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  150. }
  151. // The surface is ready to go
  152. surface->refcount = 1;
  153. return true;
  154. }
  155. /*
  156. * Create an empty surface of the appropriate depth using the given format
  157. */
  158. SDL_Surface *SDL_CreateSurface(int width, int height, SDL_PixelFormat format)
  159. {
  160. size_t pitch, size;
  161. SDL_Surface *surface;
  162. if (width < 0) {
  163. SDL_InvalidParamError("width");
  164. return NULL;
  165. }
  166. if (height < 0) {
  167. SDL_InvalidParamError("height");
  168. return NULL;
  169. }
  170. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  171. SDL_InvalidParamError("format");
  172. return NULL;
  173. }
  174. if (!SDL_CalculateSurfaceSize(format, width, height, &size, &pitch, false /* not minimal pitch */)) {
  175. // Overflow...
  176. return NULL;
  177. }
  178. // Allocate and initialize the surface
  179. surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
  180. if (!surface) {
  181. return NULL;
  182. }
  183. if (!SDL_InitializeSurface(surface, width, height, format, SDL_COLORSPACE_UNKNOWN, 0, NULL, (int)pitch, false)) {
  184. return NULL;
  185. }
  186. if (surface->w && surface->h && format != SDL_PIXELFORMAT_MJPG) {
  187. surface->flags &= ~SDL_SURFACE_PREALLOCATED;
  188. surface->pixels = SDL_aligned_alloc(SDL_GetSIMDAlignment(), size);
  189. if (!surface->pixels) {
  190. SDL_DestroySurface(surface);
  191. return NULL;
  192. }
  193. surface->flags |= SDL_SURFACE_SIMD_ALIGNED;
  194. // This is important for bitmaps
  195. SDL_memset(surface->pixels, 0, size);
  196. }
  197. return surface;
  198. }
  199. /*
  200. * Create an RGB surface from an existing memory buffer using the given
  201. * enum SDL_PIXELFORMAT_* format
  202. */
  203. SDL_Surface *SDL_CreateSurfaceFrom(int width, int height, SDL_PixelFormat format, void *pixels, int pitch)
  204. {
  205. if (width < 0) {
  206. SDL_InvalidParamError("width");
  207. return NULL;
  208. }
  209. if (height < 0) {
  210. SDL_InvalidParamError("height");
  211. return NULL;
  212. }
  213. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  214. SDL_InvalidParamError("format");
  215. return NULL;
  216. }
  217. if (pitch == 0 && !pixels) {
  218. // The application will fill these in later with valid values
  219. } else {
  220. size_t minimalPitch;
  221. if (!SDL_CalculateSurfaceSize(format, width, height, NULL, &minimalPitch, true /* minimal pitch */)) {
  222. // Overflow...
  223. return NULL;
  224. }
  225. if (pitch < 0 || (size_t)pitch < minimalPitch) {
  226. SDL_InvalidParamError("pitch");
  227. return NULL;
  228. }
  229. }
  230. // Allocate and initialize the surface
  231. SDL_Surface *surface = (SDL_Surface *)SDL_malloc(sizeof(*surface));
  232. if (!surface ||
  233. !SDL_InitializeSurface(surface, width, height, format, SDL_COLORSPACE_UNKNOWN, 0, pixels, pitch, false)) {
  234. return NULL;
  235. }
  236. return surface;
  237. }
  238. SDL_PropertiesID SDL_GetSurfaceProperties(SDL_Surface *surface)
  239. {
  240. if (!SDL_SurfaceValid(surface)) {
  241. SDL_InvalidParamError("surface");
  242. return 0;
  243. }
  244. if (!surface->props) {
  245. surface->props = SDL_CreateProperties();
  246. }
  247. return surface->props;
  248. }
  249. bool SDL_SetSurfaceColorspace(SDL_Surface *surface, SDL_Colorspace colorspace)
  250. {
  251. if (!SDL_SurfaceValid(surface)) {
  252. return SDL_InvalidParamError("surface");
  253. }
  254. surface->colorspace = colorspace;
  255. return true;
  256. }
  257. SDL_Colorspace SDL_GetSurfaceColorspace(SDL_Surface *surface)
  258. {
  259. if (!SDL_SurfaceValid(surface)) {
  260. return SDL_COLORSPACE_UNKNOWN;
  261. }
  262. return surface->colorspace;
  263. }
  264. float SDL_GetDefaultSDRWhitePoint(SDL_Colorspace colorspace)
  265. {
  266. return SDL_GetSurfaceSDRWhitePoint(NULL, colorspace);
  267. }
  268. float SDL_GetSurfaceSDRWhitePoint(SDL_Surface *surface, SDL_Colorspace colorspace)
  269. {
  270. SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
  271. if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
  272. transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  273. SDL_PropertiesID props;
  274. float default_value = 1.0f;
  275. if (SDL_SurfaceValid(surface)) {
  276. props = surface->props;
  277. } else {
  278. props = 0;
  279. }
  280. if (transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  281. /* The older standards use an SDR white point of 100 nits.
  282. * ITU-R BT.2408-6 recommends using an SDR white point of 203 nits.
  283. * This is the default Chrome uses, and what a lot of game content
  284. * assumes, so we'll go with that.
  285. */
  286. const float DEFAULT_PQ_SDR_WHITE_POINT = 203.0f;
  287. default_value = DEFAULT_PQ_SDR_WHITE_POINT;
  288. }
  289. return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_SDR_WHITE_POINT_FLOAT, default_value);
  290. }
  291. return 1.0f;
  292. }
  293. float SDL_GetDefaultHDRHeadroom(SDL_Colorspace colorspace)
  294. {
  295. return SDL_GetSurfaceHDRHeadroom(NULL, colorspace);
  296. }
  297. float SDL_GetSurfaceHDRHeadroom(SDL_Surface *surface, SDL_Colorspace colorspace)
  298. {
  299. SDL_TransferCharacteristics transfer = SDL_COLORSPACETRANSFER(colorspace);
  300. if (transfer == SDL_TRANSFER_CHARACTERISTICS_LINEAR ||
  301. transfer == SDL_TRANSFER_CHARACTERISTICS_PQ) {
  302. SDL_PropertiesID props;
  303. float default_value = 0.0f;
  304. if (SDL_SurfaceValid(surface)) {
  305. props = surface->props;
  306. } else {
  307. props = 0;
  308. }
  309. return SDL_GetFloatProperty(props, SDL_PROP_SURFACE_HDR_HEADROOM_FLOAT, default_value);
  310. }
  311. return 1.0f;
  312. }
  313. SDL_Palette *SDL_CreateSurfacePalette(SDL_Surface *surface)
  314. {
  315. SDL_Palette *palette;
  316. if (!SDL_SurfaceValid(surface)) {
  317. SDL_InvalidParamError("surface");
  318. return NULL;
  319. }
  320. if (!SDL_ISPIXELFORMAT_INDEXED(surface->format)) {
  321. SDL_SetError("The surface is not indexed format");
  322. return NULL;
  323. }
  324. palette = SDL_CreatePalette((1 << SDL_BITSPERPIXEL(surface->format)));
  325. if (!palette) {
  326. return NULL;
  327. }
  328. if (palette->ncolors == 2) {
  329. // Create a black and white bitmap palette
  330. palette->colors[0].r = 0xFF;
  331. palette->colors[0].g = 0xFF;
  332. palette->colors[0].b = 0xFF;
  333. palette->colors[1].r = 0x00;
  334. palette->colors[1].g = 0x00;
  335. palette->colors[1].b = 0x00;
  336. }
  337. if (!SDL_SetSurfacePalette(surface, palette)) {
  338. SDL_DestroyPalette(palette);
  339. return NULL;
  340. }
  341. // The surface has retained the palette, we can remove the reference here
  342. SDL_assert(palette->refcount == 2);
  343. SDL_DestroyPalette(palette);
  344. return palette;
  345. }
  346. bool SDL_SetSurfacePalette(SDL_Surface *surface, SDL_Palette *palette)
  347. {
  348. if (!SDL_SurfaceValid(surface)) {
  349. return SDL_InvalidParamError("surface");
  350. }
  351. if (palette && palette->ncolors > (1 << SDL_BITSPERPIXEL(surface->format))) {
  352. return SDL_SetError("SDL_SetSurfacePalette() passed a palette that doesn't match the surface format");
  353. }
  354. if (palette != surface->palette) {
  355. if (surface->palette) {
  356. SDL_DestroyPalette(surface->palette);
  357. }
  358. surface->palette = palette;
  359. if (surface->palette) {
  360. ++surface->palette->refcount;
  361. }
  362. }
  363. SDL_InvalidateMap(&surface->map);
  364. return true;
  365. }
  366. SDL_Palette *SDL_GetSurfacePalette(SDL_Surface *surface)
  367. {
  368. if (!SDL_SurfaceValid(surface)) {
  369. return NULL;
  370. }
  371. return surface->palette;
  372. }
  373. bool SDL_AddSurfaceAlternateImage(SDL_Surface *surface, SDL_Surface *image)
  374. {
  375. if (!SDL_SurfaceValid(surface)) {
  376. return SDL_InvalidParamError("surface");
  377. }
  378. if (!SDL_SurfaceValid(image)) {
  379. return SDL_InvalidParamError("image");
  380. }
  381. SDL_Surface **images = (SDL_Surface **)SDL_realloc(surface->images, (surface->num_images + 1) * sizeof(*images));
  382. if (!images) {
  383. return false;
  384. }
  385. images[surface->num_images] = image;
  386. surface->images = images;
  387. ++surface->num_images;
  388. ++image->refcount;
  389. return true;
  390. }
  391. bool SDL_SurfaceHasAlternateImages(SDL_Surface *surface)
  392. {
  393. if (!SDL_SurfaceValid(surface)) {
  394. return false;
  395. }
  396. return (surface->num_images > 0);
  397. }
  398. SDL_Surface **SDL_GetSurfaceImages(SDL_Surface *surface, int *count)
  399. {
  400. if (count) {
  401. *count = 0;
  402. }
  403. if (!SDL_SurfaceValid(surface)) {
  404. SDL_InvalidParamError("surface");
  405. return NULL;
  406. }
  407. int num_images = 1 + surface->num_images;
  408. SDL_Surface **images = (SDL_Surface **)SDL_malloc((num_images + 1) * sizeof(*images));
  409. if (!images) {
  410. return NULL;
  411. }
  412. images[0] = surface;
  413. if (surface->num_images > 0) {
  414. SDL_memcpy(&images[1], surface->images, (surface->num_images * sizeof(images[1])));
  415. }
  416. images[num_images] = NULL;
  417. if (count) {
  418. *count = num_images;
  419. }
  420. return images;
  421. }
  422. SDL_Surface *SDL_GetSurfaceImage(SDL_Surface *surface, float display_scale)
  423. {
  424. if (!SDL_SurfaceValid(surface)) {
  425. SDL_InvalidParamError("surface");
  426. return NULL;
  427. }
  428. if (!SDL_SurfaceHasAlternateImages(surface)) {
  429. ++surface->refcount;
  430. return surface;
  431. }
  432. // This surface has high DPI images, pick the best one available, or scale one to the correct size
  433. SDL_Surface **images = SDL_GetSurfaceImages(surface, NULL);
  434. if (!images) {
  435. // Failure, fall back to the existing surface
  436. ++surface->refcount;
  437. return surface;
  438. }
  439. // Find closest image. Images that are larger than the
  440. // desired size are preferred over images that are smaller.
  441. SDL_Surface *closest = NULL;
  442. int desired_w = (int)SDL_round(surface->w * display_scale);
  443. int desired_h = (int)SDL_round(surface->h * display_scale);
  444. int desired_size = desired_w * desired_h;
  445. int closest_distance = -1;
  446. int closest_size = -1;
  447. for (int i = 0; images[i]; ++i) {
  448. SDL_Surface *candidate = images[i];
  449. int size = candidate->w * candidate->h;
  450. int delta_w = (candidate->w - desired_w);
  451. int delta_h = (candidate->h - desired_h);
  452. int distance = (delta_w * delta_w) + (delta_h * delta_h);
  453. if (closest_distance < 0 || distance < closest_distance ||
  454. (size > desired_size && closest_size < desired_size)) {
  455. closest = candidate;
  456. closest_distance = distance;
  457. closest_size = size;
  458. }
  459. }
  460. SDL_free(images);
  461. SDL_assert(closest != NULL); // We should always have at least one surface
  462. if (closest->w == desired_w && closest->h == desired_h) {
  463. ++closest->refcount;
  464. return closest;
  465. }
  466. // We need to scale the image to the correct size. To maintain good image quality, downscaling
  467. // is done in steps, never reducing the width and height by more than half each time.
  468. SDL_Surface *scaled = closest;
  469. do {
  470. int next_scaled_w = SDL_max(desired_w, (scaled->w + 1) / 2);
  471. int next_scaled_h = SDL_max(desired_h, (scaled->h + 1) / 2);
  472. SDL_Surface *next_scaled = SDL_ScaleSurface(scaled, next_scaled_w, next_scaled_h, SDL_SCALEMODE_LINEAR);
  473. if (scaled != closest) {
  474. SDL_DestroySurface(scaled);
  475. }
  476. scaled = next_scaled;
  477. if (!scaled) {
  478. // Failure, fall back to the closest surface
  479. ++closest->refcount;
  480. return closest;
  481. }
  482. } while (scaled->w != desired_w || scaled->h != desired_h);
  483. return scaled;
  484. }
  485. void SDL_RemoveSurfaceAlternateImages(SDL_Surface *surface)
  486. {
  487. if (!SDL_SurfaceValid(surface)) {
  488. return;
  489. }
  490. if (surface->num_images > 0) {
  491. for (int i = 0; i < surface->num_images; ++i) {
  492. SDL_DestroySurface(surface->images[i]);
  493. }
  494. SDL_free(surface->images);
  495. surface->images = NULL;
  496. surface->num_images = 0;
  497. }
  498. }
  499. bool SDL_SetSurfaceRLE(SDL_Surface *surface, bool enabled)
  500. {
  501. int flags;
  502. if (!SDL_SurfaceValid(surface)) {
  503. return SDL_InvalidParamError("surface");
  504. }
  505. flags = surface->map.info.flags;
  506. if (enabled) {
  507. surface->map.info.flags |= SDL_COPY_RLE_DESIRED;
  508. } else {
  509. surface->map.info.flags &= ~SDL_COPY_RLE_DESIRED;
  510. }
  511. if (surface->map.info.flags != flags) {
  512. SDL_InvalidateMap(&surface->map);
  513. }
  514. SDL_UpdateSurfaceLockFlag(surface);
  515. return true;
  516. }
  517. bool SDL_SurfaceHasRLE(SDL_Surface *surface)
  518. {
  519. if (!SDL_SurfaceValid(surface)) {
  520. return false;
  521. }
  522. if (!(surface->map.info.flags & SDL_COPY_RLE_DESIRED)) {
  523. return false;
  524. }
  525. return true;
  526. }
  527. bool SDL_SetSurfaceColorKey(SDL_Surface *surface, bool enabled, Uint32 key)
  528. {
  529. int flags;
  530. if (!SDL_SurfaceValid(surface)) {
  531. return SDL_InvalidParamError("surface");
  532. }
  533. if (surface->palette && key >= ((Uint32)surface->palette->ncolors)) {
  534. return SDL_InvalidParamError("key");
  535. }
  536. flags = surface->map.info.flags;
  537. if (enabled) {
  538. surface->map.info.flags |= SDL_COPY_COLORKEY;
  539. surface->map.info.colorkey = key;
  540. } else {
  541. surface->map.info.flags &= ~SDL_COPY_COLORKEY;
  542. }
  543. if (surface->map.info.flags != flags) {
  544. SDL_InvalidateMap(&surface->map);
  545. }
  546. return true;
  547. }
  548. bool SDL_SurfaceHasColorKey(SDL_Surface *surface)
  549. {
  550. if (!SDL_SurfaceValid(surface)) {
  551. return false;
  552. }
  553. if (!(surface->map.info.flags & SDL_COPY_COLORKEY)) {
  554. return false;
  555. }
  556. return true;
  557. }
  558. bool SDL_GetSurfaceColorKey(SDL_Surface *surface, Uint32 *key)
  559. {
  560. if (key) {
  561. *key = 0;
  562. }
  563. if (!SDL_SurfaceValid(surface)) {
  564. return SDL_InvalidParamError("surface");
  565. }
  566. if (!(surface->map.info.flags & SDL_COPY_COLORKEY)) {
  567. return SDL_SetError("Surface doesn't have a colorkey");
  568. }
  569. if (key) {
  570. *key = surface->map.info.colorkey;
  571. }
  572. return true;
  573. }
  574. /* This is a fairly slow function to switch from colorkey to alpha
  575. NB: it doesn't handle bpp 1 or 3, because they have no alpha channel */
  576. static void SDL_ConvertColorkeyToAlpha(SDL_Surface *surface, bool ignore_alpha)
  577. {
  578. int x, y, bpp;
  579. if (!SDL_SurfaceValid(surface)) {
  580. return;
  581. }
  582. if (!(surface->map.info.flags & SDL_COPY_COLORKEY) ||
  583. !SDL_ISPIXELFORMAT_ALPHA(surface->format)) {
  584. return;
  585. }
  586. bpp = SDL_BYTESPERPIXEL(surface->format);
  587. SDL_LockSurface(surface);
  588. if (bpp == 2) {
  589. Uint16 *row, *spot;
  590. Uint16 ckey = (Uint16)surface->map.info.colorkey;
  591. Uint16 mask = (Uint16)(~surface->fmt->Amask);
  592. // Ignore, or not, alpha in colorkey comparison
  593. if (ignore_alpha) {
  594. ckey &= mask;
  595. row = (Uint16 *)surface->pixels;
  596. for (y = surface->h; y--;) {
  597. spot = row;
  598. for (x = surface->w; x--;) {
  599. if ((*spot & mask) == ckey) {
  600. *spot &= mask;
  601. }
  602. ++spot;
  603. }
  604. row += surface->pitch / 2;
  605. }
  606. } else {
  607. row = (Uint16 *)surface->pixels;
  608. for (y = surface->h; y--;) {
  609. spot = row;
  610. for (x = surface->w; x--;) {
  611. if (*spot == ckey) {
  612. *spot &= mask;
  613. }
  614. ++spot;
  615. }
  616. row += surface->pitch / 2;
  617. }
  618. }
  619. } else if (bpp == 4) {
  620. Uint32 *row, *spot;
  621. Uint32 ckey = surface->map.info.colorkey;
  622. Uint32 mask = ~surface->fmt->Amask;
  623. // Ignore, or not, alpha in colorkey comparison
  624. if (ignore_alpha) {
  625. ckey &= mask;
  626. row = (Uint32 *)surface->pixels;
  627. for (y = surface->h; y--;) {
  628. spot = row;
  629. for (x = surface->w; x--;) {
  630. if ((*spot & mask) == ckey) {
  631. *spot &= mask;
  632. }
  633. ++spot;
  634. }
  635. row += surface->pitch / 4;
  636. }
  637. } else {
  638. row = (Uint32 *)surface->pixels;
  639. for (y = surface->h; y--;) {
  640. spot = row;
  641. for (x = surface->w; x--;) {
  642. if (*spot == ckey) {
  643. *spot &= mask;
  644. }
  645. ++spot;
  646. }
  647. row += surface->pitch / 4;
  648. }
  649. }
  650. }
  651. SDL_UnlockSurface(surface);
  652. SDL_SetSurfaceColorKey(surface, false, 0);
  653. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND);
  654. }
  655. bool SDL_SetSurfaceColorMod(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
  656. {
  657. int flags;
  658. if (!SDL_SurfaceValid(surface)) {
  659. return SDL_InvalidParamError("surface");
  660. }
  661. surface->map.info.r = r;
  662. surface->map.info.g = g;
  663. surface->map.info.b = b;
  664. flags = surface->map.info.flags;
  665. if (r != 0xFF || g != 0xFF || b != 0xFF) {
  666. surface->map.info.flags |= SDL_COPY_MODULATE_COLOR;
  667. } else {
  668. surface->map.info.flags &= ~SDL_COPY_MODULATE_COLOR;
  669. }
  670. if (surface->map.info.flags != flags) {
  671. SDL_InvalidateMap(&surface->map);
  672. }
  673. return true;
  674. }
  675. bool SDL_GetSurfaceColorMod(SDL_Surface *surface, Uint8 *r, Uint8 *g, Uint8 *b)
  676. {
  677. if (!SDL_SurfaceValid(surface)) {
  678. if (r) {
  679. *r = 255;
  680. }
  681. if (g) {
  682. *g = 255;
  683. }
  684. if (b) {
  685. *b = 255;
  686. }
  687. return SDL_InvalidParamError("surface");
  688. }
  689. if (r) {
  690. *r = surface->map.info.r;
  691. }
  692. if (g) {
  693. *g = surface->map.info.g;
  694. }
  695. if (b) {
  696. *b = surface->map.info.b;
  697. }
  698. return true;
  699. }
  700. bool SDL_SetSurfaceAlphaMod(SDL_Surface *surface, Uint8 alpha)
  701. {
  702. int flags;
  703. if (!SDL_SurfaceValid(surface)) {
  704. return SDL_InvalidParamError("surface");
  705. }
  706. surface->map.info.a = alpha;
  707. flags = surface->map.info.flags;
  708. if (alpha != 0xFF) {
  709. surface->map.info.flags |= SDL_COPY_MODULATE_ALPHA;
  710. } else {
  711. surface->map.info.flags &= ~SDL_COPY_MODULATE_ALPHA;
  712. }
  713. if (surface->map.info.flags != flags) {
  714. SDL_InvalidateMap(&surface->map);
  715. }
  716. return true;
  717. }
  718. bool SDL_GetSurfaceAlphaMod(SDL_Surface *surface, Uint8 *alpha)
  719. {
  720. if (!SDL_SurfaceValid(surface)) {
  721. if (alpha) {
  722. *alpha = 255;
  723. }
  724. return SDL_InvalidParamError("surface");
  725. }
  726. if (alpha) {
  727. *alpha = surface->map.info.a;
  728. }
  729. return true;
  730. }
  731. bool SDL_SetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode blendMode)
  732. {
  733. int flags;
  734. bool result = true;
  735. if (!SDL_SurfaceValid(surface)) {
  736. return SDL_InvalidParamError("surface");
  737. }
  738. if (blendMode == SDL_BLENDMODE_INVALID) {
  739. return SDL_InvalidParamError("blendMode");
  740. }
  741. flags = surface->map.info.flags;
  742. surface->map.info.flags &= ~SDL_COPY_BLEND_MASK;
  743. switch (blendMode) {
  744. case SDL_BLENDMODE_NONE:
  745. break;
  746. case SDL_BLENDMODE_BLEND:
  747. surface->map.info.flags |= SDL_COPY_BLEND;
  748. break;
  749. case SDL_BLENDMODE_BLEND_PREMULTIPLIED:
  750. surface->map.info.flags |= SDL_COPY_BLEND_PREMULTIPLIED;
  751. break;
  752. case SDL_BLENDMODE_ADD:
  753. surface->map.info.flags |= SDL_COPY_ADD;
  754. break;
  755. case SDL_BLENDMODE_ADD_PREMULTIPLIED:
  756. surface->map.info.flags |= SDL_COPY_ADD_PREMULTIPLIED;
  757. break;
  758. case SDL_BLENDMODE_MOD:
  759. surface->map.info.flags |= SDL_COPY_MOD;
  760. break;
  761. case SDL_BLENDMODE_MUL:
  762. surface->map.info.flags |= SDL_COPY_MUL;
  763. break;
  764. default:
  765. result = SDL_Unsupported();
  766. break;
  767. }
  768. if (surface->map.info.flags != flags) {
  769. SDL_InvalidateMap(&surface->map);
  770. }
  771. return result;
  772. }
  773. bool SDL_GetSurfaceBlendMode(SDL_Surface *surface, SDL_BlendMode *blendMode)
  774. {
  775. if (blendMode) {
  776. *blendMode = SDL_BLENDMODE_INVALID;
  777. }
  778. if (!SDL_SurfaceValid(surface)) {
  779. return SDL_InvalidParamError("surface");
  780. }
  781. if (!blendMode) {
  782. return true;
  783. }
  784. switch (surface->map.info.flags & SDL_COPY_BLEND_MASK) {
  785. case SDL_COPY_BLEND:
  786. *blendMode = SDL_BLENDMODE_BLEND;
  787. break;
  788. case SDL_COPY_BLEND_PREMULTIPLIED:
  789. *blendMode = SDL_BLENDMODE_BLEND_PREMULTIPLIED;
  790. break;
  791. case SDL_COPY_ADD:
  792. *blendMode = SDL_BLENDMODE_ADD;
  793. break;
  794. case SDL_COPY_ADD_PREMULTIPLIED:
  795. *blendMode = SDL_BLENDMODE_ADD_PREMULTIPLIED;
  796. break;
  797. case SDL_COPY_MOD:
  798. *blendMode = SDL_BLENDMODE_MOD;
  799. break;
  800. case SDL_COPY_MUL:
  801. *blendMode = SDL_BLENDMODE_MUL;
  802. break;
  803. default:
  804. *blendMode = SDL_BLENDMODE_NONE;
  805. break;
  806. }
  807. return true;
  808. }
  809. bool SDL_SetSurfaceClipRect(SDL_Surface *surface, const SDL_Rect *rect)
  810. {
  811. SDL_Rect full_rect;
  812. // Don't do anything if there's no surface to act on
  813. if (!SDL_SurfaceValid(surface)) {
  814. return false;
  815. }
  816. // Set up the full surface rectangle
  817. full_rect.x = 0;
  818. full_rect.y = 0;
  819. full_rect.w = surface->w;
  820. full_rect.h = surface->h;
  821. // Set the clipping rectangle
  822. if (!rect) {
  823. surface->clip_rect = full_rect;
  824. return true;
  825. }
  826. return SDL_GetRectIntersection(rect, &full_rect, &surface->clip_rect);
  827. }
  828. bool SDL_GetSurfaceClipRect(SDL_Surface *surface, SDL_Rect *rect)
  829. {
  830. if (!SDL_SurfaceValid(surface)) {
  831. if (rect) {
  832. SDL_zerop(rect);
  833. }
  834. return SDL_InvalidParamError("surface");
  835. }
  836. if (!rect) {
  837. return SDL_InvalidParamError("rect");
  838. }
  839. *rect = surface->clip_rect;
  840. return true;
  841. }
  842. /*
  843. * Set up a blit between two surfaces -- split into three parts:
  844. * The upper part, SDL_BlitSurface(), performs clipping and rectangle
  845. * verification. The lower part is a pointer to a low level
  846. * accelerated blitting function.
  847. *
  848. * These parts are separated out and each used internally by this
  849. * library in the optimum places. They are exported so that if
  850. * you know exactly what you are doing, you can optimize your code
  851. * by calling the one(s) you need.
  852. */
  853. bool SDL_BlitSurfaceUnchecked(SDL_Surface *src, const SDL_Rect *srcrect,
  854. SDL_Surface *dst, const SDL_Rect *dstrect)
  855. {
  856. // Check to make sure the blit mapping is valid
  857. if (!SDL_ValidateMap(src, dst)) {
  858. return false;
  859. }
  860. return src->map.blit(src, srcrect, dst, dstrect);
  861. }
  862. bool SDL_BlitSurface(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect)
  863. {
  864. SDL_Rect r_src, r_dst;
  865. // Make sure the surfaces aren't locked
  866. if (!SDL_SurfaceValid(src)) {
  867. return SDL_InvalidParamError("src");
  868. } else if (!SDL_SurfaceValid(dst)) {
  869. return SDL_InvalidParamError("dst");
  870. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  871. return SDL_SetError("Surfaces must not be locked during blit");
  872. }
  873. // Full src surface
  874. r_src.x = 0;
  875. r_src.y = 0;
  876. r_src.w = src->w;
  877. r_src.h = src->h;
  878. if (dstrect) {
  879. r_dst.x = dstrect->x;
  880. r_dst.y = dstrect->y;
  881. } else {
  882. r_dst.x = 0;
  883. r_dst.y = 0;
  884. }
  885. // clip the source rectangle to the source surface
  886. if (srcrect) {
  887. SDL_Rect tmp;
  888. if (SDL_GetRectIntersection(srcrect, &r_src, &tmp) == false) {
  889. return true;
  890. }
  891. // Shift dstrect, if srcrect origin has changed
  892. r_dst.x += tmp.x - srcrect->x;
  893. r_dst.y += tmp.y - srcrect->y;
  894. // Update srcrect
  895. r_src = tmp;
  896. }
  897. // There're no dstrect.w/h parameters. It's the same as srcrect
  898. r_dst.w = r_src.w;
  899. r_dst.h = r_src.h;
  900. // clip the destination rectangle against the clip rectangle
  901. {
  902. SDL_Rect tmp;
  903. if (SDL_GetRectIntersection(&r_dst, &dst->clip_rect, &tmp) == false) {
  904. return true;
  905. }
  906. // Shift srcrect, if dstrect has changed
  907. r_src.x += tmp.x - r_dst.x;
  908. r_src.y += tmp.y - r_dst.y;
  909. r_src.w = tmp.w;
  910. r_src.h = tmp.h;
  911. // Update dstrect
  912. r_dst = tmp;
  913. }
  914. if (r_dst.w <= 0 || r_dst.h <= 0) {
  915. // No-op.
  916. return true;
  917. }
  918. // Switch back to a fast blit if we were previously stretching
  919. if (src->map.info.flags & SDL_COPY_NEAREST) {
  920. src->map.info.flags &= ~SDL_COPY_NEAREST;
  921. SDL_InvalidateMap(&src->map);
  922. }
  923. return SDL_BlitSurfaceUnchecked(src, &r_src, dst, &r_dst);
  924. }
  925. bool SDL_BlitSurfaceScaled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect, SDL_ScaleMode scaleMode)
  926. {
  927. SDL_Rect *clip_rect;
  928. double src_x0, src_y0, src_x1, src_y1;
  929. double dst_x0, dst_y0, dst_x1, dst_y1;
  930. SDL_Rect final_src, final_dst;
  931. double scaling_w, scaling_h;
  932. int src_w, src_h;
  933. int dst_w, dst_h;
  934. // Make sure the surfaces aren't locked
  935. if (!SDL_SurfaceValid(src)) {
  936. return SDL_InvalidParamError("src");
  937. } else if (!SDL_SurfaceValid(dst)) {
  938. return SDL_InvalidParamError("dst");
  939. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  940. return SDL_SetError("Surfaces must not be locked during blit");
  941. } else if (scaleMode != SDL_SCALEMODE_NEAREST &&
  942. scaleMode != SDL_SCALEMODE_LINEAR) {
  943. return SDL_InvalidParamError("scaleMode");
  944. }
  945. if (!srcrect) {
  946. src_w = src->w;
  947. src_h = src->h;
  948. } else {
  949. src_w = srcrect->w;
  950. src_h = srcrect->h;
  951. }
  952. if (!dstrect) {
  953. dst_w = dst->w;
  954. dst_h = dst->h;
  955. } else {
  956. dst_w = dstrect->w;
  957. dst_h = dstrect->h;
  958. }
  959. if (dst_w == src_w && dst_h == src_h) {
  960. // No scaling, defer to regular blit
  961. return SDL_BlitSurface(src, srcrect, dst, dstrect);
  962. }
  963. scaling_w = (double)dst_w / src_w;
  964. scaling_h = (double)dst_h / src_h;
  965. if (!dstrect) {
  966. dst_x0 = 0;
  967. dst_y0 = 0;
  968. dst_x1 = dst_w;
  969. dst_y1 = dst_h;
  970. } else {
  971. dst_x0 = dstrect->x;
  972. dst_y0 = dstrect->y;
  973. dst_x1 = dst_x0 + dst_w;
  974. dst_y1 = dst_y0 + dst_h;
  975. }
  976. if (!srcrect) {
  977. src_x0 = 0;
  978. src_y0 = 0;
  979. src_x1 = src_w;
  980. src_y1 = src_h;
  981. } else {
  982. src_x0 = srcrect->x;
  983. src_y0 = srcrect->y;
  984. src_x1 = src_x0 + src_w;
  985. src_y1 = src_y0 + src_h;
  986. // Clip source rectangle to the source surface
  987. if (src_x0 < 0) {
  988. dst_x0 -= src_x0 * scaling_w;
  989. src_x0 = 0;
  990. }
  991. if (src_x1 > src->w) {
  992. dst_x1 -= (src_x1 - src->w) * scaling_w;
  993. src_x1 = src->w;
  994. }
  995. if (src_y0 < 0) {
  996. dst_y0 -= src_y0 * scaling_h;
  997. src_y0 = 0;
  998. }
  999. if (src_y1 > src->h) {
  1000. dst_y1 -= (src_y1 - src->h) * scaling_h;
  1001. src_y1 = src->h;
  1002. }
  1003. }
  1004. // Clip destination rectangle to the clip rectangle
  1005. clip_rect = &dst->clip_rect;
  1006. // Translate to clip space for easier calculations
  1007. dst_x0 -= clip_rect->x;
  1008. dst_x1 -= clip_rect->x;
  1009. dst_y0 -= clip_rect->y;
  1010. dst_y1 -= clip_rect->y;
  1011. if (dst_x0 < 0) {
  1012. src_x0 -= dst_x0 / scaling_w;
  1013. dst_x0 = 0;
  1014. }
  1015. if (dst_x1 > clip_rect->w) {
  1016. src_x1 -= (dst_x1 - clip_rect->w) / scaling_w;
  1017. dst_x1 = clip_rect->w;
  1018. }
  1019. if (dst_y0 < 0) {
  1020. src_y0 -= dst_y0 / scaling_h;
  1021. dst_y0 = 0;
  1022. }
  1023. if (dst_y1 > clip_rect->h) {
  1024. src_y1 -= (dst_y1 - clip_rect->h) / scaling_h;
  1025. dst_y1 = clip_rect->h;
  1026. }
  1027. // Translate back to surface coordinates
  1028. dst_x0 += clip_rect->x;
  1029. dst_x1 += clip_rect->x;
  1030. dst_y0 += clip_rect->y;
  1031. dst_y1 += clip_rect->y;
  1032. final_src.x = (int)SDL_round(src_x0);
  1033. final_src.y = (int)SDL_round(src_y0);
  1034. final_src.w = (int)SDL_round(src_x1 - src_x0);
  1035. final_src.h = (int)SDL_round(src_y1 - src_y0);
  1036. final_dst.x = (int)SDL_round(dst_x0);
  1037. final_dst.y = (int)SDL_round(dst_y0);
  1038. final_dst.w = (int)SDL_round(dst_x1 - dst_x0);
  1039. final_dst.h = (int)SDL_round(dst_y1 - dst_y0);
  1040. // Clip again
  1041. {
  1042. SDL_Rect tmp;
  1043. tmp.x = 0;
  1044. tmp.y = 0;
  1045. tmp.w = src->w;
  1046. tmp.h = src->h;
  1047. SDL_GetRectIntersection(&tmp, &final_src, &final_src);
  1048. }
  1049. // Clip again
  1050. SDL_GetRectIntersection(clip_rect, &final_dst, &final_dst);
  1051. if (final_dst.w == 0 || final_dst.h == 0 ||
  1052. final_src.w < 0 || final_src.h < 0) {
  1053. // No-op.
  1054. return true;
  1055. }
  1056. return SDL_BlitSurfaceUncheckedScaled(src, &final_src, dst, &final_dst, scaleMode);
  1057. }
  1058. /**
  1059. * This is a semi-private blit function and it performs low-level surface
  1060. * scaled blitting only.
  1061. */
  1062. bool SDL_BlitSurfaceUncheckedScaled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect, SDL_ScaleMode scaleMode)
  1063. {
  1064. static const Uint32 complex_copy_flags = (SDL_COPY_MODULATE_MASK | SDL_COPY_BLEND_MASK | SDL_COPY_COLORKEY);
  1065. if (srcrect->w > SDL_MAX_UINT16 || srcrect->h > SDL_MAX_UINT16 ||
  1066. dstrect->w > SDL_MAX_UINT16 || dstrect->h > SDL_MAX_UINT16) {
  1067. return SDL_SetError("Size too large for scaling");
  1068. }
  1069. if (!(src->map.info.flags & SDL_COPY_NEAREST)) {
  1070. src->map.info.flags |= SDL_COPY_NEAREST;
  1071. SDL_InvalidateMap(&src->map);
  1072. }
  1073. if (scaleMode == SDL_SCALEMODE_NEAREST) {
  1074. if (!(src->map.info.flags & complex_copy_flags) &&
  1075. src->format == dst->format &&
  1076. !SDL_ISPIXELFORMAT_INDEXED(src->format) &&
  1077. SDL_BYTESPERPIXEL(src->format) <= 4) {
  1078. return SDL_StretchSurface(src, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST);
  1079. } else if (SDL_BITSPERPIXEL(src->format) < 8) {
  1080. // Scaling bitmap not yet supported, convert to RGBA for blit
  1081. bool result = false;
  1082. SDL_Surface *tmp = SDL_ConvertSurface(src, SDL_PIXELFORMAT_ARGB8888);
  1083. if (tmp) {
  1084. result = SDL_BlitSurfaceUncheckedScaled(tmp, srcrect, dst, dstrect, SDL_SCALEMODE_NEAREST);
  1085. SDL_DestroySurface(tmp);
  1086. }
  1087. return result;
  1088. } else {
  1089. return SDL_BlitSurfaceUnchecked(src, srcrect, dst, dstrect);
  1090. }
  1091. } else {
  1092. if (!(src->map.info.flags & complex_copy_flags) &&
  1093. src->format == dst->format &&
  1094. !SDL_ISPIXELFORMAT_INDEXED(src->format) &&
  1095. SDL_BYTESPERPIXEL(src->format) == 4 &&
  1096. src->format != SDL_PIXELFORMAT_ARGB2101010) {
  1097. // fast path
  1098. return SDL_StretchSurface(src, srcrect, dst, dstrect, SDL_SCALEMODE_LINEAR);
  1099. } else if (SDL_BITSPERPIXEL(src->format) < 8) {
  1100. // Scaling bitmap not yet supported, convert to RGBA for blit
  1101. bool result = false;
  1102. SDL_Surface *tmp = SDL_ConvertSurface(src, SDL_PIXELFORMAT_ARGB8888);
  1103. if (tmp) {
  1104. result = SDL_BlitSurfaceUncheckedScaled(tmp, srcrect, dst, dstrect, scaleMode);
  1105. SDL_DestroySurface(tmp);
  1106. }
  1107. return result;
  1108. } else {
  1109. // Use intermediate surface(s)
  1110. SDL_Surface *tmp1 = NULL;
  1111. bool result;
  1112. SDL_Rect srcrect2;
  1113. int is_complex_copy_flags = (src->map.info.flags & complex_copy_flags);
  1114. Uint8 r, g, b;
  1115. Uint8 alpha;
  1116. SDL_BlendMode blendMode;
  1117. // Save source infos
  1118. SDL_GetSurfaceColorMod(src, &r, &g, &b);
  1119. SDL_GetSurfaceAlphaMod(src, &alpha);
  1120. SDL_GetSurfaceBlendMode(src, &blendMode);
  1121. srcrect2.x = srcrect->x;
  1122. srcrect2.y = srcrect->y;
  1123. srcrect2.w = srcrect->w;
  1124. srcrect2.h = srcrect->h;
  1125. // Change source format if not appropriate for scaling
  1126. if (SDL_BYTESPERPIXEL(src->format) != 4 || src->format == SDL_PIXELFORMAT_ARGB2101010) {
  1127. SDL_Rect tmprect;
  1128. SDL_PixelFormat fmt;
  1129. tmprect.x = 0;
  1130. tmprect.y = 0;
  1131. tmprect.w = src->w;
  1132. tmprect.h = src->h;
  1133. if (SDL_BYTESPERPIXEL(dst->format) == 4 && dst->format != SDL_PIXELFORMAT_ARGB2101010) {
  1134. fmt = dst->format;
  1135. } else {
  1136. fmt = SDL_PIXELFORMAT_ARGB8888;
  1137. }
  1138. tmp1 = SDL_CreateSurface(src->w, src->h, fmt);
  1139. SDL_BlitSurfaceUnchecked(src, srcrect, tmp1, &tmprect);
  1140. srcrect2.x = 0;
  1141. srcrect2.y = 0;
  1142. SDL_SetSurfaceColorMod(tmp1, r, g, b);
  1143. SDL_SetSurfaceAlphaMod(tmp1, alpha);
  1144. SDL_SetSurfaceBlendMode(tmp1, blendMode);
  1145. src = tmp1;
  1146. }
  1147. // Intermediate scaling
  1148. if (is_complex_copy_flags || src->format != dst->format) {
  1149. SDL_Rect tmprect;
  1150. SDL_Surface *tmp2 = SDL_CreateSurface(dstrect->w, dstrect->h, src->format);
  1151. SDL_StretchSurface(src, &srcrect2, tmp2, NULL, SDL_SCALEMODE_LINEAR);
  1152. SDL_SetSurfaceColorMod(tmp2, r, g, b);
  1153. SDL_SetSurfaceAlphaMod(tmp2, alpha);
  1154. SDL_SetSurfaceBlendMode(tmp2, blendMode);
  1155. tmprect.x = 0;
  1156. tmprect.y = 0;
  1157. tmprect.w = dstrect->w;
  1158. tmprect.h = dstrect->h;
  1159. result = SDL_BlitSurfaceUnchecked(tmp2, &tmprect, dst, dstrect);
  1160. SDL_DestroySurface(tmp2);
  1161. } else {
  1162. result = SDL_StretchSurface(src, &srcrect2, dst, dstrect, SDL_SCALEMODE_LINEAR);
  1163. }
  1164. SDL_DestroySurface(tmp1);
  1165. return result;
  1166. }
  1167. }
  1168. }
  1169. bool SDL_BlitSurfaceTiled(SDL_Surface *src, const SDL_Rect *srcrect, SDL_Surface *dst, const SDL_Rect *dstrect)
  1170. {
  1171. SDL_Rect r_src, r_dst;
  1172. // Make sure the surfaces aren't locked
  1173. if (!SDL_SurfaceValid(src)) {
  1174. return SDL_InvalidParamError("src");
  1175. } else if (!SDL_SurfaceValid(dst)) {
  1176. return SDL_InvalidParamError("dst");
  1177. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  1178. return SDL_SetError("Surfaces must not be locked during blit");
  1179. }
  1180. // Full src surface
  1181. r_src.x = 0;
  1182. r_src.y = 0;
  1183. r_src.w = src->w;
  1184. r_src.h = src->h;
  1185. if (dstrect) {
  1186. r_dst.x = dstrect->x;
  1187. r_dst.y = dstrect->y;
  1188. r_dst.w = dstrect->w;
  1189. r_dst.h = dstrect->h;
  1190. } else {
  1191. r_dst.x = 0;
  1192. r_dst.y = 0;
  1193. r_dst.w = dst->w;
  1194. r_dst.h = dst->h;
  1195. }
  1196. // clip the source rectangle to the source surface
  1197. if (srcrect) {
  1198. if (SDL_GetRectIntersection(srcrect, &r_src, &r_src) == false) {
  1199. return true;
  1200. }
  1201. // For tiling we don't adjust the destination rectangle
  1202. }
  1203. // clip the destination rectangle against the clip rectangle
  1204. {
  1205. if (SDL_GetRectIntersection(&r_dst, &dst->clip_rect, &r_dst) == false) {
  1206. return true;
  1207. }
  1208. // For tiling we don't adjust the source rectangle
  1209. }
  1210. // Switch back to a fast blit if we were previously stretching
  1211. if (src->map.info.flags & SDL_COPY_NEAREST) {
  1212. src->map.info.flags &= ~SDL_COPY_NEAREST;
  1213. SDL_InvalidateMap(&src->map);
  1214. }
  1215. int rows = r_dst.h / r_src.h;
  1216. int cols = r_dst.w / r_src.w;
  1217. int remaining_w = r_dst.w % r_src.w;
  1218. int remaining_h = r_dst.h % r_src.h;
  1219. SDL_Rect curr_src, curr_dst;
  1220. SDL_copyp(&curr_src, &r_src);
  1221. curr_dst.y = r_dst.y;
  1222. curr_dst.w = r_src.w;
  1223. curr_dst.h = r_src.h;
  1224. for (int y = 0; y < rows; ++y) {
  1225. curr_dst.x = r_dst.x;
  1226. for (int x = 0; x < cols; ++x) {
  1227. if (!SDL_BlitSurfaceUnchecked(src, &curr_src, dst, &curr_dst)) {
  1228. return false;
  1229. }
  1230. curr_dst.x += curr_dst.w;
  1231. }
  1232. if (remaining_w) {
  1233. curr_src.w = remaining_w;
  1234. curr_dst.w = remaining_w;
  1235. if (!SDL_BlitSurfaceUnchecked(src, &curr_src, dst, &curr_dst)) {
  1236. return false;
  1237. }
  1238. curr_src.w = r_src.w;
  1239. curr_dst.w = r_src.w;
  1240. }
  1241. curr_dst.y += curr_dst.h;
  1242. }
  1243. if (remaining_h) {
  1244. curr_src.h = remaining_h;
  1245. curr_dst.h = remaining_h;
  1246. curr_dst.x = r_dst.x;
  1247. for (int x = 0; x < cols; ++x) {
  1248. if (!SDL_BlitSurfaceUnchecked(src, &curr_src, dst, &curr_dst)) {
  1249. return false;
  1250. }
  1251. curr_dst.x += curr_dst.w;
  1252. }
  1253. if (remaining_w) {
  1254. curr_src.w = remaining_w;
  1255. curr_dst.w = remaining_w;
  1256. if (!SDL_BlitSurfaceUnchecked(src, &curr_src, dst, &curr_dst)) {
  1257. return false;
  1258. }
  1259. }
  1260. }
  1261. return true;
  1262. }
  1263. bool SDL_BlitSurfaceTiledWithScale(SDL_Surface *src, const SDL_Rect *srcrect, float scale, SDL_ScaleMode scaleMode, SDL_Surface *dst, const SDL_Rect *dstrect)
  1264. {
  1265. SDL_Rect r_src, r_dst;
  1266. // Make sure the surfaces aren't locked
  1267. if (!SDL_SurfaceValid(src)) {
  1268. return SDL_InvalidParamError("src");
  1269. } else if (!SDL_SurfaceValid(dst)) {
  1270. return SDL_InvalidParamError("dst");
  1271. } else if ((src->flags & SDL_SURFACE_LOCKED) || (dst->flags & SDL_SURFACE_LOCKED)) {
  1272. return SDL_SetError("Surfaces must not be locked during blit");
  1273. }
  1274. if (scale <= 0.0f) {
  1275. return SDL_InvalidParamError("scale");
  1276. }
  1277. // Full src surface
  1278. r_src.x = 0;
  1279. r_src.y = 0;
  1280. r_src.w = src->w;
  1281. r_src.h = src->h;
  1282. if (dstrect) {
  1283. r_dst.x = dstrect->x;
  1284. r_dst.y = dstrect->y;
  1285. r_dst.w = dstrect->w;
  1286. r_dst.h = dstrect->h;
  1287. } else {
  1288. r_dst.x = 0;
  1289. r_dst.y = 0;
  1290. r_dst.w = dst->w;
  1291. r_dst.h = dst->h;
  1292. }
  1293. // clip the source rectangle to the source surface
  1294. if (srcrect) {
  1295. if (SDL_GetRectIntersection(srcrect, &r_src, &r_src) == false) {
  1296. return true;
  1297. }
  1298. // For tiling we don't adjust the destination rectangle
  1299. }
  1300. // clip the destination rectangle against the clip rectangle
  1301. {
  1302. if (SDL_GetRectIntersection(&r_dst, &dst->clip_rect, &r_dst) == false) {
  1303. return true;
  1304. }
  1305. // For tiling we don't adjust the source rectangle
  1306. }
  1307. // Switch back to a fast blit if we were previously stretching
  1308. if (src->map.info.flags & SDL_COPY_NEAREST) {
  1309. src->map.info.flags &= ~SDL_COPY_NEAREST;
  1310. SDL_InvalidateMap(&src->map);
  1311. }
  1312. int tile_width = (int)(r_src.w * scale);
  1313. int tile_height = (int)(r_src.h * scale);
  1314. int rows = r_dst.h / tile_height;
  1315. int cols = r_dst.w / tile_width;
  1316. int remaining_dst_w = (r_dst.w - cols * tile_width);
  1317. int remaining_dst_h = (r_dst.h - rows * tile_height);
  1318. int remaining_src_w = (int)(remaining_dst_w / scale);
  1319. int remaining_src_h = (int)(remaining_dst_h / scale);
  1320. SDL_Rect curr_src, curr_dst;
  1321. SDL_copyp(&curr_src, &r_src);
  1322. curr_dst.y = r_dst.y;
  1323. curr_dst.w = tile_width;
  1324. curr_dst.h = tile_height;
  1325. for (int y = 0; y < rows; ++y) {
  1326. curr_dst.x = r_dst.x;
  1327. for (int x = 0; x < cols; ++x) {
  1328. if (!SDL_BlitSurfaceUncheckedScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1329. return false;
  1330. }
  1331. curr_dst.x += curr_dst.w;
  1332. }
  1333. if (remaining_dst_w > 0) {
  1334. curr_src.w = remaining_src_w;
  1335. curr_dst.w = remaining_dst_w;
  1336. if (!SDL_BlitSurfaceUncheckedScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1337. return false;
  1338. }
  1339. curr_src.w = r_src.w;
  1340. curr_dst.w = tile_width;
  1341. }
  1342. curr_dst.y += curr_dst.h;
  1343. }
  1344. if (remaining_dst_h > 0) {
  1345. curr_src.h = remaining_src_h;
  1346. curr_dst.h = remaining_dst_h;
  1347. curr_dst.x = r_dst.x;
  1348. for (int x = 0; x < cols; ++x) {
  1349. if (!SDL_BlitSurfaceUncheckedScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1350. return false;
  1351. }
  1352. curr_dst.x += curr_dst.w;
  1353. }
  1354. if (remaining_dst_w > 0) {
  1355. curr_src.w = remaining_src_w;
  1356. curr_dst.w = remaining_dst_w;
  1357. if (!SDL_BlitSurfaceUncheckedScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1358. return false;
  1359. }
  1360. }
  1361. }
  1362. return true;
  1363. }
  1364. bool SDL_BlitSurface9Grid(SDL_Surface *src, const SDL_Rect *srcrect, int left_width, int right_width, int top_height, int bottom_height, float scale, SDL_ScaleMode scaleMode, SDL_Surface *dst, const SDL_Rect *dstrect)
  1365. {
  1366. SDL_Rect full_src, full_dst;
  1367. SDL_Rect curr_src, curr_dst;
  1368. int dst_left_width;
  1369. int dst_right_width;
  1370. int dst_top_height;
  1371. int dst_bottom_height;
  1372. // Make sure the surfaces aren't locked
  1373. if (!SDL_SurfaceValid(src)) {
  1374. return SDL_InvalidParamError("src");
  1375. } else if (!SDL_SurfaceValid(dst)) {
  1376. return SDL_InvalidParamError("dst");
  1377. }
  1378. if (!srcrect) {
  1379. full_src.x = 0;
  1380. full_src.y = 0;
  1381. full_src.w = src->w;
  1382. full_src.h = src->h;
  1383. srcrect = &full_src;
  1384. }
  1385. if (!dstrect) {
  1386. full_dst.x = 0;
  1387. full_dst.y = 0;
  1388. full_dst.w = dst->w;
  1389. full_dst.h = dst->h;
  1390. dstrect = &full_dst;
  1391. }
  1392. if (scale <= 0.0f || scale == 1.0f) {
  1393. dst_left_width = left_width;
  1394. dst_right_width = right_width;
  1395. dst_top_height = top_height;
  1396. dst_bottom_height = bottom_height;
  1397. } else {
  1398. dst_left_width = (int)SDL_roundf(left_width * scale);
  1399. dst_right_width = (int)SDL_roundf(right_width * scale);
  1400. dst_top_height = (int)SDL_roundf(top_height * scale);
  1401. dst_bottom_height = (int)SDL_roundf(bottom_height * scale);
  1402. }
  1403. // Upper-left corner
  1404. curr_src.x = srcrect->x;
  1405. curr_src.y = srcrect->y;
  1406. curr_src.w = left_width;
  1407. curr_src.h = top_height;
  1408. curr_dst.x = dstrect->x;
  1409. curr_dst.y = dstrect->y;
  1410. curr_dst.w = dst_left_width;
  1411. curr_dst.h = dst_top_height;
  1412. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1413. return false;
  1414. }
  1415. // Upper-right corner
  1416. curr_src.x = srcrect->x + srcrect->w - right_width;
  1417. curr_src.w = right_width;
  1418. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  1419. curr_dst.w = dst_right_width;
  1420. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1421. return false;
  1422. }
  1423. // Lower-right corner
  1424. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  1425. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  1426. curr_dst.h = dst_bottom_height;
  1427. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1428. return false;
  1429. }
  1430. // Lower-left corner
  1431. curr_src.x = srcrect->x;
  1432. curr_src.w = left_width;
  1433. curr_dst.x = dstrect->x;
  1434. curr_dst.w = dst_left_width;
  1435. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1436. return false;
  1437. }
  1438. // Left
  1439. curr_src.y = srcrect->y + top_height;
  1440. curr_src.h = srcrect->h - top_height - bottom_height;
  1441. curr_dst.y = dstrect->y + dst_top_height;
  1442. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  1443. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1444. return false;
  1445. }
  1446. // Right
  1447. curr_src.x = srcrect->x + srcrect->w - right_width;
  1448. curr_src.w = right_width;
  1449. curr_dst.x = dstrect->x + dstrect->w - dst_right_width;
  1450. curr_dst.w = dst_right_width;
  1451. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1452. return false;
  1453. }
  1454. // Top
  1455. curr_src.x = srcrect->x + left_width;
  1456. curr_src.y = srcrect->y;
  1457. curr_src.w = srcrect->w - left_width - right_width;
  1458. curr_src.h = top_height;
  1459. curr_dst.x = dstrect->x + dst_left_width;
  1460. curr_dst.y = dstrect->y;
  1461. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  1462. curr_dst.h = dst_top_height;
  1463. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1464. return false;
  1465. }
  1466. // Bottom
  1467. curr_src.y = srcrect->y + srcrect->h - bottom_height;
  1468. curr_dst.y = dstrect->y + dstrect->h - dst_bottom_height;
  1469. curr_dst.h = dst_bottom_height;
  1470. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1471. return false;
  1472. }
  1473. // Center
  1474. curr_src.x = srcrect->x + left_width;
  1475. curr_src.y = srcrect->y + top_height;
  1476. curr_src.w = srcrect->w - left_width - right_width;
  1477. curr_src.h = srcrect->h - top_height - bottom_height;
  1478. curr_dst.x = dstrect->x + dst_left_width;
  1479. curr_dst.y = dstrect->y + dst_top_height;
  1480. curr_dst.w = dstrect->w - dst_left_width - dst_right_width;
  1481. curr_dst.h = dstrect->h - dst_top_height - dst_bottom_height;
  1482. if (!SDL_BlitSurfaceScaled(src, &curr_src, dst, &curr_dst, scaleMode)) {
  1483. return false;
  1484. }
  1485. return true;
  1486. }
  1487. /*
  1488. * Lock a surface to directly access the pixels
  1489. */
  1490. bool SDL_LockSurface(SDL_Surface *surface)
  1491. {
  1492. if (!SDL_SurfaceValid(surface)) {
  1493. return SDL_InvalidParamError("surface");
  1494. }
  1495. if (!surface->locked) {
  1496. #ifdef SDL_HAVE_RLE
  1497. // Perform the lock
  1498. if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1499. SDL_UnRLESurface(surface, true);
  1500. surface->internal_flags |= SDL_INTERNAL_SURFACE_RLEACCEL; // save accel'd state
  1501. }
  1502. #endif
  1503. }
  1504. // Increment the surface lock count, for recursive locks
  1505. ++surface->locked;
  1506. surface->flags |= SDL_SURFACE_LOCKED;
  1507. // Ready to go..
  1508. return true;
  1509. }
  1510. /*
  1511. * Unlock a previously locked surface
  1512. */
  1513. void SDL_UnlockSurface(SDL_Surface *surface)
  1514. {
  1515. if (!SDL_SurfaceValid(surface)) {
  1516. return;
  1517. }
  1518. // Only perform an unlock if we are locked
  1519. if (!surface->locked || (--surface->locked > 0)) {
  1520. return;
  1521. }
  1522. #ifdef SDL_HAVE_RLE
  1523. // Update RLE encoded surface with new data
  1524. if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  1525. surface->internal_flags &= ~SDL_INTERNAL_SURFACE_RLEACCEL; // stop lying
  1526. SDL_RLESurface(surface);
  1527. }
  1528. #endif
  1529. surface->flags &= ~SDL_SURFACE_LOCKED;
  1530. }
  1531. static bool SDL_FlipSurfaceHorizontal(SDL_Surface *surface)
  1532. {
  1533. bool isstack;
  1534. Uint8 *row, *a, *b, *tmp;
  1535. int i, j, bpp;
  1536. if (SDL_BITSPERPIXEL(surface->format) < 8) {
  1537. // We could implement this if needed, but we'd have to flip sets of bits within a byte
  1538. return SDL_Unsupported();
  1539. }
  1540. if (surface->h <= 0) {
  1541. return true;
  1542. }
  1543. if (surface->w <= 1) {
  1544. return true;
  1545. }
  1546. bpp = SDL_BYTESPERPIXEL(surface->format);
  1547. row = (Uint8 *)surface->pixels;
  1548. tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
  1549. if (!tmp) {
  1550. return false;
  1551. }
  1552. for (i = surface->h; i--; ) {
  1553. a = row;
  1554. b = a + (surface->w - 1) * bpp;
  1555. for (j = surface->w / 2; j--; ) {
  1556. SDL_memcpy(tmp, a, bpp);
  1557. SDL_memcpy(a, b, bpp);
  1558. SDL_memcpy(b, tmp, bpp);
  1559. a += bpp;
  1560. b -= bpp;
  1561. }
  1562. row += surface->pitch;
  1563. }
  1564. SDL_small_free(tmp, isstack);
  1565. return true;
  1566. }
  1567. static bool SDL_FlipSurfaceVertical(SDL_Surface *surface)
  1568. {
  1569. bool isstack;
  1570. Uint8 *a, *b, *tmp;
  1571. int i;
  1572. if (surface->h <= 1) {
  1573. return true;
  1574. }
  1575. a = (Uint8 *)surface->pixels;
  1576. b = a + (surface->h - 1) * surface->pitch;
  1577. tmp = SDL_small_alloc(Uint8, surface->pitch, &isstack);
  1578. if (!tmp) {
  1579. return false;
  1580. }
  1581. for (i = surface->h / 2; i--; ) {
  1582. SDL_memcpy(tmp, a, surface->pitch);
  1583. SDL_memcpy(a, b, surface->pitch);
  1584. SDL_memcpy(b, tmp, surface->pitch);
  1585. a += surface->pitch;
  1586. b -= surface->pitch;
  1587. }
  1588. SDL_small_free(tmp, isstack);
  1589. return true;
  1590. }
  1591. bool SDL_FlipSurface(SDL_Surface *surface, SDL_FlipMode flip)
  1592. {
  1593. if (!SDL_SurfaceValid(surface)) {
  1594. return SDL_InvalidParamError("surface");
  1595. }
  1596. if (!surface->pixels) {
  1597. return true;
  1598. }
  1599. switch (flip) {
  1600. case SDL_FLIP_HORIZONTAL:
  1601. return SDL_FlipSurfaceHorizontal(surface);
  1602. case SDL_FLIP_VERTICAL:
  1603. return SDL_FlipSurfaceVertical(surface);
  1604. default:
  1605. return SDL_InvalidParamError("flip");
  1606. }
  1607. }
  1608. SDL_Surface *SDL_ConvertSurfaceAndColorspace(SDL_Surface *surface, SDL_PixelFormat format, SDL_Palette *palette, SDL_Colorspace colorspace, SDL_PropertiesID props)
  1609. {
  1610. SDL_Palette *temp_palette = NULL;
  1611. SDL_Surface *convert = NULL;
  1612. SDL_Colorspace src_colorspace;
  1613. SDL_PropertiesID src_properties;
  1614. Uint32 copy_flags;
  1615. SDL_Color copy_color;
  1616. SDL_Rect bounds;
  1617. bool result;
  1618. bool palette_ck_transform = false;
  1619. Uint8 palette_ck_value = 0;
  1620. Uint8 *palette_saved_alpha = NULL;
  1621. int palette_saved_alpha_ncolors = 0;
  1622. if (!SDL_SurfaceValid(surface)) {
  1623. SDL_InvalidParamError("surface");
  1624. goto error;
  1625. }
  1626. if (format == SDL_PIXELFORMAT_UNKNOWN) {
  1627. SDL_InvalidParamError("format");
  1628. goto error;
  1629. }
  1630. // Check for empty destination palette! (results in empty image)
  1631. if (palette) {
  1632. int i;
  1633. for (i = 0; i < palette->ncolors; ++i) {
  1634. if ((palette->colors[i].r != 0xFF) || (palette->colors[i].g != 0xFF) || (palette->colors[i].b != 0xFF)) {
  1635. break;
  1636. }
  1637. }
  1638. if (i == palette->ncolors) {
  1639. SDL_SetError("Empty destination palette");
  1640. goto error;
  1641. }
  1642. } else if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1643. // Create a dither palette for conversion
  1644. temp_palette = SDL_CreatePalette(1 << SDL_BITSPERPIXEL(format));
  1645. if (temp_palette) {
  1646. SDL_DitherPalette(temp_palette);
  1647. palette = temp_palette;
  1648. }
  1649. }
  1650. src_colorspace = surface->colorspace;
  1651. src_properties = surface->props;
  1652. // Create a new surface with the desired format
  1653. convert = SDL_CreateSurface(surface->w, surface->h, format);
  1654. if (!convert) {
  1655. goto error;
  1656. }
  1657. if (SDL_ISPIXELFORMAT_INDEXED(format)) {
  1658. SDL_SetSurfacePalette(convert, palette);
  1659. }
  1660. if (colorspace == SDL_COLORSPACE_UNKNOWN) {
  1661. colorspace = src_colorspace;
  1662. }
  1663. SDL_SetSurfaceColorspace(convert, colorspace);
  1664. if (SDL_ISPIXELFORMAT_FOURCC(format) || SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  1665. if (surface->format == SDL_PIXELFORMAT_MJPG && format == SDL_PIXELFORMAT_MJPG) {
  1666. // Just do a straight pixel copy of the JPEG image
  1667. size_t size = (size_t)surface->pitch;
  1668. convert->pixels = SDL_malloc(size);
  1669. if (!convert->pixels) {
  1670. goto error;
  1671. }
  1672. convert->flags &= ~SDL_SURFACE_PREALLOCATED;
  1673. convert->pitch = surface->pitch;
  1674. SDL_memcpy(convert->pixels, surface->pixels, size);
  1675. } else 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)) {
  1676. goto error;
  1677. }
  1678. // Save the original copy flags
  1679. copy_flags = surface->map.info.flags;
  1680. goto end;
  1681. }
  1682. // Save the original copy flags
  1683. copy_flags = surface->map.info.flags;
  1684. copy_color.r = surface->map.info.r;
  1685. copy_color.g = surface->map.info.g;
  1686. copy_color.b = surface->map.info.b;
  1687. copy_color.a = surface->map.info.a;
  1688. surface->map.info.r = 0xFF;
  1689. surface->map.info.g = 0xFF;
  1690. surface->map.info.b = 0xFF;
  1691. surface->map.info.a = 0xFF;
  1692. surface->map.info.flags = (copy_flags & (SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY));
  1693. SDL_InvalidateMap(&surface->map);
  1694. // Copy over the image data
  1695. bounds.x = 0;
  1696. bounds.y = 0;
  1697. bounds.w = surface->w;
  1698. bounds.h = surface->h;
  1699. /* Source surface has a palette with no real alpha (0 or OPAQUE).
  1700. * Destination format has alpha.
  1701. * -> set alpha channel to be opaque */
  1702. if (surface->palette && SDL_ISPIXELFORMAT_ALPHA(format)) {
  1703. bool set_opaque = false;
  1704. bool is_opaque, has_alpha_channel;
  1705. SDL_DetectPalette(surface->palette, &is_opaque, &has_alpha_channel);
  1706. if (is_opaque) {
  1707. if (!has_alpha_channel) {
  1708. set_opaque = true;
  1709. }
  1710. }
  1711. // Set opaque and backup palette alpha values
  1712. if (set_opaque) {
  1713. int i;
  1714. palette_saved_alpha_ncolors = surface->palette->ncolors;
  1715. if (palette_saved_alpha_ncolors > 0) {
  1716. palette_saved_alpha = SDL_stack_alloc(Uint8, palette_saved_alpha_ncolors);
  1717. for (i = 0; i < palette_saved_alpha_ncolors; i++) {
  1718. palette_saved_alpha[i] = surface->palette->colors[i].a;
  1719. surface->palette->colors[i].a = SDL_ALPHA_OPAQUE;
  1720. }
  1721. }
  1722. }
  1723. }
  1724. // Transform colorkey to alpha. for cases where source palette has duplicate values, and colorkey is one of them
  1725. if (copy_flags & SDL_COPY_COLORKEY) {
  1726. if (surface->palette && !palette) {
  1727. palette_ck_transform = true;
  1728. palette_ck_value = surface->palette->colors[surface->map.info.colorkey].a;
  1729. surface->palette->colors[surface->map.info.colorkey].a = SDL_ALPHA_TRANSPARENT;
  1730. }
  1731. }
  1732. result = SDL_BlitSurfaceUnchecked(surface, &bounds, convert, &bounds);
  1733. // Restore colorkey alpha value
  1734. if (palette_ck_transform) {
  1735. surface->palette->colors[surface->map.info.colorkey].a = palette_ck_value;
  1736. }
  1737. // Restore palette alpha values
  1738. if (palette_saved_alpha) {
  1739. int i;
  1740. for (i = 0; i < palette_saved_alpha_ncolors; i++) {
  1741. surface->palette->colors[i].a = palette_saved_alpha[i];
  1742. }
  1743. SDL_stack_free(palette_saved_alpha);
  1744. }
  1745. // Clean up the original surface, and update converted surface
  1746. convert->map.info.r = copy_color.r;
  1747. convert->map.info.g = copy_color.g;
  1748. convert->map.info.b = copy_color.b;
  1749. convert->map.info.a = copy_color.a;
  1750. convert->map.info.flags =
  1751. (copy_flags &
  1752. ~(SDL_COPY_COLORKEY | SDL_COPY_BLEND | SDL_COPY_RLE_DESIRED | SDL_COPY_RLE_COLORKEY |
  1753. SDL_COPY_RLE_ALPHAKEY));
  1754. surface->map.info.r = copy_color.r;
  1755. surface->map.info.g = copy_color.g;
  1756. surface->map.info.b = copy_color.b;
  1757. surface->map.info.a = copy_color.a;
  1758. surface->map.info.flags = copy_flags;
  1759. SDL_InvalidateMap(&surface->map);
  1760. // SDL_BlitSurfaceUnchecked failed, and so the conversion
  1761. if (!result) {
  1762. goto error;
  1763. }
  1764. if (copy_flags & SDL_COPY_COLORKEY) {
  1765. bool set_colorkey_by_color = false;
  1766. bool convert_colorkey = true;
  1767. if (surface->palette) {
  1768. if (palette &&
  1769. surface->palette->ncolors <= palette->ncolors &&
  1770. (SDL_memcmp(surface->palette->colors, palette->colors,
  1771. surface->palette->ncolors * sizeof(SDL_Color)) == 0)) {
  1772. // The palette is identical, just set the same colorkey
  1773. SDL_SetSurfaceColorKey(convert, true, surface->map.info.colorkey);
  1774. } else if (!palette) {
  1775. if (SDL_ISPIXELFORMAT_ALPHA(format)) {
  1776. // No need to add the colorkey, transparency is in the alpha channel
  1777. } else {
  1778. // Only set the colorkey information
  1779. set_colorkey_by_color = true;
  1780. convert_colorkey = false;
  1781. }
  1782. } else {
  1783. set_colorkey_by_color = true;
  1784. }
  1785. } else {
  1786. set_colorkey_by_color = true;
  1787. }
  1788. if (set_colorkey_by_color) {
  1789. SDL_Surface *tmp;
  1790. SDL_Surface *tmp2;
  1791. int converted_colorkey = 0;
  1792. // Create a dummy surface to get the colorkey converted
  1793. tmp = SDL_CreateSurface(1, 1, surface->format);
  1794. if (!tmp) {
  1795. goto error;
  1796. }
  1797. // Share the palette, if any
  1798. if (surface->palette) {
  1799. SDL_SetSurfacePalette(tmp, surface->palette);
  1800. }
  1801. SDL_FillSurfaceRect(tmp, NULL, surface->map.info.colorkey);
  1802. tmp->map.info.flags &= ~SDL_COPY_COLORKEY;
  1803. // Conversion of the colorkey
  1804. tmp2 = SDL_ConvertSurfaceAndColorspace(tmp, format, palette, colorspace, props);
  1805. if (!tmp2) {
  1806. SDL_DestroySurface(tmp);
  1807. goto error;
  1808. }
  1809. // Get the converted colorkey
  1810. SDL_memcpy(&converted_colorkey, tmp2->pixels, tmp2->fmt->bytes_per_pixel);
  1811. SDL_DestroySurface(tmp);
  1812. SDL_DestroySurface(tmp2);
  1813. // Set the converted colorkey on the new surface
  1814. SDL_SetSurfaceColorKey(convert, true, converted_colorkey);
  1815. // This is needed when converting for 3D texture upload
  1816. if (convert_colorkey) {
  1817. SDL_ConvertColorkeyToAlpha(convert, true);
  1818. }
  1819. }
  1820. }
  1821. end:
  1822. if (temp_palette) {
  1823. SDL_DestroyPalette(temp_palette);
  1824. }
  1825. SDL_SetSurfaceClipRect(convert, &surface->clip_rect);
  1826. /* Enable alpha blending by default if the new surface has an
  1827. * alpha channel or alpha modulation */
  1828. if (SDL_ISPIXELFORMAT_ALPHA(format) ||
  1829. (copy_flags & SDL_COPY_MODULATE_ALPHA)) {
  1830. SDL_SetSurfaceBlendMode(convert, SDL_BLENDMODE_BLEND);
  1831. }
  1832. if (copy_flags & SDL_COPY_RLE_DESIRED) {
  1833. SDL_SetSurfaceRLE(convert, true);
  1834. }
  1835. // Copy alternate images
  1836. for (int i = 0; i < surface->num_images; ++i) {
  1837. if (!SDL_AddSurfaceAlternateImage(convert, surface->images[i])) {
  1838. goto error;
  1839. }
  1840. }
  1841. // We're ready to go!
  1842. return convert;
  1843. error:
  1844. if (temp_palette) {
  1845. SDL_DestroyPalette(temp_palette);
  1846. }
  1847. if (convert) {
  1848. SDL_DestroySurface(convert);
  1849. }
  1850. return NULL;
  1851. }
  1852. SDL_Surface *SDL_DuplicateSurface(SDL_Surface *surface)
  1853. {
  1854. if (!SDL_SurfaceValid(surface)) {
  1855. SDL_InvalidParamError("surface");
  1856. return NULL;
  1857. }
  1858. return SDL_ConvertSurfaceAndColorspace(surface, surface->format, surface->palette, surface->colorspace, surface->props);
  1859. }
  1860. SDL_Surface *SDL_ScaleSurface(SDL_Surface *surface, int width, int height, SDL_ScaleMode scaleMode)
  1861. {
  1862. SDL_Surface *convert = NULL;
  1863. Uint32 copy_flags;
  1864. SDL_Color copy_color;
  1865. bool rc;
  1866. if (!SDL_SurfaceValid(surface)) {
  1867. SDL_InvalidParamError("surface");
  1868. goto error;
  1869. }
  1870. if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  1871. // We can't directly scale a YUV surface (yet!)
  1872. SDL_Surface *tmp = SDL_CreateSurface(surface->w, surface->h, SDL_PIXELFORMAT_ARGB8888);
  1873. if (!tmp) {
  1874. return NULL;
  1875. }
  1876. SDL_Surface *scaled = SDL_ScaleSurface(tmp, width, height, scaleMode);
  1877. SDL_DestroySurface(tmp);
  1878. if (!scaled) {
  1879. return NULL;
  1880. }
  1881. tmp = scaled;
  1882. SDL_Surface *result = SDL_ConvertSurfaceAndColorspace(tmp, surface->format, NULL, surface->colorspace, surface->props);
  1883. SDL_DestroySurface(tmp);
  1884. return result;
  1885. }
  1886. // Create a new surface with the desired size
  1887. convert = SDL_CreateSurface(width, height, surface->format);
  1888. if (!convert) {
  1889. goto error;
  1890. }
  1891. SDL_SetSurfacePalette(convert, surface->palette);
  1892. SDL_SetSurfaceColorspace(convert, surface->colorspace);
  1893. // Save the original copy flags
  1894. copy_flags = surface->map.info.flags;
  1895. copy_color.r = surface->map.info.r;
  1896. copy_color.g = surface->map.info.g;
  1897. copy_color.b = surface->map.info.b;
  1898. copy_color.a = surface->map.info.a;
  1899. surface->map.info.r = 0xFF;
  1900. surface->map.info.g = 0xFF;
  1901. surface->map.info.b = 0xFF;
  1902. surface->map.info.a = 0xFF;
  1903. surface->map.info.flags = (copy_flags & (SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY));
  1904. SDL_InvalidateMap(&surface->map);
  1905. rc = SDL_BlitSurfaceScaled(surface, NULL, convert, NULL, scaleMode);
  1906. // Clean up the original surface, and update converted surface
  1907. convert->map.info.r = copy_color.r;
  1908. convert->map.info.g = copy_color.g;
  1909. convert->map.info.b = copy_color.b;
  1910. convert->map.info.a = copy_color.a;
  1911. convert->map.info.flags = (copy_flags & ~(SDL_COPY_RLE_COLORKEY | SDL_COPY_RLE_ALPHAKEY));
  1912. surface->map.info.r = copy_color.r;
  1913. surface->map.info.g = copy_color.g;
  1914. surface->map.info.b = copy_color.b;
  1915. surface->map.info.a = copy_color.a;
  1916. surface->map.info.flags = copy_flags;
  1917. SDL_InvalidateMap(&surface->map);
  1918. // SDL_BlitSurfaceScaled failed, and so the conversion
  1919. if (!rc) {
  1920. goto error;
  1921. }
  1922. // We're ready to go!
  1923. return convert;
  1924. error:
  1925. if (convert) {
  1926. SDL_DestroySurface(convert);
  1927. }
  1928. return NULL;
  1929. }
  1930. SDL_Surface *SDL_ConvertSurface(SDL_Surface *surface, SDL_PixelFormat format)
  1931. {
  1932. if (!SDL_SurfaceValid(surface)) {
  1933. SDL_InvalidParamError("surface");
  1934. return NULL;
  1935. }
  1936. return SDL_ConvertSurfaceAndColorspace(surface, format, NULL, SDL_GetDefaultColorspaceForFormat(format), surface->props);
  1937. }
  1938. SDL_Surface *SDL_DuplicatePixels(int width, int height, SDL_PixelFormat format, SDL_Colorspace colorspace, void *pixels, int pitch)
  1939. {
  1940. SDL_Surface *surface = SDL_CreateSurface(width, height, format);
  1941. if (surface) {
  1942. int length = width * SDL_BYTESPERPIXEL(format);
  1943. Uint8 *src = (Uint8 *)pixels;
  1944. Uint8 *dst = (Uint8 *)surface->pixels;
  1945. int rows = height;
  1946. while (rows--) {
  1947. SDL_memcpy(dst, src, length);
  1948. dst += surface->pitch;
  1949. src += pitch;
  1950. }
  1951. SDL_SetSurfaceColorspace(surface, colorspace);
  1952. }
  1953. return surface;
  1954. }
  1955. bool SDL_ConvertPixelsAndColorspace(int width, int height,
  1956. SDL_PixelFormat src_format, SDL_Colorspace src_colorspace, SDL_PropertiesID src_properties, const void *src, int src_pitch,
  1957. SDL_PixelFormat dst_format, SDL_Colorspace dst_colorspace, SDL_PropertiesID dst_properties, void *dst, int dst_pitch)
  1958. {
  1959. SDL_Surface src_surface;
  1960. SDL_Surface dst_surface;
  1961. SDL_Rect rect;
  1962. void *nonconst_src = (void *)src;
  1963. bool result;
  1964. if (!src) {
  1965. return SDL_InvalidParamError("src");
  1966. }
  1967. if (!src_pitch) {
  1968. return SDL_InvalidParamError("src_pitch");
  1969. }
  1970. if (!dst) {
  1971. return SDL_InvalidParamError("dst");
  1972. }
  1973. if (!dst_pitch) {
  1974. return SDL_InvalidParamError("dst_pitch");
  1975. }
  1976. if (src_colorspace == SDL_COLORSPACE_UNKNOWN) {
  1977. src_colorspace = SDL_GetDefaultColorspaceForFormat(src_format);
  1978. }
  1979. if (dst_colorspace == SDL_COLORSPACE_UNKNOWN) {
  1980. dst_colorspace = SDL_GetDefaultColorspaceForFormat(dst_format);
  1981. }
  1982. if (src_format == SDL_PIXELFORMAT_MJPG) {
  1983. return SDL_ConvertPixels_STB(width, height, src_format, src_colorspace, src_properties, src, src_pitch, dst_format, dst_colorspace, dst_properties, dst, dst_pitch);
  1984. }
  1985. #ifdef SDL_HAVE_YUV
  1986. if (SDL_ISPIXELFORMAT_FOURCC(src_format) && SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1987. 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);
  1988. } else if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
  1989. 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);
  1990. } else if (SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1991. 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);
  1992. }
  1993. #else
  1994. if (SDL_ISPIXELFORMAT_FOURCC(src_format) || SDL_ISPIXELFORMAT_FOURCC(dst_format)) {
  1995. return SDL_SetError("SDL not built with YUV support");
  1996. }
  1997. #endif
  1998. // Fast path for same format copy
  1999. if (src_format == dst_format && src_colorspace == dst_colorspace) {
  2000. if (src_pitch == dst_pitch) {
  2001. SDL_memcpy(dst, src, height * src_pitch);
  2002. } else {
  2003. int i;
  2004. const int bpp = SDL_BYTESPERPIXEL(src_format);
  2005. width *= bpp;
  2006. for (i = height; i--;) {
  2007. SDL_memcpy(dst, src, width);
  2008. src = (const Uint8 *)src + src_pitch;
  2009. dst = (Uint8 *)dst + dst_pitch;
  2010. }
  2011. }
  2012. return true;
  2013. }
  2014. if (!SDL_InitializeSurface(&src_surface, width, height, src_format, src_colorspace, src_properties, nonconst_src, src_pitch, true)) {
  2015. return false;
  2016. }
  2017. SDL_SetSurfaceBlendMode(&src_surface, SDL_BLENDMODE_NONE);
  2018. if (!SDL_InitializeSurface(&dst_surface, width, height, dst_format, dst_colorspace, dst_properties, dst, dst_pitch, true)) {
  2019. return false;
  2020. }
  2021. // Set up the rect and go!
  2022. rect.x = 0;
  2023. rect.y = 0;
  2024. rect.w = width;
  2025. rect.h = height;
  2026. result = SDL_BlitSurfaceUnchecked(&src_surface, &rect, &dst_surface, &rect);
  2027. SDL_DestroySurface(&src_surface);
  2028. SDL_DestroySurface(&dst_surface);
  2029. return result;
  2030. }
  2031. bool SDL_ConvertPixels(int width, int height, SDL_PixelFormat src_format, const void *src, int src_pitch, SDL_PixelFormat dst_format, void *dst, int dst_pitch)
  2032. {
  2033. return SDL_ConvertPixelsAndColorspace(width, height,
  2034. src_format, SDL_COLORSPACE_UNKNOWN, 0, src, src_pitch,
  2035. dst_format, SDL_COLORSPACE_UNKNOWN, 0, dst, dst_pitch);
  2036. }
  2037. /*
  2038. * Premultiply the alpha on a block of pixels
  2039. *
  2040. * Here are some ideas for optimization:
  2041. * https://github.com/Wizermil/premultiply_alpha/tree/master/premultiply_alpha
  2042. * https://developer.arm.com/documentation/101964/0201/Pre-multiplied-alpha-channel-data
  2043. */
  2044. static void SDL_PremultiplyAlpha_AXYZ8888(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  2045. {
  2046. int c;
  2047. Uint32 srcpixel;
  2048. Uint32 srcR, srcG, srcB, srcA;
  2049. Uint32 dstpixel;
  2050. Uint32 dstR, dstG, dstB, dstA;
  2051. while (height--) {
  2052. const Uint32 *src_px = (const Uint32 *)src;
  2053. Uint32 *dst_px = (Uint32 *)dst;
  2054. for (c = width; c; --c) {
  2055. // Component bytes extraction.
  2056. srcpixel = *src_px++;
  2057. RGBA_FROM_ARGB8888(srcpixel, srcR, srcG, srcB, srcA);
  2058. // Alpha pre-multiplication of each component.
  2059. dstA = srcA;
  2060. dstR = (srcA * srcR) / 255;
  2061. dstG = (srcA * srcG) / 255;
  2062. dstB = (srcA * srcB) / 255;
  2063. // ARGB8888 pixel recomposition.
  2064. ARGB8888_FROM_RGBA(dstpixel, dstR, dstG, dstB, dstA);
  2065. *dst_px++ = dstpixel;
  2066. }
  2067. src = (const Uint8 *)src + src_pitch;
  2068. dst = (Uint8 *)dst + dst_pitch;
  2069. }
  2070. }
  2071. static void SDL_PremultiplyAlpha_XYZA8888(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  2072. {
  2073. int c;
  2074. Uint32 srcpixel;
  2075. Uint32 srcR, srcG, srcB, srcA;
  2076. Uint32 dstpixel;
  2077. Uint32 dstR, dstG, dstB, dstA;
  2078. while (height--) {
  2079. const Uint32 *src_px = (const Uint32 *)src;
  2080. Uint32 *dst_px = (Uint32 *)dst;
  2081. for (c = width; c; --c) {
  2082. // Component bytes extraction.
  2083. srcpixel = *src_px++;
  2084. RGBA_FROM_RGBA8888(srcpixel, srcR, srcG, srcB, srcA);
  2085. // Alpha pre-multiplication of each component.
  2086. dstA = srcA;
  2087. dstR = (srcA * srcR) / 255;
  2088. dstG = (srcA * srcG) / 255;
  2089. dstB = (srcA * srcB) / 255;
  2090. // RGBA8888 pixel recomposition.
  2091. RGBA8888_FROM_RGBA(dstpixel, dstR, dstG, dstB, dstA);
  2092. *dst_px++ = dstpixel;
  2093. }
  2094. src = (const Uint8 *)src + src_pitch;
  2095. dst = (Uint8 *)dst + dst_pitch;
  2096. }
  2097. }
  2098. static void SDL_PremultiplyAlpha_AXYZ128(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  2099. {
  2100. int c;
  2101. float flR, flG, flB, flA;
  2102. while (height--) {
  2103. const float *src_px = (const float *)src;
  2104. float *dst_px = (float *)dst;
  2105. for (c = width; c; --c) {
  2106. flA = *src_px++;
  2107. flR = *src_px++;
  2108. flG = *src_px++;
  2109. flB = *src_px++;
  2110. // Alpha pre-multiplication of each component.
  2111. flR *= flA;
  2112. flG *= flA;
  2113. flB *= flA;
  2114. *dst_px++ = flA;
  2115. *dst_px++ = flR;
  2116. *dst_px++ = flG;
  2117. *dst_px++ = flB;
  2118. }
  2119. src = (const Uint8 *)src + src_pitch;
  2120. dst = (Uint8 *)dst + dst_pitch;
  2121. }
  2122. }
  2123. static bool 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, bool linear)
  2124. {
  2125. SDL_Surface *convert = NULL;
  2126. void *final_dst = dst;
  2127. int final_dst_pitch = dst_pitch;
  2128. SDL_PixelFormat format;
  2129. SDL_Colorspace colorspace;
  2130. bool result = false;
  2131. if (!src) {
  2132. return SDL_InvalidParamError("src");
  2133. }
  2134. if (!src_pitch) {
  2135. return SDL_InvalidParamError("src_pitch");
  2136. }
  2137. if (!dst) {
  2138. return SDL_InvalidParamError("dst");
  2139. }
  2140. if (!dst_pitch) {
  2141. return SDL_InvalidParamError("dst_pitch");
  2142. }
  2143. // Use a high precision format if we're converting to linear colorspace or using high precision pixel formats
  2144. if (linear ||
  2145. SDL_ISPIXELFORMAT_10BIT(src_format) || SDL_BITSPERPIXEL(src_format) > 32 ||
  2146. SDL_ISPIXELFORMAT_10BIT(dst_format) || SDL_BITSPERPIXEL(dst_format) > 32) {
  2147. if (src_format == SDL_PIXELFORMAT_ARGB128_FLOAT ||
  2148. src_format == SDL_PIXELFORMAT_ABGR128_FLOAT) {
  2149. format = src_format;
  2150. } else {
  2151. format = SDL_PIXELFORMAT_ARGB128_FLOAT;
  2152. }
  2153. } else {
  2154. if (src_format == SDL_PIXELFORMAT_ARGB8888 ||
  2155. src_format == SDL_PIXELFORMAT_ABGR8888 ||
  2156. src_format == SDL_PIXELFORMAT_RGBA8888 ||
  2157. src_format == SDL_PIXELFORMAT_BGRA8888) {
  2158. format = src_format;
  2159. } else {
  2160. format = SDL_PIXELFORMAT_ARGB8888;
  2161. }
  2162. }
  2163. if (linear) {
  2164. colorspace = SDL_COLORSPACE_SRGB_LINEAR;
  2165. } else {
  2166. colorspace = SDL_COLORSPACE_SRGB;
  2167. }
  2168. if (src_format != format || src_colorspace != colorspace) {
  2169. convert = SDL_CreateSurface(width, height, format);
  2170. if (!convert) {
  2171. goto done;
  2172. }
  2173. if (!SDL_ConvertPixelsAndColorspace(width, height, src_format, src_colorspace, src_properties, src, src_pitch, format, colorspace, 0, convert->pixels, convert->pitch)) {
  2174. goto done;
  2175. }
  2176. src = convert->pixels;
  2177. src_pitch = convert->pitch;
  2178. dst = convert->pixels;
  2179. dst_pitch = convert->pitch;
  2180. } else if (dst_format != format || dst_colorspace != colorspace) {
  2181. convert = SDL_CreateSurface(width, height, format);
  2182. if (!convert) {
  2183. goto done;
  2184. }
  2185. dst = convert->pixels;
  2186. dst_pitch = convert->pitch;
  2187. }
  2188. switch (format) {
  2189. case SDL_PIXELFORMAT_ARGB8888:
  2190. case SDL_PIXELFORMAT_ABGR8888:
  2191. SDL_PremultiplyAlpha_AXYZ8888(width, height, src, src_pitch, dst, dst_pitch);
  2192. break;
  2193. case SDL_PIXELFORMAT_RGBA8888:
  2194. case SDL_PIXELFORMAT_BGRA8888:
  2195. SDL_PremultiplyAlpha_XYZA8888(width, height, src, src_pitch, dst, dst_pitch);
  2196. break;
  2197. case SDL_PIXELFORMAT_ARGB128_FLOAT:
  2198. case SDL_PIXELFORMAT_ABGR128_FLOAT:
  2199. SDL_PremultiplyAlpha_AXYZ128(width, height, src, src_pitch, dst, dst_pitch);
  2200. break;
  2201. default:
  2202. SDL_SetError("Unexpected internal pixel format");
  2203. goto done;
  2204. }
  2205. if (dst != final_dst) {
  2206. if (!SDL_ConvertPixelsAndColorspace(width, height, format, colorspace, 0, convert->pixels, convert->pitch, dst_format, dst_colorspace, dst_properties, final_dst, final_dst_pitch)) {
  2207. goto done;
  2208. }
  2209. }
  2210. result = true;
  2211. done:
  2212. if (convert) {
  2213. SDL_DestroySurface(convert);
  2214. }
  2215. return result;
  2216. }
  2217. bool SDL_PremultiplyAlpha(int width, int height,
  2218. SDL_PixelFormat src_format, const void *src, int src_pitch,
  2219. SDL_PixelFormat dst_format, void *dst, int dst_pitch, bool linear)
  2220. {
  2221. SDL_Colorspace src_colorspace = SDL_GetDefaultColorspaceForFormat(src_format);
  2222. SDL_Colorspace dst_colorspace = SDL_GetDefaultColorspaceForFormat(dst_format);
  2223. return SDL_PremultiplyAlphaPixelsAndColorspace(width, height, src_format, src_colorspace, 0, src, src_pitch, dst_format, dst_colorspace, 0, dst, dst_pitch, linear);
  2224. }
  2225. bool SDL_PremultiplySurfaceAlpha(SDL_Surface *surface, bool linear)
  2226. {
  2227. SDL_Colorspace colorspace;
  2228. if (!SDL_SurfaceValid(surface)) {
  2229. return SDL_InvalidParamError("surface");
  2230. }
  2231. colorspace = surface->colorspace;
  2232. return SDL_PremultiplyAlphaPixelsAndColorspace(surface->w, surface->h, surface->format, colorspace, surface->props, surface->pixels, surface->pitch, surface->format, colorspace, surface->props, surface->pixels, surface->pitch, linear);
  2233. }
  2234. bool SDL_ClearSurface(SDL_Surface *surface, float r, float g, float b, float a)
  2235. {
  2236. SDL_Rect clip_rect;
  2237. bool result = false;
  2238. if (!SDL_SurfaceValid(surface)) {
  2239. return SDL_InvalidParamError("surface");
  2240. }
  2241. SDL_GetSurfaceClipRect(surface, &clip_rect);
  2242. SDL_SetSurfaceClipRect(surface, NULL);
  2243. if (!SDL_ISPIXELFORMAT_FOURCC(surface->format) &&
  2244. SDL_BYTESPERPIXEL(surface->format) <= sizeof(Uint32)) {
  2245. Uint32 color;
  2246. color = SDL_MapSurfaceRGBA(surface,
  2247. (Uint8)SDL_roundf(SDL_clamp(r, 0.0f, 1.0f) * 255.0f),
  2248. (Uint8)SDL_roundf(SDL_clamp(g, 0.0f, 1.0f) * 255.0f),
  2249. (Uint8)SDL_roundf(SDL_clamp(b, 0.0f, 1.0f) * 255.0f),
  2250. (Uint8)SDL_roundf(SDL_clamp(a, 0.0f, 1.0f) * 255.0f));
  2251. result = SDL_FillSurfaceRect(surface, NULL, color);
  2252. } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2253. // We can't directly set an RGB value on a YUV surface
  2254. SDL_Surface *tmp = SDL_CreateSurface(surface->w, surface->h, SDL_PIXELFORMAT_ARGB8888);
  2255. if (!tmp) {
  2256. goto done;
  2257. }
  2258. if (SDL_ClearSurface(tmp, r, g, b, a)) {
  2259. result = SDL_ConvertPixelsAndColorspace(surface->w, surface->h, tmp->format, tmp->colorspace, tmp->props, tmp->pixels, tmp->pitch, surface->format, surface->colorspace, surface->props, surface->pixels, surface->pitch);
  2260. }
  2261. SDL_DestroySurface(tmp);
  2262. } else {
  2263. // Take advantage of blit color conversion
  2264. SDL_Surface *tmp = SDL_CreateSurface(1, 1, SDL_PIXELFORMAT_RGBA128_FLOAT);
  2265. if (!tmp) {
  2266. goto done;
  2267. }
  2268. SDL_SetSurfaceColorspace(tmp, surface->colorspace);
  2269. SDL_SetSurfaceBlendMode(tmp, SDL_BLENDMODE_NONE);
  2270. float *pixels = (float *)tmp->pixels;
  2271. pixels[0] = r;
  2272. pixels[1] = g;
  2273. pixels[2] = b;
  2274. pixels[3] = a;
  2275. result = SDL_BlitSurfaceScaled(tmp, NULL, surface, NULL, SDL_SCALEMODE_NEAREST);
  2276. SDL_DestroySurface(tmp);
  2277. }
  2278. done:
  2279. SDL_SetSurfaceClipRect(surface, &clip_rect);
  2280. return result;
  2281. }
  2282. Uint32 SDL_MapSurfaceRGB(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b)
  2283. {
  2284. return SDL_MapSurfaceRGBA(surface, r, g, b, SDL_ALPHA_OPAQUE);
  2285. }
  2286. Uint32 SDL_MapSurfaceRGBA(SDL_Surface *surface, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2287. {
  2288. if (!SDL_SurfaceValid(surface)) {
  2289. SDL_InvalidParamError("surface");
  2290. return true;
  2291. }
  2292. return SDL_MapRGBA(surface->fmt, surface->palette, r, g, b, a);
  2293. }
  2294. // This function Copyright 2023 Collabora Ltd., contributed to SDL under the ZLib license
  2295. bool SDL_ReadSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 *r, Uint8 *g, Uint8 *b, Uint8 *a)
  2296. {
  2297. Uint32 pixel = 0;
  2298. size_t bytes_per_pixel;
  2299. Uint8 unused;
  2300. Uint8 *p;
  2301. bool result = false;
  2302. if (r) {
  2303. *r = 0;
  2304. } else {
  2305. r = &unused;
  2306. }
  2307. if (g) {
  2308. *g = 0;
  2309. } else {
  2310. g = &unused;
  2311. }
  2312. if (b) {
  2313. *b = 0;
  2314. } else {
  2315. b = &unused;
  2316. }
  2317. if (a) {
  2318. *a = 0;
  2319. } else {
  2320. a = &unused;
  2321. }
  2322. if (!SDL_SurfaceValid(surface) || !surface->format || !surface->pixels) {
  2323. return SDL_InvalidParamError("surface");
  2324. }
  2325. if (x < 0 || x >= surface->w) {
  2326. return SDL_InvalidParamError("x");
  2327. }
  2328. if (y < 0 || y >= surface->h) {
  2329. return SDL_InvalidParamError("y");
  2330. }
  2331. bytes_per_pixel = SDL_BYTESPERPIXEL(surface->format);
  2332. if (SDL_MUSTLOCK(surface)) {
  2333. if (!SDL_LockSurface(surface)) {
  2334. return false;
  2335. }
  2336. }
  2337. p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel;
  2338. if (bytes_per_pixel <= sizeof(pixel) && !SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2339. /* Fill the appropriate number of least-significant bytes of pixel,
  2340. * leaving the most-significant bytes set to zero */
  2341. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  2342. SDL_memcpy(((Uint8 *)&pixel) + (sizeof(pixel) - bytes_per_pixel), p, bytes_per_pixel);
  2343. #else
  2344. SDL_memcpy(&pixel, p, bytes_per_pixel);
  2345. #endif
  2346. SDL_GetRGBA(pixel, surface->fmt, surface->palette, r, g, b, a);
  2347. result = true;
  2348. } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2349. // FIXME: We need code to extract a single macroblock from a YUV surface
  2350. SDL_Surface *converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888);
  2351. if (converted) {
  2352. result = SDL_ReadSurfacePixel(converted, x, y, r, g, b, a);
  2353. SDL_DestroySurface(converted);
  2354. }
  2355. } else {
  2356. // This is really slow, but it gets the job done
  2357. Uint8 rgba[4];
  2358. if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, surface->colorspace, surface->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba))) {
  2359. *r = rgba[0];
  2360. *g = rgba[1];
  2361. *b = rgba[2];
  2362. *a = rgba[3];
  2363. result = true;
  2364. }
  2365. }
  2366. if (SDL_MUSTLOCK(surface)) {
  2367. SDL_UnlockSurface(surface);
  2368. }
  2369. return result;
  2370. }
  2371. bool SDL_ReadSurfacePixelFloat(SDL_Surface *surface, int x, int y, float *r, float *g, float *b, float *a)
  2372. {
  2373. float unused;
  2374. bool result = false;
  2375. if (r) {
  2376. *r = 0.0f;
  2377. } else {
  2378. r = &unused;
  2379. }
  2380. if (g) {
  2381. *g = 0.0f;
  2382. } else {
  2383. g = &unused;
  2384. }
  2385. if (b) {
  2386. *b = 0.0f;
  2387. } else {
  2388. b = &unused;
  2389. }
  2390. if (a) {
  2391. *a = 0.0f;
  2392. } else {
  2393. a = &unused;
  2394. }
  2395. if (!SDL_SurfaceValid(surface) || !surface->format || !surface->pixels) {
  2396. return SDL_InvalidParamError("surface");
  2397. }
  2398. if (x < 0 || x >= surface->w) {
  2399. return SDL_InvalidParamError("x");
  2400. }
  2401. if (y < 0 || y >= surface->h) {
  2402. return SDL_InvalidParamError("y");
  2403. }
  2404. if (SDL_BYTESPERPIXEL(surface->format) <= sizeof(Uint32) && !SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2405. Uint8 r8, g8, b8, a8;
  2406. if (SDL_ReadSurfacePixel(surface, x, y, &r8, &g8, &b8, &a8)) {
  2407. *r = (float)r8 / 255.0f;
  2408. *g = (float)g8 / 255.0f;
  2409. *b = (float)b8 / 255.0f;
  2410. *a = (float)a8 / 255.0f;
  2411. result = true;
  2412. }
  2413. } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2414. // FIXME: We need code to extract a single macroblock from a YUV surface
  2415. SDL_Surface *converted = SDL_ConvertSurface(surface, SDL_PIXELFORMAT_ARGB8888);
  2416. if (converted) {
  2417. result = SDL_ReadSurfacePixelFloat(converted, x, y, r, g, b, a);
  2418. SDL_DestroySurface(converted);
  2419. }
  2420. } else {
  2421. // This is really slow, but it gets the job done
  2422. float rgba[4];
  2423. Uint8 *p;
  2424. if (SDL_MUSTLOCK(surface)) {
  2425. if (!SDL_LockSurface(surface)) {
  2426. return false;
  2427. }
  2428. }
  2429. p = (Uint8 *)surface->pixels + y * surface->pitch + x * SDL_BYTESPERPIXEL(surface->format);
  2430. if (surface->format == SDL_PIXELFORMAT_RGBA128_FLOAT) {
  2431. SDL_memcpy(rgba, p, sizeof(rgba));
  2432. result = true;
  2433. } else {
  2434. SDL_Colorspace src_colorspace = surface->colorspace;
  2435. SDL_Colorspace dst_colorspace = (src_colorspace == SDL_COLORSPACE_SRGB_LINEAR ? SDL_COLORSPACE_SRGB_LINEAR : SDL_COLORSPACE_SRGB);
  2436. if (SDL_ConvertPixelsAndColorspace(1, 1, surface->format, src_colorspace, surface->props, p, surface->pitch, SDL_PIXELFORMAT_RGBA128_FLOAT, dst_colorspace, 0, rgba, sizeof(rgba))) {
  2437. result = true;
  2438. }
  2439. }
  2440. if (result) {
  2441. *r = rgba[0];
  2442. *g = rgba[1];
  2443. *b = rgba[2];
  2444. *a = rgba[3];
  2445. }
  2446. if (SDL_MUSTLOCK(surface)) {
  2447. SDL_UnlockSurface(surface);
  2448. }
  2449. }
  2450. return result;
  2451. }
  2452. bool SDL_WriteSurfacePixel(SDL_Surface *surface, int x, int y, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
  2453. {
  2454. Uint32 pixel = 0;
  2455. size_t bytes_per_pixel;
  2456. Uint8 *p;
  2457. bool result = false;
  2458. if (!SDL_SurfaceValid(surface) || !surface->format || !surface->pixels) {
  2459. return SDL_InvalidParamError("surface");
  2460. }
  2461. if (x < 0 || x >= surface->w) {
  2462. return SDL_InvalidParamError("x");
  2463. }
  2464. if (y < 0 || y >= surface->h) {
  2465. return SDL_InvalidParamError("y");
  2466. }
  2467. bytes_per_pixel = SDL_BYTESPERPIXEL(surface->format);
  2468. if (SDL_MUSTLOCK(surface)) {
  2469. if (!SDL_LockSurface(surface)) {
  2470. return false;
  2471. }
  2472. }
  2473. p = (Uint8 *)surface->pixels + y * surface->pitch + x * bytes_per_pixel;
  2474. if (bytes_per_pixel <= sizeof(pixel) && !SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2475. pixel = SDL_MapRGBA(surface->fmt, surface->palette, r, g, b, a);
  2476. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  2477. SDL_memcpy(p, ((Uint8 *)&pixel) + (sizeof(pixel) - bytes_per_pixel), bytes_per_pixel);
  2478. #else
  2479. SDL_memcpy(p, &pixel, bytes_per_pixel);
  2480. #endif
  2481. result = true;
  2482. } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2483. result = SDL_Unsupported();
  2484. } else {
  2485. // This is really slow, but it gets the job done
  2486. Uint8 rgba[4];
  2487. rgba[0] = r;
  2488. rgba[1] = g;
  2489. rgba[2] = b;
  2490. rgba[3] = a;
  2491. result = SDL_ConvertPixelsAndColorspace(1, 1, SDL_PIXELFORMAT_RGBA32, SDL_COLORSPACE_SRGB, 0, rgba, sizeof(rgba), surface->format, surface->colorspace, surface->props, p, surface->pitch);
  2492. }
  2493. if (SDL_MUSTLOCK(surface)) {
  2494. SDL_UnlockSurface(surface);
  2495. }
  2496. return result;
  2497. }
  2498. bool SDL_WriteSurfacePixelFloat(SDL_Surface *surface, int x, int y, float r, float g, float b, float a)
  2499. {
  2500. bool result = false;
  2501. if (!SDL_SurfaceValid(surface) || !surface->format || !surface->pixels) {
  2502. return SDL_InvalidParamError("surface");
  2503. }
  2504. if (x < 0 || x >= surface->w) {
  2505. return SDL_InvalidParamError("x");
  2506. }
  2507. if (y < 0 || y >= surface->h) {
  2508. return SDL_InvalidParamError("y");
  2509. }
  2510. if (SDL_BYTESPERPIXEL(surface->format) <= sizeof(Uint32) && !SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2511. Uint8 r8, g8, b8, a8;
  2512. r8 = (Uint8)SDL_round(SDL_clamp(r, 0.0f, 1.0f) * 255.0f);
  2513. g8 = (Uint8)SDL_round(SDL_clamp(g, 0.0f, 1.0f) * 255.0f);
  2514. b8 = (Uint8)SDL_round(SDL_clamp(b, 0.0f, 1.0f) * 255.0f);
  2515. a8 = (Uint8)SDL_round(SDL_clamp(a, 0.0f, 1.0f) * 255.0f);
  2516. if (SDL_WriteSurfacePixel(surface, x, y, r8, g8, b8, a8)) {
  2517. result = true;
  2518. }
  2519. } else if (SDL_ISPIXELFORMAT_FOURCC(surface->format)) {
  2520. result = SDL_Unsupported();
  2521. } else {
  2522. // This is really slow, but it gets the job done
  2523. float rgba[4];
  2524. Uint8 *p;
  2525. if (SDL_MUSTLOCK(surface)) {
  2526. if (!SDL_LockSurface(surface)) {
  2527. return false;
  2528. }
  2529. }
  2530. p = (Uint8 *)surface->pixels + y * surface->pitch + x * SDL_BYTESPERPIXEL(surface->format);
  2531. rgba[0] = r;
  2532. rgba[1] = g;
  2533. rgba[2] = b;
  2534. rgba[3] = a;
  2535. if (surface->format == SDL_PIXELFORMAT_RGBA128_FLOAT) {
  2536. SDL_memcpy(p, rgba, sizeof(rgba));
  2537. result = true;
  2538. } else {
  2539. SDL_Colorspace dst_colorspace = surface->colorspace;
  2540. SDL_Colorspace src_colorspace = (dst_colorspace == SDL_COLORSPACE_SRGB_LINEAR ? SDL_COLORSPACE_SRGB_LINEAR : SDL_COLORSPACE_SRGB);
  2541. result = SDL_ConvertPixelsAndColorspace(1, 1, SDL_PIXELFORMAT_RGBA128_FLOAT, src_colorspace, 0, rgba, sizeof(rgba), surface->format, dst_colorspace, surface->props, p, surface->pitch);
  2542. }
  2543. if (SDL_MUSTLOCK(surface)) {
  2544. SDL_UnlockSurface(surface);
  2545. }
  2546. }
  2547. return result;
  2548. }
  2549. /*
  2550. * Free a surface created by the above function.
  2551. */
  2552. void SDL_DestroySurface(SDL_Surface *surface)
  2553. {
  2554. if (!SDL_SurfaceValid(surface)) {
  2555. return;
  2556. }
  2557. if (surface->internal_flags & SDL_INTERNAL_SURFACE_DONTFREE) {
  2558. return;
  2559. }
  2560. if (--surface->refcount > 0) {
  2561. return;
  2562. }
  2563. SDL_RemoveSurfaceAlternateImages(surface);
  2564. SDL_DestroyProperties(surface->props);
  2565. SDL_InvalidateMap(&surface->map);
  2566. while (surface->locked > 0) {
  2567. SDL_UnlockSurface(surface);
  2568. }
  2569. #ifdef SDL_HAVE_RLE
  2570. if (surface->internal_flags & SDL_INTERNAL_SURFACE_RLEACCEL) {
  2571. SDL_UnRLESurface(surface, false);
  2572. }
  2573. #endif
  2574. SDL_SetSurfacePalette(surface, NULL);
  2575. if (surface->flags & SDL_SURFACE_PREALLOCATED) {
  2576. // Don't free
  2577. } else if (surface->flags & SDL_SURFACE_SIMD_ALIGNED) {
  2578. // Free aligned
  2579. SDL_aligned_free(surface->pixels);
  2580. } else {
  2581. // Normal
  2582. SDL_free(surface->pixels);
  2583. }
  2584. surface->reserved = NULL;
  2585. if (!(surface->internal_flags & SDL_INTERNAL_SURFACE_STACK)) {
  2586. SDL_free(surface);
  2587. }
  2588. }