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.

385 lines
8.9 KiB

10 months ago
1 year ago
10 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
10 months ago
1 year ago
10 months ago
10 months ago
  1. /*
  2. * SPECIAL EFFECTS
  3. *
  4. * Du code bien cracra / tTh / Tetalab
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <malloc.h>
  10. #include <math.h>
  11. #include "../floatimg.h"
  12. #include "fifo.h"
  13. #include "metriques.h"
  14. #include "sfx.h"
  15. /* -------------------------------------------------------------- */
  16. /* here are global vars exported by the main module
  17. */
  18. extern int verbosity;
  19. /* -------------------------------------------------------------- */
  20. /*
  21. * please, add some parameters !
  22. */
  23. int incrustation_vignette(FloatImg *src, FloatImg *dst, int k)
  24. {
  25. int x, y, x4, y4;
  26. float rgb[3];
  27. #if DEBUG_LEVEL
  28. fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, src, dst, k);
  29. #endif
  30. x4 = dst->width / 4, y4 = dst->height / 4;
  31. for (y=0; y<y4; y++) {
  32. for (x=0; x<x4; x++) {
  33. fimg_get_rgb(src, x*4, y*4, rgb);
  34. fimg_put_rgb(dst, x+39, y+39, rgb);
  35. }
  36. }
  37. return -1;
  38. }
  39. /* -------------------------------------------------------------- */
  40. /* nouveau du premier dimanche de 2020 'nextgen' */
  41. static int pixel_trinitron(FloatImg *pimg, int pos[4], float *fvals)
  42. {
  43. int x, y, pline, off;
  44. for (y=pos[1]; y<pos[1]+pos[3]; y++) {
  45. pline = y*pimg->width;
  46. for (x=pos[0]+2; x<pos[0]+pos[2]-2; x++) {
  47. off = pline + x;
  48. pimg->R[off] = fvals[0];
  49. pimg->G[off] = fvals[1];
  50. pimg->B[off] = fvals[2];
  51. }
  52. }
  53. return 0;
  54. }
  55. int trinitron(FloatImg *pimg, int notused)
  56. {
  57. int x, y, coo[4], foo;
  58. float vals[3];
  59. #if DEBUG_LEVEL
  60. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, notused);
  61. #endif
  62. #define STP 16 /* stepd for x & y axex */
  63. coo[2] = coo[3] = STP;
  64. for (y=0; y<pimg->height; y+=STP) {
  65. coo[1] = y;
  66. for (x=0; x<pimg->width; x+=STP) {
  67. coo[0] = x;
  68. foo = stat_zone(pimg, coo, vals);
  69. if (foo) abort();
  70. /* next step : plot the datas */
  71. pixel_trinitron(pimg, coo, vals);
  72. }
  73. }
  74. #undef STP
  75. return 0;
  76. }
  77. /* -------------------------------------------------------------- */
  78. /* nouveau du 27 decembre 2020, un soir de grand froid... */
  79. int octotree_classif(FloatImg *pimg, float kdist, int notused)
  80. {
  81. int foo;
  82. float mm[6], delta[3];
  83. float r, g, b, kr, kg, kb, dp, trig;
  84. int idx, sz, n8, count;
  85. typedef struct {
  86. float x, y, z;
  87. } ptc_t;
  88. ptc_t ptc[8];
  89. #if DEBUG_LEVEL
  90. fprintf(stderr, ">>> %s ( %p %f %d )\n", __func__,
  91. pimg, kdist, notused);
  92. #endif
  93. foo = fimg_get_minmax_rgb(pimg, mm);
  94. if (foo) {
  95. fprintf(stderr, "oups %d in get minmax\n", foo);
  96. return foo;
  97. }
  98. if (verbosity>1) fimg_print_minmax(mm, " input pic ");
  99. /*
  100. * compute the 8 center points
  101. */
  102. delta[0] = mm[1] - mm[0]; /* R */
  103. delta[1] = mm[3] - mm[2]; /* G */
  104. delta[2] = mm[5] - mm[4]; /* B */
  105. // fprintf(stderr, "delta: %11.3f %11.3f %11.3f\n",
  106. // delta[0], delta[1], delta[2]);
  107. for (idx=0; idx<8; idx++) {
  108. kr = 0.25 * ((idx & 0x4) ? 1 : 3);
  109. kg = 0.25 * ((idx & 0x2) ? 1 : 3);
  110. kb = 0.25 * ((idx & 0x1) ? 1 : 3);
  111. // fprintf(stderr, "%6d %.2f %.2f %.2f\n", idx, kr, kg, kb);
  112. ptc[idx].x = (delta[0] * kr) + mm[0];
  113. ptc[idx].y = (delta[1] * kg) + mm[2];
  114. ptc[idx].z = (delta[2] * kb) + mm[4];
  115. // fprintf(stderr, "%6d %.3f %.3f %.3f\n", idx,
  116. // ptc[idx].x, ptc[idx].y, ptc[idx].z);
  117. }
  118. sz = pimg->width * pimg->height;
  119. trig = kdist * ((mm[1] + mm[3] + mm[5])/6.0);
  120. // fprintf(stderr, "trig value %f\n", trig);
  121. count = 0;
  122. #define X(a,b) ( ((a)-(b)) * ((a)-(b)) )
  123. for (idx=0; idx<sz; idx++) {
  124. r = pimg->R[idx]; g = pimg->G[idx]; b = pimg->B[idx];
  125. for (n8=0; n8<8; n8++) {
  126. dp = sqrt(X(r,ptc[n8].x)+X(g,ptc[n8].y)+X(b,ptc[n8].z));
  127. if (dp < trig) {
  128. pimg->R[idx] = ptc[n8].x;
  129. pimg->G[idx] = ptc[n8].y;
  130. pimg->B[idx] = ptc[n8].z;
  131. count++;
  132. break;
  133. }
  134. else {
  135. pimg->R[idx]=pimg->G[idx]=pimg->B[idx]=0.0;
  136. }
  137. }
  138. }
  139. if (verbosity > 1) {
  140. fprintf(stderr, "%s: %d/%d pixels, ratio %f\n", __func__, count, sz,
  141. (float)count/(float)sz);
  142. }
  143. return 0;
  144. }
  145. /* -------------------------------------------------------------- */
  146. /* nouveau du 19 decembre 2020, pour le grand ecran de da Scritch */
  147. int upside_down(FloatImg *pimg)
  148. {
  149. float *rowpix;
  150. float *Ps, *Pd;
  151. int Os, Od; /* offset of lines */
  152. int wsz;
  153. int ya, y2;
  154. if (verbosity>1) fprintf(stderr, "%s: image width is %d\n",
  155. __func__, pimg->width);
  156. rowpix = calloc(pimg->width, sizeof(float));
  157. if (NULL==rowpix) {
  158. fprintf(stderr, "%s : memory full\n", __func__);
  159. exit(1);
  160. }
  161. wsz = pimg->width * sizeof(float);
  162. if (verbosity>1) fprintf(stderr, "%s: wsx = %d\n", __func__, wsz);
  163. for (ya=0; ya<pimg->height/2; ya++) {
  164. y2 = pimg->height - (ya+1);
  165. Os = (pimg->width * ya);
  166. Od = (pimg->width * y2);
  167. /* let's go, crash coredumping... */
  168. Ps = pimg->R + Os;
  169. Pd = pimg->R + Od;
  170. memcpy(rowpix, Ps, wsz);
  171. memcpy(Ps, Pd, wsz);
  172. memcpy(Pd, rowpix, wsz);
  173. Ps = pimg->G + Os;
  174. Pd = pimg->G + Od;
  175. memcpy(rowpix, Ps, wsz);
  176. memcpy(Ps, Pd, wsz);
  177. memcpy(Pd, rowpix, wsz);
  178. Ps = pimg->B + Os;
  179. Pd = pimg->B + Od;
  180. memcpy(rowpix, Ps, wsz);
  181. memcpy(Ps, Pd, wsz);
  182. memcpy(Pd, rowpix, wsz);
  183. }
  184. free(rowpix);
  185. return 0;
  186. }
  187. /* -------------------------------------------------------------- */
  188. /* nouveau du 9 decembre 2020, en ecoutant le Fermion raconter du
  189. superbe portnawak */
  190. int bouger_les_pixels(FloatImg *pimg, int intensite)
  191. {
  192. int x, y, nx, ny;
  193. float rgb[3];
  194. #if DEBUG_LEVEL
  195. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, intensite);
  196. #endif
  197. if (intensite < 1) {
  198. fprintf(stderr, "%s: %d bad intensity\n", __func__, intensite);
  199. return -1;
  200. }
  201. for (x=0; x<pimg->width; x++) {
  202. for (y=0; y<pimg->height; y++) {
  203. nx = x+(rand()%intensite)-(intensite/2);
  204. ny = y+(rand()%intensite)-(intensite/2);
  205. if ( nx<0 || ny<0 || nx>=pimg->width
  206. || ny>=pimg->height )
  207. continue;
  208. /* XXX optimize here ? */
  209. fimg_get_rgb(pimg, nx, ny, rgb);
  210. fimg_put_rgb(pimg, x, y, rgb);
  211. }
  212. }
  213. return 0;
  214. }
  215. /* -------------------------------------------------------------- */
  216. /* nouveau du 9 decembre 2020, en ecoutant les Cernettes */
  217. int mirror_split(FloatImg *pimg, int kaboo)
  218. {
  219. int line, x, xs, xd;
  220. #if DEBUG_LEVEL
  221. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, kaboo);
  222. #endif
  223. for (line=0; line<pimg->height; line++) {
  224. for (x=0; x<pimg->width/2; x++) {
  225. xs = (pimg->width * line) + x;
  226. xd = (pimg->width * line) + (pimg->width -x);
  227. pimg->R[xd] = pimg->R[xs];
  228. pimg->G[xd] = pimg->G[xs];
  229. pimg->B[xd] = pimg->B[xs];
  230. }
  231. }
  232. return 0;
  233. }
  234. /* -------------------------------------------------------------- */
  235. /* nouveau du 20 novembre 2020, pour encoder une vidz du vernissage
  236. * du festival Sauvageonnes de Mixart-Myrys */
  237. int des_bords_sombres_a(FloatImg *pimg, int offset)
  238. {
  239. float coef;
  240. int xpos, xp2, lidx, y;
  241. #if DEBUG_LEVEL
  242. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, offset);
  243. #endif
  244. if (offset<0 || offset>=pimg->width) {
  245. fprintf(stderr, "%s: offset %d is bad\n", __func__, offset);
  246. return -66;
  247. }
  248. for (y=0; y<pimg->height; y++) {
  249. lidx = y * pimg->width; /* start of the
  250. 'y' line */
  251. for (xpos=0; xpos<offset; xpos++) {
  252. coef = (float)xpos / (float)offset;
  253. pimg->R[xpos+lidx] *= coef;
  254. pimg->G[xpos+lidx] *= coef;
  255. pimg->B[xpos+lidx] *= coef;
  256. xp2 = pimg->width-xpos;
  257. pimg->R[xp2+lidx] *= coef;
  258. pimg->G[xp2+lidx] *= coef;
  259. pimg->B[xp2+lidx] *= coef;
  260. }
  261. }
  262. return 0;
  263. }
  264. /* -------------------------------------------------------------- */
  265. /* nouveau Mon 10 May 2021 08:46:02 PM CEST
  266. * chez Eric 1KA */
  267. int des_bords_sombres_b(FloatImg *pimg, int offset)
  268. {
  269. #if DEBUG_LEVEL
  270. fprintf(stderr, ">>> %s ( %p %d )\n", __func__, pimg, offset);
  271. #endif
  272. if (offset<0 || offset>=pimg->width) {
  273. fprintf(stderr, "%s: offset %d is bad\n", __func__, offset);
  274. return -66;
  275. }
  276. return -1;
  277. }
  278. /* -------------------------------------------------------------- */
  279. /*
  280. * int binarize(FloatImg *pimg, int notused) was now in
  281. * funcs/sfx2.c
  282. * same move for trinarize.
  283. */
  284. /* -------------------------------------------------------------- */
  285. int brotche_rand48_a(FloatImg *fimg, float ratio, float mval)
  286. {
  287. int nbpix, todo, foo;
  288. int x, y;
  289. float fval;
  290. nbpix = fimg->width * fimg->height;
  291. todo = (int)((float)nbpix * ratio);
  292. if (verbosity > 1) {
  293. fprintf(stderr, "%s: ratio %f nbpix %d todo %d\n", __func__,
  294. ratio, nbpix, todo);
  295. }
  296. for (foo=0; foo<todo; foo++)
  297. {
  298. fval = (float)drand48() * mval;
  299. x = rand() % fimg->width;
  300. y = rand() % fimg->height;
  301. fimg_plot_rgb(fimg, x, y, fval, fval, fval);
  302. }
  303. return 0;
  304. }
  305. /* -------------------------------------------------------------- */
  306. int brotche_rand48_b(FloatImg *fimg, float ratio, float mval)
  307. {
  308. int nbpix, todo, foo;
  309. int x, y;
  310. float fval;
  311. nbpix = fimg->width * fimg->height;
  312. todo = (int)((float)nbpix * ratio);
  313. if (verbosity > 1) {
  314. fprintf(stderr, "%s: ratio %f nbpix %d todo %d\n", __func__,
  315. ratio, nbpix, todo);
  316. }
  317. for (foo=0; foo<todo; foo++)
  318. {
  319. fval = (float)drand48() * mval;
  320. x = 1 + (rand() % (fimg->width-2));
  321. y = rand() % fimg->height;
  322. fimg_plot_rgb(fimg, x-1, y, fval, 0.0, 0.0);
  323. fimg_plot_rgb(fimg, x , y, 0.0, 0.0, fval);
  324. fimg_plot_rgb(fimg, x+1, y, 0.0, fval, 0.0);
  325. }
  326. return 0;
  327. }
  328. /* -------------------------------------------------------------- */