istereo2

annotate libs/imago2/imago2.h @ 13:ea928c313344

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