summaryrefslogtreecommitdiff
path: root/xicc/extractttag.c
blob: 2c26bdab13f14b7197a706facefd280c32dc05ac (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

/* 
 * Extract a CGATS file from an ICC profile tag.
 * (Can also extract a tag of unknown format as a binary lump).
 *
 * Author:  Graeme W. Gill
 * Date:    2008/5/18
 * Version: 1.00
 *
 * Copyright 2008 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:
 *
 * Should uncompress ZXML type tag using zlib.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <fcntl.h>
#include <string.h>
#include <math.h>
#include "copyright.h"
#include "aconfig.h"
#include "numlib.h"
#include "icc.h"
#include "xicc.h"
#include "ui.h"

#define MXTGNMS 30

void usage(char *diag, ...) {
	int i;
	fprintf(stderr,"Extract a text tag from an ICC profile, Version %s\n",ARGYLL_VERSION_STR);
	fprintf(stderr,"Author: Graeme W. Gill, licensed under the AGPL Version 3\n");
	if (diag != NULL) {
		va_list args;
		fprintf(stderr,"Diagnostic: ");
		va_start(args, diag);
		vfprintf(stderr, diag, args);
		va_end(args);
		fprintf(stderr,"\n");
	}
	fprintf(stderr,"usage: extractttag  [-v] infile%s outfile\n",ICC_FILE_EXT);
	fprintf(stderr," -v            Verbose\n");
	fprintf(stderr," -t tag        Extract this tag rather than default 'targ'\n");
	fprintf(stderr," -c            Extract calibration file from 'targ' tag\n");
	exit(1);
}

int
main(int argc, char *argv[]) {
	int fa,nfa;					/* argument we're looking at */
	char in_name[MAXNAMEL+1];	/* TIFF input name */
	char out_name[MAXNAMEL+1];	/* ICC output name */
	char tag_name[MXTGNMS] = { 't','a','r','g' };
	int docal = 0;
	icc *icco;
	icTagSignature sig;
	icmText *ro;
	icmUnknown *uro;
	icmFile *ifp, *ofp;
	int verb = 0;
	int  size = 0;
	void *buf = NULL;
	int rv = 0;
	
	error_program = argv[0];

	if (argc < 3)
		usage("Too few parameters");

	/* Process the arguments */
	for(fa = 1;fa < argc;fa++) {
		nfa = fa;					/* skip to nfa if next argument is used */
		if (argv[fa][0] == '-')	{	/* Look for any flags */
			char *na = NULL;		/* next argument after flag, null if none */

			if (argv[fa][2] != '\000')
				na = &argv[fa][2];		/* next is directly after flag */
			else {
				if ((fa+1) < argc) {
					if (argv[fa+1][0] != '-') {
						nfa = fa + 1;
						na = argv[nfa];		/* next is seperate non-flag argument */
					}
				}
			}

			if (argv[fa][1] == '?')
				usage(NULL);

			/* Verbosity */
			else if (argv[fa][1] == 'v' || argv[fa][1] == 'V') {
				verb = 1;
			}

			/* Tag name */
			else if (argv[fa][1] == 't' || argv[fa][1] == 'T') {
				fa = nfa;
				if (na == NULL) usage("Expect tag name after -t");
				strncpy(tag_name,na,4);
				tag_name[4] = '\000';
			}

			/* Calibration */
			else if (argv[fa][1] == 'c' || argv[fa][1] == 'C') {
				docal = 1;
			}

			else 
				usage("Unknown flag '%c'",argv[fa][1]);
		} else
			break;
	}

    if (fa >= argc || argv[fa][0] == '-') usage("Missing input ICC profile");
    strncpy(in_name,argv[fa++],MAXNAMEL); in_name[MAXNAMEL] = '\000';

    if (fa >= argc || argv[fa][0] == '-') usage("Missing output filename");
    strncpy(out_name,argv[fa++],MAXNAMEL); out_name[MAXNAMEL] = '\000';

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

	/* Open up the file for reading */
	if ((ifp = new_icmFileStd_name(in_name,"r")) == NULL)
		error ("Can't open file '%s'",in_name);

	if ((icco = new_icc()) == NULL)
		error ("Creation of ICC object failed");

	if ((rv = icco->read(icco,ifp,0)) != 0)
		error ("%d, %s",rv,icco->err);

	sig = str2tag(tag_name);

	if ((ro = (icmText *)icco->read_tag_any(icco, sig)) == NULL) {
		error("%d, %s",icco->errc, icco->err);
	}

	if (ro->ttype == icmSigUnknownType) {
		uro = (icmUnknown *)ro;
	} else if (ro->ttype != icSigTextType) {
		error("Tag isn't TextType or UnknownType");
	}

	if (docal) {
		cgatsFile *cgf;
		cgats *icg;
		int tab, oi;
		xcal *cal;

		if ((icg = new_cgats()) == NULL) {
			error("new_cgats() failed");
		}
		if ((cgf = new_cgatsFileMem(ro->data, ro->size)) == NULL)  {
			error("new_cgatsFileMem() failed");
		}
		icg->add_other(icg, "CTI3");
		oi = icg->add_other(icg, "CAL");

		if (icg->read(icg, cgf) != 0) {
			error("failed to parse tag contents as a CGATS file");
		}

		for (tab = 0; tab < icg->ntables; tab++) {
			if (icg->t[tab].tt == tt_other && icg->t[tab].oi == oi) {
				break;
			}
		}
		if (tab >= icg->ntables) {
			error("Failed to locate CAL table in CGATS");
		}
		
		if ((cal = new_xcal()) == NULL) {
			error("new_xcal() failed");
		}
		if (cal->read_cgats(cal, icg, tab, in_name) != 0)  {
			error("Parsing CAL table failed");
		}
		icg->del(icg);
		cgf->del(cgf);

		if (cal->write(cal, out_name) != 0) {
			error("writing to file '%s' failed\n",out_name);
		}
	} else {
		if ((ofp = new_icmFileStd_name(out_name, "w")) == NULL) {
			error("unable to open output file '%s'",out_name);
		}
	
		if (ro->ttype == icmSigUnknownType) {
			if (ofp->write(ofp, uro->data, 1, uro->size) != (uro->size)) {
				error("writing to file '%s' failed",out_name);
			}
		} else {
			if (ofp->write(ofp, ro->data, 1, ro->size-1) != (ro->size-1)) {
				error("writing to file '%s' failed",out_name);
			}
		}
	
		if (ofp->del(ofp) != 0) {
			error("closing file '%s' failed",out_name);
		}
	}

	icco->del(icco);
	ifp->del(ifp);

	return 0;
}