File: lcmswrapper.cpp

package info (click to toggle)
photoprint 0.4.1-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 10,568 kB
  • ctags: 4,517
  • sloc: cpp: 46,643; sh: 10,360; ansic: 4,755; makefile: 543; sed: 16
file content (731 lines) | stat: -rw-r--r-- 15,601 bytes parent folder | download
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
/*
 * lcmswrapper.cpp - encapsulates typical "user" functions of LittleCMS,
 * providing a Profile and Transform class
 *
 * Copyright (c) 2004-2008 by Alastair M. Robinson
 * Distributed under the terms of the GNU General Public License -
 * see the file named "COPYING" for more details.
 *
 * 2005-05-01: No longer depends on ini.h - the neccesary data for generating
 *             rough-cast profiles is now encapsulated in suitable classes.
 * 2006-09-04: Added filename, since LCMS doesn't provide pointer to raw data
 *             which is needed for TIFF embedding...
 *
 * TODO: pixel type, support Lab, XYZ, etc.
 *
 */

#include <iostream>
#include <fstream>

#include "../support/debug.h"

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "gettext.h"
#define _(x) gettext(x)
#define N_(x) gettext_noop(x)

#include "imagesource.h"
#include "lcmswrapper.h"

using namespace std;

CMSRGBPrimaries CMSPrimaries_Rec709(.64,.33,.3,.6,.15,.06);
CMSRGBPrimaries CMSPrimaries_Adobe(0.64, 0.33,0.21, 0.71,0.15, 0.06);
CMSRGBPrimaries CMSPrimaries_NTSC(0.67, 0.33, 0.21, 0.71,0.14, 0.08);
CMSRGBPrimaries CMSPrimaries_EBU(0.64, 0.33, 0.29, 0.60, 0.15, 0.06);
CMSRGBPrimaries CMSPrimaries_SMPTE(0.63, 0.34, 0.31, 0.595, 0.155, 0.070);
CMSRGBPrimaries CMSPrimaries_HDTV(0.670, 0.330, 0.210, 0.710,0.150, 0.060);
CMSRGBPrimaries CMSPrimaries_CIE(0.7355,0.2645,0.2658,0.7243,0.1669,0.0085);

CMSProfile::CMSProfile(const char *fn) : md5(NULL), generated(false), filename(NULL), buffer(NULL), buflen(0)
{
	if(!fn)
		throw "NULL profile filename provided";

	cmsErrorAction(LCMS_ERROR_SHOW);

	filename=strdup(fn);

	if(!(prof=cmsOpenProfileFromFile(filename,"r")))
		throw "Can't open profile";

	CalcMD5();
}


void CMSProfile::CalcMD5()
{
	if(generated)
	{
		Debug[TRACE] << "Saving profile to RAM for MD5 calculation." << endl;
		size_t plen=0;
		_cmsSaveProfileToMem(prof,NULL,&plen);
		if(plen>0)
		{
			Debug[TRACE] << "Plen = " << plen << endl;
			buflen=plen;
			buffer=(char *)malloc(buflen);
			if(_cmsSaveProfileToMem(prof,buffer,&plen))
			{
				Debug[TRACE] << "Saved successfully" << endl;
				md5=new MD5Digest(buffer+sizeof(icHeader),buflen-sizeof(icHeader));
			}
		}
	}
	else if(filename)
	{
		ifstream f(filename);
		f.seekg(0,ios::end);
		int filelen=f.tellg();
		f.seekg(0);

		char *data=(char *)malloc(filelen);
		f.read((char *)data,filelen);
		f.close();
		md5=new MD5Digest(data+sizeof(icHeader),filelen-sizeof(icHeader));
		free(data);
	}
	else
	{
		md5=new MD5Digest(buffer+sizeof(icHeader),buflen-sizeof(icHeader));
	}
}


CMSProfile::CMSProfile(CMSRGBPrimaries &primaries,CMSRGBGamma &gamma,CMSWhitePoint &whitepoint)
	: md5(NULL), generated(true), filename(NULL), buffer(NULL), buflen(0)
{
	if(!(prof=cmsCreateRGBProfile(&whitepoint.whitepoint,&primaries,gamma.gammatables)))
		throw "Can't create virtual RGB profile";
	CalcMD5();
}


CMSProfile::CMSProfile(CMSGamma &gamma,CMSWhitePoint &whitepoint)
	: md5(NULL), generated(true), filename(NULL), buffer(NULL), buflen(0)
{
	if(!(prof=cmsCreateGrayProfile(&whitepoint.whitepoint,gamma.GetGammaTable())))
		throw "Can't create virtual Grey profile";
	CalcMD5();
}


CMSProfile::CMSProfile(CMSWhitePoint &whitepoint)
	: md5(NULL), generated(true), filename(NULL), buffer(NULL), buflen(0)
{
	if(!(prof=cmsCreateLabProfile(&whitepoint.whitepoint)))
		throw "Can't create virtual LAB profile";
	CalcMD5();
}


CMSProfile::CMSProfile(char *srcbuf,int length)
	: md5(NULL), generated(false), filename(NULL), buffer(NULL), buflen(0)
{
	buffer=(char *)malloc(length);
	buflen=length;
	memcpy(buffer,srcbuf,buflen);
	if(!(prof=cmsOpenProfileFromMem(buffer,buflen)))
		throw "Can't open profile";
	CalcMD5();
}


CMSProfile::CMSProfile()
	: md5(NULL), generated(true), filename(NULL), buffer(NULL), buflen(0)
{
	if(!(prof=cmsCreate_sRGBProfile()))
		throw "Can't create virtual sRGB profile";
	CalcMD5();
}


CMSProfile::CMSProfile(const CMSProfile &src)
	: md5(NULL), generated(src.generated), filename(NULL), buffer(NULL), buflen(0)
{
	Debug[TRACE] << "In CMSProfile Copy Constructor" << endl;
	if(src.filename)
	{
		filename=strdup(src.filename);

		if(!(prof=cmsOpenProfileFromFile(filename,"r")))
			throw "Can't open profile";
	}
	else
	{
		buffer=(char *)malloc(src.buflen);
		buflen=src.buflen;
		memcpy(buffer,src.buffer,src.buflen);
		if(!(prof=cmsOpenProfileFromMem(buffer,buflen)))
			throw "Can't open profile";
	}
	md5=new MD5Digest(*src.md5);
}


CMSProfile::~CMSProfile()
{
	if(filename)
		free(filename);
	if(prof)
		cmsCloseProfile(prof);
	if(buffer)
		free(buffer);
	if(md5)
		delete md5;
}


bool CMSProfile::operator==(const CMSProfile &other)
{
	if(md5&&other.md5)
	{
		return(*md5==*other.md5);
	}
	else
		return(false);
}


bool CMSProfile::IsDeviceLink()
{
	return(cmsGetDeviceClass(prof) == icSigLinkClass);
}


bool CMSProfile::IsV4()
{
	Debug[TRACE] << "Profile version: " << cmsGetProfileICCversion(prof) << endl;
	return(cmsGetProfileICCversion(prof) >= 0x04000000L);
}


enum IS_TYPE CMSProfile::GetColourSpace()
{
	icColorSpaceSignature sig=cmsGetColorSpace(prof);
	switch(sig)
	{
		case icSigGrayData:
			return(IS_TYPE_GREY);
			break;
		case icSigRgbData:
			return(IS_TYPE_RGB);
			break;
		case icSigCmykData:
			return(IS_TYPE_CMYK);
			break;
		default:
			return(IS_TYPE_NULL);
			break;
	}
}


enum IS_TYPE CMSProfile::GetDeviceLinkOutputSpace()
{
	if(!IsDeviceLink())
		throw "GetDeviceLinkOutputSpace() can only be used on DeviceLink profiles!";
	icColorSpaceSignature sig=cmsGetPCS(prof);
	switch(sig)
	{
		case icSigGrayData:
			return(IS_TYPE_GREY);
			break;
		case icSigRgbData:
			return(IS_TYPE_RGB);
			break;
		case icSigCmykData:
			return(IS_TYPE_CMYK);
			break;
		default:
			return(IS_TYPE_NULL);
			break;
	}
}


const char *CMSProfile::GetName()
{
	const char *txt=cmsTakeProductName(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


const char *CMSProfile::GetManufacturer()
{
	const char *txt=cmsTakeManufacturer(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


const char *CMSProfile::GetModel()
{
	const char *txt=cmsTakeModel(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


const char *CMSProfile::GetDescription()
{
	const char *txt=cmsTakeProductDesc(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


const char *CMSProfile::GetInfo()
{
	const char *txt=cmsTakeProductInfo(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


const char *CMSProfile::GetCopyright()
{
	const char *txt=cmsTakeCopyright(prof);
	if(txt)
		return(txt);
	else
		return("unknown");
}


MD5Digest *CMSProfile::GetMD5()
{
	return(md5);
}


const char *CMSProfile::GetFilename()
{
	return(filename);
}


bool CMSProfile::Save(const char *outfn)
{
	try
	{
//		(Since we now save a generated profile to memory so we can calc an MD5 hash
//		we can simply save that, like an embedded profile, if we need to.
//		if(generated)	// If profile was generated on the fly, then we use LCMS function
//		{				// to save it;
//			return(_cmsSaveProfile(prof,outfn));
//		}
//		else
//		{
			if(filename)	// If profile was loaded from disk, we need to load it again
			{
				ifstream f(filename);
				f.exceptions(fstream::badbit);
				f.seekg(0,ios::end);
				buflen=f.tellg();
				f.seekg(0);

				buffer=(char *)malloc(buflen);
				f.read((char *)buffer,buflen);
				f.close();
			}
			if(outfn)
			{
				Debug[TRACE] << "Saving buffer: " << long(buffer) << ", length: " << buflen << endl;
				ofstream f(outfn,ios::out|ios::binary);
				f.write(buffer,buflen);
				f.close();
			}
			if(filename)	// Did we load the profile from disk?  Need to free buffer if so
			{
				free(buffer);
				buffer=NULL;
				buflen=0;
			}
//		}
	}
	catch(fstream::failure e)
	{
		return(false);
	}
	return(true);
}


std::ostream& operator<<(std::ostream &s,CMSProfile &cp)
{
	return(s << cp.GetDescription());
}


CMSTransform::CMSTransform()
{
	inputtype=IS_TYPE_NULL;
	outputtype=IS_TYPE_NULL;
}


CMSTransform::CMSTransform(CMSProfile *in,CMSProfile *out,LCMSWrapper_Intent intent)
{
	inputtype=in->GetColourSpace();
	outputtype=out->GetColourSpace();
	MakeTransform(in,out,intent);
}


CMSTransform::CMSTransform(CMSProfile *devicelink,LCMSWrapper_Intent intent)
{
	inputtype=devicelink->GetColourSpace();
	outputtype=devicelink->GetDeviceLinkOutputSpace();
	MakeTransform(devicelink,NULL,intent);	
}


CMSTransform::CMSTransform(CMSProfile *profiles[],int profilecount,LCMSWrapper_Intent intent)
{
	inputtype=profiles[0]->GetColourSpace();
	if(profiles[profilecount-1]->IsDeviceLink())
		outputtype=profiles[profilecount-1]->GetDeviceLinkOutputSpace();
	else
		outputtype=profiles[profilecount-1]->GetColourSpace();

	cmsHPROFILE *p;
	if((p=(cmsHPROFILE *)malloc(sizeof(cmsHPROFILE)*profilecount)))
	{
		for(int i=0;i<profilecount;++i)
		{
			p[i]=profiles[i]->prof;
		}

		int it,ot;

		switch(inputtype)
		{
			case IS_TYPE_GREY:
				it=TYPE_GRAY_16_REV;
				break;
			case IS_TYPE_RGB:
				it=TYPE_RGB_16;
				break;
			case IS_TYPE_CMYK:
				it=TYPE_CMYK_16;
				break;
			default:
				throw "Unsupported colour space (input)";
				break;
		}
	
		switch(outputtype)
		{
			case IS_TYPE_GREY:
				ot=TYPE_GRAY_16_REV;
				break;
			case IS_TYPE_RGB:
				ot=TYPE_RGB_16;
				break;
			case IS_TYPE_CMYK:
				ot=TYPE_CMYK_16;
				break;
			default:
				throw "Unsupported colour space (output)";
				break;
		}
	
		transform = cmsCreateMultiprofileTransform(p,profilecount,
			it,
			ot,
			CMS_GetLCMSIntent(intent), CMS_GetLCMSFlags(intent));

		free(p);	
	}
	else
		throw "Can't create multi-profile transform";
}


CMSTransform::~CMSTransform()
{
	if(transform)
		cmsDeleteTransform(transform);
}


void CMSTransform::Transform(unsigned short *src,unsigned short *dst,int pixels)
{
	cmsDoTransform(transform,src,dst,pixels);
}


IS_TYPE CMSTransform::GetInputColourSpace()
{
	return(inputtype);
}


IS_TYPE CMSTransform::GetOutputColourSpace()
{
	return(outputtype);
}


void CMSTransform::MakeTransform(CMSProfile *in,CMSProfile *out,LCMSWrapper_Intent intent)
{
	int it,ot;

	switch(GetInputColourSpace())
	{
		case IS_TYPE_GREY:
			it=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			it=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			it=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (input)";
			break;
	}

	switch(GetOutputColourSpace())
	{
		case IS_TYPE_GREY:
			ot=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			ot=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			ot=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (output)";
			break;
	}

	transform = cmsCreateTransform(in->prof,
		it,
		(out ? out->prof : NULL),
		ot,
		CMS_GetLCMSIntent(intent), CMS_GetLCMSFlags(intent));
}


// FIXME - this will need to be updated if LCMS supports new intents at some point
// in the future...

static const char *intent_names[]=
{
	N_("Perceptual"),
	N_("Relative Colorimetric"),
	N_("Relative Colorimetric with BPC"),
	N_("Saturation"),
	N_("Absolute Colorimetric")
};

static const char *intent_descriptions[]=
{
	N_("Aims for pleasing photographic results by preserving relationships between colours, and avoiding clipping of unattainable colours"),
	N_("Attempts to preserve the original colours, relative to white-point differences."),
	N_("Attempts to preserve the original colours, relative to white-point differences.  Uses Black Point Compensation to avoid clipping of highlights and shadows."),
	N_("Attempts to provide brighter, more saturated colours, at the expense of colour relationships."),
	N_("Mimics the original colours (including white point) as closely as possible, for side-by-side comparisons.  Clips unattainable colours.")
};


static const int intent_lcmsintents[]=
{
	INTENT_PERCEPTUAL,
	INTENT_RELATIVE_COLORIMETRIC,
	INTENT_RELATIVE_COLORIMETRIC,
	INTENT_SATURATION,
	INTENT_ABSOLUTE_COLORIMETRIC
};


static const int intent_lcmsflags[]=
{
	0,
	0,
	cmsFLAGS_BLACKPOINTCOMPENSATION,
	0,
	0
};


int CMS_GetIntentCount()
{
	return(sizeof(intent_names)/sizeof(intent_names[0]));
}

const char *CMS_GetIntentName(int intent)
{
	if(intent<CMS_GetIntentCount() && intent>=0)
		return(gettext(intent_names[intent]));
	return(NULL);
}

const char *CMS_GetIntentDescription(int intent)
{
	if(intent<CMS_GetIntentCount() && intent>=0)
		return(gettext(intent_descriptions[intent]));
	return(NULL);
}


const int CMS_GetLCMSIntent(int intent)
{
	if(intent<CMS_GetIntentCount() && intent>=0)
		return(intent_lcmsintents[intent]);
	return(0);
}


const int CMS_GetLCMSFlags(int intent)
{
	if(intent<CMS_GetIntentCount() && intent>=0)
		return(intent_lcmsflags[intent]);
	return(0);
}


CMSProofingTransform::CMSProofingTransform(CMSProfile *in,CMSProfile *out,CMSProfile *proof,int proofintent,int viewintent)
{
	inputtype=in->GetColourSpace();
	if(out)
		outputtype=out->GetColourSpace();
	else
		outputtype=in->GetDeviceLinkOutputSpace();
	
	int it,ot;

	switch(inputtype)
	{
		case IS_TYPE_GREY:
			it=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			it=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			it=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (input)";
			break;
	}

	switch(outputtype)
	{
		case IS_TYPE_GREY:
			ot=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			ot=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			ot=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (output)";
			break;
	}

//	if(in->GetFilename())
//		Debug[TRACE] << "In: " << in->GetFilename() << endl;
//	Debug[TRACE] << "In: " << in->GetDescription() << endl;
	if(out)
	{
//		if(out->GetFilename())
//			Debug[TRACE] << "Out: " << out->GetFilename() << endl;
//		Debug[TRACE] << "Out: " << out->GetDescription() << endl;
	}
//	if(proof->GetFilename())
//		Debug[TRACE] << "Proof: " << proof->GetFilename() << endl;
//	Debug[TRACE] << "Proof: " << proof->GetDescription() << endl;

//	Debug[TRACE] << "Viewing intent: " << viewintent << endl;
//	Debug[TRACE] << "Rendering intent: " << proofintent << endl;

	transform = cmsCreateProofingTransform(in->prof,
		it,
		(out ? out->prof : NULL),
		ot,
		proof->prof,
		CMS_GetLCMSIntent(proofintent),
		CMS_GetLCMSIntent(viewintent), CMS_GetLCMSFlags(proofintent)|cmsFLAGS_SOFTPROOFING);
}


CMSProofingTransform::CMSProofingTransform(CMSProfile *devicelink,CMSProfile *proof,int proofintent,int viewintent)
{
	inputtype=devicelink->GetColourSpace();
	outputtype=devicelink->GetDeviceLinkOutputSpace();
	int it,ot;

	switch(inputtype)
	{
		case IS_TYPE_GREY:
			it=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			it=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			it=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (input)";
			break;
	}

	switch(outputtype)
	{
		case IS_TYPE_GREY:
			ot=TYPE_GRAY_16_REV;
			break;
		case IS_TYPE_RGB:
			ot=TYPE_RGB_16;
			break;
		case IS_TYPE_CMYK:
			ot=TYPE_CMYK_16;
			break;
		default:
			throw "Unsupported colour space (output)";
			break;
	}

//	if(devicelink->GetFilename())
//		Debug[TRACE] << "DeviceLink: " << devicelink->GetFilename() << endl;
//	Debug[TRACE] << "DeviceLink: " << devicelink->GetDescription() << endl;
//	if(proof->GetFilename())
//		Debug[TRACE] << "Proof: " << proof->GetFilename() << endl;
//	Debug[TRACE] << "Proof: " << proof->GetDescription() << endl;

//	Debug[TRACE] << "Viewing intent: " << viewintent << endl;
//	Debug[TRACE] << "Rendering intent: " << proofintent << endl;

	transform = cmsCreateProofingTransform(devicelink->prof,
		it,
		NULL,
		ot,
		proof->prof,
		CMS_GetLCMSIntent(proofintent),
		CMS_GetLCMSIntent(viewintent), CMS_GetLCMSFlags(proofintent)|cmsFLAGS_SOFTPROOFING);
}