Coverage Report

Created: 2026-09-03 06:26

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libraw/src/metadata/identify_tools.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
short LibRaw::guess_byte_order(int words)
22
0
{
23
0
  uchar test[4][2];
24
0
  int t = 2, msb;
25
0
  double diff, sum[2] = {0, 0};
26
27
0
  memset(test,0,sizeof(test));
28
29
0
  fread(test[0], 2, 2, ifp);
30
0
  for (words -= 2; words--;)
31
0
  {
32
0
    fread(test[t], 2, 1, ifp);
33
0
    for (msb = 0; msb < 2; msb++)
34
0
    {
35
0
      diff = (test[t ^ 2][msb] << 8 | test[t ^ 2][!msb]) -
36
0
             (test[t][msb] << 8 | test[t][!msb]);
37
0
      sum[msb] += diff * diff;
38
0
    }
39
0
    t = (t + 1) & 3;
40
0
  }
41
0
  return sum[0] < sum[1] ? 0x4d4d : 0x4949;
42
0
}
43
44
float LibRaw::find_green(int bps, int bite, int off0, int off1)
45
76
{
46
76
  UINT64 bitbuf = 0;
47
76
  int vbits, col, i, c;
48
76
  ushort img[2][2065];
49
76
  float sum[] = {0, 0};
50
76
  if (width > 2064)
51
10
    return 0.f; // too wide
52
53
66
  FORC(2)
54
132
  {
55
132
    fseek(ifp, c ? off1 : off0, SEEK_SET);
56
22.2k
    for (vbits = col = 0; col < width; col++)
57
22.1k
    {
58
40.5k
      for (vbits -= bps; vbits < 0; vbits += bite)
59
18.3k
      {
60
18.3k
        bitbuf <<= bite;
61
59.7k
        for (i = 0; i < bite; i += 8)
62
41.3k
          bitbuf |= (unsigned)(fgetc(ifp) << i);
63
18.3k
      }
64
22.1k
      img[c][col] = ushort((bitbuf << (64 - bps - vbits) >> (64 - bps)) & 0xffff);
65
22.1k
    }
66
132
  }
67
66
  FORC(width - 1)
68
11.0k
  {
69
11.0k
    sum[c & 1] += ABS(img[0][c] - img[1][c + 1]);
70
11.0k
    sum[~c & 1] += ABS(img[1][c] - img[0][c + 1]);
71
11.0k
  }
72
66
  if (sum[0] >= 1.0 && sum[1] >= 1.0)
73
6
    return 100.f * logf(sum[0] / sum[1]);
74
60
  else
75
60
    return 0.f;
76
66
}
77
78
void LibRaw::trimSpaces(char *s)
79
4.21k
{
80
4.21k
  char *p = s;
81
4.21k
  int l = int(strlen(p));
82
4.21k
  if (l<1)
83
709
    return;
84
31.0k
  while (l > 0 && isspace(p[l - 1]))
85
27.5k
    p[--l] = 0; /* trim trailing spaces */
86
3.50k
  if (l < 1)
87
1.42k
    return; // only spaces in the input string, all wiped out;
88
4.06k
  while (*p && isspace(*p) && l > 0)
89
1.98k
    ++p, --l;   /* trim leading spaces */
90
2.08k
  if (l < 1)  // should not happen, but safety belt
91
0
  {
92
0
    *s = 0;
93
0
    return;
94
0
  }
95
2.08k
  memmove(s, p, l + 1);
96
2.08k
}
97
98
void LibRaw::remove_trailing_spaces(char *string, size_t len)
99
35.8k
{
100
35.8k
  if (len < 1)
101
0
    return; // not needed, b/c sizeof of make/model is 64
102
35.8k
  string[len - 1] = 0;
103
35.8k
  if (len < 3)
104
0
    return; // also not needed
105
35.8k
  len = strnlen(string, len - 1);
106
36.6k
  for (int i = int(len) - 1; i >= 0; i--)
107
21.9k
  {
108
21.9k
    if (isspace((unsigned char)string[i]))
109
842
      string[i] = 0;
110
21.1k
    else
111
21.1k
      break;
112
21.9k
  }
113
35.8k
}
114
115
void LibRaw::remove_caseSubstr(char *string, char *subStr) // replace a substring with an equal length of spaces
116
698
{
117
698
  char *found;
118
722
  while ((found = strcasestr(string,subStr))) {
119
24
    if (!found) return;
120
24
    int fill_len = int(strlen(subStr));
121
24
    int p = int(found - string);
122
155
    for (int i=p; i<p+fill_len; i++) {
123
131
      string[i] = 32;
124
131
    }
125
24
  }
126
698
  trimSpaces (string);
127
698
}
128
129
void LibRaw::removeExcessiveSpaces(char *string) // replace repeating spaces with one space
130
349
{
131
349
  int orig_len = int(strlen(string));
132
349
  if (orig_len < 1)
133
57
    return;
134
292
  int i = 0;   // counter for resulting string
135
292
  int j = -1;
136
292
  bool prev_char_is_space = false;
137
292
  while (++j < orig_len && string[j] == ' ');
138
3.01k
  while (j < orig_len)  {
139
2.72k
    if (string[j] != ' ')  {
140
2.38k
        string[i++] = string[j++];
141
2.38k
        prev_char_is_space = false;
142
2.38k
    } else if (string[j++] == ' ') {
143
340
      if (!prev_char_is_space) {
144
125
        string[i++] = ' ';
145
125
        prev_char_is_space = true;
146
125
      }
147
340
    }
148
2.72k
  }
149
292
    if (i > 0 && string[i - 1] == ' ')
150
0
    string[i-1] = 0;
151
292
  if(i < orig_len)
152
20
    string[i] = 0; 
153
292
}