FloatImg4PythonBinding/experiment/extracteur.c

151 lines
3.2 KiB
C
Raw Normal View History

2021-04-07 20:55:38 +02:00
/*
* another ugly experiment
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
2021-04-07 20:55:38 +02:00
#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 parse_rectangle(char *str, Rectangle *r, int notused)
{
int x, y, w, h, foo;
if (verbosity)
fprintf(stderr, "parsing %s\n", str);
foo = sscanf(str, "%d,%d,%d,%d", &w, &h, &x, &y);
if (4 == foo) {
r->x = x, r->y = y, r->w = w, r->h = h;
return 4;
}
return -1;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
2021-04-07 20:55:38 +02:00
int essai_extraction(FloatImg *in, FloatImg *out, Rectangle *rect)
{
int foo;
int xs, ys, xd, yd;
int count;
float rgb[3];
2021-04-07 21:25:15 +02:00
if (verbosity) {
fimg_describe(in, "source");
fimg_describe(out, "destination");
print_rectangle(rect);
}
2021-04-07 20:55:38 +02:00
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;
if ((xs<0) || (xs>=in->width)) continue;
2021-04-07 20:55:38 +02:00
fimg_get_rgb(in, xs, ys, rgb);
fimg_put_rgb(out, xd, yd, rgb);
count++;
}
}
2021-04-07 21:25:15 +02:00
// fprintf(stderr, "%s: %d pix moved\n", __func__, count);
2021-04-07 20:55:38 +02:00
return 0;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
void help(int k)
{
puts("Usage:\n\textracteur in.fimg w,h,x,y out.???");
exit(0);
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */
2021-04-07 20:55:38 +02:00
int main(int argc, char *argv[])
{
int foo, opt;
2021-04-07 20:55:38 +02:00
FloatImg src, dst;
Rectangle zone;
char *infile = "foo.fimg";
char *outfile = "out.fimg";
2021-04-07 21:25:15 +02:00
verbosity = 0;
2021-04-07 20:55:38 +02:00
while ((opt = getopt(argc, argv, "hv")) != -1) {
switch(opt) {
case 'h': help(0); break;
case 'v': verbosity++; break;
default: break;
}
}
if (3 != argc-optind) {
fprintf(stderr, "---- %s errcli %d ----\n", argv[0], argc-optind);
help(1);
2021-04-07 20:55:38 +02:00
exit(1);
}
2021-04-16 12:34:45 +02:00
if (verbosity) {
fprintf(stderr, "*** %s %s %s\n", argv[0], __DATE__, __TIME__);
fimg_print_version(1);
}
2021-04-07 20:55:38 +02:00
infile = argv[optind]; outfile = argv[optind+2];
fprintf(stderr, " %s -> %s\n", infile, outfile);
2021-04-07 20:55:38 +02:00
foo = fimg_create_from_dump(infile, &src);
if (foo) {
fprintf(stderr, "%s: err %d loading image '%s'\n", __func__,
foo, infile);
exit(1);
}
if (4 != parse_rectangle( argv[optind+1], &zone, 0) ) {
fprintf(stderr, "%s: error in parsing of '%s'\n",
argv[0], argv[optind+1]);
exit(1);
}
// zone.w = src.width / 2; zone.h = src.height / 2;
// zone.x = src.width / 4 ; zone.y = src.height / 4;
2021-04-16 12:34:45 +02:00
if (verbosity) print_rectangle(&zone);
2021-04-07 20:55:38 +02:00
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);
if (foo) {
fprintf(stderr, "export '%s' -> err %d\n", outfile, foo);
exit(1);
}
2021-04-07 20:55:38 +02:00
return 0;
}
/* ---------------------------------------------- ~~~~~~~~~~~~~~~~ */