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