summaryrefslogtreecommitdiff
path: root/image.c
blob: 9ab12c927eddebaece2c4c0d3e759d009f4d04f9 (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
/* 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 <assert.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>

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

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) {
	utils_fatal("memory exhausted");
    }
    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_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
     */
    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)
{
    assert(dst->width == src->width
	    && dst->height == src->height);
    if (dst->format == src->format) {
	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:
		assert(0);
	    }
	    break;
	default:
	    assert(0);
	}
    }
}

void
image_save(const struct image *image, const char *name)
{
    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:
	assert(0);
    }
    int r = png_image_write_to_file(&pimage, name, 0,
	    image->pixels, image->stride, NULL);
    if (r == 0)
	utils_fatal("can not write image: %s", pimage.message);
}