libtthimage/Tools/genplot2.c

234 lines
5.1 KiB
C
Raw Normal View History

2022-06-27 23:39:52 +02:00
/*
Bon, c'est tres bien tout c,a mais il faudrait
faire une page de man pour expliquer comment
ce machin fonctionne...
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
2022-06-27 23:39:52 +02:00
#include "tga_outils.h"
#define XMIN 0
#define YMIN 0
2022-11-08 01:39:58 +01:00
#define XMAX 800 /* constant from a 'dddd' */
#define YMAX 600 /* constant from a 'dddd' */
2022-06-27 23:39:52 +02:00
/*::------------------------------------------------------------------::*/
static Image_Desc *image;
static RGB_map map;
static int curX, curY;
2022-12-19 13:54:39 +01:00
static int verbeux;
2022-06-27 23:39:52 +02:00
/*::------------------------------------------------------------------::*/
int initgr(int largeur, int hauteur)
{
int foo, r, v, b, dummy;
2022-09-13 00:11:53 +02:00
if (NULL==(image=Image_alloc(largeur, hauteur, 3))) {
2022-06-27 23:39:52 +02:00
fprintf(stderr, "hu hu, 'man addswap' :)\n");
exit(3);
}
2022-12-19 13:54:39 +01:00
if (verbeux) fprintf(stderr, "Genplot2: initgr %d %d\n", largeur, hauteur);
2022-09-13 00:11:53 +02:00
for (foo=0; foo<8; foo++) {
#if DEBUG_LEVEL
2022-06-27 23:39:52 +02:00
printf("\tPal(%d) = ", foo);
2022-09-13 00:11:53 +02:00
#endif
2022-06-27 23:39:52 +02:00
r = foo & 1 ? 255 : 0;
v = foo & 2 ? 255 : 0;
b = foo & 4 ? 255 : 0;
2022-09-13 00:11:53 +02:00
#if DEBUG_LEVEL
2022-06-27 23:39:52 +02:00
printf("%02X %02X %02X\n", r, v, b);
2022-09-13 00:11:53 +02:00
#endif
2022-06-27 23:39:52 +02:00
map.red[foo] = r;
map.green[foo] = v;
map.blue[foo] = b;
}
map.nbre = 8;
return 0;
}
/*::------------------------------------------------------------------::*/
int move(int x, int y)
{
#if DEBUG_LEVEL
2022-09-13 00:11:53 +02:00
fprintf(stderr, "\tMOVE %5d %5d\n", x, y);
2022-06-27 23:39:52 +02:00
#endif
curX = x; curY = y;
return 0;
}
2022-12-19 13:54:39 +01:00
/*::------------------------------------------------------------------::*/
2022-06-27 23:39:52 +02:00
int draw(int x, int y, int color)
{
RGBA rgba;
int idx;
#if DEBUG_LEVEL
fprintf(stderr, "\tdraw %5d %5d to %5d %5d\n", curX, curY, x, y);
2022-06-27 23:39:52 +02:00
#endif
idx = color % 8;
if (idx != color) {
fprintf(stderr, "%s %s : color %d out of range\n",
__FILE__, __func__, color);
}
rgba.r = map.red[color];
rgba.g = map.green[color];
rgba.b = map.blue[color];
2022-12-19 13:54:39 +01:00
/* new Mon 19 Dec 2022 12:53:32 PM CET
check for special cases: eg horizontal lines
*/
if (y == curY) {
Image_H_line(image, curX, x, y, &rgba);
}
else {
Image_draw_line(image, curX, curY, x, y, &rgba);
}
2022-06-27 23:39:52 +02:00
curX = x; curY = y;
return 0;
}
/*::------------------------------------------------------------------::*/
int endgr(char *filename)
{
2022-12-19 13:54:39 +01:00
if (verbeux) fprintf(stderr, "genplot2 is saving to '%s'\n", filename);
2022-06-27 23:39:52 +02:00
Image_TGA_save(filename, image, 0);
return 0;
}
/*::------------------------------------------------------------------::*/
void help_me(char *proggy)
{
fprintf(stderr, "%s build %s %s\n", proggy, __DATE__, __TIME__);
puts("options:");
puts("\t-v\t\tdo some blablage");
puts("\t-s WxH\t\tset image size");
}
/*::------------------------------------------------------------------::*/
2022-06-27 23:39:52 +02:00
int main(int argc, char *argv[])
{
char *filename, *image;
FILE *fp;
int outw, outh; /* size of output pic */
2022-06-27 23:39:52 +02:00
double x, y, xmin, ymin, xmax, ymax;
double fx, fy, f, X, Y;
double xC, yC, XC, YC, c1, c2;
int v, nbp, opt, foo;
/* setting some default values */
2022-06-27 23:39:52 +02:00
outw = XMAX; outh = YMAX;
filename = "a.scratch"; image = "image.tga";
2022-06-27 23:39:52 +02:00
/*---------- processing command line arguments */
while ((opt = getopt(argc, argv, "hs:v")) != -1) {
switch (opt) {
case 'h':
help_me(argv[0]);
exit(0);
case 's': /* size of output pic */
foo = parse_size_param(optarg, &outw, &outh);
break;
case 'v':
2022-12-19 13:54:39 +01:00
verbeux = 1;
break;
default:
fprintf(stderr, "invalid opt %d\n", opt);
exit(1);
break;
}
}
if (argc<=optind) filename = "a.scratch";
else filename = argv[optind];
2022-06-27 23:39:52 +02:00
if (argc<=optind+1) image = "image.tga";
else image = argv[optind+1];
2022-06-27 23:39:52 +02:00
2023-01-31 00:21:05 +01:00
/*----------- giving to the yuser some useless informations --- */
#if DEBUG_LEVEL
fprintf(stderr, "*** Genplot2 v 1.0.10 (dwtfywl) 1995,2010,2022 TontonTh \n");
#endif
2022-12-19 13:54:39 +01:00
if (verbeux) {
fprintf(stderr, "argc %d optind %d file '%s' image '%s'\n",
argc, optind, filename, image);
2022-12-19 13:54:39 +01:00
fprintf(stderr, " picsize : %d %d\n", outw, outh);
}
2022-06-27 23:39:52 +02:00
/*----------- opening input file and getting MIN and MAX values */
2022-09-13 00:11:53 +02:00
if ((fp = fopen(filename, "r"))==NULL) {
2022-06-27 23:39:52 +02:00
perror("fichier d'entree");
exit(1);
}
nbp = 0;
2022-11-08 01:39:58 +01:00
xmin = 9999999.99; xmax = -9999999.99;
ymin = 9999999.99; ymax = -9999999.99;
2022-09-13 00:11:53 +02:00
while ( fscanf(fp, "%lf %lf %d", &x, &y, &v) == 3 ) {
2022-06-27 23:39:52 +02:00
nbp++;
if (x > xmax) xmax = x;
if (x < xmin) xmin = x;
if (y > ymax) ymax = y;
if (y < ymin) ymin = y;
}
fclose(fp);
if (nbp == 0)
{
fprintf(stderr, "omg, I'v found _ZERO_ points for plotting...\n");
exit(2);
}
if (nbp == 1)
{
fprintf(stderr, "omg, I can't do any job with only one point.\n");
exit(2);
}
2022-06-27 23:39:52 +02:00
fprintf(stderr, "Genplot2: found %d points\n", nbp);
/*---------- computing coefficients (temporary hack !-) */
fx = (outw-XMIN-1)/(xmax-xmin);
fy = (outh-YMIN-1)/(ymax-ymin);
2022-09-13 00:11:53 +02:00
#if DEBUG_LEVEL
fprintf(stderr, "fc = %12f fy = %12f\n", fx, fy);
#endif
2022-06-27 23:39:52 +02:00
f = (fx<fy?fx:fy);
xC = 0.5*(xmin+xmax); yC = 0.5*(ymin+ymax);
XC = 0.5*(XMIN+outw); YC = 0.5*(YMIN+outh);
2022-06-27 23:39:52 +02:00
c1 = XC-f*xC; c2 = YC-f*yC;
2022-09-13 00:11:53 +02:00
#if DEBUG_LEVEL
2022-06-27 23:39:52 +02:00
fprintf(stderr, "c1 = %12f c2 = %12f\n", c1, c2);
2022-09-13 00:11:53 +02:00
#endif
2022-06-27 23:39:52 +02:00
/*------------- and now, plotting the image ! */
initgr(outw, outh);
2022-06-27 23:39:52 +02:00
fp = fopen(filename, "r");
puts("");
2022-09-13 00:11:53 +02:00
while ( fscanf(fp, "%lf %lf %d", &x, &y, &v) == 3 ) {
2022-06-27 23:39:52 +02:00
#if DEBUG_LEVEL
fprintf(stderr, "%12f %12f %d\n", x, y, v);
#endif
X = f*x+c1; Y = f*y+c2;
if (v < 0) move(X, Y);
else draw(X, Y, v);
}
endgr(image);
return 0;
}