gradtool

changeset 1:c438411b801b

latest changes from svn (saving grad files)
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 09 Oct 2015 01:45:12 +0300
parents 960cea2731c4
children 3ac4d33e568f
files Makefile gradtool.c
diffstat 2 files changed, 60 insertions(+), 17 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Tue Jun 19 05:30:45 2012 +0300
     1.2 +++ b/Makefile	Fri Oct 09 01:45:12 2015 +0300
     1.3 @@ -2,8 +2,14 @@
     1.4  obj = gradtool.o
     1.5  bin = gradtool
     1.6  
     1.7 -CFLAGS = -std=c89 -pedantic -Wall -g
     1.8 -LDFLAGS = -lglut -lGL
     1.9 +CFLAGS = -pedantic -Wall -g
    1.10 +LDFLAGS = $(libgl)
    1.11 +
    1.12 +ifeq ($(shell uname -s), Darwin)
    1.13 +	libgl = -framework OpenGL -framework GLUT
    1.14 +else
    1.15 +	libgl = -lGL -lglut
    1.16 +endif
    1.17  
    1.18  $(bin): $(obj)
    1.19  	$(CC) -o $@ $(obj) $(LDFLAGS)
    1.20 @@ -15,7 +21,3 @@
    1.21  .PHONY: install
    1.22  install: $(bin)
    1.23  	cp $(bin) $(PREFIX)/bin/$(bin)
    1.24 -
    1.25 -.PHONY: uninstall
    1.26 -uninstall:
    1.27 -	rm -f $(PREFIX)/bin/$(bin)
     2.1 --- a/gradtool.c	Tue Jun 19 05:30:45 2012 +0300
     2.2 +++ b/gradtool.c	Fri Oct 09 01:45:12 2015 +0300
     2.3 @@ -1,6 +1,12 @@
     2.4  #include <stdio.h>
     2.5  #include <stdlib.h>
     2.6 +#include <string.h>
     2.7 +#include <errno.h>
     2.8 +#ifdef __APPLE__
     2.9 +#include <GLUT/glut.h>
    2.10 +#else
    2.11  #include <GL/glut.h>
    2.12 +#endif
    2.13  
    2.14  #define MAX_POS		512
    2.15  
    2.16 @@ -23,7 +29,8 @@
    2.17  void rm_val(struct track *track, float pos);
    2.18  float get_val(struct track *track, float pos);
    2.19  
    2.20 -void save(void);
    2.21 +int save_ppm(const char *fname, int img_width);
    2.22 +int save_grad(const char *fname);
    2.23  
    2.24  void key_handler(unsigned char key, int x, int y);
    2.25  void skey_handler(int key, int x, int y);
    2.26 @@ -256,22 +263,20 @@
    2.27  }
    2.28  
    2.29  
    2.30 -#define IMG_X	256
    2.31 -#define FNAME	"grad.ppm"
    2.32 -void save(void)
    2.33 +int save_ppm(const char *fname, int img_width)
    2.34  {
    2.35  	int i;
    2.36  	FILE *fp;
    2.37  
    2.38 -	if(!(fp = fopen(FNAME, "wb"))) {
    2.39 -		perror("failed to write " FNAME);
    2.40 -		return;
    2.41 +	if(!(fp = fopen(fname, "wb"))) {
    2.42 +		fprintf(stderr, "failed to write %s: %s\n", fname, strerror(errno));
    2.43 +		return -1;
    2.44  	}
    2.45  
    2.46 -	fprintf(fp, "P6\n%d %d\n255\n", IMG_X, 1);
    2.47 +	fprintf(fp, "P6\n%d 1\n255\n", img_width);
    2.48  
    2.49 -	for(i=0; i<IMG_X; i++) {
    2.50 -		float t = (float)i / (float)IMG_X;
    2.51 +	for(i=0; i<img_width; i++) {
    2.52 +		float t = (float)i / (float)img_width;
    2.53  		int r = get_val(&tred, t) * 255.0;
    2.54  		int g = get_val(&tgreen, t) * 255.0;
    2.55  		int b = get_val(&tblue, t) * 255.0;
    2.56 @@ -282,6 +287,37 @@
    2.57  	}
    2.58  
    2.59  	fclose(fp);
    2.60 +	return 0;
    2.61 +}
    2.62 +
    2.63 +int save_grad(const char *fname)
    2.64 +{
    2.65 +	int i;
    2.66 +	FILE *fp;
    2.67 +	struct track *tr[] = {&tred, &tgreen, &tblue};
    2.68 +
    2.69 +	if(!(fp = fopen(fname, "w"))) {
    2.70 +		return -1;
    2.71 +	}
    2.72 +
    2.73 +	fprintf(fp, "GRAD\n");
    2.74 +
    2.75 +	for(i=0; i<3; i++) {
    2.76 +		key_t *ptr = tr[i]->keys->next;
    2.77 +	
    2.78 +		while(ptr) {
    2.79 +			float t = (float)ptr->pos / (float)MAX_POS;
    2.80 +			float r = get_val(&tred, t);
    2.81 +			float g = get_val(&tgreen, t);
    2.82 +			float b = get_val(&tblue, t);
    2.83 +
    2.84 +			fprintf(fp, "%f\t%f %f %f\n", t, r, g, b);
    2.85 +			ptr = ptr->next;
    2.86 +		}
    2.87 +	}
    2.88 +
    2.89 +	fclose(fp);
    2.90 +	return 0;
    2.91  }
    2.92  
    2.93  
    2.94 @@ -298,7 +334,12 @@
    2.95  
    2.96  	case 's':
    2.97  	case 'S':
    2.98 -		save();
    2.99 +		save_ppm("grad.ppm", 256);
   2.100 +		break;
   2.101 +
   2.102 +	case 'g':
   2.103 +	case 'G':
   2.104 +		save_grad("grad.grad");
   2.105  		break;
   2.106  
   2.107  	case 'c':