Bibliothèque de traitements d'images en virgule flottante. http://la.buvette.org/photos/cumul/
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.

323 lines
7.7 KiB

8 months ago
12 months ago
11 months ago
12 months ago
1 year ago
1 year ago
11 months ago
1 year ago
1 year ago
1 year ago
11 months ago
9 months ago
7 months ago
7 months ago
11 months ago
  1. /*
  2. * INTERPOLATOR 2070
  3. *
  4. * +---------------------------------------+
  5. * ! Do not use that software in real life !
  6. * +---------------------------------------+
  7. *
  8. * imported in FloatImg Mon Nov 9 19:08:57 CET 2020
  9. *
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <glob.h>
  17. #include "../floatimg.h"
  18. #include "fifo.h"
  19. #include "glitches.h"
  20. #include "crapulator.h"
  21. #include "metriques.h"
  22. #include "filterstack.h"
  23. int verbosity;
  24. /* -------------------------------------------------------------- */
  25. /* on va essayer de trier l'ordre d'apparition des images
  26. * selon une metrique approximative
  27. */
  28. typedef struct {
  29. int idx; /* in globbuf.gl_pathv[n] */
  30. float value; /* from metric analyse */
  31. } IdxValue;
  32. static int negative = 0;
  33. static int cmp_idxvalues(const void *pa, const void *pb)
  34. {
  35. if (negative)
  36. return ( ((IdxValue *)pa)->value < ((IdxValue *)pb)->value);
  37. else
  38. return ( ((IdxValue *)pa)->value > ((IdxValue *)pb)->value);
  39. }
  40. int tentative_triage(glob_t *ptr_glob, IdxValue **ptr_idxval,
  41. int method, double *p_average)
  42. {
  43. int idx, foo, nombre;
  44. float metrique;
  45. double average;
  46. char *filename;
  47. IdxValue *idxvalues;
  48. #if DEBUG_LEVEL
  49. fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, ptr_glob,
  50. ptr_idxval, method);
  51. #endif
  52. nombre = ptr_glob->gl_pathc;
  53. /* allocate the array for the sorting action */
  54. idxvalues = calloc(nombre, sizeof(IdxValue));
  55. if (NULL==idxvalues) {
  56. fprintf(stderr, "MEMORY ERROR in %s\n", __func__);
  57. exit(1);
  58. }
  59. fprintf(stderr, "IdxValues array at %p\n", idxvalues);
  60. *ptr_idxval = idxvalues;
  61. average = 0.0;
  62. /* compute all the needed values */
  63. for (idx=0; idx<nombre; idx++) {
  64. filename = ptr_glob->gl_pathv[idx];
  65. foo = get_float_metric_from_file(filename, &metrique, method);
  66. if (foo) {
  67. fprintf(stderr, "%s: err %d get metric of '%s'\n",
  68. __func__, foo, filename);
  69. return -1;
  70. }
  71. if (verbosity)
  72. fprintf(stderr, "%5d %s %10.3f\r",
  73. idx, filename, metrique);
  74. idxvalues[idx].idx = idx;
  75. idxvalues[idx].value = metrique;
  76. average += (double)metrique;
  77. }
  78. if (method) {
  79. /* and now, we can massage all our datas */
  80. fprintf(stderr, "sorting method = %d ...\n", method);
  81. qsort(idxvalues, nombre, sizeof(IdxValue), cmp_idxvalues);
  82. }
  83. if (verbosity > 1) {
  84. for (idx=0; idx<nombre; idx++) {
  85. printf("%5d %9.3f %5d\n", idx,
  86. idxvalues[idx].value, idxvalues[idx].idx);
  87. fflush(stdout);
  88. }
  89. }
  90. average /= (double)nombre;
  91. *p_average = average;
  92. fprintf(stderr, "\naverage %f\n", average);
  93. return 0;
  94. }
  95. /* -------------------------------------------------------------- */
  96. /*
  97. * This is the mega working loop
  98. */
  99. int interpolator(char *pattern, char *outdir, int Nsteps,
  100. int infx, int outfx, int sort)
  101. {
  102. FloatImg A, B, Out, *pFirst, *pSecond;
  103. glob_t globbuf;
  104. int foo, idx, ipng, w, h, step;
  105. int curpix;
  106. int iarray[3];
  107. char *cptr, line[200];
  108. float coef, value;
  109. double meanmetric;
  110. IdxValue *idx_values; /* gni? */
  111. fprintf(stderr, " interpolate from '%s' to '%s' with %d steps.\n",
  112. pattern, outdir, Nsteps);
  113. if (negative) fprintf(stderr, "%s: negative ON\n", __func__);
  114. memset(&globbuf, 0, sizeof(glob_t));
  115. foo = glob(pattern, 0, NULL, &globbuf);
  116. fprintf(stderr, "globbing '%s' -> %d, %d files found\n",
  117. pattern, foo, (int)globbuf.gl_pathc);
  118. if (0 == globbuf.gl_pathc) {
  119. fprintf(stderr, "%s : no file found, aborting\n", __func__);
  120. return -1;
  121. }
  122. idx_values = NULL;
  123. foo = tentative_triage(&globbuf, &idx_values, sort, &meanmetric);
  124. if (foo) {
  125. fprintf(stderr, "sort of %p -> %d\n\n", idx_values, foo);
  126. return foo;
  127. }
  128. foo = fimg_fileinfos(globbuf.gl_pathv[0], iarray);
  129. if (FIMG_TYPE_RGB != iarray[2]) {
  130. fprintf(stderr, "can work only on RGB fimg picture, was %d\n",
  131. iarray[2]);
  132. exit(1); /* BLAM! */
  133. }
  134. w = iarray[0], h = iarray[1];
  135. fprintf(stderr, "first image size : %dx%d\n", w, h);
  136. fimg_create(&A, w, h, 3); pFirst = &A; fimg_clear(&A);
  137. fimg_create(&B, w, h, 3); pSecond = &B; fimg_clear(&B);
  138. fimg_create(&Out, w, h, 3);
  139. ipng = 0;
  140. for (idx=0; idx<globbuf.gl_pathc; idx++) {
  141. curpix = idx_values[idx].idx;
  142. cptr = globbuf.gl_pathv[curpix]; /* aliasing filename */
  143. /* read the next file in B */
  144. fprintf(stderr, "%5d / %5d %s\r", idx,
  145. (int)globbuf.gl_pathc, cptr);
  146. foo = fimg_load_from_dump(cptr, &B);
  147. if (foo) {
  148. fprintf(stderr, "load %s from dump -> %d\n", cptr, foo);
  149. continue;
  150. }
  151. value = idx_values[idx].value;
  152. /* here was the input filter */
  153. foo = filterstack_run(0, &B, 0);
  154. if (foo) {
  155. fprintf(stderr, "%s: input fx fail %d\n", __func__, foo);
  156. exit(1);
  157. }
  158. for (step=0; step<Nsteps; step++) {
  159. coef = (float)step / (float)Nsteps;
  160. fimg_interpolate(pSecond, pFirst, &Out, coef);
  161. foo = filterstack_run(1, &Out, 0);
  162. if (foo) {
  163. fprintf(stderr, "run filt stack--> %d\n", foo);
  164. return foo;
  165. }
  166. sprintf(line, "%s/%05d.png", outdir, ipng);
  167. foo = fimg_save_as_png(&Out, line, 0);
  168. if (foo) {
  169. fprintf(stderr, "err saving %s\n", line);
  170. return -8;
  171. }
  172. ipng++;
  173. }
  174. #if 1
  175. /* temporary hack : move datas */
  176. fimg_copy_data(&B, &A);
  177. #else
  178. /* swap pointers to the two picz */
  179. pTmp = pSecond;
  180. pSecond = pFirst;
  181. pFirst = pTmp;
  182. /* XXX THIS CODE DON'T WORK !!! */
  183. #endif
  184. }
  185. fprintf(stderr, "\ngenerated %d png files\n", ipng);
  186. return 0;
  187. }
  188. /* -------------------------------------------------------------- */
  189. void help(void)
  190. {
  191. puts("\tINTERPOLATOR");
  192. puts("usage:\n\tinterpolator [options] <inglob> <outdir> <nbsteep>");
  193. /* may be we can make options incoherent, like
  194. * the options of 'fonderie' software ?
  195. */
  196. puts("options:");
  197. puts("\t-E i:bla:k\tinput filter chain");
  198. puts("\t-F name:j\toutput filter chain");
  199. puts("\t-n\t\tmake negative");
  200. puts("\t-S nn\t\tmysterious sort");
  201. puts("\t-L\t\tlist available filters");
  202. puts("\t-v\t\tincrease verbosity");
  203. if (verbosity) {
  204. list_crapulors("available filters");
  205. }
  206. exit(0);
  207. }
  208. /* -------------------------------------------------------------- */
  209. int main (int argc, char *argv[])
  210. {
  211. int foo;
  212. int nbrsteps = 9;
  213. int opt;
  214. // int inFx = 0;
  215. // int outFx = 0;
  216. int sort = 0;
  217. char *InFchain = "0";
  218. char *OutFchain = "0";
  219. fprintf(stderr, "*** %s : compiled by tTh, %s %s\n", __FILE__,
  220. __DATE__, __TIME__);
  221. fimg_print_version(2);
  222. #if DEBUG_LEVEL
  223. /* this is for the debug off calling shellscript */
  224. for (foo=0; foo<argc; foo++)
  225. fprintf(stderr, "%5d %s\n", foo, argv[foo]);
  226. #endif
  227. while ((opt = getopt(argc, argv, "E:F:hLnS:v")) != -1) {
  228. switch(opt) {
  229. case 'E': InFchain = optarg; break;
  230. case 'F': OutFchain = optarg; break;
  231. case 'h': help(); break;
  232. case 'L':
  233. list_crapulors("available filters");
  234. exit(0);
  235. case 'S': sort = atoi(optarg); break;
  236. case 'v': verbosity++; break;
  237. case 'n': negative = 1; break;
  238. }
  239. }
  240. #if DEBUG_LEVEL
  241. fprintf(stderr, "%s: argc = %d, optind = %d\n", argv[0], argc, optind);
  242. #endif
  243. if (3 != (argc-optind)) {
  244. fprintf(stderr, "args: [options] <inglob> <outdir> <nbsteep>\n");
  245. exit(1);
  246. }
  247. foo = parse_filter_chain(0, InFchain);
  248. if (foo) {
  249. fprintf(stderr, "err %d parsing '%s'\n", foo, InFchain);
  250. exit(1);
  251. }
  252. foo = parse_filter_chain(1, OutFchain);
  253. if (foo) {
  254. fprintf(stderr, "err %d parsing '%s'\n", foo, OutFchain);
  255. exit(1);
  256. }
  257. if (verbosity) {
  258. fprintf(stderr, "\tinput glob '%s'\n", argv[optind]);
  259. fprintf(stderr, "\toutput dir '%s'\n", argv[optind+1]);
  260. fprintf(stderr, "\tsrc filter '%s'\n", InFchain);
  261. fprintf(stderr, "\tout filter '%s'\n", OutFchain);
  262. }
  263. if (verbosity > 1) {
  264. fputs("=========================\n", stderr);
  265. filterstack_list(0, __FILE__);
  266. filterstack_list(1, __FILE__);
  267. fputs("=========================\n", stderr);
  268. }
  269. nbrsteps = atoi(argv[optind+2]);
  270. foo = interpolator(argv[optind], argv[optind+1], nbrsteps,
  271. 0, 0, sort);
  272. fprintf(stderr, "interpolator give us a %d score\n", foo);
  273. return 0;
  274. }
  275. /* -------------------------------------------------------------- */