/src/LibRaw/src/preprocessing/ext_preprocess.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_fileio_defs.h" |
20 | | |
21 | | /* |
22 | | Search from the current directory up to the root looking for |
23 | | a ".badpixels" file, and fix those pixels now. |
24 | | */ |
25 | | void LibRaw::bad_pixels(const char *cfname) |
26 | 0 | { |
27 | 0 | FILE *fp = NULL; |
28 | 0 | char *cp, line[128]; |
29 | 0 | int time, row, col, r, c, rad, tot, n; |
30 | |
|
31 | 0 | if (!filters) |
32 | 0 | return; |
33 | 0 | RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS, 0, 2); |
34 | 0 | if (cfname) |
35 | 0 | fp = fopen(cfname, "r"); |
36 | 0 | if (!fp) |
37 | 0 | { |
38 | 0 | imgdata.process_warnings |= LIBRAW_WARN_NO_BADPIXELMAP; |
39 | 0 | return; |
40 | 0 | } |
41 | 0 | while (fgets(line, 128, fp)) |
42 | 0 | { |
43 | 0 | cp = strchr(line, '#'); |
44 | 0 | if (cp) |
45 | 0 | *cp = 0; |
46 | 0 | if (sscanf(line, "%d %d %d", &col, &row, &time) != 3) |
47 | 0 | continue; |
48 | 0 | if ((unsigned)col >= width || (unsigned)row >= height) |
49 | 0 | continue; |
50 | 0 | if (time > timestamp) |
51 | 0 | continue; |
52 | 0 | for (tot = n = 0, rad = 1; rad < 3 && n == 0; rad++) |
53 | 0 | for (r = row - rad; r <= row + rad; r++) |
54 | 0 | for (c = col - rad; c <= col + rad; c++) |
55 | 0 | if ((unsigned)r < height && (unsigned)c < width && |
56 | 0 | (r != row || c != col) && fcol(r, c) == fcol(row, col)) |
57 | 0 | { |
58 | 0 | tot += BAYER2(r, c); |
59 | 0 | n++; |
60 | 0 | } |
61 | 0 | if (n > 0) |
62 | 0 | BAYER2(row, col) = tot / n; |
63 | 0 | } |
64 | 0 | fclose(fp); |
65 | 0 | RUN_CALLBACK(LIBRAW_PROGRESS_BAD_PIXELS, 1, 2); |
66 | 0 | } |
67 | | |
68 | | void LibRaw::subtract(const char *fname) |
69 | 0 | { |
70 | 0 | FILE *fp; |
71 | 0 | int dim[3] = {0, 0, 0}, comment = 0, number = 0, error = 0, nd = 0, c, row, |
72 | 0 | col; |
73 | 0 | RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME, 0, 2); |
74 | |
|
75 | 0 | if (!(fp = fopen(fname, "rb"))) |
76 | 0 | { |
77 | 0 | imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_FILE; |
78 | 0 | return; |
79 | 0 | } |
80 | 0 | if (fgetc(fp) != 'P' || fgetc(fp) != '5') |
81 | 0 | error = 1; |
82 | 0 | while (!error && nd < 3 && (c = fgetc(fp)) != EOF) |
83 | 0 | { |
84 | 0 | if (c == '#') |
85 | 0 | comment = 1; |
86 | 0 | if (c == '\n') |
87 | 0 | comment = 0; |
88 | 0 | if (comment) |
89 | 0 | continue; |
90 | 0 | if (isdigit(c)) |
91 | 0 | number = 1; |
92 | 0 | if (number) |
93 | 0 | { |
94 | 0 | if (isdigit(c)) |
95 | 0 | dim[nd] = dim[nd] * 10 + c - '0'; |
96 | 0 | else if (isspace(c)) |
97 | 0 | { |
98 | 0 | number = 0; |
99 | 0 | nd++; |
100 | 0 | } |
101 | 0 | else |
102 | 0 | error = 1; |
103 | 0 | } |
104 | 0 | } |
105 | 0 | if (error || nd < 3) |
106 | 0 | { |
107 | 0 | fclose(fp); |
108 | 0 | return; |
109 | 0 | } |
110 | 0 | else if (dim[0] != width || dim[1] != height || dim[2] != 65535) |
111 | 0 | { |
112 | 0 | imgdata.process_warnings |= LIBRAW_WARN_BAD_DARKFRAME_DIM; |
113 | 0 | fclose(fp); |
114 | 0 | return; |
115 | 0 | } |
116 | 0 | std::vector<ushort> pixel(width, 0); |
117 | 0 | for (row = 0; row < height; row++) |
118 | 0 | { |
119 | 0 | fread(pixel.data(), 2, width, fp); |
120 | 0 | for (col = 0; col < width; col++) |
121 | 0 | BAYER(row, col) = MAX(BAYER(row, col) - ntohs(pixel[col]), 0); |
122 | 0 | } |
123 | 0 | fclose(fp); |
124 | 0 | memset(cblack, 0, sizeof cblack); |
125 | 0 | black = 0; |
126 | 0 | RUN_CALLBACK(LIBRAW_PROGRESS_DARK_FRAME, 1, 2); |
127 | 0 | } |