Coverage Report

Created: 2026-08-17 07:50

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
0
{
46
0
  UINT64 bitbuf = 0;
47
0
  int vbits, col, i, c;
48
0
  ushort img[2][2065];
49
0
  float sum[] = {0, 0};
50
0
  if (width > 2064)
51
0
    return 0.f; // too wide
52
53
0
  FORC(2)
54
0
  {
55
0
    fseek(ifp, c ? off1 : off0, SEEK_SET);
56
0
    for (vbits = col = 0; col < width; col++)
57
0
    {
58
0
      for (vbits -= bps; vbits < 0; vbits += bite)
59
0
      {
60
0
        bitbuf <<= bite;
61
0
        for (i = 0; i < bite; i += 8)
62
0
          bitbuf |= (unsigned)(fgetc(ifp) << i);
63
0
      }
64
0
      img[c][col] = ushort((bitbuf << (64 - bps - vbits) >> (64 - bps)) & 0xffff);
65
0
    }
66
0
  }
67
0
  FORC(width - 1)
68
0
  {
69
0
    sum[c & 1] += ABS(img[0][c] - img[1][c + 1]);
70
0
    sum[~c & 1] += ABS(img[1][c] - img[0][c + 1]);
71
0
  }
72
0
  if (sum[0] >= 1.0 && sum[1] >= 1.0)
73
0
    return 100.f * logf(sum[0] / sum[1]);
74
0
  else
75
0
    return 0.f;
76
0
}
77
78
void LibRaw::trimSpaces(char *s)
79
0
{
80
0
  char *p = s;
81
0
  int l = int(strlen(p));
82
0
  if (l<1)
83
0
    return;
84
0
  while (l > 0 && isspace(p[l - 1]))
85
0
    p[--l] = 0; /* trim trailing spaces */
86
0
  if (l < 1)
87
0
    return; // only spaces in the input string, all wiped out;
88
0
  while (*p && isspace(*p) && l > 0)
89
0
    ++p, --l;   /* trim leading spaces */
90
0
  if (l < 1)  // should not happen, but safety belt
91
0
  {
92
0
    *s = 0;
93
0
    return;
94
0
  }
95
0
  memmove(s, p, l + 1);
96
0
}
97
98
void LibRaw::remove_trailing_spaces(char *string, size_t len)
99
0
{
100
0
  if (len < 1)
101
0
    return; // not needed, b/c sizeof of make/model is 64
102
0
  string[len - 1] = 0;
103
0
  if (len < 3)
104
0
    return; // also not needed
105
0
  len = strnlen(string, len - 1);
106
0
  for (int i = int(len) - 1; i >= 0; i--)
107
0
  {
108
0
    if (isspace((unsigned char)string[i]))
109
0
      string[i] = 0;
110
0
    else
111
0
      break;
112
0
  }
113
0
}
114
115
void LibRaw::remove_caseSubstr(char *string, char *subStr) // replace a substring with an equal length of spaces
116
0
{
117
0
  char *found;
118
0
  while ((found = strcasestr(string,subStr))) {
119
0
    if (!found) return;
120
0
    int fill_len = int(strlen(subStr));
121
0
    int p = int(found - string);
122
0
    for (int i=p; i<p+fill_len; i++) {
123
0
      string[i] = 32;
124
0
    }
125
0
  }
126
0
  trimSpaces (string);
127
0
}
128
129
void LibRaw::removeExcessiveSpaces(char *string) // replace repeating spaces with one space
130
0
{
131
0
  int orig_len = int(strlen(string));
132
0
  if (orig_len < 1)
133
0
    return;
134
0
  int i = 0;   // counter for resulting string
135
0
  int j = -1;
136
0
  bool prev_char_is_space = false;
137
0
  while (++j < orig_len && string[j] == ' ');
138
0
  while (j < orig_len)  {
139
0
    if (string[j] != ' ')  {
140
0
        string[i++] = string[j++];
141
0
        prev_char_is_space = false;
142
0
    } else if (string[j++] == ' ') {
143
0
      if (!prev_char_is_space) {
144
0
        string[i++] = ' ';
145
0
        prev_char_is_space = true;
146
0
      }
147
0
    }
148
0
  }
149
0
    if (i > 0 && string[i - 1] == ' ')
150
0
    string[i-1] = 0;
151
0
  if(i < orig_len)
152
0
    string[i] = 0; 
153
0
}