aboutsummaryrefslogtreecommitdiff
path: root/tools/hotpixels.c
blob: ee1028cc04606d5b2c88e50ff734089df3f464df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// hotpixels.c: look for hot pixels in raw image files
//
// Usage: pass a bunch of raw photo images taken with the lens cap on at,
// e.g., ISO 8000-12800 @ 1/20-1/60, and store the resulting file as,
// e.g., Nikon D7500.badpixels, which can then be directly used by Rawtherapee.
//
// Copyright (c) 2023, Přemysl Eric Janouch <p@janouch.name>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//

#include <libraw.h>

#if LIBRAW_VERSION < LIBRAW_MAKE_VERSION(0, 21, 0)
#error LibRaw 0.21.0 or newer is required.
#endif

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void *
xreallocarray(void *o, size_t n, size_t m)
{
	if (m && n > SIZE_MAX / m) {
		fprintf(stderr, "xreallocarray: %s\n", strerror(ENOMEM));
		exit(EXIT_FAILURE);
	}
	void *p = realloc(o, n * m);
	if (!p && n && m) {
		fprintf(stderr, "xreallocarray: %s\n", strerror(errno));
		exit(EXIT_FAILURE);
	}
	return p;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

struct coord { ushort x, y; };

static bool
coord_equals(struct coord a, struct coord b)
{
	return a.x == b.x && a.y == b.y;
}

static int
coord_cmp(const void *a, const void *b)
{
	const struct coord *ca = (const struct coord *) a;
	const struct coord *cb = (const struct coord *) b;
	return ca->y != cb->y
		? (int) ca->y - (int) cb->y
		: (int) ca->x - (int) cb->x;
}

struct candidates {
	struct coord *xy;
	size_t len;
	size_t alloc;
};

static void
candidates_add(struct candidates *c, ushort x, ushort y)
{
	if (c->len == c->alloc) {
		c->alloc += 64;
		c->xy = xreallocarray(c->xy, sizeof *c->xy, c->alloc);
	}

	c->xy[c->len++] = (struct coord) {x, y};
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

// A stretch of zeroes that is assumed to mean start of outliers.
#define SPAN 10

static const char *
process_raw(struct candidates *c, const uint8_t *p, size_t len)
{
	libraw_data_t *iprc = libraw_init(LIBRAW_OPIONS_NO_DATAERR_CALLBACK);
	if (!iprc)
		return "failed to obtain a LibRaw handle";

	int err = 0;
	if ((err = libraw_open_buffer(iprc, p, len)) ||
		(err = libraw_unpack(iprc))) {
		libraw_close(iprc);
		return libraw_strerror(err);
	}
	if (!iprc->rawdata.raw_image) {
		libraw_close(iprc);
		return "only Bayer raws are supported, not Foveon";
	}

	// Make a histogram.
	uint64_t bins[USHRT_MAX] = {};
	for (ushort yy = 0; yy < iprc->sizes.height; yy++) {
		for (ushort xx = 0; xx < iprc->sizes.width; xx++) {
			ushort y = iprc->sizes.top_margin + yy;
			ushort x = iprc->sizes.left_margin + xx;
			bins[iprc->rawdata.raw_image[y * iprc->sizes.raw_width + x]]++;
		}
	}

	// Detecting outliers is not completely straight-forward,
	// it may help to see the histogram.
	if (getenv("HOTPIXELS_HISTOGRAM")) {
		for (ushort i = 0; i < USHRT_MAX; i++)
			fprintf(stderr, "%u ", (unsigned) bins[i]);
		fputc('\n', stderr);
	}

	// Go to the first non-zero pixel value.
	size_t last = 0;
	for (; last < USHRT_MAX; last++)
		if (bins[last])
			break;

	// Find the last pixel value we assume to not be hot.
	for (; last < USHRT_MAX - SPAN - 1; last++) {
		uint64_t nonzero = 0;
		for (int i = 1; i <= SPAN; i++)
			nonzero += bins[last + i];
		if (!nonzero)
			break;
	}

	// Store coordinates for all pixels above that value.
	for (ushort yy = 0; yy < iprc->sizes.height; yy++) {
		for (ushort xx = 0; xx < iprc->sizes.width; xx++) {
			ushort y = iprc->sizes.top_margin + yy;
			ushort x = iprc->sizes.left_margin + xx;
			if (iprc->rawdata.raw_image[y * iprc->sizes.raw_width + x] > last)
				candidates_add(c, xx, yy);
		}
	}

	libraw_close(iprc);
	return NULL;
}

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

static const char *
do_file(struct candidates *c, const char *filename)
{
	FILE *fp = fopen(filename, "rb");
	if (!fp)
		return strerror(errno);

	uint8_t *data = NULL, buf[256 << 10];
	size_t n, len = 0;
	while ((n = fread(buf, sizeof *buf, sizeof buf / sizeof *buf, fp))) {
		data = xreallocarray(data, len + n, 1);
		memcpy(data + len, buf, n);
		len += n;
	}

	const char *err = ferror(fp)
		? strerror(errno)
		: process_raw(c, data, len);

	fclose(fp);
	free(data);
	return err;
}

int
main(int argc, char *argv[])
{
	struct candidates c = {};
	for (int i = 1; i < argc; i++) {
		const char *filename = argv[i], *err = do_file(&c, filename);
		if (err) {
			fprintf(stderr, "%s: %s\n", filename, err);
			return EXIT_FAILURE;
		}
	}

	qsort(c.xy, c.len, sizeof *c.xy, coord_cmp);

	// If it is detected in all passed photos, it is probably indeed bad.
	int count = 1;
	for (size_t i = 1; i <= c.len; i++) {
		if (i != c.len && coord_equals(c.xy[i - 1], c.xy[i])) {
			count++;
			continue;
		}

		if (count == argc - 1)
			printf("%u %u\n", c.xy[i - 1].x, c.xy[i - 1].y);

		count = 1;
	}
	return 0;
}