istereo2

annotate libs/imago2/imago2.c @ 4:d4fed8aac9a6

fixed all sideways crap in the effect code fixed fov on all orientations fixed inverted tangent vector in bump mapped version
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 21 Sep 2015 21:12:36 +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 #include <stdio.h>
nuclear@2 20 #include <stdlib.h>
nuclear@2 21 #include <string.h>
nuclear@2 22 #include "imago2.h"
nuclear@2 23 #include "ftype_module.h"
nuclear@2 24
nuclear@2 25 static int pixel_size(enum img_fmt fmt);
nuclear@2 26 static size_t def_read(void *buf, size_t bytes, void *uptr);
nuclear@2 27 static size_t def_write(void *buf, size_t bytes, void *uptr);
nuclear@2 28 static long def_seek(long offset, int whence, void *uptr);
nuclear@2 29
nuclear@2 30
nuclear@2 31 void img_init(struct img_pixmap *img)
nuclear@2 32 {
nuclear@2 33 img->pixels = 0;
nuclear@2 34 img->width = img->height = 0;
nuclear@2 35 img->fmt = IMG_FMT_RGBA32;
nuclear@2 36 img->pixelsz = pixel_size(img->fmt);
nuclear@2 37 img->name = 0;
nuclear@2 38 }
nuclear@2 39
nuclear@2 40
nuclear@2 41 void img_destroy(struct img_pixmap *img)
nuclear@2 42 {
nuclear@2 43 free(img->pixels);
nuclear@2 44 img->pixels = 0; /* just in case... */
nuclear@2 45 img->width = img->height = 0xbadbeef;
nuclear@2 46 free(img->name);
nuclear@2 47 }
nuclear@2 48
nuclear@2 49 struct img_pixmap *img_create(void)
nuclear@2 50 {
nuclear@2 51 struct img_pixmap *p;
nuclear@2 52
nuclear@2 53 if(!(p = malloc(sizeof *p))) {
nuclear@2 54 return 0;
nuclear@2 55 }
nuclear@2 56 img_init(p);
nuclear@2 57 return p;
nuclear@2 58 }
nuclear@2 59
nuclear@2 60 void img_free(struct img_pixmap *img)
nuclear@2 61 {
nuclear@2 62 img_destroy(img);
nuclear@2 63 free(img);
nuclear@2 64 }
nuclear@2 65
nuclear@2 66 int img_set_name(struct img_pixmap *img, const char *name)
nuclear@2 67 {
nuclear@2 68 char *tmp;
nuclear@2 69
nuclear@2 70 if(!(tmp = malloc(strlen(name) + 1))) {
nuclear@2 71 return -1;
nuclear@2 72 }
nuclear@2 73 strcpy(tmp, name);
nuclear@2 74 img->name = tmp;
nuclear@2 75 return 0;
nuclear@2 76 }
nuclear@2 77
nuclear@2 78 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
nuclear@2 79 {
nuclear@2 80 if(img->pixels) {
nuclear@2 81 return img_convert(img, fmt);
nuclear@2 82 }
nuclear@2 83 img->fmt = fmt;
nuclear@2 84 return 0;
nuclear@2 85 }
nuclear@2 86
nuclear@2 87 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
nuclear@2 88 {
nuclear@2 89 return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
nuclear@2 90 }
nuclear@2 91
nuclear@2 92 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
nuclear@2 93 {
nuclear@2 94 void *newpix;
nuclear@2 95 int pixsz = pixel_size(fmt);
nuclear@2 96
nuclear@2 97 if(!(newpix = malloc(w * h * pixsz))) {
nuclear@2 98 return -1;
nuclear@2 99 }
nuclear@2 100
nuclear@2 101 if(pix) {
nuclear@2 102 memcpy(newpix, pix, w * h * pixsz);
nuclear@2 103 } else {
nuclear@2 104 memset(newpix, 0, w * h * pixsz);
nuclear@2 105 }
nuclear@2 106
nuclear@2 107 free(img->pixels);
nuclear@2 108 img->pixels = newpix;
nuclear@2 109 img->width = w;
nuclear@2 110 img->height = h;
nuclear@2 111 img->pixelsz = pixsz;
nuclear@2 112 img->fmt = fmt;
nuclear@2 113 return 0;
nuclear@2 114 }
nuclear@2 115
nuclear@2 116 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
nuclear@2 117 {
nuclear@2 118 struct img_pixmap img;
nuclear@2 119
nuclear@2 120 img_init(&img);
nuclear@2 121
nuclear@2 122 if(img_load(&img, fname) == -1) {
nuclear@2 123 return 0;
nuclear@2 124 }
nuclear@2 125 if(img.fmt != fmt) {
nuclear@2 126 if(img_convert(&img, fmt) == -1) {
nuclear@2 127 img_destroy(&img);
nuclear@2 128 return 0;
nuclear@2 129 }
nuclear@2 130 }
nuclear@2 131
nuclear@2 132 *xsz = img.width;
nuclear@2 133 *ysz = img.height;
nuclear@2 134 return img.pixels;
nuclear@2 135 }
nuclear@2 136
nuclear@2 137 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
nuclear@2 138 {
nuclear@2 139 struct img_pixmap img;
nuclear@2 140
nuclear@2 141 img_init(&img);
nuclear@2 142 img.fmt = fmt;
nuclear@2 143 img.name = (char*)fname;
nuclear@2 144 img.width = xsz;
nuclear@2 145 img.height = ysz;
nuclear@2 146 img.pixels = pix;
nuclear@2 147
nuclear@2 148 return img_save(&img, fname);
nuclear@2 149 }
nuclear@2 150
nuclear@2 151 void img_free_pixels(void *pix)
nuclear@2 152 {
nuclear@2 153 free(pix);
nuclear@2 154 }
nuclear@2 155
nuclear@2 156 int img_load(struct img_pixmap *img, const char *fname)
nuclear@2 157 {
nuclear@2 158 int res;
nuclear@2 159 FILE *fp;
nuclear@2 160
nuclear@2 161 if(!(fp = fopen(fname, "rb"))) {
nuclear@2 162 return -1;
nuclear@2 163 }
nuclear@2 164 res = img_read_file(img, fp);
nuclear@2 165 fclose(fp);
nuclear@2 166 return res;
nuclear@2 167 }
nuclear@2 168
nuclear@2 169 /* TODO implement filetype selection */
nuclear@2 170 int img_save(struct img_pixmap *img, const char *fname)
nuclear@2 171 {
nuclear@2 172 int res;
nuclear@2 173 FILE *fp;
nuclear@2 174
nuclear@2 175 img_set_name(img, fname);
nuclear@2 176
nuclear@2 177 if(!(fp = fopen(fname, "wb"))) {
nuclear@2 178 return -1;
nuclear@2 179 }
nuclear@2 180 res = img_write_file(img, fp);
nuclear@2 181 fclose(fp);
nuclear@2 182 return res;
nuclear@2 183 }
nuclear@2 184
nuclear@2 185 int img_read_file(struct img_pixmap *img, FILE *fp)
nuclear@2 186 {
nuclear@2 187 struct img_io io = {0, def_read, def_write, def_seek};
nuclear@2 188
nuclear@2 189 io.uptr = fp;
nuclear@2 190 return img_read(img, &io);
nuclear@2 191 }
nuclear@2 192
nuclear@2 193 int img_write_file(struct img_pixmap *img, FILE *fp)
nuclear@2 194 {
nuclear@2 195 struct img_io io = {0, def_read, def_write, def_seek};
nuclear@2 196
nuclear@2 197 io.uptr = fp;
nuclear@2 198 return img_write(img, &io);
nuclear@2 199 }
nuclear@2 200
nuclear@2 201 int img_read(struct img_pixmap *img, struct img_io *io)
nuclear@2 202 {
nuclear@2 203 struct ftype_module *mod;
nuclear@2 204
nuclear@2 205 if((mod = img_find_format_module(io))) {
nuclear@2 206 return mod->read(img, io);
nuclear@2 207 }
nuclear@2 208 return -1;
nuclear@2 209 }
nuclear@2 210
nuclear@2 211 int img_write(struct img_pixmap *img, struct img_io *io)
nuclear@2 212 {
nuclear@2 213 struct ftype_module *mod;
nuclear@2 214
nuclear@2 215 if(!img->name || !(mod = img_guess_format(img->name))) {
nuclear@2 216 /* TODO throw some sort of warning? */
nuclear@2 217 /* TODO implement some sort of module priority or let the user specify? */
nuclear@2 218 if(!(mod = img_get_module(0))) {
nuclear@2 219 return -1;
nuclear@2 220 }
nuclear@2 221 }
nuclear@2 222
nuclear@2 223 return mod->write(img, io);
nuclear@2 224 }
nuclear@2 225
nuclear@2 226 int img_to_float(struct img_pixmap *img)
nuclear@2 227 {
nuclear@2 228 enum img_fmt targ_fmt;
nuclear@2 229
nuclear@2 230 switch(img->fmt) {
nuclear@2 231 case IMG_FMT_GREY8:
nuclear@2 232 targ_fmt = IMG_FMT_GREYF;
nuclear@2 233 break;
nuclear@2 234
nuclear@2 235 case IMG_FMT_RGB24:
nuclear@2 236 targ_fmt = IMG_FMT_RGBF;
nuclear@2 237 break;
nuclear@2 238
nuclear@2 239 case IMG_FMT_RGBA32:
nuclear@2 240 targ_fmt = IMG_FMT_RGBAF;
nuclear@2 241 break;
nuclear@2 242
nuclear@2 243 default:
nuclear@2 244 return 0; /* already float */
nuclear@2 245 }
nuclear@2 246
nuclear@2 247 return img_convert(img, targ_fmt);
nuclear@2 248 }
nuclear@2 249
nuclear@2 250 int img_to_integer(struct img_pixmap *img)
nuclear@2 251 {
nuclear@2 252 enum img_fmt targ_fmt;
nuclear@2 253
nuclear@2 254 switch(img->fmt) {
nuclear@2 255 case IMG_FMT_GREYF:
nuclear@2 256 targ_fmt = IMG_FMT_GREY8;
nuclear@2 257 break;
nuclear@2 258
nuclear@2 259 case IMG_FMT_RGBF:
nuclear@2 260 targ_fmt = IMG_FMT_RGB24;
nuclear@2 261 break;
nuclear@2 262
nuclear@2 263 case IMG_FMT_RGBAF:
nuclear@2 264 targ_fmt = IMG_FMT_RGBA32;
nuclear@2 265 break;
nuclear@2 266
nuclear@2 267 default:
nuclear@2 268 return 0; /* already integer */
nuclear@2 269 }
nuclear@2 270
nuclear@2 271 return img_convert(img, targ_fmt);
nuclear@2 272 }
nuclear@2 273
nuclear@2 274 int img_is_float(struct img_pixmap *img)
nuclear@2 275 {
nuclear@2 276 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
nuclear@2 277 }
nuclear@2 278
nuclear@2 279 int img_has_alpha(struct img_pixmap *img)
nuclear@2 280 {
nuclear@2 281 return img->fmt >= IMG_FMT_GREY8 && img->fmt <= IMG_FMT_RGBA32;
nuclear@2 282 }
nuclear@2 283
nuclear@2 284 void img_io_set_user_data(struct img_io *io, void *uptr)
nuclear@2 285 {
nuclear@2 286 io->uptr = uptr;
nuclear@2 287 }
nuclear@2 288
nuclear@2 289 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
nuclear@2 290 {
nuclear@2 291 io->read = read;
nuclear@2 292 }
nuclear@2 293
nuclear@2 294 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
nuclear@2 295 {
nuclear@2 296 io->write = write;
nuclear@2 297 }
nuclear@2 298
nuclear@2 299 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
nuclear@2 300 {
nuclear@2 301 io->seek = seek;
nuclear@2 302 }
nuclear@2 303
nuclear@2 304
nuclear@2 305 static int pixel_size(enum img_fmt fmt)
nuclear@2 306 {
nuclear@2 307 switch(fmt) {
nuclear@2 308 case IMG_FMT_GREY8:
nuclear@2 309 return 1;
nuclear@2 310 case IMG_FMT_RGB24:
nuclear@2 311 return 3;
nuclear@2 312 case IMG_FMT_RGBA32:
nuclear@2 313 return 4;
nuclear@2 314 case IMG_FMT_GREYF:
nuclear@2 315 return sizeof(float);
nuclear@2 316 case IMG_FMT_RGBF:
nuclear@2 317 return 3 * sizeof(float);
nuclear@2 318 case IMG_FMT_RGBAF:
nuclear@2 319 return 4 * sizeof(float);
nuclear@2 320 default:
nuclear@2 321 break;
nuclear@2 322 }
nuclear@2 323 return 0;
nuclear@2 324 }
nuclear@2 325
nuclear@2 326 static size_t def_read(void *buf, size_t bytes, void *uptr)
nuclear@2 327 {
nuclear@2 328 return uptr ? fread(buf, 1, bytes, uptr) : 0;
nuclear@2 329 }
nuclear@2 330
nuclear@2 331 static size_t def_write(void *buf, size_t bytes, void *uptr)
nuclear@2 332 {
nuclear@2 333 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
nuclear@2 334 }
nuclear@2 335
nuclear@2 336 static long def_seek(long offset, int whence, void *uptr)
nuclear@2 337 {
nuclear@2 338 if(!uptr || fseek(uptr, offset, whence) == -1) {
nuclear@2 339 return -1;
nuclear@2 340 }
nuclear@2 341 return ftell(uptr);
nuclear@2 342 }
nuclear@2 343