stb_image_resize.cpp 97 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282
  1. // SPDX-FileCopyrightText: Jorge L Rodriguez
  2. // SPDX-License-Identifier: MIT
  3. /* stb_image_resize - v0.97 - public domain image resizing
  4. by Jorge L Rodriguez (@VinoBS) - 2014
  5. http://github.com/nothings/stb
  6. CONTRIBUTORS
  7. Jorge L Rodriguez: Implementation
  8. Sean Barrett: API design, optimizations
  9. Aras Pranckevicius: bugfix
  10. Nathan Reed: warning fixes
  11. REVISIONS
  12. 0.97 (2020-02-02) fixed warning
  13. 0.96 (2019-03-04) fixed warnings
  14. 0.95 (2017-07-23) fixed warnings
  15. 0.94 (2017-03-18) fixed warnings
  16. 0.93 (2017-03-03) fixed bug with certain combinations of heights
  17. 0.92 (2017-01-02) fix integer overflow on large (>2GB) images
  18. 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions
  19. 0.90 (2014-09-17) first released version
  20. LICENSE
  21. See end of file for license information.
  22. TODO
  23. Don't decode all of the image data when only processing a partial tile
  24. Don't use full-width decode buffers when only processing a partial tile
  25. When processing wide images, break processing into tiles so data fits in L1 cache
  26. Installable filters?
  27. Resize that respects alpha test coverage
  28. (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:
  29. https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )
  30. */
  31. #include <stb_image_resize.h>
  32. #ifndef STBIR_ASSERT
  33. #include <assert.h>
  34. #define STBIR_ASSERT(x) assert(x)
  35. #endif
  36. // For memset
  37. #include <string.h>
  38. #include <math.h>
  39. #ifndef STBIR_MALLOC
  40. #include <stdlib.h>
  41. // use comma operator to evaluate c, to avoid "unused parameter" warnings
  42. #define STBIR_MALLOC(size,c) ((void)(c), malloc(size))
  43. #define STBIR_FREE(ptr,c) ((void)(c), free(ptr))
  44. #endif
  45. #ifndef _MSC_VER
  46. #ifdef __cplusplus
  47. #define stbir__inline inline
  48. #else
  49. #define stbir__inline
  50. #endif
  51. #else
  52. #define stbir__inline __forceinline
  53. #endif
  54. // should produce compiler error if size is wrong
  55. typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1];
  56. #ifdef _MSC_VER
  57. #define STBIR__NOTUSED(v) (void)(v)
  58. #else
  59. #define STBIR__NOTUSED(v) (void)sizeof(v)
  60. #endif
  61. #define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0]))
  62. #ifndef STBIR_DEFAULT_FILTER_UPSAMPLE
  63. #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM
  64. #endif
  65. #ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE
  66. #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL
  67. #endif
  68. #ifndef STBIR_PROGRESS_REPORT
  69. #define STBIR_PROGRESS_REPORT(float_0_to_1)
  70. #endif
  71. #ifndef STBIR_MAX_CHANNELS
  72. #define STBIR_MAX_CHANNELS 64
  73. #endif
  74. #if STBIR_MAX_CHANNELS > 65536
  75. #error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536."
  76. // because we store the indices in 16-bit variables
  77. #endif
  78. // This value is added to alpha just before premultiplication to avoid
  79. // zeroing out color values. It is equivalent to 2^-80. If you don't want
  80. // that behavior (it may interfere if you have floating point images with
  81. // very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to
  82. // disable it.
  83. #ifndef STBIR_ALPHA_EPSILON
  84. #define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20))
  85. #endif
  86. #ifdef _MSC_VER
  87. #define STBIR__UNUSED_PARAM(v) (void)(v)
  88. #else
  89. #define STBIR__UNUSED_PARAM(v) (void)sizeof(v)
  90. #endif
  91. // must match stbir_datatype
  92. static unsigned char stbir__type_size[] = {
  93. 1, // STBIR_TYPE_UINT8
  94. 2, // STBIR_TYPE_UINT16
  95. 4, // STBIR_TYPE_UINT32
  96. 4, // STBIR_TYPE_FLOAT
  97. };
  98. // Kernel function centered at 0
  99. typedef float (stbir__kernel_fn)(float x, float scale);
  100. typedef float (stbir__support_fn)(float scale);
  101. typedef struct
  102. {
  103. stbir__kernel_fn* kernel;
  104. stbir__support_fn* support;
  105. } stbir__filter_info;
  106. // When upsampling, the contributors are which source pixels contribute.
  107. // When downsampling, the contributors are which destination pixels are contributed to.
  108. typedef struct
  109. {
  110. int n0; // First contributing pixel
  111. int n1; // Last contributing pixel
  112. } stbir__contributors;
  113. typedef struct
  114. {
  115. const void* input_data;
  116. int input_w;
  117. int input_h;
  118. int input_stride_bytes;
  119. void* output_data;
  120. int output_w;
  121. int output_h;
  122. int output_stride_bytes;
  123. float s0, t0, s1, t1;
  124. float horizontal_shift; // Units: output pixels
  125. float vertical_shift; // Units: output pixels
  126. float horizontal_scale;
  127. float vertical_scale;
  128. int channels;
  129. int alpha_channel;
  130. stbir_uint32 flags;
  131. stbir_datatype type;
  132. stbir_filter horizontal_filter;
  133. stbir_filter vertical_filter;
  134. stbir_edge edge_horizontal;
  135. stbir_edge edge_vertical;
  136. stbir_colorspace colorspace;
  137. stbir__contributors* horizontal_contributors;
  138. float* horizontal_coefficients;
  139. stbir__contributors* vertical_contributors;
  140. float* vertical_coefficients;
  141. int decode_buffer_pixels;
  142. float* decode_buffer;
  143. float* horizontal_buffer;
  144. // cache these because ceil/floor are inexplicably showing up in profile
  145. int horizontal_coefficient_width;
  146. int vertical_coefficient_width;
  147. int horizontal_filter_pixel_width;
  148. int vertical_filter_pixel_width;
  149. int horizontal_filter_pixel_margin;
  150. int vertical_filter_pixel_margin;
  151. int horizontal_num_contributors;
  152. int vertical_num_contributors;
  153. int ring_buffer_length_bytes; // The length of an individual entry in the ring buffer. The total number of ring buffers is stbir__get_filter_pixel_width(filter)
  154. int ring_buffer_num_entries; // Total number of entries in the ring buffer.
  155. int ring_buffer_first_scanline;
  156. int ring_buffer_last_scanline;
  157. int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer
  158. float* ring_buffer;
  159. float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds.
  160. int horizontal_contributors_size;
  161. int horizontal_coefficients_size;
  162. int vertical_contributors_size;
  163. int vertical_coefficients_size;
  164. int decode_buffer_size;
  165. int horizontal_buffer_size;
  166. int ring_buffer_size;
  167. int encode_buffer_size;
  168. } stbir__info;
  169. static const float stbir__max_uint8_as_float = 255.0f;
  170. static const float stbir__max_uint16_as_float = 65535.0f;
  171. static const double stbir__max_uint32_as_float = 4294967295.0;
  172. static stbir__inline int stbir__min(int a, int b)
  173. {
  174. return a < b ? a : b;
  175. }
  176. static stbir__inline float stbir__saturate(float x)
  177. {
  178. if (x < 0)
  179. return 0;
  180. if (x > 1)
  181. return 1;
  182. return x;
  183. }
  184. #ifdef STBIR_SATURATE_INT
  185. static stbir__inline stbir_uint8 stbir__saturate8(int x)
  186. {
  187. if ((unsigned int) x <= 255)
  188. return x;
  189. if (x < 0)
  190. return 0;
  191. return 255;
  192. }
  193. static stbir__inline stbir_uint16 stbir__saturate16(int x)
  194. {
  195. if ((unsigned int) x <= 65535)
  196. return x;
  197. if (x < 0)
  198. return 0;
  199. return 65535;
  200. }
  201. #endif
  202. static float stbir__srgb_uchar_to_linear_float[256] = {
  203. 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f,
  204. 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f,
  205. 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f,
  206. 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f,
  207. 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f,
  208. 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f,
  209. 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f,
  210. 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f,
  211. 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f,
  212. 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f,
  213. 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f,
  214. 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f,
  215. 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f,
  216. 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f,
  217. 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f,
  218. 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f,
  219. 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f,
  220. 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f,
  221. 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f,
  222. 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f,
  223. 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f,
  224. 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f,
  225. 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f,
  226. 0.982251f, 0.991102f, 1.0f
  227. };
  228. static float stbir__srgb_to_linear(float f)
  229. {
  230. if (f <= 0.04045f)
  231. return f / 12.92f;
  232. else
  233. return (float)pow((f + 0.055f) / 1.055f, 2.4f);
  234. }
  235. static float stbir__linear_to_srgb(float f)
  236. {
  237. if (f <= 0.0031308f)
  238. return f * 12.92f;
  239. else
  240. return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f;
  241. }
  242. #ifndef STBIR_NON_IEEE_FLOAT
  243. // From https://gist.github.com/rygorous/2203834
  244. typedef union
  245. {
  246. stbir_uint32 u;
  247. float f;
  248. } stbir__FP32;
  249. static const stbir_uint32 fp32_to_srgb8_tab4[104] = {
  250. 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d,
  251. 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a,
  252. 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033,
  253. 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067,
  254. 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5,
  255. 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2,
  256. 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143,
  257. 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af,
  258. 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240,
  259. 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300,
  260. 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401,
  261. 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559,
  262. 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723,
  263. };
  264. static stbir_uint8 stbir__linear_to_srgb_uchar(float in)
  265. {
  266. static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps
  267. static const stbir__FP32 minval = { (127-13) << 23 };
  268. stbir_uint32 tab,bias,scale,t;
  269. stbir__FP32 f;
  270. // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively.
  271. // The tests are carefully written so that NaNs map to 0, same as in the reference
  272. // implementation.
  273. if (!(in > minval.f)) // written this way to catch NaNs
  274. in = minval.f;
  275. if (in > almostone.f)
  276. in = almostone.f;
  277. // Do the table lookup and unpack bias, scale
  278. f.f = in;
  279. tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20];
  280. bias = (tab >> 16) << 9;
  281. scale = tab & 0xffff;
  282. // Grab next-highest mantissa bits and perform linear interpolation
  283. t = (f.u >> 12) & 0xff;
  284. return (unsigned char) ((bias + scale*t) >> 16);
  285. }
  286. #else
  287. // sRGB transition values, scaled by 1<<28
  288. static int stbir__srgb_offset_to_linear_scaled[256] =
  289. {
  290. 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603,
  291. 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926,
  292. 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148,
  293. 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856,
  294. 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731,
  295. 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369,
  296. 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021,
  297. 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073,
  298. 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389,
  299. 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552,
  300. 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066,
  301. 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490,
  302. 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568,
  303. 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316,
  304. 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096,
  305. 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700,
  306. 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376,
  307. 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912,
  308. 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648,
  309. 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512,
  310. 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072,
  311. 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544,
  312. 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832,
  313. 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528,
  314. 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968,
  315. 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184,
  316. 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992,
  317. 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968,
  318. 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480,
  319. 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656,
  320. 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464,
  321. 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664,
  322. };
  323. static stbir_uint8 stbir__linear_to_srgb_uchar(float f)
  324. {
  325. int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp
  326. int v = 0;
  327. int i;
  328. // Refine the guess with a short binary search.
  329. i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  330. i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  331. i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  332. i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  333. i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  334. i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  335. i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  336. i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i;
  337. return (stbir_uint8) v;
  338. }
  339. #endif
  340. static float stbir__filter_trapezoid(float x, float scale)
  341. {
  342. float halfscale = scale / 2;
  343. float t = 0.5f + halfscale;
  344. STBIR_ASSERT(scale <= 1);
  345. x = (float)fabs(x);
  346. if (x >= t)
  347. return 0;
  348. else
  349. {
  350. float r = 0.5f - halfscale;
  351. if (x <= r)
  352. return 1;
  353. else
  354. return (t - x) / scale;
  355. }
  356. }
  357. static float stbir__support_trapezoid(float scale)
  358. {
  359. STBIR_ASSERT(scale <= 1);
  360. return 0.5f + scale / 2;
  361. }
  362. static float stbir__filter_triangle(float x, float s)
  363. {
  364. STBIR__UNUSED_PARAM(s);
  365. x = (float)fabs(x);
  366. if (x <= 1.0f)
  367. return 1 - x;
  368. else
  369. return 0;
  370. }
  371. static float stbir__filter_cubic(float x, float s)
  372. {
  373. STBIR__UNUSED_PARAM(s);
  374. x = (float)fabs(x);
  375. if (x < 1.0f)
  376. return (4 + x*x*(3*x - 6))/6;
  377. else if (x < 2.0f)
  378. return (8 + x*(-12 + x*(6 - x)))/6;
  379. return (0.0f);
  380. }
  381. static float stbir__filter_catmullrom(float x, float s)
  382. {
  383. STBIR__UNUSED_PARAM(s);
  384. x = (float)fabs(x);
  385. if (x < 1.0f)
  386. return 1 - x*x*(2.5f - 1.5f*x);
  387. else if (x < 2.0f)
  388. return 2 - x*(4 + x*(0.5f*x - 2.5f));
  389. return (0.0f);
  390. }
  391. static float stbir__filter_mitchell(float x, float s)
  392. {
  393. STBIR__UNUSED_PARAM(s);
  394. x = (float)fabs(x);
  395. if (x < 1.0f)
  396. return (16 + x*x*(21 * x - 36))/18;
  397. else if (x < 2.0f)
  398. return (32 + x*(-60 + x*(36 - 7*x)))/18;
  399. return (0.0f);
  400. }
  401. static float stbir__support_zero(float s)
  402. {
  403. STBIR__UNUSED_PARAM(s);
  404. return 0;
  405. }
  406. static float stbir__support_one(float s)
  407. {
  408. STBIR__UNUSED_PARAM(s);
  409. return 1;
  410. }
  411. static float stbir__support_two(float s)
  412. {
  413. STBIR__UNUSED_PARAM(s);
  414. return 2;
  415. }
  416. static stbir__filter_info stbir__filter_info_table[] = {
  417. { NULL, stbir__support_zero },
  418. { stbir__filter_trapezoid, stbir__support_trapezoid },
  419. { stbir__filter_triangle, stbir__support_one },
  420. { stbir__filter_cubic, stbir__support_two },
  421. { stbir__filter_catmullrom, stbir__support_two },
  422. { stbir__filter_mitchell, stbir__support_two },
  423. };
  424. stbir__inline static int stbir__use_upsampling(float ratio)
  425. {
  426. return ratio > 1;
  427. }
  428. stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info)
  429. {
  430. return stbir__use_upsampling(stbir_info->horizontal_scale);
  431. }
  432. stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info)
  433. {
  434. return stbir__use_upsampling(stbir_info->vertical_scale);
  435. }
  436. // This is the maximum number of input samples that can affect an output sample
  437. // with the given filter
  438. static int stbir__get_filter_pixel_width(stbir_filter filter, float scale)
  439. {
  440. STBIR_ASSERT(filter != 0);
  441. STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
  442. if (stbir__use_upsampling(scale))
  443. return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2);
  444. else
  445. return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale);
  446. }
  447. // This is how much to expand buffers to account for filters seeking outside
  448. // the image boundaries.
  449. static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale)
  450. {
  451. return stbir__get_filter_pixel_width(filter, scale) / 2;
  452. }
  453. static int stbir__get_coefficient_width(stbir_filter filter, float scale)
  454. {
  455. if (stbir__use_upsampling(scale))
  456. return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2);
  457. else
  458. return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2);
  459. }
  460. static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size)
  461. {
  462. if (stbir__use_upsampling(scale))
  463. return output_size;
  464. else
  465. return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2);
  466. }
  467. static int stbir__get_total_horizontal_coefficients(stbir__info* info)
  468. {
  469. return info->horizontal_num_contributors
  470. * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);
  471. }
  472. static int stbir__get_total_vertical_coefficients(stbir__info* info)
  473. {
  474. return info->vertical_num_contributors
  475. * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale);
  476. }
  477. static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n)
  478. {
  479. return &contributors[n];
  480. }
  481. // For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample,
  482. // if you change it here change it there too.
  483. static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c)
  484. {
  485. int width = stbir__get_coefficient_width(filter, scale);
  486. return &coefficients[width*n + c];
  487. }
  488. static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max)
  489. {
  490. switch (edge)
  491. {
  492. case STBIR_EDGE_ZERO:
  493. return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later
  494. case STBIR_EDGE_CLAMP:
  495. if (n < 0)
  496. return 0;
  497. if (n >= max)
  498. return max - 1;
  499. return n; // NOTREACHED
  500. case STBIR_EDGE_REFLECT:
  501. {
  502. if (n < 0)
  503. {
  504. if (n < max)
  505. return -n;
  506. else
  507. return max - 1;
  508. }
  509. if (n >= max)
  510. {
  511. int max2 = max * 2;
  512. if (n >= max2)
  513. return 0;
  514. else
  515. return max2 - n - 1;
  516. }
  517. return n; // NOTREACHED
  518. }
  519. case STBIR_EDGE_WRAP:
  520. if (n >= 0)
  521. return (n % max);
  522. else
  523. {
  524. int m = (-n) % max;
  525. if (m != 0)
  526. m = max - m;
  527. return (m);
  528. }
  529. // NOTREACHED
  530. default:
  531. STBIR_ASSERT(!"Unimplemented edge type");
  532. return 0;
  533. }
  534. }
  535. stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max)
  536. {
  537. // avoid per-pixel switch
  538. if (n >= 0 && n < max)
  539. return n;
  540. return stbir__edge_wrap_slow(edge, n, max);
  541. }
  542. // What input pixels contribute to this output pixel?
  543. static void stbir__calculate_sample_range_upsample(int n, float out_filter_radius, float scale_ratio, float out_shift, int* in_first_pixel, int* in_last_pixel, float* in_center_of_out)
  544. {
  545. float out_pixel_center = (float)n + 0.5f;
  546. float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius;
  547. float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius;
  548. float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio;
  549. float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio;
  550. *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio;
  551. *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5));
  552. *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5));
  553. }
  554. // What output pixels does this input pixel contribute to?
  555. static void stbir__calculate_sample_range_downsample(int n, float in_pixels_radius, float scale_ratio, float out_shift, int* out_first_pixel, int* out_last_pixel, float* out_center_of_in)
  556. {
  557. float in_pixel_center = (float)n + 0.5f;
  558. float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius;
  559. float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius;
  560. float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift;
  561. float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift;
  562. *out_center_of_in = in_pixel_center * scale_ratio - out_shift;
  563. *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5));
  564. *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5));
  565. }
  566. static void stbir__calculate_coefficients_upsample(stbir_filter filter, float scale, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group)
  567. {
  568. int i;
  569. float total_filter = 0;
  570. float filter_scale;
  571. STBIR_ASSERT(in_last_pixel - in_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical.
  572. contributor->n0 = in_first_pixel;
  573. contributor->n1 = in_last_pixel;
  574. STBIR_ASSERT(contributor->n1 >= contributor->n0);
  575. for (i = 0; i <= in_last_pixel - in_first_pixel; i++)
  576. {
  577. float in_pixel_center = (float)(i + in_first_pixel) + 0.5f;
  578. coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale);
  579. // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.)
  580. if (i == 0 && !coefficient_group[i])
  581. {
  582. contributor->n0 = ++in_first_pixel;
  583. i--;
  584. continue;
  585. }
  586. total_filter += coefficient_group[i];
  587. }
  588. // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.
  589. // It would be true in exact math but is at best approximately true in floating-point math,
  590. // and it would not make sense to try and put actual bounds on this here because it depends
  591. // on the image aspect ratio which can get pretty extreme.
  592. //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0);
  593. STBIR_ASSERT(total_filter > 0.9);
  594. STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off.
  595. // Make sure the sum of all coefficients is 1.
  596. filter_scale = 1 / total_filter;
  597. for (i = 0; i <= in_last_pixel - in_first_pixel; i++)
  598. coefficient_group[i] *= filter_scale;
  599. for (i = in_last_pixel - in_first_pixel; i >= 0; i--)
  600. {
  601. if (coefficient_group[i])
  602. break;
  603. // This line has no weight. We can skip it.
  604. contributor->n1 = contributor->n0 + i - 1;
  605. }
  606. }
  607. static void stbir__calculate_coefficients_downsample(stbir_filter filter, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group)
  608. {
  609. int i;
  610. STBIR_ASSERT(out_last_pixel - out_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(scale_ratio) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical.
  611. contributor->n0 = out_first_pixel;
  612. contributor->n1 = out_last_pixel;
  613. STBIR_ASSERT(contributor->n1 >= contributor->n0);
  614. for (i = 0; i <= out_last_pixel - out_first_pixel; i++)
  615. {
  616. float out_pixel_center = (float)(i + out_first_pixel) + 0.5f;
  617. float x = out_pixel_center - out_center_of_in;
  618. coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio;
  619. }
  620. // NOTE(fg): Not actually true in general, nor is there any reason to expect it should be.
  621. // It would be true in exact math but is at best approximately true in floating-point math,
  622. // and it would not make sense to try and put actual bounds on this here because it depends
  623. // on the image aspect ratio which can get pretty extreme.
  624. //STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0);
  625. for (i = out_last_pixel - out_first_pixel; i >= 0; i--)
  626. {
  627. if (coefficient_group[i])
  628. break;
  629. // This line has no weight. We can skip it.
  630. contributor->n1 = contributor->n0 + i - 1;
  631. }
  632. }
  633. static void stbir__normalize_downsample_coefficients(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, int input_size, int output_size)
  634. {
  635. int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);
  636. int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio);
  637. int i, j;
  638. int skip;
  639. for (i = 0; i < output_size; i++)
  640. {
  641. float scale;
  642. float total = 0;
  643. for (j = 0; j < num_contributors; j++)
  644. {
  645. if (i >= contributors[j].n0 && i <= contributors[j].n1)
  646. {
  647. float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0);
  648. total += coefficient;
  649. }
  650. else if (i < contributors[j].n0)
  651. break;
  652. }
  653. STBIR_ASSERT(total > 0.9f);
  654. STBIR_ASSERT(total < 1.1f);
  655. scale = 1 / total;
  656. for (j = 0; j < num_contributors; j++)
  657. {
  658. if (i >= contributors[j].n0 && i <= contributors[j].n1)
  659. *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale;
  660. else if (i < contributors[j].n0)
  661. break;
  662. }
  663. }
  664. // Optimize: Skip zero coefficients and contributions outside of image bounds.
  665. // Do this after normalizing because normalization depends on the n0/n1 values.
  666. for (j = 0; j < num_contributors; j++)
  667. {
  668. int range, max, width;
  669. skip = 0;
  670. while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0)
  671. skip++;
  672. contributors[j].n0 += skip;
  673. while (contributors[j].n0 < 0)
  674. {
  675. contributors[j].n0++;
  676. skip++;
  677. }
  678. range = contributors[j].n1 - contributors[j].n0 + 1;
  679. max = stbir__min(num_coefficients, range);
  680. width = stbir__get_coefficient_width(filter, scale_ratio);
  681. for (i = 0; i < max; i++)
  682. {
  683. if (i + skip >= width)
  684. break;
  685. *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip);
  686. }
  687. continue;
  688. }
  689. // Using min to avoid writing into invalid pixels.
  690. for (i = 0; i < num_contributors; i++)
  691. contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1);
  692. }
  693. // Each scan line uses the same kernel values so we should calculate the kernel
  694. // values once and then we can use them for every scan line.
  695. static void stbir__calculate_filters(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size)
  696. {
  697. int n;
  698. int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size);
  699. if (stbir__use_upsampling(scale_ratio))
  700. {
  701. float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio;
  702. // Looping through out pixels
  703. for (n = 0; n < total_contributors; n++)
  704. {
  705. float in_center_of_out; // Center of the current out pixel in the in pixel space
  706. int in_first_pixel, in_last_pixel;
  707. stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out);
  708. stbir__calculate_coefficients_upsample(filter, scale_ratio, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0));
  709. }
  710. }
  711. else
  712. {
  713. float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio;
  714. // Looping through in pixels
  715. for (n = 0; n < total_contributors; n++)
  716. {
  717. float out_center_of_in; // Center of the current out pixel in the in pixel space
  718. int out_first_pixel, out_last_pixel;
  719. int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio);
  720. stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in);
  721. stbir__calculate_coefficients_downsample(filter, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0));
  722. }
  723. stbir__normalize_downsample_coefficients(contributors, coefficients, filter, scale_ratio, input_size, output_size);
  724. }
  725. }
  726. static float* stbir__get_decode_buffer(stbir__info* stbir_info)
  727. {
  728. // The 0 index of the decode buffer starts after the margin. This makes
  729. // it okay to use negative indexes on the decode buffer.
  730. return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels];
  731. }
  732. #define STBIR__DECODE(type, colorspace) ((int)(type) * (STBIR_MAX_COLORSPACES) + (int)(colorspace))
  733. static void stbir__decode_scanline(stbir__info* stbir_info, int n)
  734. {
  735. int c;
  736. int channels = stbir_info->channels;
  737. int alpha_channel = stbir_info->alpha_channel;
  738. int type = stbir_info->type;
  739. int colorspace = stbir_info->colorspace;
  740. int input_w = stbir_info->input_w;
  741. size_t input_stride_bytes = stbir_info->input_stride_bytes;
  742. float* decode_buffer = stbir__get_decode_buffer(stbir_info);
  743. stbir_edge edge_horizontal = stbir_info->edge_horizontal;
  744. stbir_edge edge_vertical = stbir_info->edge_vertical;
  745. size_t in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes;
  746. const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset;
  747. int max_x = input_w + stbir_info->horizontal_filter_pixel_margin;
  748. int decode = STBIR__DECODE(type, colorspace);
  749. int x = -stbir_info->horizontal_filter_pixel_margin;
  750. // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input,
  751. // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO
  752. if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h))
  753. {
  754. for (; x < max_x; x++)
  755. for (c = 0; c < channels; c++)
  756. decode_buffer[x*channels + c] = 0;
  757. return;
  758. }
  759. switch (decode)
  760. {
  761. case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):
  762. for (; x < max_x; x++)
  763. {
  764. int decode_pixel_index = x * channels;
  765. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  766. for (c = 0; c < channels; c++)
  767. decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / stbir__max_uint8_as_float;
  768. }
  769. break;
  770. case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):
  771. for (; x < max_x; x++)
  772. {
  773. int decode_pixel_index = x * channels;
  774. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  775. for (c = 0; c < channels; c++)
  776. decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]];
  777. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  778. decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint8_as_float;
  779. }
  780. break;
  781. case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):
  782. for (; x < max_x; x++)
  783. {
  784. int decode_pixel_index = x * channels;
  785. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  786. for (c = 0; c < channels; c++)
  787. decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float;
  788. }
  789. break;
  790. case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):
  791. for (; x < max_x; x++)
  792. {
  793. int decode_pixel_index = x * channels;
  794. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  795. for (c = 0; c < channels; c++)
  796. decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float);
  797. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  798. decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint16_as_float;
  799. }
  800. break;
  801. case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):
  802. for (; x < max_x; x++)
  803. {
  804. int decode_pixel_index = x * channels;
  805. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  806. for (c = 0; c < channels; c++)
  807. decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float);
  808. }
  809. break;
  810. case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):
  811. for (; x < max_x; x++)
  812. {
  813. int decode_pixel_index = x * channels;
  814. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  815. for (c = 0; c < channels; c++)
  816. decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float));
  817. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  818. decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint32_as_float);
  819. }
  820. break;
  821. case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):
  822. for (; x < max_x; x++)
  823. {
  824. int decode_pixel_index = x * channels;
  825. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  826. for (c = 0; c < channels; c++)
  827. decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c];
  828. }
  829. break;
  830. case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):
  831. for (; x < max_x; x++)
  832. {
  833. int decode_pixel_index = x * channels;
  834. int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels;
  835. for (c = 0; c < channels; c++)
  836. decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]);
  837. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  838. decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel];
  839. }
  840. break;
  841. default:
  842. STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");
  843. break;
  844. }
  845. if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED))
  846. {
  847. for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++)
  848. {
  849. int decode_pixel_index = x * channels;
  850. // If the alpha value is 0 it will clobber the color values. Make sure it's not.
  851. float alpha = decode_buffer[decode_pixel_index + alpha_channel];
  852. #ifndef STBIR_NO_ALPHA_EPSILON
  853. if (stbir_info->type != STBIR_TYPE_FLOAT) {
  854. alpha += STBIR_ALPHA_EPSILON;
  855. decode_buffer[decode_pixel_index + alpha_channel] = alpha;
  856. }
  857. #endif
  858. for (c = 0; c < channels; c++)
  859. {
  860. if (c == alpha_channel)
  861. continue;
  862. decode_buffer[decode_pixel_index + c] *= alpha;
  863. }
  864. }
  865. }
  866. if (edge_horizontal == STBIR_EDGE_ZERO)
  867. {
  868. for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++)
  869. {
  870. for (c = 0; c < channels; c++)
  871. decode_buffer[x*channels + c] = 0;
  872. }
  873. for (x = input_w; x < max_x; x++)
  874. {
  875. for (c = 0; c < channels; c++)
  876. decode_buffer[x*channels + c] = 0;
  877. }
  878. }
  879. }
  880. static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length)
  881. {
  882. return &ring_buffer[index * ring_buffer_length];
  883. }
  884. static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n)
  885. {
  886. int ring_buffer_index;
  887. float* ring_buffer;
  888. stbir_info->ring_buffer_last_scanline = n;
  889. if (stbir_info->ring_buffer_begin_index < 0)
  890. {
  891. ring_buffer_index = stbir_info->ring_buffer_begin_index = 0;
  892. stbir_info->ring_buffer_first_scanline = n;
  893. }
  894. else
  895. {
  896. ring_buffer_index = (stbir_info->ring_buffer_begin_index + (stbir_info->ring_buffer_last_scanline - stbir_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries;
  897. STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index);
  898. }
  899. ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float));
  900. memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes);
  901. return ring_buffer;
  902. }
  903. static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, float* output_buffer)
  904. {
  905. int x, k;
  906. int output_w = stbir_info->output_w;
  907. int channels = stbir_info->channels;
  908. float* decode_buffer = stbir__get_decode_buffer(stbir_info);
  909. stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;
  910. float* horizontal_coefficients = stbir_info->horizontal_coefficients;
  911. int coefficient_width = stbir_info->horizontal_coefficient_width;
  912. for (x = 0; x < output_w; x++)
  913. {
  914. int n0 = horizontal_contributors[x].n0;
  915. int n1 = horizontal_contributors[x].n1;
  916. int out_pixel_index = x * channels;
  917. int coefficient_group = coefficient_width * x;
  918. int coefficient_counter = 0;
  919. STBIR_ASSERT(n1 >= n0);
  920. STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin);
  921. STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin);
  922. STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);
  923. STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin);
  924. switch (channels) {
  925. case 1:
  926. for (k = n0; k <= n1; k++)
  927. {
  928. int in_pixel_index = k * 1;
  929. float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];
  930. STBIR_ASSERT(coefficient != 0);
  931. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  932. }
  933. break;
  934. case 2:
  935. for (k = n0; k <= n1; k++)
  936. {
  937. int in_pixel_index = k * 2;
  938. float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];
  939. STBIR_ASSERT(coefficient != 0);
  940. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  941. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  942. }
  943. break;
  944. case 3:
  945. for (k = n0; k <= n1; k++)
  946. {
  947. int in_pixel_index = k * 3;
  948. float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];
  949. STBIR_ASSERT(coefficient != 0);
  950. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  951. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  952. output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;
  953. }
  954. break;
  955. case 4:
  956. for (k = n0; k <= n1; k++)
  957. {
  958. int in_pixel_index = k * 4;
  959. float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];
  960. STBIR_ASSERT(coefficient != 0);
  961. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  962. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  963. output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;
  964. output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;
  965. }
  966. break;
  967. default:
  968. for (k = n0; k <= n1; k++)
  969. {
  970. int in_pixel_index = k * channels;
  971. float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++];
  972. int c;
  973. STBIR_ASSERT(coefficient != 0);
  974. for (c = 0; c < channels; c++)
  975. output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;
  976. }
  977. break;
  978. }
  979. }
  980. }
  981. static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, float* output_buffer)
  982. {
  983. int x, k;
  984. int input_w = stbir_info->input_w;
  985. int channels = stbir_info->channels;
  986. float* decode_buffer = stbir__get_decode_buffer(stbir_info);
  987. stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors;
  988. float* horizontal_coefficients = stbir_info->horizontal_coefficients;
  989. int coefficient_width = stbir_info->horizontal_coefficient_width;
  990. int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin;
  991. int max_x = input_w + filter_pixel_margin * 2;
  992. STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info));
  993. switch (channels) {
  994. case 1:
  995. for (x = 0; x < max_x; x++)
  996. {
  997. int n0 = horizontal_contributors[x].n0;
  998. int n1 = horizontal_contributors[x].n1;
  999. int in_x = x - filter_pixel_margin;
  1000. int in_pixel_index = in_x * 1;
  1001. int max_n = n1;
  1002. int coefficient_group = coefficient_width * x;
  1003. for (k = n0; k <= max_n; k++)
  1004. {
  1005. int out_pixel_index = k * 1;
  1006. float coefficient = horizontal_coefficients[coefficient_group + k - n0];
  1007. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  1008. }
  1009. }
  1010. break;
  1011. case 2:
  1012. for (x = 0; x < max_x; x++)
  1013. {
  1014. int n0 = horizontal_contributors[x].n0;
  1015. int n1 = horizontal_contributors[x].n1;
  1016. int in_x = x - filter_pixel_margin;
  1017. int in_pixel_index = in_x * 2;
  1018. int max_n = n1;
  1019. int coefficient_group = coefficient_width * x;
  1020. for (k = n0; k <= max_n; k++)
  1021. {
  1022. int out_pixel_index = k * 2;
  1023. float coefficient = horizontal_coefficients[coefficient_group + k - n0];
  1024. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  1025. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  1026. }
  1027. }
  1028. break;
  1029. case 3:
  1030. for (x = 0; x < max_x; x++)
  1031. {
  1032. int n0 = horizontal_contributors[x].n0;
  1033. int n1 = horizontal_contributors[x].n1;
  1034. int in_x = x - filter_pixel_margin;
  1035. int in_pixel_index = in_x * 3;
  1036. int max_n = n1;
  1037. int coefficient_group = coefficient_width * x;
  1038. for (k = n0; k <= max_n; k++)
  1039. {
  1040. int out_pixel_index = k * 3;
  1041. float coefficient = horizontal_coefficients[coefficient_group + k - n0];
  1042. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  1043. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  1044. output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;
  1045. }
  1046. }
  1047. break;
  1048. case 4:
  1049. for (x = 0; x < max_x; x++)
  1050. {
  1051. int n0 = horizontal_contributors[x].n0;
  1052. int n1 = horizontal_contributors[x].n1;
  1053. int in_x = x - filter_pixel_margin;
  1054. int in_pixel_index = in_x * 4;
  1055. int max_n = n1;
  1056. int coefficient_group = coefficient_width * x;
  1057. for (k = n0; k <= max_n; k++)
  1058. {
  1059. int out_pixel_index = k * 4;
  1060. float coefficient = horizontal_coefficients[coefficient_group + k - n0];
  1061. output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient;
  1062. output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient;
  1063. output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient;
  1064. output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient;
  1065. }
  1066. }
  1067. break;
  1068. default:
  1069. for (x = 0; x < max_x; x++)
  1070. {
  1071. int n0 = horizontal_contributors[x].n0;
  1072. int n1 = horizontal_contributors[x].n1;
  1073. int in_x = x - filter_pixel_margin;
  1074. int in_pixel_index = in_x * channels;
  1075. int max_n = n1;
  1076. int coefficient_group = coefficient_width * x;
  1077. for (k = n0; k <= max_n; k++)
  1078. {
  1079. int c;
  1080. int out_pixel_index = k * channels;
  1081. float coefficient = horizontal_coefficients[coefficient_group + k - n0];
  1082. for (c = 0; c < channels; c++)
  1083. output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient;
  1084. }
  1085. }
  1086. break;
  1087. }
  1088. }
  1089. static void stbir__decode_and_resample_upsample(stbir__info* stbir_info, int n)
  1090. {
  1091. // Decode the nth scanline from the source image into the decode buffer.
  1092. stbir__decode_scanline(stbir_info, n);
  1093. // Now resample it into the ring buffer.
  1094. if (stbir__use_width_upsampling(stbir_info))
  1095. stbir__resample_horizontal_upsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));
  1096. else
  1097. stbir__resample_horizontal_downsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n));
  1098. // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling.
  1099. }
  1100. static void stbir__decode_and_resample_downsample(stbir__info* stbir_info, int n)
  1101. {
  1102. // Decode the nth scanline from the source image into the decode buffer.
  1103. stbir__decode_scanline(stbir_info, n);
  1104. memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float));
  1105. // Now resample it into the horizontal buffer.
  1106. if (stbir__use_width_upsampling(stbir_info))
  1107. stbir__resample_horizontal_upsample(stbir_info, stbir_info->horizontal_buffer);
  1108. else
  1109. stbir__resample_horizontal_downsample(stbir_info, stbir_info->horizontal_buffer);
  1110. // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers.
  1111. }
  1112. // Get the specified scan line from the ring buffer.
  1113. static float* stbir__get_ring_buffer_scanline(int get_scanline, float* ring_buffer, int begin_index, int first_scanline, int ring_buffer_num_entries, int ring_buffer_length)
  1114. {
  1115. int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_num_entries;
  1116. return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length);
  1117. }
  1118. static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode)
  1119. {
  1120. int x;
  1121. int n;
  1122. int num_nonalpha;
  1123. stbir_uint16 nonalpha[STBIR_MAX_CHANNELS];
  1124. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED))
  1125. {
  1126. for (x=0; x < num_pixels; ++x)
  1127. {
  1128. int pixel_index = x*channels;
  1129. float alpha = encode_buffer[pixel_index + alpha_channel];
  1130. float reciprocal_alpha = alpha ? 1.0f / alpha : 0;
  1131. // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb
  1132. for (n = 0; n < channels; n++)
  1133. if (n != alpha_channel)
  1134. encode_buffer[pixel_index + n] *= reciprocal_alpha;
  1135. // We added in a small epsilon to prevent the color channel from being deleted with zero alpha.
  1136. // Because we only add it for integer types, it will automatically be discarded on integer
  1137. // conversion, so we don't need to subtract it back out (which would be problematic for
  1138. // numeric precision reasons).
  1139. }
  1140. }
  1141. // build a table of all channels that need colorspace correction, so
  1142. // we don't perform colorspace correction on channels that don't need it.
  1143. for (x = 0, num_nonalpha = 0; x < channels; ++x)
  1144. {
  1145. if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))
  1146. {
  1147. nonalpha[num_nonalpha++] = (stbir_uint16)x;
  1148. }
  1149. }
  1150. #define STBIR__ROUND_INT(f) ((int) ((f)+0.5))
  1151. #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5))
  1152. #ifdef STBIR__SATURATE_INT
  1153. #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * stbir__max_uint8_as_float ))
  1154. #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * stbir__max_uint16_as_float))
  1155. #else
  1156. #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint8_as_float )
  1157. #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint16_as_float)
  1158. #endif
  1159. switch (decode)
  1160. {
  1161. case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR):
  1162. for (x=0; x < num_pixels; ++x)
  1163. {
  1164. int pixel_index = x*channels;
  1165. for (n = 0; n < channels; n++)
  1166. {
  1167. int index = pixel_index + n;
  1168. ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]);
  1169. }
  1170. }
  1171. break;
  1172. case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB):
  1173. for (x=0; x < num_pixels; ++x)
  1174. {
  1175. int pixel_index = x*channels;
  1176. for (n = 0; n < num_nonalpha; n++)
  1177. {
  1178. int index = pixel_index + nonalpha[n];
  1179. ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]);
  1180. }
  1181. if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE))
  1182. ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]);
  1183. }
  1184. break;
  1185. case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR):
  1186. for (x=0; x < num_pixels; ++x)
  1187. {
  1188. int pixel_index = x*channels;
  1189. for (n = 0; n < channels; n++)
  1190. {
  1191. int index = pixel_index + n;
  1192. ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]);
  1193. }
  1194. }
  1195. break;
  1196. case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB):
  1197. for (x=0; x < num_pixels; ++x)
  1198. {
  1199. int pixel_index = x*channels;
  1200. for (n = 0; n < num_nonalpha; n++)
  1201. {
  1202. int index = pixel_index + nonalpha[n];
  1203. ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * stbir__max_uint16_as_float);
  1204. }
  1205. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  1206. ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]);
  1207. }
  1208. break;
  1209. case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR):
  1210. for (x=0; x < num_pixels; ++x)
  1211. {
  1212. int pixel_index = x*channels;
  1213. for (n = 0; n < channels; n++)
  1214. {
  1215. int index = pixel_index + n;
  1216. ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * stbir__max_uint32_as_float);
  1217. }
  1218. }
  1219. break;
  1220. case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB):
  1221. for (x=0; x < num_pixels; ++x)
  1222. {
  1223. int pixel_index = x*channels;
  1224. for (n = 0; n < num_nonalpha; n++)
  1225. {
  1226. int index = pixel_index + nonalpha[n];
  1227. ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[index]))) * stbir__max_uint32_as_float);
  1228. }
  1229. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  1230. ((unsigned int*)output_buffer)[pixel_index + alpha_channel] = (unsigned int)STBIR__ROUND_INT(((double)stbir__saturate(encode_buffer[pixel_index + alpha_channel])) * stbir__max_uint32_as_float);
  1231. }
  1232. break;
  1233. case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR):
  1234. for (x=0; x < num_pixels; ++x)
  1235. {
  1236. int pixel_index = x*channels;
  1237. for (n = 0; n < channels; n++)
  1238. {
  1239. int index = pixel_index + n;
  1240. ((float*)output_buffer)[index] = encode_buffer[index];
  1241. }
  1242. }
  1243. break;
  1244. case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB):
  1245. for (x=0; x < num_pixels; ++x)
  1246. {
  1247. int pixel_index = x*channels;
  1248. for (n = 0; n < num_nonalpha; n++)
  1249. {
  1250. int index = pixel_index + nonalpha[n];
  1251. ((float*)output_buffer)[index] = stbir__linear_to_srgb(encode_buffer[index]);
  1252. }
  1253. if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE))
  1254. ((float*)output_buffer)[pixel_index + alpha_channel] = encode_buffer[pixel_index + alpha_channel];
  1255. }
  1256. break;
  1257. default:
  1258. STBIR_ASSERT(!"Unknown type/colorspace/channels combination.");
  1259. break;
  1260. }
  1261. }
  1262. static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n)
  1263. {
  1264. int x, k;
  1265. int output_w = stbir_info->output_w;
  1266. stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;
  1267. float* vertical_coefficients = stbir_info->vertical_coefficients;
  1268. int channels = stbir_info->channels;
  1269. int alpha_channel = stbir_info->alpha_channel;
  1270. int type = stbir_info->type;
  1271. int colorspace = stbir_info->colorspace;
  1272. int ring_buffer_entries = stbir_info->ring_buffer_num_entries;
  1273. void* output_data = stbir_info->output_data;
  1274. float* encode_buffer = stbir_info->encode_buffer;
  1275. int decode = STBIR__DECODE(type, colorspace);
  1276. int coefficient_width = stbir_info->vertical_coefficient_width;
  1277. int coefficient_counter;
  1278. int contributor = n;
  1279. float* ring_buffer = stbir_info->ring_buffer;
  1280. int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;
  1281. int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;
  1282. int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);
  1283. int n0,n1, output_row_start;
  1284. int coefficient_group = coefficient_width * contributor;
  1285. n0 = vertical_contributors[contributor].n0;
  1286. n1 = vertical_contributors[contributor].n1;
  1287. output_row_start = n * stbir_info->output_stride_bytes;
  1288. STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));
  1289. memset(encode_buffer, 0, output_w * sizeof(float) * channels);
  1290. // I tried reblocking this for better cache usage of encode_buffer
  1291. // (using x_outer, k, x_inner), but it lost speed. -- stb
  1292. coefficient_counter = 0;
  1293. switch (channels) {
  1294. case 1:
  1295. for (k = n0; k <= n1; k++)
  1296. {
  1297. int coefficient_index = coefficient_counter++;
  1298. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1299. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1300. for (x = 0; x < output_w; ++x)
  1301. {
  1302. int in_pixel_index = x * 1;
  1303. encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;
  1304. }
  1305. }
  1306. break;
  1307. case 2:
  1308. for (k = n0; k <= n1; k++)
  1309. {
  1310. int coefficient_index = coefficient_counter++;
  1311. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1312. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1313. for (x = 0; x < output_w; ++x)
  1314. {
  1315. int in_pixel_index = x * 2;
  1316. encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;
  1317. encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;
  1318. }
  1319. }
  1320. break;
  1321. case 3:
  1322. for (k = n0; k <= n1; k++)
  1323. {
  1324. int coefficient_index = coefficient_counter++;
  1325. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1326. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1327. for (x = 0; x < output_w; ++x)
  1328. {
  1329. int in_pixel_index = x * 3;
  1330. encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;
  1331. encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;
  1332. encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;
  1333. }
  1334. }
  1335. break;
  1336. case 4:
  1337. for (k = n0; k <= n1; k++)
  1338. {
  1339. int coefficient_index = coefficient_counter++;
  1340. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1341. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1342. for (x = 0; x < output_w; ++x)
  1343. {
  1344. int in_pixel_index = x * 4;
  1345. encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient;
  1346. encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient;
  1347. encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient;
  1348. encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient;
  1349. }
  1350. }
  1351. break;
  1352. default:
  1353. for (k = n0; k <= n1; k++)
  1354. {
  1355. int coefficient_index = coefficient_counter++;
  1356. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1357. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1358. for (x = 0; x < output_w; ++x)
  1359. {
  1360. int in_pixel_index = x * channels;
  1361. int c;
  1362. for (c = 0; c < channels; c++)
  1363. encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient;
  1364. }
  1365. }
  1366. break;
  1367. }
  1368. stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode);
  1369. }
  1370. static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n)
  1371. {
  1372. int x, k;
  1373. int output_w = stbir_info->output_w;
  1374. stbir__contributors* vertical_contributors = stbir_info->vertical_contributors;
  1375. float* vertical_coefficients = stbir_info->vertical_coefficients;
  1376. int channels = stbir_info->channels;
  1377. int ring_buffer_entries = stbir_info->ring_buffer_num_entries;
  1378. float* horizontal_buffer = stbir_info->horizontal_buffer;
  1379. int coefficient_width = stbir_info->vertical_coefficient_width;
  1380. int contributor = n + stbir_info->vertical_filter_pixel_margin;
  1381. float* ring_buffer = stbir_info->ring_buffer;
  1382. int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index;
  1383. int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline;
  1384. int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);
  1385. int n0,n1;
  1386. n0 = vertical_contributors[contributor].n0;
  1387. n1 = vertical_contributors[contributor].n1;
  1388. STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));
  1389. for (k = n0; k <= n1; k++)
  1390. {
  1391. int coefficient_index = k - n0;
  1392. int coefficient_group = coefficient_width * contributor;
  1393. float coefficient = vertical_coefficients[coefficient_group + coefficient_index];
  1394. float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length);
  1395. switch (channels) {
  1396. case 1:
  1397. for (x = 0; x < output_w; x++)
  1398. {
  1399. int in_pixel_index = x * 1;
  1400. ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;
  1401. }
  1402. break;
  1403. case 2:
  1404. for (x = 0; x < output_w; x++)
  1405. {
  1406. int in_pixel_index = x * 2;
  1407. ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;
  1408. ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;
  1409. }
  1410. break;
  1411. case 3:
  1412. for (x = 0; x < output_w; x++)
  1413. {
  1414. int in_pixel_index = x * 3;
  1415. ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;
  1416. ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;
  1417. ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;
  1418. }
  1419. break;
  1420. case 4:
  1421. for (x = 0; x < output_w; x++)
  1422. {
  1423. int in_pixel_index = x * 4;
  1424. ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient;
  1425. ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient;
  1426. ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient;
  1427. ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient;
  1428. }
  1429. break;
  1430. default:
  1431. for (x = 0; x < output_w; x++)
  1432. {
  1433. int in_pixel_index = x * channels;
  1434. int c;
  1435. for (c = 0; c < channels; c++)
  1436. ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient;
  1437. }
  1438. break;
  1439. }
  1440. }
  1441. }
  1442. static void stbir__buffer_loop_upsample(stbir__info* stbir_info)
  1443. {
  1444. int y;
  1445. float scale_ratio = stbir_info->vertical_scale;
  1446. float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio;
  1447. STBIR_ASSERT(stbir__use_height_upsampling(stbir_info));
  1448. for (y = 0; y < stbir_info->output_h; y++)
  1449. {
  1450. float in_center_of_out = 0; // Center of the current out scanline in the in scanline space
  1451. int in_first_scanline = 0, in_last_scanline = 0;
  1452. stbir__calculate_sample_range_upsample(y, out_scanlines_radius, scale_ratio, stbir_info->vertical_shift, &in_first_scanline, &in_last_scanline, &in_center_of_out);
  1453. STBIR_ASSERT(in_last_scanline - in_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);
  1454. if (stbir_info->ring_buffer_begin_index >= 0)
  1455. {
  1456. // Get rid of whatever we don't need anymore.
  1457. while (in_first_scanline > stbir_info->ring_buffer_first_scanline)
  1458. {
  1459. if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)
  1460. {
  1461. // We just popped the last scanline off the ring buffer.
  1462. // Reset it to the empty state.
  1463. stbir_info->ring_buffer_begin_index = -1;
  1464. stbir_info->ring_buffer_first_scanline = 0;
  1465. stbir_info->ring_buffer_last_scanline = 0;
  1466. break;
  1467. }
  1468. else
  1469. {
  1470. stbir_info->ring_buffer_first_scanline++;
  1471. stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;
  1472. }
  1473. }
  1474. }
  1475. // Load in new ones.
  1476. if (stbir_info->ring_buffer_begin_index < 0)
  1477. stbir__decode_and_resample_upsample(stbir_info, in_first_scanline);
  1478. while (in_last_scanline > stbir_info->ring_buffer_last_scanline)
  1479. stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1);
  1480. // Now all buffers should be ready to write a row of vertical sampling.
  1481. stbir__resample_vertical_upsample(stbir_info, y);
  1482. STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h);
  1483. }
  1484. }
  1485. static void stbir__empty_ring_buffer(stbir__info* stbir_info, int first_necessary_scanline)
  1486. {
  1487. int output_stride_bytes = stbir_info->output_stride_bytes;
  1488. int channels = stbir_info->channels;
  1489. int alpha_channel = stbir_info->alpha_channel;
  1490. int type = stbir_info->type;
  1491. int colorspace = stbir_info->colorspace;
  1492. int output_w = stbir_info->output_w;
  1493. void* output_data = stbir_info->output_data;
  1494. int decode = STBIR__DECODE(type, colorspace);
  1495. float* ring_buffer = stbir_info->ring_buffer;
  1496. int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float);
  1497. if (stbir_info->ring_buffer_begin_index >= 0)
  1498. {
  1499. // Get rid of whatever we don't need anymore.
  1500. while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline)
  1501. {
  1502. if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h)
  1503. {
  1504. int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes;
  1505. float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length);
  1506. stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode);
  1507. STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h);
  1508. }
  1509. if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline)
  1510. {
  1511. // We just popped the last scanline off the ring buffer.
  1512. // Reset it to the empty state.
  1513. stbir_info->ring_buffer_begin_index = -1;
  1514. stbir_info->ring_buffer_first_scanline = 0;
  1515. stbir_info->ring_buffer_last_scanline = 0;
  1516. break;
  1517. }
  1518. else
  1519. {
  1520. stbir_info->ring_buffer_first_scanline++;
  1521. stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries;
  1522. }
  1523. }
  1524. }
  1525. }
  1526. static void stbir__buffer_loop_downsample(stbir__info* stbir_info)
  1527. {
  1528. int y;
  1529. float scale_ratio = stbir_info->vertical_scale;
  1530. int output_h = stbir_info->output_h;
  1531. float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio;
  1532. int pixel_margin = stbir_info->vertical_filter_pixel_margin;
  1533. int max_y = stbir_info->input_h + pixel_margin;
  1534. STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info));
  1535. for (y = -pixel_margin; y < max_y; y++)
  1536. {
  1537. float out_center_of_in; // Center of the current out scanline in the in scanline space
  1538. int out_first_scanline, out_last_scanline;
  1539. stbir__calculate_sample_range_downsample(y, in_pixels_radius, scale_ratio, stbir_info->vertical_shift, &out_first_scanline, &out_last_scanline, &out_center_of_in);
  1540. STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries);
  1541. if (out_last_scanline < 0 || out_first_scanline >= output_h)
  1542. continue;
  1543. stbir__empty_ring_buffer(stbir_info, out_first_scanline);
  1544. stbir__decode_and_resample_downsample(stbir_info, y);
  1545. // Load in new ones.
  1546. if (stbir_info->ring_buffer_begin_index < 0)
  1547. stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline);
  1548. while (out_last_scanline > stbir_info->ring_buffer_last_scanline)
  1549. stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1);
  1550. // Now the horizontal buffer is ready to write to all ring buffer rows.
  1551. stbir__resample_vertical_downsample(stbir_info, y);
  1552. }
  1553. stbir__empty_ring_buffer(stbir_info, stbir_info->output_h);
  1554. }
  1555. static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels)
  1556. {
  1557. info->input_w = input_w;
  1558. info->input_h = input_h;
  1559. info->output_w = output_w;
  1560. info->output_h = output_h;
  1561. info->channels = channels;
  1562. }
  1563. static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform)
  1564. {
  1565. info->s0 = s0;
  1566. info->t0 = t0;
  1567. info->s1 = s1;
  1568. info->t1 = t1;
  1569. if (transform)
  1570. {
  1571. info->horizontal_scale = transform[0];
  1572. info->vertical_scale = transform[1];
  1573. info->horizontal_shift = transform[2];
  1574. info->vertical_shift = transform[3];
  1575. }
  1576. else
  1577. {
  1578. info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0);
  1579. info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0);
  1580. info->horizontal_shift = s0 * info->output_w / (s1 - s0);
  1581. info->vertical_shift = t0 * info->output_h / (t1 - t0);
  1582. }
  1583. }
  1584. static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter)
  1585. {
  1586. if (h_filter == 0)
  1587. h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;
  1588. if (v_filter == 0)
  1589. v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE;
  1590. info->horizontal_filter = h_filter;
  1591. info->vertical_filter = v_filter;
  1592. }
  1593. static stbir_uint32 stbir__calculate_memory(stbir__info *info)
  1594. {
  1595. int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);
  1596. int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale);
  1597. info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w);
  1598. info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h);
  1599. // One extra entry because floating point precision problems sometimes cause an extra to be necessary.
  1600. info->ring_buffer_num_entries = filter_height + 1;
  1601. info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors);
  1602. info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float);
  1603. info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors);
  1604. info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float);
  1605. info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float);
  1606. info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float);
  1607. info->ring_buffer_size = info->output_w * info->channels * info->ring_buffer_num_entries * sizeof(float);
  1608. info->encode_buffer_size = info->output_w * info->channels * sizeof(float);
  1609. STBIR_ASSERT(info->horizontal_filter != 0);
  1610. STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late
  1611. STBIR_ASSERT(info->vertical_filter != 0);
  1612. STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late
  1613. if (stbir__use_height_upsampling(info))
  1614. // The horizontal buffer is for when we're downsampling the height and we
  1615. // can't output the result of sampling the decode buffer directly into the
  1616. // ring buffers.
  1617. info->horizontal_buffer_size = 0;
  1618. else
  1619. // The encode buffer is to retain precision in the height upsampling method
  1620. // and isn't used when height downsampling.
  1621. info->encode_buffer_size = 0;
  1622. return info->horizontal_contributors_size + info->horizontal_coefficients_size
  1623. + info->vertical_contributors_size + info->vertical_coefficients_size
  1624. + info->decode_buffer_size + info->horizontal_buffer_size
  1625. + info->ring_buffer_size + info->encode_buffer_size;
  1626. }
  1627. static int stbir__resize_allocated(stbir__info *info,
  1628. const void* input_data, int input_stride_in_bytes,
  1629. void* output_data, int output_stride_in_bytes,
  1630. int alpha_channel, stbir_uint32 flags, stbir_datatype type,
  1631. stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace,
  1632. void* tempmem, size_t tempmem_size_in_bytes)
  1633. {
  1634. size_t memory_required = stbir__calculate_memory(info);
  1635. int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type];
  1636. int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type];
  1637. #ifdef STBIR_DEBUG_OVERWRITE_TEST
  1638. #define OVERWRITE_ARRAY_SIZE 8
  1639. unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE];
  1640. unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE];
  1641. unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE];
  1642. unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE];
  1643. size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type];
  1644. memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);
  1645. memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE);
  1646. memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE);
  1647. memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE);
  1648. #endif
  1649. STBIR_ASSERT(info->channels >= 0);
  1650. STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS);
  1651. if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS)
  1652. return 0;
  1653. STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
  1654. STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table));
  1655. if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))
  1656. return 0;
  1657. if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table))
  1658. return 0;
  1659. if (alpha_channel < 0)
  1660. flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED;
  1661. if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) {
  1662. STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels);
  1663. }
  1664. if (alpha_channel >= info->channels)
  1665. return 0;
  1666. STBIR_ASSERT(tempmem);
  1667. if (!tempmem)
  1668. return 0;
  1669. STBIR_ASSERT(tempmem_size_in_bytes >= memory_required);
  1670. if (tempmem_size_in_bytes < memory_required)
  1671. return 0;
  1672. memset(tempmem, 0, tempmem_size_in_bytes);
  1673. info->input_data = input_data;
  1674. info->input_stride_bytes = width_stride_input;
  1675. info->output_data = output_data;
  1676. info->output_stride_bytes = width_stride_output;
  1677. info->alpha_channel = alpha_channel;
  1678. info->flags = flags;
  1679. info->type = type;
  1680. info->edge_horizontal = edge_horizontal;
  1681. info->edge_vertical = edge_vertical;
  1682. info->colorspace = colorspace;
  1683. info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale);
  1684. info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale );
  1685. info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale);
  1686. info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale );
  1687. info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale);
  1688. info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale );
  1689. info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float);
  1690. info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2;
  1691. #define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size)
  1692. info->horizontal_contributors = (stbir__contributors *) tempmem;
  1693. info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float);
  1694. info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors);
  1695. info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float);
  1696. info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float);
  1697. if (stbir__use_height_upsampling(info))
  1698. {
  1699. info->horizontal_buffer = NULL;
  1700. info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);
  1701. info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float);
  1702. STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);
  1703. }
  1704. else
  1705. {
  1706. info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float);
  1707. info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float);
  1708. info->encode_buffer = NULL;
  1709. STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes);
  1710. }
  1711. #undef STBIR__NEXT_MEMPTR
  1712. // This signals that the ring buffer is empty
  1713. info->ring_buffer_begin_index = -1;
  1714. stbir__calculate_filters(info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w);
  1715. stbir__calculate_filters(info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h);
  1716. STBIR_PROGRESS_REPORT(0);
  1717. if (stbir__use_height_upsampling(info))
  1718. stbir__buffer_loop_upsample(info);
  1719. else
  1720. stbir__buffer_loop_downsample(info);
  1721. STBIR_PROGRESS_REPORT(1);
  1722. #ifdef STBIR_DEBUG_OVERWRITE_TEST
  1723. STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);
  1724. STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0);
  1725. STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0);
  1726. STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0);
  1727. #endif
  1728. return 1;
  1729. }
  1730. static int stbir__resize_arbitrary(
  1731. void *alloc_context,
  1732. const void* input_data, int input_w, int input_h, int input_stride_in_bytes,
  1733. void* output_data, int output_w, int output_h, int output_stride_in_bytes,
  1734. float s0, float t0, float s1, float t1, float *transform,
  1735. int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type,
  1736. stbir_filter h_filter, stbir_filter v_filter,
  1737. stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace)
  1738. {
  1739. stbir__info info;
  1740. int result;
  1741. size_t memory_required;
  1742. void* extra_memory;
  1743. stbir__setup(&info, input_w, input_h, output_w, output_h, channels);
  1744. stbir__calculate_transform(&info, s0,t0,s1,t1,transform);
  1745. stbir__choose_filter(&info, h_filter, v_filter);
  1746. memory_required = stbir__calculate_memory(&info);
  1747. extra_memory = STBIR_MALLOC(memory_required, alloc_context);
  1748. if (!extra_memory)
  1749. return 0;
  1750. result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes,
  1751. output_data, output_stride_in_bytes,
  1752. alpha_channel, flags, type,
  1753. edge_horizontal, edge_vertical,
  1754. colorspace, extra_memory, memory_required);
  1755. STBIR_FREE(extra_memory, alloc_context);
  1756. return result;
  1757. }
  1758. STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1759. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1760. int num_channels)
  1761. {
  1762. return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,
  1763. output_pixels, output_w, output_h, output_stride_in_bytes,
  1764. 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,
  1765. STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);
  1766. }
  1767. STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1768. float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1769. int num_channels)
  1770. {
  1771. return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,
  1772. output_pixels, output_w, output_h, output_stride_in_bytes,
  1773. 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,
  1774. STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR);
  1775. }
  1776. STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1777. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1778. int num_channels, int alpha_channel, int flags)
  1779. {
  1780. return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,
  1781. output_pixels, output_w, output_h, output_stride_in_bytes,
  1782. 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,
  1783. STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB);
  1784. }
  1785. STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1786. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1787. int num_channels, int alpha_channel, int flags,
  1788. stbir_edge edge_wrap_mode)
  1789. {
  1790. return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes,
  1791. output_pixels, output_w, output_h, output_stride_in_bytes,
  1792. 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT,
  1793. edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB);
  1794. }
  1795. STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1796. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1797. int num_channels, int alpha_channel, int flags,
  1798. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  1799. void *alloc_context)
  1800. {
  1801. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1802. output_pixels, output_w, output_h, output_stride_in_bytes,
  1803. 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter,
  1804. edge_wrap_mode, edge_wrap_mode, space);
  1805. }
  1806. STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1807. stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,
  1808. int num_channels, int alpha_channel, int flags,
  1809. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  1810. void *alloc_context)
  1811. {
  1812. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1813. output_pixels, output_w, output_h, output_stride_in_bytes,
  1814. 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter,
  1815. edge_wrap_mode, edge_wrap_mode, space);
  1816. }
  1817. STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1818. float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,
  1819. int num_channels, int alpha_channel, int flags,
  1820. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  1821. void *alloc_context)
  1822. {
  1823. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1824. output_pixels, output_w, output_h, output_stride_in_bytes,
  1825. 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter,
  1826. edge_wrap_mode, edge_wrap_mode, space);
  1827. }
  1828. STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1829. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1830. stbir_datatype datatype,
  1831. int num_channels, int alpha_channel, int flags,
  1832. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  1833. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  1834. stbir_colorspace space, void *alloc_context)
  1835. {
  1836. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1837. output_pixels, output_w, output_h, output_stride_in_bytes,
  1838. 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,
  1839. edge_mode_horizontal, edge_mode_vertical, space);
  1840. }
  1841. STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1842. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1843. stbir_datatype datatype,
  1844. int num_channels, int alpha_channel, int flags,
  1845. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  1846. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  1847. stbir_colorspace space, void *alloc_context,
  1848. float x_scale, float y_scale,
  1849. float x_offset, float y_offset)
  1850. {
  1851. float transform[4];
  1852. transform[0] = x_scale;
  1853. transform[1] = y_scale;
  1854. transform[2] = x_offset;
  1855. transform[3] = y_offset;
  1856. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1857. output_pixels, output_w, output_h, output_stride_in_bytes,
  1858. 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,
  1859. edge_mode_horizontal, edge_mode_vertical, space);
  1860. }
  1861. STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  1862. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  1863. stbir_datatype datatype,
  1864. int num_channels, int alpha_channel, int flags,
  1865. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  1866. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  1867. stbir_colorspace space, void *alloc_context,
  1868. float s0, float t0, float s1, float t1)
  1869. {
  1870. return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes,
  1871. output_pixels, output_w, output_h, output_stride_in_bytes,
  1872. s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical,
  1873. edge_mode_horizontal, edge_mode_vertical, space);
  1874. }
  1875. /*
  1876. ------------------------------------------------------------------------------
  1877. This software is available under 2 licenses -- choose whichever you prefer.
  1878. ------------------------------------------------------------------------------
  1879. ALTERNATIVE A - MIT License
  1880. Copyright (c) 2017 Sean Barrett
  1881. Permission is hereby granted, free of charge, to any person obtaining a copy of
  1882. this software and associated documentation files (the "Software"), to deal in
  1883. the Software without restriction, including without limitation the rights to
  1884. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  1885. of the Software, and to permit persons to whom the Software is furnished to do
  1886. so, subject to the following conditions:
  1887. The above copyright notice and this permission notice shall be included in all
  1888. copies or substantial portions of the Software.
  1889. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1890. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1891. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1892. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  1893. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  1894. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  1895. SOFTWARE.
  1896. ------------------------------------------------------------------------------
  1897. ALTERNATIVE B - Public Domain (www.unlicense.org)
  1898. This is free and unencumbered software released into the public domain.
  1899. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  1900. software, either in source code form or as a compiled binary, for any purpose,
  1901. commercial or non-commercial, and by any means.
  1902. In jurisdictions that recognize copyright laws, the author or authors of this
  1903. software dedicate any and all copyright interest in the software to the public
  1904. domain. We make this dedication for the benefit of the public at large and to
  1905. the detriment of our heirs and successors. We intend this dedication to be an
  1906. overt act of relinquishment in perpetuity of all present and future rights to
  1907. this software under copyright law.
  1908. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  1909. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  1910. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  1911. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  1912. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  1913. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  1914. ------------------------------------------------------------------------------
  1915. */