3dphotoshoot

annotate libs/imago/imago2.c @ 27:3d082c566b53

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