Coverage Report

Created: 2026-10-14 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/LibRaw/src/decoders/load_mfbacks.cpp
Line
Count
Source
1
/* -*- C++ -*-
2
 * Copyright 2019-2025 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
inline uint32_t abs32(int32_t x)
22
1.91M
{
23
  // Branchless version.
24
1.91M
  uint32_t sm = x >> 31;
25
1.91M
  return (uint32_t) ((x + sm) ^ sm);
26
1.91M
}
27
28
inline uint32_t min32(uint32_t x, uint32_t y)
29
364k
{
30
364k
  return x < y ? x : y;
31
364k
}
32
33
inline uint32_t max32(uint32_t x, uint32_t y)
34
45.5k
{
35
45.5k
  return x > y ? x : y;
36
45.5k
}
37
38
inline uint32_t constain32(uint32_t x, uint32_t l, uint32_t u)
39
45.5k
{
40
45.5k
  return x < l ? l : (x > u ? u : x);
41
45.5k
}
42
43
int unsigned_cmp(const void *a, const void *b)
44
6.17k
{
45
6.17k
  if (!a || !b)
46
0
    return 0;
47
48
6.17k
  return *(unsigned *)a > *(unsigned *)b ? 1 : (*(unsigned *)a < *(unsigned *)b ? -1 : 0);
49
6.17k
}
50
51
int LibRaw::p1rawc(unsigned row, unsigned col, unsigned& count)
52
283k
{
53
283k
  return (row < raw_height && col < raw_width) ? (++count, RAW(row, col)) : 0;
54
283k
}
55
56
int LibRaw::p1raw(unsigned row, unsigned col)
57
4.64M
{
58
4.64M
  return (row < raw_height && col < raw_width) ? RAW(row, col) : 0;
59
4.64M
}
60
61
62
// DNG SDK version of fixing pixels in bad column using averages sets
63
// corrected not to use pixels in the same column
64
void LibRaw::phase_one_fix_col_pixel_avg(unsigned row, unsigned col)
65
70.2k
{
66
70.2k
  static const int8_t dir[3][8][2] = {
67
70.2k
  { {-2,-2}, {-2, 2}, {2,-2}, {2, 2}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} },
68
70.2k
  { {-2,-4}, {-4,-2}, {2,-4}, {4,-2}, {-2, 4}, {-4, 2}, {2, 4}, {4, 2} },
69
70.2k
  { {-4,-4}, {-4, 4}, {4,-4}, {4, 4}, { 0, 0}, { 0, 0}, {0, 0}, {0, 0} } };
70
71
70.2k
  for (int set=0; set < 3; ++set)
72
70.2k
  {
73
70.2k
    uint32_t total = 0;
74
70.2k
    uint32_t count = 0;
75
351k
    for (int i = 0; i < 8; ++i)
76
351k
    {
77
351k
      if (!dir[set][i][0] && !dir[set][i][1])
78
70.2k
        break;
79
80
280k
      total += p1rawc(row+dir[set][i][0], col+dir[set][i][1], count);
81
280k
    }
82
83
70.2k
    if (count)
84
70.2k
    {
85
70.2k
      RAW(row,col) = (uint16_t)((total + (count >> 1)) / count);
86
70.2k
      break;
87
70.2k
    }
88
70.2k
  }
89
70.2k
}
90
91
// DNG SDK version of fixing pixels in bad column using gradient prediction
92
void LibRaw::phase_one_fix_pixel_grad(unsigned row, unsigned col)
93
45.5k
{
94
45.5k
  static const int8_t grad_sets[7][12][2] = {
95
45.5k
    { {-4,-2}, { 4, 2}, {-3,-1}, { 1, 1}, {-1,-1}, { 3, 1}, 
96
45.5k
      {-4,-1}, { 0, 1}, {-2,-1}, { 2, 1}, { 0,-1}, { 4, 1} },
97
45.5k
    { {-2,-2}, { 2, 2}, {-3,-1}, {-1, 1}, {-1,-1}, { 1, 1}, 
98
45.5k
      { 1,-1}, { 3, 1}, {-2,-1}, { 0, 1}, { 0,-1}, { 2, 1} },
99
45.5k
    { {-2,-4}, { 2, 4}, {-1,-3}, { 1, 1}, {-1,-1}, { 1, 3}, 
100
45.5k
      {-2,-1}, { 0, 3}, {-1,-2}, { 1, 2}, { 0,-3}, { 2, 1} },
101
45.5k
    { { 0,-2}, { 0, 2}, {-1,-1}, {-1, 1}, { 1,-1}, { 1, 1}, 
102
45.5k
      {-1,-2}, {-1, 2}, { 0,-1}, { 0,-1}, { 1,-2}, { 1, 2} },
103
45.5k
    { {-2, 4}, { 2,-4}, {-1, 3}, { 1,-1}, {-1, 1}, { 1,-3}, 
104
45.5k
      {-2, 1}, { 0,-3}, {-1, 2}, { 1,-2}, { 0, 3}, { 2,-1} },
105
45.5k
    { {-2, 2}, { 2,-2}, {-3, 1}, {-1,-1}, {-1, 1}, { 1,-1}, 
106
45.5k
      { 1, 1}, { 3,-1}, {-2, 1}, { 0,-1}, { 0, 1}, { 2,-1} },
107
45.5k
    { {-4, 2}, { 4,-2}, {-3, 1}, { 1,-1}, {-1, 1}, { 3,-1}, 
108
45.5k
      {-4, 1}, { 0,-1}, {-2, 1}, { 2,-1}, { 0, 1}, { 4,-1} } };
109
110
45.5k
  uint32_t est[7], grad[7];
111
45.5k
  uint32_t lower = min32(p1raw(row,col-2), p1raw(row, col+2));
112
45.5k
  uint32_t upper = max32(p1raw(row,col-2), p1raw(row, col+2));
113
45.5k
  uint32_t minGrad = 0xFFFFFFFF;
114
364k
  for (int i = 0; i<7; ++i)
115
318k
  {
116
318k
    est[i] = p1raw(row+grad_sets[i][0][0], col+grad_sets[i][0][1]) +
117
318k
             p1raw(row+grad_sets[i][1][0], col+grad_sets[i][1][1]);
118
318k
    grad[i] = 0;
119
2.22M
    for (int j=0; j<12; j+=2)
120
1.91M
      grad[i] += abs32(p1raw(row+grad_sets[i][j][0], col+grad_sets[i][j][1]) -
121
1.91M
                       p1raw(row+grad_sets[i][j+1][0], col+grad_sets[i][j+1][1]));
122
318k
    minGrad = min32(minGrad, grad[i]);
123
318k
  }
124
125
45.5k
  uint32_t limit = (minGrad * 3) >> 1;
126
45.5k
  uint32_t total = 0;
127
45.5k
  uint32_t count = 0;
128
364k
  for (int i = 0; i<7; ++i)
129
318k
    if (grad[i] <= limit)
130
145k
    {
131
145k
      total += est[i];
132
145k
      count += 2;
133
145k
    }
134
45.5k
  RAW(row, col) = constain32((total + (count >> 1)) / count, lower, upper);
135
45.5k
}
136
137
void LibRaw::phase_one_flat_field(int is_float, int nc)
138
4.05k
{
139
4.05k
  ushort head[8];
140
4.05k
  unsigned wide, high, y, x, c, rend, cend, row, col;
141
4.05k
  float *mrow, num, mult[4];
142
143
4.05k
  read_shorts(head, 8);
144
4.05k
  if (head[2] == 0 || head[3] == 0 || head[4] == 0 || head[5] == 0)
145
3.46k
    return;
146
596
  wide = head[2] / head[4] + (head[2] % head[4] != 0);
147
596
  high = head[3] / head[5] + (head[3] % head[5] != 0);
148
596
  mrow = (float *)calloc(nc * wide, sizeof *mrow);
149
130k
  for (y = 0; y < high; y++)
150
130k
  {
151
130k
    checkCancel();
152
25.6M
    for (x = 0; x < wide; x++)
153
51.0M
      for (c = 0; c < (unsigned)nc; c += 2)
154
25.5M
      {
155
25.5M
        num = is_float ? getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT) : float(get2()) / 32768.f;
156
25.5M
        if (y == 0)
157
199k
          mrow[c * wide + x] = num;
158
25.3M
        else
159
25.3M
          mrow[(c + 1) * wide + x] = (num - mrow[c * wide + x]) / head[5];
160
25.5M
      }
161
130k
    if (y == 0)
162
583
      continue;
163
129k
    rend = head[1] + y * head[5];
164
129k
    for (row = rend - head[5];
165
135k
         row < raw_height && row < rend && row < unsigned(head[1] + head[3] - head[5]);
166
129k
         row++)
167
5.61k
    {
168
2.16M
      for (x = 1; x < wide; x++)
169
2.15M
      {
170
4.31M
        for (c = 0; c < (unsigned)nc; c += 2)
171
2.15M
        {
172
2.15M
          mult[c] = mrow[c * wide + x - 1];
173
2.15M
          mult[c + 1] = (mrow[c * wide + x] - mult[c]) / head[4];
174
2.15M
        }
175
2.15M
        cend = head[0] + x * head[4];
176
2.15M
        for (col = cend - head[4];
177
2.52M
             col < raw_width && col < cend && col < unsigned(head[0] + head[2] - head[4]);
178
2.15M
             col++)
179
369k
        {
180
369k
          c = nc > 2 ? FC(row - top_margin, col - left_margin) : 0;
181
369k
          if (!(c & 1))
182
355k
          {
183
355k
            c = unsigned(RAW(row, col) * mult[c]);
184
355k
            RAW(row, col) = LIM(c, 0, 65535);
185
355k
          }
186
768k
          for (c = 0; c < (unsigned)nc; c += 2)
187
398k
            mult[c] += mult[c + 1];
188
369k
        }
189
2.15M
      }
190
2.16M
      for (x = 0; x < wide; x++)
191
4.32M
        for (c = 0; c < (unsigned)nc; c += 2)
192
2.16M
          mrow[c * wide + x] += mrow[(c + 1) * wide + x];
193
5.61k
    }
194
129k
  }
195
596
  free(mrow);
196
596
}
197
198
int LibRaw::phase_one_correct()
199
536
{
200
536
  unsigned entries, tag, data, col, row, type;
201
536
  INT64 save;
202
536
  int len, i, j, k, cip, sum;
203
#if 0
204
  int val[4], dev[4], max;
205
#endif
206
536
  int head[9], diff, mindiff = INT_MAX;
207
536
  INT64 off_412 = 0;
208
536
  /* static */ const signed char dir[12][2] = {
209
536
      {-1, -1}, {-1, 1}, {1, -1},  {1, 1},  {-2, 0}, {0, -2},
210
536
      {0, 2},   {2, 0},  {-2, -2}, {-2, 2}, {2, -2}, {2, 2}};
211
536
  float poly[8], num, cfrac, frac, mult[2], *yval[2] = {NULL, NULL};
212
536
  ushort *xval[2];
213
536
  int qmult_applied = 0, qlin_applied = 0;
214
536
  std::vector<unsigned> badCols;
215
216
536
  if (!meta_length)
217
151
    return 0;
218
385
  fseek(ifp, meta_offset, SEEK_SET);
219
385
  order = get2();
220
385
  fseek(ifp, 6, SEEK_CUR);
221
385
  fseek(ifp, meta_offset + get4(), SEEK_SET);
222
385
  entries = get4();
223
385
  get4();
224
385
  INT64 fsize = ifp->size();
225
226
385
  try
227
385
  {
228
52.2M
    while (entries--)
229
52.2M
    {
230
52.2M
      checkCancel();
231
52.2M
      tag = get4();
232
52.2M
      len = get4();
233
52.2M
      data = get4();
234
52.2M
      save = ftell(ifp);
235
52.2M
      fseek(ifp, meta_offset + data, SEEK_SET);
236
52.2M
#if 1
237
52.2M
    if (ifp->eof())
238
51.2M
    {
239
      // skip bad or unknown tag
240
51.2M
      fseek(ifp, save, SEEK_SET);
241
51.2M
      continue;
242
51.2M
    }
243
1.00M
#endif
244
1.00M
      INT64 savepos = ftell(ifp);
245
1.00M
    if (len < 0 || (len > 8 && savepos + (INT64)len > 2 * fsize))
246
741k
    {
247
741k
        fseek(ifp, save, SEEK_SET); // Recover tiff-read position!!
248
741k
        continue;
249
741k
    }
250
263k
      if (tag == 0x0400)
251
1.39k
      { /* Sensor defects */
252
10.3M
        while ((len -= 8) >= 0)
253
10.3M
        {
254
10.3M
          col = get2();
255
10.3M
          row = get2();
256
10.3M
          type = get2();
257
10.3M
          get2();
258
10.3M
          if (col >= raw_width)
259
8.15M
            continue;
260
2.20M
          if (type == 131 || type == 137) /* Bad column */
261
#if 0
262
            // Original code by Dave Coffin - it works better by
263
            // not employing special logic for G1 channel below.
264
            // Alternatively this column remap (including G1 channel
265
            // logic) should be called prior to black subtraction
266
            // unlike other corrections
267
            for (row = 0; row < raw_height; row++)
268
            {
269
              if (FC(row - top_margin, col - left_margin)==1)
270
              {
271
                for (sum = i = 0; i < 4; i++)
272
                  sum += val[i] = p1raw(row + dir[i][0], col + dir[i][1]);
273
                for (max = i = 0; i < 4; i++)
274
                {
275
                  dev[i] = abs((val[i] << 2) - sum);
276
                  if (dev[max] < dev[i])
277
                    max = i;
278
                }
279
                RAW(row, col) = (sum - val[max]) / 3.0 + 0.5;
280
              }
281
              else
282
              {
283
                for (sum = 0, i = 8; i < 12; i++)
284
                  sum += p1raw(row + dir[i][0], col + dir[i][1]);
285
                RAW(row, col) =
286
                  0.5 + sum * 0.0732233 +
287
                  (p1raw(row, col - 2) + p1raw(row, col + 2)) * 0.3535534;
288
              }
289
            }
290
#else
291
            // accumulae bad columns to be sorted later
292
1.61k
            badCols.push_back(col);
293
2.20M
#endif
294
2.20M
          else if (type == 129)
295
414
          { /* Bad pixel */
296
414
            if (row >= raw_height)
297
26
              continue;
298
388
            j = (FC(row - top_margin, col - left_margin) != 1) * 4;
299
388
            unsigned count = 0;
300
3.49k
            for (sum = 0, i = j; i < j + 8; i++)
301
3.10k
              sum += p1rawc(row + dir[i][0], col + dir[i][1], count);
302
388
            if (count)
303
388
              RAW(row, col) = (sum + (count >> 1)) / count;
304
388
          }
305
2.20M
        }
306
1.39k
      }
307
262k
      else if (tag == 0x0419)
308
111
      { /* Polynomial curve - output calibraion */
309
999
        for (get4(), i = 0; i < 8; i++)
310
888
          poly[i] = getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
311
111
        poly[3] += (ph1.tag_210 - poly[7]) * poly[6] + 1;
312
7.27M
        for (i = 0; i < 0x10000; i++)
313
7.27M
        {
314
7.27M
          num = (poly[5] * i + poly[3]) * i + poly[1];
315
7.27M
          curve[i] = ushort(LIM(num, 0, 65535));
316
7.27M
        }
317
111
        goto apply; /* apply to right half */
318
111
      }
319
261k
      else if (tag == 0x041a)
320
31
      { /* Polynomial curve */
321
155
        for (i = 0; i < 4; i++)
322
124
          poly[i] = getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
323
2.03M
        for (i = 0; i < 0x10000; i++)
324
2.03M
        {
325
10.1M
          for (num = 0, j = 4; j--;)
326
8.12M
            num = num * i + poly[j];
327
2.03M
          curve[i] = ushort(LIM(num + i, 0, 65535));
328
2.03M
        }
329
142
      apply: /* apply to whole image */
330
4.41k
        for (row = 0; row < raw_height; row++)
331
4.27k
        {
332
4.27k
          checkCancel();
333
769k
          for (col = (tag & 1) * ph1.split_col; col < raw_width; col++)
334
765k
            RAW(row, col) = curve[RAW(row, col)];
335
4.27k
        }
336
142
      }
337
261k
      else if (tag == 0x0401)
338
1.86k
      { /* All-color flat fields - luma calibration*/
339
1.86k
        phase_one_flat_field(1, 2);
340
1.86k
      }
341
260k
      else if (tag == 0x0416 || tag == 0x0410)
342
2.16k
      {
343
        // 0x410 - luma calibration
344
2.16k
        phase_one_flat_field(0, 2);
345
2.16k
      }
346
257k
      else if (tag == 0x040b)
347
26
      { /* Red+blue flat field - croma calibration */
348
26
        phase_one_flat_field(0, 4);
349
26
      }
350
257k
      else if (tag == 0x0412)
351
2.98k
      {
352
2.98k
        fseek(ifp, 36, SEEK_CUR);
353
2.98k
        diff = abs(get2() - ph1.tag_21a);
354
2.98k
        if (mindiff > diff)
355
474
        {
356
474
          mindiff = diff;
357
474
          off_412 = ftell(ifp) - 38;
358
474
        }
359
2.98k
      }
360
254k
      else if (tag == 0x041f && !qlin_applied && ph1.split_col > 0 && ph1.split_col < raw_width
361
8
    && ph1.split_row > 0 && ph1.split_row < raw_height)
362
0
      { /* Quadrant linearization */
363
0
        ushort lc[2][2][16], ref[16];
364
0
        int qr, qc;
365
0
    bool baddiv = false;
366
0
        for (qr = 0; qr < 2; qr++)
367
0
      for (qc = 0; qc < 2; qc++)
368
0
      {
369
0
        for (i = 0; i < 16; i++)
370
0
          lc[qr][qc][i] = get4();
371
0
        if (lc[qr][qc][15] == 0)
372
0
          baddiv = true;
373
0
      }
374
0
    if(baddiv)
375
0
      continue;
376
0
        for (i = 0; i < 16; i++)
377
0
        {
378
0
          int v = 0;
379
0
          for (qr = 0; qr < 2; qr++)
380
0
            for (qc = 0; qc < 2; qc++)
381
0
              v += lc[qr][qc][i];
382
0
          ref[i] = (v + 2) >> 2;
383
0
        }
384
0
        for (qr = 0; qr < 2; qr++)
385
0
        {
386
0
          for (qc = 0; qc < 2; qc++)
387
0
          {
388
0
            int cx[19], cf[19];
389
0
            for (i = 0; i < 16; i++)
390
0
            {
391
0
              cx[1 + i] = lc[qr][qc][i];
392
0
              cf[1 + i] = ref[i];
393
0
            }
394
0
            cx[0] = cf[0] = 0;
395
0
            cx[17] = cf[17] = ((unsigned int)ref[15] * 65535) / lc[qr][qc][15];
396
0
            cf[18] = cx[18] = 65535;
397
0
            cubic_spline(cx, cf, 19);
398
399
0
            for (row = (qr ? ph1.split_row : 0);
400
0
                 row < unsigned(qr ? raw_height : ph1.split_row); row++)
401
0
            {
402
0
              checkCancel();
403
0
              for (col = (qc ? ph1.split_col : 0);
404
0
                   col < unsigned(qc ? raw_width : ph1.split_col); col++)
405
0
                RAW(row, col) = curve[RAW(row, col)];
406
0
            }
407
0
          }
408
0
        }
409
0
        qlin_applied = 1;
410
0
      }
411
254k
      else if (tag == 0x041e && !qmult_applied)
412
50
      { /* Quadrant multipliers - output calibraion */
413
50
        float qmult[2][2] = {{1, 1}, {1, 1}};
414
50
        get4();
415
50
        get4();
416
50
        get4();
417
50
        get4();
418
50
        qmult[0][0] = 1.0f + getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
419
50
        get4();
420
50
        get4();
421
50
        get4();
422
50
        get4();
423
50
        get4();
424
50
        qmult[0][1] = 1.0f + getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
425
50
        get4();
426
50
        get4();
427
50
        get4();
428
50
        qmult[1][0] = 1.0f + getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
429
50
        get4();
430
50
        get4();
431
50
        get4();
432
50
        qmult[1][1] = 1.0f + getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
433
3.33k
        for (row = 0; row < raw_height; row++)
434
3.28k
        {
435
3.28k
          checkCancel();
436
3.40M
          for (col = 0; col < raw_width; col++)
437
3.39M
          {
438
3.39M
            i = int(qmult[row >= (unsigned)ph1.split_row][col >= (unsigned)ph1.split_col] *
439
3.39M
                RAW(row, col));
440
3.39M
            RAW(row, col) = LIM(i, 0, 65535);
441
3.39M
          }
442
3.28k
        }
443
50
        qmult_applied = 1;
444
50
      }
445
254k
      else if (tag == 0x0431 && !qmult_applied && ph1.split_col > 0 && ph1.split_col < raw_width 
446
6
    && ph1.split_row > 0 && ph1.split_row < raw_height)
447
0
      { /* Quadrant combined - four tile gain calibration */
448
0
        ushort lc[2][2][7], ref[7];
449
0
        int qr, qc;
450
0
        for (i = 0; i < 7; i++)
451
0
          ref[i] = get4();
452
0
        for (qr = 0; qr < 2; qr++)
453
0
          for (qc = 0; qc < 2; qc++)
454
0
            for (i = 0; i < 7; i++)
455
0
              lc[qr][qc][i] = get4();
456
0
        for (qr = 0; qr < 2; qr++)
457
0
        {
458
0
          for (qc = 0; qc < 2; qc++)
459
0
          {
460
0
            int cx[9], cf[9];
461
0
            for (i = 0; i < 7; i++)
462
0
            {
463
0
              cx[1 + i] = ref[i];
464
0
              cf[1 + i] = ((unsigned)ref[i] * lc[qr][qc][i]) / 10000;
465
0
            }
466
0
            cx[0] = cf[0] = 0;
467
0
            cx[8] = cf[8] = 65535;
468
0
            cubic_spline(cx, cf, 9);
469
0
            for (row = (qr ? ph1.split_row : 0);
470
0
                 row < unsigned(qr ? raw_height : ph1.split_row); row++)
471
0
            {
472
0
              checkCancel();
473
0
              for (col = (qc ? ph1.split_col : 0);
474
0
                   col < unsigned(qc ? raw_width : ph1.split_col); col++)
475
0
                RAW(row, col) = curve[RAW(row, col)];
476
0
            }
477
0
          }
478
0
        }
479
0
        qmult_applied = 1;
480
0
        qlin_applied = 1;
481
0
      }
482
263k
      fseek(ifp, save, SEEK_SET);
483
263k
    }
484
385
    if (!badCols.empty())
485
152
    {
486
152
      qsort(badCols.data(), badCols.size(), sizeof(unsigned), unsigned_cmp);
487
152
      bool prevIsolated = true;
488
1.76k
      for (i = 0; i < (int)badCols.size(); ++i)
489
1.61k
      {
490
1.61k
        bool nextIsolated = i == ((int)(badCols.size()-1)) || badCols[i+1]>badCols[i]+4;
491
117k
        for (row = 0; row < raw_height; ++row)
492
115k
          if (prevIsolated && nextIsolated)
493
45.5k
            phase_one_fix_pixel_grad(row,badCols[i]);
494
70.2k
          else
495
70.2k
            phase_one_fix_col_pixel_avg(row,badCols[i]);
496
1.61k
        prevIsolated = nextIsolated;
497
1.61k
      }
498
152
    }
499
385
    if (off_412)
500
221
    {
501
221
      fseek(ifp, off_412, SEEK_SET);
502
2.21k
      for (i = 0; i < 9; i++)
503
1.98k
        head[i] = get4() & 0x7fff;
504
221
    unsigned w0 = head[1] * head[3], w1 = head[2] * head[4];
505
221
    if (w0 > 10240000 || w1 > 10240000)
506
135
      throw LIBRAW_EXCEPTION_ALLOC;
507
86
    if (w0 < 1 || w1 < 1)
508
11
      throw LIBRAW_EXCEPTION_IO_CORRUPT;
509
75
      yval[0] = (float *)calloc(head[1] * head[3] + head[2] * head[4], 6);
510
75
      yval[1] = (float *)(yval[0] + head[1] * head[3]);
511
75
      xval[0] = (ushort *)(yval[1] + head[2] * head[4]);
512
75
      xval[1] = (ushort *)(xval[0] + head[1] * head[3]);
513
75
      get2();
514
225
      for (i = 0; i < 2; i++)
515
53.2M
        for (j = 0; j < head[i + 1] * head[i + 3]; j++)
516
53.2M
          yval[i][j] = getrealf(LIBRAW_EXIFTAG_TYPE_FLOAT);
517
225
      for (i = 0; i < 2; i++)
518
53.2M
        for (j = 0; j < head[i + 1] * head[i + 3]; j++)
519
53.2M
          xval[i][j] = get2();
520
10.9k
      for (row = 0; row < raw_height; row++)
521
10.8k
      {
522
10.8k
        checkCancel();
523
5.45M
        for (col = 0; col < raw_width; col++)
524
5.44M
        {
525
5.44M
          cfrac = (float)col * head[3] / raw_width;
526
5.44M
      cip = (int)cfrac;
527
5.44M
          cfrac -= cip;
528
5.44M
          num = RAW(row, col) * 0.5f;
529
16.2M
          for (i = cip; i < cip + 2 && i < head[3]; i++)
530
10.7M
          {
531
11.2M
            for (k = j = 0; j < head[1]; j++)
532
11.1M
              if (num < xval[0][k = head[1] * i + j])
533
10.6M
                break;
534
10.7M
      if (j == 0 || j == head[1] || k < 1 || k >= w0+w1)
535
10.7M
        frac = 0;
536
46.6k
      else
537
46.6k
      {
538
46.6k
        int xdiv = (xval[0][k] - xval[0][k - 1]);
539
46.6k
        frac = xdiv ? (xval[0][k] - num) / (xval[0][k] - xval[0][k - 1]) : 0;
540
46.6k
      }
541
10.7M
      if (k < w0 + w1)
542
10.7M
        mult[i - cip] = yval[0][k > 0 ? k - 1 : 0] * frac + yval[0][k] * (1 - frac);
543
0
      else
544
0
        mult[i - cip] = 0;
545
10.7M
          }
546
5.44M
          i = int(((mult[0] * (1.f - cfrac) + mult[1] * cfrac) * row + num) * 2.f);
547
5.44M
          RAW(row, col) = LIM(i, 0, 65535);
548
5.44M
        }
549
10.8k
      }
550
75
      free(yval[0]);
551
75
    }
552
385
  }
553
385
  catch (...)
554
385
  {
555
159
    if (yval[0])
556
0
      free(yval[0]);
557
159
    return LIBRAW_CANCELLED_BY_CALLBACK;
558
159
  }
559
226
  return 0;
560
385
}
561
562
void LibRaw::phase_one_load_raw()
563
296
{
564
296
  int a, b, i;
565
296
  ushort akey, bkey, t_mask;
566
567
296
  fseek(ifp, ph1.key_off, SEEK_SET);
568
296
  akey = get2();
569
296
  bkey = get2();
570
296
  t_mask = ph1.format == 1 ? 0x5555 : 0x1354;
571
296
  if (ph1.black_col || ph1.black_row)
572
5
  {
573
5
    imgdata.rawdata.ph1_cblack =
574
5
        (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
575
5
    imgdata.rawdata.ph1_rblack =
576
5
        (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
577
5
    if (ph1.black_col)
578
2
    {
579
2
      fseek(ifp, ph1.black_col, SEEK_SET);
580
2
      read_shorts((ushort *)imgdata.rawdata.ph1_cblack[0], raw_height * 2);
581
2
    }
582
5
    if (ph1.black_row)
583
3
    {
584
3
      fseek(ifp, ph1.black_row, SEEK_SET);
585
3
      read_shorts((ushort *)imgdata.rawdata.ph1_rblack[0], raw_width * 2);
586
3
    }
587
5
  }
588
296
  fseek(ifp, data_offset, SEEK_SET);
589
296
  read_shorts(raw_image, raw_width * raw_height);
590
296
  if (ph1.format)
591
79.0k
    for (i = 0; i < raw_width * raw_height; i += 2)
592
79.0k
    {
593
79.0k
      a = raw_image[i + 0] ^ akey;
594
79.0k
      b = raw_image[i + 1] ^ bkey;
595
79.0k
      raw_image[i + 0] = (a & t_mask) | (b & ~t_mask);
596
79.0k
      raw_image[i + 1] = (b & t_mask) | (a & ~t_mask);
597
79.0k
    }
598
296
}
599
600
unsigned LibRaw::ph1_bithuff(int nbits, ushort *huff)
601
213M
{
602
213M
#ifndef LIBRAW_NOTHREADS
603
317M
#define bitbuf tls->ph1_bits.bitbuf
604
664M
#define vbits tls->ph1_bits.vbits
605
#else
606
  static UINT64 bitbuf = 0;
607
  static int vbits = 0;
608
#endif
609
213M
  unsigned c;
610
611
213M
  if (nbits == -1)
612
614k
  {
613
614k
    bitbuf = vbits = 0;
614
614k
    return 0;
615
614k
  }
616
212M
  if (nbits == 0)
617
10.4M
    return 0;
618
202M
  if (vbits < nbits)
619
57.1M
  {
620
57.1M
    bitbuf = bitbuf << 32 | get4();
621
57.1M
    vbits += 32;
622
57.1M
  }
623
202M
  c = unsigned ((bitbuf << (64 - vbits) >> (64 - nbits)) & 0xffffffff);
624
202M
  if (huff)
625
0
  {
626
0
    vbits -= huff[c] >> 8;
627
0
    return (uchar)huff[c];
628
0
  }
629
202M
  vbits -= nbits;
630
202M
  return c;
631
202M
#ifndef LIBRAW_NOTHREADS
632
202M
#undef bitbuf
633
202M
#undef vbits
634
202M
#endif
635
202M
}
636
637
void LibRaw::phase_one_load_raw_c()
638
253
{
639
253
  static const int length[] = {8, 7, 6, 9, 11, 10, 5, 12, 14, 13};
640
253
  int *offset, len[2], pred[2], row, col, i, j;
641
253
  ushort *pixel;
642
253
  short(*c_black)[2], (*r_black)[2];
643
253
  if (ph1.format == 6)
644
0
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
645
646
253
  pixel = (ushort *)calloc(raw_width * 3 + raw_height * 4, 2);
647
253
  offset = (int *)(pixel + raw_width);
648
253
  fseek(ifp, strip_offset, SEEK_SET);
649
474k
  for (row = 0; row < raw_height; row++)
650
473k
    offset[row] = get4();
651
253
  c_black = (short(*)[2])(offset + raw_height);
652
253
  fseek(ifp, ph1.black_col, SEEK_SET);
653
253
  if (ph1.black_col)
654
12
    read_shorts((ushort *)c_black[0], raw_height * 2);
655
253
  r_black = c_black + raw_height;
656
253
  fseek(ifp, ph1.black_row, SEEK_SET);
657
253
  if (ph1.black_row)
658
34
    read_shorts((ushort *)r_black[0], raw_width * 2);
659
660
  // Copy data to internal copy (ever if not read)
661
253
  if (ph1.black_col || ph1.black_row)
662
45
  {
663
45
    imgdata.rawdata.ph1_cblack =
664
45
        (short(*)[2])calloc(raw_height * 2, sizeof(ushort));
665
45
    memmove(imgdata.rawdata.ph1_cblack, (ushort *)c_black[0],
666
45
            raw_height * 2 * sizeof(ushort));
667
45
    imgdata.rawdata.ph1_rblack =
668
45
        (short(*)[2])calloc(raw_width * 2, sizeof(ushort));
669
45
    memmove(imgdata.rawdata.ph1_rblack, (ushort *)r_black[0],
670
45
            raw_width * 2 * sizeof(ushort));
671
45
  }
672
673
64.7k
  for (i = 0; i < 256; i++)
674
64.5k
    curve[i] = ushort(float(i * i) / 3.969f + 0.5f);
675
253
  try
676
253
  {
677
472k
    for (row = 0; row < raw_height; row++)
678
472k
    {
679
472k
      checkCancel();
680
472k
      fseek(ifp, data_offset + offset[row], SEEK_SET);
681
472k
      ph1_bits(-1);
682
472k
      pred[0] = pred[1] = 0;
683
93.8M
      for (col = 0; col < raw_width; col++)
684
93.3M
      {
685
93.3M
        if (col >= (raw_width & -8))
686
573k
          len[0] = len[1] = 14;
687
92.7M
        else if ((col & 7) == 0)
688
34.7M
          for (i = 0; i < 2; i++)
689
23.1M
          {
690
49.1M
            for (j = 0; j < 5 && !ph1_bits(1); j++)
691
25.9M
              ;
692
23.1M
            if (j--)
693
7.37M
              len[i] = length[j * 2 + ph1_bits(1)];
694
23.1M
          }
695
93.3M
        if ((i = len[col & 1]) == 14)
696
54.1M
          pixel[col] = pred[col & 1] = ph1_bits(16);
697
39.2M
        else
698
39.2M
          pixel[col] = pred[col & 1] += ph1_bits(i) + 1 - (1 << (i - 1));
699
93.3M
        if (pred[col & 1] >> 16)
700
16.9M
          derror();
701
93.3M
        if (ph1.format == 5 && pixel[col] < 256)
702
98.4k
          pixel[col] = curve[pixel[col]];
703
93.3M
      }
704
472k
      if (ph1.format == 8)
705
18.0k
        memmove(&RAW(row, 0), &pixel[0], raw_width * 2);
706
454k
      else
707
91.1M
        for (col = 0; col < raw_width; col++)
708
90.6M
          RAW(row, col) = pixel[col] << 2;
709
472k
    }
710
253
  }
711
253
  catch (...)
712
253
  {
713
6
    free(pixel);
714
6
    throw;
715
6
  }
716
246
  free(pixel);
717
246
  maximum = 0xfffc - ph1.t_black;
718
246
}
719
720
void LibRaw::hasselblad_load_raw()
721
2
{
722
2
  struct jhead jh;
723
2
  int shot, row, col, *back[5]={0,0,0,0,0},
724
2
   len[2], diff[12], pred, sh, f, c;
725
2
  unsigned s;
726
2
  unsigned upix, urow, ucol;
727
2
  ushort *ip;
728
729
2
  if (!ljpeg_start(&jh, 0))
730
2
    return;
731
0
  order = 0x4949;
732
0
  ph1_bits(-1);
733
0
  try
734
0
  {
735
0
    back[4] = (int *)calloc(raw_width, 3 * sizeof **back);
736
0
    FORC3 back[c] = back[4] + c * raw_width;
737
0
    cblack[6] >>= sh = tiff_samples > 1;
738
0
    shot = LIM(shot_select, 1, tiff_samples) - 1;
739
0
    for (row = 0; row < raw_height; row++)
740
0
    {
741
0
      checkCancel();
742
0
      FORC4 back[(c + 3) & 3] = back[c];
743
0
      for (col = 0; col < raw_width; col += 2)
744
0
      {
745
0
        for (s = 0; s < tiff_samples * 2; s += 2)
746
0
        {
747
0
          FORC(2) len[c] = ph1_huff(jh.huff[0]);
748
0
          FORC(2)
749
0
          {
750
0
            diff[s + c] = ph1_bits(len[c]);
751
0
            if (len[c] > 0 && (diff[s + c] & (1 << (len[c] - 1))) == 0)
752
0
              diff[s + c] -= (1 << len[c]) - 1;
753
0
            if (diff[s + c] == 65535)
754
0
              diff[s + c] = -32768;
755
0
          }
756
0
        }
757
0
        for (s = col; s < unsigned(col + 2); s++)
758
0
        {
759
0
          pred = 0x8000 + load_flags;
760
0
          if (col)
761
0
            pred = back[2][s - 2];
762
0
          if (col && row > 1)
763
0
            switch (jh.psv)
764
0
            {
765
0
            case 11:
766
0
              pred += back[0][s] / 2 - back[0][s - 2] / 2;
767
0
              break;
768
0
            }
769
0
          f = (row & 1) * 3 ^ ((col + s) & 1);
770
0
          FORC(int(tiff_samples))
771
0
          {
772
0
            pred += diff[(s & 1) * tiff_samples + c];
773
0
            upix = pred >> sh & 0xffff;
774
0
            if (raw_image && c == shot)
775
0
              RAW(row, s) = upix;
776
0
            if (image)
777
0
            {
778
0
              urow = row - top_margin + (c & 1);
779
0
              ucol = col - left_margin - ((c >> 1) & 1);
780
0
              ip = &image[urow * width + ucol][f];
781
0
              if (urow < height && ucol < width)
782
0
                *ip = c < 4 ? upix : (*ip + upix) >> 1;
783
0
            }
784
0
          }
785
0
          back[2][s] = pred;
786
0
        }
787
0
      }
788
0
    }
789
0
  }
790
0
  catch (...)
791
0
  {
792
0
    if(back[4])
793
0
      free(back[4]);
794
0
    ljpeg_end(&jh);
795
0
    throw;
796
0
  }
797
0
  if(back[4])
798
0
    free(back[4]);
799
0
  ljpeg_end(&jh);
800
0
  if (image)
801
0
    mix_green = 1;
802
0
}
803
804
void LibRaw::leaf_hdr_load_raw()
805
110
{
806
110
  ushort *pixel = 0;
807
110
  unsigned tile = 0, r, c, row, col;
808
809
110
  if (!filters || !raw_image)
810
23
  {
811
23
    if (!image)
812
0
      throw LIBRAW_EXCEPTION_IO_CORRUPT;
813
23
    pixel = (ushort *)calloc(raw_width, sizeof *pixel);
814
23
  }
815
110
  try
816
110
  {
817
110
    FORC(tiff_samples)
818
14.0k
    for (r = 0; r < raw_height; r++)
819
14.0k
    {
820
14.0k
      checkCancel();
821
14.0k
      if (r % tile_length == 0)
822
4.68k
      {
823
4.68k
        fseek(ifp, data_offset + 4 * tile++, SEEK_SET);
824
4.68k
        fseek(ifp, get4(), SEEK_SET);
825
4.68k
      }
826
14.0k
      if (filters && c != shot_select)
827
0
        continue;
828
14.0k
      if (filters && raw_image)
829
1.10k
        pixel = raw_image + r * raw_width;
830
14.0k
      read_shorts(pixel, raw_width);
831
14.0k
      if (!filters && image && (row = r - top_margin) < height)
832
527k
        for (col = 0; col < width && col + left_margin < raw_width; col++)
833
517k
          image[row * width + col][c] = pixel[col + left_margin];
834
14.0k
    }
835
110
  }
836
110
  catch (...)
837
110
  {
838
31
    if (!filters)
839
21
      free(pixel);
840
31
    throw;
841
31
  }
842
79
  if (!filters)
843
2
  {
844
2
    maximum = 0xffff;
845
2
    raw_color = 1;
846
2
    free(pixel);
847
2
  }
848
79
}
849
850
void LibRaw::unpacked_load_raw_FujiDBP()
851
/*
852
for Fuji DBP for GX680, aka DX-2000
853
  DBP_tile_width = 688;
854
  DBP_tile_height = 3856;
855
  DBP_n_tiles = 8;
856
*/
857
8
{
858
8
  int scan_line, tile_n;
859
8
  int nTiles;
860
861
8
  nTiles = 8;
862
8
  tile_width = raw_width / nTiles;
863
864
8
  ushort *tile;
865
8
  tile = (ushort *)calloc(raw_height, tile_width * 2);
866
867
40
  for (tile_n = 0; tile_n < nTiles; tile_n++)
868
32
  {
869
32
    read_shorts(tile, tile_width * raw_height);
870
1.43k
    for (scan_line = 0; scan_line < raw_height; scan_line++)
871
1.40k
    {
872
1.40k
      memcpy(&raw_image[scan_line * raw_width + tile_n * tile_width],
873
1.40k
             &tile[scan_line * tile_width], tile_width * 2);
874
1.40k
    }
875
32
  }
876
8
  free(tile);
877
8
  fseek(ifp, -2, SEEK_CUR); // avoid EOF error
878
8
}
879
880
void LibRaw::sinar_4shot_load_raw()
881
110
{
882
110
  ushort *pixel;
883
110
  unsigned shot, row, col, r, c;
884
885
110
  if (raw_image)
886
0
  {
887
0
    shot = LIM(shot_select, 1, 4) - 1;
888
0
    fseek(ifp, data_offset + shot * 4, SEEK_SET);
889
0
    fseek(ifp, get4(), SEEK_SET);
890
0
    unpacked_load_raw();
891
0
    return;
892
0
  }
893
110
  if (!image)
894
0
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
895
110
  pixel = (ushort *)calloc(raw_width, sizeof *pixel);
896
110
  try
897
110
  {
898
454
    for (shot = 0; shot < 4; shot++)
899
344
    {
900
344
      checkCancel();
901
344
      fseek(ifp, data_offset + shot * 4, SEEK_SET);
902
344
      fseek(ifp, get4(), SEEK_SET);
903
10.6k
      for (row = 0; row < raw_height; row++)
904
10.3k
      {
905
10.3k
        read_shorts(pixel, raw_width);
906
10.3k
        if ((r = row - top_margin - (shot >> 1 & 1)) >= height)
907
1.51k
          continue;
908
306k
        for (col = 0; col < raw_width; col++)
909
298k
        {
910
298k
          if ((c = col - left_margin - (shot & 1)) >= width)
911
56.2k
            continue;
912
241k
          image[r * width + c][(row & 1) * 3 ^ (~col & 1)] = pixel[col];
913
241k
        }
914
8.80k
      }
915
344
    }
916
110
  }
917
110
  catch (...)
918
110
  {
919
35
    free(pixel);
920
35
    throw;
921
35
  }
922
75
  free(pixel);
923
75
  mix_green = 1;
924
75
}
925
926
void LibRaw::imacon_full_load_raw()
927
17
{
928
17
  if (!image)
929
1
    throw LIBRAW_EXCEPTION_IO_CORRUPT;
930
16
  int row, col;
931
932
16
  unsigned short *buf =
933
16
      (unsigned short *)malloc(width * 3 * sizeof(unsigned short));
934
935
487
  for (row = 0; row < height; row++)
936
471
  {
937
471
    checkCancel();
938
471
    read_shorts(buf, width * 3);
939
471
    unsigned short(*rowp)[4] = &image[row * width];
940
15.4k
    for (col = 0; col < width; col++)
941
14.9k
    {
942
14.9k
      rowp[col][0] = buf[col * 3];
943
14.9k
      rowp[col][1] = buf[col * 3 + 1];
944
14.9k
      rowp[col][2] = buf[col * 3 + 2];
945
14.9k
      rowp[col][3] = 0;
946
14.9k
    }
947
471
  }
948
16
  free(buf);
949
16
}