-
Notifications
You must be signed in to change notification settings - Fork 38
/
IccTagLut.cpp
5715 lines (4849 loc) · 146 KB
/
IccTagLut.cpp
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
/** @file
File: IccTagLut.cpp
Contains: Implementation of the Lut Tag classes
Version: V1
Copyright: � see ICC Software License
*/
/*
* The ICC Software License, Version 0.2
*
*
* Copyright (c) 2003-2012 The International Color Consortium. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. In the absence of prior written permission, the names "ICC" and "The
* International Color Consortium" must not be used to imply that the
* ICC organization endorses or promotes products derived from this
* software.
*
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE INTERNATIONAL COLOR CONSORTIUM OR
* ITS CONTRIBUTING MEMBERS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the The International Color Consortium.
*
*
* Membership in the ICC is encouraged when this software is used for
* commercial purposes.
*
*
* For more information on The International Color Consortium, please
* see <http://www.color.org/>.
*
*
*/
//////////////////////////////////////////////////////////////////////
// HISTORY:
//
// -Initial implementation by Max Derhak 5-15-2003
//
// -Moved LUT tags to separate file 4-30-2005
//
//////////////////////////////////////////////////////////////////////
#ifdef WIN32
#pragma warning( disable: 4786) //disable warning in <list.h>
#include <windows.h>
#endif
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include "IccTag.h"
#include "IccUtil.h"
#include "IccProfile.h"
#include "IccMpeBasic.h"
#ifdef USEREFICCMAXNAMESPACE
namespace refIccMAX {
#endif
/**
****************************************************************************
* Name: CIccCurve::Find
*
* Purpose: Read in the tag contents into a data block
*
* Args:
* v = index to be searched,
* v0 = index less than/equal to v,
* p0 = the value at index v0,
* v1 = index greater than/equal to v,
* p1 = value at index v1
*
* Return: The value at the requested index
*
*****************************************************************************
*/
icFloatNumber CIccCurve::Find(icFloatNumber v,
icFloatNumber p0, icFloatNumber v0,
icFloatNumber p1, icFloatNumber v1)
{
if (v<=v0)
return p0;
if (v>=v1)
return p1;
if (p1-p0 <= 0.00001) {
icFloatNumber d0 = fabs(v-v0);
icFloatNumber d1 = fabs(v1-v);
if (d0<d1)
return p0;
return p1;
}
icFloatNumber np = (icFloatNumber)((p0 + p1)/2.0);
icFloatNumber nv = Apply(np);
if (v<=nv) {
return Find(v, p0, v0, np, nv);
}
return Find(v, np, nv, p1, v1);
}
/**
****************************************************************************
* Name: CIccTagCurve::CIccTagCurve
*
* Purpose: Constructor
*
*****************************************************************************
*/
CIccTagCurve::CIccTagCurve(int nSize/*=0*/)
{
m_nSize = nSize;
if (m_nSize <0)
m_nSize = 0;
if (m_nSize>0)
m_Curve = (icFloatNumber*)calloc(nSize, sizeof(icFloatNumber));
else
m_Curve = NULL;
}
/**
****************************************************************************
* Name: CIccTagCurve::CIccTagCurve
*
* Purpose: Copy Constructor
*
* Args:
* ITCurve = The CIccTagCurve object to be copied
*****************************************************************************
*/
CIccTagCurve::CIccTagCurve(const CIccTagCurve &ITCurve)
{
m_nSize = ITCurve.m_nSize;
m_nMaxIndex = ITCurve.m_nMaxIndex;
m_Curve = (icFloatNumber*)calloc(m_nSize, sizeof(icFloatNumber));
memcpy(m_Curve, ITCurve.m_Curve, m_nSize*sizeof(icFloatNumber));
}
/**
****************************************************************************
* Name: CIccTagCurve::operator=
*
* Purpose: Copy Operator
*
* Args:
* CurveTag = The CIccTagCurve object to be copied
*****************************************************************************
*/
CIccTagCurve &CIccTagCurve::operator=(const CIccTagCurve &CurveTag)
{
if (&CurveTag == this)
return *this;
m_nSize = CurveTag.m_nSize;
m_nMaxIndex = CurveTag.m_nMaxIndex;
if (m_Curve)
free(m_Curve);
m_Curve = (icFloatNumber*)calloc(m_nSize, sizeof(icFloatNumber));
memcpy(m_Curve, CurveTag.m_Curve, m_nSize*sizeof(icFloatNumber));
return *this;
}
/**
****************************************************************************
* Name: CIccTagCurve::~CIccTagCurve
*
* Purpose: Destructor
*
*****************************************************************************
*/
CIccTagCurve::~CIccTagCurve()
{
if (m_Curve)
free(m_Curve);
}
/**
****************************************************************************
* Name: CIccTagCurve::Read
*
* Purpose: Read in the tag contents into a data block
*
* Args:
* size - # of bytes in tag,
* pIO - IO object to read tag from
*
* Return:
* true = successful, false = failure
*****************************************************************************
*/
bool CIccTagCurve::Read(icUInt32Number size, CIccIO *pIO)
{
icTagTypeSignature sig;
if (sizeof(icTagTypeSignature) +
sizeof(icUInt32Number) +
sizeof(icUInt32Number) > size)
return false;
if (!pIO) {
return false;
}
if (!pIO->Read32(&sig))
return false;
if (!pIO->Read32(&m_nReserved))
return false;
icUInt32Number nSize;
if (!pIO->Read32(&nSize))
return false;
if (!SetSize(nSize, icInitNone))
return false;
if (m_nSize) {
if (pIO->ReadUInt16Float(m_Curve, m_nSize)!=(icInt32Number)m_nSize)
return false;
}
return true;
}
/**
****************************************************************************
* Name: CIccTagCurve::Write
*
* Purpose: Write the tag to a file
*
* Args:
* pIO - The IO object to write tag to.
*
* Return:
* true = succesful, false = failure
*****************************************************************************
*/
bool CIccTagCurve::Write(CIccIO *pIO)
{
icTagTypeSignature sig = GetType();
if (!pIO)
return false;
if (!pIO->Write32(&sig))
return false;
if (!pIO->Write32(&m_nReserved))
return false;
if (!pIO->Write32(&m_nSize))
return false;
if (m_nSize)
if (pIO->WriteUInt16Float(m_Curve, m_nSize)!=(icInt32Number)m_nSize)
return false;
pIO->Align32();
return true;
}
/**
****************************************************************************
* Name: CIccTagCurve::Describe
*
* Purpose: Dump data associated with the tag to a string
*
* Args:
* sDescription - string to concatenate tag dump to
*****************************************************************************
*/
void CIccTagCurve::Describe(std::string &sDescription)
{
icChar buf[128], *ptr;
if (!m_nSize) {
sprintf(buf, "BEGIN_CURVE In_Out\r\n");
sDescription += buf;
sDescription += "Y = X\r\n";
}
else if (m_nSize==1) {
icFloatNumber dGamma = (icFloatNumber)(m_Curve[0] * 256.0);
sprintf(buf, "BEGIN_CURVE In_Out\r\n");
sDescription += buf;
sprintf(buf, "Y = X ^ %.4lf\r\n", dGamma);
sDescription += buf;
}
else {
int i;
sprintf(buf, "BEGIN_LUT In_Out 1 1\r\n");
sDescription += buf;
sDescription += "IN OUT\r\n";
for (i=0; i<(int)m_nSize; i++) {
ptr = buf;
icColorValue(buf, (icFloatNumber)i/(m_nSize-1), icSigMCH1Data, 1);
ptr += strlen(buf);
strcpy(ptr, " ");
ptr ++;
icColorValue(ptr, m_Curve[i], icSigMCH1Data, 1);
ptr += strlen(ptr);
strcpy(ptr, "\r\n");
sDescription += buf;
}
}
sDescription += "\r\n";
}
/**
****************************************************************************
* Name: CIccTagCurve::DumpLut
*
* Purpose: Dump data associated with the tag to a string. Basically has
* the same function as Describe()
*
* Args:
* sDescription = string to concatenate tag dump to,
* szName = name of the curve to be printed,
* csSig = color space signature of the LUT data,
* nIndex = the channel number of color space
*****************************************************************************
*/
void CIccTagCurve::DumpLut(std::string &sDescription, const icChar *szName,
icColorSpaceSignature csSig, int nIndex)
{
icChar buf[128], *ptr;
if (!m_nSize) {
sprintf(buf, "BEGIN_CURVE %s\r\n", szName);
sDescription += buf;
sDescription += "Y = X\r\n";
}
else if (m_nSize==1) {
icFloatNumber dGamma = (icFloatNumber)(m_Curve[0] * 256.0);
sprintf(buf, "BEGIN_CURVE %s\r\n", szName);
sDescription += buf;
sprintf(buf, "Y = X ^ %.4lf\r\n", dGamma);
sDescription += buf;
}
else {
int i;
sprintf(buf, "BEGIN_LUT %s 1 1\r\n", szName);
sDescription += buf;
sDescription += "IN OUT\r\n";
sDescription.reserve(sDescription.size() + m_nSize * 20);
for (i=0; i<(int)m_nSize; i++) {
ptr = buf;
icColorValue(buf, (icFloatNumber)i/(m_nSize-1), csSig, nIndex);
ptr += strlen(buf);
strcpy(ptr, " ");
ptr ++;
icColorValue(ptr, m_Curve[i], csSig, nIndex);
ptr += strlen(ptr);
strcpy(ptr, "\r\n");
sDescription += buf;
}
}
sDescription += "\r\n";
}
/**
****************************************************************************
* Name: CIccTagCurve::SetSize
*
* Purpose: Sets the size of the curve array.
*
* Args:
* nSize - number of entries in the curve,
* nSizeOpt - flag to zero newly formed values
*****************************************************************************
*/
bool CIccTagCurve::SetSize(icUInt32Number nSize, icTagCurveSizeInit nSizeOpt/*=icInitZero*/)
{
if (nSize==m_nSize)
return true;
if (!nSize && m_Curve) {
free(m_Curve);
m_Curve = NULL;
}
else {
if (!m_Curve)
m_Curve = (icFloatNumber*)malloc(nSize*sizeof(icFloatNumber));
else
m_Curve = (icFloatNumber*)icRealloc(m_Curve, nSize*sizeof(icFloatNumber));
if (!m_Curve) {
m_nSize = 0;
return false;
}
switch (nSizeOpt) {
case icInitNone:
default:
break;
case icInitZero:
if (m_nSize < nSize) {
memset(&m_Curve[m_nSize], 0, (nSize-m_nSize)*sizeof(icFloatNumber));
}
break;
case icInitIdentity:
if (nSize>1) {
icUInt32Number i;
icFloatNumber last = (icFloatNumber)(nSize-1);
for (i=0; i<nSize; i++) {
m_Curve[i] = (icFloatNumber)i/last;
}
}
else if (nSize==1) {
//Encode a gamma 1.0 u8Fixed8Number converted to 16 bit as a 0.0 to 1.0 float
m_Curve[0] = (icFloatNumber)(0x0100) / (icFloatNumber)65535.0;
}
break;
}
}
m_nSize = nSize;
m_nMaxIndex = (icUInt16Number)(nSize - 1);
return true;
}
/**
****************************************************************************
* Name: refIccMAX::CIccTagCurve::SetGamma
*
* Purpose: Set the curve with a single gamma value.
*
* Args:
* gamma - gamma value to use
*****************************************************************************
*/
bool CIccTagCurve::SetGamma(icFloatNumber gamma)
{
if (!SetSize(1, icInitNone))
return false;
icInt16Number whole = (icInt16Number)gamma;
icFloatNumber frac = gamma - (icFloatNumber)whole;
m_Curve[0] = (icFloatNumber)((whole * 256) + (frac*256.0)) / (icFloatNumber)65535.0;
return true;
}
/**
****************************************************************************
*
*****************************************************************************
*/
const icFloatNumber VERYSMALLNUM = (icFloatNumber)0.0000001;
static bool IsUnity(const icFloatNumber& num)
{
return (num>(1.0-VERYSMALLNUM) && num<(1.0+VERYSMALLNUM));
}
/**
****************************************************************************
* Name: CIccTagCurve::IsIdentity
*
* Purpose: Checks if this is an identity curve.
*
* Return: true if the curve is an identity
*
*****************************************************************************
*/
bool CIccTagCurve::IsIdentity()
{
if (!m_nSize) {
return true;
}
if (m_nSize==1) {
return IsUnity(icFloatNumber(m_Curve[0]*65535.0/256.0));
}
icUInt32Number i;
for (i=0; i<m_nSize; i++) {
if (fabs(m_Curve[i]-((icFloatNumber)i/(icFloatNumber)m_nMaxIndex))>VERYSMALLNUM) {
return false;
}
}
return true;
}
/**
****************************************************************************
* Name: CIccTagCurve::Apply
*
* Purpose: Applies the curve to the value passed.
*
* Args:
* v = value to be passed through the curve.
*
* Return: The value modified by the curve.
*
*****************************************************************************
*/
icFloatNumber CIccTagCurve::Apply(icFloatNumber v) const
{
if(v<0.0) v = 0.0;
else if(v>1.0) v = 1.0;
icUInt32Number nIndex = (icUInt32Number)(v * m_nMaxIndex);
if (!m_nSize) {
return v;
}
if (m_nSize==1) {
//Convert 0.0 to 1.0 float to 16bit and then convert from u8Fixed8Number
icFloatNumber dGamma = (icFloatNumber)(m_Curve[0] * 65535.0 / 256.0);
return pow(v, dGamma);
}
if (nIndex == m_nMaxIndex) {
return m_Curve[nIndex];
}
else {
icFloatNumber nDif = v*m_nMaxIndex - nIndex;
icFloatNumber p0 = m_Curve[nIndex];
icFloatNumber rv = p0 + (m_Curve[nIndex+1]-p0)*nDif;
if (rv>1.0)
rv=1.0;
return rv;
}
}
/**
******************************************************************************
* Name: CIccTagCurve::Validate
*
* Purpose: Check tag data validity.
*
* Args:
* sig = signature of tag being validated,
* sReport = String to add report information to
*
* Return:
* icValidateStatusOK if valid, or other error status.
******************************************************************************
*/
icValidateStatus CIccTagCurve::Validate(std::string sigPath, std::string &sReport, const CIccProfile* pProfile/*=NULL*/) const
{
icValidateStatus rv = CIccTag::Validate(sigPath, sReport, pProfile);
CIccInfo Info;
std::string sSigPathName = Info.GetSigPathName(sigPath);
icSignature sig = icGetFirstSigPathSig(sigPath);
if (sig==icSigBlueTRCTag || sig==icSigRedTRCTag || sig==icSigGreenTRCTag || sig==icSigGrayTRCTag) {
if (m_nSize>1) {
if (m_Curve) {
if (m_Curve[0]>0.0 || m_Curve[m_nSize-1]<1.0) {
sReport += icValidateWarningMsg;
sReport += sSigPathName;
sReport += " - Curve cannot be accurately inverted.\r\n";
rv = icMaxStatus(rv, icValidateWarning);
}
}
}
}
return rv;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::CIccTagParametricCurve
*
* Purpose: Constructor
*
*****************************************************************************
*/
CIccTagParametricCurve::CIccTagParametricCurve()
{
m_nFunctionType = 0xffff;
m_nNumParam = 0;
m_dParam = NULL;
m_nReserved2 = 0;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::CIccTagParametricCurve
*
* Purpose: Copy Constructor
*
* Args:
* ITPC = The CIccTagParametricCurve object to be copied
*****************************************************************************
*/
CIccTagParametricCurve::CIccTagParametricCurve(const CIccTagParametricCurve &ITPC)
{
m_nFunctionType = ITPC.m_nFunctionType;
m_nNumParam = ITPC.m_nNumParam;
m_dParam = new icFloatNumber[m_nNumParam];
memcpy(m_dParam, ITPC.m_dParam, m_nNumParam*sizeof(icFloatNumber));
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::operator=
*
* Purpose: Copy Operator
*
* Args:
* ParamCurveTag = The CIccTagParametricCurve object to be copied
*****************************************************************************
*/
CIccTagParametricCurve &CIccTagParametricCurve::operator=(const CIccTagParametricCurve &ParamCurveTag)
{
if (&ParamCurveTag == this)
return *this;
m_nFunctionType = ParamCurveTag.m_nFunctionType;
m_nNumParam = ParamCurveTag.m_nNumParam;
if (m_dParam)
delete [] m_dParam;
m_dParam = new icFloatNumber[m_nNumParam];
memcpy(m_dParam, ParamCurveTag.m_dParam, m_nNumParam*sizeof(icFloatNumber));
return *this;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::~CIccTagParametricCurve
*
* Purpose: Destructor
*
*****************************************************************************
*/
CIccTagParametricCurve::~CIccTagParametricCurve()
{
if (m_dParam)
delete [] m_dParam;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::Read
*
* Purpose: Read in the tag contents into a data block
*
* Args:
* size - # of bytes in tag,
* pIO - IO object to read tag from
*
* Return:
* true = successful, false = failure
*****************************************************************************
*/
bool CIccTagParametricCurve::Read(icUInt32Number size, CIccIO *pIO)
{
icTagTypeSignature sig;
icUInt16Number nFunctionType;
icUInt32Number nHdrSize = sizeof(icTagTypeSignature) +
sizeof(icUInt32Number) +
2*sizeof(icUInt16Number);
if ( nHdrSize > size)
return false;
if (!pIO) {
return false;
}
if (!pIO->Read32(&sig) ||
!pIO->Read32(&m_nReserved) ||
!pIO->Read16(&nFunctionType) ||
!pIO->Read16(&m_nReserved2))
return false;
SetFunctionType(nFunctionType);
if (!m_nNumParam) {
m_nNumParam = (icUInt16Number)((size-nHdrSize) / sizeof(icS15Fixed16Number));
m_dParam = new icFloatNumber[m_nNumParam];
}
if (m_nNumParam) {
int i;
if (nHdrSize + m_nNumParam*sizeof(icS15Fixed16Number) > size)
return false;
for (i=0; i<m_nNumParam; i++) {
icS15Fixed16Number num;
if (!pIO->Read32(&num, 1))
return false;
m_dParam[i]=icFtoD(num);
}
}
return true;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::Write
*
* Purpose: Write the tag to a file
*
* Args:
* pIO - The IO object to write tag to.
*
* Return:
* true = succesful, false = failure
*****************************************************************************
*/
bool CIccTagParametricCurve::Write(CIccIO *pIO)
{
icTagTypeSignature sig;
if (!pIO) {
return false;
}
sig = GetType();
if (!pIO->Write32(&sig) ||
!pIO->Write32(&m_nReserved) ||
!pIO->Write16(&m_nFunctionType) ||
!pIO->Write16(&m_nReserved2))
return false;
if (m_nNumParam) {
int i;
for (i=0; i<m_nNumParam; i++) {
icS15Fixed16Number num = icDtoF(m_dParam[i]);
if (!pIO->Write32(&num, 1))
return false;
}
}
if (!pIO->Align32())
return false;
return true;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::Describe
*
* Purpose: Dump data associated with the tag to a string
*
* Args:
* sDescription - string to concatenate tag dump to
*****************************************************************************
*/
void CIccTagParametricCurve::Describe(std::string &sDescription)
{
icChar buf[128];
sprintf(buf, "FunctionType: %04Xh\r\n", m_nFunctionType);
sDescription += buf;
switch(m_nFunctionType) {
case 0x0000:
sprintf(buf, "Y = X ^ %.4lf\r\n", m_dParam[0]);
sDescription += buf;
return;
case 0x0001:
sprintf(buf, "Y = 0 when (X < %.4lf / %.4lf)\r\n",
-m_dParam[2], m_dParam[1]);
sDescription += buf;
sprintf(buf, "Y = (%.4lf * X + %.4lf) ^ %.4lf when (X >= %.4lf / %.4lf)\r\n",
m_dParam[1], m_dParam[2], m_dParam[0],
m_dParam[2], m_dParam[1]);
sDescription += buf;
return;
case 0x0002:
sprintf(buf, "Y = %.4lf when (X < %.4lf / %.4lf)\r\n", m_dParam[3],
-m_dParam[2], m_dParam[1]);
sDescription += buf;
sprintf(buf, "Y = (%.4lf * X + %.4lf) ^ %.4lf + %.4lf when (X >= %.4lf / %.4lf)\r\n",
m_dParam[1], m_dParam[2], m_dParam[0],
m_dParam[3],
-m_dParam[2], m_dParam[1]);
sDescription += buf;
return;
case 0x0003:
sprintf(buf, "Y = %lf * X when (X < %.4lf)\r\n",
m_dParam[3], m_dParam[4]);
sDescription += buf;
sprintf(buf, "Y = (%.4lf * X + %.4lf) ^ %.4lf when (X >= %.4lf)\r\n",
m_dParam[1], m_dParam[2], m_dParam[0],
m_dParam[4]);
sDescription += buf;
return;
case 0x0004:
sprintf(buf, "Y = %lf * X + %.4lf when (X < %.4lf)\r\n",
m_dParam[3], m_dParam[6], m_dParam[4]);
sDescription += buf;
sprintf(buf, "Y = (%.4lf * X + %.4lf) ^ %.4lf + %.4lf when (X >= %.4lf)\r\n",
m_dParam[1], m_dParam[2], m_dParam[0],
m_dParam[5], m_dParam[4]);
sDescription += buf;
return;
default:
int i;
sprintf(buf, "Unknown Function with %d parameters:\r\n", m_nNumParam);
sDescription += buf;
for (i=0; i<m_nNumParam; i++) {
sprintf(buf, "Param[%d] = %.4lf\r\n", i, m_dParam[i]);
sDescription += buf;
}
}
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::DumpLut
*
* Purpose: Dump data associated with the tag to a string. Basically has
* the same function as Describe()
*
* Args:
* sDescription = string to concatenate tag dump to,
* szName = name of the curve to be printed,
* csSig = color space signature of the curve data,
* nIndex = the channel number of color space
*****************************************************************************
*/
void CIccTagParametricCurve::DumpLut(std::string &sDescription, const icChar *szName,
icColorSpaceSignature csSig, int nIndex)
{
icChar buf[128];
sprintf(buf, "BEGIN_CURVE %s\r\n", szName);
sDescription += buf;
Describe(sDescription);
sDescription += "\r\n";
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::SetFunctionType
*
* Purpose: Sets the type of the function the Parametric curve represents
*
* Args:
* nFunctionType = the type of the function encoded as 0-4
*
* Return:
* always true!!
*****************************************************************************
*/
bool CIccTagParametricCurve::SetFunctionType(icUInt16Number nFunctionType)
{
icUInt16Number nNumParam;
switch(nFunctionType) {
case 0x0000:
nNumParam = 1;
break;
case 0x0001:
nNumParam = 3;
break;
case 0x0002:
nNumParam = 4;
break;
case 0x0003:
nNumParam = 5;
break;
case 0x0004:
nNumParam = 7;
break;
default:
nNumParam = 0;
}
if (m_dParam)
delete m_dParam;
m_nNumParam = nNumParam;
m_nFunctionType = nFunctionType;
if (m_nNumParam)
m_dParam = new icFloatNumber[m_nNumParam];
else
m_dParam = NULL;
return true;
}
/**
****************************************************************************
* Name: CIccTagParametricCurve::IsIdentity
*
* Purpose: Checks if this is an identity curve.
*
* Return: true if the curve is an identity
*
*****************************************************************************
*/
bool CIccTagParametricCurve::IsIdentity()
{
switch(m_nFunctionType) {
case 0x0000:
return IsUnity(m_dParam[0]);
case 0x0001:
case 0x0002:
case 0x0003:
case 0x0004:
return false;