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: #include nuclear@26: #include "imago2.h" nuclear@26: nuclear@26: /* pixel-format conversions are sub-optimal at the moment to avoid nuclear@26: * writing a lot of code. optimize at some point ? nuclear@26: */ nuclear@26: nuclear@26: #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x))) nuclear@26: nuclear@26: struct pixel { nuclear@26: float r, g, b, a; nuclear@26: }; nuclear@26: nuclear@26: static void unpack_grey8(struct pixel *unp, void *pptr, int count); nuclear@26: static void unpack_rgb24(struct pixel *unp, void *pptr, int count); nuclear@26: static void unpack_rgba32(struct pixel *unp, void *pptr, int count); nuclear@26: static void unpack_greyf(struct pixel *unp, void *pptr, int count); nuclear@26: static void unpack_rgbf(struct pixel *unp, void *pptr, int count); nuclear@26: static void unpack_rgbaf(struct pixel *unp, void *pptr, int count); nuclear@26: nuclear@26: static void pack_grey8(void *pptr, struct pixel *unp, int count); nuclear@26: static void pack_rgb24(void *pptr, struct pixel *unp, int count); nuclear@26: static void pack_rgba32(void *pptr, struct pixel *unp, int count); nuclear@26: static void pack_greyf(void *pptr, struct pixel *unp, int count); nuclear@26: static void pack_rgbf(void *pptr, struct pixel *unp, int count); nuclear@26: static void pack_rgbaf(void *pptr, struct pixel *unp, int count); nuclear@26: nuclear@26: /* XXX keep in sync with enum img_fmt at imago2.h */ nuclear@26: static void (*unpack[])(struct pixel*, void*, int) = { nuclear@26: unpack_grey8, nuclear@26: unpack_rgb24, nuclear@26: unpack_rgba32, nuclear@26: unpack_greyf, nuclear@26: unpack_rgbf, nuclear@26: unpack_rgbaf nuclear@26: }; nuclear@26: nuclear@26: /* XXX keep in sync with enum img_fmt at imago2.h */ nuclear@26: static void (*pack[])(void*, struct pixel*, int) = { nuclear@26: pack_grey8, nuclear@26: pack_rgb24, nuclear@26: pack_rgba32, nuclear@26: pack_greyf, nuclear@26: pack_rgbf, nuclear@26: pack_rgbaf nuclear@26: }; nuclear@26: nuclear@26: nuclear@26: int img_convert(struct img_pixmap *img, enum img_fmt tofmt) nuclear@26: { nuclear@26: struct pixel pbuf[8]; nuclear@26: int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1); nuclear@26: int i, num_pix = img->width * img->height; nuclear@26: int num_iter = num_pix / bufsz; nuclear@26: char *sptr, *dptr; nuclear@26: struct img_pixmap nimg; nuclear@26: nuclear@26: if(img->fmt == tofmt) { nuclear@26: return 0; /* nothing to do */ nuclear@26: } nuclear@26: nuclear@26: img_init(&nimg); nuclear@26: if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) { nuclear@26: img_destroy(&nimg); nuclear@26: return -1; nuclear@26: } nuclear@26: nuclear@26: sptr = img->pixels; nuclear@26: dptr = nimg.pixels; nuclear@26: nuclear@26: for(i=0; ifmt](pbuf, sptr, bufsz); nuclear@26: pack[tofmt](dptr, pbuf, bufsz); nuclear@26: nuclear@26: sptr += bufsz * img->pixelsz; nuclear@26: dptr += bufsz * nimg.pixelsz; nuclear@26: } nuclear@26: nuclear@26: img_copy(img, &nimg); nuclear@26: img_destroy(&nimg); nuclear@26: return 0; nuclear@26: } nuclear@26: nuclear@26: /* the following functions *could* benefit from SIMD */ nuclear@26: nuclear@26: static void unpack_grey8(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: int i; nuclear@26: unsigned char *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir = unp->g = unp->b = (float)*pix++ / 255.0; nuclear@26: unp->a = 1.0; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void unpack_rgb24(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: int i; nuclear@26: unsigned char *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir = (float)*pix++ / 255.0; nuclear@26: unp->g = (float)*pix++ / 255.0; nuclear@26: unp->b = (float)*pix++ / 255.0; nuclear@26: unp->a = 1.0; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void unpack_rgba32(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: memcpy(unp, pptr, count * sizeof *unp); nuclear@26: } nuclear@26: nuclear@26: static void unpack_greyf(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: int i; nuclear@26: float *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir = unp->g = unp->b = *pix++; nuclear@26: unp->a = 1.0; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void unpack_rgbf(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: int i; nuclear@26: float *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir = *pix++; nuclear@26: unp->g = *pix++; nuclear@26: unp->b = *pix++; nuclear@26: unp->a = 1.0; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void unpack_rgbaf(struct pixel *unp, void *pptr, int count) nuclear@26: { nuclear@26: int i; nuclear@26: float *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir = *pix++; nuclear@26: unp->g = *pix++; nuclear@26: unp->b = *pix++; nuclear@26: unp->a = *pix++; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: nuclear@26: static void pack_grey8(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: int i; nuclear@26: unsigned char *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir + unp->g + unp->b) / 3.0); nuclear@26: *pix++ = CLAMP(lum, 0, 255); nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void pack_rgb24(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: int i; nuclear@26: unsigned char *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir * 255.0); nuclear@26: int g = (int)(unp->g * 255.0); nuclear@26: int b = (int)(unp->b * 255.0); nuclear@26: nuclear@26: *pix++ = CLAMP(r, 0, 255); nuclear@26: *pix++ = CLAMP(g, 0, 255); nuclear@26: *pix++ = CLAMP(b, 0, 255); nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void pack_rgba32(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: int i; nuclear@26: unsigned char *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir * 255.0); nuclear@26: int g = (int)(unp->g * 255.0); nuclear@26: int b = (int)(unp->b * 255.0); nuclear@26: int a = (int)(unp->a * 255.0); nuclear@26: nuclear@26: *pix++ = CLAMP(r, 0, 255); nuclear@26: *pix++ = CLAMP(g, 0, 255); nuclear@26: *pix++ = CLAMP(b, 0, 255); nuclear@26: *pix++ = CLAMP(a, 0, 255); nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void pack_greyf(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: int i; nuclear@26: float *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir + unp->g + unp->b) / 3.0; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void pack_rgbf(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: int i; nuclear@26: float *pix = pptr; nuclear@26: nuclear@26: for(i=0; ir; nuclear@26: *pix++ = unp->g; nuclear@26: *pix++ = unp->b; nuclear@26: unp++; nuclear@26: } nuclear@26: } nuclear@26: nuclear@26: static void pack_rgbaf(void *pptr, struct pixel *unp, int count) nuclear@26: { nuclear@26: memcpy(pptr, unp, count * sizeof *unp); nuclear@26: } nuclear@26: