FloatImg/funcs/qsortrgb.c

125 lines
2.6 KiB
C
Raw Normal View History

2020-10-08 20:24:29 +11:00
/*
* qsort_rgb.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "../floatimg.h"
2020-11-09 06:40:30 +11:00
extern int verbosity;
2020-10-08 20:24:29 +11:00
/* --------------------------------------------------------------------- */
/* nouveau 7 octobre 2020, juste avant sonoptic de la pluie craignos */
2020-10-09 10:26:07 +11:00
static int compare_a(const void *p1, const void *p2)
2020-10-08 20:24:29 +11:00
{
return ( *(float *)p1 < *(float *)p2 );
}
2020-10-09 10:26:07 +11:00
int fimg_qsort_rgb_a(FloatImg *psrc, FloatImg *pdst, int notused)
2020-10-08 20:24:29 +11:00
{
int foo, szimg;
#if DEBUG_LEVEL
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, psrc, pdst, notused);
2020-10-09 10:26:07 +11:00
#endif
2020-10-08 20:24:29 +11:00
2020-10-14 19:32:29 +11:00
if (FIMG_TYPE_RGB != psrc->type) {
fprintf(stderr, "%s: bad src type %d\n", __func__, psrc->type);
return -7;
}
if (fimg_images_not_compatible(psrc, pdst)) {
fprintf(stderr, "%s: bad dst type %d\n", __func__, pdst->type);
return -8;
}
2021-01-11 01:17:54 +11:00
if (psrc != pdst) { /* optimize or futurbug ? */
foo = fimg_copy_data(psrc, pdst);
}
2020-10-08 20:24:29 +11:00
szimg = pdst->width * pdst->height;
fprintf(stderr, "%s : %d pixels\n", __func__, szimg);
2020-10-09 10:26:07 +11:00
qsort(pdst->R, szimg, sizeof(float), compare_a);
qsort(pdst->G, szimg, sizeof(float), compare_a);
qsort(pdst->B, szimg, sizeof(float), compare_a);
return 0;
}
/* --------------------------------------------------------------------- */
typedef struct {
float sum;
float r, g, b;
} pix;
2020-10-14 19:32:29 +11:00
static int compare_b(const void *p1, const void *p2)
2020-10-09 10:26:07 +11:00
{
pix *s1, *s2;
s1 = (pix *)p1;
s2 = (pix *)p2;
return ( s1->sum < s2->sum );
}
int fimg_qsort_rgb_b(FloatImg *psrc, FloatImg *pdst, int notused)
{
int x, y, szimg;
pix *ppix, *ptr;
float rgb[3];
2020-10-10 10:26:42 +11:00
#if DEBUG_LEVEL
2020-10-09 10:26:07 +11:00
fprintf(stderr, ">>> %s ( %p %p %d )\n", __func__, psrc, pdst, notused);
2020-10-10 10:26:42 +11:00
#endif
2020-10-09 10:26:07 +11:00
2020-10-14 19:32:29 +11:00
if (FIMG_TYPE_RGB != psrc->type) {
fprintf(stderr, "%s: bad src type %d\n", __func__, psrc->type);
return -7;
}
if (fimg_images_not_compatible(psrc, pdst)) {
fprintf(stderr, "%s: bad dst type %d\n", __func__, pdst->type);
return -8;
}
2020-10-09 10:26:07 +11:00
szimg = pdst->width * pdst->height;
fprintf(stderr, "%s : %d pixels\n", __func__, szimg);
ppix = calloc(szimg, sizeof(pix));
2020-10-10 10:26:42 +11:00
ptr = ppix; /* mobile pointer */
2020-10-09 10:26:07 +11:00
for (y=0; y<psrc->height; y++) {
for (x=0; x<psrc->width; x++) {
fimg_get_rgb(psrc, x, y, rgb);
ptr->sum = rgb[0] + rgb[1] + rgb[2];
ptr->r = rgb[0];
ptr->g = rgb[1];
ptr->b = rgb[2];
ptr++; /* next pixel */
}
}
qsort(ppix, szimg, sizeof(pix), compare_b);
2020-10-10 10:26:42 +11:00
ptr = ppix; /* mobile pointer */
2020-10-09 10:26:07 +11:00
for (y=0; y<psrc->height; y++) {
for (x=0; x<psrc->width; x++) {
rgb[0] = ptr->r;
rgb[1] = ptr->g;
rgb[2] = ptr->b;
fimg_put_rgb(pdst, x, y, rgb);
ptr++; /* next pixel */
}
}
free(ppix);
2020-10-08 20:24:29 +11:00
return 0;
}
/* --------------------------------------------------------------------- */