summaryrefslogtreecommitdiff
path: root/src/pnmcurve.c
blob: c043566d41bb6148d97ee49bb87fe1a4b262531f (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <math.h>

#include <gsl/gsl_spline.h>

#include "pnm.h"

typedef struct {
	int n;
	unsigned 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

static void
usage() {
        fprintf(stderr,
                "usage: pnmcurve [-rgb] -c <curve>\n"
                " -r apply curve to red channel.\n"
                " -g apply curve to green channel.\n"
                " -b apply curve to blue channel.\n"
				" -c <curve> curve to apply. <curve> must be\n"
				"    of the form x1:y1,x2:y2,x3:y3 where xi:yi are\n"
				"    control points of a cubic spline curve.\n"
				"    xi,yi must be in the range [0.0,1.0].\n");
}

int
main(int argc, char **argv) {
	int c, i;
	char *curve = NULL;
	struct pnm in_pnm;
	int channels = 0;
	table_t *table, *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 (!curve) {
		usage();
		return 1;
	}

	if (readPnmHeader(stdin, &in_pnm) != 0) {
		fprintf(stderr, "failed to read PNM file\n");
		return 1;
	}

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

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

	table = buildCurve(curve, in_pnm.maxval + 1, in_pnm.maxval);

	if (!table) {
		fprintf(stderr, "could not build table.\n");
		exit(1);
	}

	gTable[0] = channels & RED ? table : NULL;	
	gTable[1] = channels & GREEN ? table : NULL;	
	gTable[2] = channels & BLUE ? table : NULL;	

	writePnmHeader(stdout, &in_pnm);

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

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 < 1 << (nbytes * 8)) {
			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 = buf[(i * 3 + c) * nbytes];
					if (nbytes == 2) {
						val = (val << 8) | buf[(i * 3 + c) * nbytes + 1];
					}

					val = tables[c]->value[val];

					if (nbytes == 1) {
						buf[i * 3 + c] = (unsigned char) val;
					} else if (nbytes == 2) {
						buf[(i * 3 + c) * 2] = (unsigned char) (val >> 8);
						buf[(i * 3 + c) * 2 + 1] = (unsigned char) 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);

	if (n < 3) {
		fprintf(stderr, "not enough control points specified.\n");
		return NULL;
	}

	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 val = _y * maxval + 0.5;
        if (val > maxval) val = (double) maxval;
        if (val < 0) val = 0;

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

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