summaryrefslogtreecommitdiff
path: root/src/pnmlcms.c
blob: 0a8fdac2589287ce3c38597af4dac1203588eaba (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
#include <lcms.h>

#include "pnm.h"

static int
pam_transform(FILE *in_fp, FILE *out_fp,
	const struct pnm *in_pnm, cmsHTRANSFORM *hTransform);

int
main(int argc, char **argv) {
	int c, i, nProf = 0;
	char *in_prof = NULL, *out_prof = NULL;
	double gamma = 0.0, bright = 0.0, contrast = 1.0, hue = 0.0;
	double saturation = 0.0;
	int tempSrc = 5000, tempDst = 5000;
	cmsHTRANSFORM hTransform;
	cmsHPROFILE profiles[3];
	DWORD type;
	struct pnm in_pnm;

	while ((c = getopt(argc, argv, "i:o:g:b:c:h:s:S:D:")) != EOF) {
		switch (c) {  
			case 'i':
				in_prof = optarg;
				break;
			case 'o':
				out_prof = optarg;
				break;
			case 'g':
				gamma = atof(optarg);
				break;
			case 'b':
				bright = atof(optarg);
				break;
			case 'c':
				contrast = atof(optarg);
				break;
			case 'h':
				hue = atof(optarg);
				break;
			case 's':
				saturation = atof(optarg);
				break;
			case 'S':
				tempSrc = atoi(optarg);
				break;
			case 'D':
				tempDst = atoi(optarg);
				break;

			default:
				break;
		}
	}

	if (in_prof) {
		profiles[nProf++] = cmsOpenProfileFromFile(in_prof, "r");
	} else {
		profiles[nProf++] = cmsCreate_sRGBProfile();
	}

	if (gamma) {
		LPGAMMATABLE gTable[3];

		gTable[0] = cmsBuildGamma(256, gamma);
		gTable[1] = gTable[2] = cmsBuildGamma(256, 1.0);

		profiles[nProf++] = cmsCreateLinearizationDeviceLink(
			icSigLabData,
			gTable);
	}

	profiles[nProf++] = cmsCreateBCHSWabstractProfile(
		10,
		bright,
		contrast,
		hue,
		saturation,
		tempSrc,
		tempDst);

	if (out_prof) {
		profiles[nProf++] = cmsOpenProfileFromFile(out_prof, "r");
	} else {
		profiles[nProf++] = cmsCreate_sRGBProfile();
	}

	if (readPnmHeader(stdin, &in_pnm) != 0) {
		exit(1);
	}

	if (in_pnm.maxval == 255) {
		type = TYPE_RGB_8;
	} else if (in_pnm.maxval == 65535) {
		type = TYPE_RGB_16_SE;
	} else {
		fprintf(stderr, "unsupported PNM maxval %d\n", in_pnm.maxval);
		exit(1);
	}

	hTransform = cmsCreateMultiprofileTransform(
		profiles,
		nProf,
		type,
		type,
		INTENT_PERCEPTUAL,
		cmsFLAGS_BLACKPOINTCOMPENSATION);
	
	writePnmHeader(stdout, &in_pnm);

	pam_transform(stdin, stdout, &in_pnm, &hTransform);

	cmsDeleteTransform(hTransform);
	
	for (i = 0; i < nProf; i++) {
	//	cmsCloseProfile(profiles[i]);
	}

	return 0;
}

static int
pam_transform(FILE *in_fp, FILE *out_fp,
	const struct pnm *in_pnm, cmsHTRANSFORM *hTransform)
{
	int row, col;
	int nbytes = in_pnm->maxval == 65535?2:1;
	char *buf = malloc(in_pnm->width * nbytes * 3);

	for (row = 0; row < in_pnm->height; row++) {
		fread(buf, in_pnm->width, nbytes * 3, in_fp);

		cmsDoTransform(*hTransform, buf, buf, in_pnm->width);

		fwrite(buf, in_pnm->width, nbytes * 3, out_fp);
	}

	free(buf);
	return 0;
}