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