stb_image_resize.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // SPDX-FileCopyrightText: Jorge L Rodriguez
  2. // SPDX-License-Identifier: MIT
  3. /* stb_image_resize - v0.97 - public domain image resizing
  4. by Jorge L Rodriguez (@VinoBS) - 2014
  5. http://github.com/nothings/stb
  6. Written with emphasis on usability, portability, and efficiency. (No
  7. SIMD or threads, so it be easily outperformed by libs that use those.)
  8. Only scaling and translation is supported, no rotations or shears.
  9. Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation.
  10. COMPILING & LINKING
  11. In one C/C++ file that #includes this file, do this:
  12. #define STB_IMAGE_RESIZE_IMPLEMENTATION
  13. before the #include. That will create the implementation in that file.
  14. QUICKSTART
  15. stbir_resize_uint8( input_pixels , in_w , in_h , 0,
  16. output_pixels, out_w, out_h, 0, num_channels)
  17. stbir_resize_float(...)
  18. stbir_resize_uint8_srgb( input_pixels , in_w , in_h , 0,
  19. output_pixels, out_w, out_h, 0,
  20. num_channels , alpha_chan , 0)
  21. stbir_resize_uint8_srgb_edgemode(
  22. input_pixels , in_w , in_h , 0,
  23. output_pixels, out_w, out_h, 0,
  24. num_channels , alpha_chan , 0, STBIR_EDGE_CLAMP)
  25. // WRAP/REFLECT/ZERO
  26. FULL API
  27. See the "header file" section of the source for API documentation.
  28. ADDITIONAL DOCUMENTATION
  29. SRGB & FLOATING POINT REPRESENTATION
  30. The sRGB functions presume IEEE floating point. If you do not have
  31. IEEE floating point, define STBIR_NON_IEEE_FLOAT. This will use
  32. a slower implementation.
  33. MEMORY ALLOCATION
  34. The resize functions here perform a single memory allocation using
  35. malloc. To control the memory allocation, before the #include that
  36. triggers the implementation, do:
  37. #define STBIR_MALLOC(size,context) ...
  38. #define STBIR_FREE(ptr,context) ...
  39. Each resize function makes exactly one call to malloc/free, so to use
  40. temp memory, store the temp memory in the context and return that.
  41. ASSERT
  42. Define STBIR_ASSERT(boolval) to override assert() and not use assert.h
  43. OPTIMIZATION
  44. Define STBIR_SATURATE_INT to compute clamp values in-range using
  45. integer operations instead of float operations. This may be faster
  46. on some platforms.
  47. DEFAULT FILTERS
  48. For functions which don't provide explicit control over what filters
  49. to use, you can change the compile-time defaults with
  50. #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something
  51. #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something
  52. See stbir_filter in the header-file section for the list of filters.
  53. NEW FILTERS
  54. A number of 1D filter kernels are used. For a list of
  55. supported filters see the stbir_filter enum. To add a new filter,
  56. write a filter function and add it to stbir__filter_info_table.
  57. PROGRESS
  58. For interactive use with slow resize operations, you can install
  59. a progress-report callback:
  60. #define STBIR_PROGRESS_REPORT(val) some_func(val)
  61. The parameter val is a float which goes from 0 to 1 as progress is made.
  62. For example:
  63. static void my_progress_report(float progress);
  64. #define STBIR_PROGRESS_REPORT(val) my_progress_report(val)
  65. #define STB_IMAGE_RESIZE_IMPLEMENTATION
  66. #include "stb_image_resize.h"
  67. static void my_progress_report(float progress)
  68. {
  69. printf("Progress: %f%%\n", progress*100);
  70. }
  71. MAX CHANNELS
  72. If your image has more than 64 channels, define STBIR_MAX_CHANNELS
  73. to the max you'll have.
  74. ALPHA CHANNEL
  75. Most of the resizing functions provide the ability to control how
  76. the alpha channel of an image is processed. The important things
  77. to know about this:
  78. 1. The best mathematically-behaved version of alpha to use is
  79. called "premultiplied alpha", in which the other color channels
  80. have had the alpha value multiplied in. If you use premultiplied
  81. alpha, linear filtering (such as image resampling done by this
  82. library, or performed in texture units on GPUs) does the "right
  83. thing". While premultiplied alpha is standard in the movie CGI
  84. industry, it is still uncommon in the videogame/real-time world.
  85. If you linearly filter non-premultiplied alpha, strange effects
  86. occur. (For example, the 50/50 average of 99% transparent bright green
  87. and 1% transparent black produces 50% transparent dark green when
  88. non-premultiplied, whereas premultiplied it produces 50%
  89. transparent near-black. The former introduces green energy
  90. that doesn't exist in the source image.)
  91. 2. Artists should not edit premultiplied-alpha images; artists
  92. want non-premultiplied alpha images. Thus, art tools generally output
  93. non-premultiplied alpha images.
  94. 3. You will get best results in most cases by converting images
  95. to premultiplied alpha before processing them mathematically.
  96. 4. If you pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED, the
  97. resizer does not do anything special for the alpha channel;
  98. it is resampled identically to other channels. This produces
  99. the correct results for premultiplied-alpha images, but produces
  100. less-than-ideal results for non-premultiplied-alpha images.
  101. 5. If you do not pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED,
  102. then the resizer weights the contribution of input pixels
  103. based on their alpha values, or, equivalently, it multiplies
  104. the alpha value into the color channels, resamples, then divides
  105. by the resultant alpha value. Input pixels which have alpha=0 do
  106. not contribute at all to output pixels unless _all_ of the input
  107. pixels affecting that output pixel have alpha=0, in which case
  108. the result for that pixel is the same as it would be without
  109. STBIR_FLAG_ALPHA_PREMULTIPLIED. However, this is only true for
  110. input images in integer formats. For input images in float format,
  111. input pixels with alpha=0 have no effect, and output pixels
  112. which have alpha=0 will be 0 in all channels. (For float images,
  113. you can manually achieve the same result by adding a tiny epsilon
  114. value to the alpha channel of every image, and then subtracting
  115. or clamping it at the end.)
  116. 6. You can suppress the behavior described in #5 and make
  117. all-0-alpha pixels have 0 in all channels by #defining
  118. STBIR_NO_ALPHA_EPSILON.
  119. 7. You can separately control whether the alpha channel is
  120. interpreted as linear or affected by the colorspace. By default
  121. it is linear; you almost never want to apply the colorspace.
  122. (For example, graphics hardware does not apply sRGB conversion
  123. to the alpha channel.)
  124. CONTRIBUTORS
  125. Jorge L Rodriguez: Implementation
  126. Sean Barrett: API design, optimizations
  127. Aras Pranckevicius: bugfix
  128. Nathan Reed: warning fixes
  129. REVISIONS
  130. 0.97 (2020-02-02) fixed warning
  131. 0.96 (2019-03-04) fixed warnings
  132. 0.95 (2017-07-23) fixed warnings
  133. 0.94 (2017-03-18) fixed warnings
  134. 0.93 (2017-03-03) fixed bug with certain combinations of heights
  135. 0.92 (2017-01-02) fix integer overflow on large (>2GB) images
  136. 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions
  137. 0.90 (2014-09-17) first released version
  138. LICENSE
  139. See end of file for license information.
  140. TODO
  141. Don't decode all of the image data when only processing a partial tile
  142. Don't use full-width decode buffers when only processing a partial tile
  143. When processing wide images, break processing into tiles so data fits in L1 cache
  144. Installable filters?
  145. Resize that respects alpha test coverage
  146. (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage:
  147. https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp )
  148. */
  149. #ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H
  150. #define STBIR_INCLUDE_STB_IMAGE_RESIZE_H
  151. #ifdef _MSC_VER
  152. typedef unsigned char stbir_uint8;
  153. typedef unsigned short stbir_uint16;
  154. typedef unsigned int stbir_uint32;
  155. #else
  156. #include <stdint.h>
  157. typedef uint8_t stbir_uint8;
  158. typedef uint16_t stbir_uint16;
  159. typedef uint32_t stbir_uint32;
  160. #endif
  161. #ifndef STBIRDEF
  162. #ifdef STB_IMAGE_RESIZE_STATIC
  163. #define STBIRDEF static
  164. #else
  165. #ifdef __cplusplus
  166. #define STBIRDEF extern "C"
  167. #else
  168. #define STBIRDEF extern
  169. #endif
  170. #endif
  171. #endif
  172. //////////////////////////////////////////////////////////////////////////////
  173. //
  174. // Easy-to-use API:
  175. //
  176. // * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4)
  177. // * input_w is input image width (x-axis), input_h is input image height (y-axis)
  178. // * stride is the offset between successive rows of image data in memory, in bytes. you can
  179. // specify 0 to mean packed continuously in memory
  180. // * alpha channel is treated identically to other channels.
  181. // * colorspace is linear or sRGB as specified by function name
  182. // * returned result is 1 for success or 0 in case of an error.
  183. // #define STBIR_ASSERT() to trigger an assert on parameter validation errors.
  184. // * Memory required grows approximately linearly with input and output size, but with
  185. // discontinuities at input_w == output_w and input_h == output_h.
  186. // * These functions use a "default" resampling filter defined at compile time. To change the filter,
  187. // you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE
  188. // and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API.
  189. STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  190. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  191. int num_channels);
  192. STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  193. float *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  194. int num_channels);
  195. // The following functions interpret image data as gamma-corrected sRGB.
  196. // Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel,
  197. // or otherwise provide the index of the alpha channel. Flags value
  198. // of 0 will probably do the right thing if you're not sure what
  199. // the flags mean.
  200. #define STBIR_ALPHA_CHANNEL_NONE -1
  201. // Set this flag if your texture has premultiplied alpha. Otherwise, stbir will
  202. // use alpha-weighted resampling (effectively premultiplying, resampling,
  203. // then unpremultiplying).
  204. #define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0)
  205. // The specified alpha channel should be handled as gamma-corrected value even
  206. // when doing sRGB operations.
  207. #define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1)
  208. STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  209. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  210. int num_channels, int alpha_channel, int flags);
  211. typedef enum
  212. {
  213. STBIR_EDGE_CLAMP = 1,
  214. STBIR_EDGE_REFLECT = 2,
  215. STBIR_EDGE_WRAP = 3,
  216. STBIR_EDGE_ZERO = 4,
  217. } stbir_edge;
  218. // This function adds the ability to specify how requests to sample off the edge of the image are handled.
  219. STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  220. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  221. int num_channels, int alpha_channel, int flags,
  222. stbir_edge edge_wrap_mode);
  223. //////////////////////////////////////////////////////////////////////////////
  224. //
  225. // Medium-complexity API
  226. //
  227. // This extends the easy-to-use API as follows:
  228. //
  229. // * Alpha-channel can be processed separately
  230. // * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE
  231. // * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT)
  232. // * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)
  233. // * Filter can be selected explicitly
  234. // * uint16 image type
  235. // * sRGB colorspace available for all types
  236. // * context parameter for passing to STBIR_MALLOC
  237. typedef enum
  238. {
  239. STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses
  240. STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios
  241. STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering
  242. STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque
  243. STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline
  244. STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3
  245. } stbir_filter;
  246. typedef enum
  247. {
  248. STBIR_COLORSPACE_LINEAR,
  249. STBIR_COLORSPACE_SRGB,
  250. STBIR_MAX_COLORSPACES,
  251. } stbir_colorspace;
  252. // The following functions are all identical except for the type of the image data
  253. STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  254. unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  255. int num_channels, int alpha_channel, int flags,
  256. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  257. void *alloc_context);
  258. STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  259. stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes,
  260. int num_channels, int alpha_channel, int flags,
  261. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  262. void *alloc_context);
  263. STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  264. float *output_pixels , int output_w, int output_h, int output_stride_in_bytes,
  265. int num_channels, int alpha_channel, int flags,
  266. stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space,
  267. void *alloc_context);
  268. //////////////////////////////////////////////////////////////////////////////
  269. //
  270. // Full-complexity API
  271. //
  272. // This extends the medium API as follows:
  273. //
  274. // * uint32 image type
  275. // * not typesafe
  276. // * separate filter types for each axis
  277. // * separate edge modes for each axis
  278. // * can specify scale explicitly for subpixel correctness
  279. // * can specify image source tile using texture coordinates
  280. typedef enum
  281. {
  282. STBIR_TYPE_UINT8 ,
  283. STBIR_TYPE_UINT16,
  284. STBIR_TYPE_UINT32,
  285. STBIR_TYPE_FLOAT ,
  286. STBIR_MAX_TYPES
  287. } stbir_datatype;
  288. STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  289. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  290. stbir_datatype datatype,
  291. int num_channels, int alpha_channel, int flags,
  292. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  293. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  294. stbir_colorspace space, void *alloc_context);
  295. STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  296. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  297. stbir_datatype datatype,
  298. int num_channels, int alpha_channel, int flags,
  299. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  300. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  301. stbir_colorspace space, void *alloc_context,
  302. float x_scale, float y_scale,
  303. float x_offset, float y_offset);
  304. STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes,
  305. void *output_pixels, int output_w, int output_h, int output_stride_in_bytes,
  306. stbir_datatype datatype,
  307. int num_channels, int alpha_channel, int flags,
  308. stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical,
  309. stbir_filter filter_horizontal, stbir_filter filter_vertical,
  310. stbir_colorspace space, void *alloc_context,
  311. float s0, float t0, float s1, float t1);
  312. // (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.
  313. //
  314. //
  315. //// end header file /////////////////////////////////////////////////////
  316. #endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H
  317. /*
  318. ------------------------------------------------------------------------------
  319. This software is available under 2 licenses -- choose whichever you prefer.
  320. ------------------------------------------------------------------------------
  321. ALTERNATIVE A - MIT License
  322. Copyright (c) 2017 Sean Barrett
  323. Permission is hereby granted, free of charge, to any person obtaining a copy of
  324. this software and associated documentation files (the "Software"), to deal in
  325. the Software without restriction, including without limitation the rights to
  326. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  327. of the Software, and to permit persons to whom the Software is furnished to do
  328. so, subject to the following conditions:
  329. The above copyright notice and this permission notice shall be included in all
  330. copies or substantial portions of the Software.
  331. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  332. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  333. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  334. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  335. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  336. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  337. SOFTWARE.
  338. ------------------------------------------------------------------------------
  339. ALTERNATIVE B - Public Domain (www.unlicense.org)
  340. This is free and unencumbered software released into the public domain.
  341. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  342. software, either in source code form or as a compiled binary, for any purpose,
  343. commercial or non-commercial, and by any means.
  344. In jurisdictions that recognize copyright laws, the author or authors of this
  345. software dedicate any and all copyright interest in the software to the public
  346. domain. We make this dedication for the benefit of the public at large and to
  347. the detriment of our heirs and successors. We intend this dedication to be an
  348. overt act of relinquishment in perpetuity of all present and future rights to
  349. this software under copyright law.
  350. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  351. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  352. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  353. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  354. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  355. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  356. ------------------------------------------------------------------------------
  357. */