2 libimago - a multi-format image file input/output library.
3 Copyright (C) 2010-2020 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published
7 by the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 /* pixel-format conversions are sub-optimal at the moment to avoid
23 * writing a lot of code. optimize at some point ?
26 #define CLAMP(x, a, b) ((x) < (a) ? (a) : ((x) > (b) ? (b) : (x)))
32 static void unpack_grey8(struct pixel *unp, void *pptr, int count);
33 static void unpack_rgb24(struct pixel *unp, void *pptr, int count);
34 static void unpack_rgba32(struct pixel *unp, void *pptr, int count);
35 static void unpack_greyf(struct pixel *unp, void *pptr, int count);
36 static void unpack_rgbf(struct pixel *unp, void *pptr, int count);
37 static void unpack_rgbaf(struct pixel *unp, void *pptr, int count);
38 static void unpack_rgb565(struct pixel *unp, void *pptr, int count);
40 static void pack_grey8(void *pptr, struct pixel *unp, int count);
41 static void pack_rgb24(void *pptr, struct pixel *unp, int count);
42 static void pack_rgba32(void *pptr, struct pixel *unp, int count);
43 static void pack_greyf(void *pptr, struct pixel *unp, int count);
44 static void pack_rgbf(void *pptr, struct pixel *unp, int count);
45 static void pack_rgbaf(void *pptr, struct pixel *unp, int count);
46 static void pack_rgb565(void *pptr, struct pixel *unp, int count);
48 /* XXX keep in sync with enum img_fmt at imago2.h */
49 static void (*unpack[])(struct pixel*, void*, int) = {
59 /* XXX keep in sync with enum img_fmt at imago2.h */
60 static void (*pack[])(void*, struct pixel*, int) = {
71 int img_convert(struct img_pixmap *img, enum img_fmt tofmt)
74 int bufsz = (img->width & 7) == 0 ? 8 : ((img->width & 3) == 0 ? 4 : 1);
75 int i, num_pix = img->width * img->height;
76 int num_iter = num_pix / bufsz;
78 struct img_pixmap nimg;
80 if(img->fmt == tofmt) {
81 return 0; /* nothing to do */
85 if(img_set_pixels(&nimg, img->width, img->height, tofmt, 0) == -1) {
93 for(i=0; i<num_iter; i++) {
94 unpack[img->fmt](pbuf, sptr, bufsz);
95 pack[tofmt](dptr, pbuf, bufsz);
97 sptr += bufsz * img->pixelsz;
98 dptr += bufsz * nimg.pixelsz;
101 img_copy(img, &nimg);
106 /* the following functions *could* benefit from SIMD */
108 static void unpack_grey8(struct pixel *unp, void *pptr, int count)
111 unsigned char *pix = pptr;
113 for(i=0; i<count; i++) {
114 unp->r = unp->g = unp->b = (float)*pix++ / 255.0;
120 static void unpack_rgb24(struct pixel *unp, void *pptr, int count)
123 unsigned char *pix = pptr;
125 for(i=0; i<count; i++) {
126 unp->r = (float)*pix++ / 255.0;
127 unp->g = (float)*pix++ / 255.0;
128 unp->b = (float)*pix++ / 255.0;
134 static void unpack_rgba32(struct pixel *unp, void *pptr, int count)
137 unsigned char *pix = pptr;
139 for(i=0; i<count; i++) {
140 unp->r = (float)*pix++ / 255.0;
141 unp->g = (float)*pix++ / 255.0;
142 unp->b = (float)*pix++ / 255.0;
143 unp->a = (float)*pix++ / 255.0;
148 static void unpack_greyf(struct pixel *unp, void *pptr, int count)
153 for(i=0; i<count; i++) {
154 unp->r = unp->g = unp->b = *pix++;
160 static void unpack_rgbf(struct pixel *unp, void *pptr, int count)
165 for(i=0; i<count; i++) {
174 static void unpack_rgbaf(struct pixel *unp, void *pptr, int count)
179 for(i=0; i<count; i++) {
188 static void unpack_rgb565(struct pixel *unp, void *pptr, int count)
191 uint16_t *pix = pptr;
193 for(i=0; i<count; i++) {
194 uint16_t r, g, b, p = *pix++;
196 if(r & 8) r |= 7; /* fill LSbits with whatever bit 0 was */
198 if(g & 4) g |= 3; /* ditto */
200 if(b & 8) r |= 7; /* same */
202 unp->r = (float)r / 255.0f;
203 unp->g = (float)g / 255.0f;
204 unp->b = (float)b / 255.0f;
211 static void pack_grey8(void *pptr, struct pixel *unp, int count)
214 unsigned char *pix = pptr;
216 for(i=0; i<count; i++) {
217 int lum = (int)(255.0 * (unp->r + unp->g + unp->b) / 3.0);
218 *pix++ = CLAMP(lum, 0, 255);
223 static void pack_rgb24(void *pptr, struct pixel *unp, int count)
226 unsigned char *pix = pptr;
228 for(i=0; i<count; i++) {
229 int r = (int)(unp->r * 255.0);
230 int g = (int)(unp->g * 255.0);
231 int b = (int)(unp->b * 255.0);
233 *pix++ = CLAMP(r, 0, 255);
234 *pix++ = CLAMP(g, 0, 255);
235 *pix++ = CLAMP(b, 0, 255);
240 static void pack_rgba32(void *pptr, struct pixel *unp, int count)
243 unsigned char *pix = pptr;
245 for(i=0; i<count; i++) {
246 int r = (int)(unp->r * 255.0);
247 int g = (int)(unp->g * 255.0);
248 int b = (int)(unp->b * 255.0);
249 int a = (int)(unp->a * 255.0);
251 *pix++ = CLAMP(r, 0, 255);
252 *pix++ = CLAMP(g, 0, 255);
253 *pix++ = CLAMP(b, 0, 255);
254 *pix++ = CLAMP(a, 0, 255);
259 static void pack_greyf(void *pptr, struct pixel *unp, int count)
264 for(i=0; i<count; i++) {
265 *pix++ = (unp->r + unp->g + unp->b) / 3.0;
270 static void pack_rgbf(void *pptr, struct pixel *unp, int count)
275 for(i=0; i<count; i++) {
283 static void pack_rgbaf(void *pptr, struct pixel *unp, int count)
285 memcpy(pptr, unp, count * sizeof *unp);
289 static void pack_rgb565(void *pptr, struct pixel *unp, int count)
292 uint16_t *pix = pptr;
294 for(i=0; i<count; i++) {
295 uint16_t r = (uint16_t)(unp->r * 255.0f);
296 uint16_t g = (uint16_t)(unp->g * 255.0f);
297 uint16_t b = (uint16_t)(unp->b * 255.0f);
301 *pix++ = (r >> 3) | ((g & 0x3f) << 2) | ((b & 0x1f) << 8);