FloatImg4PythonBinding/experiment/extracteur.c

105 lines
2.0 KiB
C
Raw Normal View History

2021-04-07 20:55:38 +02:00
/*
* another ugly experiment
*/
#include <stdio.h>
#include <stdlib.h>
#include "../floatimg.h"
#include "incrustator.h"
int verbosity;
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
typedef struct {
int w, h;
int x, y;
int flags;
} Rectangle;
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
int print_rectangle(Rectangle *rect)
{
printf("rect @ %p : %dx%d at %d,%d\n", rect, rect->w, rect->h,
rect->x, rect->y);
return 0;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
int essai_extraction(FloatImg *in, FloatImg *out, Rectangle *rect)
{
int foo;
int xs, ys, xd, yd;
int count;
float rgb[3];
fimg_describe(in, "source");
fimg_describe(out, "destination");
print_rectangle(rect);
count = 0;
for (yd=0; yd<rect->h; yd++) {
ys = yd + rect->y;
if ((ys<0) || (ys>=in->height)) continue;
for (xd=0; xd<rect->w; xd++) {
xs = xd + rect->x;
fimg_get_rgb(in, xs, ys, rgb);
fimg_put_rgb(out, xd, yd, rgb);
count++;
}
}
fprintf(stderr, "%s: %d pix moved\n", __func__, count);
return 0;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
int main(int argc, char *argv[])
{
int foo;
FloatImg src, dst;
Rectangle zone;
char *infile = "foo.fimg";
char *outfile = "out.fimg";
verbosity = 1;
if (3 != argc) {
fprintf(stderr, "usage\n\t %s infile.fimg outfile.???\n",
argv[0]);
exit(1);
}
fimg_print_version(1);
infile = argv[1]; outfile = argv[2];
foo = fimg_create_from_dump(infile, &src);
if (foo) {
fprintf(stderr, "%s: err %d loading image '%s'\n", __func__,
foo, infile);
exit(1);
}
zone.w = src.width / 2; zone.h = src.height / 2;
zone.x = src.width / 4 ; zone.y = src.height / 4;
foo = fimg_create(&dst, zone.w, zone.h, FIMG_TYPE_RGB);
foo = essai_extraction(&src, &dst, &zone);
if (foo) {
fprintf(stderr, "EXTRACTOR EPIC FAIL %d\n", foo);
exit(1);
}
foo = fimg_export_picture(&dst, outfile, 0);
return 0;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */