1 /* -*- C++ -*-
2 * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3 *
4 LibRaw is free software; you can redistribute it and/or modify
5 it under the terms of the one of two licenses as you choose:
6
7 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
8 (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
9
10 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
11 (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
12
13 */
14 #include "../../internal/libraw_cameraids.h"
15 #include "../../internal/libraw_cxx_defs.h"
16
unpack(void)17 int LibRaw::unpack(void)
18 {
19 CHECK_ORDER_HIGH(LIBRAW_PROGRESS_LOAD_RAW);
20 CHECK_ORDER_LOW(LIBRAW_PROGRESS_IDENTIFY);
21 try
22 {
23
24 if (!libraw_internal_data.internal_data.input)
25 return LIBRAW_INPUT_CLOSED;
26
27 RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW, 0, 2);
28 if (imgdata.rawparams.shot_select >= P1.raw_count)
29 return LIBRAW_REQUEST_FOR_NONEXISTENT_IMAGE;
30
31 if (!load_raw)
32 return LIBRAW_UNSPECIFIED_ERROR;
33
34 // already allocated ?
35 if (imgdata.image)
36 {
37 free(imgdata.image);
38 imgdata.image = 0;
39 }
40 if (imgdata.rawdata.raw_alloc)
41 {
42 free(imgdata.rawdata.raw_alloc);
43 imgdata.rawdata.raw_alloc = 0;
44 }
45 if (libraw_internal_data.unpacker_data.meta_length)
46 {
47 libraw_internal_data.internal_data.meta_data =
48 (char *)malloc(libraw_internal_data.unpacker_data.meta_length);
49 merror(libraw_internal_data.internal_data.meta_data, "LibRaw::unpack()");
50 }
51
52 libraw_decoder_info_t decoder_info;
53 get_decoder_info(&decoder_info);
54
55 int save_iwidth = S.iwidth, save_iheight = S.iheight,
56 save_shrink = IO.shrink;
57
58 int rwidth = S.raw_width, rheight = S.raw_height;
59 if (!IO.fuji_width)
60 {
61 // adjust non-Fuji allocation
62 if (rwidth < S.width + S.left_margin)
63 rwidth = S.width + S.left_margin;
64 if (rheight < S.height + S.top_margin)
65 rheight = S.height + S.top_margin;
66 }
67 if (rwidth > 65535 ||
68 rheight > 65535) // No way to make image larger than 64k pix
69 throw LIBRAW_EXCEPTION_IO_CORRUPT;
70
71 imgdata.rawdata.raw_image = 0;
72 imgdata.rawdata.color4_image = 0;
73 imgdata.rawdata.color3_image = 0;
74 imgdata.rawdata.float_image = 0;
75 imgdata.rawdata.float3_image = 0;
76
77 #ifdef USE_DNGSDK
78 if (imgdata.idata.dng_version && dnghost
79 && libraw_internal_data.unpacker_data.tiff_samples != 2 // Fuji SuperCCD; it is better to detect is more rigid way
80 && valid_for_dngsdk() && load_raw != &LibRaw::pentax_4shot_load_raw)
81 {
82 // Data size check
83 INT64 pixcount =
84 INT64(MAX(S.width, S.raw_width)) * INT64(MAX(S.height, S.raw_height));
85 INT64 planecount =
86 (imgdata.idata.filters || P1.colors == 1) ? 1 : LIM(P1.colors, 3, 4);
87 INT64 samplesize = is_floating_point() ? 4 : 2;
88 INT64 bytes = pixcount * planecount * samplesize;
89 if (bytes > INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
90 throw LIBRAW_EXCEPTION_TOOBIG;
91
92 // find ifd to check sample
93 int rr = try_dngsdk();
94 if (raw_was_read())
95 imgdata.process_warnings |= LIBRAW_WARN_DNGSDK_PROCESSED;
96 }
97 #endif
98
99 #ifdef USE_RAWSPEED
100 if (!raw_was_read())
101 {
102 int rawspeed_enabled = 1;
103
104 if (imgdata.idata.dng_version && (libraw_internal_data.unpacker_data.tiff_samples == 2 || imgdata.idata.raw_count > 1))
105 rawspeed_enabled = 0;
106
107 if (libraw_internal_data.unpacker_data.is_NikonTransfer)
108 rawspeed_enabled = 0;
109
110 if (libraw_internal_data.unpacker_data.pana_encoding == 5)
111 rawspeed_enabled = 0;
112
113 if (imgdata.idata.raw_count > 1)
114 rawspeed_enabled = 0;
115 if (!strncasecmp(imgdata.idata.software, "Magic", 5))
116 rawspeed_enabled = 0;
117 // Disable rawspeed for double-sized Oly files
118 if (makeIs(LIBRAW_CAMERAMAKER_Olympus) &&
119 ((imgdata.sizes.raw_width > 6000) ||
120 !strncasecmp(imgdata.idata.model, "SH-", 3) ||
121 !strncasecmp(imgdata.idata.model, "TG-", 3) ))
122 rawspeed_enabled = 0;
123
124 if (makeIs(LIBRAW_CAMERAMAKER_Canon) &&
125 (libraw_internal_data.identify_data.unique_id == CanonID_EOS_6D_Mark_II))
126 rawspeed_enabled = 0;
127
128 if (imgdata.idata.dng_version && imgdata.idata.filters == 0 &&
129 libraw_internal_data.unpacker_data.tiff_bps == 8) // Disable for 8 bit
130 rawspeed_enabled = 0;
131
132 if (load_raw == &LibRaw::packed_load_raw &&
133 makeIs(LIBRAW_CAMERAMAKER_Nikon) &&
134 (!strncasecmp(imgdata.idata.model, "E", 1) ||
135 !strncasecmp(imgdata.idata.model, "COOLPIX B", 9) ||
136 !strncasecmp(imgdata.idata.model, "COOLPIX P9", 10) ||
137 !strncasecmp(imgdata.idata.model, "COOLPIX P1000", 13)))
138 rawspeed_enabled = 0;
139
140 if (load_raw == &LibRaw::nikon_load_raw && makeIs(LIBRAW_CAMERAMAKER_Nikon) &&
141 !strcasecmp(imgdata.idata.model, "D6"))
142 rawspeed_enabled = 0;
143
144 if (load_raw == &LibRaw::lossless_jpeg_load_raw &&
145 MN.canon.RecordMode && makeIs(LIBRAW_CAMERAMAKER_Kodak) &&
146 /* Not normalized models here, it is intentional */
147 (!strncasecmp(imgdata.idata.model, "EOS D2000", 9) ||
148 !strncasecmp(imgdata.idata.model, "EOS D6000", 9)))
149 rawspeed_enabled = 0;
150
151 if (load_raw == &LibRaw::nikon_load_raw &&
152 makeIs(LIBRAW_CAMERAMAKER_Nikon) &&
153 (!strncasecmp(imgdata.idata.model, "Z", 1) || !strncasecmp(imgdata.idata.model,"D780",4)))
154 rawspeed_enabled = 0;
155
156 if (load_raw == &LibRaw::panasonic_load_raw &&
157 libraw_internal_data.unpacker_data.pana_encoding > 4)
158 rawspeed_enabled = 0;
159
160 // RawSpeed Supported,
161 if (imgdata.rawparams.use_rawspeed && rawspeed_enabled &&
162 !(is_sraw() && (imgdata.rawparams.specials &
163 (LIBRAW_RAWSPECIAL_SRAW_NO_RGB |
164 LIBRAW_RAWSPECIAL_SRAW_NO_INTERPOLATE))) &&
165 (decoder_info.decoder_flags & LIBRAW_DECODER_TRYRAWSPEED) &&
166 _rawspeed_camerameta)
167 {
168 INT64 pixcount = INT64(MAX(S.width, S.raw_width)) *
169 INT64(MAX(S.height, S.raw_height));
170 INT64 planecount = (imgdata.idata.filters || P1.colors == 1)
171 ? 1
172 : LIM(P1.colors, 3, 4);
173 INT64 bytes =
174 pixcount * planecount * 2; // sample size is always 2 for rawspeed
175 if (bytes >
176 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
177 throw LIBRAW_EXCEPTION_TOOBIG;
178
179 int rr = try_rawspeed();
180 }
181 }
182 #endif
183 if (!raw_was_read()) // RawSpeed failed or not run
184 {
185 // Not allocated on RawSpeed call, try call LibRaow
186 int zero_rawimage = 0;
187 if (decoder_info.decoder_flags & LIBRAW_DECODER_OWNALLOC)
188 {
189 // x3f foveon decoder and DNG float
190 // Do nothing! Decoder will allocate data internally
191 }
192 if (decoder_info.decoder_flags & LIBRAW_DECODER_SINAR4SHOT)
193 {
194 if (imgdata.rawparams.shot_select) // single image extract
195 {
196 if (INT64(rwidth) * INT64(rheight + 8) *
197 sizeof(imgdata.rawdata.raw_image[0]) >
198 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
199 throw LIBRAW_EXCEPTION_TOOBIG;
200 imgdata.rawdata.raw_alloc = malloc(
201 rwidth * (rheight + 8) * sizeof(imgdata.rawdata.raw_image[0]));
202 imgdata.rawdata.raw_image = (ushort *)imgdata.rawdata.raw_alloc;
203 if (!S.raw_pitch)
204 S.raw_pitch = S.raw_width * 2; // Bayer case, not set before
205 }
206 else // Full image extract
207 {
208 if (INT64(rwidth) * INT64(rheight + 8) *
209 sizeof(imgdata.rawdata.raw_image[0]) * 4 >
210 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
211 throw LIBRAW_EXCEPTION_TOOBIG;
212 S.raw_pitch = S.raw_width * 8;
213 imgdata.rawdata.raw_alloc = 0;
214 imgdata.image = (ushort(*)[4])calloc(
215 unsigned(MAX(S.width, S.raw_width)) *
216 unsigned(MAX(S.height, S.raw_height) + 8),
217 sizeof(*imgdata.image));
218 }
219 }
220 else if (decoder_info.decoder_flags & LIBRAW_DECODER_3CHANNEL)
221 {
222 if (INT64(rwidth) * INT64(rheight + 8) *
223 sizeof(imgdata.rawdata.raw_image[0]) * 3 >
224 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
225 throw LIBRAW_EXCEPTION_TOOBIG;
226
227 imgdata.rawdata.raw_alloc = malloc(
228 rwidth * (rheight + 8) * sizeof(imgdata.rawdata.raw_image[0]) * 3);
229 imgdata.rawdata.color3_image = (ushort(*)[3])imgdata.rawdata.raw_alloc;
230 if (!S.raw_pitch)
231 S.raw_pitch = S.raw_width * 6;
232 }
233 else if (imgdata.idata.filters ||
234 P1.colors ==
235 1) // Bayer image or single color -> decode to raw_image
236 {
237 if (INT64(rwidth) * INT64(rheight + 8) *
238 sizeof(imgdata.rawdata.raw_image[0]) >
239 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
240 throw LIBRAW_EXCEPTION_TOOBIG;
241 imgdata.rawdata.raw_alloc = malloc(
242 rwidth * (rheight + 8) * sizeof(imgdata.rawdata.raw_image[0]));
243 imgdata.rawdata.raw_image = (ushort *)imgdata.rawdata.raw_alloc;
244 if (!S.raw_pitch)
245 S.raw_pitch = S.raw_width * 2; // Bayer case, not set before
246 }
247 else // NO LEGACY FLAG if (decoder_info.decoder_flags &
248 // LIBRAW_DECODER_LEGACY)
249 {
250 if (decoder_info.decoder_flags & LIBRAW_DECODER_ADOBECOPYPIXEL)
251 {
252 S.raw_pitch = S.raw_width * 8;
253 }
254 else
255 {
256 S.iwidth = S.width;
257 S.iheight = S.height;
258 IO.shrink = 0;
259 if (!S.raw_pitch)
260 S.raw_pitch = (decoder_info.decoder_flags &
261 LIBRAW_DECODER_LEGACY_WITH_MARGINS)
262 ? S.raw_width * 8
263 : S.width * 8;
264 }
265 // sRAW and old Foveon decoders only, so extra buffer size is just 1/4
266 // allocate image as temporary buffer, size
267 if (INT64(MAX(S.width, S.raw_width)) *
268 INT64(MAX(S.height, S.raw_height) + 8) *
269 sizeof(*imgdata.image) >
270 INT64(imgdata.rawparams.max_raw_memory_mb) * INT64(1024 * 1024))
271 throw LIBRAW_EXCEPTION_TOOBIG;
272
273 imgdata.rawdata.raw_alloc = 0;
274 imgdata.image =
275 (ushort(*)[4])calloc(unsigned(MAX(S.width, S.raw_width)) *
276 unsigned(MAX(S.height, S.raw_height) + 8),
277 sizeof(*imgdata.image));
278 if (!(decoder_info.decoder_flags & LIBRAW_DECODER_ADOBECOPYPIXEL))
279 {
280 imgdata.rawdata.raw_image = (ushort *)imgdata.image;
281 zero_rawimage = 1;
282 }
283 }
284 ID.input->seek(libraw_internal_data.unpacker_data.data_offset, SEEK_SET);
285
286 unsigned m_save = C.maximum;
287 if (load_raw == &LibRaw::unpacked_load_raw &&
288 (!strcasecmp(imgdata.idata.make, "Nikon") || !strcasecmp(imgdata.idata.make, "Hasselblad"))
289 )
290 C.maximum = 65535;
291 (this->*load_raw)();
292 if (zero_rawimage)
293 imgdata.rawdata.raw_image = 0;
294 if (load_raw == &LibRaw::unpacked_load_raw &&
295 (!strcasecmp(imgdata.idata.make, "Nikon") || !strcasecmp(imgdata.idata.make, "Hasselblad"))
296 )
297 C.maximum = m_save;
298 if (decoder_info.decoder_flags & LIBRAW_DECODER_OWNALLOC)
299 {
300 // x3f foveon decoder only: do nothing
301 }
302 else if (decoder_info.decoder_flags & LIBRAW_DECODER_SINAR4SHOT &&
303 imgdata.rawparams.shot_select == 0)
304 {
305 imgdata.rawdata.raw_alloc = imgdata.image;
306 imgdata.rawdata.color4_image = (ushort(*)[4])imgdata.rawdata.raw_alloc;
307 imgdata.image = 0;
308 }
309 else if (!(imgdata.idata.filters ||
310 P1.colors == 1)) // legacy decoder, ownalloc handled above
311 {
312 // successfully decoded legacy image, attach image to raw_alloc
313 imgdata.rawdata.raw_alloc = imgdata.image;
314 imgdata.rawdata.color4_image = (ushort(*)[4])imgdata.rawdata.raw_alloc;
315 imgdata.image = 0;
316 // Restore saved values. Note: Foveon have masked frame
317 // Other 4-color legacy data: no borders
318 if (!(libraw_internal_data.unpacker_data.load_flags & 256) &&
319 !(decoder_info.decoder_flags & LIBRAW_DECODER_ADOBECOPYPIXEL) &&
320 !(decoder_info.decoder_flags & LIBRAW_DECODER_LEGACY_WITH_MARGINS))
321 {
322 S.raw_width = S.width;
323 S.left_margin = 0;
324 S.raw_height = S.height;
325 S.top_margin = 0;
326 }
327 }
328 }
329
330 if (imgdata.rawdata.raw_image)
331 crop_masked_pixels(); // calculate black levels
332
333 // recover image sizes
334 S.iwidth = save_iwidth;
335 S.iheight = save_iheight;
336 IO.shrink = save_shrink;
337
338 // adjust black to possible maximum
339 unsigned int i = C.cblack[3];
340 unsigned int c;
341 for (c = 0; c < 3; c++)
342 if (i > C.cblack[c])
343 i = C.cblack[c];
344 for (c = 0; c < 4; c++)
345 C.cblack[c] -= i;
346 C.black += i;
347
348 // Save color,sizes and internal data into raw_image fields
349 memmove(&imgdata.rawdata.color, &imgdata.color, sizeof(imgdata.color));
350 memmove(&imgdata.rawdata.sizes, &imgdata.sizes, sizeof(imgdata.sizes));
351 memmove(&imgdata.rawdata.iparams, &imgdata.idata, sizeof(imgdata.idata));
352 memmove(&imgdata.rawdata.ioparams,
353 &libraw_internal_data.internal_output_params,
354 sizeof(libraw_internal_data.internal_output_params));
355
356 SET_PROC_FLAG(LIBRAW_PROGRESS_LOAD_RAW);
357 RUN_CALLBACK(LIBRAW_PROGRESS_LOAD_RAW, 1, 2);
358
359 return 0;
360 }
361 catch (const std::bad_alloc&)
362 {
363 EXCEPTION_HANDLER(LIBRAW_EXCEPTION_ALLOC);
364 }
365 catch (const LibRaw_exceptions& err)
366 {
367 EXCEPTION_HANDLER(err);
368 }
369 catch (const std::exception& ee)
370 {
371 EXCEPTION_HANDLER(LIBRAW_EXCEPTION_IO_CORRUPT);
372 }
373 }
374