🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2585 lines
111 KiB

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