Coverage Report

Created: 2025-11-11 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libraw/src/decoders/smal.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
0
#define HOLE(row) ((holes >> (((row)-raw_height) & 7)) & 1)
22
23
/* Kudos to Rich Taylor for figuring out SMaL's compression algorithm. */
24
void LibRaw::smal_decode_segment(unsigned seg[2][2], int holes)
25
0
{
26
0
  uchar hist[3][13] = {{7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0},
27
0
                       {7, 7, 0, 0, 63, 55, 47, 39, 31, 23, 15, 7, 0},
28
0
                       {3, 3, 0, 0, 63, 47, 31, 15, 0}};
29
0
  int low, high = 0xff, carry = 0, nbits = 8;
30
0
  int s, count, bin, next, i, sym[3];
31
0
  unsigned pix;
32
0
  uchar diff, pred[] = {0, 0};
33
0
  ushort data = 0, range = 0;
34
35
0
  fseek(ifp, seg[0][1] + 1, SEEK_SET);
36
0
  getbits(-1);
37
0
  if (seg[1][0] > unsigned(raw_width * raw_height))
38
0
    seg[1][0] = raw_width * raw_height;
39
0
  for (pix = seg[0][0]; pix < seg[1][0]; pix++)
40
0
  {
41
0
    for (s = 0; s < 3; s++)
42
0
    {
43
0
      data = data << nbits | getbits(nbits);
44
0
      if (carry < 0)
45
0
        carry = (nbits += carry + 1) < 1 ? nbits - 1 : 0;
46
0
      while (--nbits >= 0)
47
0
        if ((data >> nbits & 0xff) == 0xff)
48
0
          break;
49
0
      if (nbits > 0)
50
0
        data =
51
0
            ((data & ((1 << (nbits - 1)) - 1)) << 1) |
52
0
            ((data + (((data & (1 << (nbits - 1)))) << 1)) & ((~0u) << nbits));
53
0
      if (nbits >= 0)
54
0
      {
55
0
        data += getbits(1);
56
0
        carry = nbits - 8;
57
0
      }
58
0
      count = ((((data - range + 1) & 0xffff) << 2) - 1) / (high >> 4);
59
0
      for (bin = 0; hist[s][bin + 5] > count; bin++)
60
0
        ;
61
0
      low = hist[s][bin + 5] * (high >> 4) >> 2;
62
0
      if (bin)
63
0
        high = hist[s][bin + 4] * (high >> 4) >> 2;
64
0
      high -= low;
65
0
      for (nbits = 0; high << nbits < 128; nbits++)
66
0
        ;
67
0
      range = (range + low) << nbits;
68
0
      high <<= nbits;
69
0
      next = hist[s][1];
70
0
      if (++hist[s][2] > hist[s][3])
71
0
      {
72
0
        next = (next + 1) & hist[s][0];
73
0
        hist[s][3] = (hist[s][next + 4] - hist[s][next + 5]) >> 2;
74
0
        hist[s][2] = 1;
75
0
      }
76
0
      if (hist[s][hist[s][1] + 4] - hist[s][hist[s][1] + 5] > 1)
77
0
      {
78
0
        if (bin < hist[s][1])
79
0
          for (i = bin; i < hist[s][1]; i++)
80
0
            hist[s][i + 5]--;
81
0
        else if (next <= bin)
82
0
          for (i = hist[s][1]; i < bin; i++)
83
0
            hist[s][i + 5]++;
84
0
      }
85
0
      hist[s][1] = next;
86
0
      sym[s] = bin;
87
0
    }
88
0
    diff = sym[2] << 5 | sym[1] << 2 | (sym[0] & 3);
89
0
    if (sym[0] & 4)
90
0
      diff = diff ? -diff : 0x80;
91
0
    if (ftell(ifp) + 12 >= seg[1][1])
92
0
      diff = 0;
93
0
    if (pix >= unsigned(raw_width * raw_height))
94
0
      throw LIBRAW_EXCEPTION_IO_CORRUPT;
95
0
    raw_image[pix] = pred[pix & 1] += diff;
96
0
    if (!(pix & 1) && HOLE(pix / raw_width))
97
0
      pix += 2;
98
0
  }
99
0
  maximum = 0xff;
100
0
}
101
102
void LibRaw::smal_v6_load_raw()
103
0
{
104
0
  unsigned seg[2][2];
105
106
0
  fseek(ifp, 16, SEEK_SET);
107
0
  seg[0][0] = 0;
108
0
  seg[0][1] = get2();
109
0
  seg[1][0] = raw_width * raw_height;
110
0
  seg[1][1] = INT_MAX;
111
0
  smal_decode_segment(seg, 0);
112
0
}
113
114
int LibRaw::median4(int *p)
115
0
{
116
0
  int min, max, sum, i;
117
118
0
  min = max = sum = p[0];
119
0
  for (i = 1; i < 4; i++)
120
0
  {
121
0
    sum += p[i];
122
0
    if (min > p[i])
123
0
      min = p[i];
124
0
    if (max < p[i])
125
0
      max = p[i];
126
0
  }
127
0
  return (sum - min - max) >> 1;
128
0
}
129
130
void LibRaw::fill_holes(int holes)
131
0
{
132
0
  int row, col, val[4];
133
134
0
  for (row = 2; row < height - 2; row++)
135
0
  {
136
0
    if (!HOLE(row))
137
0
      continue;
138
0
    for (col = 1; col < width - 1; col += 4)
139
0
    {
140
0
      val[0] = RAW(row - 1, col - 1);
141
0
      val[1] = RAW(row - 1, col + 1);
142
0
      val[2] = RAW(row + 1, col - 1);
143
0
      val[3] = RAW(row + 1, col + 1);
144
0
      RAW(row, col) = median4(val);
145
0
    }
146
0
    for (col = 2; col < width - 2; col += 4)
147
0
      if (HOLE(row - 2) || HOLE(row + 2))
148
0
        RAW(row, col) = (RAW(row, col - 2) + RAW(row, col + 2)) >> 1;
149
0
      else
150
0
      {
151
0
        val[0] = RAW(row, col - 2);
152
0
        val[1] = RAW(row, col + 2);
153
0
        val[2] = RAW(row - 2, col);
154
0
        val[3] = RAW(row + 2, col);
155
0
        RAW(row, col) = median4(val);
156
0
      }
157
0
  }
158
0
}
159
160
void LibRaw::smal_v9_load_raw()
161
0
{
162
0
  unsigned seg[256][2], offset, nseg, holes, i;
163
164
0
  fseek(ifp, 67, SEEK_SET);
165
0
  offset = get4();
166
0
  nseg = (uchar)fgetc(ifp);
167
0
  fseek(ifp, offset, SEEK_SET);
168
0
  for (i = 0; i < nseg * 2; i++)
169
0
    ((unsigned *)seg)[i] = get4() + unsigned(data_offset & 0xffffffff) * (i & 1);
170
0
  fseek(ifp, 78, SEEK_SET);
171
0
  holes = fgetc(ifp);
172
0
  fseek(ifp, 88, SEEK_SET);
173
0
  seg[nseg][0] = raw_height * raw_width;
174
0
  seg[nseg][1] = get4() + unsigned(data_offset & 0xffffffff);
175
0
  for (i = 0; i < nseg; i++)
176
0
    smal_decode_segment(seg + i, holes);
177
0
  if (holes)
178
0
    fill_holes(holes);
179
0
}
180
181
#undef HOLE