resample.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <draw.h>
  12. #include <memdraw.h>
  13. #define K2 7 /* from -.7 to +.7 inclusive, meaning .2 into each adjacent pixel */
  14. #define NK (2*K2+1)
  15. double K[NK];
  16. double
  17. fac(int L)
  18. {
  19. int i, f;
  20. f = 1;
  21. for(i=L; i>1; --i)
  22. f *= i;
  23. return f;
  24. }
  25. /*
  26. * i0(x) is the modified Bessel function, Σ (x/2)^2L / (L!)²
  27. * There are faster ways to calculate this, but we precompute
  28. * into a table so let's keep it simple.
  29. */
  30. double
  31. i0(double x)
  32. {
  33. double v;
  34. int L;
  35. v = 1.0;
  36. for(L=1; L<10; L++)
  37. v += pow(x/2., 2*L)/pow(fac(L), 2);
  38. return v;
  39. }
  40. double
  41. kaiser(double x, double τ, double α)
  42. {
  43. if(fabs(x) > τ)
  44. return 0.;
  45. return i0(α*sqrt(1-(x*x/(τ*τ))))/i0(α);
  46. }
  47. void
  48. usage(void)
  49. {
  50. fprint(2, "usage: resample [-x xsize] [-y ysize] [imagefile]\n");
  51. fprint(2, "\twhere size is an integer or a percentage in the form 25%%\n");
  52. exits("usage");
  53. }
  54. int
  55. getint(char *s, int *percent)
  56. {
  57. if(s == nil)
  58. usage();
  59. *percent = (s[strlen(s)-1] == '%');
  60. if(*s == '+')
  61. return atoi(s+1);
  62. if(*s == '-')
  63. return -atoi(s+1);
  64. return atoi(s);
  65. }
  66. void
  67. resamplex(uint8_t *in, int off, int d, int inx, uint8_t *out, int outx)
  68. {
  69. int i, x, k;
  70. double X, xx, v, rat;
  71. rat = (double)inx/(double)outx;
  72. for(x=0; x<outx; x++){
  73. if(inx == outx){
  74. /* don't resample if size unchanged */
  75. out[off+x*d] = in[off+x*d];
  76. continue;
  77. }
  78. v = 0.0;
  79. X = x*rat;
  80. for(k=-K2; k<=K2; k++){
  81. xx = X + rat*k/10.;
  82. i = xx;
  83. if(i < 0)
  84. i = 0;
  85. if(i >= inx)
  86. i = inx-1;
  87. v += in[off+i*d] * K[K2+k];
  88. }
  89. out[off+x*d] = v;
  90. }
  91. }
  92. void
  93. resampley(uint8_t **in, int off, int iny, uint8_t **out, int outy)
  94. {
  95. int y, i, k;
  96. double Y, yy, v, rat;
  97. rat = (double)iny/(double)outy;
  98. for(y=0; y<outy; y++){
  99. if(iny == outy){
  100. /* don't resample if size unchanged */
  101. out[y][off] = in[y][off];
  102. continue;
  103. }
  104. v = 0.0;
  105. Y = y*rat;
  106. for(k=-K2; k<=K2; k++){
  107. yy = Y + rat*k/10.;
  108. i = yy;
  109. if(i < 0)
  110. i = 0;
  111. if(i >= iny)
  112. i = iny-1;
  113. v += in[i][off] * K[K2+k];
  114. }
  115. out[y][off] = v;
  116. }
  117. }
  118. int
  119. max(int a, int b)
  120. {
  121. if(a > b)
  122. return a;
  123. return b;
  124. }
  125. Memimage*
  126. resample(int xsize, int ysize, Memimage *m)
  127. {
  128. int i, j, bpl, nchan;
  129. Memimage *new;
  130. uint8_t **oscan, **nscan;
  131. new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
  132. if(new == nil)
  133. sysfatal("can't allocate new image: %r");
  134. oscan = malloc(Dy(m->r)*sizeof(uint8_t*));
  135. nscan = malloc(max(ysize, Dy(m->r))*sizeof(uint8_t*));
  136. if(oscan == nil || nscan == nil)
  137. sysfatal("can't allocate: %r");
  138. /* unload original image into scan lines */
  139. bpl = bytesperline(m->r, m->depth);
  140. for(i=0; i<Dy(m->r); i++){
  141. oscan[i] = malloc(bpl);
  142. if(oscan[i] == nil)
  143. sysfatal("can't allocate: %r");
  144. j = unloadmemimage(m, Rect(m->r.min.x, m->r.min.y+i, m->r.max.x, m->r.min.y+i+1), oscan[i], bpl);
  145. if(j != bpl)
  146. sysfatal("unloadmemimage");
  147. }
  148. /* allocate scan lines for destination. we do y first, so need at least Dy(m->r) lines */
  149. bpl = bytesperline(Rect(0, 0, xsize, Dy(m->r)), m->depth);
  150. for(i=0; i<max(ysize, Dy(m->r)); i++){
  151. nscan[i] = malloc(bpl);
  152. if(nscan[i] == nil)
  153. sysfatal("can't allocate: %r");
  154. }
  155. /* resample in X */
  156. nchan = m->depth/8;
  157. for(i=0; i<Dy(m->r); i++){
  158. for(j=0; j<nchan; j++){
  159. if(j==0 && m->chan==XRGB32)
  160. continue;
  161. resamplex(oscan[i], j, nchan, Dx(m->r), nscan[i], xsize);
  162. }
  163. free(oscan[i]);
  164. oscan[i] = nscan[i];
  165. nscan[i] = malloc(bpl);
  166. if(nscan[i] == nil)
  167. sysfatal("can't allocate: %r");
  168. }
  169. /* resample in Y */
  170. for(i=0; i<xsize; i++)
  171. for(j=0; j<nchan; j++)
  172. resampley(oscan, nchan*i+j, Dy(m->r), nscan, ysize);
  173. /* pack data into destination */
  174. bpl = bytesperline(new->r, m->depth);
  175. for(i=0; i<ysize; i++){
  176. j = loadmemimage(new, Rect(0, i, xsize, i+1), nscan[i], bpl);
  177. if(j != bpl)
  178. sysfatal("loadmemimage: %r");
  179. }
  180. return new;
  181. }
  182. void
  183. main(int argc, char *argv[])
  184. {
  185. int i, fd, xsize, ysize, xpercent, ypercent;
  186. Rectangle rparam;
  187. Memimage *m, *new, *t1, *t2;
  188. char *file;
  189. ulong tchan;
  190. char tmp[100];
  191. double v;
  192. for(i=-K2; i<=K2; i++){
  193. K[K2+i] = kaiser(i/10., K2/10., 4.);
  194. // print("%g %g\n", i/10., K[K2+i]);
  195. }
  196. /* normalize */
  197. v = 0.0;
  198. for(i=0; i<NK; i++)
  199. v += K[i];
  200. for(i=0; i<NK; i++)
  201. K[i] /= v;
  202. memimageinit();
  203. memset(&rparam, 0, sizeof rparam);
  204. xsize = ysize = 0;
  205. xpercent = ypercent = 0;
  206. ARGBEGIN{
  207. case 'a': /* compatibility; equivalent to just -x or -y */
  208. if(xsize != 0 || ysize != 0)
  209. usage();
  210. xsize = getint(ARGF(), &xpercent);
  211. if(xsize <= 0)
  212. usage();
  213. ysize = xsize;
  214. ypercent = xpercent;
  215. break;
  216. case 'x':
  217. if(xsize != 0)
  218. usage();
  219. xsize = getint(ARGF(), &xpercent);
  220. if(xsize <= 0)
  221. usage();
  222. break;
  223. case 'y':
  224. if(ysize != 0)
  225. usage();
  226. ysize = getint(ARGF(), &ypercent);
  227. if(ysize <= 0)
  228. usage();
  229. break;
  230. default:
  231. usage();
  232. }ARGEND
  233. if(xsize == 0 && ysize == 0)
  234. usage();
  235. file = "<stdin>";
  236. fd = 0;
  237. if(argc > 1)
  238. usage();
  239. else if(argc == 1){
  240. file = argv[0];
  241. fd = open(file, OREAD);
  242. if(fd < 0)
  243. sysfatal("can't open %s: %r", file);
  244. }
  245. m = readmemimage(fd);
  246. if(m == nil)
  247. sysfatal("can't read %s: %r", file);
  248. if(xpercent)
  249. xsize = Dx(m->r)*xsize/100;
  250. if(ypercent)
  251. ysize = Dy(m->r)*ysize/100;
  252. if(ysize == 0)
  253. ysize = (xsize * Dy(m->r)) / Dx(m->r);
  254. if(xsize == 0)
  255. xsize = (ysize * Dx(m->r)) / Dy(m->r);
  256. new = nil;
  257. switch(m->chan){
  258. case GREY8:
  259. case RGB24:
  260. case RGBA32:
  261. case ARGB32:
  262. case XRGB32:
  263. new = resample(xsize, ysize, m);
  264. break;
  265. case CMAP8:
  266. case RGB15:
  267. case RGB16:
  268. tchan = RGB24;
  269. goto Convert;
  270. case GREY1:
  271. case GREY2:
  272. case GREY4:
  273. tchan = GREY8;
  274. Convert:
  275. /* use library to convert to byte-per-chan form, then convert back */
  276. t1 = allocmemimage(m->r, tchan);
  277. if(t1 == nil)
  278. sysfatal("can't allocate temporary image: %r");
  279. memimagedraw(t1, t1->r, m, m->r.min, nil, ZP, S);
  280. t2 = resample(xsize, ysize, t1);
  281. freememimage(t1);
  282. new = allocmemimage(Rect(0, 0, xsize, ysize), m->chan);
  283. if(new == nil)
  284. sysfatal("can't allocate new image: %r");
  285. /* should do error diffusion here */
  286. memimagedraw(new, new->r, t2, t2->r.min, nil, ZP, S);
  287. freememimage(t2);
  288. break;
  289. default:
  290. sysfatal("can't handle channel type %s", chantostr(tmp, m->chan));
  291. }
  292. assert(new);
  293. if(writememimage(1, new) < 0)
  294. sysfatal("write error on output: %r");
  295. exits(nil);
  296. }