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.

fimgfx.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. FIMGFX
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include "../floatimg.h"
  9. /* --------------------------------------------------------------------- */
  10. int verbosity;
  11. float global_fvalue;
  12. typedef struct {
  13. char *name;
  14. int id;
  15. int nbarg;
  16. int flags;
  17. } Fx;
  18. enum fxid { Fx_cos01=5, Fx_cos010, Fx_pow2, Fx_sqrt, Fx_gray0, Fx_halfsz0,
  19. Fx_rot90, Fx_cmixa,
  20. Fx_xper };
  21. Fx fx_list[] = {
  22. { "cos01", Fx_cos01, 0, 1 },
  23. { "cos010", Fx_cos010, 0, 1 },
  24. { "pow2", Fx_pow2, 0, 1 },
  25. { "sqrt", Fx_sqrt, 0, 1 },
  26. { "gray0", Fx_gray0, 0, 1 },
  27. { "halfsz0", Fx_halfsz0, 0, 1 },
  28. { "rot90", Fx_rot90, 0, 0 },
  29. { "cmixa", Fx_cmixa, 0, 1 },
  30. { "xper", Fx_xper, 0, 1 },
  31. { NULL, 0, 0 }
  32. };
  33. /* --------------------------------------------------------------------- */
  34. int lookup_fxidx(char *txt)
  35. {
  36. Fx *fx;
  37. int n;
  38. #if DEBUG_LEVEL
  39. fprintf(stderr, ">>> %s ( '%s' )\n", __func__, txt);
  40. #endif
  41. for (n=0, fx=fx_list; fx->name; fx++, n++) {
  42. #if DEBUG_LEVEL > 1
  43. fprintf(stderr, " -> %3d %s\n", n, fx->name);
  44. #endif
  45. if (!strcmp(fx->name, txt)) {
  46. return n;
  47. }
  48. }
  49. return -1; /* NOT FOUND */
  50. }
  51. /* --------------------------------------------------------------------- */
  52. int do_experiment(FloatImg *S, FloatImg *D, float kf)
  53. {
  54. int foo;
  55. #if DEBUG_LEVEL
  56. fprintf(stderr, ">>> %s ( %p %p %f )\n", __func__, S, D, kf);
  57. #endif
  58. foo = fimg_copy_data(S, D);
  59. if (foo) {
  60. fprintf(stderr, "%s err %d copy data %p -> %p\n", __func__,
  61. foo, S, D);
  62. return -98;
  63. }
  64. foo = fimg_killcolors_a(D, kf);
  65. if (foo) {
  66. fprintf(stderr, "%s err %d killcolors %p %f\n", __func__,
  67. foo, S, kf);
  68. return -98;
  69. }
  70. return 0;
  71. }
  72. /* --------------------------------------------------------------------- */
  73. static void help(int lvl)
  74. {
  75. Fx *fx;
  76. printf("-- fimg special effects -- %s %s --\n", __DATE__, __TIME__);
  77. puts("usage:");
  78. puts("\tfimgfx [options] <effect> source.fimg resultat.fimg");
  79. puts("options:");
  80. puts("\t-k N.N\tset the floating value");
  81. puts("\t-v\tincrease verbosity");
  82. puts("effects:");
  83. printf("\t");
  84. for (fx=fx_list; fx->name; fx++) {
  85. printf("%s ", fx->name);
  86. }
  87. puts("");
  88. fimg_print_version(1);
  89. exit(0);
  90. }
  91. /* --------------------------------------------------------------------- */
  92. int do_an_effect(char *sf, int fxidx, char *df)
  93. {
  94. FloatImg src, dest;
  95. int foo, action;
  96. double maxval;
  97. #if DEBUG_LEVEL
  98. fprintf(stderr, ">>> %s ( '%s' %d '%s' )\n", __func__,
  99. sf, action, df);
  100. #endif
  101. foo = fimg_create_from_dump(sf, &src);
  102. if (foo) {
  103. fprintf(stderr, "err load '%s' : %d\n", sf, foo);
  104. return foo;
  105. }
  106. maxval = (double)fimg_get_maxvalue(&src);
  107. if (fx_list[fxidx].flags & 1) {
  108. foo = fimg_clone(&src, &dest, 0);
  109. if (foo) {
  110. fprintf(stderr, "err clone %p : %d\n", &src, foo);
  111. return foo;
  112. }
  113. }
  114. else {
  115. /* XXX */
  116. memset(&dest, 0, sizeof(dest));
  117. }
  118. action = fx_list[fxidx].id;
  119. switch (action) {
  120. case Fx_cos01:
  121. fimg_cos_01(&src, &dest, maxval); break;
  122. case Fx_cos010:
  123. fimg_cos_010(&src, &dest, maxval); break;
  124. case Fx_pow2:
  125. fimg_power_2(&src, &dest, maxval); break;
  126. case Fx_sqrt:
  127. fimg_square_root(&src, &dest, maxval); break;
  128. case Fx_gray0: /* new 2020 01 10 */
  129. fimg_to_gray(&src); fimg_copy_data(&src, &dest);
  130. break;
  131. case Fx_xper:
  132. do_experiment(&src, &dest, maxval); break;
  133. case Fx_rot90:
  134. foo = fimg_rotate_90(&src, &dest, 0); break;
  135. case Fx_cmixa:
  136. fimg_copy_data(&src, &dest);
  137. foo = fimg_colors_mixer_a(&dest, 2.0); break;
  138. case Fx_halfsz0:
  139. fprintf(stderr, "halfsize was not implemented\n");
  140. return -3;
  141. default:
  142. fprintf(stderr, "%s %s : %d is bad action\n",
  143. __FILE__, __func__, action);
  144. break;
  145. }
  146. foo = fimg_dump_to_file(&dest, df, 0);
  147. if (foo) {
  148. fprintf(stderr, "dumping datas to file give us a %d\n", foo);
  149. return foo;
  150. }
  151. return 0;
  152. }
  153. /* --------------------------------------------------------------------- */
  154. int main(int argc, char *argv[])
  155. {
  156. int foo, opt, action;
  157. int nba;
  158. char *operator;
  159. char *srcname = "";
  160. char *dstname = "out.fimg";
  161. while ((opt = getopt(argc, argv, "hk:v")) != -1) {
  162. switch(opt) {
  163. case 'h': help(0); break;
  164. case 'k': global_fvalue = atof(optarg); break;
  165. case 'v': verbosity++; break;
  166. }
  167. }
  168. #if DEBUG_LEVEL
  169. fprintf(stderr, "argc %d optind %d\n", argc, optind);
  170. for (foo=0; foo<argc; foo++)
  171. fprintf(stderr, "%3d %c %s\n", foo, foo==optind?'*':' ', argv[foo]);
  172. #endif
  173. if (3 > argc-optind) {
  174. fprintf(stderr, "%s need some arguments...\n", argv[0]);
  175. exit(1);
  176. }
  177. if (verbosity>1) fprintf(stderr, "*** fimgfx *** %s %s\n", __DATE__, __TIME__);
  178. operator = argv[optind];
  179. action = lookup_fxidx(operator);
  180. if (action < 0) {
  181. fprintf(stderr, "garbage found in opcode field : %s\n", operator);
  182. exit(1);
  183. }
  184. if (verbosity) {
  185. fprintf(stderr, " global fvalue %f\n", global_fvalue);
  186. fprintf(stderr, " action %d\n", action);
  187. fprintf(stderr, " verbosity %d\n", verbosity);
  188. }
  189. if ((nba=fx_list[action].nbarg)) {
  190. fprintf(stderr, "action '%s' need %d arg\n", operator, nba);
  191. }
  192. srcname = argv[optind+1];
  193. dstname = argv[optind+2];
  194. if (verbosity) fprintf(stderr, "%s ==> %s\n", srcname, dstname);
  195. foo = do_an_effect(srcname, action, dstname);
  196. if (foo) {
  197. fprintf(stderr, "do an effect -> %d\n", foo);
  198. }
  199. return 0;
  200. }
  201. /* --------------------------------------------------------------------- */