1 /* -*- C++ -*-
2  * Copyright 2019-2021 LibRaw LLC (info@libraw.org)
3  *
4 
5  LibRaw is free software; you can redistribute it and/or modify
6  it under the terms of the one of two licenses as you choose:
7 
8 1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
9    (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
10 
11 2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
12    (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
13 
14  */
15 
16 #include "../../internal/dcraw_defs.h"
17 
parseSamsungMakernotes(int base,unsigned tag,unsigned type,unsigned len,unsigned dng_writer)18 void LibRaw::parseSamsungMakernotes(int base, unsigned tag, unsigned type,
19                                     unsigned len, unsigned dng_writer)
20 {
21   int i, c;
22   if (tag == 0x0002)
23   {
24     imSamsung.DeviceType = get4();
25     if (imSamsung.DeviceType == 0x2000)
26     {
27       ilm.CameraMount = LIBRAW_MOUNT_Samsung_NX;
28       ilm.CameraFormat = LIBRAW_FORMAT_APSC;
29     }
30     else if (!strncmp(model, "NX mini", 7))
31     { // device type 0x1000: 'NX mini', EX2F, EX1, WB2000
32       ilm.CameraMount = LIBRAW_MOUNT_Samsung_NX_M;
33       ilm.CameraFormat = LIBRAW_FORMAT_1INCH;
34     }
35     else
36     {
37       ilm.CameraMount = LIBRAW_MOUNT_FixedLens;
38       ilm.LensMount = LIBRAW_MOUNT_FixedLens;
39     }
40   }
41   else if (tag == 0x0003)
42   {
43     ilm.CamID = unique_id = get4();
44   }
45   else if (tag == 0x0043)
46   {
47     if ((i = get4()))
48     {
49       imCommon.CameraTemperature = (float)i;
50       if (get4() == 10)
51         imCommon.CameraTemperature /= 10.0f;
52     }
53   }
54   else if ((tag == 0xa002) && (dng_writer != AdobeDNG))
55   {
56     stmread(imgdata.shootinginfo.BodySerial, len, ifp);
57   }
58   else if (tag == 0xa003)
59   {
60     ilm.LensID = get2();
61     if (ilm.LensID)
62       ilm.LensMount = LIBRAW_MOUNT_Samsung_NX;
63   }
64   else if (tag == 0xa004)
65   { // LensFirmware
66     stmread(imSamsung.LensFirmware, len, ifp);
67   }
68   else if (tag == 0xa005)
69   {
70     stmread(imgdata.lens.InternalLensSerial, len, ifp);
71   }
72   else if (tag == 0xa010)
73   {
74     FORC4 imSamsung.ImageSizeFull[c] = get4();
75     FORC4 imSamsung.ImageSizeCrop[c] = get4();
76   }
77   else if ((tag == 0xa011) && ((len == 1) || (len == 2)) && tagtypeIs(LIBRAW_EXIFTAG_TYPE_SHORT))
78   {
79     imSamsung.ColorSpace[0] = (int)get2();
80 		switch (imSamsung.ColorSpace[0]) {
81 		case 0:
82 			imCommon.ColorSpace = LIBRAW_COLORSPACE_sRGB;
83 			break;
84 		case 1:
85 			imCommon.ColorSpace = LIBRAW_COLORSPACE_AdobeRGB;
86 			break;
87 		default:
88 			imCommon.ColorSpace = LIBRAW_COLORSPACE_Unknown;
89 			break;
90 		}
91     if (len == 2)
92       imSamsung.ColorSpace[1] = (int)get2();
93   }
94   else if (tag == 0xa019)
95   {
96     ilm.CurAp = getreal(type);
97   }
98   else if ((tag == 0xa01a) && (unique_id != 0x5000000) &&
99            (!imgdata.lens.FocalLengthIn35mmFormat))
100   {
101     ilm.FocalLengthIn35mmFormat = get4();
102     if (ilm.FocalLengthIn35mmFormat >= 160)
103       ilm.FocalLengthIn35mmFormat /= 10.0f;
104     if ((ilm.CameraMount == LIBRAW_MOUNT_Samsung_NX_M) &&
105         (imSamsung.LensFirmware[10] < '6'))
106       ilm.FocalLengthIn35mmFormat *= 1.6f;
107   }
108   else if (tag == 0xa020)
109   {
110     FORC(11) imSamsung.key[c] = get4();
111   }
112   else if ((tag == 0xa021) && (dng_writer == nonDNG))
113   {
114     FORC4 cam_mul[RGGB_2_RGBG(c)] = get4() - imSamsung.key[c];
115   }
116   else if (tag == 0xa022)
117   {
118     FORC4 icWBC[LIBRAW_WBI_Auto][RGGB_2_RGBG(c)] =
119         get4() - imSamsung.key[c + 4];
120     if (icWBC[LIBRAW_WBI_Auto][0] <
121         (icWBC[LIBRAW_WBI_Auto][1] >> 1))
122     {
123       icWBC[LIBRAW_WBI_Auto][1] =
124           icWBC[LIBRAW_WBI_Auto][1] >> 4;
125       icWBC[LIBRAW_WBI_Auto][3] =
126           icWBC[LIBRAW_WBI_Auto][3] >> 4;
127     }
128   }
129   else if (tag == 0xa023)
130   {
131     ushort ki[4] = {8, 9, 10, 0};
132     FORC4 icWBC[LIBRAW_WBI_Ill_A][RGGB_2_RGBG(c)] =
133         get4() - imSamsung.key[ki[c]];
134     if (icWBC[LIBRAW_WBI_Ill_A][0] <
135         (icWBC[LIBRAW_WBI_Ill_A][1] >> 1))
136     {
137       icWBC[LIBRAW_WBI_Ill_A][1] =
138           icWBC[LIBRAW_WBI_Ill_A][1] >> 4;
139       icWBC[LIBRAW_WBI_Ill_A][3] =
140           icWBC[LIBRAW_WBI_Ill_A][3] >> 4;
141     }
142   }
143   else if (tag == 0xa024)
144   {
145     FORC4 icWBC[LIBRAW_WBI_D65][RGGB_2_RGBG(c)] =
146         get4() - imSamsung.key[c + 1];
147     if (icWBC[LIBRAW_WBI_D65][0] <
148         (icWBC[LIBRAW_WBI_D65][1] >> 1))
149     {
150       icWBC[LIBRAW_WBI_D65][1] =
151           icWBC[LIBRAW_WBI_D65][1] >> 4;
152       icWBC[LIBRAW_WBI_D65][3] =
153           icWBC[LIBRAW_WBI_D65][3] >> 4;
154     }
155   }
156   else if (tag == 0xa025)
157   {
158     unsigned t = get4() + imSamsung.key[0];
159     if (t == 4096)
160       imSamsung.DigitalGain = 1.0;
161     else
162       imSamsung.DigitalGain = ((double)t) / 4096.0;
163   }
164   else if ((tag == 0xa028) && (dng_writer == nonDNG))
165   {
166     FORC4 cblack[RGGB_2_RGBG(c)] = get4() - imSamsung.key[c];
167   }
168   else if ((tag == 0xa030) && (len == 9))
169   {
170     for (i = 0; i < 3; i++)
171       FORC3 imgdata.color.ccm[i][c] =
172           (float)((short)((get4() + imSamsung.key[i * 3 + c]))) / 256.0;
173   }
174   else if ((tag == 0xa032) && (len == 9) && (dng_writer == nonDNG))
175   {
176     double aRGB_cam[3][3];
177     FORC(9)
178     ((double *)aRGB_cam)[c] =
179         ((double)((short)((get4() + imSamsung.key[c])))) / 256.0;
180     aRGB_coeff(aRGB_cam);
181   }
182 }
183