summaryrefslogtreecommitdiff
path: root/src/image.h
blob: 97046763dc3c1f7d90d77b1027388c2dcf867ebf (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
#ifndef image_h
#define image_h
/* 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 <glib.h>
#include <stdbool.h>
#include <stdint.h>

/* Error domain for image related errors. */
#define IMAGE_ERROR image_error_quark()

/* Image format. */
enum image_format
{
#define IMAGE_FORMAT_FOURCC(a, b, c, d) \
    (((uint32_t)(a)) | ((uint32_t)(b) << 8) \
     | ((uint32_t)(c) << 16) | ((uint32_t)(d) << 24))
    /* Bayer image format:
     * G R G R
     * B G B G */
    IMAGE_FORMAT_SGRBG8 = IMAGE_FORMAT_FOURCC('B', 'R', 'B', 'G'),
    /* RGB 32 bits, B in LSB. */
    IMAGE_FORMAT_XBGR32 = IMAGE_FORMAT_FOURCC('X', 'R', '2', '4'),
};

struct image;

/* Image release callback. */
typedef void (*image_release_f)(struct image *image, void *user_data);

/* Image with attached information. */
struct image {
    /* Image width. */
    int width;
    /* Image height. */
    int height;
    /* Byte offset between a line and the next line. */
    int stride;
    /* Image format. */
    enum image_format format;
    /* Frame buffer. */
    uint8_t *pixels;
    /* Number of reference to this image. */
    int refs;
    /* Release callback. */
    image_release_f release;
    /* Release callback user data. */
    void *release_user_data;
};

/* Reference for white balance. */
struct image_white_balance_reference {
    /* White (maximum of each component). */
    uint8_t r, g, b;
};

/* Error codes. */
enum ImageError {
    /* Error writing an image to disk. */
    IMAGE_ERROR_SAVE,
};

/* Return quark of error domain for image related errors. */
GQuark
image_error_quark(void);

/* Allocate an image using system allocator. */
struct image *
image_new(int width, int height, int stride, enum image_format format);

/* Add a reference to an image. */
void
image_ref(struct image *image);

/* Release a reference to an image. */
void
image_unref(struct image *image);

/* Compute histogram(s), should provide an uint32_t[3][256] array. */
void
image_histogram(const struct image *image, uint32_t histogram[3][256]);

/* Calibrate for white balance. */
void
image_white_balance_calibrate(const struct image *image,
	struct image_white_balance_reference *reference);

/* Apply the given white balance. */
void
image_white_balance(struct image *image,
	const struct image_white_balance_reference *reference);

/* Copy an image to another one, converting format. */
void
image_convert(struct image *dst, const struct image *src);

/* Save image to PNG. */
bool
image_save(const struct image *image, const char *name, GError **error);

#endif /* image_h */