summaryrefslogtreecommitdiff
path: root/target/ppoint.c
blob: 2a5dd26d2efa070201e76130ff9536e03562c382 (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056

// ppoint7c
// Approach that picks poorly supprted points with maximum interpolation
// error each time. Version that creates a candidate list when adding
// previous points to the distance grid.
// Development of version that uses interpolation error and perceptual
// distance to nearest sample point driven point placement metric, this
// one usin incremental rspl for interpolation estimation.

/* 
 * Argyll Color Correction System
 *
 * Perceptually distributed point class
 *
 * Author: Graeme W. Gill
 * Date:   5/10/96
 *
 * Copyright 1996 - 2004 Graeme W. Gill
 * All rights reserved.
 *
 * This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
 * see the License.txt file for licencing details.
 */

/* TTBD:

 */


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#if defined(__IBMC__)
#include <float.h>
#endif
#include "numlib.h"
#include "rspl.h"
#include "sort.h"
#include "plot.h"
#include "icc.h"
#include "xcolorants.h"
#include "targen.h"
#include "ppoint.h"

#undef DEBUG
#define DUMP_PLOT		/* Show on screen plot */
#define PERC_PLOT 0		/* Emit perceptive space plots */
#define DO_WAIT 1		/* Wait for user key after each plot */

#define ALWAYS
#undef NEVER

#ifdef NEVER
#ifdef	__STDC__
#include <stdarg.h>
void error(char *fmt, ...), warning(char *fmt, ...), verbose(int level, char *fmt, ...);
#else
#include <varargs.h>
void error(), warning(), verbose();
#endif
#endif	/* NEVER */

#ifdef STANDALONE_TEST
#ifdef DUMP_PLOT
static void dump_image(ppoint *s, int pcp);
#endif
#endif

static void add_dist_points(ppoint *s, co *pp, int nn);
//static double far_dist(ppoint *s, double *p);

/* Default convert the nodes device coordinates into approximate perceptual coordinates */
/* (usually overriden by caller supplied function) */
static void
default_ppoint_to_percept(void *od, double *p, double *d) {
	ppoint *s = (ppoint *)od;
	int e;

#ifndef NEVER
	/* Default Do nothing - copy device to perceptual. */
	for (e = 0; e < s->di; e++) {
		double tt = d[e];
		if (e == 0)
			tt = pow(tt, 2.0);
		else
			tt = pow(tt, 0.5);
		p[e] = tt * 100.0;
	}
#else
	for (e = 0; e < s->di; e++) {
		double tt = d[e];
		/* Two slopes with a sharp turnover in X */
		if (e == 0) {
			if (tt < 0.5)
				tt = tt * 0.3/0.5;
			else
				tt = 0.3 + ((tt-0.5) * 0.7/0.5);
		}
		p[e] = tt * 100.0;
	}
#endif
}

/* return the distance of the device value from the device gamut */
/* This will be -ve if the point is outside */
/* If bvp is non-null, the index of the closest dim times 2 */
/* will be returned for the 0.0 boundary, dim * 2 + 1 for the 1.0 */
/* boundary, and di * 2 for the ink limit boundary. */
static double
ppoint_in_dev_gamut(ppoint *s, double *d, int *bvp) {
	int e;
	int di = s->di;
	double tt, dd = 1.0;
	double ss = 0.0;
	int bv = di;
	for (e = 0; e < di; e++) {
		tt = d[e];
		if (tt < dd) {
			dd = tt;
			bv = e * 2;
		}
		tt = 1.0 - d[e];
		if (tt < dd) {
			dd = tt;
			bv = e * 2 + 1;
		}
		ss += d[e];
	}
	ss = (s->ilimit-ss)/di;	/* Axis aligned distance to ink limit */
	tt = sqrt((double)di) * ss;	/* Diagonal distance to ink limit */
	if (tt < dd) {
		dd = tt;
		bv = di * 2;
	}
	if (bvp != NULL)
		*bvp = bv;
	return dd;
}

#ifdef NEVER	/* Not currently used */
/* Given the new intended device coordinates, */
/* clip the new position to the device gamut edge */
/* return non-zero if the point was clipped */
static int
ppoint_clip_point(ppoint *s, double *d) {
	int e;
	double ss = 0.0;
	int rv = 0;
	for (e = 0; e < s->di; e++) {
		if (d[e] < 0.0) {
			d[e] = 0.0;
			rv |= 1;
		} else if (d[e] > 1.0) {
			d[e] = 1.0;
			rv |= 1;
		}
		ss += d[e];
	}
	if (ss > s->ilimit) {
		ss = (ss - s->ilimit)/s->di;
		for (e = 0; e < s->di; e++)
			d[e] -= ss;
		rv |= 1;
	}
	return rv;
}
#endif /* NEVER */

/* --------------------------------------------------- */
/* Locate the best set of points to add */

/* Definition of the optimization functions handed to powell(.) */
/* Return distance error to be minimised (maximises distance from */
/* an existing sample point) */
static double efunc1(ppoint *s, double p[]) {
	double rv = 0.0;		/* return value */

//printf("\n~1 p = %f %f\n",p[0],p[1]);
	if ((rv = (ppoint_in_dev_gamut(s, p, NULL))) < 0.0) {
		rv = rv * -500.0 + 50000.0;		/* Discourage being out of gamut */
//printf("~1 out of gamut, rv = %f\n",rv);

	} else {
		int e, di = s->di;
		double vf[MXPD];		/* Perceptual value of reference */
		co tp;					/* Lookup from interpolation grid */
		double ierr;			/* Interpolation error */
		double cdist;			/* closest distance to point */
		double errd;			/* Overall error/distance to maximise */

		for (e = 0; e < di; e++)
			tp.p[e] = p[e];

		s->pd->interp(s->pd, &tp);	/* Lookup current closest distance value */
		cdist = tp.v[di];
		if (cdist >= 10000.0)		/* Initial value */
			cdist = 0.0;

//printf("~1 min pdist = %f\n",cdist);

		/* Not quite sure which is best here. */
		/* Using percept() is slower, and has more point placement artefacts, */
		/* but seems to arrive at a better result. */
#ifdef NEVER
		for (e = 0; e < di; e++)
			vf[e] = tp.v[e];		/* Use interpolated perceptual value */
#else
		s->percept(s->od, vf, p);	/* Lookup perceptual value */
#endif
		s->g->interp(s->g, &tp);	/* Lookup current interpolation */

//printf("~1 interp %f %f, percept %f %f\n",tp.v[0],tp.v[1],vf[0],vf[1]);
		for (ierr = 0.0, e = 0; e < di; e++) {
			double tt = tp.v[e] - vf[e]; 
			ierr += tt * tt;
		}
		ierr = sqrt(ierr);
//printf("~1 interp error = %f\n",ierr);

		/* The ratio of interpolation error to support distance affects */
		/* peak vs. average error in final result. */
#ifdef NEVER
		/* Weighted squares */
		errd = ierr * ierr + DWEIGHT * cdist * cdist;
#else
		/* Linear weighted then squared */
		errd = ierr + DWEIGHT * cdist;
		errd = errd * errd;
#endif

		/* Convert max error to min return value */
		rv = 1000.0/(0.1 + errd);
//printf("~1 err val %f\n",rv);

	}

//printf("~1 efunc1 returning %f from %f %f\n",rv,p[0],p[1]);
	return rv;
}


/* return the interpolation error at the given device location */
static double
ppoint_ierr(
ppoint *s,
double *p
) {
	int e, di = s->di;
	double vf[MXPD];	/* Perceptual value of reference */
	double err;
	co tp;				/* Perceptual value of test point */

	for (e = 0; e < di; e++)
		tp.p[e] = p[e];
	s->g->interp(s->g, &tp);

	s->percept(s->od, vf, p);

	for (err = 0.0, e = 0; e < di; e++) {
		double tt = tp.v[e] - vf[e]; 
		err += tt * tt;
	}
	err = sqrt(err);

	return err;
}

/* Find the next set of points to add to our test sample set. */
/* Both device and perceptual value are returned. */
/* We try and do a batch of points because adding points to the rspl interpolation */
/* is a high cost operation. The main trap is that we may add points that are almost identical, */
/* since we don't know the effect of adding other points in this batch. */
/* To try and counter this, points are rejected that are two close together in this group. */

/* Candidate points are located that have amongst the largest distances to existing */
/* points (measured in a device/perceptual distance mix), and from those points, */
/* the ones with the highest current interpolation mis-prediction error are selected. */
/* In this way a well spread set of samples is hoped to be gemerated, but favouring */
/* those that best reduce overall interpolation error. */
static int
ppoint_find_worst(
ppoint *s,
co *p,			/* return device values */
int tnn			/* Number to return */
) {
	co *fp = s->fwfp;	/* Copy of s-> info, stored in s because of size. */
	int nfp;			/* Current number in fp[] */
	int opoints;
	int e, di = s->di;
	double sr[MXPD];	/* Search radius */
	int i, j;

	for (e = 0; e < di; e++)
		sr[e] = 0.01;			/* Device space search radius */

//printf("~1 currently %d points in fp list\n",s->nfp);

	/* The distance grid functions will have a list of the FPOINTS best */
	/* grid points to start from. Make a copy of it */
	for (nfp = 0; nfp < s->nfp; nfp++) {
		fp[nfp] = s->fp[nfp];		/* Structure copy */
		fp[nfp].v[0] = efunc1(s, fp[nfp].p);	/* Compute optimiser error value */
	}

	/* If list is not full, fill with random numbers: */
	if (nfp < FPOINTS) {
//printf("~1 not full, so adding %d random points\n",FPOINTS-nfp);
//		for (; nfp < FPOINTS; nfp++) {
		for (; nfp < tnn; nfp++) {
			double sum;

			for (;;) {		/* Find an in-gamut point */
				for (sum = 0.0, e = 0; e < di; e++)
					sum += fp[nfp].p[e] = d_rand(0.0, 1.0);
				if (sum <= s->ilimit)
				break;
			}
			fp[nfp].v[0] = efunc1(s, fp[nfp].p);	/* Compute optimiser dist error value */
		}
	}

	/* Sort them by derr, smallest to largest */
#define HEAP_COMPARE(A,B) ((A).v[0] < (B).v[0])
	HEAPSORT(co, fp, nfp);
#undef HEAP_COMPARE

	opoints = nfp < OPOINTS ? nfp : OPOINTS;

	/* Optimise best portion of the list of starting points, according to */
	/* interpolation error weighted distance. */
	for (i = 0; i < opoints; i++) {
		double mx;

		if (powell(&mx, di, fp[i].p, sr,  0.001, 1000, 
		(double (*)(void *, double *))efunc1, (void *)s, NULL, NULL) != 0 || mx >= 50000.0) {
#ifdef ALWAYS
			printf("ppoint powell failed, tt = %f\n",mx);
#endif
		}
		fp[i].v[0] = mx;
//printf("~1 optimised point %d to %f %f derr %f\n",i,fp[i].p[0],fp[i].p[1],mx);

		/* Check if this duplicates a previous point */
		for (j = 0; j < i; j++) {

			double ddif = 0.0;
			for (e = 0; e < di; e++) {
				double tt = fp[i].p[e] - fp[j].p[e];
				ddif += tt * tt;
			}
			ddif = sqrt(ddif);			/* Device value difference */
			if (ddif < CLOSED) {
//printf("~1 duplicate of %d, so marked\n",j);
				fp[i].v[0] = 50000.0;	/* Mark so it won't be used */
				break;		/* too close */
			}
		}
	}

//printf("~1 derr sorted list:\n");
//for (i = 0; i < opoints; i++)
//	printf("~1 %d: loc %f %f derr %f\n", i, fp[i].p[0],fp[i].p[1],fp[i].v[0]);

	/* Compute the interpolation error for the points of interest */
	for (i = 0; i < opoints; i++) {
		if (fp[i].v[0] >= 50000.0)		/* Duplicate or failed to optimis point */
			fp[i].v[0] = -1.0;			/* Impossibly low interpolation error */
		else
			fp[i].v[0] = ppoint_ierr(s, fp[i].p);
	}

	/* Sort them by ierr, largest to smallest */
#define HEAP_COMPARE(A,B) ((A).v[0] > (B).v[0])
	HEAPSORT(co, fp, opoints);
#undef HEAP_COMPARE

//printf("~1 ierr sorted list:\n");
//for (i = 0; i < OPOINTS; i++)
//	printf("~1 %d: loc %f %f ierr %f\n", i, fp[i].p[0],fp[i].p[1],fp[i].v[0]);

	/* Return the best tnn as next points */
	for (j = i = 0; j < tnn && i < opoints; i++) {
		if (fp[i].v[0] < 0.0)
			continue;					/* Skip marked points */
		for (e = 0; e < di; e++)
			p[j].p[e] = fp[i].p[e];
		s->percept(s->od, p[j].v, p[j].p);
		j++;
	}
//printf("~1 returning %d points\n",j);
	return j;
}


/* --------------------------------------------------- */

/* determine the errors between the rspl and 100000 random test points */
static void
ppoint_stats(
ppoint *s
) {
	int i, n;
	int e, di = s->di;
	double mx = -1e80, av = 0.0, mn = 1e80;

	for (i = n = 0; i < 100000; i++) {
		co tp;				/* Perceptual value of test point */
		double vf[MXPD];	/* Perceptual value of reference */
		double sum, err;

		for (sum = 0.0, e = 0; e < di; e++)
			sum += tp.p[e] = d_rand(0.0, 1.0);

		if (sum <= s->ilimit) {

			/* rspl estimate of expected profile interpolation */
			s->g->interp(s->g, &tp);

			/* Target values */
			s->percept(s->od, vf, tp.p);

			for (err = 0.0, e = 0; e < di; e++) {
				double tt = tp.v[e] - vf[e]; 
				err += tt * tt;
			}
			err = sqrt(err);
			if (err > mx)
				mx = err;
			if (err < mn)
				mn = err;
			av += err;
			n++;
		}
	}
	av /= (double)n;

	printf("~1 Random check errors max %f, avg %f, min %f\n",mx,av,mn);
}

/* --------------------------------------------------- */
/* Support for maintaining the device/perceptual distance grid */
/* as well as keeping the far point candidate list up to date. */

/* Structure to hold data for callback function */
struct _pdatas {
	ppoint *s;			/* ppoint structure */
	int init;			/* Initialisation flag */
	co *pp;				/* List of new points */
	int nn;				/* Number of points */
}; typedef struct _pdatas pdatas;

/* rspl set callback function for maintaining perceptual distance information */
static void
pdfunc1(
	void *ctx,			/* Context */
	double *out,		/* output value, = di percept + distance */
	double *in			/* inut value */
) {
	pdatas *pp = (pdatas *)ctx;
	ppoint *s = pp->s;
	int e, di = s->di;

	if (pp->init) {
		s->percept(s->od, out, in);		/* Lookup perceptual value */
		out[di] = 10000.0;				/* Set to very high distance */

	} else {	/* Adding some points */
		int i;
		double sd = 1e80;

		/* Find smallest distance from this grid point to any of the new points */
		for (i = 0; i < pp->nn; i++) {
			double ddist, pdist;
			double dist;			/* Combined distance */

			/* Compute device and perceptual distance */
			for (ddist = pdist = 0.0, e = 0; e < di; e++) {
				double tt = out[e] - pp->pp[i].v[e];
				pdist += tt * tt;
				tt = 100.0 * (in[e] - pp->pp[i].p[e]);
				ddist += tt * tt;
			}
			dist = DDMIX * ddist + (1.0-DDMIX) * pdist;	/* Combine both */
			if (dist < sd)
				sd = dist;
		}

		sd = sqrt(sd);
		if (sd < out[di])
			out[di] = sd;

		/* Update far point candidate list */
		if (s->nfp < FPOINTS) {				/* List isn't full yet */
			for (e = 0; e < di; e++)
				s->fp[s->nfp].p[e] = in[e];
			s->fp[s->nfp].v[0] = sd;		/* store distance here */

			if (sd > s->wfpd) {				/* If this is the worst */
				s->wfpd = sd;
				s->wfp = s->nfp;
			}
			s->nfp++;

		} else if (sd < s->wfpd) {			/* Found better, replace current worst */

			for (e = 0; e < di; e++)
				s->fp[s->wfp].p[e] = in[e];
			s->fp[s->wfp].v[0] = sd;		/* store distance here */

			/* Locate the next worst */
			s->wfpd = -1.0;
			for (i = 0; i < s->nfp; i++) {
				if (s->fp[i].v[0] > s->wfp) {
					s->wfp = i;
					s->wfpd = s->fp[i].v[0];
				}
			}
		}
	}
}

/* Add a list of new points to the perceptual distance grid */
/* (Can change this to just adding 1 point) */
static void add_dist_points(
ppoint *s,
co *pp,		/* List of points including device and perceptual values */
int nn		/* Number in the list */
) {
	pdatas pdd;			/* pd callback context */

	pdd.s = s;
	pdd.init = 0;		/* Initialise values in the grid */
	pdd.pp = pp;
	pdd.nn = nn;

	/* let callback do all the work */
	s->pd->re_set_rspl(s->pd,
	           0,					/* No special flags */
	           &pdd,				/* Callback function context */
	           pdfunc1);			/* Callback function */
}

#ifdef NEVER	/* Not currently used */
/* Return the farthest distance value for this given location */
static double far_dist(ppoint *s, double *p) {
	int e, di = s->di;
	double cdist;
	co tp;
	
	for (e = 0; e < di; e++)
		tp.p[e] = p[e];

	s->pd->interp(s->pd, &tp);	/* Lookup current closest distance value */
	cdist = tp.v[di];
	if (cdist >= 10000.0)		/* Initial value */
		cdist = 0.0;
	return cdist;
}
#endif /* NEVER */

/* --------------------------------------------------- */
/* Seed the whole thing with points */

static void
ppoint_seed(
ppoint *s,
fxpos *fxlist,			/* List of existing fixed points */
int fxno				/* Number in fixed list */
) {
	int e, di = s->di;
	int i, j;

	if (fxno > 0) {
		co *pp;

		/* Place all the fixed points at the start of the list */
		if ((pp = (co *)malloc(fxno * sizeof(co))) == NULL)
			error ("ppoint: malloc failed on %d fixed nodes",fxno);

		for (i = 0; (i < fxno) && (i < s->tinp); i++) {
			node *p = &s->list[i];	/* Destination for point */

			for (e = 0; e < di; e++)
				p->p[e] = fxlist[i].p[e];

			p->fx = 1;			/* is a fixed point */
			s->percept(s->od, p->v, p->p);

			for (e = 0; e < di; e++) {
				pp[i].p[e] = p->p[e];
				pp[i].v[e] = p->v[e];
			}
		}
		s->np = s->fnp = i;

		/* Add new points to rspl interpolation */
		s->g->add_rspl(s->g, 0, pp, i);

		free(pp);
	}

	/* Seed the remainder points randomly */
	i = 0;
	while(s->np < s->tinp) {


#ifdef NEVER
		node *p = &s->list[s->np];
		double sum;

		/* Add random points */
		for (sum = 0.0, e = 0; e < di; e++)
			sum += p->p[e] = d_rand(0.0, 1.0);

		if (sum > s->ilimit)
			continue;
		s->np++;
		i++;
		printf("%cAdded: %d",cr_char,i);
#else 

#ifdef NEVER
		int nn;
		co pp[WPOINTS];		/* Space for return values */

		/* Add points at location with the largest error */
		nn = WPOINTS;

		if ((s->np + nn) > s->tinp)		/* Limit to desired value */
			nn = s->tinp - s->np;
		nn = ppoint_find_worst(s, pp, nn);

		/* Add new points to rspl interpolation and far field */
		s->g->add_rspl(s->g, 0, pp, nn);
		add_dist_points(s, pp, nn);
#else
		/* Diagnostic version */
		int nn;
		co pp[WPOINTS];		/* Space for return values */
		double err1[WPOINTS];
		double err2[WPOINTS];

		nn = WPOINTS;

		if ((s->np + nn) > s->tinp)		/* Limit to desired value */
			nn = s->tinp - s->np;
		nn = ppoint_find_worst(s, pp, nn);

		for (j = 0; j < nn; j++)
			err1[j] = ppoint_ierr(s, pp[j].p);

		/* Add new points to rspl interpolation and far field */
		s->g->add_rspl(s->g, 0, pp, nn);
		add_dist_points(s, pp, nn);

		for (j = 0; j < nn; j++)
			err2[j] = ppoint_ierr(s, pp[j].p);

		for (j = 0; j < nn; j++)
			printf("~1 improvement after adding point is %f to %f\n",err1[j],err2[j]);
#endif
		/* Copy points into ppoint */
		for (j = 0; j < nn; j++) {
			for (e = 0; e < di; e++) {
				s->list[s->np].p[e] = pp[j].p[e]; 
				s->list[s->np].v[e] = pp[j].v[e]; 
			}
			s->np++;
		}
		i += nn;
		printf("%cAdded: %d",cr_char,i);
#endif
	}
	printf("\n");		/* Finish "Added:" */
}

/* --------------------------------------------------- */

/* Rest the read index */
static void
ppoint_reset(ppoint *s) {
	s->rix = 0;
}

/* Read the next non-fixed point value */
/* Return nz if no more */
static int
ppoint_read(ppoint *s, double *p, double *f) {
	int e;

	/* Advance to next non-fixed point */
	while(s->rix < s->np && s->list[s->rix].fx)
		s->rix++;
	
	if (s->rix >= s->np)
		return 1;

	/* Return point info to caller */
	for (e = 0; e < s->di; e++) {
		if (p != NULL)
			p[e] = s->list[s->rix].p[e];
		if (f != NULL)
			f[e] = s->list[s->rix].v[e];
	}
	s->rix++;

	return 0;
}

/* Destroy ourselves */
static void
ppoint_del(ppoint *s) {

	/* Free our nodes */
	free(s->list);

	/* Free our rspl interpolation */
	s->g->del(s->g);

	/* Free our perceptual distance grid */
	s->pd->del(s->pd);

	free (s);
}

/* Creator */
ppoint *new_ppoint(
int di,					/* Dimensionality of device space */
double ilimit,			/* Ink limit (sum of device coords max) */
int tinp,				/* Total number of points to generate, including fixed */
fxpos *fxlist,			/* List of existing fixed points (may be NULL) */
int fxno,				/* Number of existing fixes points */
void (*percept)(void *od, double *out, double *in),		/* Perceptual lookup func. */
void *od				/* context for Perceptual function */
) {
	ppoint *s;

	// ~~~99 Info for logging
	fprintf(stderr, "WPOINTS = %d\n",WPOINTS);
	fprintf(stderr, "FPOINTS = %d\n",FPOINTS);
	fprintf(stderr, "OPOINTS = %d\n",OPOINTS);
	fprintf(stderr, "DDMIX   = %f\n",DDMIX);
	fprintf(stderr, "DWEIGHT = %f\n",DWEIGHT);
	fprintf(stderr, "CLOSED  = %f\n",CLOSED);

	if ((s = (ppoint *)calloc(sizeof(ppoint), 1)) == NULL)
		error ("ppoint: malloc failed");

#if defined(__IBMC__)
	_control87(EM_UNDERFLOW, EM_UNDERFLOW);
	_control87(EM_OVERFLOW, EM_OVERFLOW);
#endif

	if (di > MXPD)
		error ("ppoint: Can't handle di %d",di);

	s->di = di;

	if (tinp < fxno)	/* Make sure we return at least the fixed points */
		tinp = fxno;

	s->tinp = tinp;		/* Target total number of points */
	s->ilimit = ilimit;

	/* Init method pointers */
	s->reset = ppoint_reset;
	s->read  = ppoint_read;
	s->stats = ppoint_stats;
	s->del   = ppoint_del;

	/* If no perceptual function given, use default */
	if (percept == NULL) {
		s->percept = default_ppoint_to_percept;
		s->od = s;
	} else {
		s->percept = percept;
		s->od = od;
	}

	/* Allocate the list of points */
	s->np = 0;
	
	if ((s->list = (node *)calloc(sizeof(node), tinp)) == NULL)
		error ("ppoint: malloc failed on nodes");

	/* Setup the interpolation and perceptual distance rspls */
	{
		int e;
		int tres, gres[MXDI];
		datai pl,ph;
		datai vl,vh;
		double avgdev[MXDO];
		pdatas pdd;			/* pd callback context */

#ifndef NEVER	/* High res. */
		if (di <= 2)
			tres = 41;		/* Make depend on no points and dim ? */
		else if (di <= 3)
			tres = 33;		/* Make depend on no points and dim ? */
		else
			tres = 15;
#else
		if (di <= 2)
			tres = 3;		/* Make depend on no points and dim ? */
		else if (di <= 3)
			tres = 17;		/* Make depend on no points and dim ? */
		else
			tres = 9;
#endif

		/* The interpolation grid mimics the operation of the profile */
		/* package creating a device to CIE mapping for the device from */
		/* the given test points. */
		s->g = new_rspl(RSPL_NOFLAGS, di, di);

		for (e = 0; e < di; e++) {
			pl[e] = 0.0;
			ph[e] = 1.0;
			if (e == 1 || e == 2) {		/* Assume Lab */
				vl[e] = -128.0;
				vh[e] = 128.0;
			} else {
				vl[e] = 0.0;
				vh[e] = 100.0;
			}
			gres[e] = tres;
			avgdev[e] = 0.005;
		}

		/* Setup other details of rspl */
		s->g->fit_rspl(s->g,
		           RSPL_INCREMENTAL |
		           /* RSPL_EXTRAFIT | */	/* Extra fit flag */
		           0,
		           NULL,				/* No test points initialy */
		           0,					/* No test points */
		           pl, ph, gres,		/* Low, high, resolution of grid */
		           vl, vh,				/* Data scale */
		           0.3,					/* Smoothing */
		           avgdev,				/* Average Deviation */
		           NULL);


		/* Track closest perceptual distance to existing test points. */
		/* To save looking up the perceptual value for every grid location */
		/* every time a point is added, cache this values in the grid too. */
		s->pd = new_rspl(RSPL_NOFLAGS, di, di+1);

		/* Initialise the pd grid ready for the first points. */
		pdd.s = s;
		pdd.init = 1;		/* Initialise values in the grid */

		s->pd->set_rspl(s->pd,
		           0,					/* No special flags */
		           &pdd,				/* Callback function context */
		           pdfunc1,				/* Callback function */
		           pl, ph, gres,		/* Low, high, resolution of grid */
		           vl, vh);				/* Data scale */

		s->wfpd = -1.0;		/* Impossibly good worst point distance */
	}

	/* Create the points */
	ppoint_seed(s, fxlist, fxno);

	/* Print some stats */
	ppoint_stats(s);

	ppoint_reset(s);		/* Reset read index */

	return s;
}

/* =================================================== */

#ifdef STANDALONE_TEST

/* Graphics Gems curve */
static double gcurve(double vv, double g) {
	if (g >= 0.0) {
		vv = vv/(g - g * vv + 1.0);
	} else {
		vv = (vv - g * vv)/(1.0 - g * vv);
	}
	return vv;
}

#ifdef NEVER
static void sa_percept(void *od, double *out, double *in) {
	double lab[3];
	
	clu->dev_to_rLab(clu, lab, in);

	out[0] = lab[0];
//	out[1] = (lab[1]+100.0)/2.0;
	out[1] = (lab[2]+100.0)/2.0;
}
#else

static void sa_percept(void *od, double *p, double *d) {

#ifndef NEVER
	/* Default Do nothing - copy device to perceptual. */
	p[0] = 100.0 * gcurve(d[0], -4.5);
	p[1] = 100.0 * gcurve(d[1], 2.8);
	p[1] = 0.8 * p[1] + 0.2 * p[0];
#else
	for (e = 0; e < di; e++) {
		double tt = d[e];
		/* Two slopes with a sharp turnover in X */
		if (e == 0) {
			if (tt < 0.5)
				tt = tt * 0.3/0.5;
			else
				tt = 0.3 + ((tt-0.5) * 0.7/0.5);
		}
		p[e] = tt * 100.0;
	}
#endif
}
#endif


int
main(argc,argv)
int argc;
char *argv[];
{
	int npoints = 21;
	ppoint *s;
	long stime,ttime;
	error_program = argv[0];

	printf("Standalone test of ppoint, argument is number of points, default %d\n",npoints);

	if (argc > 1)
		npoints = atoi(argv[1]);

	/* Create the required points */
	stime = clock();
	s = new_ppoint(2, 1.5, npoints, NULL, 0, sa_percept, (void *)NULL);

	ttime = clock() - stime;
	printf("Execution time = %f seconds\n",ttime/(double)CLOCKS_PER_SEC);

#ifdef DUMP_PLOT
	printf("Perceptual plot:\n");
	dump_image(s, 1);

	printf("Device plot:\n");
	dump_image(s, 0);
#endif /* DUMP_PLOT */

	s->del(s);

	return 0;
}

#ifdef NEVER
/* Basic printf type error() and warning() routines */
#ifdef	__STDC__
void
error(char *fmt, ...)
#else
void
error(va_alist) 
va_dcl
#endif
{
	va_list args;
#ifndef	__STDC__
	char *fmt;
#endif

	fprintf(stderr,"ppoint: Error - ");
#ifdef	__STDC__
	va_start(args, fmt);
#else
	va_start(args);
	fmt = va_arg(args, char *);
#endif
	vfprintf(stderr, fmt, args);
	va_end(args);
	fprintf(stderr, "\n");
	fflush(stdout);
	exit (-1);
}
#endif /* NEVER */
#endif /* STANDALONE_TEST */


#ifdef STANDALONE_TEST
#ifdef DUMP_PLOT

/* Dump the current point positions to a plot window file */
void
static dump_image(ppoint *s, int pcp) {
	int i;
	double minx, miny, maxx, maxy;
	static double *x1a = NULL;
	static double *y1a = NULL;

	if (pcp != 0) {	/* Perceptual range */
		minx = 0.0;	/* Assume */
		maxx = 100.0;
		miny = 0.0;
		maxy = 100.0;
	} else {
		minx = 0.0;	/* Assume */
		miny = 0.0;
		maxx = 1.0;
		maxy = 1.0;
	}
	
	if (x1a == NULL) {
		if ((x1a = (double *)malloc(s->np * sizeof(double))) == NULL)
			error ("ppoint: malloc failed");
		if ((y1a = (double *)malloc(s->np * sizeof(double))) == NULL)
			error ("ppoint: malloc failed");
	}

	for (i = 0; i < s->np; i++) {
		node *p = &s->list[i];
		
		if (pcp != 0) {
			x1a[i] = p->v[0];
			y1a[i] = p->v[1];
		} else {
			x1a[i] = p->p[0];
			y1a[i] = p->p[1];
		}
	}

	/* Plot the vectors */
	do_plot_vec(minx, maxx, miny, maxy, 
				x1a, y1a, x1a, y1a, s->np, DO_WAIT, NULL, NULL, NULL, NULL, 0);
}

#endif /* DUMP_PLOT */
#endif /* STANDALONE_TEST */