summaryrefslogtreecommitdiffstats
path: root/src/libs/lprof/lcmsprf.h
blob: 00c7ac40ac39a0eaad82749acad955c5090b22b7 (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
/*
Little cms - profiler construction set
Copyright (C) 1998-2001 Marti Maria <marti@littlecms.com>

THIS SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.

IN NO EVENT SHALL MARTI MARIA BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
OF THIS SOFTWARE.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

As a special exception to the GNU General Public License, if you
distribute this file as part of a program that contains a
configuration script generated by Autoconf, you may include it under
the same distribution terms that you use for the rest of that program.
*/

/* Version 1.09a */

#ifndef __cmsprf_H

#include <config.h>
#include LCMS_HEADER

#include <ctype.h>
#include <limits.h>
#include <stdarg.h>
#include <sys/stat.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef NON_WINDOWS
#  ifndef stricmp
#     define stricmp strcasecmp
#  endif
#endif

#ifndef max
#define max(a,b) ((a) > (b)?(a):(b))
#endif


/* Matrix operations - arbitrary size ----------------------------------------------------- */

typedef struct {

    int       Cols, Rows;
    double**  Values;

    } MATN,FAR* LPMATN;

// See B.K.O #148930: compile with lcms v.1.17
#if (LCMS_VERSION > 116)
typedef LCMSBOOL BOOL;
#endif

LPMATN      cdecl MATNalloc(int Rows, int Cols);
void        cdecl MATNfree (LPMATN mat);
LPMATN      cdecl MATNmult(LPMATN a1, LPMATN a2);
double      cdecl MATNcross(LPMATN a);
void        cdecl MATNscalar (LPMATN a, double scl, LPMATN b);
LPMATN      cdecl MATNtranspose (LPMATN a);
BOOL        cdecl MATNsolve(LPMATN a, LPMATN b);


/* IT8.7 / CGATS.17-200x handling -------------------------------------------------------- */

#define cmsxIT8_ROWS                    12
#define cmsxIT8_COLS                    22
#define cmsxIT8_GRAYCOLS                24
#define cmsxIT8_NORMAL_PATCHES          (cmsxIT8_ROWS*cmsxIT8_COLS + cmsxIT8_GRAYCOLS)
#define cmsxIT8_CUSTOM_PATCHES          10
#define cmsxIT8_TOTAL_PATCHES           (cmsxIT8_NORMAL_PATCHES + cmsxIT8_CUSTOM_PATCHES)


LCMSHANDLE  cdecl cmsxIT8Alloc(void);
void        cdecl cmsxIT8Free(LCMSHANDLE cmsxIT8);
LCMSHANDLE  cdecl cmsxIT8LoadFromFile(const char* cFileName);
LCMSHANDLE  cdecl cmsxIT8LoadFromMem(void *Ptr, size_t len);
BOOL        cdecl cmsxIT8SaveToFile(LCMSHANDLE cmsxIT8, const char* cFileName);
const char* cdecl cmsxIT8GetSheetType(LCMSHANDLE hIT8);
BOOL        cdecl cmsxIT8SetSheetType(LCMSHANDLE hIT8, const char* Type);
const char* cdecl cmsxIT8GetPatchName(LCMSHANDLE hIT8, int nPatch, char* buffer);
BOOL        cdecl cmsxIT8SetProperty(LCMSHANDLE hcmsxIT8, const char* cProp, const char *Str);
BOOL        cdecl cmsxIT8SetPropertyDbl(LCMSHANDLE hcmsxIT8, const char* cProp, double Val);
const char* cdecl cmsxIT8GetProperty(LCMSHANDLE hcmsxIT8, const char* cProp);
double      cdecl cmsxIT8GetPropertyDbl(LCMSHANDLE hcmsxIT8, const char* cProp);
int         cdecl cmsxIT8EnumProperties(LCMSHANDLE cmsxIT8, char ***PropertyNames);
int         cdecl cmsxIT8EnumDataFormat(LCMSHANDLE cmsxIT8, char ***SampleNames);
BOOL        cdecl cmsxIT8SetDataFormat(LCMSHANDLE cmsxIT8, int n, const char *Sample);

BOOL        cdecl cmsxIT8GetDataSetByPos(LCMSHANDLE IT8, int col, int row, char* Val, int ValBufferLen);

BOOL        cdecl cmsxIT8GetDataSet(LCMSHANDLE cmsxIT8, const char* cPatch,
                        const char* cSample,
                        char* Val, int ValBuffLen);

BOOL        cdecl cmsxIT8GetDataSetDbl(LCMSHANDLE cmsxIT8, const char* cPatch, const char* cSample, double* v);

BOOL        cdecl cmsxIT8SetDataSet(LCMSHANDLE cmsxIT8, const char* cPatch,
                        const char* cSample,
                        char *Val);

BOOL        cdecl cmsxIT8SetDataSetDbl(LCMSHANDLE cmsxIT8, const char* cPatch, const char* cSample, double Val);

const char *cdecl cmsxIT8GenericPatchName(int nPatch, char* buffer);



/* Patch collections (measurement lists) -------------------------------------------------- */

#define PATCH_HAS_Lab         0x00000001
#define PATCH_HAS_XYZ         0x00000002
#define PATCH_HAS_RGB         0x00000004
#define PATCH_HAS_CMY         0x00000008
#define PATCH_HAS_CMYK        0x00000010
#define PATCH_HAS_HEXACRM     0x00000020
#define PATCH_HAS_STD_Lab     0x00010000
#define PATCH_HAS_STD_XYZ     0x00020000
#define PATCH_HAS_XYZ_PROOF   0x00100000  
#define PATCH_HAS_MEAN_DE     0x01000000
#define PATCH_HAS_STD_DE      0x02000000
#define PATCH_HAS_CHISQ       0x04000000


#define MAXPATCHNAMELEN     20
/* A patch in memory */

typedef struct {

                DWORD        dwFlags;   /* Is quite possible to have colorant in only */
                                        /* some patches of sheet, so mark each entry with */
                                        /* the values it has. */

                char Name[MAXPATCHNAMELEN];

                cmsCIELab Lab;          /* The tristimulus values of target */
                cmsCIEXYZ XYZ;

				cmsCIEXYZ XYZProof;		/* The absolute XYZ value returned by profile */
										/* (gamut constrained to device) */

                union {                 /* The possible colorants. Only one space is */
                                        /* allowed...obviously only one set of */
                                        /* device-dependent values per patch does make sense. */
                        double RGB[3];
                        double CMY[3];
                        double CMYK[4];
                        double Hexa[MAXCHANNELS];

                        } Colorant;             

                double dEStd;               /* Standard deviation  */
                double ChiSq;               /* Chi-square parameter (mean of STD of colorants) */
                double dEMean;              /* Mean dE */

           } PATCH, FAR* LPPATCH;



/* A set of patches is simply an array of bools, TRUE if the patch */
/* belong to the set, false otherwise. */

typedef BOOL* SETOFPATCHES;

/* This struct holds whole Patches collection */

typedef struct _measurement {

           int          nPatches;
           LPPATCH      Patches;
           SETOFPATCHES Allowed;

           } MEASUREMENT,FAR *LPMEASUREMENT;


void         cdecl cmsxPCollFreeMeasurements(LPMEASUREMENT m);
SETOFPATCHES cdecl cmsxPCollBuildSet(LPMEASUREMENT m, BOOL lDefault);

BOOL         cdecl cmsxPCollBuildMeasurement(LPMEASUREMENT m, 
                                             const char *ReferenceSheet,
                                             const char *MeasurementSheet,
                                             DWORD dwNeededSamplesType);

int          cdecl cmsxPCollCountSet(LPMEASUREMENT m, SETOFPATCHES Set);
BOOL         cdecl cmsxPCollValidatePatches(LPMEASUREMENT m, DWORD dwFlags);

BOOL         cdecl cmsxPCollLoadFromSheet(LPMEASUREMENT m, LCMSHANDLE hSheet);
BOOL         cdecl cmsxPCollSaveToSheet(LPMEASUREMENT m, LCMSHANDLE it8);

LPPATCH      cdecl cmsxPCollGetPatch(LPMEASUREMENT m, int n);
LPPATCH      cdecl cmsxPCollGetPatchByName(LPMEASUREMENT m, const char* Name, int* lpPos);
LPPATCH      cdecl cmsxPCollGetPatchByPos(LPMEASUREMENT m, int row, int col);
LPPATCH      cdecl cmsxPCollAddPatchRGB(LPMEASUREMENT m, const char *Name,
                                        double r, double g, double b,
                                        LPcmsCIEXYZ XYZ, LPcmsCIELab Lab);

void         cdecl cmsxPCollLinearizePatches(LPMEASUREMENT m, SETOFPATCHES Valids,
                                             LPGAMMATABLE Gamma[3]);

/* Extraction utilities */

/* Collect "need" patches of the specific kind, return the number of collected (that */
/* could be less if set of patches is exhausted) */

void         cdecl cmsxPCollPatchesGS(LPMEASUREMENT m, SETOFPATCHES Result);

int          cdecl cmsxPCollPatchesNearRGB(LPMEASUREMENT m, SETOFPATCHES Valids,
                                            double r, double g, double b, int need, SETOFPATCHES Result);

int          cdecl cmsxPCollPatchesNearNeutral(LPMEASUREMENT m, SETOFPATCHES Valids,
                                            int need, SETOFPATCHES Result);

int          cdecl cmsxPCollPatchesNearPrimary(LPMEASUREMENT m, SETOFPATCHES Valids,
                                           int nChannel, int need, SETOFPATCHES Result);

int          cdecl cmsxPCollPatchesInLabCube(LPMEASUREMENT m, SETOFPATCHES Valids, 
                                           double Lmin, double LMax, double a, double b, SETOFPATCHES Result);

int          cdecl cmsxPCollPatchesInGamutLUT(LPMEASUREMENT m, SETOFPATCHES Valids, 
                                              LPLUT Gamut, SETOFPATCHES Result);

/* Find important values */

LPPATCH		 cdecl cmsxPCollFindWhite(LPMEASUREMENT m, SETOFPATCHES Valids, double* Distance);
LPPATCH		 cdecl cmsxPCollFindBlack(LPMEASUREMENT m, SETOFPATCHES Valids, double* Distance);
LPPATCH		 cdecl cmsxPCollFindPrimary(LPMEASUREMENT m, SETOFPATCHES Valids, int Channel, double* Distance);

/* Multiple linear regression stuff ---------------------------------------- */


/* A measurement of error */

typedef struct {

        double SSE;             /* The error sum of squares */
        double MSE;             /* The error mean sum of squares */
        double SSR;             /* The regression sum of squares */
        double MSR;             /* The regression mean sum of squares */
        double SSTO;            /* Total sum of squares */
        double F;               /* The Fisher-F value (MSR / MSE) */
        double R2;              /* Proportion of variability explained by the regression */
                                /* (root is Pearson correlation coefficient) */

        double R2adj;           /* The adjusted coefficient of multiple determination. */
                                /* R2-adjusted or R2adj. This is calculated as */
                                /* R2adj = 1 - (1-R2)(N-n-1)/(N-1) */
                                /* and used as multiple correlation coefficient */
                                /* (really, it should be square root) */

    } MLRSTATISTICS, FAR* LPMLRSTATISTICS;


int  cdecl cmsxRegressionCreateMatrix(LPMEASUREMENT m, SETOFPATCHES Allowed, int nterms,
                                       int ColorSpace,
                                       LPMATN* lpMat, LPMLRSTATISTICS Stat);

BOOL cdecl cmsxRegressionRGB2Lab(double r, double g, double b,
                                       LPMATN tfm, LPcmsCIELab Lab);

BOOL cdecl cmsxRegressionRGB2XYZ(double r, double g, double b,
                                       LPMATN tfm, LPcmsCIEXYZ XYZ);

BOOL cdecl cmsxRegressionInterpolatorRGB(LPMEASUREMENT m,
                                       int ColorSpace,                            
                                       int    RegressionTerms,
                                       BOOL   lUseLocalPatches,
                                       int    MinPatchesToCollect,
                                       double r, double g, double b,
                                       void* Res);



/* Levenberg-Marquardt ---------------------------------------------------------------------- */

LCMSHANDLE cdecl cmsxLevenbergMarquardtInit(LPSAMPLEDCURVE x, LPSAMPLEDCURVE y, double sig,
								double a[],
								int ma,  
								void (*funcs)(double, double[], double*, double[], int)
								);

double    cdecl cmsxLevenbergMarquardtAlamda(LCMSHANDLE hMRQ);
double    cdecl cmsxLevenbergMarquardtChiSq(LCMSHANDLE hMRQ);
BOOL      cdecl cmsxLevenbergMarquardtIterate(LCMSHANDLE hMRQ);
BOOL      cdecl cmsxLevenbergMarquardtFree(LCMSHANDLE hMRQ);


/* Convex hull geometric routines ------------------------------------------------------------ */

LCMSHANDLE cdecl cmsxHullInit(void);
void	   cdecl cmsxHullDone(LCMSHANDLE hHull);
BOOL	   cdecl cmsxHullAddPoint(LCMSHANDLE hHull, int x, int y, int z);
BOOL	   cdecl cmsxHullComputeHull(LCMSHANDLE hHull);
char	   cdecl cmsxHullCheckpoint(LCMSHANDLE hHull, int x, int y, int z);
BOOL       cdecl cmsxHullDumpVRML(LCMSHANDLE hHull, const char* fname);


/* Linearization ---------------------------------------------------------------------------- */


#define MEDIUM_REFLECTIVE_D50	0	/* Used for scanner targets */
#define MEDIUM_TRANSMISSIVE		1	/* Used for monitors & projectors */
           
void cdecl cmsxComputeLinearizationTables(LPMEASUREMENT m, 
                                    int ColorSpace, 
                                    LPGAMMATABLE Lin[3],
									int nResultingPoints,
									int Medium);                                    
                     
         
void		 cdecl cmsxCompleteLabOfPatches(LPMEASUREMENT m, SETOFPATCHES Valids, int Medium);
LPGAMMATABLE cdecl cmsxEstimateGamma(LPSAMPLEDCURVE X, LPSAMPLEDCURVE Y, int nResultingPoints);
                                            
void cdecl cmsxApplyLinearizationTable(double In[3], LPGAMMATABLE Gamma[3], double Out[3]);
void cdecl cmsxApplyLinearizationGamma(WORD In[3], LPGAMMATABLE Gamma[3], WORD Out[3]);

/* Support routines ---------------------------------------------------------------------- */

double cdecl _cmsxSaturate65535To255(double d);
double cdecl _cmsxSaturate255To65535(double d);
void   cdecl _cmsxClampXYZ100(LPcmsCIEXYZ xyz);

/* Matrix shaper profiler API ------------------------------------------------------------- */


BOOL cdecl cmsxComputeMatrixShaper(const char* ReferenceSheet,                              
                                   const char* MeasurementSheet,
								   int Medium,
                                   LPGAMMATABLE TransferCurves[3],
                                   LPcmsCIEXYZ WhitePoint,
                                   LPcmsCIEXYZ BlackPoint,
                                   LPcmsCIExyYTRIPLE Primaries);


/* Common to all profilers ------------------------------------------------------------------- */

#define MAX_STR 256

typedef int (* cmsxGAUGER)(const char *Label, int nMin, int nMax, int Pos);
typedef int (* cmsxPRINTF)(const char *Frm, ...);

typedef struct {

           /* Files */
           char ReferenceSheet[MAX_PATH];
           char MeasurementSheet[MAX_PATH];
           char OutputProfileFile[MAX_PATH];

           /* Some infos            */
           char Description[MAX_STR];
           char Manufacturer[MAX_STR];
           char Model[MAX_STR];
           char Copyright[MAX_STR];

           /* Callbacks */
           cmsxGAUGER Gauger;
           cmsxPRINTF printf;

           /* EndPoints */
           cmsCIEXYZ WhitePoint;            /* Black point in 0.xxx notation */
           cmsCIEXYZ BlackPoint;            /* Black point in 0.xxx notation */
           cmsCIExyYTRIPLE Primaries;       /* The primaries */
           LPGAMMATABLE Gamma[3];           /* Gamma curves */

		   /* Profile */ 
		   cmsHPROFILE hProfile;            /* handle to profile */

           icProfileClassSignature DeviceClass;
           icColorSpaceSignature   ColorSpace;

           int PCSType;                    /* PT_XYZ or PT_Lab */
           int CLUTPoints;                 /* Final CLUT resolution */
		   int ProfileVerbosityLevel;	   /* 0=minimum, 1=additional, 2=Verbose, 3=Any suitable */

           
		   /* Measurement */	
           MEASUREMENT m;                /* Contains list of available patches */
		   int Medium;


		   /* RGB Gamut hull */
		   LCMSHANDLE hRGBHull;   /* Contains bobbin of valid RGB values */

		   /* CIECAM97s */
		   BOOL lUseCIECAM97s;   /* Use CIECAM97s for chromatic adaptation? */

		   cmsViewingConditions device;     /* Viewing condition of source */
           cmsViewingConditions PCS;        /* Viewing condition of PCS */

           LCMSHANDLE hDevice;              /* CIECAM97s models used for adaptation */
           LCMSHANDLE hPCS;                 /* and viewing conditions */
           

    } PROFILERCOMMONDATA,FAR* LPPROFILERCOMMONDATA;
 

/* Shared routines */

BOOL cdecl cmsxEmbedCharTarget(LPPROFILERCOMMONDATA hdr);
BOOL cdecl cmsxEmbedMatrixShaper(LPPROFILERCOMMONDATA hdr);
BOOL cdecl cmsxEmbedTextualInfo(LPPROFILERCOMMONDATA hdr);

int  cdecl cmsxFindOptimumNumOfTerms(LPPROFILERCOMMONDATA hdr, int nMaxTerms, BOOL* lAllOk);
void cdecl cmsxChromaticAdaptationAndNormalization(LPPROFILERCOMMONDATA hdr, LPcmsCIEXYZ xyz, BOOL lReverse);
void cdecl cmsxInitPCSViewingConditions(LPPROFILERCOMMONDATA hdr);
void cdecl cmsxComputeGamutHull(LPPROFILERCOMMONDATA hdr);
BOOL cdecl cmsxChoosePCS(LPPROFILERCOMMONDATA hdr);

/* Monitor profiler API ------------------------------------------------------------------- */

typedef struct {

           PROFILERCOMMONDATA hdr;
                                                
                     
           LPGAMMATABLE         Prelinearization[3];    /* Canonic gamma */
           LPGAMMATABLE         ReverseTables[3];       /* Reverse (direct) gamma */
           LPGAMMATABLE         PreLab[3];
           LPGAMMATABLE         PreLabRev[3];
         

           MAT3                 PrimariesMatrix;
           MAT3                 PrimariesMatrixRev; 
         

           } MONITORPROFILERDATA,FAR* LPMONITORPROFILERDATA;



BOOL   cdecl cmsxMonitorProfilerInit(LPMONITORPROFILERDATA sys);
BOOL   cdecl cmsxMonitorProfilerDo(LPMONITORPROFILERDATA sys);


/* Scanner profiler API ------------------------------------------------------------------- */


typedef struct {

           PROFILERCOMMONDATA hdr;
                                                                                   
           LPGAMMATABLE Prelinearization[3];        
                             
		   LPMATN HiTerms;      /* Regression matrix of many terms */
		   LPMATN LoTerms;      /* Low order regression matrix used for extrapolation */
		   
		   BOOL   lLocalConvergenceExtrapolation;
			 		                          

           } SCANNERPROFILERDATA,FAR* LPSCANNERPROFILERDATA;




BOOL   cdecl cmsxScannerProfilerInit(LPSCANNERPROFILERDATA sys);                                       
BOOL   cdecl cmsxScannerProfilerDo(LPSCANNERPROFILERDATA sys);

/* ----------------------------------------------------------- end of profilers */


#ifdef __cplusplus
}
#endif

#define __cmsprf_H
#endif