libopenraw
neffile.cpp
1/* -*- tab-width:4; c-basic-offset:4 -*- */
2/*
3 * libopenraw - neffile.cpp
4 *
5 * Copyright (C) 2006-2017 Hubert Figuière
6 * Copyright (C) 2008 Novell, Inc.
7 *
8 * This library is free software: you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation, either version 3 of
11 * the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23#include <stddef.h>
24#include <stdio.h>
25#include <sys/types.h>
26
27#include <cstdint>
28#include <memory>
29#include <vector>
30
31#include <libopenraw/cameraids.h>
32#include <libopenraw/consts.h>
33#include <libopenraw/debug.h>
34
35#include "cfapattern.hpp"
36#include "rawdata.hpp"
37#include "rawfile.hpp"
38
39
40#include "trace.hpp"
41#include "ifd.hpp"
42#include "ifdfilecontainer.hpp"
43#include "ifdentry.hpp"
44#include "makernotedir.hpp"
45#include "nefdiffiterator.hpp"
46#include "nefcfaiterator.hpp"
47#include "neffile.hpp"
48#include "rawcontainer.hpp"
49#include "rawfile_private.hpp"
50
51using namespace Debug;
52
53namespace OpenRaw {
54
55namespace Internals {
56
57#define OR_MAKE_NIKON_TYPEID(camid) \
58 OR_MAKE_FILE_TYPEID(OR_TYPEID_VENDOR_NIKON,camid)
59
60/* taken from dcraw, by default */
61static const BuiltinColourMatrix s_matrices[] = {
62 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D1),
63 0,
64 0, /* multiplied by 2.218750, 1.0, 1.148438 */
65 { 16772,-4726,-2141,-7611,15713,1972,-2846,3494,9521 } },
66 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D100),
67 0,
68 0,
69 { 5902,-933,-782,-8983,16719,2354,-1402,1455,6464 } },
70 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D1X),
71 0,
72 0,
73 { 7702,-2245,-975,-9114,17242,1875,-2679,3055,8521 } },
74 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D200),
75 0,
76 0xfbc,
77 { 8367,-2248,-763,-8758,16447,2422,-1527,1550,8053 } },
78 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D2H),
79 0,
80 0,
81 { 5710,-901,-615,-8594,16617,2024,-2975,4120,6830 } },
82 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D2X),
83 0,
84 0,
85 { 10231,-2769,-1255,-8301,15900,2552,-797,680,7148 } },
86
87 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3),
88 0,
89 0,
90 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
91 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3S),
92 0,
93 0,
94 { 8828,-2406,-694,-4874,12603,2541,-660,1509,7587 } },
95 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3X),
96 0,
97 0,
98 { 7171,-1986,-648,-8085,15555,2718,-2170,2512,7457 } },
99 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D300),
100 0,
101 0,
102 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
103 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D300S),
104 0,
105 0,
106 { 9030,-1992,-715,-8465,16302,2255,-2689,3217,8069 } },
107 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3000),
108 0,
109 0,
110 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
111 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3100),
112 0,
113 0,
114 { 7911,-2167,-813,-5327,13150,2408,-1288,2483,7968 } },
115 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3200),
116 0,
117 0xfb9,
118 { 7013, -1408, -635, -5268, 12902, 2640, -1470, 2801, 7379 } },
119 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3300),
120 0,
121 0,
122 { 6988, -1384, -714, -5631, 13410, 2447, -1485, 2204, 7318 } },
123
124 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D4),
125 0,
126 0,
127 { 8598, -2848, -857, -5618, 13606, 2195, -1002, 1773, 7137 } },
128 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D4S),
129 0,
130 0,
131 { 8598, -2848, -857, -5618, 13606, 2195, -1002, 1773, 7137 } },
132 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D40),
133 0,
134 0,
135 { 6992,-1668,-806,-8138,15748,2543,-874,850,7897 } },
136 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D40X),
137 0,
138 0,
139 { 8819,-2543,-911,-9025,16928,2151,-1329,1213,8449 } },
140
141 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5),
142 0,
143 0,
144 { 9200,-3522,-992,-5755,13803,2117,-753,1486,6338 } },
145 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D50),
146 0,
147 0,
148 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
149 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D500),
150 0,
151 0,
152 { 8813, -3210, -1036, -4703, 12868, 2021, -1054, 1940, 6129 } },
153 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5000),
154 0,
155 0xf00,
156 { 7309,-1403,-519,-8474,16008,2622,-2433,2826,8064 } },
157 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5100),
158 0,
159 0x3de6,
160 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
161 // From DNG Converter 7.4
162 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5200),
163 0,
164 0,
165 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
166 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5300),
167 0,
168 0,
169 { 6988, -1384, -714, -5631, 13410, 2447, -1485, 2204, 7318 } },
170 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5500),
171 0,
172 0,
173 { 8821, -2938, -785, -4178, 12142, 2287, -824, 1651, 6860 } },
174 // From DNG Converter 10.3
175 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5600),
176 0,
177 0,
178 { 8821, -2938, -785, -4178, 12142, 2287, -824, 1651, 6860 } },
179 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D60),
180 0,
181 0,
182 { 8736,-2458,-935,-9075,16894,2251,-1354,1242,8263 } },
183 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D600),
184 0,
185 0,
186 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
187 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D610),
188 0,
189 0,
190 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
191
192 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D70),
193 0,
194 0,
195 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
196 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D70S),
197 0,
198 0,
199 { 7732,-2422,-789,-8238,15884,2498,-859,783,7330 } },
200 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D700),
201 0,
202 0,
203 { 8139,-2171,-663,-8747,16541,2295,-1925,2008,8093 } },
204 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7000),
205 0,
206 0,
207 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
208 // From DNG Converter 7.4
209 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7100),
210 0,
211 0,
212 { 8322,-3112,-1047,-6367,14342,2179,-988,1638,6394 } },
213 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7200),
214 0,
215 0,
216 { 8322, -3112, -1047, -6367, 14342, 2179, -988, 1638, 6394 } },
217 // From DNG Converter 8.7-rc
218 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D750),
219 0,
220 0,
221 { 9020, -2890, -715, -4535, 12436, 2348, -934, 1919, 7086 } },
222
223 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D80),
224 0,
225 0,
226 { 8629,-2410,-883,-9055,16940,2171,-1490,1363,8520 } },
227 // From DNG Converter 7.1-rc
228 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D800),
229 0,
230 0,
231 { 7866, -2108, -555, -4869, 12483, 2681, -1176, 2069, 7501 } },
232 // From DNG Converter 7.1-rc
233 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D800E),
234 0,
235 0,
236 { 7866, -2108, -555, -4869, 12483, 2681, -1176, 2069, 7501 } },
237 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D810),
238 0,
239 0,
240 { 9369, -3195, -791, -4488, 12430, 2301, -893, 1796, 6872 } },
241
242 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D90),
243 0,
244 0xf00,
245 { 7309,-1403,-519,-8474,16008,2622,-2434,2826,8064 } },
246
247 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_DF),
248 0,
249 0,
250 { 8598,-2848,-857,-5618,13606,2195,-1002,1773,7137 } },
251
252 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E5400),
253 0,
254 0,
255 { 9349,-2987,-1001,-7919,15766,2266,-2098,2680,6839 } },
256// { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E5700), 0, 0,
257// { -5368,11478,2368,5537,-113,3148,-4969,10021,5782,778,9028,211 } },
258 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E8400),
259 0,
260 0,
261 { 7842,-2320,-992,-8154,15718,2599,-1098,1342,7560 } },
262 // From DNG Converter 10.3
263 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_B700),
264 200,
265 0,
266 { 14387, -6014, -1299, -1357, 9975, 1616, 467, 1047, 4744 } },
267 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P330),
268 200,
269 0,
270 { 10321, -3920, -931, -2750, 11146, 1824, -442, 1545, 5539 } },
271 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P340),
272 200,
273 0,
274 { 10321, -3920, -931, -2750, 11146, 1824, -442, 1545, 5539 } },
275 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P6000),
276 0,
277 0,
278 { 9698,-3367,-914,-4706,12584,2368,-837,968,5801 } },
279 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7000),
280 0,
281 0,
282 { 11432,-3679,-1111,-3169,11239,2202,-791,1380,4455 } },
283 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7100),
284 0,
285 0,
286 { 11053,-4269,-1024,-1976,10182,2088,-526,1263,4469 } },
287 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7700),
288 0,
289 0,
290 { 10321,-3920,-931,-2750,11146,1824,-442,1545,5539 } },
291
292 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_AW1),
293 0,
294 0,
295 { 6588, -1305, -693, -3277, 10987, 2634, -355, 2016, 5106 } },
296 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J1),
297 0,
298 0,
299 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
300 // From DNG Converter 7.4
301 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J2),
302 0,
303 0,
304 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
305 // From DNG Converter 7.4
306 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J3),
307 0,
308 0,
309 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
310 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J4),
311 0,
312 0,
313 { 5958, -1559, -571, -4021, 11453, 2939, -634, 1548, 5087 } },
314 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J5),
315 0,
316 0,
317 { 7520, -2518, -645, -3844, 12102, 1945, -913, 2249, 6835 } },
318 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V1),
319 0,
320 0,
321 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
322 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V2),
323 0,
324 0,
325 { 6588,-1305,-693,-3277,10987,2634,-355,2016,5106 } },
326 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V3),
327 0,
328 0,
329 { 5958, -1559, -571, -4021, 11453, 2939, -634, 1548, 5087 } },
330 // From DNG Converter 7.4
331 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_S1),
332 0,
333 0,
334 { 8994,-2667,-865,-4594,12324,2552,-699,1786,6260 } },
335 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_S2),
336 0,
337 0,
338 { 6612, -1342, -618, -3338, 11055, 2623, -174, 1792, 5075 } },
339
340 // From DNG Converter 7.4
341 { OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_A),
342 0,
343 0,
344 { 8198,-2239,-724,-4871,12389,2798,-1043,2050,7181 } },
345
346 { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0 } }
347};
348
349const IfdFile::camera_ids_t NefFile::s_def[] = {
350 { "NIKON D1 ", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D1) },
351 { "NIKON D100 ", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D100) },
352 { "NIKON D1X", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D1X) },
353 { "NIKON D200", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D200) },
354 { "NIKON D2H", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D2H ) },
355 { "NIKON D2X", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D2X ) },
356 { "NIKON D3", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3) },
357 { "NIKON D3S", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3S) },
358 { "NIKON D3X", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3X) },
359 { "NIKON D300", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D300) },
360 { "NIKON D300S", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D300S) },
361 { "NIKON D3000", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3000) },
362 { "NIKON D3100", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3100) },
363 { "NIKON D3200", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3200) },
364 { "NIKON D3300", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3300) },
365 { "NIKON D3400", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D3400) },
366 { "NIKON D4", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D4) },
367 { "NIKON D4S", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D4S) },
368 { "NIKON D40", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D40) },
369 { "NIKON D40X", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D40X) },
370 { "NIKON D5", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5) },
371 { "NIKON D50", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D50) },
372 { "NIKON D500", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D500) },
373 { "NIKON D5000", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5000) },
374 { "NIKON D5100", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5100) },
375 { "NIKON D5200", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5200) },
376 { "NIKON D5300", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5300) },
377 { "NIKON D5500", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5500) },
378 { "NIKON D5600", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D5600) },
379 { "NIKON D60", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D60) },
380 { "NIKON D600", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D600) },
381 { "NIKON D610", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D610) },
382 { "NIKON D70", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D70) },
383 { "NIKON D70s", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D70S) },
384 { "NIKON D700", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D700) },
385 { "NIKON D7000", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7000) },
386 { "NIKON D7100", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7100) },
387 { "NIKON D7200", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D7200) },
388 { "NIKON D750", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D750) },
389 { "NIKON D80", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D80) },
390 { "NIKON D800", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D800) },
391 { "NIKON D800E", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D800E) },
392 { "NIKON D810", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D810) },
393 { "NIKON D90", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D90) },
394 { "NIKON Df", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_DF) },
395 { "E5400", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E5400) },
396 { "E5700", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E5700) },
397 { "E8400", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_E8400) },
398 { "COOLPIX B700", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_B700) },
399 { "COOLPIX P330", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P330) },
400 { "COOLPIX P340", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P340) },
401 { "COOLPIX P6000", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P6000) },
402 { "COOLPIX P7000", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7000) },
403 { "COOLPIX P7100", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7100) },
404 { "COOLPIX P7700", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_P7700) },
405 { "COOLPIX A", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_COOLPIX_A) },
406 { "NIKON 1 J1", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J1) },
407 { "NIKON 1 J2", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J2) },
408 { "NIKON 1 J3", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J3) },
409 { "NIKON 1 J4", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J4) },
410 { "NIKON 1 J5", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_J5) },
411 { "NIKON 1 V1", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V1) },
412 { "NIKON 1 V2", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V2) },
413 { "NIKON 1 V3", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_V3) },
414 { "NIKON 1 S1", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_S1) },
415 { "NIKON 1 S2", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_S2) },
416 { "NIKON 1 AW1", OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_1_AW1) },
417 { 0, 0 }
418};
419
420RawFile *NefFile::factory(const IO::Stream::Ptr & _filename)
421{
422 return new NefFile(_filename);
423}
424
425NefFile::NefFile(const IO::Stream::Ptr & _filename)
426 : TiffEpFile(_filename, OR_RAWFILE_TYPE_NEF)
427{
428 _setIdMap(s_def);
429 _setMatrices(s_matrices);
430}
431
432
433NefFile::~NefFile()
434{
435}
436
437bool NefFile::isCompressed(RawContainer & container, uint32_t offset)
438{
439 int i;
440 uint8_t buf[256];
441 size_t real_size = container.fetchData(buf, offset,
442 256);
443 if(real_size != 256) {
444 return true;
445 }
446 for(i = 15; i < 256; i+= 16) {
447 if(buf[i]) {
448 LOGDBG1("isCompressed: true\n");
449 return true;
450 }
451 }
452 LOGDBG1("isCompressed: false\n");
453 return false;
454}
455
456bool NefFile::isNrw()
457{
458 MakerNoteDir::Ref _makerNoteIfd = makerNoteIfd();
459 if(!_makerNoteIfd) {
460 LOGERR("makernote not found\n");
461 return false;
462 }
463 auto value = _makerNoteIfd->getValue<std::string>(IFD::MNOTE_NIKON_QUALITY);
464 if (value && value.value() == "NRW") {
465 LOGDBG1("NRW found");
466 return true;
467 }
468 return false;
469}
470
471::or_error
472NefFile::_unpackData(uint16_t bpc, uint32_t compression, RawData & data,
473 uint32_t x, uint32_t y, uint32_t offset, uint32_t byte_length)
474{
475 if (typeId() == OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D100)) {
476 x += 6;
477 }
478 return TiffEpFile::_unpackData(bpc, compression, data, x, y, offset, byte_length);
479}
480
481
482uint32_t NefFile::_translateCompressionType(IFD::TiffCompress tiffCompression)
483{
484 if (tiffCompression == IFD::COMPRESS_NIKON_QUANTIZED) {
485 // must check whether it is really compressed
486 // only for D100
487 if (typeId() == OR_MAKE_NIKON_TYPEID(OR_TYPEID_NIKON_D100)) {
488 return IFD::COMPRESS_NIKON_PACK;
489 }
490 }
491 return TiffEpFile::_translateCompressionType(tiffCompression);
492}
493
494::or_error NefFile::_decompressNikonQuantized(RawData & data)
495{
496 NEFCompressionInfo c;
497 if (!_getCompressionCurve(data, c)) {
498 LOGERR("compression curve not found\n");
499 return OR_ERROR_NOT_FOUND;
500 }
501 const uint32_t rows = data.height();
502 const uint32_t raw_columns = data.width();
503
504 //FIXME: not always true
505 const uint32_t columns = raw_columns - 1;
506
507 NefDiffIterator
508 diffs(c.huffman, static_cast<uint8_t*>(data.data()), data.size());
509 NefCfaIterator iter(diffs, rows, raw_columns, c.vpred);
510
511 RawData newData;
512 uint16_t *p = (uint16_t *) newData.allocData(rows * columns * 2);
513 newData.setDimensions(columns, rows);
514 newData.setDataType(OR_DATA_TYPE_RAW);
515 uint16_t bpc = data.bpc();
516 newData.setBpc(bpc);
517 newData.setWhiteLevel((1 << bpc) - 1);
518 newData.setCfaPatternType(data.cfaPattern()->patternType());
519
520 for (unsigned int i = 0; i < rows; i++) {
521 for (unsigned int j = 0; j < raw_columns; j++) {
522 uint16_t t = iter.get();
523 if (j < columns) {
524 unsigned shift = 16 - data.bpc();
525 p[i * columns + j] = c.curve[t & 0x3fff] << shift;
526 }
527 }
528 }
529
530 data.swap(newData);
531 return OR_ERROR_NONE;
532}
533
534::or_error NefFile::_decompressIfNeeded(RawData & data,
535 uint32_t options)
536{
537 uint32_t compression = data.compression();
538 if (isNrw()) {
539 LOGDBG1("NRW file found, currently not supported.");
540 return OR_ERROR_INVALID_FORMAT;
541 } else if((options & OR_OPTIONS_DONT_DECOMPRESS) ||
542 compression == IFD::COMPRESS_NONE) {
543 return OR_ERROR_NONE;
544 } else if(compression == IFD::COMPRESS_NIKON_QUANTIZED) {
545 return _decompressNikonQuantized(data);
546 } else {
547 return OR_ERROR_INVALID_FORMAT;
548 }
549}
550
551bool
552NefFile::_getCompressionCurve(RawData & data, NefFile::NEFCompressionInfo& c)
553{
554 MakerNoteDir::Ref _makerNoteIfd = makerNoteIfd();
555 if(!_makerNoteIfd) {
556 LOGERR("makernote not found\n");
557 return false;
558 }
559 IfdEntry::Ref curveEntry = _makerNoteIfd->getEntry(IFD::MNOTE_NIKON_NEFDECODETABLE2);
560 if(!curveEntry) {
561 LOGERR("decode table2 tag not found\n");
562 return false;
563 }
564
565 size_t pos = _makerNoteIfd->getMnoteOffset() + curveEntry->offset();
566
567 auto file = m_container->file();
568 file->seek(pos, SEEK_SET);
569
570 uint16_t bpc = data.bpc();
571 uint8_t header0, header1;
572 auto result = m_container->readUInt8(file);
573 if(result.empty()) {
574 LOGERR("Header not found\n");
575 return false;
576 }
577 header0 = result.value();
578 result = m_container->readUInt8(file);
579 if(result.empty()) {
580 LOGERR("Header not found\n");
581 return false;
582 }
583 header1 = result.value();
584
585 if (header0 == 0x49) {
586 // some interesting stuff at 2110
587 // XXX we need to implement this.
588 LOGWARN("NEF: header0 is 0x49 - case not yet handled\n");
589 m_container->skip(2110);
590 }
591
592 for (int i = 0; i < 2; ++i) {
593 for (int j = 0; j < 2; ++j) {
594 auto result16 = m_container->readInt16(file);
595 if(result16.empty()) {
596 LOGERR("Failed to read vpred (%d,%d)\n", i, j);
597 return false;
598 }
599 c.vpred[i][j] = result16.value();
600 }
601 }
602
603 LOGDBG2("header0 = %d header1 = %d bpc = %u\n", header0, header1, bpc);
604
605 bool header_ok = false;
606 // header0 == 0x44 || 0x49 -> lossy
607 // header0 == 0x46 -> lossless
608 if (header0 == 0x44 || header0 == 0x49) {
609 if (bpc == 12) {
610 c.huffman = NefDiffIterator::Lossy12Bit;
611 LOGDBG1("12 bits lossy %u\n", bpc);
612 header_ok = true;
613 } else if (bpc == 14) {
614 c.huffman = NefDiffIterator::Lossy14Bit;
615 LOGDBG1("14 bits lossy %u\n", bpc);
616 header_ok = true;
617 }
618 } else if (header0 == 0x46) {
619 if (bpc == 14) {
620 c.huffman = NefDiffIterator::LossLess14Bit;
621 LOGDBG1("14 bits lossless\n");
622 header_ok = true;
623 } else if (bpc == 12) {
624// c.huffman = NefDiffIterator::LossLess12Bit;
625 LOGDBG1("12 bits lossless\n");
626 LOGERR("12 bits lossless isn't yet supported\n");
627 header_ok = true;
628 return false;
629 }
630 }
631 if (!header_ok) {
632 LOGERR("Wrong header, found %d-%d\n", header0, header1);
633 return false;
634 }
635
636 // number of elements in the curve
637 size_t nelems;
638 nelems = m_container->readInt16(file).value_or(0);
639 LOGDBG1("Num elems %ld\n", nelems);
640
641 uint32_t ceiling = 1 << bpc & 0x7fff;
642 uint32_t step = 0;
643 if (nelems > 1) {
644 step = ceiling / (nelems - 1);
645 }
646 LOGDBG1("ceiling %u, step = %u\n", ceiling, step);
647
648 if (header0 == 0x44 && header1 == 0x20 && step > 0) {
649 for (size_t i = 0; i < nelems; ++i) {
650 auto result16 = m_container->readInt16(file);
651 if (result16.empty()) {
652 LOGERR("NEF: short read\n");
653 return false;
654 }
655 c.curve[i * step] = result16.value();
656 }
657 for (size_t i = 0; i < ceiling; ++i) {
658 c.curve[i] = (c.curve[i - i % step] * (step - i % step) +
659 c.curve[i - i % step + step] * (i % step)) / step;
660 }
661 // split flag is at offset 562.
662 // XXX
663 } else if (header0 != 0x46 && nelems <= 0x4001) {
664 size_t num_read = m_container->readUInt16Array(file, c.curve, nelems);
665 if (num_read < nelems) {
666 LOGERR("NEF: short read of %ld elements instead of %ld\n", num_read, nelems);
667 return false;
668 }
669 ceiling = nelems;
670 }
671
672 auto black = c.curve[0];
673 auto white = c.curve[ceiling - 1];
674 for (size_t i = ceiling; i < 0x8000; i++) {
675 c.curve[i] = white;
676 }
677
678 data.setBlackLevel(black);
679 data.setWhiteLevel(white);
680 LOGDBG1("black %u white %u\n", black, white);
681 return true;
682}
683
684}
685}
686
687/*
688 Local Variables:
689 mode:c++
690 c-file-style:"stroustrup"
691 c-file-offsets:((innamespace . 0))
692 indent-tabs-mode:nil
693 fill-column:80
694 End:
695*/
void setDataType(DataType _type)
Definition: bitmapdata.cpp:100
void setBpc(uint32_t _bpc)
Definition: bitmapdata.cpp:164
size_t size() const
Definition: bitmapdata.cpp:129
uint32_t bpc() const
Definition: bitmapdata.cpp:159
::or_cfa_pattern patternType() const
Definition: cfapattern.cpp:186
size_t fetchData(void *buf, off_t offset, size_t buf_size)
void swap(RawData &with)
Definition: rawdata.cpp:245
const CfaPattern * cfaPattern() const
Definition: rawdata.cpp:287
virtual void setDimensions(uint32_t x, uint32_t y) override
Definition: rawdata.cpp:260
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard....
Definition: arwfile.cpp:30