vrshoot

annotate libs/imago/imago2.c @ 2:334d17aed7de

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