nuclear@26: /* nuclear@26: libimago - a multi-format image file input/output library. nuclear@26: Copyright (C) 2010 John Tsiombikas nuclear@26: nuclear@26: This program is free software: you can redistribute it and/or modify nuclear@26: it under the terms of the GNU Lesser General Public License as published nuclear@26: by the Free Software Foundation, either version 3 of the License, or nuclear@26: (at your option) any later version. nuclear@26: nuclear@26: This program is distributed in the hope that it will be useful, nuclear@26: but WITHOUT ANY WARRANTY; without even the implied warranty of nuclear@26: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the nuclear@26: GNU Lesser General Public License for more details. nuclear@26: nuclear@26: You should have received a copy of the GNU Lesser General Public License nuclear@26: along with this program. If not, see . nuclear@26: */ nuclear@26: nuclear@26: #include nuclear@26: #include nuclear@26: #include nuclear@26: #include "imago2.h" nuclear@26: #include "ftype_module.h" nuclear@26: nuclear@26: static int pixel_size(enum img_fmt fmt); nuclear@26: static size_t def_read(void *buf, size_t bytes, void *uptr); nuclear@26: static size_t def_write(void *buf, size_t bytes, void *uptr); nuclear@26: static long def_seek(long offset, int whence, void *uptr); nuclear@26: nuclear@26: nuclear@26: void img_init(struct img_pixmap *img) nuclear@26: { nuclear@26: img->pixels = 0; nuclear@26: img->width = img->height = 0; nuclear@26: img->fmt = IMG_FMT_RGBA32; nuclear@26: img->pixelsz = pixel_size(img->fmt); nuclear@26: img->name = 0; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: void img_destroy(struct img_pixmap *img) nuclear@26: { nuclear@26: free(img->pixels); nuclear@26: img->pixels = 0; /* just in case... */ nuclear@26: img->width = img->height = 0xbadbeef; nuclear@26: free(img->name); nuclear@26: } nuclear@26: nuclear@26: struct img_pixmap *img_create(void) nuclear@26: { nuclear@26: struct img_pixmap *p; nuclear@26: nuclear@26: if(!(p = malloc(sizeof *p))) { nuclear@26: return 0; nuclear@26: } nuclear@26: img_init(p); nuclear@26: return p; nuclear@26: } nuclear@26: nuclear@26: void img_free(struct img_pixmap *img) nuclear@26: { nuclear@26: img_destroy(img); nuclear@26: free(img); nuclear@26: } nuclear@26: nuclear@26: int img_set_name(struct img_pixmap *img, const char *name) nuclear@26: { nuclear@26: char *tmp; nuclear@26: nuclear@26: if(!(tmp = malloc(strlen(name) + 1))) { nuclear@26: return -1; nuclear@26: } nuclear@26: strcpy(tmp, name); nuclear@26: img->name = tmp; nuclear@26: return 0; nuclear@26: } nuclear@26: nuclear@26: int img_set_format(struct img_pixmap *img, enum img_fmt fmt) nuclear@26: { nuclear@26: if(img->pixels) { nuclear@26: return img_convert(img, fmt); nuclear@26: } nuclear@26: img->fmt = fmt; nuclear@26: return 0; nuclear@26: } nuclear@26: nuclear@26: int img_copy(struct img_pixmap *dest, struct img_pixmap *src) nuclear@26: { nuclear@26: return img_set_pixels(dest, src->width, src->height, src->fmt, src->pixels); nuclear@26: } nuclear@26: nuclear@26: int img_set_pixels(struct img_pixmap *img, int w, int h, enum img_fmt fmt, void *pix) nuclear@26: { nuclear@26: void *newpix; nuclear@26: int pixsz = pixel_size(fmt); nuclear@26: nuclear@26: if(!(newpix = malloc(w * h * pixsz))) { nuclear@26: return -1; nuclear@26: } nuclear@26: nuclear@26: if(pix) { nuclear@26: memcpy(newpix, pix, w * h * pixsz); nuclear@26: } else { nuclear@26: memset(newpix, 0, w * h * pixsz); nuclear@26: } nuclear@26: nuclear@26: free(img->pixels); nuclear@26: img->pixels = newpix; nuclear@26: img->width = w; nuclear@26: img->height = h; nuclear@26: img->pixelsz = pixsz; nuclear@26: img->fmt = fmt; nuclear@26: return 0; nuclear@26: } nuclear@26: nuclear@26: void *img_load_pixels(const char *fname, int *xsz, int *ysz, enum img_fmt fmt) nuclear@26: { nuclear@26: struct img_pixmap img; nuclear@26: nuclear@26: img_init(&img); nuclear@26: nuclear@26: if(img_load(&img, fname) == -1) { nuclear@26: return 0; nuclear@26: } nuclear@26: if(img.fmt != fmt) { nuclear@26: if(img_convert(&img, fmt) == -1) { nuclear@26: img_destroy(&img); nuclear@26: return 0; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: *xsz = img.width; nuclear@26: *ysz = img.height; nuclear@26: return img.pixels; nuclear@26: } nuclear@26: nuclear@26: int img_save_pixels(const char *fname, void *pix, int xsz, int ysz, enum img_fmt fmt) nuclear@26: { nuclear@26: struct img_pixmap img; nuclear@26: nuclear@26: img_init(&img); nuclear@26: img.fmt = fmt; nuclear@26: img.name = (char*)fname; nuclear@26: img.width = xsz; nuclear@26: img.height = ysz; nuclear@26: img.pixels = pix; nuclear@26: nuclear@26: return img_save(&img, fname); nuclear@26: } nuclear@26: nuclear@26: void img_free_pixels(void *pix) nuclear@26: { nuclear@26: free(pix); nuclear@26: } nuclear@26: nuclear@26: int img_load(struct img_pixmap *img, const char *fname) nuclear@26: { nuclear@26: int res; nuclear@26: FILE *fp; nuclear@26: nuclear@26: if(!(fp = fopen(fname, "rb"))) { nuclear@26: return -1; nuclear@26: } nuclear@26: res = img_read_file(img, fp); nuclear@26: fclose(fp); nuclear@26: return res; nuclear@26: } nuclear@26: nuclear@26: /* TODO implement filetype selection */ nuclear@26: int img_save(struct img_pixmap *img, const char *fname) nuclear@26: { nuclear@26: int res; nuclear@26: FILE *fp; nuclear@26: nuclear@26: img_set_name(img, fname); nuclear@26: nuclear@26: if(!(fp = fopen(fname, "wb"))) { nuclear@26: return -1; nuclear@26: } nuclear@26: res = img_write_file(img, fp); nuclear@26: fclose(fp); nuclear@26: return res; nuclear@26: } nuclear@26: nuclear@26: int img_read_file(struct img_pixmap *img, FILE *fp) nuclear@26: { nuclear@26: struct img_io io = {0, def_read, def_write, def_seek}; nuclear@26: nuclear@26: io.uptr = fp; nuclear@26: return img_read(img, &io); nuclear@26: } nuclear@26: nuclear@26: int img_write_file(struct img_pixmap *img, FILE *fp) nuclear@26: { nuclear@26: struct img_io io = {0, def_read, def_write, def_seek}; nuclear@26: nuclear@26: io.uptr = fp; nuclear@26: return img_write(img, &io); nuclear@26: } nuclear@26: nuclear@26: int img_read(struct img_pixmap *img, struct img_io *io) nuclear@26: { nuclear@26: struct ftype_module *mod; nuclear@26: nuclear@26: if((mod = img_find_format_module(io))) { nuclear@26: return mod->read(img, io); nuclear@26: } nuclear@26: return -1; nuclear@26: } nuclear@26: nuclear@26: int img_write(struct img_pixmap *img, struct img_io *io) nuclear@26: { nuclear@26: struct ftype_module *mod; nuclear@26: nuclear@26: if(!img->name || !(mod = img_guess_format(img->name))) { nuclear@26: /* TODO throw some sort of warning? */ nuclear@26: /* TODO implement some sort of module priority or let the user specify? */ nuclear@26: if(!(mod = img_get_module(0))) { nuclear@26: return -1; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: return mod->write(img, io); nuclear@26: } nuclear@26: nuclear@26: int img_to_float(struct img_pixmap *img) nuclear@26: { nuclear@26: enum img_fmt targ_fmt; nuclear@26: nuclear@26: switch(img->fmt) { nuclear@26: case IMG_FMT_GREY8: nuclear@26: targ_fmt = IMG_FMT_GREYF; nuclear@26: break; nuclear@26: nuclear@26: case IMG_FMT_RGB24: nuclear@26: targ_fmt = IMG_FMT_RGBF; nuclear@26: break; nuclear@26: nuclear@26: case IMG_FMT_RGBA32: nuclear@26: targ_fmt = IMG_FMT_RGBAF; nuclear@26: break; nuclear@26: nuclear@26: default: nuclear@26: return 0; /* already float */ nuclear@26: } nuclear@26: nuclear@26: return img_convert(img, targ_fmt); nuclear@26: } nuclear@26: nuclear@26: int img_to_integer(struct img_pixmap *img) nuclear@26: { nuclear@26: enum img_fmt targ_fmt; nuclear@26: nuclear@26: switch(img->fmt) { nuclear@26: case IMG_FMT_GREYF: nuclear@26: targ_fmt = IMG_FMT_GREY8; nuclear@26: break; nuclear@26: nuclear@26: case IMG_FMT_RGBF: nuclear@26: targ_fmt = IMG_FMT_RGB24; nuclear@26: break; nuclear@26: nuclear@26: case IMG_FMT_RGBAF: nuclear@26: targ_fmt = IMG_FMT_RGBA32; nuclear@26: break; nuclear@26: nuclear@26: default: nuclear@26: return 0; /* already integer */ nuclear@26: } nuclear@26: nuclear@26: return img_convert(img, targ_fmt); nuclear@26: } nuclear@26: nuclear@26: int img_is_float(struct img_pixmap *img) nuclear@26: { nuclear@26: return img->fmt >= IMG_FMT_GREYF && img->fmt <= IMG_FMT_RGBAF; nuclear@26: } nuclear@26: nuclear@26: int img_has_alpha(struct img_pixmap *img) nuclear@26: { nuclear@26: return img->fmt >= IMG_FMT_GREY8 && img->fmt <= IMG_FMT_RGBA32; nuclear@26: } nuclear@26: nuclear@26: void img_io_set_user_data(struct img_io *io, void *uptr) nuclear@26: { nuclear@26: io->uptr = uptr; nuclear@26: } nuclear@26: nuclear@26: void img_io_set_read_func(struct img_io *io, size_t (*read)(void*, size_t, void*)) nuclear@26: { nuclear@26: io->read = read; nuclear@26: } nuclear@26: nuclear@26: void img_io_set_write_func(struct img_io *io, size_t (*write)(void*, size_t, void*)) nuclear@26: { nuclear@26: io->write = write; nuclear@26: } nuclear@26: nuclear@26: void img_io_set_seek_func(struct img_io *io, long (*seek)(long, int, void*)) nuclear@26: { nuclear@26: io->seek = seek; nuclear@26: } nuclear@26: nuclear@26: nuclear@26: static int pixel_size(enum img_fmt fmt) nuclear@26: { nuclear@26: switch(fmt) { nuclear@26: case IMG_FMT_GREY8: nuclear@26: return 1; nuclear@26: case IMG_FMT_RGB24: nuclear@26: return 3; nuclear@26: case IMG_FMT_RGBA32: nuclear@26: return 4; nuclear@26: case IMG_FMT_GREYF: nuclear@26: return sizeof(float); nuclear@26: case IMG_FMT_RGBF: nuclear@26: return 3 * sizeof(float); nuclear@26: case IMG_FMT_RGBAF: nuclear@26: return 4 * sizeof(float); nuclear@26: default: nuclear@26: break; nuclear@26: } nuclear@26: return 0; nuclear@26: } nuclear@26: nuclear@26: static size_t def_read(void *buf, size_t bytes, void *uptr) nuclear@26: { nuclear@26: return uptr ? fread(buf, 1, bytes, uptr) : 0; nuclear@26: } nuclear@26: nuclear@26: static size_t def_write(void *buf, size_t bytes, void *uptr) nuclear@26: { nuclear@26: return uptr ? fwrite(buf, 1, bytes, uptr) : 0; nuclear@26: } nuclear@26: nuclear@26: static long def_seek(long offset, int whence, void *uptr) nuclear@26: { nuclear@26: if(!uptr || fseek(uptr, offset, whence) == -1) { nuclear@26: return -1; nuclear@26: } nuclear@26: return ftell(uptr); nuclear@26: } nuclear@26: