Geometry.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. #include "Imaging.h"
  2. /* For large images rotation is an inefficient operation in terms of CPU cache.
  3. One row in the source image affects each column in destination.
  4. Rotating in chunks that fit in the cache can speed up rotation
  5. 8x on a modern CPU. A chunk size of 128 requires only 65k and is large enough
  6. that the overhead from the extra loops are not apparent. */
  7. #define ROTATE_CHUNK 512
  8. #define ROTATE_SMALL_CHUNK 8
  9. #define COORD(v) ((v) < 0.0 ? -1 : ((int)(v)))
  10. #define FLOOR(v) ((v) < 0.0 ? ((int)floor(v)) : ((int)(v)))
  11. /* -------------------------------------------------------------------- */
  12. /* Transpose operations */
  13. Imaging
  14. ImagingFlipLeftRight(Imaging imOut, Imaging imIn)
  15. {
  16. ImagingSectionCookie cookie;
  17. int x, y, xr;
  18. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  19. return (Imaging) ImagingError_ModeError();
  20. if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize)
  21. return (Imaging) ImagingError_Mismatch();
  22. ImagingCopyPalette(imOut, imIn);
  23. #define FLIP_LEFT_RIGHT(INT, image) \
  24. for (y = 0; y < imIn->ysize; y++) { \
  25. INT* in = (INT *)imIn->image[y]; \
  26. INT* out = (INT *)imOut->image[y]; \
  27. xr = imIn->xsize-1; \
  28. for (x = 0; x < imIn->xsize; x++, xr--) \
  29. out[xr] = in[x]; \
  30. }
  31. ImagingSectionEnter(&cookie);
  32. if (imIn->image8) {
  33. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  34. FLIP_LEFT_RIGHT(UINT16, image8)
  35. } else {
  36. FLIP_LEFT_RIGHT(UINT8, image8)
  37. }
  38. } else {
  39. FLIP_LEFT_RIGHT(INT32, image32)
  40. }
  41. ImagingSectionLeave(&cookie);
  42. #undef FLIP_LEFT_RIGHT
  43. return imOut;
  44. }
  45. Imaging
  46. ImagingFlipTopBottom(Imaging imOut, Imaging imIn)
  47. {
  48. ImagingSectionCookie cookie;
  49. int y, yr;
  50. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  51. return (Imaging) ImagingError_ModeError();
  52. if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize)
  53. return (Imaging) ImagingError_Mismatch();
  54. ImagingCopyPalette(imOut, imIn);
  55. ImagingSectionEnter(&cookie);
  56. yr = imIn->ysize - 1;
  57. for (y = 0; y < imIn->ysize; y++, yr--)
  58. memcpy(imOut->image[yr], imIn->image[y], imIn->linesize);
  59. ImagingSectionLeave(&cookie);
  60. return imOut;
  61. }
  62. Imaging
  63. ImagingRotate90(Imaging imOut, Imaging imIn)
  64. {
  65. ImagingSectionCookie cookie;
  66. int x, y, xx, yy, xr, xxsize, yysize;
  67. int xxx, yyy, xxxsize, yyysize;
  68. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  69. return (Imaging) ImagingError_ModeError();
  70. if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize)
  71. return (Imaging) ImagingError_Mismatch();
  72. ImagingCopyPalette(imOut, imIn);
  73. #define ROTATE_90(INT, image) \
  74. for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \
  75. for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \
  76. yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \
  77. xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \
  78. for (yy = y; yy < yysize; yy += ROTATE_SMALL_CHUNK) { \
  79. for (xx = x; xx < xxsize; xx += ROTATE_SMALL_CHUNK) { \
  80. yyysize = yy + ROTATE_SMALL_CHUNK < imIn->ysize ? yy + ROTATE_SMALL_CHUNK : imIn->ysize; \
  81. xxxsize = xx + ROTATE_SMALL_CHUNK < imIn->xsize ? xx + ROTATE_SMALL_CHUNK : imIn->xsize; \
  82. for (yyy = yy; yyy < yyysize; yyy++) { \
  83. INT* in = (INT *)imIn->image[yyy]; \
  84. xr = imIn->xsize - 1 - xx; \
  85. for (xxx = xx; xxx < xxxsize; xxx++, xr--) { \
  86. INT* out = (INT *)imOut->image[xr]; \
  87. out[yyy] = in[xxx]; \
  88. } \
  89. } \
  90. } \
  91. } \
  92. } \
  93. }
  94. ImagingSectionEnter(&cookie);
  95. if (imIn->image8) {
  96. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  97. ROTATE_90(UINT16, image8);
  98. } else {
  99. ROTATE_90(UINT8, image8);
  100. }
  101. } else {
  102. ROTATE_90(INT32, image32);
  103. }
  104. ImagingSectionLeave(&cookie);
  105. #undef ROTATE_90
  106. return imOut;
  107. }
  108. Imaging
  109. ImagingTranspose(Imaging imOut, Imaging imIn)
  110. {
  111. ImagingSectionCookie cookie;
  112. int x, y, xx, yy, xxsize, yysize;
  113. int xxx, yyy, xxxsize, yyysize;
  114. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  115. return (Imaging) ImagingError_ModeError();
  116. if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize)
  117. return (Imaging) ImagingError_Mismatch();
  118. ImagingCopyPalette(imOut, imIn);
  119. #define TRANSPOSE(INT, image) \
  120. for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \
  121. for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \
  122. yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \
  123. xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \
  124. for (yy = y; yy < yysize; yy += ROTATE_SMALL_CHUNK) { \
  125. for (xx = x; xx < xxsize; xx += ROTATE_SMALL_CHUNK) { \
  126. yyysize = yy + ROTATE_SMALL_CHUNK < imIn->ysize ? yy + ROTATE_SMALL_CHUNK : imIn->ysize; \
  127. xxxsize = xx + ROTATE_SMALL_CHUNK < imIn->xsize ? xx + ROTATE_SMALL_CHUNK : imIn->xsize; \
  128. for (yyy = yy; yyy < yyysize; yyy++) { \
  129. INT* in = (INT *)imIn->image[yyy]; \
  130. for (xxx = xx; xxx < xxxsize; xxx++) { \
  131. INT* out = (INT *)imOut->image[xxx]; \
  132. out[yyy] = in[xxx]; \
  133. } \
  134. } \
  135. } \
  136. } \
  137. } \
  138. }
  139. ImagingSectionEnter(&cookie);
  140. if (imIn->image8) {
  141. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  142. TRANSPOSE(UINT16, image8);
  143. } else {
  144. TRANSPOSE(UINT8, image8);
  145. }
  146. } else {
  147. TRANSPOSE(INT32, image32);
  148. }
  149. ImagingSectionLeave(&cookie);
  150. #undef TRANSPOSE
  151. return imOut;
  152. }
  153. Imaging
  154. ImagingTransverse(Imaging imOut, Imaging imIn)
  155. {
  156. ImagingSectionCookie cookie;
  157. int x, y, xr, yr, xx, yy, xxsize, yysize;
  158. int xxx, yyy, xxxsize, yyysize;
  159. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  160. return (Imaging) ImagingError_ModeError();
  161. if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize)
  162. return (Imaging) ImagingError_Mismatch();
  163. ImagingCopyPalette(imOut, imIn);
  164. #define TRANSVERSE(INT, image) \
  165. for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \
  166. for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \
  167. yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \
  168. xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \
  169. for (yy = y; yy < yysize; yy += ROTATE_SMALL_CHUNK) { \
  170. for (xx = x; xx < xxsize; xx += ROTATE_SMALL_CHUNK) { \
  171. yyysize = yy + ROTATE_SMALL_CHUNK < imIn->ysize ? yy + ROTATE_SMALL_CHUNK : imIn->ysize; \
  172. xxxsize = xx + ROTATE_SMALL_CHUNK < imIn->xsize ? xx + ROTATE_SMALL_CHUNK : imIn->xsize; \
  173. yr = imIn->ysize - 1 - yy; \
  174. for (yyy = yy; yyy < yyysize; yyy++, yr--) { \
  175. INT* in = (INT *)imIn->image[yyy]; \
  176. xr = imIn->xsize - 1 - xx; \
  177. for (xxx = xx; xxx < xxxsize; xxx++, xr--) { \
  178. INT* out = (INT *)imOut->image[xr]; \
  179. out[yr] = in[xxx]; \
  180. } \
  181. } \
  182. } \
  183. } \
  184. } \
  185. }
  186. ImagingSectionEnter(&cookie);
  187. if (imIn->image8) {
  188. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  189. TRANSVERSE(UINT16, image8);
  190. } else {
  191. TRANSVERSE(UINT8, image8);
  192. }
  193. } else {
  194. TRANSVERSE(INT32, image32);
  195. }
  196. ImagingSectionLeave(&cookie);
  197. #undef TRANSVERSE
  198. return imOut;
  199. }
  200. Imaging
  201. ImagingRotate180(Imaging imOut, Imaging imIn)
  202. {
  203. ImagingSectionCookie cookie;
  204. int x, y, xr, yr;
  205. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  206. return (Imaging) ImagingError_ModeError();
  207. if (imIn->xsize != imOut->xsize || imIn->ysize != imOut->ysize)
  208. return (Imaging) ImagingError_Mismatch();
  209. ImagingCopyPalette(imOut, imIn);
  210. #define ROTATE_180(INT, image) \
  211. for (y = 0; y < imIn->ysize; y++, yr--) { \
  212. INT* in = (INT *)imIn->image[y]; \
  213. INT* out = (INT *)imOut->image[yr]; \
  214. xr = imIn->xsize-1; \
  215. for (x = 0; x < imIn->xsize; x++, xr--) \
  216. out[xr] = in[x]; \
  217. }
  218. ImagingSectionEnter(&cookie);
  219. yr = imIn->ysize-1;
  220. if (imIn->image8) {
  221. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  222. ROTATE_180(UINT16, image8)
  223. } else {
  224. ROTATE_180(UINT8, image8)
  225. }
  226. } else {
  227. ROTATE_180(INT32, image32)
  228. }
  229. ImagingSectionLeave(&cookie);
  230. #undef ROTATE_180
  231. return imOut;
  232. }
  233. Imaging
  234. ImagingRotate270(Imaging imOut, Imaging imIn)
  235. {
  236. ImagingSectionCookie cookie;
  237. int x, y, xx, yy, yr, xxsize, yysize;
  238. int xxx, yyy, xxxsize, yyysize;
  239. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  240. return (Imaging) ImagingError_ModeError();
  241. if (imIn->xsize != imOut->ysize || imIn->ysize != imOut->xsize)
  242. return (Imaging) ImagingError_Mismatch();
  243. ImagingCopyPalette(imOut, imIn);
  244. #define ROTATE_270(INT, image) \
  245. for (y = 0; y < imIn->ysize; y += ROTATE_CHUNK) { \
  246. for (x = 0; x < imIn->xsize; x += ROTATE_CHUNK) { \
  247. yysize = y + ROTATE_CHUNK < imIn->ysize ? y + ROTATE_CHUNK : imIn->ysize; \
  248. xxsize = x + ROTATE_CHUNK < imIn->xsize ? x + ROTATE_CHUNK : imIn->xsize; \
  249. for (yy = y; yy < yysize; yy += ROTATE_SMALL_CHUNK) { \
  250. for (xx = x; xx < xxsize; xx += ROTATE_SMALL_CHUNK) { \
  251. yyysize = yy + ROTATE_SMALL_CHUNK < imIn->ysize ? yy + ROTATE_SMALL_CHUNK : imIn->ysize; \
  252. xxxsize = xx + ROTATE_SMALL_CHUNK < imIn->xsize ? xx + ROTATE_SMALL_CHUNK : imIn->xsize; \
  253. yr = imIn->ysize - 1 - yy; \
  254. for (yyy = yy; yyy < yyysize; yyy++, yr--) { \
  255. INT* in = (INT *)imIn->image[yyy]; \
  256. for (xxx = xx; xxx < xxxsize; xxx++) { \
  257. INT* out = (INT *)imOut->image[xxx]; \
  258. out[yr] = in[xxx]; \
  259. } \
  260. } \
  261. } \
  262. } \
  263. } \
  264. }
  265. ImagingSectionEnter(&cookie);
  266. if (imIn->image8) {
  267. if (strncmp(imIn->mode, "I;16", 4) == 0) {
  268. ROTATE_270(UINT16, image8);
  269. } else {
  270. ROTATE_270(UINT8, image8);
  271. }
  272. } else {
  273. ROTATE_270(INT32, image32);
  274. }
  275. ImagingSectionLeave(&cookie);
  276. #undef ROTATE_270
  277. return imOut;
  278. }
  279. /* -------------------------------------------------------------------- */
  280. /* Transforms */
  281. /* transform primitives (ImagingTransformMap) */
  282. static int
  283. affine_transform(double* xout, double* yout, int x, int y, void* data)
  284. {
  285. /* full moon tonight. your compiler will generate bogus code
  286. for simple expressions, unless you reorganize the code, or
  287. install Service Pack 3 */
  288. double* a = (double*) data;
  289. double a0 = a[0]; double a1 = a[1]; double a2 = a[2];
  290. double a3 = a[3]; double a4 = a[4]; double a5 = a[5];
  291. double xin = x + 0.5;
  292. double yin = y + 0.5;
  293. xout[0] = a0*xin + a1*yin + a2;
  294. yout[0] = a3*xin + a4*yin + a5;
  295. return 1;
  296. }
  297. static int
  298. perspective_transform(double* xout, double* yout, int x, int y, void* data)
  299. {
  300. double* a = (double*) data;
  301. double a0 = a[0]; double a1 = a[1]; double a2 = a[2];
  302. double a3 = a[3]; double a4 = a[4]; double a5 = a[5];
  303. double a6 = a[6]; double a7 = a[7];
  304. double xin = x + 0.5;
  305. double yin = y + 0.5;
  306. xout[0] = (a0*xin + a1*yin + a2) / (a6*xin + a7*yin + 1);
  307. yout[0] = (a3*xin + a4*yin + a5) / (a6*xin + a7*yin + 1);
  308. return 1;
  309. }
  310. static int
  311. quad_transform(double* xout, double* yout, int x, int y, void* data)
  312. {
  313. /* quad warp: map quadrilateral to rectangle */
  314. double* a = (double*) data;
  315. double a0 = a[0]; double a1 = a[1]; double a2 = a[2]; double a3 = a[3];
  316. double a4 = a[4]; double a5 = a[5]; double a6 = a[6]; double a7 = a[7];
  317. double xin = x + 0.5;
  318. double yin = y + 0.5;
  319. xout[0] = a0 + a1*xin + a2*yin + a3*xin*yin;
  320. yout[0] = a4 + a5*xin + a6*yin + a7*xin*yin;
  321. return 1;
  322. }
  323. /* transform filters (ImagingTransformFilter) */
  324. static int
  325. nearest_filter8(void* out, Imaging im, double xin, double yin)
  326. {
  327. int x = COORD(xin);
  328. int y = COORD(yin);
  329. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
  330. return 0;
  331. ((UINT8*)out)[0] = im->image8[y][x];
  332. return 1;
  333. }
  334. static int
  335. nearest_filter16(void* out, Imaging im, double xin, double yin)
  336. {
  337. int x = COORD(xin);
  338. int y = COORD(yin);
  339. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
  340. return 0;
  341. memcpy(out, im->image8[y] + x * sizeof(INT16), sizeof(INT16));
  342. return 1;
  343. }
  344. static int
  345. nearest_filter32(void* out, Imaging im, double xin, double yin)
  346. {
  347. int x = COORD(xin);
  348. int y = COORD(yin);
  349. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize)
  350. return 0;
  351. memcpy(out, &im->image32[y][x], sizeof(INT32));
  352. return 1;
  353. }
  354. #define XCLIP(im, x) ( ((x) < 0) ? 0 : ((x) < im->xsize) ? (x) : im->xsize-1 )
  355. #define YCLIP(im, y) ( ((y) < 0) ? 0 : ((y) < im->ysize) ? (y) : im->ysize-1 )
  356. #define BILINEAR(v, a, b, d)\
  357. (v = (a) + ( (b) - (a) ) * (d))
  358. #define BILINEAR_HEAD(type)\
  359. int x, y;\
  360. int x0, x1;\
  361. double v1, v2;\
  362. double dx, dy;\
  363. type* in;\
  364. if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize)\
  365. return 0;\
  366. xin -= 0.5;\
  367. yin -= 0.5;\
  368. x = FLOOR(xin);\
  369. y = FLOOR(yin);\
  370. dx = xin - x;\
  371. dy = yin - y;
  372. #define BILINEAR_BODY(type, image, step, offset) {\
  373. in = (type*) ((image)[YCLIP(im, y)] + offset);\
  374. x0 = XCLIP(im, x+0)*step;\
  375. x1 = XCLIP(im, x+1)*step;\
  376. BILINEAR(v1, in[x0], in[x1], dx);\
  377. if (y+1 >= 0 && y+1 < im->ysize) {\
  378. in = (type*) ((image)[y+1] + offset);\
  379. BILINEAR(v2, in[x0], in[x1], dx);\
  380. } else\
  381. v2 = v1;\
  382. BILINEAR(v1, v1, v2, dy);\
  383. }
  384. static int
  385. bilinear_filter8(void* out, Imaging im, double xin, double yin)
  386. {
  387. BILINEAR_HEAD(UINT8);
  388. BILINEAR_BODY(UINT8, im->image8, 1, 0);
  389. ((UINT8*)out)[0] = (UINT8) v1;
  390. return 1;
  391. }
  392. static int
  393. bilinear_filter32I(void* out, Imaging im, double xin, double yin)
  394. {
  395. INT32 k;
  396. BILINEAR_HEAD(INT32);
  397. BILINEAR_BODY(INT32, im->image32, 1, 0);
  398. k = v1;
  399. memcpy(out, &k, sizeof(k));
  400. return 1;
  401. }
  402. static int
  403. bilinear_filter32F(void* out, Imaging im, double xin, double yin)
  404. {
  405. FLOAT32 k;
  406. BILINEAR_HEAD(FLOAT32);
  407. BILINEAR_BODY(FLOAT32, im->image32, 1, 0);
  408. k = v1;
  409. memcpy(out, &k, sizeof(k));
  410. return 1;
  411. }
  412. static int
  413. bilinear_filter32LA(void* out, Imaging im, double xin, double yin)
  414. {
  415. BILINEAR_HEAD(UINT8);
  416. BILINEAR_BODY(UINT8, im->image, 4, 0);
  417. ((UINT8*)out)[0] = (UINT8) v1;
  418. ((UINT8*)out)[1] = (UINT8) v1;
  419. ((UINT8*)out)[2] = (UINT8) v1;
  420. BILINEAR_BODY(UINT8, im->image, 4, 3);
  421. ((UINT8*)out)[3] = (UINT8) v1;
  422. return 1;
  423. }
  424. static int
  425. bilinear_filter32RGB(void* out, Imaging im, double xin, double yin)
  426. {
  427. int b;
  428. BILINEAR_HEAD(UINT8);
  429. for (b = 0; b < im->bands; b++) {
  430. BILINEAR_BODY(UINT8, im->image, 4, b);
  431. ((UINT8*)out)[b] = (UINT8) v1;
  432. }
  433. return 1;
  434. }
  435. #undef BILINEAR
  436. #undef BILINEAR_HEAD
  437. #undef BILINEAR_BODY
  438. #define BICUBIC(v, v1, v2, v3, v4, d) {\
  439. double p1 = v2;\
  440. double p2 = -v1 + v3;\
  441. double p3 = 2*(v1 - v2) + v3 - v4;\
  442. double p4 = -v1 + v2 - v3 + v4;\
  443. v = p1 + (d)*(p2 + (d)*(p3 + (d)*p4));\
  444. }
  445. #define BICUBIC_HEAD(type)\
  446. int x = FLOOR(xin);\
  447. int y = FLOOR(yin);\
  448. int x0, x1, x2, x3;\
  449. double v1, v2, v3, v4;\
  450. double dx, dy;\
  451. type* in;\
  452. if (xin < 0.0 || xin >= im->xsize || yin < 0.0 || yin >= im->ysize)\
  453. return 0;\
  454. xin -= 0.5;\
  455. yin -= 0.5;\
  456. x = FLOOR(xin);\
  457. y = FLOOR(yin);\
  458. dx = xin - x;\
  459. dy = yin - y;\
  460. x--; y--;
  461. #define BICUBIC_BODY(type, image, step, offset) {\
  462. in = (type*) ((image)[YCLIP(im, y)] + offset);\
  463. x0 = XCLIP(im, x+0)*step;\
  464. x1 = XCLIP(im, x+1)*step;\
  465. x2 = XCLIP(im, x+2)*step;\
  466. x3 = XCLIP(im, x+3)*step;\
  467. BICUBIC(v1, in[x0], in[x1], in[x2], in[x3], dx);\
  468. if (y+1 >= 0 && y+1 < im->ysize) {\
  469. in = (type*) ((image)[y+1] + offset);\
  470. BICUBIC(v2, in[x0], in[x1], in[x2], in[x3], dx);\
  471. } else\
  472. v2 = v1;\
  473. if (y+2 >= 0 && y+2 < im->ysize) {\
  474. in = (type*) ((image)[y+2] + offset);\
  475. BICUBIC(v3, in[x0], in[x1], in[x2], in[x3], dx);\
  476. } else\
  477. v3 = v2;\
  478. if (y+3 >= 0 && y+3 < im->ysize) {\
  479. in = (type*) ((image)[y+3] + offset);\
  480. BICUBIC(v4, in[x0], in[x1], in[x2], in[x3], dx);\
  481. } else\
  482. v4 = v3;\
  483. BICUBIC(v1, v1, v2, v3, v4, dy);\
  484. }
  485. static int
  486. bicubic_filter8(void* out, Imaging im, double xin, double yin)
  487. {
  488. BICUBIC_HEAD(UINT8);
  489. BICUBIC_BODY(UINT8, im->image8, 1, 0);
  490. if (v1 <= 0.0)
  491. ((UINT8*)out)[0] = 0;
  492. else if (v1 >= 255.0)
  493. ((UINT8*)out)[0] = 255;
  494. else
  495. ((UINT8*)out)[0] = (UINT8) v1;
  496. return 1;
  497. }
  498. static int
  499. bicubic_filter32I(void* out, Imaging im, double xin, double yin)
  500. {
  501. INT32 k;
  502. BICUBIC_HEAD(INT32);
  503. BICUBIC_BODY(INT32, im->image32, 1, 0);
  504. k = v1;
  505. memcpy(out, &k, sizeof(k));
  506. return 1;
  507. }
  508. static int
  509. bicubic_filter32F(void* out, Imaging im, double xin, double yin)
  510. {
  511. FLOAT32 k;
  512. BICUBIC_HEAD(FLOAT32);
  513. BICUBIC_BODY(FLOAT32, im->image32, 1, 0);
  514. k = v1;
  515. memcpy(out, &k, sizeof(k));
  516. return 1;
  517. }
  518. static int
  519. bicubic_filter32LA(void* out, Imaging im, double xin, double yin)
  520. {
  521. BICUBIC_HEAD(UINT8);
  522. BICUBIC_BODY(UINT8, im->image, 4, 0);
  523. if (v1 <= 0.0) {
  524. ((UINT8*)out)[0] = 0;
  525. ((UINT8*)out)[1] = 0;
  526. ((UINT8*)out)[2] = 0;
  527. } else if (v1 >= 255.0) {
  528. ((UINT8*)out)[0] = 255;
  529. ((UINT8*)out)[1] = 255;
  530. ((UINT8*)out)[2] = 255;
  531. } else {
  532. ((UINT8*)out)[0] = (UINT8) v1;
  533. ((UINT8*)out)[1] = (UINT8) v1;
  534. ((UINT8*)out)[2] = (UINT8) v1;
  535. }
  536. BICUBIC_BODY(UINT8, im->image, 4, 3);
  537. if (v1 <= 0.0)
  538. ((UINT8*)out)[3] = 0;
  539. else if (v1 >= 255.0)
  540. ((UINT8*)out)[3] = 255;
  541. else
  542. ((UINT8*)out)[3] = (UINT8) v1;
  543. return 1;
  544. }
  545. static int
  546. bicubic_filter32RGB(void* out, Imaging im, double xin, double yin)
  547. {
  548. int b;
  549. BICUBIC_HEAD(UINT8);
  550. for (b = 0; b < im->bands; b++) {
  551. BICUBIC_BODY(UINT8, im->image, 4, b);
  552. if (v1 <= 0.0)
  553. ((UINT8*)out)[b] = 0;
  554. else if (v1 >= 255.0)
  555. ((UINT8*)out)[b] = 255;
  556. else
  557. ((UINT8*)out)[b] = (UINT8) v1;
  558. }
  559. return 1;
  560. }
  561. #undef BICUBIC
  562. #undef BICUBIC_HEAD
  563. #undef BICUBIC_BODY
  564. static ImagingTransformFilter
  565. getfilter(Imaging im, int filterid)
  566. {
  567. switch (filterid) {
  568. case IMAGING_TRANSFORM_NEAREST:
  569. if (im->image8)
  570. switch (im->type) {
  571. case IMAGING_TYPE_UINT8:
  572. return nearest_filter8;
  573. case IMAGING_TYPE_SPECIAL:
  574. switch (im->pixelsize) {
  575. case 1:
  576. return nearest_filter8;
  577. case 2:
  578. return nearest_filter16;
  579. case 4:
  580. return nearest_filter32;
  581. }
  582. }
  583. else
  584. return nearest_filter32;
  585. break;
  586. case IMAGING_TRANSFORM_BILINEAR:
  587. if (im->image8)
  588. return bilinear_filter8;
  589. else if (im->image32) {
  590. switch (im->type) {
  591. case IMAGING_TYPE_UINT8:
  592. if (im->bands == 2)
  593. return bilinear_filter32LA;
  594. else
  595. return bilinear_filter32RGB;
  596. case IMAGING_TYPE_INT32:
  597. return bilinear_filter32I;
  598. case IMAGING_TYPE_FLOAT32:
  599. return bilinear_filter32F;
  600. }
  601. }
  602. break;
  603. case IMAGING_TRANSFORM_BICUBIC:
  604. if (im->image8)
  605. return bicubic_filter8;
  606. else if (im->image32) {
  607. switch (im->type) {
  608. case IMAGING_TYPE_UINT8:
  609. if (im->bands == 2)
  610. return bicubic_filter32LA;
  611. else
  612. return bicubic_filter32RGB;
  613. case IMAGING_TYPE_INT32:
  614. return bicubic_filter32I;
  615. case IMAGING_TYPE_FLOAT32:
  616. return bicubic_filter32F;
  617. }
  618. }
  619. break;
  620. }
  621. /* no such filter */
  622. return NULL;
  623. }
  624. /* transformation engines */
  625. Imaging
  626. ImagingGenericTransform(
  627. Imaging imOut, Imaging imIn, int x0, int y0, int x1, int y1,
  628. ImagingTransformMap transform, void* transform_data,
  629. int filterid, int fill)
  630. {
  631. /* slow generic transformation. use ImagingTransformAffine or
  632. ImagingScaleAffine where possible. */
  633. ImagingSectionCookie cookie;
  634. int x, y;
  635. char *out;
  636. double xx, yy;
  637. ImagingTransformFilter filter = getfilter(imIn, filterid);
  638. if (!filter)
  639. return (Imaging) ImagingError_ValueError("bad filter number");
  640. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  641. return (Imaging) ImagingError_ModeError();
  642. ImagingCopyPalette(imOut, imIn);
  643. ImagingSectionEnter(&cookie);
  644. if (x0 < 0)
  645. x0 = 0;
  646. if (y0 < 0)
  647. y0 = 0;
  648. if (x1 > imOut->xsize)
  649. x1 = imOut->xsize;
  650. if (y1 > imOut->ysize)
  651. y1 = imOut->ysize;
  652. for (y = y0; y < y1; y++) {
  653. out = imOut->image[y] + x0*imOut->pixelsize;
  654. for (x = x0; x < x1; x++) {
  655. if ( ! transform(&xx, &yy, x-x0, y-y0, transform_data) ||
  656. ! filter(out, imIn, xx, yy)) {
  657. if (fill)
  658. memset(out, 0, imOut->pixelsize);
  659. }
  660. out += imOut->pixelsize;
  661. }
  662. }
  663. ImagingSectionLeave(&cookie);
  664. return imOut;
  665. }
  666. static Imaging
  667. ImagingScaleAffine(Imaging imOut, Imaging imIn,
  668. int x0, int y0, int x1, int y1,
  669. double a[6], int fill)
  670. {
  671. /* scale, nearest neighbour resampling */
  672. ImagingSectionCookie cookie;
  673. int x, y;
  674. int xin;
  675. double xo, yo;
  676. int xmin, xmax;
  677. int *xintab;
  678. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  679. return (Imaging) ImagingError_ModeError();
  680. ImagingCopyPalette(imOut, imIn);
  681. if (x0 < 0)
  682. x0 = 0;
  683. if (y0 < 0)
  684. y0 = 0;
  685. if (x1 > imOut->xsize)
  686. x1 = imOut->xsize;
  687. if (y1 > imOut->ysize)
  688. y1 = imOut->ysize;
  689. /* malloc check ok, uses calloc for overflow */
  690. xintab = (int*) calloc(imOut->xsize, sizeof(int));
  691. if (!xintab) {
  692. ImagingDelete(imOut);
  693. return (Imaging) ImagingError_MemoryError();
  694. }
  695. xo = a[2] + a[0] * 0.5;
  696. yo = a[5] + a[4] * 0.5;
  697. xmin = x1;
  698. xmax = x0;
  699. /* Pretabulate horizontal pixel positions */
  700. for (x = x0; x < x1; x++) {
  701. xin = COORD(xo);
  702. if (xin >= 0 && xin < (int) imIn->xsize) {
  703. xmax = x+1;
  704. if (x < xmin)
  705. xmin = x;
  706. xintab[x] = xin;
  707. }
  708. xo += a[0];
  709. }
  710. #define AFFINE_SCALE(pixel, image)\
  711. for (y = y0; y < y1; y++) {\
  712. int yi = COORD(yo);\
  713. pixel *in, *out;\
  714. out = imOut->image[y];\
  715. if (fill && x1 > x0)\
  716. memset(out+x0, 0, (x1-x0)*sizeof(pixel));\
  717. if (yi >= 0 && yi < imIn->ysize) {\
  718. in = imIn->image[yi];\
  719. for (x = xmin; x < xmax; x++)\
  720. out[x] = in[xintab[x]];\
  721. }\
  722. yo += a[4];\
  723. }
  724. ImagingSectionEnter(&cookie);
  725. if (imIn->image8) {
  726. AFFINE_SCALE(UINT8, image8);
  727. } else {
  728. AFFINE_SCALE(INT32, image32);
  729. }
  730. ImagingSectionLeave(&cookie);
  731. #undef AFFINE_SCALE
  732. free(xintab);
  733. return imOut;
  734. }
  735. static inline int
  736. check_fixed(double a[6], int x, int y)
  737. {
  738. return (fabs(x*a[0] + y*a[1] + a[2]) < 32768.0 &&
  739. fabs(x*a[3] + y*a[4] + a[5]) < 32768.0);
  740. }
  741. static inline Imaging
  742. affine_fixed(Imaging imOut, Imaging imIn,
  743. int x0, int y0, int x1, int y1,
  744. double a[6], int filterid, int fill)
  745. {
  746. /* affine transform, nearest neighbour resampling, fixed point
  747. arithmetics */
  748. ImagingSectionCookie cookie;
  749. int x, y;
  750. int xin, yin;
  751. int xsize, ysize;
  752. int xx, yy;
  753. int a0, a1, a2, a3, a4, a5;
  754. ImagingCopyPalette(imOut, imIn);
  755. xsize = (int) imIn->xsize;
  756. ysize = (int) imIn->ysize;
  757. /* use 16.16 fixed point arithmetics */
  758. #define FIX(v) FLOOR((v)*65536.0 + 0.5)
  759. a0 = FIX(a[0]); a1 = FIX(a[1]);
  760. a3 = FIX(a[3]); a4 = FIX(a[4]);
  761. a2 = FIX(a[2] + a[0] * 0.5 + a[1] * 0.5);
  762. a5 = FIX(a[5] + a[3] * 0.5 + a[4] * 0.5);
  763. #undef FIX
  764. #define AFFINE_TRANSFORM_FIXED(pixel, image)\
  765. for (y = y0; y < y1; y++) {\
  766. pixel *out;\
  767. xx = a2;\
  768. yy = a5;\
  769. out = imOut->image[y];\
  770. if (fill && x1 > x0)\
  771. memset(out+x0, 0, (x1-x0)*sizeof(pixel));\
  772. for (x = x0; x < x1; x++, out++) {\
  773. xin = xx >> 16;\
  774. if (xin >= 0 && xin < xsize) {\
  775. yin = yy >> 16;\
  776. if (yin >= 0 && yin < ysize)\
  777. *out = imIn->image[yin][xin];\
  778. }\
  779. xx += a0;\
  780. yy += a3;\
  781. }\
  782. a2 += a1;\
  783. a5 += a4;\
  784. }
  785. ImagingSectionEnter(&cookie);
  786. if (imIn->image8)
  787. AFFINE_TRANSFORM_FIXED(UINT8, image8)
  788. else
  789. AFFINE_TRANSFORM_FIXED(INT32, image32)
  790. ImagingSectionLeave(&cookie);
  791. #undef AFFINE_TRANSFORM_FIXED
  792. return imOut;
  793. }
  794. Imaging
  795. ImagingTransformAffine(Imaging imOut, Imaging imIn,
  796. int x0, int y0, int x1, int y1,
  797. double a[6], int filterid, int fill)
  798. {
  799. /* affine transform, nearest neighbour resampling, floating point
  800. arithmetics*/
  801. ImagingSectionCookie cookie;
  802. int x, y;
  803. int xin, yin;
  804. int xsize, ysize;
  805. double xx, yy;
  806. double xo, yo;
  807. if (filterid || imIn->type == IMAGING_TYPE_SPECIAL) {
  808. return ImagingGenericTransform(
  809. imOut, imIn,
  810. x0, y0, x1, y1,
  811. affine_transform, a,
  812. filterid, fill);
  813. }
  814. if (a[1] == 0 && a[3] == 0) {
  815. /* Scaling */
  816. return ImagingScaleAffine(imOut, imIn, x0, y0, x1, y1, a, fill);
  817. }
  818. if (!imOut || !imIn || strcmp(imIn->mode, imOut->mode) != 0)
  819. return (Imaging) ImagingError_ModeError();
  820. if (x0 < 0)
  821. x0 = 0;
  822. if (y0 < 0)
  823. y0 = 0;
  824. if (x1 > imOut->xsize)
  825. x1 = imOut->xsize;
  826. if (y1 > imOut->ysize)
  827. y1 = imOut->ysize;
  828. /* translate all four corners to check if they are within the
  829. range that can be represented by the fixed point arithmetics */
  830. if (check_fixed(a, 0, 0) && check_fixed(a, x1-x0, y1-y0) &&
  831. check_fixed(a, 0, y1-y0) && check_fixed(a, x1-x0, 0))
  832. return affine_fixed(imOut, imIn, x0, y0, x1, y1, a, filterid, fill);
  833. /* FIXME: cannot really think of any reasonable case when the
  834. following code is used. maybe we should fall back on the slow
  835. generic transform engine in this case? */
  836. ImagingCopyPalette(imOut, imIn);
  837. xsize = (int) imIn->xsize;
  838. ysize = (int) imIn->ysize;
  839. xo = a[2] + a[1] * 0.5 + a[0] * 0.5;
  840. yo = a[5] + a[4] * 0.5 + a[3] * 0.5;
  841. #define AFFINE_TRANSFORM(pixel, image)\
  842. for (y = y0; y < y1; y++) {\
  843. pixel *out;\
  844. xx = xo;\
  845. yy = yo;\
  846. out = imOut->image[y];\
  847. if (fill && x1 > x0)\
  848. memset(out+x0, 0, (x1-x0)*sizeof(pixel));\
  849. for (x = x0; x < x1; x++, out++) {\
  850. xin = COORD(xx);\
  851. if (xin >= 0 && xin < xsize) {\
  852. yin = COORD(yy);\
  853. if (yin >= 0 && yin < ysize)\
  854. *out = imIn->image[yin][xin];\
  855. }\
  856. xx += a[0];\
  857. yy += a[3];\
  858. }\
  859. xo += a[1];\
  860. yo += a[4];\
  861. }
  862. ImagingSectionEnter(&cookie);
  863. if (imIn->image8)
  864. AFFINE_TRANSFORM(UINT8, image8)
  865. else
  866. AFFINE_TRANSFORM(INT32, image32)
  867. ImagingSectionLeave(&cookie);
  868. #undef AFFINE_TRANSFORM
  869. return imOut;
  870. }
  871. Imaging
  872. ImagingTransform(Imaging imOut, Imaging imIn, int method,
  873. int x0, int y0, int x1, int y1,
  874. double a[8], int filterid, int fill)
  875. {
  876. ImagingTransformMap transform;
  877. switch(method) {
  878. case IMAGING_TRANSFORM_AFFINE:
  879. return ImagingTransformAffine(
  880. imOut, imIn, x0, y0, x1, y1, a, filterid, fill);
  881. break;
  882. case IMAGING_TRANSFORM_PERSPECTIVE:
  883. transform = perspective_transform;
  884. break;
  885. case IMAGING_TRANSFORM_QUAD:
  886. transform = quad_transform;
  887. break;
  888. default:
  889. return (Imaging) ImagingError_ValueError("bad transform method");
  890. }
  891. return ImagingGenericTransform(
  892. imOut, imIn,
  893. x0, y0, x1, y1,
  894. transform, a, filterid, fill);
  895. }