quickpreview.c 13.1 KB
Newer Older
1
2
3
4
5
/*
 * Fast but bad debayer method that scales and rotates by skipping source
 * pixels and doesn't interpolate any values at all
 */

Martijn Braam's avatar
Martijn Braam committed
6
#include "quickpreview.h"
7
#include "matrix.h"
8
9
#include <assert.h>
#include <stdio.h>
10

Martijn Braam's avatar
Martijn Braam committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* Linear -> sRGB lookup table */
static const int srgb[] = {
	0, 12, 21, 28, 33, 38, 42, 46, 49, 52, 55, 58, 61, 63, 66, 68, 70,
	73, 75, 77, 79, 81, 82, 84, 86, 88, 89, 91, 93, 94, 96, 97, 99, 100,
	102, 103, 104, 106, 107, 109, 110, 111, 112, 114, 115, 116, 117, 118,
	120, 121, 122, 123, 124, 125, 126, 127, 129, 130, 131, 132, 133, 134,
	135, 136, 137, 138, 139, 140, 141, 142, 142, 143, 144, 145, 146, 147,
	148, 149, 150, 151, 151, 152, 153, 154, 155, 156, 157, 157, 158, 159,
	160, 161, 161, 162, 163, 164, 165, 165, 166, 167, 168, 168, 169, 170,
	171, 171, 172, 173, 174, 174, 175, 176, 176, 177, 178, 179, 179, 180,
	181, 181, 182, 183, 183, 184, 185, 185, 186, 187, 187, 188, 189, 189,
	190, 191, 191, 192, 193, 193, 194, 194, 195, 196, 196, 197, 197, 198,
	199, 199, 200, 201, 201, 202, 202, 203, 204, 204, 205, 205, 206, 206,
	207, 208, 208, 209, 209, 210, 210, 211, 212, 212, 213, 213, 214, 214,
	215, 215, 216, 217, 217, 218, 218, 219, 219, 220, 220, 221, 221, 222,
	222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, 228, 229, 229,
	230, 230, 231, 231, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236,
	237, 237, 237, 238, 238, 239, 239, 240, 240, 241, 241, 242, 242, 243,
	243, 244, 244, 245, 245, 245, 246, 246, 247, 247, 248, 248, 249, 249,
	250, 250, 251, 251, 251, 252, 252, 253, 253, 254, 254, 255
};

Martijn Braam's avatar
Martijn Braam committed
33
34
static inline uint32_t
pack_rgb(uint8_t r, uint8_t g, uint8_t b)
35
{
36
	return (r << 16) | (g << 8) | b;
37
}
38

Martijn Braam's avatar
Martijn Braam committed
39
40
static inline uint32_t
convert_yuv_to_srgb(uint8_t y, uint8_t u, uint8_t v)
41
{
42
43
44
45
46
47
	uint32_t r = 1.164f * y + 1.596f * (v - 128);
	uint32_t g = 1.164f * y - 0.813f * (v - 128) - 0.391f * (u - 128);
	uint32_t b = 1.164f * y + 2.018f * (u - 128);
	return pack_rgb(r, g, b);
}

Martijn Braam's avatar
Martijn Braam committed
48
static inline uint32_t
49
apply_colormatrix(uint32_t color, const float *colormatrix, uint32_t whitepoint)
50
51
52
{
	if (!colormatrix) {
		return color;
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
	uint8_t cR = color >> 16;
	uint8_t cG = color >> 8;
	uint8_t cB = color;

	uint8_t wR = whitepoint >> 16;
	uint8_t wG = whitepoint >> 8;
	uint8_t wB = whitepoint;

	// Clip highlights
	if (cR >= wR || cG >= wG || cB >= wB) {
		float rR = cR / (float)wR, rG = cG / (float)wG, rB = cB / (float)wB;
		float ratio = (rR + rG + rB) * 0.95 / 3.0;
		if (ratio > 1) {
			cR = wR; cG = wG; cB = wB;
		}
	}

	uint32_t r = cR * colormatrix[0] +
		     cG * colormatrix[1] +
		     cB * colormatrix[2];
	uint32_t g = cR * colormatrix[3] +
		     cG * colormatrix[4] +
		     cB * colormatrix[5];
	uint32_t b = cR * colormatrix[6] +
		     cG * colormatrix[7] +
		     cB * colormatrix[8];

	// Clip out-of-gamut colors
Benjamin Schaaf's avatar
Benjamin Schaaf committed
83
84
85
86
87
88
	if (r > 0xFF)
		r = 0xFF;
	if (g > 0xFF)
		g = 0xFF;
	if (b > 0xFF)
		b = 0xFF;
89

90
	return pack_rgb(srgb[r], srgb[g], srgb[b]);
91
}
92

Martijn Braam's avatar
Martijn Braam committed
93
94
95
static inline uint32_t
coord_map(uint32_t x, uint32_t y, uint32_t width, uint32_t height, int rotation,
	  bool mirrored)
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
{
	uint32_t x_r, y_r;
	if (rotation == 0) {
		x_r = x;
		y_r = y;
	} else if (rotation == 90) {
		x_r = y;
		y_r = height - x - 1;
	} else if (rotation == 270) {
		x_r = width - y - 1;
		y_r = x;
	} else {
		x_r = width - x - 1;
		y_r = height - y - 1;
	}
111

112
113
114
	if (mirrored) {
		x_r = width - x_r - 1;
	}
Martijn Braam's avatar
Martijn Braam committed
115

116
117
	uint32_t index = y_r * width + x_r;
#ifdef DEBUG
118
119
120
121
    if ((index >= width * height) || (index < 0)) {
        g_printerr("MPCamera: x:y %d:%d x_r:y_r %d:%d\n", x, y, x_r, y_r);
        g_printerr("MPCamera: %d:%d:%d\n", index, width, height);
    }
122
	assert(index < width * height);
123
	assert(index >= 0);
124
125
#endif
	return index;
126
}
Martijn Braam's avatar
Martijn Braam committed
127

128
129
130
131
132
133
static inline uint16_t
scale_value(uint16_t val, uint16_t min, uint16_t max)
{
	return val <= min ? 0 : (val - (min * (max - val) / (max - min)));
}

Martijn Braam's avatar
Martijn Braam committed
134
135
136
137
138
139
140
static void
quick_preview_rggb8(uint32_t *dst, const uint32_t dst_width,
		    const uint32_t dst_height, const uint8_t *src,
		    const uint32_t src_width, const uint32_t src_height,
		    const MPPixelFormat format, const uint32_t rotation,
		    const bool mirrored, const float *colormatrix,
		    const uint8_t blacklevel, const uint32_t skip)
141
{
142
143
144
145
	float wp[3];
	matrix_white_point(colormatrix, wp);
	uint32_t whitepoint = pack_rgb(wp[0] * 255, wp[1] * 255, wp[2] * 255);

146
147
148
149
150
151
	uint32_t src_y = 0, dst_y = 0;
	while (src_y < src_height) {
		uint32_t src_x = 0, dst_x = 0;
		while (src_x < src_width) {
			uint32_t src_i = src_y * src_width + src_x;

152
153
154
155
			uint8_t b0 = scale_value(src[src_i], blacklevel, 255);
			uint8_t b1 = scale_value(src[src_i + 1], blacklevel, 255);
			uint8_t b2 = scale_value(src[src_i + src_width], blacklevel, 255);
			uint8_t b3 = scale_value(src[src_i + src_width + 1], blacklevel, 255);
156
157
158

			uint32_t color;
			switch (format) {
Martijn Braam's avatar
Martijn Braam committed
159
			case MP_PIXEL_FMT_BGGR8:
160
				color = pack_rgb(b3, (b1 + b2) / 2, b0);
Martijn Braam's avatar
Martijn Braam committed
161
162
				break;
			case MP_PIXEL_FMT_GBRG8:
163
				color = pack_rgb(b2, (b0 + b3) / 2, b1);
Martijn Braam's avatar
Martijn Braam committed
164
165
				break;
			case MP_PIXEL_FMT_GRBG8:
166
				color = pack_rgb(b1, (b0 + b3) / 2, b2);
Martijn Braam's avatar
Martijn Braam committed
167
168
				break;
			case MP_PIXEL_FMT_RGGB8:
169
				color = pack_rgb(b0, (b1 + b2) / 2, b3);
Martijn Braam's avatar
Martijn Braam committed
170
171
172
				break;
			default:
				assert(false);
173
174
			}

175
			color = apply_colormatrix(color, colormatrix, whitepoint);
176

Martijn Braam's avatar
Martijn Braam committed
177
178
			dst[coord_map(dst_x, dst_y, dst_width, dst_height, rotation,
				      mirrored)] = color;
Martijn Braam's avatar
Martijn Braam committed
179

180
181
182
183
184
185
186
187
188
			src_x += 2 + 2 * skip;
			++dst_x;
		}

		src_y += 2 + 2 * skip;
		++dst_y;
	}
}

Martijn Braam's avatar
Martijn Braam committed
189
static void
190
quick_preview_rggb10p(uint32_t *dst, const uint32_t dst_width,
Martijn Braam's avatar
Martijn Braam committed
191
192
193
194
195
		     const uint32_t dst_height, const uint8_t *src,
		     const uint32_t src_width, const uint32_t src_height,
		     const MPPixelFormat format, const uint32_t rotation,
		     const bool mirrored, const float *colormatrix,
		     const uint8_t blacklevel, const uint32_t skip)
Martijn Braam's avatar
Martijn Braam committed
196
{
197
198
	assert(src_width % 2 == 0);

199
200
201
202
	float wp[3];
	matrix_white_point(colormatrix, wp);
	uint32_t whitepoint = pack_rgb(wp[0] * 255, wp[1] * 255, wp[2] * 255);

203
204
205
206
207
208
209
210
	uint32_t width_bytes = mp_pixel_format_width_to_bytes(format, src_width);

	uint32_t src_y = 0, dst_y = 0;
	while (src_y < src_height) {
		uint32_t src_x = 0, dst_x = 0;
		while (src_x < width_bytes) {
			uint32_t src_i = src_y * width_bytes + src_x;

211
212
213
214
			uint8_t b0 = scale_value(src[src_i], blacklevel, 255);
			uint8_t b1 = scale_value(src[src_i + 1], blacklevel, 255);
			uint8_t b2 = scale_value(src[src_i + width_bytes], blacklevel, 255);
			uint8_t b3 = scale_value(src[src_i + width_bytes + 1], blacklevel, 255);
215
216
217

			uint32_t color;
			switch (format) {
Martijn Braam's avatar
Martijn Braam committed
218
			case MP_PIXEL_FMT_BGGR10P:
219
				color = pack_rgb(b3, (b1 + b2) / 2, b0);
Martijn Braam's avatar
Martijn Braam committed
220
221
				break;
			case MP_PIXEL_FMT_GBRG10P:
222
				color = pack_rgb(b2, (b0 + b3) / 2, b1);
Martijn Braam's avatar
Martijn Braam committed
223
224
				break;
			case MP_PIXEL_FMT_GRBG10P:
225
				color = pack_rgb(b1, (b0 + b3) / 2, b2);
Martijn Braam's avatar
Martijn Braam committed
226
227
				break;
			case MP_PIXEL_FMT_RGGB10P:
228
				color = pack_rgb(b0, (b1 + b2) / 2, b3);
Martijn Braam's avatar
Martijn Braam committed
229
230
231
				break;
			default:
				assert(false);
232
233
			}

234
			color = apply_colormatrix(color, colormatrix, whitepoint);
235

Martijn Braam's avatar
Martijn Braam committed
236
237
			dst[coord_map(dst_x, dst_y, dst_width, dst_height, rotation,
				      mirrored)] = color;
238
239

			uint32_t advance = 1 + skip;
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
            if (src_x % 5 == 0) {
                src_x += 2 * (advance % 2) + 5 * (advance / 2);
            } else {
                src_x += 3 * (advance % 2) + 5 * (advance / 2);
            }
			++dst_x;
		}

		src_y += 2 + 2 * skip;
		++dst_y;
	}
}

static void
quick_preview_rggb10(uint32_t *dst, const uint32_t dst_width,
		     const uint32_t dst_height, const uint8_t *src,
		     const uint32_t src_width, const uint32_t src_height,
		     const MPPixelFormat format, const uint32_t rotation,
		     const bool mirrored, const float *colormatrix,
		     const uint8_t blacklevel, const uint32_t skip)
{
	assert(src_width % 2 == 0);
262
	uint16_t *src16 = (uint16_t *)src;
263

264
265
266
267
	float wp[3];
	matrix_white_point(colormatrix, wp);
	uint32_t whitepoint = pack_rgb(wp[0] * 255, wp[1] * 255, wp[2] * 255);

Dorota Czaplejewicz's avatar
Dorota Czaplejewicz committed
268
#ifdef NO
269
270
271
272
273
274
275
276
	g_printerr("MPCamera: dst %d:%d src %d:%d skip: %d blacklevel %d\n", dst_width, dst_height, src_width, src_height, skip, blacklevel);
#endif

	uint32_t src_y = 0, dst_y = 0;
	while (src_y < src_height) {
		uint32_t src_x = 0, dst_x = 0;
		while (src_x < src_width) {
			uint32_t src_i = src_y * src_width + src_x;
277
278
279
280
281

			uint8_t b0 = scale_value(src16[src_i], blacklevel, 1023) >> 2;
			uint8_t b1 = scale_value(src16[src_i + 1], blacklevel, 1023) >> 2;
			uint8_t b2 = scale_value(src16[src_i + src_width], blacklevel, 1023) >> 2;
			uint8_t b3 = scale_value(src16[src_i + src_width + 1], blacklevel, 1023) >> 2;
282
283
284
285

			uint32_t color;
			switch (format) {
			case MP_PIXEL_FMT_BGGR10:
286
				color = pack_rgb(b3, (b1 + b2) / 2, b0);
287
288
				break;
			case MP_PIXEL_FMT_GBRG10:
289
				color = pack_rgb(b2, (b0 + b3) / 2, b1);
290
291
				break;
			case MP_PIXEL_FMT_GRBG10:
292
				color = pack_rgb(b1, (b0 + b3) / 2, b2);
293
294
				break;
			case MP_PIXEL_FMT_RGGB10:
295
				color = pack_rgb(b0, (b1 + b2) / 2, b3);
296
297
298
				break;
			default:
				assert(false);
299
			}
300

301
			color = apply_colormatrix(color, colormatrix, whitepoint);
302
303
304
305

			dst[coord_map(dst_x, dst_y, dst_width, dst_height, rotation,
				      mirrored)] = color;

306
			src_x += 2 + 2 * skip;
307
			++dst_x;
Martijn Braam's avatar
Martijn Braam committed
308
		}
309
310
311
312
313
314

		src_y += 2 + 2 * skip;
		++dst_y;
	}
}

Martijn Braam's avatar
Martijn Braam committed
315
316
317
318
319
320
static void
quick_preview_yuv(uint32_t *dst, const uint32_t dst_width, const uint32_t dst_height,
		  const uint8_t *src, const uint32_t src_width,
		  const uint32_t src_height, const MPPixelFormat format,
		  const uint32_t rotation, const bool mirrored,
		  const float *colormatrix, const uint32_t skip)
321
322
323
{
	assert(src_width % 2 == 0);

324
325
326
327
	float wp[3];
	matrix_white_point(colormatrix, wp);
	uint32_t whitepoint = pack_rgb(wp[0] * 255, wp[1] * 255, wp[2] * 255);

328
329
	uint32_t width_bytes = src_width * 2;

Benjamin Schaaf's avatar
Benjamin Schaaf committed
330
331
332
333
334
	uint32_t unrot_dst_width = dst_width;
	if (rotation != 0 && rotation != 180) {
		unrot_dst_width = dst_height;
	}

335
336
337
338
339
340
341
342
343
344
345
346
347
	uint32_t src_y = 0, dst_y = 0;
	while (src_y < src_height) {
		uint32_t src_x = 0, dst_x = 0;
		while (src_x < width_bytes) {
			uint32_t src_i = src_y * width_bytes + src_x;

			uint8_t b0 = src[src_i];
			uint8_t b1 = src[src_i + 1];
			uint8_t b2 = src[src_i + 2];
			uint8_t b3 = src[src_i + 3];

			uint32_t color1, color2;
			switch (format) {
Martijn Braam's avatar
Martijn Braam committed
348
349
350
351
352
353
354
355
356
357
			case MP_PIXEL_FMT_UYVY:
				color1 = convert_yuv_to_srgb(b1, b0, b2);
				color2 = convert_yuv_to_srgb(b3, b0, b2);
				break;
			case MP_PIXEL_FMT_YUYV:
				color1 = convert_yuv_to_srgb(b0, b1, b3);
				color2 = convert_yuv_to_srgb(b2, b1, b3);
				break;
			default:
				assert(false);
358
359
			}

360
361
			color1 = apply_colormatrix(color1, colormatrix, whitepoint);
			color2 = apply_colormatrix(color2, colormatrix, whitepoint);
362

Martijn Braam's avatar
Martijn Braam committed
363
364
			uint32_t dst_i1 = coord_map(dst_x, dst_y, dst_width,
						    dst_height, rotation, mirrored);
365
366
367
			dst[dst_i1] = color1;
			++dst_x;

Benjamin Schaaf's avatar
Benjamin Schaaf committed
368
369
			// The last pixel needs to be skipped if we have an odd un-rotated width
			if (dst_x < unrot_dst_width) {
Martijn Braam's avatar
Martijn Braam committed
370
371
372
				uint32_t dst_i2 =
					coord_map(dst_x, dst_y, dst_width,
						  dst_height, rotation, mirrored);
Benjamin Schaaf's avatar
Benjamin Schaaf committed
373
374
375
				dst[dst_i2] = color2;
				++dst_x;
			}
376
377

			src_x += 4 + 4 * skip;
Martijn Braam's avatar
Martijn Braam committed
378
		}
379
380
381
382
383
384

		src_y += 1 + skip;
		++dst_y;
	}
}

Martijn Braam's avatar
Martijn Braam committed
385
386
387
388
389
390
void
quick_preview(uint32_t *dst, const uint32_t dst_width, const uint32_t dst_height,
	      const uint8_t *src, const uint32_t src_width,
	      const uint32_t src_height, const MPPixelFormat format,
	      const uint32_t rotation, const bool mirrored, const float *colormatrix,
	      const uint8_t blacklevel, const uint32_t skip)
391
392
{
	switch (format) {
Martijn Braam's avatar
Martijn Braam committed
393
394
395
396
397
398
399
400
401
402
403
404
	case MP_PIXEL_FMT_BGGR8:
	case MP_PIXEL_FMT_GBRG8:
	case MP_PIXEL_FMT_GRBG8:
	case MP_PIXEL_FMT_RGGB8:
		quick_preview_rggb8(dst, dst_width, dst_height, src, src_width,
				    src_height, format, rotation, mirrored,
				    colormatrix, blacklevel, skip);
		break;
	case MP_PIXEL_FMT_BGGR10P:
	case MP_PIXEL_FMT_GBRG10P:
	case MP_PIXEL_FMT_GRBG10P:
	case MP_PIXEL_FMT_RGGB10P:
405
406
407
408
409
410
411
412
		quick_preview_rggb10p(dst, dst_width, dst_height, src, src_width,
				     src_height, format, rotation, mirrored,
				     colormatrix, blacklevel, skip);
		break;
	case MP_PIXEL_FMT_BGGR10:
	case MP_PIXEL_FMT_GBRG10:
	case MP_PIXEL_FMT_GRBG10:
	case MP_PIXEL_FMT_RGGB10:
Martijn Braam's avatar
Martijn Braam committed
413
414
415
416
417
418
419
420
421
422
423
424
		quick_preview_rggb10(dst, dst_width, dst_height, src, src_width,
				     src_height, format, rotation, mirrored,
				     colormatrix, blacklevel, skip);
		break;
	case MP_PIXEL_FMT_UYVY:
	case MP_PIXEL_FMT_YUYV:
		quick_preview_yuv(dst, dst_width, dst_height, src, src_width,
				  src_height, format, rotation, mirrored,
				  colormatrix, skip);
		break;
	default:
		assert(false);
425
426
427
	}
}

Martijn Braam's avatar
Martijn Braam committed
428
429
static uint32_t
div_ceil(uint32_t x, uint32_t y)
430
{
Martijn Braam's avatar
Martijn Braam committed
431
	return x / y + !!(x % y);
432
433
}

Martijn Braam's avatar
Martijn Braam committed
434
435
436
437
438
void
quick_preview_size(uint32_t *dst_width, uint32_t *dst_height, uint32_t *skip,
		   const uint32_t preview_width, const uint32_t preview_height,
		   const uint32_t src_width, const uint32_t src_height,
		   const MPPixelFormat format, const int rotation)
439
440
441
442
443
444
445
446
447
448
{
	uint32_t colors_x = mp_pixel_format_width_to_colors(format, src_width);
	uint32_t colors_y = mp_pixel_format_height_to_colors(format, src_height);

	if (rotation != 0 && rotation != 180) {
		uint32_t tmp = colors_x;
		colors_x = colors_y;
		colors_y = tmp;
	}

449
450
	uint32_t scale_x = colors_x / (preview_width * 0.75);
	uint32_t scale_y = colors_y / (preview_height * 0.75);
451
452
453
454
455
456
457
458
459
460
461
462
463
464

	if (scale_x > 0)
		--scale_x;
	if (scale_y > 0)
		--scale_y;
	*skip = scale_x > scale_y ? scale_x : scale_y;

	*dst_width = div_ceil(colors_x, (1 + *skip));
	if (*dst_width <= 0)
		*dst_width = 1;

	*dst_height = div_ceil(colors_y, (1 + *skip));
	if (*dst_height <= 0)
		*dst_height = 1;
Martijn Braam's avatar
Martijn Braam committed
465
}