/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][2064]; |
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) |
83 | 0 | return; |
84 | 0 | while (isspace(p[l - 1])) |
85 | 0 | p[--l] = 0; /* trim trailing spaces */ |
86 | 0 | while (*p && isspace(*p)) |
87 | 0 | ++p, --l; /* trim leading spaces */ |
88 | 0 | memmove(s, p, l + 1); |
89 | 0 | } |
90 | | |
91 | | void LibRaw::remove_trailing_spaces(char *string, size_t len) |
92 | 0 | { |
93 | 0 | if (len < 1) |
94 | 0 | return; // not needed, b/c sizeof of make/model is 64 |
95 | 0 | string[len - 1] = 0; |
96 | 0 | if (len < 3) |
97 | 0 | return; // also not needed |
98 | 0 | len = strnlen(string, len - 1); |
99 | 0 | for (int i = int(len) - 1; i >= 0; i--) |
100 | 0 | { |
101 | 0 | if (isspace((unsigned char)string[i])) |
102 | 0 | string[i] = 0; |
103 | 0 | else |
104 | 0 | break; |
105 | 0 | } |
106 | 0 | } |
107 | | |
108 | | void LibRaw::remove_caseSubstr(char *string, char *subStr) // replace a substring with an equal length of spaces |
109 | 0 | { |
110 | 0 | char *found; |
111 | 0 | while ((found = strcasestr(string,subStr))) { |
112 | 0 | if (!found) return; |
113 | 0 | int fill_len = int(strlen(subStr)); |
114 | 0 | int p = int(found - string); |
115 | 0 | for (int i=p; i<p+fill_len; i++) { |
116 | 0 | string[i] = 32; |
117 | 0 | } |
118 | 0 | } |
119 | 0 | trimSpaces (string); |
120 | 0 | } |
121 | | |
122 | | void LibRaw::removeExcessiveSpaces(char *string) // replace repeating spaces with one space |
123 | 0 | { |
124 | 0 | int orig_len = int(strlen(string)); |
125 | 0 | int i = 0; // counter for resulting string |
126 | 0 | int j = -1; |
127 | 0 | bool prev_char_is_space = false; |
128 | 0 | while (++j < orig_len && string[j] == ' '); |
129 | 0 | while (j < orig_len) { |
130 | 0 | if (string[j] != ' ') { |
131 | 0 | string[i++] = string[j++]; |
132 | 0 | prev_char_is_space = false; |
133 | 0 | } else if (string[j++] == ' ') { |
134 | 0 | if (!prev_char_is_space) { |
135 | 0 | string[i++] = ' '; |
136 | 0 | prev_char_is_space = true; |
137 | 0 | } |
138 | 0 | } |
139 | 0 | } |
140 | 0 | if (string[i-1] == ' ') |
141 | 0 | string[i-1] = 0; |
142 | 0 | } |