dungeon_crawler

annotate prototype/imago2/imago2.c @ 80:a373b36ffc17

better
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 27 Oct 2012 01:59:39 +0300
parents
children
rev   line source
nuclear@67 1 /*
nuclear@67 2 libimago - a multi-format image file input/output library.
nuclear@67 3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org>
nuclear@67 4
nuclear@67 5 This program is free software: you can redistribute it and/or modify
nuclear@67 6 it under the terms of the GNU Lesser General Public License as published
nuclear@67 7 by the Free Software Foundation, either version 3 of the License, or
nuclear@67 8 (at your option) any later version.
nuclear@67 9
nuclear@67 10 This program is distributed in the hope that it will be useful,
nuclear@67 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@67 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@67 13 GNU Lesser General Public License for more details.
nuclear@67 14
nuclear@67 15 You should have received a copy of the GNU Lesser General Public License
nuclear@67 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@67 17 */
nuclear@67 18
nuclear@67 19 #include <stdio.h>
nuclear@67 20 #include <stdlib.h>
nuclear@67 21 #include <string.h>
nuclear@67 22 #include "imago2.h"
nuclear@67 23 #include "ftype_module.h"
nuclear@67 24
nuclear@67 25 static int pixel_size(enum img_fmt fmt);
nuclear@67 26 static size_t def_read(void *buf, size_t bytes, void *uptr);
nuclear@67 27 static size_t def_write(void *buf, size_t bytes, void *uptr);
nuclear@67 28 static long def_seek(long offset, int whence, void *uptr);
nuclear@67 29
nuclear@67 30
nuclear@67 31 void img_init(struct img_pixmap *img)
nuclear@67 32 {
nuclear@67 33 img->pixels = 0;
nuclear@67 34 img->width = img->height = 0;
nuclear@67 35 img->fmt = IMG_FMT_RGBA32;
nuclear@67 36 img->pixelsz = pixel_size(img->fmt);
nuclear@67 37 img->name = 0;
nuclear@67 38 }
nuclear@67 39
nuclear@67 40
nuclear@67 41 void img_destroy(struct img_pixmap *img)
nuclear@67 42 {
nuclear@67 43 free(img->pixels);
nuclear@67 44 img->pixels = 0; /* just in case... */
nuclear@67 45 img->width = img->height = 0xbadbeef;
nuclear@67 46 free(img->name);
nuclear@67 47 }
nuclear@67 48
nuclear@67 49 struct img_pixmap *img_create(void)
nuclear@67 50 {
nuclear@67 51 struct img_pixmap *p;
nuclear@67 52
nuclear@67 53 if(!(p = malloc(sizeof *p))) {
nuclear@67 54 return 0;
nuclear@67 55 }
nuclear@67 56 img_init(p);
nuclear@67 57 return p;
nuclear@67 58 }
nuclear@67 59
nuclear@67 60 void img_free(struct img_pixmap *img)
nuclear@67 61 {
nuclear@67 62 img_destroy(img);
nuclear@67 63 free(img);
nuclear@67 64 }
nuclear@67 65
nuclear@67 66 int img_set_name(struct img_pixmap *img, const char *name)
nuclear@67 67 {
nuclear@67 68 char *tmp;
nuclear@67 69
nuclear@67 70 if(!(tmp = malloc(strlen(name) + 1))) {
nuclear@67 71 return -1;
nuclear@67 72 }
nuclear@67 73 strcpy(tmp, name);
nuclear@67 74 img->name = tmp;
nuclear@67 75 return 0;
nuclear@67 76 }
nuclear@67 77
nuclear@67 78 int img_set_format(struct img_pixmap *img, enum img_fmt fmt)
nuclear@67 79 {
nuclear@67 80 if(img->pixels) {
nuclear@67 81 return img_convert(img, fmt);
nuclear@67 82 }
nuclear@67 83 img->fmt = fmt;
nuclear@67 84 return 0;
nuclear@67 85 }
nuclear@67 86
nuclear@67 87 int img_copy(struct img_pixmap *dest, struct img_pixmap *src)
nuclear@67 88 {
nuclear@67 89 return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels);
nuclear@67 90 }
nuclear@67 91
nuclear@67 92 int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix)
nuclear@67 93 {
nuclear@67 94 void *newpix;
nuclear@67 95 int pixsz = pixel_size(fmt);
nuclear@67 96
nuclear@67 97 if(!(newpix = malloc(w * h * pixsz))) {
nuclear@67 98 return -1;
nuclear@67 99 }
nuclear@67 100
nuclear@67 101 if(pix) {
nuclear@67 102 memcpy(newpix, pix, w * h * pixsz);
nuclear@67 103 } else {
nuclear@67 104 memset(newpix, 0, w * h * pixsz);
nuclear@67 105 }
nuclear@67 106
nuclear@67 107 free(img->pixels);
nuclear@67 108 img->pixels = newpix;
nuclear@67 109 img->width = w;
nuclear@67 110 img->height = h;
nuclear@67 111 img->pixelsz = pixsz;
nuclear@67 112 img->fmt = fmt;
nuclear@67 113 return 0;
nuclear@67 114 }
nuclear@67 115
nuclear@67 116 void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt)
nuclear@67 117 {
nuclear@67 118 struct img_pixmap img;
nuclear@67 119
nuclear@67 120 img_init(&img);
nuclear@67 121
nuclear@67 122 if(img_load(&img, fname) == -1) {
nuclear@67 123 return 0;
nuclear@67 124 }
nuclear@67 125 if(img.fmt != fmt) {
nuclear@67 126 if(img_convert(&img, fmt) == -1) {
nuclear@67 127 img_destroy(&img);
nuclear@67 128 return 0;
nuclear@67 129 }
nuclear@67 130 }
nuclear@67 131
nuclear@67 132 *xsz = img.width;
nuclear@67 133 *ysz = img.height;
nuclear@67 134 return img.pixels;
nuclear@67 135 }
nuclear@67 136
nuclear@67 137 int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt)
nuclear@67 138 {
nuclear@67 139 struct img_pixmap img;
nuclear@67 140
nuclear@67 141 img_init(&img);
nuclear@67 142 img.fmt = fmt;
nuclear@67 143 img.name = (char*)fname;
nuclear@67 144 img.width = xsz;
nuclear@67 145 img.height = ysz;
nuclear@67 146 img.pixels = pix;
nuclear@67 147
nuclear@67 148 return img_save(&img, fname);
nuclear@67 149 }
nuclear@67 150
nuclear@67 151 void img_free_pixels(void *pix)
nuclear@67 152 {
nuclear@67 153 free(pix);
nuclear@67 154 }
nuclear@67 155
nuclear@67 156 int img_load(struct img_pixmap *img, const char *fname)
nuclear@67 157 {
nuclear@67 158 int res;
nuclear@67 159 FILE *fp;
nuclear@67 160
nuclear@67 161 if(!(fp = fopen(fname, "rb"))) {
nuclear@67 162 return -1;
nuclear@67 163 }
nuclear@67 164 res = img_read_file(img, fp);
nuclear@67 165 fclose(fp);
nuclear@67 166 return res;
nuclear@67 167 }
nuclear@67 168
nuclear@67 169 /* TODO implement filetype selection */
nuclear@67 170 int img_save(struct img_pixmap *img, const char *fname)
nuclear@67 171 {
nuclear@67 172 int res;
nuclear@67 173 FILE *fp;
nuclear@67 174
nuclear@67 175 img_set_name(img, fname);
nuclear@67 176
nuclear@67 177 if(!(fp = fopen(fname, "wb"))) {
nuclear@67 178 return -1;
nuclear@67 179 }
nuclear@67 180 res = img_write_file(img, fp);
nuclear@67 181 fclose(fp);
nuclear@67 182 return res;
nuclear@67 183 }
nuclear@67 184
nuclear@67 185 int img_read_file(struct img_pixmap *img, FILE *fp)
nuclear@67 186 {
nuclear@67 187 struct img_io io = {0, def_read, def_write, def_seek};
nuclear@67 188
nuclear@67 189 io.uptr = fp;
nuclear@67 190 return img_read(img, &io);
nuclear@67 191 }
nuclear@67 192
nuclear@67 193 int img_write_file(struct img_pixmap *img, FILE *fp)
nuclear@67 194 {
nuclear@67 195 struct img_io io = {0, def_read, def_write, def_seek};
nuclear@67 196
nuclear@67 197 io.uptr = fp;
nuclear@67 198 return img_write(img, &io);
nuclear@67 199 }
nuclear@67 200
nuclear@67 201 int img_read(struct img_pixmap *img, struct img_io *io)
nuclear@67 202 {
nuclear@67 203 struct ftype_module *mod;
nuclear@67 204
nuclear@67 205 if((mod = img_find_format_module(io))) {
nuclear@67 206 return mod->read(img, io);
nuclear@67 207 }
nuclear@67 208 return -1;
nuclear@67 209 }
nuclear@67 210
nuclear@67 211 int img_write(struct img_pixmap *img, struct img_io *io)
nuclear@67 212 {
nuclear@67 213 struct ftype_module *mod;
nuclear@67 214
nuclear@67 215 if(!img->name || !(mod = img_guess_format(img->name))) {
nuclear@67 216 /* TODO throw some sort of warning? */
nuclear@67 217 /* TODO implement some sort of module priority or let the user specify? */
nuclear@67 218 if(!(mod = img_get_module(0))) {
nuclear@67 219 return -1;
nuclear@67 220 }
nuclear@67 221 }
nuclear@67 222
nuclear@67 223 return mod->write(img, io);
nuclear@67 224 }
nuclear@67 225
nuclear@67 226 int img_to_float(struct img_pixmap *img)
nuclear@67 227 {
nuclear@67 228 enum img_fmt targ_fmt;
nuclear@67 229
nuclear@67 230 switch(img->fmt) {
nuclear@67 231 case IMG_FMT_GREY8:
nuclear@67 232 targ_fmt = IMG_FMT_GREYF;
nuclear@67 233 break;
nuclear@67 234
nuclear@67 235 case IMG_FMT_RGB24:
nuclear@67 236 targ_fmt = IMG_FMT_RGBF;
nuclear@67 237 break;
nuclear@67 238
nuclear@67 239 case IMG_FMT_RGBA32:
nuclear@67 240 targ_fmt = IMG_FMT_RGBAF;
nuclear@67 241 break;
nuclear@67 242
nuclear@67 243 default:
nuclear@67 244 return 0; /* already float */
nuclear@67 245 }
nuclear@67 246
nuclear@67 247 return img_convert(img, targ_fmt);
nuclear@67 248 }
nuclear@67 249
nuclear@67 250 int img_to_integer(struct img_pixmap *img)
nuclear@67 251 {
nuclear@67 252 enum img_fmt targ_fmt;
nuclear@67 253
nuclear@67 254 switch(img->fmt) {
nuclear@67 255 case IMG_FMT_GREYF:
nuclear@67 256 targ_fmt = IMG_FMT_GREY8;
nuclear@67 257 break;
nuclear@67 258
nuclear@67 259 case IMG_FMT_RGBF:
nuclear@67 260 targ_fmt = IMG_FMT_RGB24;
nuclear@67 261 break;
nuclear@67 262
nuclear@67 263 case IMG_FMT_RGBAF:
nuclear@67 264 targ_fmt = IMG_FMT_RGBA32;
nuclear@67 265 break;
nuclear@67 266
nuclear@67 267 default:
nuclear@67 268 return 0; /* already integer */
nuclear@67 269 }
nuclear@67 270
nuclear@67 271 return img_convert(img, targ_fmt);
nuclear@67 272 }
nuclear@67 273
nuclear@67 274 int img_is_float(struct img_pixmap *img)
nuclear@67 275 {
nuclear@67 276 return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF;
nuclear@67 277 }
nuclear@67 278
nuclear@67 279 int img_has_alpha(struct img_pixmap *img)
nuclear@67 280 {
nuclear@67 281 if(img->fmt == IMG_FMT_RGBA32 || img->fmt == IMG_FMT_RGBAF) {
nuclear@67 282 return 1;
nuclear@67 283 }
nuclear@67 284 return 0;
nuclear@67 285 }
nuclear@67 286
nuclear@67 287
nuclear@67 288 void img_setpixel(struct img_pixmap *img, int x, int y, void *pixel)
nuclear@67 289 {
nuclear@67 290 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
nuclear@67 291 memcpy(dest, pixel, img->pixelsz);
nuclear@67 292 }
nuclear@67 293
nuclear@67 294 void img_getpixel(struct img_pixmap *img, int x, int y, void *pixel)
nuclear@67 295 {
nuclear@67 296 char *dest = (char*)img->pixels + (y * img->width + x) * img->pixelsz;
nuclear@67 297 memcpy(pixel, dest, img->pixelsz);
nuclear@67 298 }
nuclear@67 299
nuclear@67 300 void img_setpixel1i(struct img_pixmap *img, int x, int y, int pix)
nuclear@67 301 {
nuclear@67 302 img_setpixel4i(img, x, y, pix, pix, pix, pix);
nuclear@67 303 }
nuclear@67 304
nuclear@67 305 void img_setpixel1f(struct img_pixmap *img, int x, int y, float pix)
nuclear@67 306 {
nuclear@67 307 img_setpixel4f(img, x, y, pix, pix, pix, pix);
nuclear@67 308 }
nuclear@67 309
nuclear@67 310 void img_setpixel4i(struct img_pixmap *img, int x, int y, int r, int g, int b, int a)
nuclear@67 311 {
nuclear@67 312 if(img_is_float(img)) {
nuclear@67 313 img_setpixel4f(img, x, y, r / 255.0, g / 255.0, b / 255.0, a / 255.0);
nuclear@67 314 } else {
nuclear@67 315 unsigned char pixel[4];
nuclear@67 316 pixel[0] = r;
nuclear@67 317 pixel[1] = g;
nuclear@67 318 pixel[2] = b;
nuclear@67 319 pixel[3] = a;
nuclear@67 320
nuclear@67 321 img_setpixel(img, x, y, pixel);
nuclear@67 322 }
nuclear@67 323 }
nuclear@67 324
nuclear@67 325 void img_setpixel4f(struct img_pixmap *img, int x, int y, float r, float g, float b, float a)
nuclear@67 326 {
nuclear@67 327 if(img_is_float(img)) {
nuclear@67 328 float pixel[4];
nuclear@67 329 pixel[0] = r;
nuclear@67 330 pixel[1] = g;
nuclear@67 331 pixel[2] = b;
nuclear@67 332 pixel[3] = a;
nuclear@67 333
nuclear@67 334 img_setpixel(img, x, y, pixel);
nuclear@67 335 } else {
nuclear@67 336 img_setpixel4i(img, x, y, (int)(r * 255.0), (int)(g * 255.0), (int)(b * 255.0), (int)(a * 255.0));
nuclear@67 337 }
nuclear@67 338 }
nuclear@67 339
nuclear@67 340 void img_getpixel1i(struct img_pixmap *img, int x, int y, int *pix)
nuclear@67 341 {
nuclear@67 342 int junk[3];
nuclear@67 343 img_getpixel4i(img, x, y, pix, junk, junk + 1, junk + 2);
nuclear@67 344 }
nuclear@67 345
nuclear@67 346 void img_getpixel1f(struct img_pixmap *img, int x, int y, float *pix)
nuclear@67 347 {
nuclear@67 348 float junk[3];
nuclear@67 349 img_getpixel4f(img, x, y, pix, junk, junk + 1, junk + 2);
nuclear@67 350 }
nuclear@67 351
nuclear@67 352 void img_getpixel4i(struct img_pixmap *img, int x, int y, int *r, int *g, int *b, int *a)
nuclear@67 353 {
nuclear@67 354 if(img_is_float(img)) {
nuclear@67 355 float pixel[4] = {0, 0, 0, 0};
nuclear@67 356 img_getpixel(img, x, y, pixel);
nuclear@67 357 *r = pixel[0] * 255.0;
nuclear@67 358 *g = pixel[1] * 255.0;
nuclear@67 359 *b = pixel[2] * 255.0;
nuclear@67 360 *a = pixel[3] * 255.0;
nuclear@67 361 } else {
nuclear@67 362 unsigned char pixel[4];
nuclear@67 363 img_getpixel(img, x, y, pixel);
nuclear@67 364 *r = pixel[0];
nuclear@67 365 *g = pixel[1];
nuclear@67 366 *b = pixel[2];
nuclear@67 367 *a = pixel[3];
nuclear@67 368 }
nuclear@67 369 }
nuclear@67 370
nuclear@67 371 void img_getpixel4f(struct img_pixmap *img, int x, int y, float *r, float *g, float *b, float *a)
nuclear@67 372 {
nuclear@67 373 if(img_is_float(img)) {
nuclear@67 374 float pixel[4] = {0, 0, 0, 0};
nuclear@67 375 img_getpixel(img, x, y, pixel);
nuclear@67 376 *r = pixel[0];
nuclear@67 377 *g = pixel[1];
nuclear@67 378 *b = pixel[2];
nuclear@67 379 *a = pixel[3];
nuclear@67 380 } else {
nuclear@67 381 unsigned char pixel[4];
nuclear@67 382 img_getpixel(img, x, y, pixel);
nuclear@67 383 *r = pixel[0] / 255.0;
nuclear@67 384 *g = pixel[1] / 255.0;
nuclear@67 385 *b = pixel[2] / 255.0;
nuclear@67 386 *a = pixel[3] / 255.0;
nuclear@67 387 }
nuclear@67 388 }
nuclear@67 389
nuclear@67 390 void img_io_set_user_data(struct img_io *io, void *uptr)
nuclear@67 391 {
nuclear@67 392 io->uptr = uptr;
nuclear@67 393 }
nuclear@67 394
nuclear@67 395 void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*))
nuclear@67 396 {
nuclear@67 397 io->read = read;
nuclear@67 398 }
nuclear@67 399
nuclear@67 400 void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*))
nuclear@67 401 {
nuclear@67 402 io->write = write;
nuclear@67 403 }
nuclear@67 404
nuclear@67 405 void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*))
nuclear@67 406 {
nuclear@67 407 io->seek = seek;
nuclear@67 408 }
nuclear@67 409
nuclear@67 410
nuclear@67 411 static int pixel_size(enum img_fmt fmt)
nuclear@67 412 {
nuclear@67 413 switch(fmt) {
nuclear@67 414 case IMG_FMT_GREY8:
nuclear@67 415 return 1;
nuclear@67 416 case IMG_FMT_RGB24:
nuclear@67 417 return 3;
nuclear@67 418 case IMG_FMT_RGBA32:
nuclear@67 419 return 4;
nuclear@67 420 case IMG_FMT_GREYF:
nuclear@67 421 return sizeof(float);
nuclear@67 422 case IMG_FMT_RGBF:
nuclear@67 423 return 3 * sizeof(float);
nuclear@67 424 case IMG_FMT_RGBAF:
nuclear@67 425 return 4 * sizeof(float);
nuclear@67 426 default:
nuclear@67 427 break;
nuclear@67 428 }
nuclear@67 429 return 0;
nuclear@67 430 }
nuclear@67 431
nuclear@67 432 static size_t def_read(void *buf, size_t bytes, void *uptr)
nuclear@67 433 {
nuclear@67 434 return uptr ? fread(buf, 1, bytes, uptr) : 0;
nuclear@67 435 }
nuclear@67 436
nuclear@67 437 static size_t def_write(void *buf, size_t bytes, void *uptr)
nuclear@67 438 {
nuclear@67 439 return uptr ? fwrite(buf, 1, bytes, uptr) : 0;
nuclear@67 440 }
nuclear@67 441
nuclear@67 442 static long def_seek(long offset, int whence, void *uptr)
nuclear@67 443 {
nuclear@67 444 if(!uptr || fseek(uptr, offset, whence) == -1) {
nuclear@67 445 return -1;
nuclear@67 446 }
nuclear@67 447 return ftell(uptr);
nuclear@67 448 }
nuclear@67 449