vrshoot

annotate libs/imago/conv.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 #include <string.h>
nuclear@0 19 #include "imago2.h"
nuclear@0 20
nuclear@0 21 /* pixel-format conversions are sub-optimal at the moment to avoid
nuclear@0 22 * writing a lot of code. optimize at some point ?
nuclear@0 23 */
nuclear@0 24
nuclear@0 25 #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
nuclear@0 26
nuclear@0 27 struct pixel {
nuclear@0 28 float r, g, b, a;
nuclear@0 29 };
nuclear@0 30
nuclear@0 31 static void unpack_grey8(struct pixel *unp, void *pptr, int count);
nuclear@0 32 static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
nuclear@0 33 static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
nuclear@0 34 static void unpack_greyf(struct pixel *unp, void *pptr, int count);
nuclear@0 35 static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
nuclear@0 36 static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
nuclear@0 37
nuclear@0 38 static void pack_grey8(void *pptr, struct pixel *unp, int count);
nuclear@0 39 static void pack_rgb24(void *pptr, struct pixel *unp, int count);
nuclear@0 40 static void pack_rgba32(void *pptr, struct pixel *unp, int count);
nuclear@0 41 static void pack_greyf(void *pptr, struct pixel *unp, int count);
nuclear@0 42 static void pack_rgbf(void *pptr, struct pixel *unp, int count);
nuclear@0 43 static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
nuclear@0 44
nuclear@0 45 /* XXX keep in sync with enum img_fmt at imago2.h */
nuclear@0 46 static void (*unpack[])(struct pixel*, void*, int) = {
nuclear@0 47 unpack_grey8,
nuclear@0 48 unpack_rgb24,
nuclear@0 49 unpack_rgba32,
nuclear@0 50 unpack_greyf,
nuclear@0 51 unpack_rgbf,
nuclear@0 52 unpack_rgbaf
nuclear@0 53 };
nuclear@0 54
nuclear@0 55 /* XXX keep in sync with enum img_fmt at imago2.h */
nuclear@0 56 static void (*pack[])(void*, struct pixel*, int) = {
nuclear@0 57 pack_grey8,
nuclear@0 58 pack_rgb24,
nuclear@0 59 pack_rgba32,
nuclear@0 60 pack_greyf,
nuclear@0 61 pack_rgbf,
nuclear@0 62 pack_rgbaf
nuclear@0 63 };
nuclear@0 64
nuclear@0 65
nuclear@0 66 int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
nuclear@0 67 {
nuclear@0 68 struct pixel pbuf[8];
nuclear@0 69 int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1);
nuclear@0 70 int i, num_pix = img->width * img->height;
nuclear@0 71 int num_iter = num_pix / bufsz;
nuclear@0 72 char *sptr, *dptr;
nuclear@0 73 struct img_pixmap nimg;
nuclear@0 74
nuclear@0 75 if(img->fmt == tofmt) {
nuclear@0 76 return 0; /* nothing to do */
nuclear@0 77 }
nuclear@0 78
nuclear@0 79 img_init(&nimg);
nuclear@0 80 if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) {
nuclear@0 81 img_destroy(&nimg);
nuclear@0 82 return -1;
nuclear@0 83 }
nuclear@0 84
nuclear@0 85 sptr = img->pixels;
nuclear@0 86 dptr = nimg.pixels;
nuclear@0 87
nuclear@0 88 for(i=0; i<num_iter; i++) {
nuclear@0 89 unpack[img->fmt](pbuf, sptr, bufsz);
nuclear@0 90 pack[tofmt](dptr, pbuf, bufsz);
nuclear@0 91
nuclear@0 92 sptr += bufsz * img->pixelsz;
nuclear@0 93 dptr += bufsz * nimg.pixelsz;
nuclear@0 94 }
nuclear@0 95
nuclear@0 96 img_copy(img, &nimg);
nuclear@0 97 img_destroy(&nimg);
nuclear@0 98 return 0;
nuclear@0 99 }
nuclear@0 100
nuclear@0 101 /* the following functions *could* benefit from SIMD */
nuclear@0 102
nuclear@0 103 static void unpack_grey8(struct pixel *unp, void *pptr, int count)
nuclear@0 104 {
nuclear@0 105 int i;
nuclear@0 106 unsigned char *pix = pptr;
nuclear@0 107
nuclear@0 108 for(i=0; i<count; i++) {
nuclear@0 109 unp->r = unp->g = unp->b = (float)*pix++ / 255.0;
nuclear@0 110 unp->a = 1.0;
nuclear@0 111 unp++;
nuclear@0 112 }
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 static void unpack_rgb24(struct pixel *unp, void *pptr, int count)
nuclear@0 116 {
nuclear@0 117 int i;
nuclear@0 118 unsigned char *pix = pptr;
nuclear@0 119
nuclear@0 120 for(i=0; i<count; i++) {
nuclear@0 121 unp->r = (float)*pix++ / 255.0;
nuclear@0 122 unp->g = (float)*pix++ / 255.0;
nuclear@0 123 unp->b = (float)*pix++ / 255.0;
nuclear@0 124 unp->a = 1.0;
nuclear@0 125 unp++;
nuclear@0 126 }
nuclear@0 127 }
nuclear@0 128
nuclear@0 129 static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
nuclear@0 130 {
nuclear@0 131 int i;
nuclear@0 132 unsigned char *pix = pptr;
nuclear@0 133
nuclear@0 134 for(i=0; i<count; i++) {
nuclear@0 135 unp->r = (float)*pix++ / 255.0;
nuclear@0 136 unp->g = (float)*pix++ / 255.0;
nuclear@0 137 unp->b = (float)*pix++ / 255.0;
nuclear@0 138 unp->a = (float)*pix++ / 255.0;
nuclear@0 139 unp++;
nuclear@0 140 }
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 static void unpack_greyf(struct pixel *unp, void *pptr, int count)
nuclear@0 144 {
nuclear@0 145 int i;
nuclear@0 146 float *pix = pptr;
nuclear@0 147
nuclear@0 148 for(i=0; i<count; i++) {
nuclear@0 149 unp->r = unp->g = unp->b = *pix++;
nuclear@0 150 unp->a = 1.0;
nuclear@0 151 unp++;
nuclear@0 152 }
nuclear@0 153 }
nuclear@0 154
nuclear@0 155 static void unpack_rgbf(struct pixel *unp, void *pptr, int count)
nuclear@0 156 {
nuclear@0 157 int i;
nuclear@0 158 float *pix = pptr;
nuclear@0 159
nuclear@0 160 for(i=0; i<count; i++) {
nuclear@0 161 unp->r = *pix++;
nuclear@0 162 unp->g = *pix++;
nuclear@0 163 unp->b = *pix++;
nuclear@0 164 unp->a = 1.0;
nuclear@0 165 unp++;
nuclear@0 166 }
nuclear@0 167 }
nuclear@0 168
nuclear@0 169 static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
nuclear@0 170 {
nuclear@0 171 int i;
nuclear@0 172 float *pix = pptr;
nuclear@0 173
nuclear@0 174 for(i=0; i<count; i++) {
nuclear@0 175 unp->r = *pix++;
nuclear@0 176 unp->g = *pix++;
nuclear@0 177 unp->b = *pix++;
nuclear@0 178 unp->a = *pix++;
nuclear@0 179 unp++;
nuclear@0 180 }
nuclear@0 181 }
nuclear@0 182
nuclear@0 183
nuclear@0 184 static void pack_grey8(void *pptr, struct pixel *unp, int count)
nuclear@0 185 {
nuclear@0 186 int i;
nuclear@0 187 unsigned char *pix = pptr;
nuclear@0 188
nuclear@0 189 for(i=0; i<count; i++) {
nuclear@0 190 int lum = (int)(255.0 * (unp->r + unp->g + unp->b) / 3.0);
nuclear@0 191 *pix++ = CLAMP(lum, 0, 255);
nuclear@0 192 unp++;
nuclear@0 193 }
nuclear@0 194 }
nuclear@0 195
nuclear@0 196 static void pack_rgb24(void *pptr, struct pixel *unp, int count)
nuclear@0 197 {
nuclear@0 198 int i;
nuclear@0 199 unsigned char *pix = pptr;
nuclear@0 200
nuclear@0 201 for(i=0; i<count; i++) {
nuclear@0 202 int r = (int)(unp->r * 255.0);
nuclear@0 203 int g = (int)(unp->g * 255.0);
nuclear@0 204 int b = (int)(unp->b * 255.0);
nuclear@0 205
nuclear@0 206 *pix++ = CLAMP(r, 0, 255);
nuclear@0 207 *pix++ = CLAMP(g, 0, 255);
nuclear@0 208 *pix++ = CLAMP(b, 0, 255);
nuclear@0 209 unp++;
nuclear@0 210 }
nuclear@0 211 }
nuclear@0 212
nuclear@0 213 static void pack_rgba32(void *pptr, struct pixel *unp, int count)
nuclear@0 214 {
nuclear@0 215 int i;
nuclear@0 216 unsigned char *pix = pptr;
nuclear@0 217
nuclear@0 218 for(i=0; i<count; i++) {
nuclear@0 219 int r = (int)(unp->r * 255.0);
nuclear@0 220 int g = (int)(unp->g * 255.0);
nuclear@0 221 int b = (int)(unp->b * 255.0);
nuclear@0 222 int a = (int)(unp->a * 255.0);
nuclear@0 223
nuclear@0 224 *pix++ = CLAMP(r, 0, 255);
nuclear@0 225 *pix++ = CLAMP(g, 0, 255);
nuclear@0 226 *pix++ = CLAMP(b, 0, 255);
nuclear@0 227 *pix++ = CLAMP(a, 0, 255);
nuclear@0 228 unp++;
nuclear@0 229 }
nuclear@0 230 }
nuclear@0 231
nuclear@0 232 static void pack_greyf(void *pptr, struct pixel *unp, int count)
nuclear@0 233 {
nuclear@0 234 int i;
nuclear@0 235 float *pix = pptr;
nuclear@0 236
nuclear@0 237 for(i=0; i<count; i++) {
nuclear@0 238 *pix++ = (unp->r + unp->g + unp->b) / 3.0;
nuclear@0 239 unp++;
nuclear@0 240 }
nuclear@0 241 }
nuclear@0 242
nuclear@0 243 static void pack_rgbf(void *pptr, struct pixel *unp, int count)
nuclear@0 244 {
nuclear@0 245 int i;
nuclear@0 246 float *pix = pptr;
nuclear@0 247
nuclear@0 248 for(i=0; i<count; i++) {
nuclear@0 249 *pix++ = unp->r;
nuclear@0 250 *pix++ = unp->g;
nuclear@0 251 *pix++ = unp->b;
nuclear@0 252 unp++;
nuclear@0 253 }
nuclear@0 254 }
nuclear@0 255
nuclear@0 256 static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
nuclear@0 257 {
nuclear@0 258 memcpy(pptr, unp, count * sizeof *unp);
nuclear@0 259 }
nuclear@0 260