PageRenderTime 80ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 2ms

/Sources/ptDcRaw.cpp

https://code.google.com/p/photivo/
C++ | 9640 lines | 8460 code | 595 blank | 585 comment | 3306 complexity | 6bd366b9ee58b4f6fb38567fb6e12d2e MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*******************************************************************************
  2. **
  3. ** Photivo
  4. **
  5. ** Copyright (C) 2008,2009 Jos De Laender <jos.de_laender@telenet.be>
  6. ** Copyright (C) 2009-2012 Michael Munzert <mail@mm-log.com>
  7. ** Copyright (C) 2011 Bernd Schoeler <brjohn@brother-john.net>
  8. **
  9. ** This file is part of Photivo.
  10. **
  11. ** Photivo is free software: you can redistribute it and/or modify
  12. ** it under the terms of the GNU General Public License version 3
  13. ** as published by the Free Software Foundation.
  14. **
  15. ** Photivo is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ** GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with Photivo. If not, see <http://www.gnu.org/licenses/>.
  22. **
  23. *******************************************************************************/
  24. /*
  25. ** This is basically the translation into a more or less C++ object of
  26. ** dcraw.c -- Dave Coffin's raw photo decoder
  27. ** Copyright 1997-2008 by Dave Coffin, dcoffin a cybercom o net
  28. **
  29. *******************************************************************************/
  30. #include "ptDcRaw.h"
  31. #include "ptDefines.h"
  32. #include "ptError.h"
  33. #include "ptConstants.h"
  34. #include "ptCalloc.h"
  35. #include <cassert>
  36. #define NO_JASPER
  37. #ifndef NO_JASPER
  38. #include <jasper/jasper.h>
  39. #endif
  40. // Macro fix for explicit fread returnvalue check.
  41. #define ptfread(ptr,size,n,stream) \
  42. { \
  43. size_t RV = fread(ptr,size,n,stream); \
  44. if (RV != (size_t) n) assert(!ferror(stream)); \
  45. }
  46. #define ptfwrite(ptr,size,n,stream) \
  47. { \
  48. size_t RV = fwrite(ptr,size,n,stream); \
  49. assert(RV == (size_t) n); \
  50. }
  51. #define ptfscanf(file,format,arg) \
  52. { \
  53. int RV = fscanf(file,format,arg); \
  54. assert (RV == 1); \
  55. }
  56. #define ptfgets(str,num,file) \
  57. { \
  58. char* RV = fgets(str,num,file); \
  59. assert (RV); \
  60. }
  61. inline void VAppend(TImage8RawData &AVector, char* AArray, const int ALength) {
  62. char* hEnd = AArray + ALength * sizeof(char);
  63. AVector.insert(AVector.end(), AArray, hEnd);
  64. }
  65. // The class.
  66. #define CLASS ptDcRaw::
  67. CLASS ptDcRaw() {
  68. //printf("(%s,%d) '%s'\n",__FILE__,__LINE__,__PRETTY_FUNCTION__);
  69. // This were the original global variables initialized.
  70. // Now moved into constructor.
  71. // All m_UserSetting* are obviously ,uh, usersettings
  72. // that were done via the command line parameters.
  73. m_UserSetting_ShotSelect=0;
  74. m_UserSetting_MaxMultiplier=0;
  75. m_UserSetting_Multiplier[0]=0;
  76. m_UserSetting_Multiplier[1]=0;
  77. m_UserSetting_Multiplier[2]=0;
  78. m_UserSetting_Multiplier[3]=0;
  79. m_UserSetting_HalfSize=0;
  80. m_UserSetting_HotpixelReduction=0;
  81. m_UserSetting_BayerDenoise=0;
  82. m_UserSetting_CfaLineDn=0;
  83. m_UserSetting_GreenEquil=0;
  84. m_UserSetting_CaCorrect=0;
  85. m_UserSetting_CaRed=0;
  86. m_UserSetting_CaBlue=0;
  87. m_UserSetting_AutoWb=0;
  88. m_UserSetting_CameraWb=0;
  89. m_UserSetting_CameraMatrix=-1;
  90. m_UserSetting_GreyBox[0] = 0;
  91. m_UserSetting_GreyBox[1] = 0;
  92. m_UserSetting_GreyBox[2] = 0xFFFF;
  93. m_UserSetting_GreyBox[3] = 0xFFFF;
  94. m_UserSetting_photivo_ClipMode = ptClipMode_Clip;
  95. m_UserSetting_photivo_ClipParameter = 0;
  96. m_UserSetting_Quality = 3;
  97. m_UserSetting_BlackPoint = -1;
  98. m_UserSetting_Saturation = -1;
  99. m_UserSetting_InputFileName = NULL;
  100. m_UserSetting_DetailView = 0;
  101. m_UserSetting_DetailViewCropX = 0;
  102. m_UserSetting_DetailViewCropY = 0;
  103. m_UserSetting_DetailViewCropW = 0;
  104. m_UserSetting_DetailViewCropH = 0;
  105. m_UserSetting_BadPixelsFileName = NULL;
  106. m_UserSetting_DarkFrameFileName = NULL;
  107. m_UserSetting_AdjustMaximum = 0;
  108. m_UserSetting_DenoiseThreshold = 0;
  109. m_UserSetting_InterpolationPasses = 0;
  110. m_UserSetting_MedianPasses = 0;
  111. m_UserSetting_ESMedianPasses = 0;
  112. // Safety settings to have NULL on uninitialized images.
  113. m_Image = NULL;
  114. m_Image_AfterPhase1 = NULL;
  115. m_Image_AfterPhase2 = NULL;
  116. m_Image_AfterPhase3 = NULL;
  117. m_Image_AfterPhase4 = NULL;
  118. // Some other pointers that are in a dynamic environment better NULL.
  119. m_MetaData = NULL;
  120. m_InputFile = NULL;
  121. m_Thumb.clear();
  122. ResetNonUserSettings();
  123. }
  124. ////////////////////////////////////////////////////////////////////////////////
  125. //
  126. // Destructor
  127. // Deallocate everything dynamic.
  128. //
  129. ////////////////////////////////////////////////////////////////////////////////
  130. CLASS ~ptDcRaw() {
  131. //printf("(%s,%d) '%s'\n",__FILE__,__LINE__,__PRETTY_FUNCTION__);
  132. FREE(m_UserSetting_InputFileName);
  133. FREE(m_UserSetting_BadPixelsFileName);
  134. FREE(m_UserSetting_DarkFrameFileName);
  135. FREE(m_Image);
  136. FREE(m_Image_AfterPhase1);
  137. FREE(m_Image_AfterPhase2);
  138. FREE(m_Image_AfterPhase3);
  139. FCLOSE(m_InputFile);
  140. FREE(m_MetaData);
  141. }
  142. ////////////////////////////////////////////////////////////////////////////////
  143. //
  144. // ResetNonUserSettings
  145. // Reset all variables except user settings.
  146. // This is for second entry support.
  147. //
  148. ////////////////////////////////////////////////////////////////////////////////
  149. void CLASS ResetNonUserSettings() {
  150. // Safety settings to have NULL on uninitialized images.
  151. // And freeing the underlying memory (which was long time a leak !)
  152. // FREE(NULL) is safe, so the beginsituation is fine too.
  153. // FREE implies setting of the pointer to NULL
  154. FREE(m_Image);
  155. FREE(m_Image_AfterPhase1);
  156. FREE(m_Image_AfterPhase2);
  157. FREE(m_Image_AfterPhase3);
  158. FREE(m_Image_AfterPhase4);
  159. // Some other pointers that are in a dynamic environment better NULL.
  160. // Same remarks as above.
  161. FREE(m_MetaData);
  162. FCLOSE(m_InputFile);
  163. // This was originally in the identify code, but which is called
  164. // anyway in the beginning. So this is simply global initialization like
  165. // anything else.
  166. m_Tiff_Flip = m_Flip = -1; /* 0 is valid, so -1 is unknown */
  167. m_Filters = (unsigned)(-1); // hack not to change dcraw.
  168. m_RawHeight = m_RawWidth = m_Fuji_Width = m_IsFuji = fuji_layout = cr2_slice[0] = 0;
  169. m_WhiteLevel = m_Height = m_Width = m_TopMargin = m_LeftMargin = 0;
  170. m_ColorDescriptor[0] = m_Description[0] = m_Artist[0] = m_CameraMake[0] = m_CameraModel[0] = m_CameraModelBis[0] = 0;
  171. m_IsoSpeed = m_Shutter = m_Aperture = m_FocalLength = unique_id = 0;
  172. m_Tiff_NrIFDs = 0;
  173. memset (m_Tiff_IFD, 0, sizeof m_Tiff_IFD);
  174. memset (white, 0, sizeof white);
  175. m_ThumbOffset = m_ThumbLength = m_ThumbWidth = m_ThumbHeight = 0;
  176. m_LoadRawFunction = m_ThumbLoadRawFunction = 0;
  177. m_WriteThumb = &CLASS jpeg_thumb;
  178. m_Data_Offset = m_MetaLength = m_Tiff_bps = m_Tiff_Compress = 0;
  179. m_Kodak_cbpp = zero_after_ff = m_DNG_Version = m_Load_Flags = 0;
  180. m_TimeStamp = m_ShotOrder = m_Tiff_Samples = m_BlackLevel = m_IsFoveon = 0;
  181. for (int k=0; k<8; k++) m_CBlackLevel[k] = 0;
  182. m_MixGreen = m_ProfileLength = data_error = m_ZeroIsBad = 0;
  183. m_PixelAspect = m_IsRaw = m_RawColor = 1; m_RawColorPhotivo = 0;
  184. m_TileWidth = m_TileLength = 0;
  185. m_Raw_Image = 0;
  186. memset (m_Mask, 0, sizeof m_Mask);
  187. for (int i=0; i < 4; i++) {
  188. short c;
  189. ASSIGN(m_CameraMultipliers[i], i == 1);
  190. ASSIGN(m_PreMultipliers[i], i < 3);
  191. ASSIGN(m_D65Multipliers[i], i < 3);
  192. for (c=0; c<3; c++) m_cmatrix[c][i] = 0;
  193. for (c=0; c<3; c++) m_MatrixCamRGBToSRGB[c][i] = c == i;
  194. }
  195. m_Colors = 3;
  196. for (int i=0; i < 0x10000; i++) m_Curve[i] = i;
  197. m_Gamma[0] = 0.45;
  198. m_Gamma[1] = 4.50;
  199. m_Gamma[2] = 0;
  200. m_Gamma[3] = 0;
  201. m_Gamma[4] = 0;
  202. m_Gamma[5] = 0;
  203. m_getbithuff_bitbuf=0;
  204. m_getbithuff_reset=0;
  205. m_getbithuff_vbits=0;
  206. m_ph1_bithuffbitbuf=0;
  207. m_ph1_bithuffvbits=0;
  208. for (int i = 0; i < 0x4000; i++) m_pana_bits_buf[i] = 0;
  209. m_pana_bits_vbits = 0;
  210. for (int i = 0; i < 4096; i++) jpeg_buffer[i] = 0;
  211. for (int i = 0; i < 128; i++) m_sony_decrypt_pad[i] = 0;
  212. m_sony_decrypt_p = 0;
  213. for (int i = 0; i < 1024; i++) m_foveon_decoder_huff[i] = 0;
  214. for (int i = 0; i < 4; i++) {
  215. for (int j = 0; j < 3; j++) {
  216. MatrixXYZToCam[i][j] = 0.0;
  217. }
  218. }
  219. ToCamFunctionInited = 0;
  220. for (int i = 0; i < 0x20000; i++) ToLABFunctionTable[i] = 0.0;
  221. ToLABFunctionInited = 0;
  222. for (int i = 0; i < 3; i++) {
  223. for (int j = 0; j < 4; j++) {
  224. MatrixCamToXYZ[i][j] = 0.0;
  225. }
  226. }
  227. }
  228. /*
  229. In order to inline this calculation, I make the risky
  230. assumption that all filter patterns can be described
  231. by a repeating pattern of eight rows and two columns
  232. Do not use the FC or BAYER macros with the Leaf CatchLight,
  233. because its pattern is 16x16, not 2x8.
  234. Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
  235. PowerShot 600 PowerShot A50 PowerShot Pro70 Pro90 & G1
  236. 0xe1e4e1e4: 0x1b4e4b1e: 0x1e4b4e1b: 0xb4b4b4b4:
  237. 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
  238. 0 G M G M G M 0 C Y C Y C Y 0 Y C Y C Y C 0 G M G M G M
  239. 1 C Y C Y C Y 1 M G M G M G 1 M G M G M G 1 Y C Y C Y C
  240. 2 M G M G M G 2 Y C Y C Y C 2 C Y C Y C Y
  241. 3 C Y C Y C Y 3 G M G M G M 3 G M G M G M
  242. 4 C Y C Y C Y 4 Y C Y C Y C
  243. PowerShot A5 5 G M G M G M 5 G M G M G M
  244. 0x1e4e1e4e: 6 Y C Y C Y C 6 C Y C Y C Y
  245. 7 M G M G M G 7 M G M G M G
  246. 0 1 2 3 4 5
  247. 0 C Y C Y C Y
  248. 1 G M G M G M
  249. 2 C Y C Y C Y
  250. 3 M G M G M G
  251. All RGB cameras use one of these Bayer grids:
  252. 0x16161616: 0x61616161: 0x49494949: 0x94949494:
  253. 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5
  254. 0 B G B G B G 0 G R G R G R 0 G B G B G B 0 R G R G R G
  255. 1 G R G R G R 1 B G B G B G 1 R G R G R G 1 G B G B G B
  256. 2 B G B G B G 2 G R G R G R 2 G B G B G B 2 R G R G R G
  257. 3 G R G R G R 3 B G B G B G 3 R G R G R G 3 G B G B G B
  258. */
  259. #define RAW(row,col) \
  260. m_Raw_Image[(row)*m_RawWidth+(col)]
  261. #define FC(row,col) \
  262. (m_Filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
  263. #define BAYER(row,col) \
  264. m_Image[(row)*m_Width + (col)][FC(row,col)]
  265. // old: m_Image[((row) >> m_Shrink)*m_OutWidth + ((col) >> m_Shrink)][FC(row,col)]
  266. #define BAYER2(row,col) \
  267. m_Image[(row)*m_Width + (col)][fcol(row,col)]
  268. int CLASS fcol (int row, int col)
  269. {
  270. static const char filter[16][16] =
  271. { { 2,1,1,3,2,3,2,0,3,2,3,0,1,2,1,0 },
  272. { 0,3,0,2,0,1,3,1,0,1,1,2,0,3,3,2 },
  273. { 2,3,3,2,3,1,1,3,3,1,2,1,2,0,0,3 },
  274. { 0,1,0,1,0,2,0,2,2,0,3,0,1,3,2,1 },
  275. { 3,1,1,2,0,1,0,2,1,3,1,3,0,1,3,0 },
  276. { 2,0,0,3,3,2,3,1,2,0,2,0,3,2,2,1 },
  277. { 2,3,3,1,2,1,2,1,2,1,1,2,3,0,0,1 },
  278. { 1,0,0,2,3,0,0,3,0,3,0,3,2,1,2,3 },
  279. { 2,3,3,1,1,2,1,0,3,2,3,0,2,3,1,3 },
  280. { 1,0,2,0,3,0,3,2,0,1,1,2,0,1,0,2 },
  281. { 0,1,1,3,3,2,2,1,1,3,3,0,2,1,3,2 },
  282. { 2,3,2,0,0,1,3,0,2,0,1,2,3,0,1,0 },
  283. { 1,3,1,2,3,2,3,2,0,2,0,1,1,0,3,0 },
  284. { 0,2,0,3,1,0,0,1,1,3,3,2,3,2,2,1 },
  285. { 2,1,3,2,3,1,2,1,0,3,0,2,0,2,0,2 },
  286. { 0,3,1,0,0,2,0,3,2,1,3,1,1,3,1,3 } };
  287. static const char filter2[6][6] =
  288. { { 1,1,0,1,1,2 },
  289. { 1,1,2,1,1,0 },
  290. { 2,0,1,0,2,1 },
  291. { 1,1,2,1,1,0 },
  292. { 1,1,0,1,1,2 },
  293. { 0,2,1,2,0,1 } };
  294. if (m_Filters == 1) return filter[(row+m_TopMargin)&15][(col+m_LeftMargin)&15];
  295. if (m_Filters == 2) return filter2[(row+6) % 6][(col+6) % 6];
  296. return FC(row,col);
  297. }
  298. #ifndef __GLIBC__
  299. char* CLASS my_memmem (char *haystack, size_t haystacklen,
  300. char *neepte, size_t neeptelen)
  301. {
  302. char *c;
  303. for (c = haystack; c <= haystack + haystacklen - neeptelen; c++)
  304. if (!memcmp (c, neepte, neeptelen))
  305. return c;
  306. return 0;
  307. }
  308. #define memmem my_memmem
  309. #endif
  310. float rgb_cam[3][4];
  311. const double xyz_rgb[3][3] = { /* XYZ from RGB */
  312. { 0.412453, 0.357580, 0.180423 },
  313. { 0.212671, 0.715160, 0.072169 },
  314. { 0.019334, 0.119193, 0.950227 } };
  315. const float d65_white[3] = { 0.950456, 1, 1.088754 };
  316. // Now everything importent is set up, so we can include external demosaicers
  317. #include "dcb/dcb_demosaicing.c"
  318. #include "dcb/dcb_demosaicing_old.c"
  319. #include "vcd/ahd_interpolate_mod.c"
  320. #include "vcd/ahd_partial_interpolate.c"
  321. #include "vcd/refinement.c"
  322. #include "vcd/vcd_interpolate.c"
  323. #include "vcd/es_median_filter.c"
  324. #include "vcd/median_filter_new.c"
  325. #include "perfectraw/lmmse_interpolate.c"
  326. #include "rawtherapee/amaze_interpolate.c"
  327. #include "rawtherapee/cfa_line_dn.c"
  328. #include "rawtherapee/ca_correct.c"
  329. #include "rawtherapee/green_equil.c"
  330. void CLASS merror (void *ptr, const char *where)
  331. {
  332. if (ptr) return;
  333. fprintf (stderr,_("%s: Out of memory in %s\n"), m_UserSetting_InputFileName, where);
  334. longjmp (m_Failure, 1);
  335. }
  336. void CLASS derror()
  337. {
  338. if (!data_error) {
  339. fprintf (stderr, "%s: ", m_UserSetting_InputFileName);
  340. if (feof(m_InputFile))
  341. fprintf (stderr,_("Unexpected end of file\n"));
  342. else
  343. //fprintf (stderr,_("Corrupt data near 0x%lx\n"), (int64_t) ftell(m_InputFile));
  344. fprintf (stderr,_("Corrupt data near 0x%lx\n"), (long unsigned int) ftell(m_InputFile));
  345. }
  346. data_error++;
  347. }
  348. uint16_t CLASS sget2 (uint8_t *s)
  349. {
  350. if (m_ByteOrder == 0x4949) /* "II" means little-endian */
  351. return s[0] | s[1] << 8;
  352. else /* "MM" means big-endian */
  353. return s[0] << 8 | s[1];
  354. }
  355. uint16_t CLASS get2()
  356. {
  357. uint8_t str[2] = { 0xff,0xff };
  358. ptfread (str, 1, 2, m_InputFile);
  359. return sget2(str);
  360. }
  361. unsigned CLASS sget4 (uint8_t *s)
  362. {
  363. if (m_ByteOrder == 0x4949)
  364. return s[0] | s[1] << 8 | s[2] << 16 | s[3] << 24;
  365. else
  366. return s[0] << 24 | s[1] << 16 | s[2] << 8 | s[3];
  367. }
  368. #define sget4(s) sget4((uint8_t *)s)
  369. unsigned CLASS get4()
  370. {
  371. uint8_t str[4] = { 0xff,0xff,0xff,0xff };
  372. ptfread (str, 1, 4, m_InputFile);
  373. return sget4(str);
  374. }
  375. unsigned CLASS getint (int type)
  376. {
  377. return type == 3 ? get2() : get4();
  378. }
  379. float CLASS int_to_float (int i)
  380. {
  381. union { int i; float f; } u;
  382. u.i = i;
  383. return u.f;
  384. }
  385. double CLASS getreal (int type)
  386. {
  387. union { char c[8]; double d; } u;
  388. int i, rev;
  389. switch (type) {
  390. case 3: return (unsigned short) get2();
  391. case 4: return (unsigned int) get4();
  392. case 5: u.d = (unsigned int) get4();
  393. return u.d / (unsigned int) get4();
  394. case 8: return (signed short) get2();
  395. case 9: return (signed int) get4();
  396. case 10: u.d = (signed int) get4();
  397. return u.d / (signed int) get4();
  398. case 11: return int_to_float (get4());
  399. case 12:
  400. rev = 7 * ((m_ByteOrder == 0x4949) == (ntohs(0x1234) == 0x1234));
  401. for (i=0; i < 8; i++)
  402. u.c[i ^ rev] = fgetc(m_InputFile);
  403. return u.d;
  404. default: return fgetc(m_InputFile);
  405. }
  406. }
  407. void CLASS read_shorts (uint16_t *pixel, int count)
  408. {
  409. if (fread (pixel, 2, count, m_InputFile) < (size_t) count) derror();
  410. if ((m_ByteOrder == 0x4949) == (ntohs(0x1234) == 0x1234))
  411. swab ((char *)pixel, (char *)pixel, count*2);
  412. }
  413. /* -> 1438
  414. void CLASS canon_black (double dark[2], int nblack)
  415. {
  416. int c, diff, row, col;
  417. if (!nblack) return;
  418. for (c=0; c<2; c++) dark[c] /= nblack >> 1;
  419. if ((diff = (int)(dark[0] - dark[1])))
  420. for (row=0; row < m_Height; row++)
  421. for (col=1; col < m_Width; col+=2)
  422. BAYER(row,col) += diff;
  423. dark[1] += diff;
  424. m_BlackLevel = (unsigned)((dark[0] + dark[1] + 1) / 2);
  425. }
  426. */
  427. void CLASS canon_600_fixed_wb (int temp)
  428. {
  429. static const short mul[4][5] = {
  430. { 667, 358,397,565,452 },
  431. { 731, 390,367,499,517 },
  432. { 1119, 396,348,448,537 },
  433. { 1399, 485,431,508,688 } };
  434. int lo, hi, i;
  435. float frac=0;
  436. for (lo=4; --lo; )
  437. if (*mul[lo] <= temp) break;
  438. for (hi=0; hi < 3; hi++)
  439. if (*mul[hi] >= temp) break;
  440. if (lo != hi)
  441. frac = (float) (temp - *mul[lo]) / (*mul[hi] - *mul[lo]);
  442. for (i=1; i < 5; i++)
  443. ASSIGN(m_D65Multipliers[i-1],1/(frac * mul[hi][i] + (1-frac) * mul[lo][i]));
  444. }
  445. /* Return values: 0 = white 1 = near white 2 = not white */
  446. int CLASS canon_600_color (int ratio[2], int mar)
  447. {
  448. int clipped=0, target, miss;
  449. if (flash_used) {
  450. if (ratio[1] < -104)
  451. { ratio[1] = -104; clipped = 1; }
  452. if (ratio[1] > 12)
  453. { ratio[1] = 12; clipped = 1; }
  454. } else {
  455. if (ratio[1] < -264 || ratio[1] > 461) return 2;
  456. if (ratio[1] < -50)
  457. { ratio[1] = -50; clipped = 1; }
  458. if (ratio[1] > 307)
  459. { ratio[1] = 307; clipped = 1; }
  460. }
  461. target = flash_used || ratio[1] < 197
  462. ? -38 - (398 * ratio[1] >> 10)
  463. : -123 + (48 * ratio[1] >> 10);
  464. if (target - mar <= ratio[0] &&
  465. target + 20 >= ratio[0] && !clipped) return 0;
  466. miss = target - ratio[0];
  467. if (abs(miss) >= mar*4) return 2;
  468. if (miss < -20) miss = -20;
  469. if (miss > mar) miss = mar;
  470. ratio[0] = target - miss;
  471. return 1;
  472. }
  473. void CLASS canon_600_auto_wb()
  474. {
  475. int mar, row, col, i, j, st, count[] = { 0,0 };
  476. int test[8], total[2][8], ratio[2][2], stat[2];
  477. memset (&total, 0, sizeof total);
  478. i = int ( canon_ev + 0.5 );
  479. if (i < 10) mar = 150;
  480. else if (i > 12) mar = 20;
  481. else mar = 280 - 20 * i;
  482. if (flash_used) mar = 80;
  483. for (row=14; row < m_Height-14; row+=4)
  484. for (col=10; col < m_Width; col+=2) {
  485. for (i=0; i < 8; i++)
  486. test[(i & 4) + FC(row+(i >> 1),col+(i & 1))] =
  487. BAYER(row+(i >> 1),col+(i & 1));
  488. for (i=0; i < 8; i++)
  489. if (test[i] < 150 || test[i] > 1500) goto next;
  490. for (i=0; i < 4; i++)
  491. if (abs(test[i] - test[i+4]) > 50) goto next;
  492. for (i=0; i < 2; i++) {
  493. for (j=0; j < 4; j+=2)
  494. ratio[i][j >> 1] = ((test[i*4+j+1]-test[i*4+j]) << 10) / test[i*4+j];
  495. stat[i] = canon_600_color (ratio[i], mar);
  496. }
  497. if ((st = stat[0] | stat[1]) > 1) goto next;
  498. for (i=0; i < 2; i++)
  499. if (stat[i])
  500. for (j=0; j < 2; j++)
  501. test[i*4+j*2+1] = test[i*4+j*2] * (0x400 + ratio[i][j]) >> 10;
  502. for (i=0; i < 8; i++)
  503. total[st][i] += test[i];
  504. count[st]++;
  505. next: ;
  506. }
  507. if (count[0] | count[1]) {
  508. st = count[0]*200 < count[1];
  509. for (i=0; i < 4; i++)
  510. ASSIGN(m_D65Multipliers[i], 1.0 / (total[st][i] + total[st][i+4]));
  511. }
  512. }
  513. void CLASS canon_600_coeff()
  514. {
  515. static const short table[6][12] = {
  516. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  517. { -1203,1715,-1136,1648, 1388,-876,267,245, -1641,2153,3921,-3409 },
  518. { -615,1127,-1563,2075, 1437,-925,509,3, -756,1268,2519,-2007 },
  519. { -190,702,-1886,2398, 2153,-1641,763,-251, -452,964,3040,-2528 },
  520. { -190,702,-1878,2390, 1861,-1349,905,-393, -432,944,2617,-2105 },
  521. { -807,1319,-1785,2297, 1388,-876,769,-257, -230,742,2067,-1555 } };
  522. int t=0, i, c;
  523. float mc, yc;
  524. mc = VALUE(m_D65Multipliers[1]) / VALUE(m_D65Multipliers[2]);
  525. yc = VALUE(m_D65Multipliers[3]) / VALUE(m_D65Multipliers[2]);
  526. if (mc > 1 && mc <= 1.28 && yc < 0.8789) t=1;
  527. if (mc > 1.28 && mc <= 2) {
  528. if (yc < 0.8789) t=3;
  529. else if (yc <= 2) t=4;
  530. }
  531. if (flash_used) t=5;
  532. for (m_RawColor = i=0; i < 3; i++)
  533. for (c=0; c < m_Colors; c++)
  534. m_MatrixCamRGBToSRGB[i][c] = table[t][i*4 + c] / 1024.0;
  535. }
  536. void CLASS canon_600_load_raw()
  537. {
  538. uint8_t data[1120], *dp;
  539. uint16_t *pix;
  540. int irow, row;
  541. for (irow=row=0; irow < m_Height; irow++) {
  542. if (fread (data, 1, 1120, m_InputFile) < 1120) derror();
  543. pix = m_Raw_Image + row*m_RawWidth;
  544. for (dp=data; dp < data+1120; dp+=10, pix+=8) {
  545. pix[0] = (dp[0] << 2) + (dp[1] >> 6 );
  546. pix[1] = (dp[2] << 2) + (dp[1] >> 4 & 3);
  547. pix[2] = (dp[3] << 2) + (dp[1] >> 2 & 3);
  548. pix[3] = (dp[4] << 2) + (dp[1] & 3);
  549. pix[4] = (dp[5] << 2) + (dp[9] & 3);
  550. pix[5] = (dp[6] << 2) + (dp[9] >> 2 & 3);
  551. pix[6] = (dp[7] << 2) + (dp[9] >> 4 & 3);
  552. pix[7] = (dp[8] << 2) + (dp[9] >> 6 );
  553. }
  554. if ((row+=2) > m_Height) row = 1;
  555. }
  556. }
  557. void CLASS canon_600_correct()
  558. {
  559. int row, col, val;
  560. static const short mul[4][2] =
  561. { { 1141,1145 }, { 1128,1109 }, { 1178,1149 }, { 1128,1109 } };
  562. for (row=0; row < m_Height; row++)
  563. for (col=0; col < m_Width; col++) {
  564. if ((val = BAYER(row,col) - m_BlackLevel) < 0) val = 0;
  565. val = val * mul[row & 3][col & 1] >> 9;
  566. BAYER(row,col) = val;
  567. }
  568. canon_600_fixed_wb(1311);
  569. canon_600_auto_wb();
  570. canon_600_coeff();
  571. m_WhiteLevel = (0x3ff - m_BlackLevel) * 1109 >> 9;
  572. m_BlackLevel = 0;
  573. }
  574. int CLASS canon_s2is()
  575. {
  576. unsigned row;
  577. for (row=0; row < 100; row++) {
  578. fseek (m_InputFile, row*3340 + 3284, SEEK_SET);
  579. if (getc(m_InputFile) > 15) return 1;
  580. }
  581. return 0;
  582. }
  583. /*
  584. getbits(-1) initializes the buffer
  585. getbits(n) where 0 <= n <= 25 returns an n-bit integer
  586. */
  587. unsigned CLASS getbithuff(int nbits,uint16_t *huff)
  588. {
  589. // unsigned c;
  590. int c;
  591. if (nbits == -1)
  592. return m_getbithuff_bitbuf = m_getbithuff_vbits = m_getbithuff_reset = 0;
  593. if (nbits == 0 || m_getbithuff_vbits < 0) return 0;
  594. while (!m_getbithuff_reset && m_getbithuff_vbits < nbits && (c = fgetc(m_InputFile)) != EOF &&
  595. !(m_getbithuff_reset = zero_after_ff && c == 0xff && fgetc(m_InputFile))) {
  596. m_getbithuff_bitbuf = (m_getbithuff_bitbuf << 8) + (uint8_t) c;
  597. m_getbithuff_vbits += 8;
  598. }
  599. c = m_getbithuff_bitbuf << (32-m_getbithuff_vbits) >> (32-nbits);
  600. if (huff) {
  601. m_getbithuff_vbits -= huff[c] >> 8;
  602. c = (uint8_t) huff[c];
  603. } else
  604. m_getbithuff_vbits -= nbits;
  605. if (m_getbithuff_vbits < 0) derror();
  606. return c;
  607. }
  608. #define getbits(n) getbithuff(n,0)
  609. #define gethuff(h) getbithuff(*h,h+1)
  610. /*
  611. Construct a decode tree according the specification in *source.
  612. The first 16 bytes specify how many codes should be 1-bit, 2-bit
  613. 3-bit, etc. Bytes after that are the leaf values.
  614. For example, if the source is
  615. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  616. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  617. then the code is
  618. 00 0x04
  619. 010 0x03
  620. 011 0x05
  621. 100 0x06
  622. 101 0x02
  623. 1100 0x07
  624. 1101 0x01
  625. 11100 0x08
  626. 11101 0x09
  627. 11110 0x00
  628. 111110 0x0a
  629. 1111110 0x0b
  630. 1111111 0xff
  631. */
  632. uint16_t * CLASS make_decoder_ref (const uint8_t **source)
  633. {
  634. int max, len, h, i, j;
  635. const uint8_t *count;
  636. uint16_t *huff;
  637. count = (*source += 16) - 17;
  638. for (max=16; max && !count[max]; max--) {} ;
  639. huff = (uint16_t *) CALLOC (1 + (1 << max), sizeof *huff);
  640. merror (huff, "make_decoder()");
  641. huff[0] = max;
  642. for (h=len=1; len <= max; len++)
  643. for (i=0; i < count[len]; i++, ++*source)
  644. for (j=0; j < 1 << (max-len); j++)
  645. if (h <= 1 << max)
  646. huff[h++] = len << 8 | **source;
  647. return huff;
  648. }
  649. uint16_t * CLASS make_decoder (const uint8_t *source)
  650. {
  651. return make_decoder_ref (&source);
  652. }
  653. void CLASS crw_init_tables (unsigned table, uint16_t *huff[2])
  654. {
  655. static const uint8_t first_tree[3][29] = {
  656. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0,
  657. 0x04,0x03,0x05,0x06,0x02,0x07,0x01,0x08,0x09,0x00,0x0a,0x0b,0xff },
  658. { 0,2,2,3,1,1,1,1,2,0,0,0,0,0,0,0,
  659. 0x03,0x02,0x04,0x01,0x05,0x00,0x06,0x07,0x09,0x08,0x0a,0x0b,0xff },
  660. { 0,0,6,3,1,1,2,0,0,0,0,0,0,0,0,0,
  661. 0x06,0x05,0x07,0x04,0x08,0x03,0x09,0x02,0x00,0x0a,0x01,0x0b,0xff },
  662. };
  663. static const uint8_t second_tree[3][180] = {
  664. { 0,2,2,2,1,4,2,1,2,5,1,1,0,0,0,139,
  665. 0x03,0x04,0x02,0x05,0x01,0x06,0x07,0x08,
  666. 0x12,0x13,0x11,0x14,0x09,0x15,0x22,0x00,0x21,0x16,0x0a,0xf0,
  667. 0x23,0x17,0x24,0x31,0x32,0x18,0x19,0x33,0x25,0x41,0x34,0x42,
  668. 0x35,0x51,0x36,0x37,0x38,0x29,0x79,0x26,0x1a,0x39,0x56,0x57,
  669. 0x28,0x27,0x52,0x55,0x58,0x43,0x76,0x59,0x77,0x54,0x61,0xf9,
  670. 0x71,0x78,0x75,0x96,0x97,0x49,0xb7,0x53,0xd7,0x74,0xb6,0x98,
  671. 0x47,0x48,0x95,0x69,0x99,0x91,0xfa,0xb8,0x68,0xb5,0xb9,0xd6,
  672. 0xf7,0xd8,0x67,0x46,0x45,0x94,0x89,0xf8,0x81,0xd5,0xf6,0xb4,
  673. 0x88,0xb1,0x2a,0x44,0x72,0xd9,0x87,0x66,0xd4,0xf5,0x3a,0xa7,
  674. 0x73,0xa9,0xa8,0x86,0x62,0xc7,0x65,0xc8,0xc9,0xa1,0xf4,0xd1,
  675. 0xe9,0x5a,0x92,0x85,0xa6,0xe7,0x93,0xe8,0xc1,0xc6,0x7a,0x64,
  676. 0xe1,0x4a,0x6a,0xe6,0xb3,0xf1,0xd3,0xa5,0x8a,0xb2,0x9a,0xba,
  677. 0x84,0xa4,0x63,0xe5,0xc5,0xf3,0xd2,0xc4,0x82,0xaa,0xda,0xe4,
  678. 0xf2,0xca,0x83,0xa3,0xa2,0xc3,0xea,0xc2,0xe2,0xe3,0xff,0xff },
  679. { 0,2,2,1,4,1,4,1,3,3,1,0,0,0,0,140,
  680. 0x02,0x03,0x01,0x04,0x05,0x12,0x11,0x06,
  681. 0x13,0x07,0x08,0x14,0x22,0x09,0x21,0x00,0x23,0x15,0x31,0x32,
  682. 0x0a,0x16,0xf0,0x24,0x33,0x41,0x42,0x19,0x17,0x25,0x18,0x51,
  683. 0x34,0x43,0x52,0x29,0x35,0x61,0x39,0x71,0x62,0x36,0x53,0x26,
  684. 0x38,0x1a,0x37,0x81,0x27,0x91,0x79,0x55,0x45,0x28,0x72,0x59,
  685. 0xa1,0xb1,0x44,0x69,0x54,0x58,0xd1,0xfa,0x57,0xe1,0xf1,0xb9,
  686. 0x49,0x47,0x63,0x6a,0xf9,0x56,0x46,0xa8,0x2a,0x4a,0x78,0x99,
  687. 0x3a,0x75,0x74,0x86,0x65,0xc1,0x76,0xb6,0x96,0xd6,0x89,0x85,
  688. 0xc9,0xf5,0x95,0xb4,0xc7,0xf7,0x8a,0x97,0xb8,0x73,0xb7,0xd8,
  689. 0xd9,0x87,0xa7,0x7a,0x48,0x82,0x84,0xea,0xf4,0xa6,0xc5,0x5a,
  690. 0x94,0xa4,0xc6,0x92,0xc3,0x68,0xb5,0xc8,0xe4,0xe5,0xe6,0xe9,
  691. 0xa2,0xa3,0xe3,0xc2,0x66,0x67,0x93,0xaa,0xd4,0xd5,0xe7,0xf8,
  692. 0x88,0x9a,0xd7,0x77,0xc4,0x64,0xe2,0x98,0xa5,0xca,0xda,0xe8,
  693. 0xf3,0xf6,0xa9,0xb2,0xb3,0xf2,0xd2,0x83,0xba,0xd3,0xff,0xff },
  694. { 0,0,6,2,1,3,3,2,5,1,2,2,8,10,0,117,
  695. 0x04,0x05,0x03,0x06,0x02,0x07,0x01,0x08,
  696. 0x09,0x12,0x13,0x14,0x11,0x15,0x0a,0x16,0x17,0xf0,0x00,0x22,
  697. 0x21,0x18,0x23,0x19,0x24,0x32,0x31,0x25,0x33,0x38,0x37,0x34,
  698. 0x35,0x36,0x39,0x79,0x57,0x58,0x59,0x28,0x56,0x78,0x27,0x41,
  699. 0x29,0x77,0x26,0x42,0x76,0x99,0x1a,0x55,0x98,0x97,0xf9,0x48,
  700. 0x54,0x96,0x89,0x47,0xb7,0x49,0xfa,0x75,0x68,0xb6,0x67,0x69,
  701. 0xb9,0xb8,0xd8,0x52,0xd7,0x88,0xb5,0x74,0x51,0x46,0xd9,0xf8,
  702. 0x3a,0xd6,0x87,0x45,0x7a,0x95,0xd5,0xf6,0x86,0xb4,0xa9,0x94,
  703. 0x53,0x2a,0xa8,0x43,0xf5,0xf7,0xd4,0x66,0xa7,0x5a,0x44,0x8a,
  704. 0xc9,0xe8,0xc8,0xe7,0x9a,0x6a,0x73,0x4a,0x61,0xc7,0xf4,0xc6,
  705. 0x65,0xe9,0x72,0xe6,0x71,0x91,0x93,0xa6,0xda,0x92,0x85,0x62,
  706. 0xf3,0xc5,0xb2,0xa4,0x84,0xba,0x64,0xa5,0xb3,0xd2,0x81,0xe5,
  707. 0xd3,0xaa,0xc4,0xca,0xf2,0xb1,0xe4,0xd1,0x83,0x63,0xea,0xc3,
  708. 0xe2,0x82,0xf1,0xa3,0xc2,0xa1,0xc1,0xe3,0xa2,0xe1,0xff,0xff }
  709. };
  710. if (table > 2) table = 2;
  711. huff[0] = make_decoder ( first_tree[table]);
  712. huff[1] = make_decoder (second_tree[table]);
  713. }
  714. /*
  715. Return 0 if the image starts with compressed data,
  716. 1 if it starts with uncompressed low-order bits.
  717. In Canon compressed data, 0xff is always followed by 0x00.
  718. */
  719. int CLASS canon_has_lowbits()
  720. {
  721. uint8_t test[0x4000];
  722. int ret=1, i;
  723. fseek (m_InputFile, 0, SEEK_SET);
  724. ptfread (test, 1, sizeof test, m_InputFile);
  725. for (i=540; i < (int)(sizeof test) - 1; i++)
  726. if (test[i] == 0xff) {
  727. if (test[i+1]) return 1;
  728. ret=0;
  729. }
  730. return ret;
  731. }
  732. void CLASS canon_load_raw()
  733. {
  734. uint16_t *pixel, *prow, *huff[2];
  735. int nblocks, lowbits, i, c, row, r, save, val;
  736. int block, diffbuf[64], leaf, len, diff, carry=0, pnum=0, base[2];
  737. crw_init_tables (m_Tiff_Compress,huff);
  738. lowbits = canon_has_lowbits();
  739. if (!lowbits) m_WhiteLevel = 0x3ff;
  740. fseek (m_InputFile, 540 + lowbits*m_RawHeight*m_RawWidth/4, SEEK_SET);
  741. zero_after_ff = 1;
  742. getbits(-1);
  743. for (row=0; row < m_RawHeight; row+=8) {
  744. pixel = m_Raw_Image + row*m_RawWidth;
  745. nblocks = MIN (8, m_RawHeight-row) * m_RawWidth >> 6;
  746. for (block=0; block < nblocks; block++) {
  747. memset (diffbuf, 0, sizeof diffbuf);
  748. for (i=0; i < 64; i++ ) {
  749. leaf = gethuff(huff[i>0]);
  750. if (leaf == 0 && i) break;
  751. if (leaf == 0xff) continue;
  752. i += leaf >> 4;
  753. len = leaf & 15;
  754. if (len == 0) continue;
  755. diff = getbits(len);
  756. if ((diff & (1 << (len-1))) == 0)
  757. diff -= (1 << len) - 1;
  758. if (i < 64) diffbuf[i] = diff;
  759. }
  760. diffbuf[0] += carry;
  761. carry = diffbuf[0];
  762. for (i=0; i < 64; i++ ) {
  763. if (pnum++ % m_RawWidth == 0)
  764. base[0] = base[1] = 512;
  765. if ((pixel[(block << 6) + i] = base[i & 1] += diffbuf[i]) >> 10)
  766. derror();
  767. }
  768. }
  769. if (lowbits) {
  770. save = ftell(m_InputFile);
  771. fseek (m_InputFile, 26 + row*m_RawWidth/4, SEEK_SET);
  772. for (prow=pixel, i=0; i < m_RawWidth*2; i++) {
  773. c = fgetc(m_InputFile);
  774. for (r=0; r < 8; r+=2, prow++) {
  775. val = (*prow << 2) + ((c >> r) & 3);
  776. if (m_RawWidth == 2672 && val < 512) val += 2;
  777. *prow = val;
  778. }
  779. }
  780. fseek (m_InputFile, save, SEEK_SET);
  781. }
  782. }
  783. FREE(huff[0]);
  784. FREE(huff[1]);
  785. }
  786. /*
  787. Not a full implementation of Lossless JPEG, just
  788. enough to decode Canon, Kodak and Adobe DNG images.
  789. */
  790. struct jhead {
  791. int bits, high, wide, clrs, sraw, psv, restart, vpred[6];
  792. uint16_t *huff[6], *free[4], *row;
  793. };
  794. int CLASS ljpeg_start (struct jhead *jh, int info_only)
  795. {
  796. int c, tag, len;
  797. uint8_t data[0x10000];
  798. const uint8_t *dp;
  799. memset (jh, 0, sizeof *jh);
  800. jh->restart = INT_MAX;
  801. ptfread (data, 2, 1, m_InputFile);
  802. if (data[1] != 0xd8) return 0;
  803. do {
  804. ptfread (data, 2, 2, m_InputFile);
  805. tag = data[0] << 8 | data[1];
  806. len = (data[2] << 8 | data[3]) - 2;
  807. if (tag <= 0xff00) return 0;
  808. ptfread (data, 1, len, m_InputFile);
  809. switch (tag) {
  810. case 0xffc3:
  811. jh->sraw = ((data[7] >> 4) * (data[7] & 15) - 1) & 3;
  812. case 0xffc0:
  813. jh->bits = data[0];
  814. jh->high = data[1] << 8 | data[2];
  815. jh->wide = data[3] << 8 | data[4];
  816. jh->clrs = data[5] + jh->sraw;
  817. if (len == 9 && !m_DNG_Version) getc(m_InputFile);
  818. break;
  819. case 0xffc4:
  820. if (info_only) break;
  821. for (dp = data; dp < data+len && (c = *dp++) < 4; )
  822. jh->free[c] = jh->huff[c] = make_decoder_ref (&dp);
  823. break;
  824. case 0xffda:
  825. jh->psv = data[1+data[0]*2];
  826. jh->bits -= data[3+data[0]*2] & 15;
  827. break;
  828. case 0xffdd:
  829. jh->restart = data[0] << 8 | data[1];
  830. }
  831. } while (tag != 0xffda);
  832. if (info_only) return 1;
  833. for (c=0;c<5;c++) if (!jh->huff[c+1]) jh->huff[c+1] = jh->huff[c];
  834. if (jh->sraw) {
  835. for (c=0;c<4;c++) jh->huff[2+c] = jh->huff[1];
  836. for (c=0;c<(jh->sraw);c++) jh->huff[1+c] = jh->huff[0];
  837. }
  838. jh->row = (uint16_t *) CALLOC (jh->wide*jh->clrs, 4);
  839. merror (jh->row, "ljpeg_start()");
  840. return zero_after_ff = 1;
  841. }
  842. void CLASS ljpeg_end (struct jhead *jh)
  843. {
  844. int c;
  845. for(c=0;c<4;c++) if (jh->free[c]) FREE (jh->free[c]);
  846. FREE (jh->row);
  847. }
  848. int CLASS ljpeg_diff (uint16_t *huff)
  849. {
  850. int len, diff;
  851. len = gethuff(huff);
  852. if (len == 16 && (!m_DNG_Version || m_DNG_Version >= 0x1010000))
  853. return -32768;
  854. diff = getbits(len);
  855. if ((diff & (1 << (len-1))) == 0)
  856. diff -= (1 << len) - 1;
  857. return diff;
  858. }
  859. uint16_t * CLASS ljpeg_row (int jrow, struct jhead *jh)
  860. {
  861. int col, c, diff, pred, spred=0;
  862. uint16_t mark=0, *row[3];
  863. if (jrow * jh->wide % jh->restart == 0) {
  864. for (c=0;c<6;c++) jh->vpred[c] = 1 << (jh->bits-1);
  865. if (jrow) {
  866. fseek(m_InputFile,-2,SEEK_CUR);
  867. do mark = (mark << 8) + (c = fgetc(m_InputFile));
  868. while (c != EOF && mark >> 4 != 0xffd);
  869. }
  870. getbits(-1);
  871. }
  872. for (c=0; c<3; c++) row[c] = jh->row + jh->wide*jh->clrs*((jrow+c) & 1);
  873. for (col=0; col < jh->wide; col++)
  874. for (c=0; c < jh->clrs; c++) {
  875. diff = ljpeg_diff (jh->huff[c]);
  876. if (jh->sraw && c <= jh->sraw && (col | c))
  877. pred = spred;
  878. else if (col) pred = row[0][-jh->clrs];
  879. else pred = (jh->vpred[c] += diff) - diff;
  880. if (jrow && col) switch (jh->psv) {
  881. case 1: break;
  882. case 2: pred = row[1][0]; break;
  883. case 3: pred = row[1][-jh->clrs]; break;
  884. case 4: pred = pred + row[1][0] - row[1][-jh->clrs]; break;
  885. case 5: pred = pred + ((row[1][0] - row[1][-jh->clrs]) >> 1); break;
  886. case 6: pred = row[1][0] + ((pred - row[1][-jh->clrs]) >> 1); break;
  887. case 7: pred = (pred + row[1][0]) >> 1; break;
  888. default: pred = 0;
  889. }
  890. if ((**row = pred + diff) >> jh->bits) derror();
  891. if (c <= jh->sraw) spred = **row;
  892. row[0]++; row[1]++;
  893. }
  894. return row[2];
  895. }
  896. void CLASS lossless_jpeg_load_raw()
  897. {
  898. int jwide, jrow, jcol, val, jidx, i, j, row=0, col=0;
  899. struct jhead jh;
  900. uint16_t *rp;
  901. if (!ljpeg_start (&jh, 0)) return;
  902. jwide = jh.wide * jh.clrs;
  903. TRACEKEYVALS("jh.high","%d",jh.high);
  904. TRACEKEYVALS("jwide","%d",jwide);
  905. for (jrow=0; jrow < jh.high; jrow++) {
  906. rp = ljpeg_row (jrow, &jh);
  907. if (m_Load_Flags & 1)
  908. row = jrow & 1 ? m_Height-1-jrow/2 : jrow/2;
  909. for (jcol=0; jcol < jwide; jcol++) {
  910. val = m_Curve[*rp++];
  911. if (cr2_slice[0]) {
  912. jidx = jrow*jwide + jcol;
  913. i = jidx / (cr2_slice[1]*jh.high);
  914. if ((j = i >= cr2_slice[0]))
  915. i = cr2_slice[0];
  916. jidx -= i * (cr2_slice[1]*jh.high);
  917. row = jidx / cr2_slice[1+j];
  918. col = jidx % cr2_slice[1+j] + i*cr2_slice[1];
  919. }
  920. if (m_RawWidth == 3984 && (col -= 2) < 0)
  921. col += (row--,m_RawWidth);
  922. if (row >= 0) RAW(row,col) = val;
  923. if (++col >= m_RawWidth)
  924. col = (row++,0);
  925. }
  926. }
  927. ljpeg_end(&jh);
  928. // printf("TIEDELIE dark[0]:%f dark[1]:%f nblack:%d\n",dark[0],dark[1],nblack);
  929. }
  930. void CLASS canon_sraw_load_raw()
  931. {
  932. struct jhead jh;
  933. short *rp=0, (*ip)[4];
  934. int jwide, slice, scol, ecol, row, col, jrow=0, jcol=0, pix[3], c;
  935. int v[3]={0,0,0}, ver, hue;
  936. char *cp;
  937. if (!ljpeg_start (&jh, 0)) return;
  938. jwide = (jh.wide >>= 1) * jh.clrs;
  939. for (ecol=slice=0; slice <= cr2_slice[0]; slice++) {
  940. scol = ecol;
  941. ecol += cr2_slice[1] * 2 / jh.clrs;
  942. if (!cr2_slice[0] || ecol > m_RawWidth-1) ecol = m_RawWidth & -2;
  943. for (row=0; row < m_Height; row += (jh.clrs >> 1) - 1) {
  944. ip = (short (*)[4]) m_Image + row*m_Width;
  945. for (col=scol; col < ecol; col+=2, jcol+=jh.clrs) {
  946. if ((jcol %= jwide) == 0)
  947. rp = (short *) ljpeg_row (jrow++, &jh);
  948. if (col >= m_Width) continue;
  949. for (c=0; c< (jh.clrs-2); c++)
  950. ip[col + (c >> 1)*m_Width + (c & 1)][0] = rp[jcol+c];
  951. ip[col][1] = rp[jcol+jh.clrs-2] - 16384;
  952. ip[col][2] = rp[jcol+jh.clrs-1] - 16384;
  953. }
  954. }
  955. }
  956. for (cp=m_CameraModelBis; *cp && !isdigit(*cp); cp++) {};
  957. sscanf (cp, "%d.%d.%d", v, v+1, v+2);
  958. ver = (v[0]*1000 + v[1])*1000 + v[2];
  959. hue = (jh.sraw+1) << 2;
  960. if (unique_id >= 0x80000281 || (unique_id == 0x80000218 && ver > 1000006))
  961. hue = jh.sraw << 1;
  962. ip = (short (*)[4]) m_Image;
  963. rp = ip[0];
  964. for (row=0; row < m_Height; row++, ip+=m_Width) {
  965. if (row & (jh.sraw >> 1))
  966. for (col=0; col < m_Width; col+=2)
  967. for (c=1; c < 3; c++)
  968. if (row == m_Height-1) {
  969. ip[col][c] = ip[col-m_Width][c];
  970. } else {
  971. ip[col][c] = (ip[col-m_Width][c] + ip[col+m_Width][c] + 1) >> 1;
  972. }
  973. for (col=1; col < m_Width; col+=2)
  974. for (c=1; c < 3; c++)
  975. if (col == m_Width-1)
  976. ip[col][c] = ip[col-1][c];
  977. else ip[col][c] = (ip[col-1][c] + ip[col+1][c] + 1) >> 1;
  978. }
  979. for ( ; rp < ip[0]; rp+=4) {
  980. if (unique_id == 0x80000218 ||
  981. unique_id == 0x80000250 ||
  982. unique_id == 0x80000261 ||
  983. unique_id == 0x80000281 ||
  984. unique_id == 0x80000287) {
  985. rp[1] = (rp[1] << 2) + hue;
  986. rp[2] = (rp[2] << 2) + hue;
  987. pix[0] = rp[0] + (( 200*rp[1] + 22929*rp[2]) >> 14);
  988. pix[1] = rp[0] + ((-5640*rp[1] - 11751*rp[2]) >> 14);
  989. pix[2] = rp[0] + ((29040*rp[1] - 101*rp[2]) >> 14);
  990. } else {
  991. if (unique_id < 0x80000218) rp[0] -= 512;
  992. pix[0] = rp[0] + rp[2];
  993. pix[2] = rp[0] + rp[1];
  994. pix[1] = rp[0] + ((-778*rp[1] - (rp[2] << 11)) >> 12);
  995. }
  996. for (c=0;c<3;c++) rp[c] = CLIP(pix[c] * sraw_mul[c] >> 10);
  997. }
  998. ljpeg_end(&jh);
  999. m_WhiteLevel = 0x3fff;
  1000. }
  1001. void CLASS adobe_copy_pixel (unsigned row, unsigned col, uint16_t **rp)
  1002. {
  1003. int c;
  1004. if (m_IsRaw == 2 && m_UserSetting_ShotSelect) (*rp)++;
  1005. if (m_Raw_Image) {
  1006. if (row < m_RawHeight && col < m_RawWidth)
  1007. RAW(row,col) = m_Curve[**rp];
  1008. *rp += m_IsRaw;
  1009. } else {
  1010. if (row < m_Height && col < m_Width)
  1011. for (c=0; c < (int32_t)m_Tiff_Samples; c++)
  1012. m_Image[row*m_Width+col][c] = m_Curve[(*rp)[c]];
  1013. *rp += m_Tiff_Samples;
  1014. }
  1015. if (m_IsRaw == 2 && m_UserSetting_ShotSelect) (*rp)--;
  1016. }
  1017. void CLASS lossless_dng_load_raw()
  1018. {
  1019. unsigned save, trow=0, tcol=0, jwide, jrow, jcol, row, col;
  1020. struct jhead jh;
  1021. uint16_t *rp;
  1022. while (trow < m_RawHeight) {
  1023. save = ftell(m_InputFile);
  1024. if (m_TileLength < INT_MAX)
  1025. fseek (m_InputFile, get4(), SEEK_SET);
  1026. if (!ljpeg_start (&jh, 0)) break;
  1027. jwide = jh.wide;
  1028. if (m_Filters) jwide *= jh.clrs;
  1029. jwide /= m_IsRaw;
  1030. for (row=col=jrow=0; jrow < (unsigned) jh.high; jrow++) {
  1031. rp = ljpeg_row (jrow, &jh);
  1032. for (jcol=0; jcol < jwide; jcol++) {
  1033. adobe_copy_pixel (trow+row, tcol+col, &rp);
  1034. if (++col >= m_TileWidth || col >= m_RawWidth)
  1035. row += 1 + (col = 0);
  1036. }
  1037. }
  1038. fseek (m_InputFile, save+4, SEEK_SET);
  1039. if ((tcol += m_TileWidth) >= m_RawWidth)
  1040. trow += m_TileLength + (tcol = 0);
  1041. ljpeg_end(&jh);
  1042. }
  1043. }
  1044. void CLASS packed_dng_load_raw()
  1045. {
  1046. uint16_t *pixel, *rp;
  1047. // int row, col;
  1048. unsigned row, col;
  1049. pixel = (uint16_t *) CALLOC (m_RawWidth * m_Tiff_Samples, sizeof *pixel);
  1050. merror (pixel, "packed_dng_load_raw()");
  1051. for (row=0; row < m_RawHeight; row++) {
  1052. if (m_Tiff_bps == 16)
  1053. read_shorts (pixel, m_RawWidth * m_Tiff_Samples);
  1054. else {
  1055. getbits(-1);
  1056. for (col=0; col < m_RawWidth * m_Tiff_Samples; col++)
  1057. pixel[col] = getbits(m_Tiff_bps);
  1058. }
  1059. for (rp=pixel, col=0; col < m_RawWidth; col++)
  1060. adobe_copy_pixel (row, col, &rp);
  1061. }
  1062. FREE (pixel);
  1063. }
  1064. void CLASS pentax_load_raw()
  1065. {
  1066. uint16_t bit[2][15], huff[4097];
  1067. int dep, row, col, diff, c, i;
  1068. uint16_t vpred[2][2] = {{0,0},{0,0}}, hpred[2];
  1069. fseek (m_InputFile, meta_offset, SEEK_SET);
  1070. dep = (get2() + 12) & 15;
  1071. fseek (m_InputFile, 12, SEEK_CUR);
  1072. for(c=0;c<dep;c++) bit[0][c] = get2();
  1073. for(c=0;c<dep;c++) bit[1][c] = fgetc(m_InputFile);
  1074. for(c=0;c<dep;c++)
  1075. for (i=bit[0][c]; i <= ((bit[0][c]+(4096 >> bit[1][c])-1) & 4095); )
  1076. huff[++i] = bit[1][c] << 8 | c;
  1077. huff[0] = 12;
  1078. fseek (m_InputFile, m_Data_Offset, SEEK_SET);
  1079. getbits(-1);
  1080. for (row=0; row < m_RawHeight; row++)
  1081. for (col=0; col < m_RawWidth; col++) {
  1082. diff = ljpeg_diff (huff);
  1083. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1084. else hpred[col & 1] += diff;
  1085. RAW(row,col) = hpred[col & 1];
  1086. if (hpred[col & 1] >> m_Tiff_bps) derror();
  1087. }
  1088. }
  1089. void CLASS nikon_load_raw()
  1090. {
  1091. static const uint8_t nikon_tree[][32] = {
  1092. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy */
  1093. 5,4,3,6,2,7,1,0,8,9,11,10,12 },
  1094. { 0,1,5,1,1,1,1,1,1,2,0,0,0,0,0,0, /* 12-bit lossy after split */
  1095. 0x39,0x5a,0x38,0x27,0x16,5,4,3,2,1,0,11,12,12 },
  1096. { 0,1,4,2,3,1,2,0,0,0,0,0,0,0,0,0, /* 12-bit lossless */
  1097. 5,4,6,3,7,2,8,1,9,0,10,11,12 },
  1098. { 0,1,4,3,1,1,1,1,1,2,0,0,0,0,0,0, /* 14-bit lossy */
  1099. 5,6,4,7,8,3,9,2,1,0,10,11,12,13,14 },
  1100. { 0,1,5,1,1,1,1,1,1,1,2,0,0,0,0,0, /* 14-bit lossy after split */
  1101. 8,0x5c,0x4b,0x3a,0x29,7,6,5,4,3,2,1,0,13,14 },
  1102. { 0,1,4,2,2,3,1,2,0,0,0,0,0,0,0,0, /* 14-bit lossless */
  1103. 7,6,8,5,9,4,10,3,11,12,2,0,1,13,14 } };
  1104. uint16_t *huff, ver0, ver1, vpred[2][2], hpred[2], csize;
  1105. int i, min, max, step=0, tree=0, split=0, row, col, len, shl, diff;
  1106. fseek (m_InputFile, meta_offset, SEEK_SET);
  1107. ver0 = fgetc(m_InputFile);
  1108. ver1 = fgetc(m_InputFile);
  1109. if (ver0 == 0x49 || ver1 == 0x58)
  1110. fseek (m_InputFile, 2110, SEEK_CUR);
  1111. if (ver0 == 0x46) tree = 2;
  1112. if (m_Tiff_bps == 14) tree += 3;
  1113. read_shorts (vpred[0], 4);
  1114. max = 1 << m_Tiff_bps & 0x7fff;
  1115. if ((csize = get2()) > 1)
  1116. step = max / (csize-1);
  1117. if (ver0 == 0x44 && ver1 == 0x20 && step > 0) {
  1118. for (i=0; i < csize; i++)
  1119. m_Curve[i*step] = get2();
  1120. for (i=0; i < max; i++)
  1121. m_Curve[i] = ( m_Curve[i-i%step]*(step-i%step) +
  1122. m_Curve[i-i%step+step]*(i%step) ) / step;
  1123. fseek (m_InputFile, meta_offset+562, SEEK_SET);
  1124. split = get2();
  1125. } else if (ver0 != 0x46 && csize <= 0x4001)
  1126. read_shorts (m_Curve, max=csize);
  1127. while (m_Curve[max-2] == m_Curve[max-1]) max--;
  1128. huff = make_decoder(nikon_tree[tree]);
  1129. fseek (m_InputFile, m_Data_Offset, SEEK_SET);
  1130. getbits(-1);
  1131. for (min=row=0; row < m_Height; row++) {
  1132. if (split && row == split) {
  1133. FREE(huff);
  1134. huff = make_decoder(nikon_tree[tree+1]);
  1135. max += (min = 16) << 1;
  1136. }
  1137. for (col=0; col < m_RawWidth; col++) {
  1138. i = gethuff(huff);
  1139. len = i & 15;
  1140. shl = i >> 4;
  1141. diff = ((getbits(len-shl) << 1) + 1) << shl >> 1;
  1142. if ((diff & (1 << (len-1))) == 0)
  1143. diff -= (1 << len) - !shl;
  1144. if (col < 2) hpred[col] = vpred[row & 1][col] += diff;
  1145. else hpred[col & 1] += diff;
  1146. if ((uint16_t)(hpred[col & 1] + min) >= max) derror();
  1147. RAW(row,col) = m_Curve[LIM((int16_t)hpred[col & 1],(int16_t)0,(int16_t)0x3fff)];
  1148. }
  1149. }
  1150. FREE(huff);
  1151. }
  1152. /*
  1153. Returns 1 for a Coolpix 995, 0 for anything else.
  1154. */
  1155. int CLASS nikon_e995()
  1156. {
  1157. int i, histo[256];
  1158. const uint8_t often[] = { 0x00, 0x55, 0xaa, 0xff };
  1159. memset (histo, 0, sizeof histo);
  1160. fseek (m_InputFile, -2000, SEEK_END);
  1161. for (i=0; i < 2000; i++)
  1162. histo[fgetc(m_InputFile)]++;
  1163. for (i=0; i < 4; i++)
  1164. if (histo[often[i]] < 200)
  1165. return 0;
  1166. return 1;
  1167. }
  1168. /*
  1169. Returns 1 for a Coolpix 2100, 0 for anything else.
  1170. */
  1171. int CLASS nikon_e2100()
  1172. {
  1173. uint8_t t[12];
  1174. int i;
  1175. fseek (m_InputFile, 0, SEEK_SET);
  1176. for (i=0; i < 1024; i++) {
  1177. ptfread (t, 1, 12, m_InputFile);
  1178. if (((t[2] & t[4] & t[7] & t[9]) >> 4
  1179. & t[1] & t[6] & t[8] & t[11] & 3) != 3)
  1180. return 0;
  1181. }
  1182. return 1;
  1183. }
  1184. void CLASS nikon_3700()
  1185. {
  1186. int bits; // i;
  1187. uint8_t dp[24];
  1188. static const struct {
  1189. int bits;
  1190. char make[12], model[15];
  1191. } table[] = {
  1192. { 0x00, "PENTAX", "Optio 33WR" },
  1193. { 0x03, "NIKON", "E3200" },
  1194. { 0x32, "NIKON", "E3700" },
  1195. { 0x33, "OLYMPUS", "C740UZ" } };
  1196. fseek (m_InputFile, 3072, SEEK_SET);
  1197. ptfread (dp, 1, 24, m_InputFile);
  1198. bits = (dp[8] & 3) << 4 | (dp[20] & 3);
  1199. for (unsigned i=0; i < sizeof table / sizeof *table; i++)
  1200. if (bits == table[i].bits) {
  1201. strcpy (m_CameraMake, table[i].make );
  1202. strcpy (m_CameraModel, table[i].model);
  1203. }
  1204. }
  1205. /*
  1206. Separates a Minolta DiMAGE Z2 from a Nikon E4300.
  1207. */
  1208. int CLASS minolta_z2()
  1209. {
  1210. unsigned i,nz;
  1211. char tail[424];
  1212. fseek (m_InputFile, (long)(-sizeof tail), SEEK_END);
  1213. ptfread (tail, 1, sizeof tail, m_InputFile);
  1214. for (nz=i=0; i < sizeof tail; i++)
  1215. if (tail[i]) nz++;
  1216. return nz > 20;
  1217. }
  1218. //void CLASS jpeg_thumb ();
  1219. void CLASS ppm_thumb ()
  1220. /* Remember: This function is modified to write the raw’s thumbnail to the
  1221. m_Thumb instead of a file on disk. Always access thumbnails via DcRaw::thumbnail()!
  1222. */
  1223. {
  1224. char *thumb;
  1225. m_ThumbLength = m_ThumbWidth*m_ThumbHeight*3;
  1226. thumb = (char *) MALLOC (m_ThumbLength);
  1227. merror (thumb, "ppm_thumb()");
  1228. //fprintf (m_OutputFile, "P6\n%d %d\n255\n", m_ThumbWidth, m_ThumbHeight);
  1229. QString dummy = QString("P6\n%1 %2\n255\n").arg(m_ThumbWidth).arg(m_ThumbHeight);
  1230. VAppend(m_Thumb, dummy.toLocal8Bit().data(), dummy.length());
  1231. ptfread (thumb, 1, m_ThumbLength, m_InputFile);
  1232. VAppend(m_Thumb, thumb, m_ThumbLength);
  1233. FREE (thumb);
  1234. }
  1235. void CLASS ppm16_thumb()
  1236. {
  1237. int i;
  1238. char *thumb;
  1239. m_ThumbLength = m_ThumbWidth*m_ThumbHeight*3;
  1240. thumb = (char *) CALLOC (m_ThumbLength,2);
  1241. merror (thumb, "ppm16_thumb()");
  1242. read_shorts ((uint16_t *) thumb, m_ThumbLength);
  1243. for (i=0; i < (int32_t)m_ThumbLength; i++)
  1244. thumb[i] = ((uint16_t *) thumb)[i] >> 8;
  1245. QString dummy = QString("P6\n%1 %2\n255\n").arg(m_ThumbWidth).arg(m_ThumbHeight);
  1246. VAppend(m_Thumb, dummy.toLocal8Bit().data(), dummy.length());
  1247. //fprintf (ofp, "P6\n%d %d\n255\n", m_ThumbWidth, m_ThumbHeight);
  1248. //fwrite (thumb, 1, m_ThumbLength, ofp);
  1249. VAppend(m_Thumb, thumb, m_ThumbLength);
  1250. FREE (thumb);
  1251. }
  1252. void CLASS layer_thumb ()
  1253. /* Remember: This function is modified to write the raw’s thumbnail to the
  1254. m_Thumb instead of a file on disk. Always access thumbnails via DcRaw::thumbnail()!
  1255. */
  1256. // TODO: Tests needed: What format is this? Can it be read by QPixmap?
  1257. {
  1258. // int i, c;
  1259. int c;
  1260. char *thumb, map[][4] = { "012","102" };
  1261. m_Colors = m_ThumbMisc >> 5 & 7;
  1262. m_ThumbLength = m_ThumbWidth*m_ThumbHeight;
  1263. thumb = (char *) CALLOC (m_Colors, m_ThumbLength);
  1264. merror (thumb, "layer_thumb()");
  1265. //fprintf (m_OutputFile, "P%d\n%d %d\n255\n", 5 + (m_Colors >> 1), m_ThumbWidth, m_ThumbHeight);
  1266. QString dummy = QString("P%1\n%2 %3\n255\n")
  1267. .arg(5 + (m_Colors >> 1)).arg(m_ThumbWidth).arg(m_ThumbHeight);
  1268. VAppend(m_Thumb, dummy.toLocal8Bit().data(), dummy.length());
  1269. ptfread (thumb, m_ThumbLength, m_Colors, m_InputFile);
  1270. for (unsigned i=0; i < m_ThumbLength; i++) {
  1271. for (c=0; c < m_Colors; c++) {
  1272. //putc (thumb[i+m_ThumbLength*(map[m_ThumbMisc >> 8][c]-'0')], m_OutputFile);
  1273. #pragma GCC diagnostic push
  1274. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
  1275. m_Thumb.push_back(thumb[i+m_ThumbLength*(map[m_ThumbMisc >> 8][c]-'0')]);
  1276. #pragma GCC diagnostic pop
  1277. }
  1278. }
  1279. FREE (thumb);
  1280. }
  1281. void CLASS rollei_thumb ()
  1282. /* Remember: This function is modified to write the raw’s thumbnail to the
  1283. m_Thumb instead of a file on disk. Always access thumbnails via DcRaw::thumbnail()!
  1284. */
  1285. // TODO: Tests needed: What format is this? Can it be read by QPixmap?
  1286. {
  1287. unsigned i;
  1288. uint16_t *thumb;
  1289. m_ThumbLength = m_ThumbWidth * m_ThumbHeight;
  1290. thumb = (uint16_t *) CALLOC (m_ThumbLength, 2);
  1291. merror (thumb, "rollei_thumb()");
  1292. //fprintf (m_OutputFile, "P6\n%d %d\n255\n", m_ThumbWidth, m_ThumbHeight);
  1293. QString dummy = QString("P6\n%1 %2\n255\n").arg(m_ThumbWidth).arg(m_ThumbHeight);
  1294. VAppend(m_Thumb, dummy.toLocal8Bit().data(), dummy.length());
  1295. read_shorts (thumb, m_ThumbLength);
  1296. for (i=0; i < m_ThumbLength; i++) {
  1297. //putc (thumb[i] << 3, m_OutputFile);
  1298. //putc (thumb[i] >> 5 << 2, m_OutputFile);
  1299. //putc (thumb[i] >> 11 << 3, m_OutputFile);
  1300. #pragma GCC diagnostic push
  1301. #pragma GCC diagnostic ignored "-Wint-to-pointer-cast"
  1302. m_Thumb.push_back(thumb[i] << 3);
  1303. m_Thumb.push_back(thumb[i] >> 5 << 2);
  1304. m_Thumb.push_back(thumb[i] >> 11 << 3);
  1305. #pragma GCC diagnostic pop
  1306. }
  1307. FREE (thumb);
  1308. }
  1309. void CLASS rollei_load_raw()
  1310. {
  1311. uint8_t pixel[10];
  1312. unsigned iten=0, isix, i, buffer=0, todo[16];
  1313. isix = m_RawWidth * m_RawHeight * 5 / 8;
  1314. while (fread (pixel, 1, 10, m_InputFile) == 10) {
  1315. for (i=0; i < 10; i+=2) {
  1316. todo[i] = iten++;
  1317. todo[i+1] = pixel[i] << 8 | pixel[i+1];
  1318. buffer = pixel[i] >> 2 | buffer << 6;
  1319. }
  1320. for ( ; i < 16; i+=2) {
  1321. todo[i] = isix++;
  1322. todo[i+1] = buffer >> (14-i)*5;
  1323. }
  1324. for (i=0; i < 16; i+=2)
  1325. m_Raw_Image[todo[i]] = (todo[i+1] & 0x3ff);
  1326. }
  1327. m_WhiteLevel = 0x3ff;
  1328. }
  1329. int CLASS raw (unsigned row, unsigned col)
  1330. {
  1331. return (row < m_RawHeight && col < m_RawWidth) ? RAW(row,col) : 0;
  1332. }
  1333. void CLASS phase_one_flat_field (int is_float, int nc)
  1334. {
  1335. uint16_t head[8];
  1336. unsigned wide, y, x, c, rend, cend, row, col;
  1337. float *mrow, num, mult[4];
  1338. read_shorts (head, 8);
  1339. wide = head[2] / head[4];
  1340. mrow = (float *) CALLOC (nc*wide, sizeof *mrow);
  1341. merror (mrow, "phase_one_flat_field()");
  1342. for (y=0; y < (unsigned) (head[3] / head[5]); y++) {
  1343. for (x=0; x < wide; x++)
  1344. for (c=0; c < (unsigned) nc; c+=2) {
  1345. num = is_float ? getreal(11) : get2()/32768.0;
  1346. if (y==0) mrow[c*wide+x] = num;
  1347. else mrow[(c+1)*wide+x] = (num - mrow[c*wide+x]) / head[5];
  1348. }
  1349. if (y==0) continue;
  1350. rend = head[1] + y*head[5];
  1351. for (row = rend-head[5]; row < m_RawHeight && row < rend; row++) {
  1352. for (x=1; x < wide; x++) {
  1353. for (c=0; c < (unsigned) nc; c+=2) {
  1354. mult[c] = mrow[c*wide+x-1];
  1355. mult[c+1] = (mrow[c*wide+x] - mult[c]) / head[4];
  1356. }
  1357. cend = head[0] + x*head[4];
  1358. for (col = cend-head[4]; col < m_RawWidth && col < cend; col++) {
  1359. c = nc > 2 ? FC(row-m_TopMargin,col-m_LeftMargin) : 0;
  1360. if (!(c & 1)) {
  1361. c = unsigned(RAW(row,col) * mult[c]);
  1362. RAW(row,col) = MIN((int32_t)c,65535);
  1363. }
  1364. for (c=0; c < (unsigned) nc; c+=2)
  1365. mult[c] += mult[c+1];
  1366. }
  1367. }
  1368. for (x=0; x < wide; x++)
  1369. for (c=0; c < (unsigned) nc; c+=2)
  1370. mrow[c*wide+x] += mrow[(c+1)*wide+x];
  1371. }
  1372. }
  1373. FREE (mrow);
  1374. }
  1375. void CLASS phase_one_correct()
  1376. {
  1377. unsigned entries, tag, data, save, col, row, type;
  1378. int len, i, j, k, cip, val[4], dev[4], sum, max;
  1379. int head[9], diff, mindiff=INT_MAX, off_412=0;
  1380. static const signed char dir[12][2] =
  1381. { {-1,-1}, {-1,1}, {1,-1}, {1,1}, {-2,0}, {0,-2}, {0,2}, {2,0},
  1382. {-2,-2}, {-2,2}, {2,-2}, {2,2} };
  1383. float poly[8], num, cfrac, frac, mult[2], *yval[2];
  1384. uint16_t *xval[2];
  1385. if (m_UserSetting_HalfSize || !m_MetaLength) return;
  1386. TRACEKEYVALS("Phase One correction","%s","");
  1387. fseek (m_InputFile, meta_offset, SEEK_SET);
  1388. m_ByteOrder = get2();
  1389. fseek (m_InputFile, 6, SEEK_CUR);
  1390. fseek (m_InputFile, meta_offset+get4(), SEEK_SET);
  1391. entries = get4(); get4();
  1392. while (entries--) {
  1393. tag = get4();
  1394. len = get4();
  1395. data = get4();
  1396. save = ftell(m_InputFile);
  1397. fseek (m_InputFile, meta_offset+data, SEEK_SET);
  1398. if (tag == 0x419) { /* Polynomial curve */
  1399. for (get4(), i=0; i < 8; i++)
  1400. poly[i] = getreal(11);

Large files files are truncated, but you can click here to view the full file