1 /* -*- C++ -*-
2 * Copyright 2019-2020 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
guess_byte_order(int words)21 short LibRaw::guess_byte_order(int words)
22 {
23 uchar test[4][2];
24 int t = 2, msb;
25 double diff, sum[2] = {0, 0};
26
27 fread(test[0], 2, 2, ifp);
28 for (words -= 2; words--;)
29 {
30 fread(test[t], 2, 1, ifp);
31 for (msb = 0; msb < 2; msb++)
32 {
33 diff = (test[t ^ 2][msb] << 8 | test[t ^ 2][!msb]) -
34 (test[t][msb] << 8 | test[t][!msb]);
35 sum[msb] += diff * diff;
36 }
37 t = (t + 1) & 3;
38 }
39 return sum[0] < sum[1] ? 0x4d4d : 0x4949;
40 }
41
find_green(int bps,int bite,int off0,int off1)42 float LibRaw::find_green(int bps, int bite, int off0, int off1)
43 {
44 UINT64 bitbuf = 0;
45 int vbits, col, i, c;
46 ushort img[2][2064];
47 double sum[] = {0, 0};
48 if (width > 2064)
49 return 0.f; // too wide
50
51 FORC(2)
52 {
53 fseek(ifp, c ? off1 : off0, SEEK_SET);
54 for (vbits = col = 0; col < width; col++)
55 {
56 for (vbits -= bps; vbits < 0; vbits += bite)
57 {
58 bitbuf <<= bite;
59 for (i = 0; i < bite; i += 8)
60 bitbuf |= (unsigned)(fgetc(ifp) << i);
61 }
62 img[c][col] = bitbuf << (64 - bps - vbits) >> (64 - bps);
63 }
64 }
65 FORC(width - 1)
66 {
67 sum[c & 1] += ABS(img[0][c] - img[1][c + 1]);
68 sum[~c & 1] += ABS(img[1][c] - img[0][c + 1]);
69 }
70 if (sum[0] >= 1.0 && sum[1] >= 1.0)
71 return 100 * log(sum[0] / sum[1]);
72 else
73 return 0.f;
74 }
75
trimSpaces(char * s)76 void LibRaw::trimSpaces(char *s)
77 {
78 char *p = s;
79 int l = strlen(p);
80 if (!l)
81 return;
82 while (isspace(p[l - 1]))
83 p[--l] = 0; /* trim trailing spaces */
84 while (*p && isspace(*p))
85 ++p, --l; /* trim leading spaces */
86 memmove(s, p, l + 1);
87 }
88
remove_trailing_spaces(char * string,size_t len)89 void LibRaw::remove_trailing_spaces(char *string, size_t len)
90 {
91 if (len < 1)
92 return; // not needed, b/c sizeof of make/model is 64
93 string[len - 1] = 0;
94 if (len < 3)
95 return; // also not needed
96 len = strnlen(string, len - 1);
97 for (int i = len - 1; i >= 0; i--)
98 {
99 if (isspace((unsigned char)string[i]))
100 string[i] = 0;
101 else
102 break;
103 }
104 }
105
remove_caseSubstr(char * string,char * subStr)106 void LibRaw::remove_caseSubstr(char *string, char *subStr) // replace a substring with an equal length of spaces
107 {
108 char *found;
109 while ((found = strcasestr(string,subStr))) {
110 if (!found) return;
111 int fill_len = strlen(subStr);
112 int p = found - string;
113 for (int i=p; i<p+fill_len; i++) {
114 string[i] = 32;
115 }
116 }
117 trimSpaces (string);
118 }
119
removeExcessiveSpaces(char * string)120 void LibRaw::removeExcessiveSpaces(char *string) // replace repeating spaces with one space
121 {
122 int orig_len = strlen(string);
123 int i = 0; // counter for resulting string
124 int j = -1;
125 bool prev_char_is_space = false;
126 while (++j < orig_len && string[j] == ' ');
127 while (j < orig_len) {
128 if (string[j] != ' ') {
129 string[i++] = string[j++];
130 prev_char_is_space = false;
131 } else if (string[j++] == ' ') {
132 if (!prev_char_is_space) {
133 string[i++] = ' ';
134 prev_char_is_space = true;
135 }
136 }
137 }
138 if (string[i-1] == ' ')
139 string[i-1] = 0;
140 }
141