libtthimage/Tools/genplot2.c

181 lines
3.7 KiB
C

/*
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 "tga_outils.h"
#define XMIN 0
#define YMIN 0
#define XMAX 4096 /* constant from a 'dddd' */
#define YMAX 4096 /* constant from a 'dddd' */
/*::------------------------------------------------------------------::*/
static Image_Desc *image;
static RGB_map map;
static int curX, curY;
/*::------------------------------------------------------------------::*/
int initgr(int largeur, int hauteur)
{
int foo, r, v, b, dummy;
if ((image=Image_alloc(largeur, hauteur, 3))==NULL)
{
fprintf(stderr, "hu hu, 'man addswap' :)\n");
exit(3);
}
printf("\n\tinitgr %d %d\n", largeur, hauteur);
for (foo=0; foo<8; foo++)
{
printf("\tPal(%d) = ", foo);
r = foo & 1 ? 255 : 0;
v = foo & 2 ? 255 : 0;
b = foo & 4 ? 255 : 0;
printf("%02X %02X %02X\n", r, v, b);
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
printf("\tMOVE %5d %5d\n", x, y);
#endif
curX = x; curY = y;
return 0;
}
int draw(int x, int y, int color)
{
RGBA rgba;
int idx;
#if DEBUG_LEVEL
printf("\tDRAW %5d %5d to %5d %5d\n", curX, curY, x, y);
#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];
Image_draw_line(image, curX, curY, x, y, &rgba);
curX = x; curY = y;
return 0;
}
/*::------------------------------------------------------------------::*/
int endgr(char *filename)
{
fprintf(stderr, "saving '%s'\n", filename);
Image_TGA_save(filename, image, 0);
return 0;
}
/*::------------------------------------------------------------------::*/
int main(int argc, char *argv[])
{
char *filename, *image;
FILE *fp;
double x, y, xmin, ymin, xmax, ymax;
double fx, fy, f, X, Y;
double xC, yC, XC, YC, c1, c2;
int v, nbp;
/*---------- processing command line arguments */
if (argc<=1) filename = "a.scratch";
else filename = argv[1];
if (argc<=2) image = "image.tga";
else image = argv[2];
fprintf(stderr, "*** Genplot2 v 1.0.4 [%s] (dwtfywl) 1995,2010 TontonTh \n",
TGA_OUTILS_VERSION);
/*----------- giving to the yuser some useless informations --- */
fprintf(stderr, "hardcoded picsize : %d %d\n", XMAX, YMAX);
/*----------- opening input file and getting MIN and MAX values */
if ((fp = fopen(filename, "r"))==NULL)
{
perror("fichier d'entree");
exit(1);
}
nbp = 0;
xmin = 9999999; xmax = -9999999;
ymin = 9999999; ymax = -9999999;
while ( fscanf(fp, "%lf %lf %d", &x, &y, &v) == 3 )
{
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);
}
fprintf(stderr, "Genplot2: found %d points\n", nbp);
/*---------- computing coefficients (temporary hack !-) */
fx = (XMAX-XMIN-1)/(xmax-xmin);
fy = (YMAX-YMIN-1)/(ymax-ymin);
fprintf(stderr, "\nfc = %12f fy = %12f\n", fx, fy);
f = (fx<fy?fx:fy);
xC = 0.5*(xmin+xmax); yC = 0.5*(ymin+ymax);
XC = 0.5*(XMIN+XMAX); YC = 0.5*(YMIN+YMAX);
c1 = XC-f*xC; c2 = YC-f*yC;
fprintf(stderr, "c1 = %12f c2 = %12f\n", c1, c2);
/*------------- and now, plotting the image ! */
initgr(XMAX, YMAX);
fp = fopen(filename, "r");
puts("");
while ( fscanf(fp, "%lf %lf %d", &x, &y, &v) == 3 )
{
#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;
}