1 /* -*- C++ -*-
2  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3  *
4  LibRaw uses code from dcraw.c -- Dave Coffin's raw photo decoder,
5  dcraw.c is copyright 1997-2018 by Dave Coffin, dcoffin a cybercom o net.
6  LibRaw do not use RESTRICTED code from dcraw.c
7 
8  LibRaw is free software; you can redistribute it and/or modify
9  it under the terms of the one of two licenses as you choose:
10 
11 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
12    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
13 
14 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
15    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
16 
17  */
18 
19 #include "../../internal/dcraw_defs.h"
20 
21 /*
22    Returns 1 for a Coolpix 2100, 0 for anything else.
23  */
nikon_e2100()24 int LibRaw::nikon_e2100()
25 {
26   uchar t[12];
27   int i;
28 
29   fseek(ifp, 0, SEEK_SET);
30   for (i = 0; i < 1024; i++)
31   {
32     fread(t, 1, 12, ifp);
33     if (((t[2] & t[4] & t[7] & t[9]) >> 4 & t[1] & t[6] & t[8] & t[11] & 3) !=
34         3)
35       return 0;
36   }
37   return 1;
38 }
39 
nikon_3700()40 void LibRaw::nikon_3700()
41 {
42   int bits, i;
43   uchar dp[24];
44   static const struct
45   {
46     int bits;
47     char t_make[12], t_model[15];
48     int t_maker_idx;
49   } table[] = {{0x00, "Pentax", "Optio 33WR", LIBRAW_CAMERAMAKER_Pentax},
50                {0x03, "Nikon", "E3200", LIBRAW_CAMERAMAKER_Nikon},
51                {0x32, "Nikon", "E3700", LIBRAW_CAMERAMAKER_Nikon},
52                {0x33, "Olympus", "C-740UZ", LIBRAW_CAMERAMAKER_Olympus}};
53 
54   fseek(ifp, 3072, SEEK_SET);
55   fread(dp, 1, 24, ifp);
56   bits = (dp[8] & 3) << 4 | (dp[20] & 3);
57   for (i = 0; i < int(sizeof table / sizeof *table); i++)
58     if (bits == table[i].bits)
59     {
60       strcpy(make, table[i].t_make);
61       maker_index = table[i].t_maker_idx;
62       strcpy(model, table[i].t_model);
63     }
64 }
65 
66 /*
67    Separates a Minolta DiMAGE Z2 from a Nikon E4300.
68  */
minolta_z2()69 int LibRaw::minolta_z2()
70 {
71   int i, nz;
72   char tail[424];
73 
74   fseek(ifp, -sizeof tail, SEEK_END);
75   fread(tail, 1, sizeof tail, ifp);
76   for (nz = i = 0; i < int(sizeof tail); i++)
77     if (tail[i])
78       nz++;
79   return nz > 20;
80 }
81 
canon_s2is()82 int LibRaw::canon_s2is()
83 {
84   unsigned row;
85 
86   for (row = 0; row < 100; row++)
87   {
88     fseek(ifp, row * 3340 + 3284, SEEK_SET);
89     if (getc(ifp) > 15)
90       return 1;
91   }
92   return 0;
93 }
94 
95 #ifdef LIBRAW_OLD_VIDEO_SUPPORT
parse_redcine()96 void LibRaw::parse_redcine()
97 {
98   unsigned i, len, rdvo;
99 
100   order = 0x4d4d;
101   is_raw = 0;
102   fseek(ifp, 52, SEEK_SET);
103   width = get4();
104   height = get4();
105   fseek(ifp, 0, SEEK_END);
106   fseek(ifp, -(i = ftello(ifp) & 511), SEEK_CUR);
107   if (get4() != i || get4() != 0x52454f42)
108   {
109     fseek(ifp, 0, SEEK_SET);
110     while ((len = get4()) != (unsigned)EOF)
111     {
112       if (get4() == 0x52454456)
113         if (is_raw++ == shot_select)
114           data_offset = ftello(ifp) - 8;
115       fseek(ifp, len - 8, SEEK_CUR);
116     }
117   }
118   else
119   {
120     rdvo = get4();
121     fseek(ifp, 12, SEEK_CUR);
122     is_raw = get4();
123     fseeko(ifp, rdvo + 8 + shot_select * 4, SEEK_SET);
124     data_offset = get4();
125   }
126 }
127 #endif
128 
parse_cine()129 void LibRaw::parse_cine()
130 {
131   unsigned off_head, off_setup, off_image, i, temp;
132 
133   order = 0x4949;
134   fseek(ifp, 4, SEEK_SET);
135   is_raw = get2() == 2;
136   fseek(ifp, 14, SEEK_CUR);
137   is_raw *= get4();
138   off_head = get4();
139   off_setup = get4();
140   off_image = get4();
141   timestamp = get4();
142   if ((i = get4()))
143     timestamp = i;
144   fseek(ifp, off_head + 4, SEEK_SET);
145   raw_width = get4();
146   raw_height = get4();
147   switch (get2(), get2())
148   {
149   case 8:
150     load_raw = &LibRaw::eight_bit_load_raw;
151     break;
152   case 16:
153     load_raw = &LibRaw::unpacked_load_raw;
154   }
155   fseek(ifp, off_setup + 792, SEEK_SET);
156   strcpy(make, "CINE");
157   sprintf(model, "%d", get4());
158   fseek(ifp, 12, SEEK_CUR);
159   switch ((i = get4()) & 0xffffff)
160   {
161   case 3:
162     filters = 0x94949494;
163     break;
164   case 4:
165     filters = 0x49494949;
166     break;
167   default:
168     is_raw = 0;
169   }
170   fseek(ifp, 72, SEEK_CUR);
171   switch ((get4() + 3600) % 360)
172   {
173   case 270:
174     flip = 4;
175     break;
176   case 180:
177     flip = 1;
178     break;
179   case 90:
180     flip = 7;
181     break;
182   case 0:
183     flip = 2;
184   }
185   cam_mul[0] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
186   cam_mul[2] = getreal(LIBRAW_EXIFTAG_TYPE_FLOAT);
187   temp = get4();
188   maximum = ~((~0u) << LIM(temp, 1, 31));
189   fseek(ifp, 668, SEEK_CUR);
190   shutter = get4() / 1000000000.0;
191   fseek(ifp, off_image, SEEK_SET);
192   if (shot_select < is_raw)
193     fseek(ifp, shot_select * 8, SEEK_CUR);
194   data_offset = (INT64)get4() + 8;
195   data_offset += (INT64)get4() << 32;
196 }
197 
parse_qt(int end)198 void LibRaw::parse_qt(int end)
199 {
200   unsigned save, size;
201   char tag[4];
202 
203   order = 0x4d4d;
204   while (ftell(ifp) + 7 < end)
205   {
206     save = ftell(ifp);
207     if ((size = get4()) < 8)
208       return;
209     if ((int)size < 0)
210       return; // 2+GB is too much
211     if (save + size < save)
212       return; // 32bit overflow
213     fread(tag, 4, 1, ifp);
214     if (!memcmp(tag, "moov", 4) || !memcmp(tag, "udta", 4) ||
215         !memcmp(tag, "CNTH", 4))
216       parse_qt(save + size);
217     if (!memcmp(tag, "CNDA", 4))
218       parse_jpeg(ftell(ifp));
219     fseek(ifp, save + size, SEEK_SET);
220   }
221 }
222 
parse_smal(int offset,int fsize)223 void LibRaw::parse_smal(int offset, int fsize)
224 {
225   int ver;
226 
227   fseek(ifp, offset + 2, SEEK_SET);
228   order = 0x4949;
229   ver = fgetc(ifp);
230   if (ver == 6)
231     fseek(ifp, 5, SEEK_CUR);
232   if (get4() != (unsigned)fsize)
233     return;
234   if (ver > 6)
235     data_offset = get4();
236   raw_height = height = get2();
237   raw_width = width = get2();
238   strcpy(make, "SMaL");
239   sprintf(model, "v%d %dx%d", ver, width, height);
240   if (ver == 6)
241     load_raw = &LibRaw::smal_v6_load_raw;
242   if (ver == 9)
243     load_raw = &LibRaw::smal_v9_load_raw;
244 }
245 
parse_riff()246 void LibRaw::parse_riff()
247 {
248   unsigned i, size, end;
249   char tag[4], date[64], month[64];
250   static const char mon[12][4] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
251                                   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
252   struct tm t;
253 
254   order = 0x4949;
255   fread(tag, 4, 1, ifp);
256   size = get4();
257   end = ftell(ifp) + size;
258   if (!memcmp(tag, "RIFF", 4) || !memcmp(tag, "LIST", 4))
259   {
260     int maxloop = 1000;
261     get4();
262     while (ftell(ifp) + 7 < end && !feof(ifp) && maxloop--)
263       parse_riff();
264   }
265   else if (!memcmp(tag, "nctg", 4))
266   {
267     while (ftell(ifp) + 7 < end)
268     {
269       i = get2();
270       size = get2();
271       if ((i + 1) >> 1 == 10 && size == 20)
272         get_timestamp(0);
273       else
274         fseek(ifp, size, SEEK_CUR);
275     }
276   }
277   else if (!memcmp(tag, "IDIT", 4) && size < 64)
278   {
279     fread(date, 64, 1, ifp);
280     date[size] = 0;
281     memset(&t, 0, sizeof t);
282     if (sscanf(date, "%*s %s %d %d:%d:%d %d", month, &t.tm_mday, &t.tm_hour,
283                &t.tm_min, &t.tm_sec, &t.tm_year) == 6)
284     {
285       for (i = 0; i < 12 && strcasecmp(mon[i], month); i++)
286         ;
287       t.tm_mon = i;
288       t.tm_year -= 1900;
289       if (mktime(&t) > 0)
290         timestamp = mktime(&t);
291     }
292   }
293   else
294     fseek(ifp, size, SEEK_CUR);
295 }
296 
parse_rollei()297 void LibRaw::parse_rollei()
298 {
299   char line[128], *val;
300   struct tm t;
301 
302   fseek(ifp, 0, SEEK_SET);
303   memset(&t, 0, sizeof t);
304   do
305   {
306     line[0] = 0;
307     if (!fgets(line, 128, ifp))
308       break;
309     line[127] = 0;
310     if(!line[0]) break; // zero-length
311     if ((val = strchr(line, '=')))
312       *val++ = 0;
313     else
314       val = line + strbuflen(line);
315     if (!strcmp(line, "DAT"))
316       sscanf(val, "%d.%d.%d", &t.tm_mday, &t.tm_mon, &t.tm_year);
317     if (!strcmp(line, "TIM"))
318       sscanf(val, "%d:%d:%d", &t.tm_hour, &t.tm_min, &t.tm_sec);
319     if (!strcmp(line, "HDR"))
320       thumb_offset = atoi(val);
321     if (!strcmp(line, "X  "))
322       raw_width = atoi(val);
323     if (!strcmp(line, "Y  "))
324       raw_height = atoi(val);
325     if (!strcmp(line, "TX "))
326       thumb_width = atoi(val);
327     if (!strcmp(line, "TY "))
328       thumb_height = atoi(val);
329     if (!strcmp(line, "APT"))
330       aperture = atof(val);
331     if (!strcmp(line, "SPE"))
332       shutter = atof(val);
333     if (!strcmp(line, "FOCLEN"))
334       focal_len = atof(val);
335     if (!strcmp(line, "BLKOFS"))
336       black = atoi(val) +1;
337     if (!strcmp(line, "ORI"))
338       switch (atoi(val)) {
339       case 1:
340         flip = 6;
341         break;
342       case 2:
343         flip = 3;
344         break;
345       case 3:
346         flip = 5;
347         break;
348       }
349     if (!strcmp(line, "CUTRECT")) {
350       sscanf(val, "%hu %hu %hu %hu",
351              &imgdata.sizes.raw_inset_crops[0].cleft,
352              &imgdata.sizes.raw_inset_crops[0].ctop,
353              &imgdata.sizes.raw_inset_crops[0].cwidth,
354              &imgdata.sizes.raw_inset_crops[0].cheight);
355     }
356   } while (strncmp(line, "EOHD", 4));
357   data_offset = thumb_offset + thumb_width * thumb_height * 2;
358   t.tm_year -= 1900;
359   t.tm_mon -= 1;
360   if (mktime(&t) > 0)
361     timestamp = mktime(&t);
362   strcpy(make, "Rollei");
363   strcpy(model, "d530flex");
364   write_thumb = &LibRaw::rollei_thumb;
365 }
366 
parse_sinar_ia()367 void LibRaw::parse_sinar_ia()
368 {
369   int entries, off;
370   char str[8], *cp;
371 
372   order = 0x4949;
373   fseek(ifp, 4, SEEK_SET);
374   entries = get4();
375   if (entries < 1 || entries > 8192)
376     return;
377   fseek(ifp, get4(), SEEK_SET);
378   while (entries--)
379   {
380     off = get4();
381     get4();
382     fread(str, 8, 1, ifp);
383     str[7] = 0; // Ensure end of string
384     if (!strcmp(str, "META"))
385       meta_offset = off;
386     if (!strcmp(str, "THUMB"))
387       thumb_offset = off;
388     if (!strcmp(str, "RAW0"))
389       data_offset = off;
390   }
391   fseek(ifp, meta_offset + 20, SEEK_SET);
392   fread(make, 64, 1, ifp);
393   make[63] = 0;
394   if ((cp = strchr(make, ' ')))
395   {
396     strcpy(model, cp + 1);
397     *cp = 0;
398   }
399   raw_width = get2();
400   raw_height = get2();
401   load_raw = &LibRaw::unpacked_load_raw;
402   thumb_width = (get4(), get2());
403   thumb_height = get2();
404   write_thumb = &LibRaw::ppm_thumb;
405   maximum = 0x3fff;
406 }
407 
parse_kyocera()408 void LibRaw::parse_kyocera()
409 {
410 
411   int c;
412   static const ushort table[13] = {25,  32,  40,  50,  64,  80, 100,
413                                    125, 160, 200, 250, 320, 400};
414 
415   fseek(ifp, 33, SEEK_SET);
416   get_timestamp(1);
417   fseek(ifp, 52, SEEK_SET);
418   c = get4();
419   if ((c > 6) && (c < 20))
420     iso_speed = table[c - 7];
421   shutter = libraw_powf64l(2.0f, (((float)get4()) / 8.0f)) / 16000.0f;
422   FORC4 cam_mul[RGGB_2_RGBG(c)] = get4();
423   fseek(ifp, 88, SEEK_SET);
424   aperture = libraw_powf64l(2.0f, ((float)get4()) / 16.0f);
425   fseek(ifp, 112, SEEK_SET);
426   focal_len = get4();
427 
428   fseek(ifp, 104, SEEK_SET);
429   ilm.MaxAp4CurFocal = libraw_powf64l(2.0f, ((float)get4()) / 16.0f);
430   fseek(ifp, 124, SEEK_SET);
431   stmread(ilm.Lens, 32, ifp);
432   ilm.CameraMount = LIBRAW_MOUNT_Contax_N;
433   ilm.CameraFormat = LIBRAW_FORMAT_FF;
434   if (ilm.Lens[0])
435   {
436     ilm.LensMount = LIBRAW_MOUNT_Contax_N;
437     ilm.LensFormat = LIBRAW_FORMAT_FF;
438   }
439 }
440 
parse_jpeg(int offset)441 int LibRaw::parse_jpeg(int offset)
442 {
443   int len, save, hlen, mark;
444   fseek(ifp, offset, SEEK_SET);
445   if (fgetc(ifp) != 0xff || fgetc(ifp) != 0xd8)
446     return 0;
447 
448   while (fgetc(ifp) == 0xff && (mark = fgetc(ifp)) != 0xda)
449   {
450     order = 0x4d4d;
451     len = get2() - 2;
452     save = ftell(ifp);
453     if (mark == 0xc0 || mark == 0xc3 || mark == 0xc9)
454     {
455       fgetc(ifp);
456       raw_height = get2();
457       raw_width = get2();
458     }
459     order = get2();
460     hlen = get4();
461     if (get4() == 0x48454150 && (save + hlen) >= 0 &&
462         (save + hlen) <= ifp->size()) /* "HEAP" */
463     {
464       parse_ciff(save + hlen, len - hlen, 0);
465     }
466     if (parse_tiff(save + 6))
467       apply_tiff();
468     fseek(ifp, save + len, SEEK_SET);
469   }
470   return 1;
471 }
472 
parse_thumb_note(int base,unsigned toff,unsigned tlen)473 void LibRaw::parse_thumb_note(int base, unsigned toff, unsigned tlen)
474 {
475   unsigned entries, tag, type, len, save;
476 
477   entries = get2();
478   while (entries--)
479   {
480     tiff_get(base, &tag, &type, &len, &save);
481     if (tag == toff)
482       thumb_offset = get4() + base;
483     if (tag == tlen)
484       thumb_length = get4();
485     fseek(ifp, save, SEEK_SET);
486   }
487 }
488 
parse_broadcom()489 void LibRaw::parse_broadcom()
490 {
491 
492   /* This structure is at offset 0xb0 from the 'BRCM' ident. */
493   struct
494   {
495     uint8_t umode[32];
496     uint16_t uwidth;
497     uint16_t uheight;
498     uint16_t padding_right;
499     uint16_t padding_down;
500     uint32_t unknown_block[6];
501     uint16_t transform;
502     uint16_t format;
503     uint8_t bayer_order;
504     uint8_t bayer_format;
505   } header;
506 
507   header.bayer_order = 0;
508   fseek(ifp, 0xb0 - 0x20, SEEK_CUR);
509   fread(&header, 1, sizeof(header), ifp);
510   raw_stride =
511       ((((((header.uwidth + header.padding_right) * 5) + 3) >> 2) + 0x1f) &
512        (~0x1f));
513   raw_width = width = header.uwidth;
514   raw_height = height = header.uheight;
515   filters = 0x16161616; /* default Bayer order is 2, BGGR */
516 
517   switch (header.bayer_order)
518   {
519   case 0: /* RGGB */
520     filters = 0x94949494;
521     break;
522   case 1: /* GBRG */
523     filters = 0x49494949;
524     break;
525   case 3: /* GRBG */
526     filters = 0x61616161;
527     break;
528   }
529 }
530 
531 /*
532    Returns 1 for a Coolpix 995, 0 for anything else.
533  */
nikon_e995()534 int LibRaw::nikon_e995()
535 {
536   int i, histo[256];
537   const uchar often[] = {0x00, 0x55, 0xaa, 0xff};
538 
539   memset(histo, 0, sizeof histo);
540   fseek(ifp, -2000, SEEK_END);
541   for (i = 0; i < 2000; i++)
542     histo[fgetc(ifp)]++;
543   for (i = 0; i < 4; i++)
544     if (histo[often[i]] < 200)
545       return 0;
546   return 1;
547 }
548 
549 /*
550    Since the TIFF DateTime string has no timezone information,
551    assume that the camera's clock was set to Universal Time.
552  */
get_timestamp(int reversed)553 void LibRaw::get_timestamp(int reversed)
554 {
555   struct tm t;
556   char str[20];
557   int i;
558 
559   str[19] = 0;
560   if (reversed)
561     for (i = 19; i--;)
562       str[i] = fgetc(ifp);
563   else
564     fread(str, 19, 1, ifp);
565   memset(&t, 0, sizeof t);
566   if (sscanf(str, "%d:%d:%d %d:%d:%d", &t.tm_year, &t.tm_mon, &t.tm_mday,
567              &t.tm_hour, &t.tm_min, &t.tm_sec) != 6)
568     return;
569   t.tm_year -= 1900;
570   t.tm_mon -= 1;
571   t.tm_isdst = -1;
572   if (mktime(&t) > 0)
573     timestamp = mktime(&t);
574 }
575 
576 #ifdef USE_6BY9RPI
parse_raspberrypi()577 void LibRaw::parse_raspberrypi()
578 {
579 	//This structure is at offset 0xB0 from the 'BRCM' ident.
580 	struct brcm_raw_header {
581 		uint8_t name[32];
582 		uint16_t h_width;
583 		uint16_t h_height;
584 		uint16_t padding_right;
585 		uint16_t padding_down;
586 		uint32_t dummy[6];
587 		uint16_t transform;
588 		uint16_t format;
589 		uint8_t bayer_order;
590 		uint8_t bayer_format;
591 	};
592 	//Values taken from https://github.com/raspberrypi/userland/blob/master/interface/vctypes/vc_image_types.h
593 #define BRCM_FORMAT_BAYER  33
594 #define BRCM_BAYER_RAW8    2
595 #define BRCM_BAYER_RAW10   3
596 #define BRCM_BAYER_RAW12   4
597 #define BRCM_BAYER_RAW14   5
598 #define BRCM_BAYER_RAW16   6
599 
600 	struct brcm_raw_header header;
601 	uint8_t brcm_tag[4];
602 
603     if (ftell(ifp) > 22LL) // 22 bytes is minimum jpeg size
604     {
605         thumb_length = ftell(ifp);
606         thumb_offset = 0;
607         thumb_width = thumb_height = 0;
608         load_flags |= 0x4000; // flag: we have JPEG from beginning to meta_offset
609     }
610 
611 	// Sanity check that the caller has found a BRCM header
612 	if (!fread(brcm_tag, 1, sizeof(brcm_tag), ifp) ||
613 		memcmp(brcm_tag, "BRCM", sizeof(brcm_tag)))
614 		return;
615 
616 	width = raw_width;
617 	data_offset = ftell(ifp) + 0x8000 - sizeof(brcm_tag);
618 
619 	if (!fseek(ifp, 0xB0 - int(sizeof(brcm_tag)), SEEK_CUR) &&
620 		fread(&header, 1, sizeof(header), ifp)) {
621 		switch (header.bayer_order) {
622 		case 0: //RGGB
623 			filters = 0x94949494;
624 			break;
625 		case 1: //GBRG
626 			filters = 0x49494949;
627 			break;
628 		default:
629 		case 2: //BGGR
630 			filters = 0x16161616;
631 			break;
632 		case 3: //GRBG
633 			filters = 0x61616161;
634 			break;
635 		}
636 
637 		if (header.format == BRCM_FORMAT_BAYER) {
638 			switch (header.bayer_format) {
639 			case BRCM_BAYER_RAW8:
640 				load_raw = &LibRaw::rpi_load_raw8;
641 				//1 pixel per byte
642 				raw_stride = ((header.h_width + header.padding_right) + 31)&(~31);
643 				width = header.h_width;
644 				raw_height = height = header.h_height;
645 				is_raw = 1;
646 				order = 0x4d4d;
647 				break;
648 			case BRCM_BAYER_RAW10:
649 				load_raw = &LibRaw::nokia_load_raw;
650 				//4 pixels per 5 bytes
651 				raw_stride = (((((header.h_width + header.padding_right) * 5) + 3) >> 2) + 31)&(~31);
652 				width = header.h_width;
653 				raw_height = height = header.h_height;
654 				is_raw = 1;
655 				order = 0x4d4d;
656 				break;
657 			case BRCM_BAYER_RAW12:
658 				load_raw = &LibRaw::rpi_load_raw12;
659 				//2 pixels per 3 bytes
660 				raw_stride = (((((header.h_width + header.padding_right) * 3) + 1) >> 1) + 31)&(~31);
661 				width = header.h_width;
662 				raw_height = height = header.h_height;
663 				is_raw = 1;
664 				order = 0x4d4d;
665 				break;
666 			case BRCM_BAYER_RAW14:
667 				load_raw = &LibRaw::rpi_load_raw14;
668 				//4 pixels per 7 bytes
669 				raw_stride = (((((header.h_width + header.padding_right) * 7) + 3) >> 2) + 31)&(~31);
670 				width = header.h_width;
671 				raw_height = height = header.h_height;
672 				is_raw = 1;
673 				order = 0x4d4d;
674 				break;
675 			case BRCM_BAYER_RAW16:
676 				load_raw = &LibRaw::rpi_load_raw16;
677 				//1 pixel per 2 bytes
678 				raw_stride = (((header.h_width + header.padding_right) << 1) + 31)&(~31);
679 				width = header.h_width;
680 				raw_height = height = header.h_height;
681 				is_raw = 1;
682 				order = 0x4d4d;
683 				break;
684 			default:
685 				break;
686 			}
687 		}
688 	}
689 }
690 #endif
691