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.

fimgops.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. FIMGOPS
  3. */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include "../floatimg.h"
  9. int verbosity;
  10. /* --------------------------------------------------------------------- */
  11. float global_fvalue;
  12. /* --------------------------------------------------------------------- */
  13. #define OP_ADD 1
  14. #define OP_SUB 2
  15. #define OP_MIX 3
  16. #define OP_MUL 4
  17. #define OP_MINI 5
  18. #define OP_MAXI 6
  19. typedef struct {
  20. int code;
  21. char *op;
  22. } Opcode;
  23. Opcode opcodes[] = {
  24. { OP_ADD, "add" },
  25. { OP_SUB, "sub" },
  26. { OP_MIX, "mix" },
  27. { OP_MUL, "mul" },
  28. { OP_MINI, "mini" },
  29. { OP_MAXI, "maxi" },
  30. { 0, NULL }
  31. };
  32. static void pr_opcodes(void)
  33. {
  34. Opcode *optr;
  35. puts("operators:");
  36. for (optr = opcodes; optr->code; optr++) {
  37. printf("\t%-15s %d\n", optr->op, optr->code);
  38. }
  39. }
  40. static int look_opcode(char *txt)
  41. {
  42. Opcode *optr;
  43. #if DEBUG_LEVEL
  44. fprintf(stderr, ">>> %s ( '%s' )\n", __func__, txt);
  45. #endif
  46. for (optr = opcodes; optr->code; optr++) {
  47. if (!strcmp(txt, optr->op)) {
  48. // printf("found %s as %d\n", optr->op, optr->code);
  49. return optr->code;
  50. }
  51. }
  52. return -1;
  53. }
  54. /* --------------------------------------------------------------------- */
  55. static void help(int lj)
  56. {
  57. puts("usage:\n\tfimgops [options] A.fimg B.fimg operator D.fimg");
  58. puts("options:");
  59. puts("\t-g convert output to gray");
  60. puts("\t-k N.N set float value");
  61. puts("\t-v increase verbosity");
  62. pr_opcodes();
  63. if (verbosity) fimg_print_version(1);
  64. exit(0);
  65. }
  66. /* --------------------------------------------------------------------- */
  67. int exec_operator(FloatImg *A, FloatImg *B, int action, FloatImg *D)
  68. {
  69. int foo;
  70. switch (action) {
  71. case OP_ADD:
  72. foo = fimg_add_3(A, B, D); break;
  73. case OP_SUB:
  74. foo = fimg_sub_3(A, B, D); break;
  75. case OP_MIX:
  76. if (verbosity) fprintf(stderr, "fvalue is %f\n",
  77. global_fvalue);
  78. foo = fimg_interpolate(A, B, D, global_fvalue);
  79. break;
  80. case OP_MUL:
  81. foo = fimg_mul_3(A, B, D); break;
  82. case OP_MINI:
  83. foo = fimg_maximum(A, B, D); break;
  84. case OP_MAXI:
  85. foo = fimg_minimum(A, B, D); break;
  86. default:
  87. foo = -99; break;
  88. }
  89. return foo;
  90. }
  91. /* --------------------------------------------------------------------- */
  92. int main(int argc, char *argv[])
  93. {
  94. int foo, opt, action;
  95. char *operator;
  96. FloatImg srcA, srcB, dest;
  97. while ((opt = getopt(argc, argv, "hk:v")) != -1) {
  98. switch(opt) {
  99. case 'h': help(0); break;
  100. case 'k': global_fvalue = atof(optarg); break;
  101. case 'v': verbosity++; break;
  102. }
  103. }
  104. #if DEBUG_LEVEL
  105. fprintf(stderr, "argc %d optind %d\n", argc, optind);
  106. for (foo=0; foo<argc; foo++)
  107. fprintf(stderr, "%3d %s\n", foo, argv[foo]);
  108. #endif
  109. if (4 != argc-optind) {
  110. fprintf(stderr, "%s need some arguments...\n", argv[0]);
  111. exit(1);
  112. }
  113. operator = argv[optind+2];
  114. action = look_opcode(operator);
  115. if (action < 0) {
  116. fprintf(stderr, "%s : opcode '%s' unknow\n", argv[0], operator);
  117. exit(1);
  118. }
  119. /*
  120. * load the two source files, and check compatibility
  121. */
  122. if ((foo=fimg_create_from_dump(argv[optind], &srcA))) {
  123. fprintf(stderr, "read error on '%s' is %d\n", argv[optind], foo);
  124. exit(2);
  125. }
  126. if ((foo=fimg_create_from_dump(argv[optind+1], &srcB))) {
  127. fprintf(stderr, "read error on '%s' is %d\n", argv[optind+1], foo);
  128. exit(3);
  129. }
  130. if (verbosity) { /* please, debug me */
  131. fimg_describe(&srcA, argv[optind]);
  132. fimg_describe(&srcB, argv[optind+1]);
  133. }
  134. foo = fimg_images_compatible(&srcA, &srcB);
  135. if (foo) {
  136. fprintf(stderr, "images are not compatibles, %d\n", foo);
  137. exit(4);
  138. }
  139. /*
  140. * we can now create the resultant image, and going coredump...
  141. */
  142. foo = fimg_create(&dest, srcA.width, srcA.height, srcA.type);
  143. // fimg_describe(&dest, "destination");
  144. foo = exec_operator(&srcA, &srcB, action, &dest);
  145. if (foo) {
  146. fprintf(stderr, "operator '%s' exec give us a %d\n",
  147. operator, foo);
  148. }
  149. foo = fimg_dump_to_file(&dest, argv[optind+3], 0);
  150. if (foo) {
  151. fprintf(stderr, "dumping datas to file give us a %d\n", foo);
  152. }
  153. return 0;
  154. }
  155. /* --------------------------------------------------------------------- */