Compare commits

...

3 Commits

Author SHA1 Message Date
tTh
e5ecb0eb13 cosmetic 2022-09-15 17:47:16 +02:00
tTh
e596e11425 oups! 2022-09-15 16:18:54 +02:00
tTh
42e12974db optimize for speed 2022-09-13 10:32:14 +02:00
4 changed files with 73 additions and 119 deletions

View File

@ -487,7 +487,9 @@ for (y=0; y<im->height; y++)
} }
for (x=0; x<nv; x++) for (x=0; x<nv; x++)
{ {
Image_plotRGB(im, x+im->width - nv, y, r, g, b); (im->Rpix[y])[im->width-x] = r;
(im->Gpix[y])[im->width-x] = g;
(im->Bpix[y])[im->width-x] = b;
} }
} }

View File

@ -20,8 +20,7 @@
echange de deux composantes d'une image RGB echange de deux composantes d'une image RGB
what about swapping with the alpha channel ? what about swapping with the alpha channel ?
*/ */
int int Image_swap_colors(Image_Desc *im, char *cols)
Image_swap_colors(Image_Desc *im, char *cols)
{ {
uint8_t *buffer; uint8_t *buffer;
int y, code; int y, code;
@ -30,15 +29,13 @@ int y, code;
fprintf(stderr, "Echange de couleurs: [%s] %p\n", cols, im); fprintf(stderr, "Echange de couleurs: [%s] %p\n", cols, im);
#endif #endif
if (strlen(cols) != 2) if (strlen(cols) != 2) {
{ fprintf(stderr, "%s: bad control string: '%s'\n", __func__, cols);
fprintf(stderr, "SwapColors: bad control string: '%s'\n", cols);
return INVALID_PARAM; return INVALID_PARAM;
} }
code = 0; code = 0;
switch (cols[0]) switch (cols[0]) {
{
case 'r': case 'R': code |= 0x10; break; case 'r': case 'R': code |= 0x10; break;
case 'g': case 'G': code |= 0x20; break; case 'g': case 'G': code |= 0x20; break;
case 'b': case 'B': code |= 0x30; break; case 'b': case 'B': code |= 0x30; break;
@ -46,8 +43,7 @@ switch (cols[0])
fprintf(stderr, "SwapColors: bad first color '%c'\n", cols[0]); fprintf(stderr, "SwapColors: bad first color '%c'\n", cols[0]);
return INVALID_PARAM; return INVALID_PARAM;
} }
switch (cols[1]) switch (cols[1]) {
{
case 'r': case 'R': code |= 0x1; break; case 'r': case 'R': code |= 0x1; break;
case 'g': case 'G': code |= 0x2; break; case 'g': case 'G': code |= 0x2; break;
case 'b': case 'B': code |= 0x3; break; case 'b': case 'B': code |= 0x3; break;
@ -56,20 +52,17 @@ switch (cols[1])
return INVALID_PARAM; return INVALID_PARAM;
} }
if ( (buffer=(uint8_t *)alloca(im->width)) == NULL ) if ( (buffer=(uint8_t *)alloca(im->width)) == NULL ) {
{ fprintf(stderr, "%s: no mem for buffer\n", __func__);
fprintf(stderr, "SwapColors: no mem for buffer\n");
return BUFFER_NO_MEM; return BUFFER_NO_MEM;
} }
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "SwapColors: code is %02x, buffer at %p\n", code, buffer); fprintf(stderr, "%s: code is %02x, buffer at %p\n", __func__, code, buffer);
#endif #endif
for (y=0; y<im->height; y++) for (y=0; y<im->height; y++) {
{ switch (code) {
switch (code)
{
case 0x12: case 0x21: case 0x12: case 0x21:
memcpy(buffer, im->Rpix[y], im->width); memcpy(buffer, im->Rpix[y], im->width);
memcpy(im->Rpix[y], im->Gpix[y], im->width); memcpy(im->Rpix[y], im->Gpix[y], im->width);
@ -86,7 +79,7 @@ for (y=0; y<im->height; y++)
memcpy(im->Gpix[y], buffer, im->width); memcpy(im->Gpix[y], buffer, im->width);
break; break;
default: default:
fprintf(stderr, "SwapColors: code=%02x, Aie.\n", code); fprintf(stderr, "%s: code=%02x, Aie.\n", __func__, code);
exit(1); exit(1);
} }
} }
@ -101,8 +94,7 @@ return OLL_KORRECT;
fonction de deux parametre [0..1], mais c,a marche fonction de deux parametre [0..1], mais c,a marche
pas terrible, alors je vais faire encore des essais pas terrible, alors je vais faire encore des essais
*/ */
int int Image_color_x(double v1, double v2, int *pr, int *pg, int *pb)
Image_color_x(double v1, double v2, int *pr, int *pg, int *pb)
{ {
double s, c; double s, c;
@ -122,8 +114,7 @@ return OLL_KORRECT;
* Il doit exister quelque part la meme fonction pour ce * Il doit exister quelque part la meme fonction pour ce
* calcul sur une zone rectangulaire d'une image. * calcul sur une zone rectangulaire d'une image.
*/ */
int int Image_couleur_moyenne(Image_Desc *im, int *pr, int *pg, int *pb)
Image_couleur_moyenne(Image_Desc *im, int *pr, int *pg, int *pb)
{ {
int x, y; int x, y;
long sr, sg, sb, surface; long sr, sg, sb, surface;
@ -153,21 +144,19 @@ return OLL_KORRECT;
/* /*
* nouveau 28 juillet 2008 - avenue St Exupery * nouveau 28 juillet 2008 - avenue St Exupery
*/ */
int int Image_saturate(Image_Desc *src, Image_Desc *dst, int sr, int sg, int sb)
Image_saturate(Image_Desc *src, Image_Desc *dst, int sr, int sg, int sb)
{ {
int x, y, r, g, b; int x, y, r, g, b;
int foo; int foo;
int minmax[6]; int minmax[6];
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : %p -> %p\n", __func__, src, dst); fprintf(stderr, ">>> %s ( %p %p %d %d %d )", __func__, src, dst,
fprintf(stderr, " r=%d g=%d b=%d\n", sr, sb, sg); sr, sg, sb);
#endif #endif
if ( (foo=Image_compare_desc(src, dst)) ) if ( (foo=Image_compare_desc(src, dst)) ) {
{ fprintf(stderr, "%s: images are differents %d\n", __func__, foo);
fprintf(stderr, "Image saturate: images are differents %d\n", foo);
return foo; return foo;
} }
@ -176,15 +165,13 @@ foo = Image_minmax_RGB(src, minmax); /* pourquoi faire ce truc ici ? */
fprintf(stderr, " calcul minmax -> %d\n", foo); fprintf(stderr, " calcul minmax -> %d\n", foo);
#endif #endif
for (y=0; y<src->height; y++) for (y=0; y<src->height; y++) {
{
for (x=0; x<src->width; x++) for (x=0; x<src->width; x++)
{ {
/* et il faut faire quoi, exactement ? */ /* et il faut faire quoi, exactement ? */
r = src->Rpix[y][x]; r = src->Rpix[y][x];
g = src->Rpix[y][x]; g = src->Rpix[y][x];
b = src->Rpix[y][x]; b = src->Rpix[y][x];
/* je n'en ai pas la moindre ideee LOL */ /* je n'en ai pas la moindre ideee LOL */
} }
} }
@ -195,27 +182,23 @@ return FUNC_IS_ALPHA;
/* /*
* nouveau 12 mars 2009 - avenue St Exupery, avec du "Gros plant nantais" * nouveau 12 mars 2009 - avenue St Exupery, avec du "Gros plant nantais"
*/ */
int int Image_desaturate(Image_Desc *src, Image_Desc *dst, int sr, int sg, int sb)
Image_desaturate(Image_Desc *src, Image_Desc *dst, int sr, int sg, int sb)
{ {
int x, y, r, g, b; int x, y, r, g, b;
int foo; int foo;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s : %p -> %p : ", __func__, src, dst); fprintf(stderr, ">>> %s ( %p %p %d %d %d )", __func__, src, dst,
fprintf(stderr, " %d %d %d\n", sr, sg, sb); sr, sg, sb);
#endif #endif
if ( (foo=Image_compare_desc(src, dst)) ) if ( (foo=Image_compare_desc(src, dst)) ) {
{ fprintf(stderr, "%s: images are differents %d\n", __func__, foo);
fprintf(stderr, "Image desaturate: images are differents %d\n", foo);
return foo; return foo;
} }
for (y=0; y<src->height; y++) for (y=0; y<src->height; y++) {
{ for (x=0; x<src->width; x++) {
for (x=0; x<src->width; x++)
{
r = src->Rpix[y][x]; r = src->Rpix[y][x];
g = src->Gpix[y][x]; g = src->Gpix[y][x];
b = src->Bpix[y][x]; b = src->Bpix[y][x];
@ -267,9 +250,8 @@ int Image_color_shift_rgb(Image_Desc *s, Image_Desc *d, int rs, int gs, int bs)
int x, y, r, g, b; int x, y, r, g, b;
int foo; int foo;
if ( (foo=Image_compare_desc(s, d)) ) if ( (foo=Image_compare_desc(s, d)) ) {
{ fprintf(stderr, "%s: src & dst are differents (%d)\n", __func__, foo);
fprintf(stderr, "Shift RGB: src & dst are differents (%d)\n", foo);
return foo; return foo;
} }
@ -277,10 +259,8 @@ if ( (foo=Image_compare_desc(s, d)) )
fprintf(stderr, "Shift RGB: %d %d %d\n", rs, gs, bs); fprintf(stderr, "Shift RGB: %d %d %d\n", rs, gs, bs);
#endif #endif
for (y=0; y<s->height; y++) for (y=0; y<s->height; y++) {
{ for (x=0; x<s->width; x++) {
for (x=0; x<s->width; x++)
{
r = (s->Rpix[y][x] + rs) % 256; r = (s->Rpix[y][x] + rs) % 256;
g = (s->Gpix[y][x] + gs) % 256; g = (s->Gpix[y][x] + gs) % 256;
b = (s->Bpix[y][x] + bs) % 256; b = (s->Bpix[y][x] + bs) % 256;
@ -303,8 +283,7 @@ return FUNC_IS_ALPHA;
* 16 Dec 2001: not fully tested, may be bogus :-( * 16 Dec 2001: not fully tested, may be bogus :-(
*/ */
int int Image_colors_recenter_0(Image_Desc *s, Image_Desc *d)
Image_colors_recenter_0(Image_Desc *s, Image_Desc *d)
{ {
int x, y, r, g, b; int x, y, r, g, b;
int or, og, ob; int or, og, ob;
@ -313,10 +292,8 @@ double cr, cg, cb, surface;
fprintf(stderr, "Image_colors_recenter_0 is a work in progress... ymmv.\n"); fprintf(stderr, "Image_colors_recenter_0 is a work in progress... ymmv.\n");
cr = cg = cb = 0.0; cr = cg = cb = 0.0;
for (y=0; y<s->height; y++) for (y=0; y<s->height; y++) {
{ for (x=0; x<s->width; x++) {
for (x=0; x<s->width; x++)
{
Image_getRGB(s, x, y, &r, &g, &b); Image_getRGB(s, x, y, &r, &g, &b);
cr += (double)r; cr += (double)r;
cg += (double)g; cg += (double)g;
@ -335,10 +312,8 @@ og -= 127;
ob -= 127; ob -= 127;
fprintf(stderr, "colors recenter 0: %d %d %d\n", or, og, ob); fprintf(stderr, "colors recenter 0: %d %d %d\n", or, og, ob);
for (y=0; y<s->height; y++) for (y=0; y<s->height; y++) {
{ for (x=0; x<s->width; x++) {
for (x=0; x<s->width; x++)
{
Image_getRGB(s, x, y, &r, &g, &b); Image_getRGB(s, x, y, &r, &g, &b);
r = Image_clamp_pixel(r + or); r = Image_clamp_pixel(r + or);
g = Image_clamp_pixel(g + og); g = Image_clamp_pixel(g + og);
@ -365,8 +340,7 @@ long mindist, pos, dist;
if ( (foo=Image_compare_desc(s, d)) ) return foo; if ( (foo=Image_compare_desc(s, d)) ) return foo;
if ( map->nbre == 0 ) if ( map->nbre == 0 ) {
{
fprintf(stderr, "%s: empty map at %p\n", __func__, map); fprintf(stderr, "%s: empty map at %p\n", __func__, map);
/* /*
* WTF ? we have an empty map ?!? * * WTF ? we have an empty map ?!? *
@ -380,19 +354,15 @@ fprintf(stderr, "Colors 2 Map: %d couleurs dans la map\n", map->nbre);
pos = 0; /* XXX verifier a quoi c,a sert ?! */ pos = 0; /* XXX verifier a quoi c,a sert ?! */
for (y=0; y<s->height; y++) for (y=0; y<s->height; y++) {
{ for (x=0; x<s->width; x++) {
for (x=0; x<s->width; x++)
{
mindist = 99999999; mindist = 99999999;
sr = s->Rpix[y][x]; sr = s->Rpix[y][x];
sg = s->Gpix[y][x]; sg = s->Gpix[y][x];
sb = s->Bpix[y][x]; sb = s->Bpix[y][x];
for (m=0; m<map->nbre; m++) for (m=0; m<map->nbre; m++) {
{ switch (mode) {
switch (mode)
{
case 0: case 0:
dist = CUB(sr-map->red[m]) + dist = CUB(sr-map->red[m]) +
CUB(sg-map->green[m]) + CUB(sg-map->green[m]) +
@ -433,8 +403,7 @@ for (y=0; y<s->height; y++)
return INVALID_PARAM; return INVALID_PARAM;
} }
if (dist < mindist) if (dist < mindist) {
{
pos = m; mindist = dist; pos = m; mindist = dist;
} }
} }
@ -461,8 +430,7 @@ return OLL_KORRECT;
#define B_COEF 114 #define B_COEF 114
#define RGB_DIV (R_COEF+G_COEF+B_COEF) #define RGB_DIV (R_COEF+G_COEF+B_COEF)
int int Image_to_gray_k(Image_Desc *s, Image_Desc *d, int kr, int kg, int kb, int flag)
Image_to_gray_k(Image_Desc *s, Image_Desc *d, int kr, int kg, int kb, int flag)
{ {
int x, y, r, g, b, v, foo; int x, y, r, g, b, v, foo;
int grey, diviseur; int grey, diviseur;
@ -472,8 +440,7 @@ int vmax;
fprintf(stderr, "converting image %p to gray in %p, flag %d\n", s, d, flag); fprintf(stderr, "converting image %p to gray in %p, flag %d\n", s, d, flag);
#endif #endif
if ( (foo=Image_compare_desc(s, d)) ) if ( (foo=Image_compare_desc(s, d)) ) {
{
fprintf(stderr, "To Gray: src & dst are differents (%d)\n", foo); fprintf(stderr, "To Gray: src & dst are differents (%d)\n", foo);
return foo; return foo;
} }
@ -496,14 +463,11 @@ if (diviseur==0) {
} }
vmax = 0; vmax = 0;
for (y=0; y<s->height; y++) for (y=0; y<s->height; y++) {
{ for (x=0; x<s->width; x++) {
for (x=0; x<s->width; x++)
{
r = s->Rpix[y][x]; r = s->Rpix[y][x];
g = s->Gpix[y][x]; g = s->Gpix[y][x];
b = s->Bpix[y][x]; b = s->Bpix[y][x];
grey = (r * kr) + (g * kg) + (b * kb); grey = (r * kr) + (g * kg) + (b * kb);
v = grey / diviseur; v = grey / diviseur;
@ -525,8 +489,7 @@ d->modified = 1;
return OLL_KORRECT; return OLL_KORRECT;
} }
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
int int Image_to_gray(Image_Desc *s, Image_Desc *d, int flag)
Image_to_gray(Image_Desc *s, Image_Desc *d, int flag)
{ {
int foo; int foo;
@ -547,30 +510,25 @@ return foo;
* et on fait quoi si il y a pas assez de couleurs dans la MAP ? * et on fait quoi si il y a pas assez de couleurs dans la MAP ?
* ben, on patche ? * ben, on patche ?
*/ */
int int Image_apply_Map(Image_Desc *src, Image_Desc *dst, RGB_map *map)
Image_apply_Map(Image_Desc *src, Image_Desc *dst, RGB_map *map)
{ {
int x, y, r, g, b; int x, y, r, g, b;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s ( %p %p %p )\n", __func__, src, dst, map); fprintf(stderr, ">>> %s ( %p %p %p )\n", __func__, src, dst, map);
#endif #endif
if (map->nbre == 0) if (map->nbre == 0) {
{
fprintf(stderr, "%s: no colors in palette ?\n", __func__); fprintf(stderr, "%s: no colors in palette ?\n", __func__);
return EMPTY_COL_MAP; return EMPTY_COL_MAP;
} }
if (map->nbre < 255) if (map->nbre < 255) {
{
fprintf(stderr, "%s: only %d cols in palette\n", __func__, map->nbre); fprintf(stderr, "%s: only %d cols in palette\n", __func__, map->nbre);
} }
for (y=0; y<src->height; y++) for (y=0; y<src->height; y++) {
{ for (x=0; x<src->width; x++) {
for (x=0; x<src->width; x++)
{
r = (src->Rpix[y])[x]; r = (src->Rpix[y])[x];
g = (src->Gpix[y])[x]; g = (src->Gpix[y])[x];
b = (src->Bpix[y])[x]; b = (src->Bpix[y])[x];
@ -591,32 +549,26 @@ return OLL_KORRECT;
/*::------------------------------------------------------------------::*/ /*::------------------------------------------------------------------::*/
/* 8 May 2001 /* 8 May 2001
et on fait quoi si il y a pas assez de couleurs dans la MAP ? */ et on fait quoi si il y a pas assez de couleurs dans la MAP ? */
int int Image_gray_Map(Image_Desc *src, Image_Desc *dst, RGB_map *map)
Image_gray_Map(Image_Desc *src, Image_Desc *dst, RGB_map *map)
{ {
int x, y, r, g, b, gray; int x, y, r, g, b, gray;
if (map->nbre == 0) if (map->nbre == 0) {
{
fprintf(stderr, "%s: no colors in palette ?\n", __func__); fprintf(stderr, "%s: no colors in palette ?\n", __func__);
return VERY_STRANGE; return VERY_STRANGE;
} }
if (map->nbre < 255) if (map->nbre < 255) {
{
fprintf(stderr, "%s: only %d cols in palette\n", __func__, map->nbre); fprintf(stderr, "%s: only %d cols in palette\n", __func__, map->nbre);
} }
for (y=0; y<src->height; y++) for (y=0; y<src->height; y++) {
{ for (x=0; x<src->width; x++) {
for (x=0; x<src->width; x++)
{
r = (src->Rpix[y])[x]; r = (src->Rpix[y])[x];
g = (src->Gpix[y])[x]; g = (src->Gpix[y])[x];
b = (src->Bpix[y])[x]; b = (src->Bpix[y])[x];
gray = ((r * R_COEF) + (g * G_COEF) + (b * B_COEF)) / RGB_DIV; gray = ((r * R_COEF) + (g * G_COEF) + (b * B_COEF)) / RGB_DIV;
if (gray<0 || gray>255) if (gray<0 || gray>255) {
{
fprintf(stderr, "%s: GRAY---- %d\n", __func__, gray); fprintf(stderr, "%s: GRAY---- %d\n", __func__, gray);
exit(5); exit(5);
} }
@ -641,7 +593,7 @@ char *cptr, sep[] = " \t,";
int idx; int idx;
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s ( %p %p )\n", __func__, str, ints); fprintf(stderr, ">>> %s ( %p %p )\n", __func__, str, ints);
#endif #endif
idx = 0; idx = 0;
@ -666,7 +618,7 @@ int foo;
int values[4]; int values[4];
#if DEBUG_LEVEL #if DEBUG_LEVEL
fprintf(stderr, "%s ( %p '%s' 0x%x )\n", __func__, ptr, texte, flags); fprintf(stderr, ">>> %s ( %p '%s' 0x%x )\n", __func__, ptr, texte, flags);
#endif #endif
if ( NULL != (envptr=getenv(ENV_DEFAULT_RGBA)) ) { if ( NULL != (envptr=getenv(ENV_DEFAULT_RGBA)) ) {
@ -678,7 +630,7 @@ if ( NULL != (envptr=getenv(ENV_DEFAULT_RGBA)) ) {
ptr->r = values[0]; ptr->r = values[0];
ptr->g = values[1]; ptr->g = values[1];
ptr->b = values[2]; ptr->b = values[2];
ptr->a = values[3]; ptr->a = values[3]; /* wtf ? */
} }
else { else {
fprintf(stderr, "no '%s' env var defined\n", ENV_DEFAULT_RGBA); fprintf(stderr, "no '%s' env var defined\n", ENV_DEFAULT_RGBA);

View File

@ -1,6 +1,6 @@
#!/bin/bash #!/bin/bash
set -e # obort on error set -e # abort on error
cd Lib cd Lib
echo "========== in $PWD" echo "========== in $PWD"