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.

780 lines
22KB

  1. /*
  2. * High quality image resampling with polyphase filters
  3. * Copyright (c) 2001 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file imgresample.c
  21. * High quality image resampling with polyphase filters .
  22. */
  23. #include "avcodec.h"
  24. #include "dsputil.h"
  25. #ifdef USE_FASTMEMCPY
  26. #include "fastmemcpy.h"
  27. #endif
  28. #define NB_COMPONENTS 3
  29. #define PHASE_BITS 4
  30. #define NB_PHASES (1 << PHASE_BITS)
  31. #define NB_TAPS 4
  32. #define FCENTER 1 /* index of the center of the filter */
  33. //#define TEST 1 /* Test it */
  34. #define POS_FRAC_BITS 16
  35. #define POS_FRAC (1 << POS_FRAC_BITS)
  36. /* 6 bits precision is needed for MMX */
  37. #define FILTER_BITS 8
  38. #define LINE_BUF_HEIGHT (NB_TAPS * 4)
  39. struct ImgReSampleContext {
  40. int iwidth, iheight, owidth, oheight, topBand, bottomBand, leftBand, rightBand;
  41. int h_incr, v_incr;
  42. int16_t h_filters[NB_PHASES][NB_TAPS] __align8; /* horizontal filters */
  43. int16_t v_filters[NB_PHASES][NB_TAPS] __align8; /* vertical filters */
  44. uint8_t *line_buf;
  45. };
  46. static inline int get_phase(int pos)
  47. {
  48. return ((pos) >> (POS_FRAC_BITS - PHASE_BITS)) & ((1 << PHASE_BITS) - 1);
  49. }
  50. /* This function must be optimized */
  51. static void h_resample_fast(uint8_t *dst, int dst_width, const uint8_t *src,
  52. int src_width, int src_start, int src_incr,
  53. int16_t *filters)
  54. {
  55. int src_pos, phase, sum, i;
  56. const uint8_t *s;
  57. int16_t *filter;
  58. src_pos = src_start;
  59. for(i=0;i<dst_width;i++) {
  60. #ifdef TEST
  61. /* test */
  62. if ((src_pos >> POS_FRAC_BITS) < 0 ||
  63. (src_pos >> POS_FRAC_BITS) > (src_width - NB_TAPS))
  64. av_abort();
  65. #endif
  66. s = src + (src_pos >> POS_FRAC_BITS);
  67. phase = get_phase(src_pos);
  68. filter = filters + phase * NB_TAPS;
  69. #if NB_TAPS == 4
  70. sum = s[0] * filter[0] +
  71. s[1] * filter[1] +
  72. s[2] * filter[2] +
  73. s[3] * filter[3];
  74. #else
  75. {
  76. int j;
  77. sum = 0;
  78. for(j=0;j<NB_TAPS;j++)
  79. sum += s[j] * filter[j];
  80. }
  81. #endif
  82. sum = sum >> FILTER_BITS;
  83. if (sum < 0)
  84. sum = 0;
  85. else if (sum > 255)
  86. sum = 255;
  87. dst[0] = sum;
  88. src_pos += src_incr;
  89. dst++;
  90. }
  91. }
  92. /* This function must be optimized */
  93. static void v_resample(uint8_t *dst, int dst_width, const uint8_t *src,
  94. int wrap, int16_t *filter)
  95. {
  96. int sum, i;
  97. const uint8_t *s;
  98. s = src;
  99. for(i=0;i<dst_width;i++) {
  100. #if NB_TAPS == 4
  101. sum = s[0 * wrap] * filter[0] +
  102. s[1 * wrap] * filter[1] +
  103. s[2 * wrap] * filter[2] +
  104. s[3 * wrap] * filter[3];
  105. #else
  106. {
  107. int j;
  108. uint8_t *s1 = s;
  109. sum = 0;
  110. for(j=0;j<NB_TAPS;j++) {
  111. sum += s1[0] * filter[j];
  112. s1 += wrap;
  113. }
  114. }
  115. #endif
  116. sum = sum >> FILTER_BITS;
  117. if (sum < 0)
  118. sum = 0;
  119. else if (sum > 255)
  120. sum = 255;
  121. dst[0] = sum;
  122. dst++;
  123. s++;
  124. }
  125. }
  126. #ifdef HAVE_MMX
  127. #include "i386/mmx.h"
  128. #define FILTER4(reg) \
  129. {\
  130. s = src + (src_pos >> POS_FRAC_BITS);\
  131. phase = get_phase(src_pos);\
  132. filter = filters + phase * NB_TAPS;\
  133. movq_m2r(*s, reg);\
  134. punpcklbw_r2r(mm7, reg);\
  135. movq_m2r(*filter, mm6);\
  136. pmaddwd_r2r(reg, mm6);\
  137. movq_r2r(mm6, reg);\
  138. psrlq_i2r(32, reg);\
  139. paddd_r2r(mm6, reg);\
  140. psrad_i2r(FILTER_BITS, reg);\
  141. src_pos += src_incr;\
  142. }
  143. #define DUMP(reg) movq_r2m(reg, tmp); printf(#reg "=%016Lx\n", tmp.uq);
  144. /* XXX: do four pixels at a time */
  145. static void h_resample_fast4_mmx(uint8_t *dst, int dst_width,
  146. const uint8_t *src, int src_width,
  147. int src_start, int src_incr, int16_t *filters)
  148. {
  149. int src_pos, phase;
  150. const uint8_t *s;
  151. int16_t *filter;
  152. mmx_t tmp;
  153. src_pos = src_start;
  154. pxor_r2r(mm7, mm7);
  155. while (dst_width >= 4) {
  156. FILTER4(mm0);
  157. FILTER4(mm1);
  158. FILTER4(mm2);
  159. FILTER4(mm3);
  160. packuswb_r2r(mm7, mm0);
  161. packuswb_r2r(mm7, mm1);
  162. packuswb_r2r(mm7, mm3);
  163. packuswb_r2r(mm7, mm2);
  164. movq_r2m(mm0, tmp);
  165. dst[0] = tmp.ub[0];
  166. movq_r2m(mm1, tmp);
  167. dst[1] = tmp.ub[0];
  168. movq_r2m(mm2, tmp);
  169. dst[2] = tmp.ub[0];
  170. movq_r2m(mm3, tmp);
  171. dst[3] = tmp.ub[0];
  172. dst += 4;
  173. dst_width -= 4;
  174. }
  175. while (dst_width > 0) {
  176. FILTER4(mm0);
  177. packuswb_r2r(mm7, mm0);
  178. movq_r2m(mm0, tmp);
  179. dst[0] = tmp.ub[0];
  180. dst++;
  181. dst_width--;
  182. }
  183. emms();
  184. }
  185. static void v_resample4_mmx(uint8_t *dst, int dst_width, const uint8_t *src,
  186. int wrap, int16_t *filter)
  187. {
  188. int sum, i, v;
  189. const uint8_t *s;
  190. mmx_t tmp;
  191. mmx_t coefs[4];
  192. for(i=0;i<4;i++) {
  193. v = filter[i];
  194. coefs[i].uw[0] = v;
  195. coefs[i].uw[1] = v;
  196. coefs[i].uw[2] = v;
  197. coefs[i].uw[3] = v;
  198. }
  199. pxor_r2r(mm7, mm7);
  200. s = src;
  201. while (dst_width >= 4) {
  202. movq_m2r(s[0 * wrap], mm0);
  203. punpcklbw_r2r(mm7, mm0);
  204. movq_m2r(s[1 * wrap], mm1);
  205. punpcklbw_r2r(mm7, mm1);
  206. movq_m2r(s[2 * wrap], mm2);
  207. punpcklbw_r2r(mm7, mm2);
  208. movq_m2r(s[3 * wrap], mm3);
  209. punpcklbw_r2r(mm7, mm3);
  210. pmullw_m2r(coefs[0], mm0);
  211. pmullw_m2r(coefs[1], mm1);
  212. pmullw_m2r(coefs[2], mm2);
  213. pmullw_m2r(coefs[3], mm3);
  214. paddw_r2r(mm1, mm0);
  215. paddw_r2r(mm3, mm2);
  216. paddw_r2r(mm2, mm0);
  217. psraw_i2r(FILTER_BITS, mm0);
  218. packuswb_r2r(mm7, mm0);
  219. movq_r2m(mm0, tmp);
  220. *(uint32_t *)dst = tmp.ud[0];
  221. dst += 4;
  222. s += 4;
  223. dst_width -= 4;
  224. }
  225. while (dst_width > 0) {
  226. sum = s[0 * wrap] * filter[0] +
  227. s[1 * wrap] * filter[1] +
  228. s[2 * wrap] * filter[2] +
  229. s[3 * wrap] * filter[3];
  230. sum = sum >> FILTER_BITS;
  231. if (sum < 0)
  232. sum = 0;
  233. else if (sum > 255)
  234. sum = 255;
  235. dst[0] = sum;
  236. dst++;
  237. s++;
  238. dst_width--;
  239. }
  240. emms();
  241. }
  242. #endif
  243. #ifdef HAVE_ALTIVEC
  244. typedef union {
  245. vector unsigned char v;
  246. unsigned char c[16];
  247. } vec_uc_t;
  248. typedef union {
  249. vector signed short v;
  250. signed short s[8];
  251. } vec_ss_t;
  252. void v_resample16_altivec(uint8_t *dst, int dst_width, const uint8_t *src,
  253. int wrap, int16_t *filter)
  254. {
  255. int sum, i;
  256. const uint8_t *s;
  257. vector unsigned char *tv, tmp, dstv, zero;
  258. vec_ss_t srchv[4], srclv[4], fv[4];
  259. vector signed short zeros, sumhv, sumlv;
  260. s = src;
  261. for(i=0;i<4;i++)
  262. {
  263. /*
  264. The vec_madds later on does an implicit >>15 on the result.
  265. Since FILTER_BITS is 8, and we have 15 bits of magnitude in
  266. a signed short, we have just enough bits to pre-shift our
  267. filter constants <<7 to compensate for vec_madds.
  268. */
  269. fv[i].s[0] = filter[i] << (15-FILTER_BITS);
  270. fv[i].v = vec_splat(fv[i].v, 0);
  271. }
  272. zero = vec_splat_u8(0);
  273. zeros = vec_splat_s16(0);
  274. /*
  275. When we're resampling, we'd ideally like both our input buffers,
  276. and output buffers to be 16-byte aligned, so we can do both aligned
  277. reads and writes. Sadly we can't always have this at the moment, so
  278. we opt for aligned writes, as unaligned writes have a huge overhead.
  279. To do this, do enough scalar resamples to get dst 16-byte aligned.
  280. */
  281. i = (-(int)dst) & 0xf;
  282. while(i>0) {
  283. sum = s[0 * wrap] * filter[0] +
  284. s[1 * wrap] * filter[1] +
  285. s[2 * wrap] * filter[2] +
  286. s[3 * wrap] * filter[3];
  287. sum = sum >> FILTER_BITS;
  288. if (sum<0) sum = 0; else if (sum>255) sum=255;
  289. dst[0] = sum;
  290. dst++;
  291. s++;
  292. dst_width--;
  293. i--;
  294. }
  295. /* Do our altivec resampling on 16 pixels at once. */
  296. while(dst_width>=16) {
  297. /*
  298. Read 16 (potentially unaligned) bytes from each of
  299. 4 lines into 4 vectors, and split them into shorts.
  300. Interleave the multipy/accumulate for the resample
  301. filter with the loads to hide the 3 cycle latency
  302. the vec_madds have.
  303. */
  304. tv = (vector unsigned char *) &s[0 * wrap];
  305. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[i * wrap]));
  306. srchv[0].v = (vector signed short) vec_mergeh(zero, tmp);
  307. srclv[0].v = (vector signed short) vec_mergel(zero, tmp);
  308. sumhv = vec_madds(srchv[0].v, fv[0].v, zeros);
  309. sumlv = vec_madds(srclv[0].v, fv[0].v, zeros);
  310. tv = (vector unsigned char *) &s[1 * wrap];
  311. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[1 * wrap]));
  312. srchv[1].v = (vector signed short) vec_mergeh(zero, tmp);
  313. srclv[1].v = (vector signed short) vec_mergel(zero, tmp);
  314. sumhv = vec_madds(srchv[1].v, fv[1].v, sumhv);
  315. sumlv = vec_madds(srclv[1].v, fv[1].v, sumlv);
  316. tv = (vector unsigned char *) &s[2 * wrap];
  317. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[2 * wrap]));
  318. srchv[2].v = (vector signed short) vec_mergeh(zero, tmp);
  319. srclv[2].v = (vector signed short) vec_mergel(zero, tmp);
  320. sumhv = vec_madds(srchv[2].v, fv[2].v, sumhv);
  321. sumlv = vec_madds(srclv[2].v, fv[2].v, sumlv);
  322. tv = (vector unsigned char *) &s[3 * wrap];
  323. tmp = vec_perm(tv[0], tv[1], vec_lvsl(0, &s[3 * wrap]));
  324. srchv[3].v = (vector signed short) vec_mergeh(zero, tmp);
  325. srclv[3].v = (vector signed short) vec_mergel(zero, tmp);
  326. sumhv = vec_madds(srchv[3].v, fv[3].v, sumhv);
  327. sumlv = vec_madds(srclv[3].v, fv[3].v, sumlv);
  328. /*
  329. Pack the results into our destination vector,
  330. and do an aligned write of that back to memory.
  331. */
  332. dstv = vec_packsu(sumhv, sumlv) ;
  333. vec_st(dstv, 0, (vector unsigned char *) dst);
  334. dst+=16;
  335. s+=16;
  336. dst_width-=16;
  337. }
  338. /*
  339. If there are any leftover pixels, resample them
  340. with the slow scalar method.
  341. */
  342. while(dst_width>0) {
  343. sum = s[0 * wrap] * filter[0] +
  344. s[1 * wrap] * filter[1] +
  345. s[2 * wrap] * filter[2] +
  346. s[3 * wrap] * filter[3];
  347. sum = sum >> FILTER_BITS;
  348. if (sum<0) sum = 0; else if (sum>255) sum=255;
  349. dst[0] = sum;
  350. dst++;
  351. s++;
  352. dst_width--;
  353. }
  354. }
  355. #endif
  356. /* slow version to handle limit cases. Does not need optimisation */
  357. static void h_resample_slow(uint8_t *dst, int dst_width,
  358. const uint8_t *src, int src_width,
  359. int src_start, int src_incr, int16_t *filters)
  360. {
  361. int src_pos, phase, sum, j, v, i;
  362. const uint8_t *s, *src_end;
  363. int16_t *filter;
  364. src_end = src + src_width;
  365. src_pos = src_start;
  366. for(i=0;i<dst_width;i++) {
  367. s = src + (src_pos >> POS_FRAC_BITS);
  368. phase = get_phase(src_pos);
  369. filter = filters + phase * NB_TAPS;
  370. sum = 0;
  371. for(j=0;j<NB_TAPS;j++) {
  372. if (s < src)
  373. v = src[0];
  374. else if (s >= src_end)
  375. v = src_end[-1];
  376. else
  377. v = s[0];
  378. sum += v * filter[j];
  379. s++;
  380. }
  381. sum = sum >> FILTER_BITS;
  382. if (sum < 0)
  383. sum = 0;
  384. else if (sum > 255)
  385. sum = 255;
  386. dst[0] = sum;
  387. src_pos += src_incr;
  388. dst++;
  389. }
  390. }
  391. static void h_resample(uint8_t *dst, int dst_width, const uint8_t *src,
  392. int src_width, int src_start, int src_incr,
  393. int16_t *filters)
  394. {
  395. int n, src_end;
  396. if (src_start < 0) {
  397. n = (0 - src_start + src_incr - 1) / src_incr;
  398. h_resample_slow(dst, n, src, src_width, src_start, src_incr, filters);
  399. dst += n;
  400. dst_width -= n;
  401. src_start += n * src_incr;
  402. }
  403. src_end = src_start + dst_width * src_incr;
  404. if (src_end > ((src_width - NB_TAPS) << POS_FRAC_BITS)) {
  405. n = (((src_width - NB_TAPS + 1) << POS_FRAC_BITS) - 1 - src_start) /
  406. src_incr;
  407. } else {
  408. n = dst_width;
  409. }
  410. #ifdef HAVE_MMX
  411. if ((mm_flags & MM_MMX) && NB_TAPS == 4)
  412. h_resample_fast4_mmx(dst, n,
  413. src, src_width, src_start, src_incr, filters);
  414. else
  415. #endif
  416. h_resample_fast(dst, n,
  417. src, src_width, src_start, src_incr, filters);
  418. if (n < dst_width) {
  419. dst += n;
  420. dst_width -= n;
  421. src_start += n * src_incr;
  422. h_resample_slow(dst, dst_width,
  423. src, src_width, src_start, src_incr, filters);
  424. }
  425. }
  426. static void component_resample(ImgReSampleContext *s,
  427. uint8_t *output, int owrap, int owidth, int oheight,
  428. uint8_t *input, int iwrap, int iwidth, int iheight)
  429. {
  430. int src_y, src_y1, last_src_y, ring_y, phase_y, y1, y;
  431. uint8_t *new_line, *src_line;
  432. last_src_y = - FCENTER - 1;
  433. /* position of the bottom of the filter in the source image */
  434. src_y = (last_src_y + NB_TAPS) * POS_FRAC;
  435. ring_y = NB_TAPS; /* position in ring buffer */
  436. for(y=0;y<oheight;y++) {
  437. /* apply horizontal filter on new lines from input if needed */
  438. src_y1 = src_y >> POS_FRAC_BITS;
  439. while (last_src_y < src_y1) {
  440. if (++ring_y >= LINE_BUF_HEIGHT + NB_TAPS)
  441. ring_y = NB_TAPS;
  442. last_src_y++;
  443. /* handle limit conditions : replicate line (slightly
  444. inefficient because we filter multiple times) */
  445. y1 = last_src_y;
  446. if (y1 < 0) {
  447. y1 = 0;
  448. } else if (y1 >= iheight) {
  449. y1 = iheight - 1;
  450. }
  451. src_line = input + y1 * iwrap;
  452. new_line = s->line_buf + ring_y * owidth;
  453. /* apply filter and handle limit cases correctly */
  454. h_resample(new_line, owidth,
  455. src_line, iwidth, - FCENTER * POS_FRAC, s->h_incr,
  456. &s->h_filters[0][0]);
  457. /* handle ring buffer wraping */
  458. if (ring_y >= LINE_BUF_HEIGHT) {
  459. memcpy(s->line_buf + (ring_y - LINE_BUF_HEIGHT) * owidth,
  460. new_line, owidth);
  461. }
  462. }
  463. /* apply vertical filter */
  464. phase_y = get_phase(src_y);
  465. #ifdef HAVE_MMX
  466. /* desactivated MMX because loss of precision */
  467. if ((mm_flags & MM_MMX) && NB_TAPS == 4 && 0)
  468. v_resample4_mmx(output, owidth,
  469. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  470. &s->v_filters[phase_y][0]);
  471. else
  472. #endif
  473. #ifdef HAVE_ALTIVEC
  474. if ((mm_flags & MM_ALTIVEC) && NB_TAPS == 4 && FILTER_BITS <= 6)
  475. v_resample16_altivec(output, owidth,
  476. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  477. &s->v_filters[phase_y][0]);
  478. else
  479. #endif
  480. v_resample(output, owidth,
  481. s->line_buf + (ring_y - NB_TAPS + 1) * owidth, owidth,
  482. &s->v_filters[phase_y][0]);
  483. src_y += s->v_incr;
  484. output += owrap;
  485. }
  486. }
  487. /* XXX: the following filter is quite naive, but it seems to suffice
  488. for 4 taps */
  489. static void build_filter(int16_t *filter, float factor)
  490. {
  491. int ph, i, v;
  492. float x, y, tab[NB_TAPS], norm, mult;
  493. /* if upsampling, only need to interpolate, no filter */
  494. if (factor > 1.0)
  495. factor = 1.0;
  496. for(ph=0;ph<NB_PHASES;ph++) {
  497. norm = 0;
  498. for(i=0;i<NB_TAPS;i++) {
  499. x = M_PI * ((float)(i - FCENTER) - (float)ph / NB_PHASES) * factor;
  500. if (x == 0)
  501. y = 1.0;
  502. else
  503. y = sin(x) / x;
  504. tab[i] = y;
  505. norm += y;
  506. }
  507. /* normalize so that an uniform color remains the same */
  508. mult = (float)(1 << FILTER_BITS) / norm;
  509. for(i=0;i<NB_TAPS;i++) {
  510. v = (int)(tab[i] * mult);
  511. filter[ph * NB_TAPS + i] = v;
  512. }
  513. }
  514. }
  515. ImgReSampleContext *img_resample_init(int owidth, int oheight,
  516. int iwidth, int iheight)
  517. {
  518. return img_resample_full_init(owidth, oheight, iwidth, iheight, 0, 0, 0, 0);
  519. }
  520. ImgReSampleContext *img_resample_full_init(int owidth, int oheight,
  521. int iwidth, int iheight,
  522. int topBand, int bottomBand,
  523. int leftBand, int rightBand)
  524. {
  525. ImgReSampleContext *s;
  526. s = av_mallocz(sizeof(ImgReSampleContext));
  527. if (!s)
  528. return NULL;
  529. s->line_buf = av_mallocz(owidth * (LINE_BUF_HEIGHT + NB_TAPS));
  530. if (!s->line_buf)
  531. goto fail;
  532. s->owidth = owidth;
  533. s->oheight = oheight;
  534. s->iwidth = iwidth;
  535. s->iheight = iheight;
  536. s->topBand = topBand;
  537. s->bottomBand = bottomBand;
  538. s->leftBand = leftBand;
  539. s->rightBand = rightBand;
  540. s->h_incr = ((iwidth - leftBand - rightBand) * POS_FRAC) / owidth;
  541. s->v_incr = ((iheight - topBand - bottomBand) * POS_FRAC) / oheight;
  542. build_filter(&s->h_filters[0][0], (float) owidth / (float) (iwidth - leftBand - rightBand));
  543. build_filter(&s->v_filters[0][0], (float) oheight / (float) (iheight - topBand - bottomBand));
  544. return s;
  545. fail:
  546. av_free(s);
  547. return NULL;
  548. }
  549. void img_resample(ImgReSampleContext *s,
  550. AVPicture *output, const AVPicture *input)
  551. {
  552. int i, shift;
  553. for(i=0;i<3;i++) {
  554. shift = (i == 0) ? 0 : 1;
  555. component_resample(s, output->data[i], output->linesize[i],
  556. s->owidth >> shift, s->oheight >> shift,
  557. input->data[i] + (input->linesize[i] * (s->topBand >> shift)) + (s->leftBand >> shift),
  558. input->linesize[i], ((s->iwidth - s->leftBand - s->rightBand) >> shift),
  559. (s->iheight - s->topBand - s->bottomBand) >> shift);
  560. }
  561. }
  562. void img_resample_close(ImgReSampleContext *s)
  563. {
  564. av_free(s->line_buf);
  565. av_free(s);
  566. }
  567. #ifdef TEST
  568. void *av_mallocz(int size)
  569. {
  570. void *ptr;
  571. ptr = malloc(size);
  572. memset(ptr, 0, size);
  573. return ptr;
  574. }
  575. void av_free(void *ptr)
  576. {
  577. /* XXX: this test should not be needed on most libcs */
  578. if (ptr)
  579. free(ptr);
  580. }
  581. /* input */
  582. #define XSIZE 256
  583. #define YSIZE 256
  584. uint8_t img[XSIZE * YSIZE];
  585. /* output */
  586. #define XSIZE1 512
  587. #define YSIZE1 512
  588. uint8_t img1[XSIZE1 * YSIZE1];
  589. uint8_t img2[XSIZE1 * YSIZE1];
  590. void save_pgm(const char *filename, uint8_t *img, int xsize, int ysize)
  591. {
  592. FILE *f;
  593. f=fopen(filename,"w");
  594. fprintf(f,"P5\n%d %d\n%d\n", xsize, ysize, 255);
  595. fwrite(img,1, xsize * ysize,f);
  596. fclose(f);
  597. }
  598. static void dump_filter(int16_t *filter)
  599. {
  600. int i, ph;
  601. for(ph=0;ph<NB_PHASES;ph++) {
  602. printf("%2d: ", ph);
  603. for(i=0;i<NB_TAPS;i++) {
  604. printf(" %5.2f", filter[ph * NB_TAPS + i] / 256.0);
  605. }
  606. printf("\n");
  607. }
  608. }
  609. #ifdef HAVE_MMX
  610. int mm_flags;
  611. #endif
  612. int main(int argc, char **argv)
  613. {
  614. int x, y, v, i, xsize, ysize;
  615. ImgReSampleContext *s;
  616. float fact, factors[] = { 1/2.0, 3.0/4.0, 1.0, 4.0/3.0, 16.0/9.0, 2.0 };
  617. char buf[256];
  618. /* build test image */
  619. for(y=0;y<YSIZE;y++) {
  620. for(x=0;x<XSIZE;x++) {
  621. if (x < XSIZE/2 && y < YSIZE/2) {
  622. if (x < XSIZE/4 && y < YSIZE/4) {
  623. if ((x % 10) <= 6 &&
  624. (y % 10) <= 6)
  625. v = 0xff;
  626. else
  627. v = 0x00;
  628. } else if (x < XSIZE/4) {
  629. if (x & 1)
  630. v = 0xff;
  631. else
  632. v = 0;
  633. } else if (y < XSIZE/4) {
  634. if (y & 1)
  635. v = 0xff;
  636. else
  637. v = 0;
  638. } else {
  639. if (y < YSIZE*3/8) {
  640. if ((y+x) & 1)
  641. v = 0xff;
  642. else
  643. v = 0;
  644. } else {
  645. if (((x+3) % 4) <= 1 &&
  646. ((y+3) % 4) <= 1)
  647. v = 0xff;
  648. else
  649. v = 0x00;
  650. }
  651. }
  652. } else if (x < XSIZE/2) {
  653. v = ((x - (XSIZE/2)) * 255) / (XSIZE/2);
  654. } else if (y < XSIZE/2) {
  655. v = ((y - (XSIZE/2)) * 255) / (XSIZE/2);
  656. } else {
  657. v = ((x + y - XSIZE) * 255) / XSIZE;
  658. }
  659. img[(YSIZE - y) * XSIZE + (XSIZE - x)] = v;
  660. }
  661. }
  662. save_pgm("/tmp/in.pgm", img, XSIZE, YSIZE);
  663. for(i=0;i<sizeof(factors)/sizeof(float);i++) {
  664. fact = factors[i];
  665. xsize = (int)(XSIZE * fact);
  666. ysize = (int)((YSIZE - 100) * fact);
  667. s = img_resample_full_init(xsize, ysize, XSIZE, YSIZE, 50 ,50, 0, 0);
  668. printf("Factor=%0.2f\n", fact);
  669. dump_filter(&s->h_filters[0][0]);
  670. component_resample(s, img1, xsize, xsize, ysize,
  671. img + 50 * XSIZE, XSIZE, XSIZE, YSIZE - 100);
  672. img_resample_close(s);
  673. sprintf(buf, "/tmp/out%d.pgm", i);
  674. save_pgm(buf, img1, xsize, ysize);
  675. }
  676. /* mmx test */
  677. #ifdef HAVE_MMX
  678. printf("MMX test\n");
  679. fact = 0.72;
  680. xsize = (int)(XSIZE * fact);
  681. ysize = (int)(YSIZE * fact);
  682. mm_flags = MM_MMX;
  683. s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
  684. component_resample(s, img1, xsize, xsize, ysize,
  685. img, XSIZE, XSIZE, YSIZE);
  686. mm_flags = 0;
  687. s = img_resample_init(xsize, ysize, XSIZE, YSIZE);
  688. component_resample(s, img2, xsize, xsize, ysize,
  689. img, XSIZE, XSIZE, YSIZE);
  690. if (memcmp(img1, img2, xsize * ysize) != 0) {
  691. fprintf(stderr, "mmx error\n");
  692. exit(1);
  693. }
  694. printf("MMX OK\n");
  695. #endif
  696. return 0;
  697. }
  698. #endif