textures, overlay images, libimago
[demo_prior] / libs / imago2 / src / imago2.h
1 /*
2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2020 John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef IMAGO2_H_
20 #define IMAGO2_H_
21
22 #include <stdio.h>
23
24 #ifdef __cplusplus
25 #define IMG_OPTARG(arg, val)    arg = val
26 #else
27 #define IMG_OPTARG(arg, val)    arg
28 #endif
29
30 /* XXX if you change this make sure to also change pack/unpack arrays in conv.c */
31 enum img_fmt {
32         IMG_FMT_GREY8,
33         IMG_FMT_RGB24,
34         IMG_FMT_RGBA32,
35         IMG_FMT_GREYF,
36         IMG_FMT_RGBF,
37         IMG_FMT_RGBAF,
38         IMG_FMT_BGRA32,
39         IMG_FMT_RGB565,
40
41         NUM_IMG_FMT
42 };
43
44 struct img_pixmap {
45         void *pixels;
46         int width, height;
47         enum img_fmt fmt;
48         int pixelsz;
49         char *name;
50 };
51
52 struct img_io {
53         void *uptr;     /* user-data */
54
55         size_t (*read)(void *buf, size_t bytes, void *uptr);
56         size_t (*write)(void *buf, size_t bytes, void *uptr);
57         long (*seek)(long offs, int whence, void *uptr);
58 };
59
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63
64 /* initialize the img_pixmap structure */
65 void img_init(struct img_pixmap *img);
66 /* destroys the img_pixmap structure, freeing the pixel buffer (if available)
67  * and any other memory held by the pixmap.
68  */
69 void img_destroy(struct img_pixmap *img);
70
71 /* convenience function that allocates an img_pixmap struct and then initializes it.
72  * returns null if the malloc fails.
73  */
74 struct img_pixmap *img_create(void);
75 /* frees a pixmap previously allocated with img_create (free followed by img_destroy) */
76 void img_free(struct img_pixmap *img);
77
78 int img_set_name(struct img_pixmap *img, const char *name);
79
80 /* set the image pixel format */
81 int img_set_format(struct img_pixmap *img, enum img_fmt fmt);
82
83 /* copies one pixmap to another.
84  * equivalent to: img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels)
85  */
86 int img_copy(struct img_pixmap *dest, struct img_pixmap *src);
87
88 /* allocates a pixel buffer of the specified dimensions and format, and copies the
89  * pixels given through the pix pointer into it.
90  * the pix pointer can be null, in which case there's no copy, just allocation.
91  *
92  * C++: fmt and pix have default parameters IMG_FMT_RGBA32 and null respectively.
93  */
94 int img_set_pixels(struct img_pixmap *img, int w, int h, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32), IMG_OPTARG(void *pix, 0));
95
96 /* Simplified image loading
97  * Loads the specified file, and returns a pointer to an array of pixels of the
98  * requested pixel format. The width and height of the image are returned through
99  * the xsz and ysz pointers.
100  * If the image cannot be loaded, the function returns null.
101  *
102  * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
103  */
104 void *img_load_pixels(const char *fname, int *xsz, int *ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
105
106 /* Simplified image saving
107  * Reads an array of pixels supplied through the pix pointer, of dimensions xsz
108  * and ysz, and pixel-format fmt, and saves it to a file.
109  * The output filetype is guessed by the filename suffix.
110  *
111  * C++: the format argument is optional and defaults to IMG_FMT_RGBA32
112  */
113 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, IMG_OPTARG(enum img_fmt fmt, IMG_FMT_RGBA32));
114
115 /* Frees the memory allocated by img_load_pixels */
116 void img_free_pixels(void *pix);
117
118 /* Loads an image file into the supplied pixmap */
119 int img_load(struct img_pixmap *img, const char *fname);
120 /* Saves the supplied pixmap to a file. The output filetype is guessed by the filename suffix */
121 int img_save(struct img_pixmap *img, const char *fname);
122
123 /* Reads an image from an open FILE* into the supplied pixmap */
124 int img_read_file(struct img_pixmap *img, FILE *fp);
125 /* Writes the supplied pixmap to an open FILE* */
126 int img_write_file(struct img_pixmap *img, FILE *fp);
127
128 /* Reads an image using user-defined file-i/o functions (see img_io_set_*) */
129 int img_read(struct img_pixmap *img, struct img_io *io);
130 /* Writes an image using user-defined file-i/o functions (see img_io_set_*) */
131 int img_write(struct img_pixmap *img, struct img_io *io);
132
133 /* Converts an image to the specified pixel format */
134 int img_convert(struct img_pixmap *img, enum img_fmt tofmt);
135
136 /* Converts an image from an integer pixel format to the corresponding floating point one */
137 int img_to_float(struct img_pixmap *img);
138 /* Converts an image from a floating point pixel format to the corresponding integer one */
139 int img_to_integer(struct img_pixmap *img);
140
141 /* Returns non-zero (true) if the supplied image is in a floating point pixel format */
142 int img_is_float(struct img_pixmap *img);
143 /* Returns non-zero (true) if the supplied image has an alpha channel */
144 int img_has_alpha(struct img_pixmap *img);
145 /* Returns non-zero (true) if the supplied image is greyscale */
146 int img_is_greyscale(struct img_pixmap *img);
147
148
149 /* don't use these for anything performance-critical */
150 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel);
151 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel);
152
153 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix);
154 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix);
155 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a);
156 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a);
157
158 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix);
159 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix);
160 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a);
161 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a);
162
163
164 /* OpenGL helper functions */
165
166 /* Returns the equivalent OpenGL "format" as expected by the 7th argument of glTexImage2D */
167 unsigned int img_fmt_glfmt(enum img_fmt fmt);
168 /* Returns the equivalent OpenGL "type" as expected by the 8th argument of glTexImage2D */
169 unsigned int img_fmt_gltype(enum img_fmt fmt);
170 /* Returns the equivalent OpenGL "internal format" as expected by the 3rd argument of glTexImage2D */
171 unsigned int img_fmt_glintfmt(enum img_fmt fmt);
172
173 /* Same as above, based on the pixel format of the supplied image */
174 unsigned int img_glfmt(struct img_pixmap *img);
175 unsigned int img_gltype(struct img_pixmap *img);
176 unsigned int img_glintfmt(struct img_pixmap *img);
177
178 /* Creates an OpenGL texture from the image, and returns the texture id, or 0 for failure */
179 unsigned int img_gltexture(struct img_pixmap *img);
180
181 /* Load an image and create an OpenGL texture out of it */
182 unsigned int img_gltexture_load(const char *fname);
183 unsigned int img_gltexture_read_file(FILE *fp);
184 unsigned int img_gltexture_read(struct img_io *io);
185
186 /* These functions can be used to fill an img_io struct before it's passed to
187  * one of the user-defined i/o image reading/writing functions (img_read/img_write).
188  *
189  * User-defined i/o functions:
190  *
191  * - size_t read_func(void *buffer, size_t bytes, void *user_ptr)
192  * Must try to fill the buffer with the specified number of bytes, and return
193  * the number of bytes actually read.
194  *
195  * - size_t write_func(void *buffer, size_t bytes, void *user_ptr)
196  * Must write the specified number of bytes from the supplied buffer and return
197  * the number of bytes actually written.
198  *
199  * - long seek_func(long offset, int whence, void *user_ptr)
200  * Must seek offset bytes from: the beginning of the file if whence is SEEK_SET,
201  * the current position if whence is SEEK_CUR, or the end of the file if whence is
202  * SEEK_END, and return the resulting file offset from the beginning of the file.
203  * (i.e. seek_func(0, SEEK_CUR, user_ptr); must be equivalent to an ftell).
204  *
205  * All three functions get the user-data pointer set through img_io_set_user_data
206  * as their last argument.
207  *
208  * Note: obviously you don't need to set a write function if you're only going
209  * to call img_read, or the read and seek function if you're only going to call
210  * img_write.
211  *
212  * Note: if the user-supplied write function is buffered, make sure to flush
213  * (or close the file) after img_write returns.
214  */
215 void img_io_set_user_data(struct img_io *io, void *uptr);
216 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*));
217 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*));
218 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*));
219
220
221 #ifdef __cplusplus
222 }
223 #endif
224
225
226 #endif  /* IMAGO_H_ */