summaryrefslogtreecommitdiff
path: root/src/pnmcurve.c
blob: 37d2dd21c481600a42b1f57bf3a43fc83bd1c5d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include <gsl/gsl_spline.h>

#include "pnm.h"

typedef struct {
	int n;
	short value[1];
} table_t;

static int
pam_transform(FILE *in_fp, FILE *out_fp,
	const struct pnm *in_pnm, table_t **tables);

static table_t*
buildCurve(const char *ctrl_points, int resolution, int maxval);

#define RED 0x1
#define GREEN 0x2
#define BLUE 0x4

int
main(int argc, char **argv) {
	int c, i;
	char *curve = NULL;
	struct pnm in_pnm;
	int channels = 0;
	table_t *gTable[3];

	while ((c = getopt(argc, argv, "rgbC:h")) != EOF) {
		switch (c) {  
			case 'r':
				channels |= RED;
				break;
			case 'g':
				channels |= GREEN;
				break;
			case 'b':
				channels |= BLUE;
				break;
			case 'C':
				curve = optarg;
				break;

			default:
				break;
		}
	}

	if (readPnmHeader(stdin, &in_pnm) != 0) {
		exit(1);
	}

	if (in_pnm.maxval == 255) {
	} else if (in_pnm.maxval == 65535) {
	} else {
		fprintf(stderr, "unsupported PNM maxval %d\n", in_pnm.maxval);
		exit(1);
	}

	if (curve) {
		table_t *table;

		if (channels == 0) channels = RED | GREEN | BLUE;

		table = buildCurve(curve, in_pnm.maxval + 1, in_pnm.maxval);
	
		gTable[0] = channels & RED?table:NULL;	
		gTable[1] = channels & GREEN?table:NULL;	
		gTable[2] = channels & BLUE?table:NULL;	
	}

	writePnmHeader(stdout, &in_pnm);

	pam_transform(stdin, stdout, &in_pnm, gTable);

	return 0;
}

static int
pam_transform(FILE *in_fp, FILE *out_fp,
	const struct pnm *in_pnm, table_t **tables) {
	int i, c, row, col;
	int nbytes = in_pnm->maxval == 65535?2:1;
	unsigned char *buf = malloc(in_pnm->width * nbytes * 3);

	for (c = 0; c < 3; c++) {
		if (tables[c] && tables[c]->n < in_pnm->maxval) {
			fprintf(stderr, "Table %d too small (%d)\n", c, tables[c]->n);
			return 1;
		}
	}

	for (row = 0; row < in_pnm->height; row++) {
		fread(buf, in_pnm->width, nbytes * 3, in_fp);

		for (i = 0; i < in_pnm->width; i++) {
			for (c = 0; c < 3; c++) {
				if (tables[c]) {
					int val = (unsigned char) buf[(i * 3 + c) * nbytes];	
					if (nbytes == 2) {
						val = (val << 8) |
							(unsigned char) buf[(i * 3 + c) * nbytes + 1];
					}

					if (val < 0 || val > in_pnm->maxval) {
						fprintf(stderr, "Invalid pixel value %d\n", val);
						return 1;
					}

					if (nbytes == 1) {
						buf[i * 3 + c] = (unsigned char) tables[c]->value[val];
					}
				}
			}
		}

		fwrite(buf, in_pnm->width, nbytes * 3, out_fp);
	}

	free(buf);
	return 0;
}

#define MAX_CTRL 256

static table_t*
buildCurve(const char *ctrl_points, int resolution, int maxval) {
	table_t *table;
	char *pstr, *buf = strdup(ctrl_points);
	int i, n = 0;
	double X[MAX_CTRL], Y[MAX_CTRL];
	gsl_interp_accel *acc;
	gsl_spline *spline;

	while (pstr = strsep(&buf, ",")) {
		if (n == MAX_CTRL) {
			fprintf(stderr, "Maximum number of control points (%d) reached.\n",
				MAX_CTRL);
			break;
		}

		if (sscanf(pstr, "%lf:%lf", &X[n], &Y[n]) != 2 ||
			X[n] < 0.0 || X[n] > 1.0 || Y[n] < 0.0 || Y[n] > 1.0) {
			fprintf(stderr, "Could not parse control point %s.\n", pstr);
			free(buf);
			return NULL;
		}

		n++;
	}

	free(buf);

	table = (table_t*) malloc(sizeof(table_t) + resolution * sizeof(short));
	table->n = resolution;

	acc = gsl_interp_accel_alloc();
	spline = gsl_spline_alloc(gsl_interp_cspline, n);
	gsl_spline_init (spline, X, Y, n);

	for (i = 0; i < resolution; i++) {
		double _x = (double) i / (resolution - 1);
		double _y = gsl_spline_eval(spline, _x, acc);
        double dval = _y * (double) maxval + .5;
        if (dval > (double) maxval) dval = (double) maxval;
        if (dval < 0) dval = 0;

        table->value[i] = (short) floor(dval);
	}

	gsl_spline_free(spline);
	gsl_interp_accel_free(acc);
	
	return table;
}