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.

fimg-core.c 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * fimg-core.c
  3. *
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include "string.h"
  10. #include "../floatimg.h"
  11. extern int verbosity; /* must be declared around main() */
  12. /* ---------------------------------------------------------------- */
  13. static int fimg_type_is_valid(int t)
  14. {
  15. switch (t) {
  16. case 1: case 3: case 4: return 1;
  17. }
  18. return 0;
  19. }
  20. /* --------------------------------------------------------------------- */
  21. int fimg_print_version(int k)
  22. {
  23. fprintf(stderr, "*** FloatImg library, alpha v%d (%s, %s)\n",
  24. FIMG_VERSION, __DATE__, __TIME__);
  25. if (51 == k) {
  26. puts("+--------------------+");
  27. puts("| Pastis is coming. |");
  28. puts("+--------------------+");
  29. }
  30. return 0;
  31. }
  32. /* --------------------------------------------------------------------- */
  33. void fimg_printhead(FloatImg *h)
  34. {
  35. printf("%5d %5d %2d %p %p %p %p\n", h->width, h->height, h->type,
  36. h->R, h->G, h->B, h->A);
  37. }
  38. /* --------------------------------------------------------------------- */
  39. int fimg_describe(FloatImg *head, char *txt)
  40. {
  41. printf("----- '%s' at %p -----\n", txt, head);
  42. if( ! fimg_type_is_valid(head->type) ) {
  43. fprintf(stderr, "*** %s *** type %d invalid *** %s ***\n",
  44. __func__, head->type, txt);
  45. return -1;
  46. }
  47. printf(" size %d x %d x %d\n",
  48. head->width, head->height, head->type);
  49. printf(" fval/count %f %d\n", head->fval, head->count);
  50. printf(" pixels@ %p %p %p %p\n",
  51. head->R, head->G, head->B, head->A);
  52. return 0;
  53. }
  54. /* ---------------------------------------------------------------- */
  55. int fimg_create(FloatImg *fimg, int w, int h, int t)
  56. {
  57. int surface, size;
  58. float *fptr;
  59. #if DEBUG_LEVEL
  60. fprintf(stderr, ">>> %-25s ( %p %d %d %d )\n", __func__, fimg, w, h, t);
  61. #endif
  62. if ( ! fimg_type_is_valid(t) ) {
  63. return -2;
  64. }
  65. memset(fimg, 0, sizeof(FloatImg));
  66. surface = w * h;
  67. size = surface * t * sizeof(float);
  68. #if DEBUG_LEVEL > 1
  69. fprintf(stderr, "surface is %d pixels, need %d bytes\n", surface, size);
  70. #endif
  71. fptr = (float *)malloc(size);
  72. if (NULL==fptr) {
  73. fprintf(stderr, "%s : no mem, exiting.\n", __func__);
  74. exit(1);
  75. }
  76. #if DEBUG_LEVEL > 1
  77. fprintf(stderr, " got %d bytes at %p\n", size, fptr);
  78. #endif
  79. fimg->width = w; fimg->height = h;
  80. fimg->type = t;
  81. fimg->R = fptr;
  82. if ( (t==3) || (t==4) ) {
  83. fimg->G = fptr + surface;
  84. fimg->B = fptr + surface + surface;
  85. }
  86. if ( t==4 ) fimg->A = fptr + (3 * surface);
  87. return 0;
  88. }
  89. /* --------------------------------------------------------------------- */
  90. int fimg_destroy(FloatImg *fimg)
  91. {
  92. #if DEBUG_LEVEL
  93. fprintf(stderr, ">>> %-25s ( %p )\n", __func__, fimg);
  94. #endif
  95. if (NULL == fimg) {
  96. fprintf(stderr, "%s : parameter is null\n", __func__);
  97. return -1;
  98. }
  99. if ( ! fimg_type_is_valid(fimg->type) ) {
  100. return -2;
  101. }
  102. if (NULL == fimg->R) {
  103. fprintf(stderr, "%s : %p already freeed\n", __func__, fimg);
  104. return -3;
  105. }
  106. free(fimg->R);
  107. memset(fimg, 0, sizeof(FloatImg));
  108. return 0;
  109. }
  110. /* --------------------------------------------------------------------- */
  111. int fimg_clear(FloatImg *fimg)
  112. {
  113. int size;
  114. #if DEBUG_LEVEL
  115. fprintf(stderr, ">>> %-25s ( %p )\n", __func__, fimg);
  116. #endif
  117. if ( ! fimg_type_is_valid(fimg->type) ) {
  118. fprintf(stderr, "invalid type %d in %s\n", fimg->type, __func__);
  119. return -2;
  120. }
  121. size = fimg->width * fimg->height * fimg->type * sizeof(float);
  122. memset(fimg->R, 0, size);
  123. return -1;
  124. }
  125. /* --------------------------------------------------------------------- */
  126. int fimg_plot_rgb (FloatImg *head, int x, int y,
  127. float r, float g, float b)
  128. {
  129. int offset;
  130. if (head->type < 3) {
  131. #if DEBUG_LEVEL > 1
  132. fprintf(stderr, "%s : type %d is bad.\n", __func__, head->type);
  133. #endif
  134. return -1;
  135. }
  136. offset = x + (y * head->width);
  137. #if DEBUG_LEVEL > 1
  138. fprintf(stderr, ">>> %s ( %p %d %d %f )\n", __func__, head, x, y, gray);
  139. fprintf(stderr, " offset %d\n", offset);
  140. #endif
  141. head->R[offset] = r;
  142. head->G[offset] = g;
  143. head->B[offset] = b;
  144. return 0;
  145. }
  146. /* ---------------------------------------------------------------- */
  147. int fimg_add_rgb(FloatImg *head, int x, int y, float r, float g, float b)
  148. {
  149. int offset;
  150. offset = x + (y * head->width);
  151. head->R[offset] += r;
  152. head->G[offset] += g;
  153. head->B[offset] += b;
  154. return 0;
  155. }
  156. /* --------------------------------------------------------------------- */
  157. /* --------------------------------------------------------------------- */