summaryrefslogtreecommitdiff
path: root/image.c
blob: ec29751e86492f13610d159c4544fada0cbc6741 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/* Camicro - Microscope camera viewer.
 *
 * Copyright (C) 2019 Nicolas Schodet
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * Contact :
 *        Web: http://ni.fr.eu.org/
 *      Email: <nico at ni.fr.eu.org>
 */
#include <stdlib.h>
#include <string.h>
#include <png.h>

#include "image.h"
#include "utils.h"

typedef uint8_t u8x16 __attribute__((vector_size(16)));
typedef uint16_t u16x8 __attribute__((vector_size(16)));

GQuark
image_error_quark(void)
{
    return g_quark_from_static_string("image-error-quark");
}

static void
image_free(struct image *image, void *user_data)
{
    free(image);
}

struct image *
image_new(int width, int height, int stride, enum image_format format)
{
    size_t struct_size_aligned = (sizeof(struct image) + 15) / 16 * 16;
    size_t image_size_aligned = ((height * stride) + 15) / 16 * 16;
    uint8_t *m = aligned_alloc(16,
	    struct_size_aligned + image_size_aligned);
    if (!m)
	g_error("aligned_alloc: failed to allocate");
    struct image *image = (struct image *) m;
    image->width = width;
    image->height = height;
    image->stride = stride;
    image->format = format;
    image->pixels = m + struct_size_aligned;
    image->refs = 1;
    image->release = image_free;
    image->release_user_data = NULL;
    return image;
}

void
image_ref(struct image *image)
{
    image->refs++;
}

void
image_unref(struct image *image)
{
    image->refs--;
    if (image->refs == 0 && image->release)
	image->release(image, image->release_user_data);
}

static void
image_histogram_sgrbg8(const struct image *image, uint32_t histogram[3][256])
{
    g_assert(image->width % 2 == 0);
    g_assert(image->height % 2 == 0);
    g_assert(image->stride == image->width);
    memset(histogram, 0, 3 * 256 * sizeof(uint32_t));
    const uint8_t *p = image->pixels;
    for (int i = 0; i < image->height; i += 2) {
	for (int j = 0; j < image->width; j += 2) {
	    uint8_t g = *p++;
	    histogram[1][g] += 2;
	    uint8_t r = *p++;
	    histogram[0][r] += 4;
	}
	for (int j = 0; j < image->width; j += 2) {
	    uint8_t b = *p++;
	    histogram[2][b] += 4;
	    uint8_t g = *p++;
	    histogram[1][g] += 2;
	}
    }
}

static void
image_histogram_xrgb32(const struct image *image, uint32_t histogram[3][256])
{
    g_assert(image->stride == image->width * 4);
    memset(histogram, 0, 3 * 256 * sizeof(uint32_t));
    const uint8_t *p = image->pixels;
    const uint8_t *pend = image->pixels + image->height * image->stride;
    while (p != pend) {
	uint8_t b = *p++;
	uint8_t g = *p++;
	uint8_t r = *p++;
	p++;
	histogram[0][r]++;
	histogram[1][g]++;
	histogram[2][b]++;
    }
}

void
image_histogram(const struct image *image, uint32_t histogram[3][256])
{
    switch (image->format) {
    case IMAGE_FORMAT_SGRBG8:
	image_histogram_sgrbg8(image, histogram);
	break;
    case IMAGE_FORMAT_XBGR32:
	image_histogram_xrgb32(image, histogram);
	break;
    default:
	g_assert_not_reached();
    }
}

void
image_white_balance_calibrate(const struct image *image,
	struct image_white_balance_reference *reference)
{
    uint32_t histogram[3][256];
    image_histogram(image, histogram);
    const uint32_t top_ignore = image->width * image->height / 1000;
    uint8_t top[3];
    for (int i = 0; i < 3; i++) {
	uint32_t acc = 0;
	uint8_t j;
	for (j = 255; acc < top_ignore && j > 0; j--) {
	    acc += histogram[i][j];
	}
	top[i] = j;
    }
    reference->r = top[0];
    reference->g = top[1];
    reference->b = top[2];
}

static void
image_white_balance_factors(
	const struct image_white_balance_reference *reference,
	uint16_t *fr, uint16_t *fg, uint16_t *fb)
{
    uint8_t wr = reference->r;
    uint8_t wg = reference->g;
    uint8_t wb = reference->b;
    if (wr > wg && wr > wb) {
	*fr = 256;
	*fg = 256 * wr / wg;
	*fb = 256 * wr / wb;
    } else if (wg > wr && wg > wb) {
	*fr = 256 * wg / wr;
	*fg = 256;
	*fb = 256 * wg / wb;
    } else {
	*fr = 256 * wb / wr;
	*fg = 256 * wb / wg;
	*fb = 256;
    }
}

static void
image_white_balance_sgrbg8(struct image *image,
	const struct image_white_balance_reference *reference)
{
    g_assert(image->width % 16 == 0);
    g_assert(image->height % 2 == 0);
    g_assert(image->stride == image->width);
    uint16_t fr, fg, fb;
    image_white_balance_factors(reference, &fr, &fg, &fb);
    u8x16 *p = (u8x16 *) image->pixels;
    const u8x16 zero = { 0 };
    const u8x16 ex0 = { 0, 16, 1, 17, 2, 18, 3, 19,
	4, 20, 5, 21, 6, 22, 7, 23 };
    const u8x16 ex1 = { 8, 24, 9, 25, 10, 26, 11, 27,
	12, 28, 13, 29, 14, 30, 15, 31 };
    const u16x8 mulo = { fg, fr, fg, fr, fg, fr, fg, fr };
    const u16x8 mule = { fb, fg, fb, fg, fb, fg, fb, fg };
    const u8x16 imp = { 0, 2, 4, 6, 8, 10, 12, 14,
	16, 18, 20, 22, 24, 26, 28, 30 };
    for (int i = 0; i < image->height; i += 2) {
	for (int j = 0; j < image->width; j += 16) {
	    u8x16 pix4 = *p;
	    u16x8 pix2_0 = (u16x8) __builtin_shuffle(pix4, zero, ex0);
	    u16x8 pix2_0_ovf = __builtin_ia32_pmulhuw128(pix2_0, mulo)
		!= (u16x8) zero;
	    pix2_0 = ((pix2_0 * mulo) | pix2_0_ovf) / 256;
	    u16x8 pix2_1 = (u16x8) __builtin_shuffle(pix4, zero, ex1);
	    u16x8 pix2_1_ovf = __builtin_ia32_pmulhuw128(pix2_1, mulo)
		!= (u16x8) zero;
	    pix2_1 = ((pix2_1 * mulo) | pix2_1_ovf) / 256;
	    pix4 = __builtin_shuffle((u8x16) pix2_0, (u8x16) pix2_1, imp);
	    *p++ = pix4;
	}
	for (int j = 0; j < image->width; j += 16) {
	    u8x16 pix4 = *p;
	    u16x8 pix2_0 = (u16x8) __builtin_shuffle(pix4, zero, ex0);
	    u16x8 pix2_0_ovf = __builtin_ia32_pmulhuw128(pix2_0, mule)
		!= (u16x8) zero;
	    pix2_0 = ((pix2_0 * mule) | pix2_0_ovf) / 256;
	    u16x8 pix2_1 = (u16x8) __builtin_shuffle(pix4, zero, ex1);
	    u16x8 pix2_1_ovf = __builtin_ia32_pmulhuw128(pix2_1, mule)
		!= (u16x8) zero;
	    pix2_1 = ((pix2_1 * mule) | pix2_1_ovf) / 256;
	    pix4 = __builtin_shuffle((u8x16) pix2_0, (u8x16) pix2_1, imp);
	    *p++ = pix4;
	}
    }
}

static void
image_white_balance_xbgr32(struct image *image,
	    const struct image_white_balance_reference *reference)
{
    g_assert(image->width % 4 == 0);
    g_assert(image->stride == image->width * 4);
    uint16_t fr, fg, fb;
    image_white_balance_factors(reference, &fr, &fg, &fb);
    u8x16 *p = (u8x16 *) image->pixels;
    u8x16 *pend = (u8x16 *) (image->pixels + image->height * image->stride);
    const u8x16 zero = { 0 };
    const u8x16 ex0 = { 0, 16, 1, 17, 2, 18, 3, 19,
	4, 20, 5, 21, 6, 22, 7, 23 };
    const u8x16 ex1 = { 8, 24, 9, 25, 10, 26, 11, 27,
	12, 28, 13, 29, 14, 30, 15, 31 };
    const u16x8 mul = { fb, fg, fr, 255, fb, fg, fr, 255 };
    const u8x16 imp = { 0, 2, 4, 6, 8, 10, 12, 14,
	16, 18, 20, 22, 24, 26, 28, 30 };
    while (p != pend) {
	u8x16 pix4 = *p;
	u16x8 pix2_0 = (u16x8) __builtin_shuffle(pix4, zero, ex0);
	u16x8 pix2_0_ovf = __builtin_ia32_pmulhuw128(pix2_0, mul)
	    != (u16x8) zero;
	pix2_0 = ((pix2_0 * mul) | pix2_0_ovf) / 256;
	u16x8 pix2_1 = (u16x8) __builtin_shuffle(pix4, zero, ex1);
	u16x8 pix2_1_ovf = __builtin_ia32_pmulhuw128(pix2_1, mul)
	    != (u16x8) zero;
	pix2_1 = ((pix2_1 * mul) | pix2_1_ovf) / 256;
	pix4 = __builtin_shuffle((u8x16) pix2_0, (u8x16) pix2_1, imp);
	*p++ = pix4;
    }
}

void
image_white_balance(struct image *image,
	const struct image_white_balance_reference *reference)
{
    switch (image->format) {
    case IMAGE_FORMAT_SGRBG8:
	image_white_balance_sgrbg8(image, reference);
	break;
    case IMAGE_FORMAT_XBGR32:
	image_white_balance_xbgr32(image, reference);
	break;
    default:
	g_assert_not_reached();
    }
}

static void
image_convert_to_xbgr32_from_sgrbg8(struct image *dst,
	const struct image *src)
{
    /*
     * Compute missing value using an average of its neighbours.
     * Input pattern:
     * G R G R G R
     * B G B G B G
     * G R G R G R
     * B G B G B G
     */
    g_assert(dst->width == src->width
	    && dst->height == src->height);
    const int in_stride = src->stride;
    const int out_stride = dst->stride;
    int width = src->width;
    int height = src->height;
    /* Skip first line and column. */
    const uint8_t *in = src->pixels + in_stride + 1;
    uint8_t *out = dst->pixels + out_stride + 4;
    /* Loop over lines. */
    for (int i = 1; i < height - 1; i += 2) {
	/* Even lines. */
	const uint8_t *in_stop = in + (width - 2);
	while (in != in_stop) {
	    *out++ = (in[-1] + in[+1] + 1) >> 1;                 /* B */
	    *out++ = in[0];                                      /* G */
	    *out++ = (in[-in_stride] + in[+in_stride] + 1) >> 1; /* R */
	    *out++ = 255;                                        /* A */
	    in++;
	    *out++ = in[0];                                      /* B */
	    *out++ = (in[-in_stride] + in[+in_stride]            /* G */
		    + in[-1] + in[+1] + 2) >> 2;
	    *out++ = (in[-in_stride - 1] + in[-in_stride + 1]    /* R */
		    + in[+in_stride - 1] + in[+in_stride + 1] + 2) >> 2;
	    *out++ = 255;                                        /* A */
	    in++;
	}
	/* Fill first and last pixels. */
	out[-(width - 1) * 4 + 0] = out[-(width - 2) * 4 + 0];
	out[-(width - 1) * 4 + 1] = out[-(width - 2) * 4 + 1];
	out[-(width - 1) * 4 + 2] = out[-(width - 2) * 4 + 2];
	out[-(width - 1) * 4 + 3] = out[-(width - 2) * 4 + 3];
	out[0] = out[-4];
	out[1] = out[-3];
	out[2] = out[-2];
	out[3] = out[-1];
	out += out_stride - (width - 2) * 4;
	in += in_stride - (width - 2);
	/* Odd lines. */
	in_stop = in + (width - 2);
	while (in != in_stop) {
	    *out++ = (in[-in_stride - 1] + in[-in_stride + 1]    /* B */
		    + in[+in_stride - 1] + in[+in_stride + 1] + 2) >> 2;
	    *out++ = (in[-in_stride] + in[+in_stride]            /* G */
		    + in[-1] + in[+1] + 2) >> 2;
	    *out++ = in[0];                                      /* R */
	    *out++ = 255;                                        /* A */
	    in++;
	    *out++ = (in[-in_stride] + in[+in_stride] + 1) >> 1; /* B */
	    *out++ = in[0];                                      /* G */
	    *out++ = (in[-1] + in[+1] + 1) >> 1;                 /* R */
	    *out++ = 255;                                        /* A */
	    in++;
	}
	/* Fill first and last pixels. */
	out[-(width - 1) * 4 + 0] = out[-(width - 2) * 4 + 0];
	out[-(width - 1) * 4 + 1] = out[-(width - 2) * 4 + 1];
	out[-(width - 1) * 4 + 2] = out[-(width - 2) * 4 + 2];
	out[-(width - 1) * 4 + 3] = out[-(width - 2) * 4 + 3];
	out[0] = out[-4];
	out[1] = out[-3];
	out[2] = out[-2];
	out[3] = out[-1];
	out += out_stride - (width - 2) * 4;
	in += in_stride - (width - 2);
    }
    /* Last line. */
    out -= 4;
    memcpy(out, out - out_stride, width * 4);
    /* First line. */
    out -= (height - 1) * out_stride;
    memcpy(out, out + out_stride, width * 4);
}

void
image_convert(struct image *dst, const struct image *src)
{
    g_assert(dst->width == src->width
	    && dst->height == src->height);
    if (dst->format == src->format) {
	g_assert(dst->stride == src->stride);
	memcpy(dst->pixels, src->pixels, src->stride * src->height);
    } else {
	switch (src->format) {
	case IMAGE_FORMAT_SGRBG8:
	    switch (dst->format) {
	    case IMAGE_FORMAT_XBGR32:
		image_convert_to_xbgr32_from_sgrbg8(dst, src);
		break;
	    default:
		g_assert_not_reached();
	    }
	    break;
	default:
	    g_assert_not_reached();
	}
    }
}

bool
image_save(const struct image *image, const char *name, GError **error)
{
    g_return_val_if_fail(error == NULL || *error == NULL, false);
    png_image pimage;
    memset(&pimage, 0, sizeof(pimage));
    pimage.version = PNG_IMAGE_VERSION;
    pimage.width = image->width;
    pimage.height = image->height;
    switch(image->format) {
    case IMAGE_FORMAT_SGRBG8:
	pimage.format = PNG_FORMAT_GRAY;
	break;
    case IMAGE_FORMAT_XBGR32:
	pimage.format = PNG_FORMAT_BGRA;
	break;
    default:
	g_assert_not_reached();
    }
    int r = png_image_write_to_file(&pimage, name, 0,
	    image->pixels, image->stride, NULL);
    if (r == 0) {
	g_set_error(error, IMAGE_ERROR, IMAGE_ERROR_SAVE,
		"can not write image: %s", pimage.message);
	return false;
    }
    return true;
}