stb_image_resize.h 114 KB

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