Resample.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. #include "Imaging.h"
  2. #include <math.h>
  3. #define ROUND_UP(f) ((int) ((f) >= 0.0 ? (f) + 0.5F : (f) - 0.5F))
  4. struct filter {
  5. double (*filter)(double x);
  6. double support;
  7. };
  8. static inline double box_filter(double x)
  9. {
  10. if (x >= -0.5 && x < 0.5)
  11. return 1.0;
  12. return 0.0;
  13. }
  14. static inline double bilinear_filter(double x)
  15. {
  16. if (x < 0.0)
  17. x = -x;
  18. if (x < 1.0)
  19. return 1.0-x;
  20. return 0.0;
  21. }
  22. static inline double hamming_filter(double x)
  23. {
  24. if (x < 0.0)
  25. x = -x;
  26. if (x == 0.0)
  27. return 1.0;
  28. if (x >= 1.0)
  29. return 0.0;
  30. x = x * M_PI;
  31. return sin(x) / x * (0.54f + 0.46f * cos(x));
  32. }
  33. static inline double bicubic_filter(double x)
  34. {
  35. /* https://en.wikipedia.org/wiki/Bicubic_interpolation#Bicubic_convolution_algorithm */
  36. #define a -0.5
  37. if (x < 0.0)
  38. x = -x;
  39. if (x < 1.0)
  40. return ((a + 2.0) * x - (a + 3.0)) * x*x + 1;
  41. if (x < 2.0)
  42. return (((x - 5) * x + 8) * x - 4) * a;
  43. return 0.0;
  44. #undef a
  45. }
  46. static inline double sinc_filter(double x)
  47. {
  48. if (x == 0.0)
  49. return 1.0;
  50. x = x * M_PI;
  51. return sin(x) / x;
  52. }
  53. static inline double lanczos_filter(double x)
  54. {
  55. /* truncated sinc */
  56. if (-3.0 <= x && x < 3.0)
  57. return sinc_filter(x) * sinc_filter(x/3);
  58. return 0.0;
  59. }
  60. static struct filter BOX = { box_filter, 0.5 };
  61. static struct filter BILINEAR = { bilinear_filter, 1.0 };
  62. static struct filter HAMMING = { hamming_filter, 1.0 };
  63. static struct filter BICUBIC = { bicubic_filter, 2.0 };
  64. static struct filter LANCZOS = { lanczos_filter, 3.0 };
  65. /* 8 bits for result. Filter can have negative areas.
  66. In one cases the sum of the coefficients will be negative,
  67. in the other it will be more than 1.0. That is why we need
  68. two extra bits for overflow and int type. */
  69. #define PRECISION_BITS (32 - 8 - 2)
  70. /* Handles values form -640 to 639. */
  71. UINT8 _clip8_lookups[1280] = {
  72. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  73. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  74. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  75. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  76. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  77. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  78. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  79. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  80. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  81. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  82. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  83. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  84. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  85. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  86. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  87. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  88. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  91. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  94. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  96. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  112. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  113. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  114. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
  115. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
  116. 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
  117. 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
  118. 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
  119. 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  120. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
  121. 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159,
  122. 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  123. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191,
  124. 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207,
  125. 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  126. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239,
  127. 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255,
  128. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  129. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  130. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  131. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  132. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  133. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  134. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  135. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  136. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  137. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  138. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  139. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  140. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  141. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  142. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  143. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  144. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  145. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  146. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  147. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  148. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  149. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  150. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  151. 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  152. };
  153. UINT8 *clip8_lookups = &_clip8_lookups[640];
  154. static inline UINT8 clip8(int in)
  155. {
  156. return clip8_lookups[in >> PRECISION_BITS];
  157. }
  158. int
  159. precompute_coeffs(int inSize, float in0, float in1, int outSize,
  160. struct filter *filterp, int **boundsp, double **kkp) {
  161. double support, scale, filterscale;
  162. double center, ww, ss;
  163. int xx, x, ksize, xmin, xmax;
  164. int *bounds;
  165. double *kk, *k;
  166. /* prepare for horizontal stretch */
  167. filterscale = scale = (double) (in1 - in0) / outSize;
  168. if (filterscale < 1.0) {
  169. filterscale = 1.0;
  170. }
  171. /* determine support size (length of resampling filter) */
  172. support = filterp->support * filterscale;
  173. /* maximum number of coeffs */
  174. ksize = (int) ceil(support) * 2 + 1;
  175. // check for overflow
  176. if (outSize > INT_MAX / (ksize * sizeof(double))) {
  177. ImagingError_MemoryError();
  178. return 0;
  179. }
  180. /* coefficient buffer */
  181. /* malloc check ok, overflow checked above */
  182. kk = malloc(outSize * ksize * sizeof(double));
  183. if ( ! kk) {
  184. ImagingError_MemoryError();
  185. return 0;
  186. }
  187. /* malloc check ok, ksize*sizeof(double) > 2*sizeof(int) */
  188. bounds = malloc(outSize * 2 * sizeof(int));
  189. if ( ! bounds) {
  190. free(kk);
  191. ImagingError_MemoryError();
  192. return 0;
  193. }
  194. for (xx = 0; xx < outSize; xx++) {
  195. center = in0 + (xx + 0.5) * scale;
  196. ww = 0.0;
  197. ss = 1.0 / filterscale;
  198. // Round the value
  199. xmin = (int) (center - support + 0.5);
  200. if (xmin < 0)
  201. xmin = 0;
  202. // Round the value
  203. xmax = (int) (center + support + 0.5);
  204. if (xmax > inSize)
  205. xmax = inSize;
  206. xmax -= xmin;
  207. k = &kk[xx * ksize];
  208. for (x = 0; x < xmax; x++) {
  209. double w = filterp->filter((x + xmin - center + 0.5) * ss);
  210. k[x] = w;
  211. ww += w;
  212. }
  213. for (x = 0; x < xmax; x++) {
  214. if (ww != 0.0)
  215. k[x] /= ww;
  216. }
  217. // Remaining values should stay empty if they are used despite of xmax.
  218. for (; x < ksize; x++) {
  219. k[x] = 0;
  220. }
  221. bounds[xx * 2 + 0] = xmin;
  222. bounds[xx * 2 + 1] = xmax;
  223. }
  224. *boundsp = bounds;
  225. *kkp = kk;
  226. return ksize;
  227. }
  228. void
  229. normalize_coeffs_8bpc(int outSize, int ksize, double *prekk)
  230. {
  231. int x;
  232. INT32 *kk;
  233. // use the same buffer for normalized coefficients
  234. kk = (INT32 *) prekk;
  235. for (x = 0; x < outSize * ksize; x++) {
  236. if (prekk[x] < 0) {
  237. kk[x] = (int) (-0.5 + prekk[x] * (1 << PRECISION_BITS));
  238. } else {
  239. kk[x] = (int) (0.5 + prekk[x] * (1 << PRECISION_BITS));
  240. }
  241. }
  242. }
  243. void
  244. ImagingResampleHorizontal_8bpc(Imaging imOut, Imaging imIn, int offset,
  245. int ksize, int *bounds, double *prekk)
  246. {
  247. ImagingSectionCookie cookie;
  248. int ss0, ss1, ss2, ss3;
  249. int xx, yy, x, xmin, xmax;
  250. INT32 *k, *kk;
  251. // use the same buffer for normalized coefficients
  252. kk = (INT32 *) prekk;
  253. normalize_coeffs_8bpc(imOut->xsize, ksize, prekk);
  254. ImagingSectionEnter(&cookie);
  255. if (imIn->image8) {
  256. for (yy = 0; yy < imOut->ysize; yy++) {
  257. for (xx = 0; xx < imOut->xsize; xx++) {
  258. xmin = bounds[xx * 2 + 0];
  259. xmax = bounds[xx * 2 + 1];
  260. k = &kk[xx * ksize];
  261. ss0 = 1 << (PRECISION_BITS -1);
  262. for (x = 0; x < xmax; x++)
  263. ss0 += ((UINT8) imIn->image8[yy + offset][x + xmin]) * k[x];
  264. imOut->image8[yy][xx] = clip8(ss0);
  265. }
  266. }
  267. } else if (imIn->type == IMAGING_TYPE_UINT8) {
  268. if (imIn->bands == 2) {
  269. for (yy = 0; yy < imOut->ysize; yy++) {
  270. for (xx = 0; xx < imOut->xsize; xx++) {
  271. UINT32 v;
  272. xmin = bounds[xx * 2 + 0];
  273. xmax = bounds[xx * 2 + 1];
  274. k = &kk[xx * ksize];
  275. ss0 = ss3 = 1 << (PRECISION_BITS -1);
  276. for (x = 0; x < xmax; x++) {
  277. ss0 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 0]) * k[x];
  278. ss3 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 3]) * k[x];
  279. }
  280. v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
  281. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  282. }
  283. }
  284. } else if (imIn->bands == 3) {
  285. for (yy = 0; yy < imOut->ysize; yy++) {
  286. for (xx = 0; xx < imOut->xsize; xx++) {
  287. UINT32 v;
  288. xmin = bounds[xx * 2 + 0];
  289. xmax = bounds[xx * 2 + 1];
  290. k = &kk[xx * ksize];
  291. ss0 = ss1 = ss2 = 1 << (PRECISION_BITS -1);
  292. for (x = 0; x < xmax; x++) {
  293. ss0 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 0]) * k[x];
  294. ss1 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 1]) * k[x];
  295. ss2 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 2]) * k[x];
  296. }
  297. v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
  298. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  299. }
  300. }
  301. } else {
  302. for (yy = 0; yy < imOut->ysize; yy++) {
  303. for (xx = 0; xx < imOut->xsize; xx++) {
  304. UINT32 v;
  305. xmin = bounds[xx * 2 + 0];
  306. xmax = bounds[xx * 2 + 1];
  307. k = &kk[xx * ksize];
  308. ss0 = ss1 = ss2 = ss3 = 1 << (PRECISION_BITS -1);
  309. for (x = 0; x < xmax; x++) {
  310. ss0 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 0]) * k[x];
  311. ss1 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 1]) * k[x];
  312. ss2 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 2]) * k[x];
  313. ss3 += ((UINT8) imIn->image[yy + offset][(x + xmin)*4 + 3]) * k[x];
  314. }
  315. v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
  316. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  317. }
  318. }
  319. }
  320. }
  321. ImagingSectionLeave(&cookie);
  322. }
  323. void
  324. ImagingResampleVertical_8bpc(Imaging imOut, Imaging imIn, int offset,
  325. int ksize, int *bounds, double *prekk)
  326. {
  327. ImagingSectionCookie cookie;
  328. int ss0, ss1, ss2, ss3;
  329. int xx, yy, y, ymin, ymax;
  330. INT32 *k, *kk;
  331. // use the same buffer for normalized coefficients
  332. kk = (INT32 *) prekk;
  333. normalize_coeffs_8bpc(imOut->ysize, ksize, prekk);
  334. ImagingSectionEnter(&cookie);
  335. if (imIn->image8) {
  336. for (yy = 0; yy < imOut->ysize; yy++) {
  337. k = &kk[yy * ksize];
  338. ymin = bounds[yy * 2 + 0];
  339. ymax = bounds[yy * 2 + 1];
  340. for (xx = 0; xx < imOut->xsize; xx++) {
  341. ss0 = 1 << (PRECISION_BITS -1);
  342. for (y = 0; y < ymax; y++)
  343. ss0 += ((UINT8) imIn->image8[y + ymin][xx]) * k[y];
  344. imOut->image8[yy][xx] = clip8(ss0);
  345. }
  346. }
  347. } else if (imIn->type == IMAGING_TYPE_UINT8) {
  348. if (imIn->bands == 2) {
  349. for (yy = 0; yy < imOut->ysize; yy++) {
  350. k = &kk[yy * ksize];
  351. ymin = bounds[yy * 2 + 0];
  352. ymax = bounds[yy * 2 + 1];
  353. for (xx = 0; xx < imOut->xsize; xx++) {
  354. UINT32 v;
  355. ss0 = ss3 = 1 << (PRECISION_BITS -1);
  356. for (y = 0; y < ymax; y++) {
  357. ss0 += ((UINT8) imIn->image[y + ymin][xx*4 + 0]) * k[y];
  358. ss3 += ((UINT8) imIn->image[y + ymin][xx*4 + 3]) * k[y];
  359. }
  360. v = MAKE_UINT32(clip8(ss0), 0, 0, clip8(ss3));
  361. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  362. }
  363. }
  364. } else if (imIn->bands == 3) {
  365. for (yy = 0; yy < imOut->ysize; yy++) {
  366. k = &kk[yy * ksize];
  367. ymin = bounds[yy * 2 + 0];
  368. ymax = bounds[yy * 2 + 1];
  369. for (xx = 0; xx < imOut->xsize; xx++) {
  370. UINT32 v;
  371. ss0 = ss1 = ss2 = 1 << (PRECISION_BITS -1);
  372. for (y = 0; y < ymax; y++) {
  373. ss0 += ((UINT8) imIn->image[y + ymin][xx*4 + 0]) * k[y];
  374. ss1 += ((UINT8) imIn->image[y + ymin][xx*4 + 1]) * k[y];
  375. ss2 += ((UINT8) imIn->image[y + ymin][xx*4 + 2]) * k[y];
  376. }
  377. v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), 0);
  378. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  379. }
  380. }
  381. } else {
  382. for (yy = 0; yy < imOut->ysize; yy++) {
  383. k = &kk[yy * ksize];
  384. ymin = bounds[yy * 2 + 0];
  385. ymax = bounds[yy * 2 + 1];
  386. for (xx = 0; xx < imOut->xsize; xx++) {
  387. UINT32 v;
  388. ss0 = ss1 = ss2 = ss3 = 1 << (PRECISION_BITS -1);
  389. for (y = 0; y < ymax; y++) {
  390. ss0 += ((UINT8) imIn->image[y + ymin][xx*4 + 0]) * k[y];
  391. ss1 += ((UINT8) imIn->image[y + ymin][xx*4 + 1]) * k[y];
  392. ss2 += ((UINT8) imIn->image[y + ymin][xx*4 + 2]) * k[y];
  393. ss3 += ((UINT8) imIn->image[y + ymin][xx*4 + 3]) * k[y];
  394. }
  395. v = MAKE_UINT32(clip8(ss0), clip8(ss1), clip8(ss2), clip8(ss3));
  396. memcpy(imOut->image[yy] + xx * sizeof(v), &v, sizeof(v));
  397. }
  398. }
  399. }
  400. }
  401. ImagingSectionLeave(&cookie);
  402. }
  403. void
  404. ImagingResampleHorizontal_32bpc(Imaging imOut, Imaging imIn, int offset,
  405. int ksize, int *bounds, double *kk)
  406. {
  407. ImagingSectionCookie cookie;
  408. double ss;
  409. int xx, yy, x, xmin, xmax;
  410. double *k;
  411. ImagingSectionEnter(&cookie);
  412. switch(imIn->type) {
  413. case IMAGING_TYPE_INT32:
  414. for (yy = 0; yy < imOut->ysize; yy++) {
  415. for (xx = 0; xx < imOut->xsize; xx++) {
  416. xmin = bounds[xx * 2 + 0];
  417. xmax = bounds[xx * 2 + 1];
  418. k = &kk[xx * ksize];
  419. ss = 0.0;
  420. for (x = 0; x < xmax; x++)
  421. ss += IMAGING_PIXEL_I(imIn, x + xmin, yy + offset) * k[x];
  422. IMAGING_PIXEL_I(imOut, xx, yy) = ROUND_UP(ss);
  423. }
  424. }
  425. break;
  426. case IMAGING_TYPE_FLOAT32:
  427. for (yy = 0; yy < imOut->ysize; yy++) {
  428. for (xx = 0; xx < imOut->xsize; xx++) {
  429. xmin = bounds[xx * 2 + 0];
  430. xmax = bounds[xx * 2 + 1];
  431. k = &kk[xx * ksize];
  432. ss = 0.0;
  433. for (x = 0; x < xmax; x++)
  434. ss += IMAGING_PIXEL_F(imIn, x + xmin, yy + offset) * k[x];
  435. IMAGING_PIXEL_F(imOut, xx, yy) = ss;
  436. }
  437. }
  438. break;
  439. }
  440. ImagingSectionLeave(&cookie);
  441. }
  442. void
  443. ImagingResampleVertical_32bpc(Imaging imOut, Imaging imIn, int offset,
  444. int ksize, int *bounds, double *kk)
  445. {
  446. ImagingSectionCookie cookie;
  447. double ss;
  448. int xx, yy, y, ymin, ymax;
  449. double *k;
  450. ImagingSectionEnter(&cookie);
  451. switch(imIn->type) {
  452. case IMAGING_TYPE_INT32:
  453. for (yy = 0; yy < imOut->ysize; yy++) {
  454. ymin = bounds[yy * 2 + 0];
  455. ymax = bounds[yy * 2 + 1];
  456. k = &kk[yy * ksize];
  457. for (xx = 0; xx < imOut->xsize; xx++) {
  458. ss = 0.0;
  459. for (y = 0; y < ymax; y++)
  460. ss += IMAGING_PIXEL_I(imIn, xx, y + ymin) * k[y];
  461. IMAGING_PIXEL_I(imOut, xx, yy) = ROUND_UP(ss);
  462. }
  463. }
  464. break;
  465. case IMAGING_TYPE_FLOAT32:
  466. for (yy = 0; yy < imOut->ysize; yy++) {
  467. ymin = bounds[yy * 2 + 0];
  468. ymax = bounds[yy * 2 + 1];
  469. k = &kk[yy * ksize];
  470. for (xx = 0; xx < imOut->xsize; xx++) {
  471. ss = 0.0;
  472. for (y = 0; y < ymax; y++)
  473. ss += IMAGING_PIXEL_F(imIn, xx, y + ymin) * k[y];
  474. IMAGING_PIXEL_F(imOut, xx, yy) = ss;
  475. }
  476. }
  477. break;
  478. }
  479. ImagingSectionLeave(&cookie);
  480. }
  481. typedef void (*ResampleFunction)(Imaging imOut, Imaging imIn, int offset,
  482. int ksize, int *bounds, double *kk);
  483. Imaging
  484. ImagingResampleInner(Imaging imIn, int xsize, int ysize,
  485. struct filter *filterp, float box[4],
  486. ResampleFunction ResampleHorizontal,
  487. ResampleFunction ResampleVertical);
  488. Imaging
  489. ImagingResample(Imaging imIn, int xsize, int ysize, int filter, float box[4])
  490. {
  491. struct filter *filterp;
  492. ResampleFunction ResampleHorizontal;
  493. ResampleFunction ResampleVertical;
  494. if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "1") == 0)
  495. return (Imaging) ImagingError_ModeError();
  496. if (imIn->type == IMAGING_TYPE_SPECIAL) {
  497. return (Imaging) ImagingError_ModeError();
  498. } else if (imIn->image8) {
  499. ResampleHorizontal = ImagingResampleHorizontal_8bpc;
  500. ResampleVertical = ImagingResampleVertical_8bpc;
  501. } else {
  502. switch(imIn->type) {
  503. case IMAGING_TYPE_UINT8:
  504. ResampleHorizontal = ImagingResampleHorizontal_8bpc;
  505. ResampleVertical = ImagingResampleVertical_8bpc;
  506. break;
  507. case IMAGING_TYPE_INT32:
  508. case IMAGING_TYPE_FLOAT32:
  509. ResampleHorizontal = ImagingResampleHorizontal_32bpc;
  510. ResampleVertical = ImagingResampleVertical_32bpc;
  511. break;
  512. default:
  513. return (Imaging) ImagingError_ModeError();
  514. }
  515. }
  516. /* check filter */
  517. switch (filter) {
  518. case IMAGING_TRANSFORM_BOX:
  519. filterp = &BOX;
  520. break;
  521. case IMAGING_TRANSFORM_BILINEAR:
  522. filterp = &BILINEAR;
  523. break;
  524. case IMAGING_TRANSFORM_HAMMING:
  525. filterp = &HAMMING;
  526. break;
  527. case IMAGING_TRANSFORM_BICUBIC:
  528. filterp = &BICUBIC;
  529. break;
  530. case IMAGING_TRANSFORM_LANCZOS:
  531. filterp = &LANCZOS;
  532. break;
  533. default:
  534. return (Imaging) ImagingError_ValueError(
  535. "unsupported resampling filter"
  536. );
  537. }
  538. return ImagingResampleInner(imIn, xsize, ysize, filterp, box,
  539. ResampleHorizontal, ResampleVertical);
  540. }
  541. Imaging
  542. ImagingResampleInner(Imaging imIn, int xsize, int ysize,
  543. struct filter *filterp, float box[4],
  544. ResampleFunction ResampleHorizontal,
  545. ResampleFunction ResampleVertical)
  546. {
  547. Imaging imTemp = NULL;
  548. Imaging imOut = NULL;
  549. int i, need_horizontal, need_vertical;
  550. int ybox_first, ybox_last;
  551. int ksize_horiz, ksize_vert;
  552. int *bounds_horiz, *bounds_vert;
  553. double *kk_horiz, *kk_vert;
  554. need_horizontal = xsize != imIn->xsize || box[0] || box[2] != xsize;
  555. need_vertical = ysize != imIn->ysize || box[1] || box[3] != ysize;
  556. ksize_horiz = precompute_coeffs(imIn->xsize, box[0], box[2], xsize,
  557. filterp, &bounds_horiz, &kk_horiz);
  558. if ( ! ksize_horiz) {
  559. return NULL;
  560. }
  561. ksize_vert = precompute_coeffs(imIn->ysize, box[1], box[3], ysize,
  562. filterp, &bounds_vert, &kk_vert);
  563. if ( ! ksize_vert) {
  564. free(bounds_horiz);
  565. free(kk_horiz);
  566. free(bounds_vert);
  567. free(kk_vert);
  568. return NULL;
  569. }
  570. // First used row in the source image
  571. ybox_first = bounds_vert[0];
  572. // Last used row in the source image
  573. ybox_last = bounds_vert[ysize*2 - 2] + bounds_vert[ysize*2 - 1];
  574. /* two-pass resize, horizontal pass */
  575. if (need_horizontal) {
  576. // Shift bounds for vertical pass
  577. for (i = 0; i < ysize; i++) {
  578. bounds_vert[i * 2] -= ybox_first;
  579. }
  580. imTemp = ImagingNewDirty(imIn->mode, xsize, ybox_last - ybox_first);
  581. if (imTemp) {
  582. ResampleHorizontal(imTemp, imIn, ybox_first,
  583. ksize_horiz, bounds_horiz, kk_horiz);
  584. }
  585. free(bounds_horiz);
  586. free(kk_horiz);
  587. if ( ! imTemp) {
  588. free(bounds_vert);
  589. free(kk_vert);
  590. return NULL;
  591. }
  592. imOut = imIn = imTemp;
  593. } else {
  594. // Free in any case
  595. free(bounds_horiz);
  596. free(kk_horiz);
  597. }
  598. /* vertical pass */
  599. if (need_vertical) {
  600. imOut = ImagingNewDirty(imIn->mode, imIn->xsize, ysize);
  601. if (imOut) {
  602. /* imIn can be the original image or horizontally resampled one */
  603. ResampleVertical(imOut, imIn, 0,
  604. ksize_vert, bounds_vert, kk_vert);
  605. }
  606. /* it's safe to call ImagingDelete with empty value
  607. if previous step was not performed. */
  608. ImagingDelete(imTemp);
  609. free(bounds_vert);
  610. free(kk_vert);
  611. if ( ! imOut) {
  612. return NULL;
  613. }
  614. } else {
  615. // Free in any case
  616. free(bounds_vert);
  617. free(kk_vert);
  618. }
  619. /* none of the previous steps are performed, copying */
  620. if ( ! imOut) {
  621. imOut = ImagingCopy(imIn);
  622. }
  623. return imOut;
  624. }