summaryrefslogtreecommitdiff
path: root/rspl/revbench.c
blob: 81e668e7236854533e4c6a3030c5905b6c205f2f (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307

/************************************************/
/* Benchmark RSPL reverse lookup                */ 
/************************************************/

/* Author: Graeme Gill
 * Date:   31/10/96
 * Derived from tnd.c
 * Copyright 1999 - 2000 Graeme W. Gill
 *
 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 */


#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <math.h>
#include <time.h>
#include "copyright.h"
#include "aconfig.h"
#include "rspl.h"
#include "numlib.h"
//#include "ui.h"

#undef DOLIMIT			/* Define to have ink limit */
#define LIMITVAL 2.5	/* Total ink limit sum */
#undef DOCHECK

#define SHOW_OUTPUT		/* Define for printf on each conversion */

/* 11, 19 give about 60 seconds */
#define GRES 17			/* Default grid resolution */
#define RRES 43			/* Default reverse test resolution */
#define DI 4			/* Dimensions in */
#define FDI 3			/* Function (out) Dimensions */
#define NIP 10			/* Number of solutions allowed */

#define flimit(vv) ((vv) < 0.0 ? 0.0 : ((vv) > 1.0 ? 1.0 : (vv)))
#define fmin(a,b) ((a) < (b) ? (a) : (b))
#define fmin3(a,b,c)  (fmin((a), fmin((b),(c))))
#define fmax(a,b) ((a) > (b) ? (a) : (b))
#define fmax3(a,b,c)  (fmax((a), fmax((b),(c))))

/* Fwd function approximated by rspl */
/* Dummy cmyk->rgb conversion. This simulates our device */
void func(
void *cbctx,
double *out,
double *in) {
	double kk;
	double ci = in[0];
	double mi = in[1];
	double yi = in[2];
	double ki = in[3];
	double r,g,b;

	ci += ki;			/* Add black back in */
	mi += ki;
	yi += ki;
	kk = fmax3(ci,mi,yi);
	if (kk > 1.0) {
		ci /= kk;
		mi /= kk;
		yi /= kk;
	}
	r = 1.0 - ci;
	g = 1.0 - mi;
	b = 1.0 - yi;
	out[0] = flimit(r);
	out[1] = flimit(g);
	out[2] = flimit(b);
}

/* Simplex ink limit function */
double limitf(
void *lcntx,
float *in
) {
	int i;
	double ov;

	for (ov = 0.0, i = 0; i < DI; i++) {
		ov += in[i];
	} 
	return ov;
}


void usage(void) {
	fprintf(stderr,"Benchmark rspl reverse, Version %s\n",ARGYLL_VERSION_STR);
	fprintf(stderr,"usage: revbench [-f fwdres] [-r revres] [-v level] iccin iccout\n");
	fprintf(stderr," -v            Verbose\n");
	fprintf(stderr," -f res        Set forward grid res\n");
	fprintf(stderr," -r res        Set reverse test res\n");
	exit(1);
}

int
main(int argc, char *argv[]) {
	int fa,nfa;				/* argument we're looking at */
	int clutres = GRES;
	int rres = RRES;
	int verb = 0;
	int gres[MXDI];
	int e;

	clock_t stime,ttime;
	rspl *rss;	/* Multi-resolution regularized spline structure */

	error_program = argv[0];

	/* Process the arguments */
	for(fa = 1;fa < argc;fa++) {
		nfa = fa;					/* skip to nfa if next argument is used */
		if (argv[fa][0] == '-')	{	/* Look for any flags */
			char *na = NULL;		/* next argument after flag, null if none */

			if (argv[fa][2] != '\000')
				na = &argv[fa][2];		/* next is directly after flag */
			else {
				if ((fa+1) < argc) {
					if (argv[fa+1][0] != '-') {
						nfa = fa + 1;
						na = argv[nfa];		/* next is seperate non-flag argument */
					}
				}
			}

			if (argv[fa][1] == '?')
				usage();

			/* Verbosity */
			else if (argv[fa][1] == 'v' || argv[fa][1] == 'V') {
				verb = 1;
			}
			else if (argv[fa][1] == 'f' || argv[fa][1] == 'F') {
				fa = nfa;
				if (na == NULL) usage();
				clutres = atoi(na);
			}
			else if (argv[fa][1] == 'r' || argv[fa][1] == 'R') {
				fa = nfa;
				if (na == NULL) usage();
				rres = atoi(na);
			}
			else 
				usage();
		} else
			break;
	}

	printf("Started benchmark\n");
	/* Create the object */
	rss =  new_rspl(RSPL_NOFLAGS, DI, FDI);

	for (e = 0; e < DI; e++)
		gres[e] = clutres;

	printf("Rspl allocated\n");
	rss->set_rspl(rss, 0, (void *)NULL, func,
	               NULL, NULL, gres, NULL, NULL);

	printf("Rspl set\n");

	/* Start exploring the reverse test grid */
	{
		int ops = 0;
		double secs;
		rpsh counter;
		unsigned rcount;
		int ii[10];
		int f, rgres[MXDO];

		int flags = 0;		/* rev hint flags */
		co tp[NIP];			/* Test point */
		double cvec[4];		/* Text clip vector */
		int auxm[4];		/* Auxiliary target value valid flag */
#ifdef DOCHECK
		int j;
#endif
#ifdef NEVER
		double lmin[4], lmax[4];	/* Locus min/max values */
#endif

#ifdef DOCHECK
		char *check;		/* Check that we hit every cell */
#endif /* DOCHECK */

		/* Set auxiliary target mask */
		auxm[0] = 0;
		auxm[1] = 0;
		auxm[2] = 0;
		auxm[3] = 1;

#ifdef DOLIMIT
		rss->rev_set_limit(rss,
			limitf,
			NULL,
			LIMITVAL	/* limit maximum value */
		);
#endif /* DOLIMIT */

		printf("Forward resolution %d\n",clutres);
		printf("Reverse resolution %d\n",rres);

#ifdef DOCHECK
		if ((check = (char *)calloc(1, rcount)) == NULL)
			error("Malloc of check array\n");
#endif /* DOCHECK */

		for (f = 0; f < FDI; f++)
			rgres[f] = rres;

		rcount = rpsh_init(&counter, FDI, (unsigned int *)rgres, ii);	/* Initialise counter */
		
		stime = clock();

		/* Itterate though the grid */
		for (ops = 0;; ops++) {
			int r;
			int e;			/* Table index */
	
#ifdef DOCHECK
			check[((ii[2] * rres + ii[1]) * rres) + ii[0]] = 1;
#endif /* DOCHECK */

			for (e = 0; e < FDI; e++) { 	/* Input tables */
				tp[0].v[e] = ii[e]/(rres-1.0);		/* Vertex coordinates */
			}
	
			if (verb)
				printf("Input = %f %f %f\n",tp[0].v[0], tp[0].v[1], tp[0].v[2]);

#ifdef NEVER	/* Do locus lookup explicitly ? */
			/* Lookup the locus for the auxiliary (Black) chanel */
			if ((r = rss->rev_locus(rss,
				auxm, 	/* auxm Auxiliary mask flags */
				tp,		/* Input and auxiliary values */
				lmin,	/* Locus min/max return values */
				lmax
				)) == 0) {
				/* Rev locus failed - means that it will clip ? */
				tp[0].p[3] = 0.5;
				flags = RSPL_WILLCLIP;	/* Since there was no locus, we expect to clip */
			} else {
				/* Set the auxiliary target */
				tp[0].p[3] = (lmin[3] + lmax[3])/2.0;
				flags = RSPL_EXACTAUX;	/* Since we got locus, expect exact auxiliary match */
			}
#else
				tp[0].p[3] = 0.5;
				flags = RSPL_AUXLOCUS;	/* Auxiliary target is proportion of locus */
#endif	/* NEVER */

			/* Clip vector to 0.5 */
			cvec[0] = 0.5 - tp[0].v[0];
			cvec[1] = 0.5 - tp[0].v[1];
			cvec[2] = 0.5 - tp[0].v[2];
			cvec[3] = 0.5 - tp[0].v[3];

			/* Do reverse interpolation */
			if ((r = rss->rev_interp(rss,
				flags,	/* Hint flags */
				NIP,	/* Number of solutions allowed */
				auxm, 	/* auxm Auxiliary mask flags */
				cvec, 	/* cvec Clip vector direction & length */
				tp)		/* Input and output values */
				) == 0) {
				error("rev_interp failed\n");
			}
			
			r &= RSPL_NOSOLNS;		/* Get number of solutions */

			if (verb)
				printf("Output 1 of %d: %f, %f, %f, %f%s\n",
				  r & RSPL_NOSOLNS, tp[0].p[0], tp[0].p[1], tp[0].p[2], tp[0].p[3],
			      (r & RSPL_DIDCLIP) ? " [Clipped]" : "");


			if (rpsh_inc(&counter, ii))
				break;

		}		/* Next grid point */

		ttime = clock() - stime;
		secs = (double)ttime/CLOCKS_PER_SEC;
		printf("Done - %d ops in %f seconds, rate = %f ops/sec\n",ops, secs,ops/secs);
#ifdef DOCHECK
		for (j = 0; j < rcount; j++) {
			if (check[j] != 1) {
				printf("~~CHeck error at %d\n",j);
			}
		}
#endif /* DOCHECK */
	}

	rss->del(rss);
	return 0;
}