summaryrefslogtreecommitdiff
path: root/tools/ordiag.cpp
blob: d566c462af6ec66357a14eda353451c2ac1e65f6 (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
/*
 * libopenraw - ordiag.cpp
 *
 * Copyright (C) 2007-2008 Hubert Figuiere
 * Copyright (C) 2008 Novell, Inc.
 *
 * This library is free software: you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 */


#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

#include <boost/format.hpp>
#include <boost/scoped_ptr.hpp>

#include <libopenraw++/rawfile.h>
#include <libopenraw++/thumbnail.h>
#include <libopenraw++/rawdata.h>
using OpenRaw::RawFile;
using OpenRaw::Thumbnail;
using OpenRaw::RawData;

/**
 * Dump on RawFile. (functor)
 */
class OrDiag
{
public:
	/** constructor
	 * @param out the output stream
	 */
	OrDiag(std::ostream & out)
		: m_out(out)
		{
		}

	std::string cfaPatternToString(RawData::CfaPattern t) 
		{
			switch(t) {
			case OR_CFA_PATTERN_NONE:
				return "None";
				break;
			case OR_CFA_PATTERN_NON_RGB22:
				return "Non RGB 2x2";
				break;
			case OR_CFA_PATTERN_RGGB:
				return "R,G,G,B";
				break;
			case OR_CFA_PATTERN_GBRG:
				return "G,B,R,G";
				break;
			case OR_CFA_PATTERN_BGGR:
				return "B,G,G,R";
				break;
			case OR_CFA_PATTERN_GRBG:
				return "G,R,B,G";
				break;
			default:
				break;
			}
			return str(boost::format("Unknown %1%") % t);
		};

	std::string dataTypeToString(Thumbnail::DataType t)
		{
			switch(t) {
			case OR_DATA_TYPE_NONE:
				return "None";
				break;
			case OR_DATA_TYPE_PIXMAP_8RGB:
				return "8bits per channel RGB pixmap";
				break;
			case OR_DATA_TYPE_JPEG:
				return "JPEG data";
				break;
			case OR_DATA_TYPE_TIFF:
				return "TIFF container";
				break;
			case OR_DATA_TYPE_PNG:
				return "PNG container";
				break;
			case OR_DATA_TYPE_CFA:
				return "CFA data";
				break;
			case OR_DATA_TYPE_COMPRESSED_CFA:
				return "Compressed CFA data";
				break;
			case OR_DATA_TYPE_UNKNOWN:
				return "Unknown type";
				break;
			default:
				break;
			}
			return "Invalid";
		}

	/** return a string for the raw file type
	 */
	std::string typeToString(RawFile::Type t)
		{
			switch(t) {
			case OR_RAWFILE_TYPE_UNKNOWN:
				break;
			case OR_RAWFILE_TYPE_CR2:
				return "Canon CR2";
				break;
			case OR_RAWFILE_TYPE_CRW:
				return "Canon CRW";
				break;
			case OR_RAWFILE_TYPE_NEF:
				return "Nikon NEF";
				break;
			case OR_RAWFILE_TYPE_MRW:
				return "Minolta MRW";
				break;
			case OR_RAWFILE_TYPE_ARW:
				return "Sony ARW";
				break;
			case OR_RAWFILE_TYPE_DNG:
				return "Adobe DNG";
				break;
			case OR_RAWFILE_TYPE_ORF:
				return "Olympus ORF";
				break;
			case OR_RAWFILE_TYPE_PEF:
				return "Pentax PEF";
				break;
			case OR_RAWFILE_TYPE_ERF:
				return "Epson ERF";
				break;
			default:
				break;
			}
			return "Unknown";
		}

	/** dump the previews of the raw file to mout
	 */
	void dumpPreviews(const boost::scoped_ptr<RawFile> & rf)
		{
			const std::vector<uint32_t> & previews = rf->listThumbnailSizes();
			m_out << boost::format("\tNumber of previews: %1%\n") 
				% previews.size();
			
			m_out << "\tAvailable previews:\n";
			for(std::vector<uint32_t>::const_iterator iter = previews.begin();
					iter != previews.end(); iter++)	{

				m_out << boost::format("\t\tSize %1%\n") % *iter;

				Thumbnail thumb;
				::or_error err = rf->getThumbnail(*iter, thumb);
				if (err != OR_ERROR_NONE) {
					m_out << boost::format("\t\t\tError getting thumbnail %1%\n") % err;
				}
				else {
					m_out << boost::format("\t\t\tFormat %1%\n") 
						% dataTypeToString(thumb.dataType());
					m_out << boost::format("\t\t\tDimensions: x = %1% y = %2%\n")
						% thumb.x() % thumb.y();
					m_out << boost::format("\t\t\tByte size: %1%\n") 
						% thumb.size();
				}
			}
		}

	void dumpRawData(const boost::scoped_ptr<RawFile> & rf)
		{
			RawData rd;
			::or_error err = rf->getRawData(rd, 0);
			if (err == OR_ERROR_NONE) {
				m_out << "\tRAW data\n";
				m_out << boost::format("\t\tType: %1%")
					% dataTypeToString(rd.dataType());
				if(rd.dataType() == OR_DATA_TYPE_COMPRESSED_CFA)  {
					m_out << boost::format(" (compression = %1%)\n") % rd.compression();
				}
				else {
					m_out << "\n";
				}
				m_out << boost::format("\t\tByte size: %1%\n")
					% rd.size();
				m_out << boost::format("\t\tDimensions: x = %1% y = %2%\n")
					% rd.x() % rd.y();
				m_out << boost::format("\t\tBayer Type: %1%\n")
					% cfaPatternToString(rd.cfaPattern());
				m_out << boost::format("\t\tBits per channel: %1%\n")
					% rd.bpc();
				m_out << boost::format("\t\tValues: min = %1% max = %2%\n")
					% rd.min() % rd.max();
			}
			else {
				m_out << boost::format("\tNo Raw Data found! (error = %1%)\n")
					% err;
			}
		}
	void dumpMetaData(const boost::scoped_ptr<RawFile> & rf)
		{
			int32_t o;
			o = rf->getOrientation();
			m_out << "\tMeta data\n";
			m_out << boost::format("\t\tOrientation: %1%\n")
				% o;
		}
	void operator()(const std::string &s)
		{
			m_out << boost::format("Dumping %1%\n") % s;

			boost::scoped_ptr<RawFile> rf(RawFile::newRawFile(s.c_str()));

			if (rf == NULL) {
				m_out << "unrecognized file\n";
			}
			else {
				m_out << boost::format("\tType = %1% (%2%)\n") % rf->type() 
															 % typeToString(rf->type());
				m_out << boost::format("\tType ID = %1%\n") % rf->typeId();
				dumpPreviews(rf);
				dumpRawData(rf);
				dumpMetaData(rf);
			}
		}
private:
	std::ostream & m_out;
};


void print_help()
{
	std::cerr << "ordiag [-v] [-h] [-d 0-9] [files...]\n";
	std::cerr << "Print libopenraw diagnostics\n";
	std::cerr << "\t-h: show this help\n";
	std::cerr << "\t-v: show version\n";
	std::cerr << "\t-d level: set debug / verbosity to level\n";
	std::cerr << "\tfiles: the files to diagnose\n";
}

void print_version()
{
	std::cerr << "ordiag version 0.0 - (c) 2007 Hubert Figuiere\n";
}



int main(int argc, char **argv)
{
	int done = 0;
	int dbl = 0;
	std::vector<std::string> files;

	OpenRaw::init();

	int o;
	while((o = getopt(argc, argv, "hvd")) != -1) {
		switch (o) {
		case 'h':
			print_help();
			done = 1;
			break;
		case 'v':
			print_version();
			done = 1;
			break;
		case 'd':
			dbl++;
			break;
		case '?':
			break;
		default:
			break;
		}
	}
	if (done) {
		return 0;
	}
	for ( ; optind < argc; optind++) {
		files.push_back(argv[optind]);
	}
	
	if (files.empty()) {
		std::cerr << "missing file name.\n";
		if (dbl) {
			print_version();
		}
		print_help();
		return 1;
	}

	if (dbl >=2) {
		or_debug_set_level(DEBUG2);
	}
	// do the business.
	for_each(files.begin(), files.end(), OrDiag(std::cout));

	return 0;
}