1 | /* |
---|
2 | * libpipi Proper image processing implementation library |
---|
3 | * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id$ |
---|
7 | * |
---|
8 | * This library is free software. It comes without any warranty, to |
---|
9 | * the extent permitted by applicable law. You can redistribute it |
---|
10 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
11 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | /* |
---|
16 | * pixels.c: pixel-level image manipulation |
---|
17 | */ |
---|
18 | |
---|
19 | #include "config.h" |
---|
20 | #include "common.h" |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <stdlib.h> |
---|
24 | #include <string.h> |
---|
25 | |
---|
26 | #include <math.h> |
---|
27 | |
---|
28 | #include "pipi.h" |
---|
29 | #include "pipi_internals.h" |
---|
30 | |
---|
31 | #define GAMMA 2.2 |
---|
32 | |
---|
33 | static void init_tables(void); |
---|
34 | |
---|
35 | static float u8tof32_table[256]; |
---|
36 | static inline float u8tof32(uint8_t p) { return u8tof32_table[(int)p]; } |
---|
37 | |
---|
38 | |
---|
39 | /* Return a direct pointer to an image's pixels. */ |
---|
40 | pipi_pixels_t *pipi_getpixels(pipi_image_t *img, pipi_format_t type) |
---|
41 | { |
---|
42 | size_t bytes = 0; |
---|
43 | int x, y, i, bpp = 0; |
---|
44 | |
---|
45 | if(type < 0 || type >= PIPI_PIXELS_MAX) |
---|
46 | return NULL; |
---|
47 | |
---|
48 | if(img->last_modified == type) |
---|
49 | return &img->p[type]; |
---|
50 | |
---|
51 | /* Preliminary conversions */ |
---|
52 | if(img->last_modified == PIPI_PIXELS_RGBA32 |
---|
53 | && type == PIPI_PIXELS_Y_F) |
---|
54 | pipi_getpixels(img, PIPI_PIXELS_RGBA_F); |
---|
55 | else if(img->last_modified == PIPI_PIXELS_BGR24 |
---|
56 | && type == PIPI_PIXELS_Y_F) |
---|
57 | pipi_getpixels(img, PIPI_PIXELS_RGBA_F); |
---|
58 | else if(img->last_modified == PIPI_PIXELS_Y_F |
---|
59 | && type == PIPI_PIXELS_RGBA32) |
---|
60 | pipi_getpixels(img, PIPI_PIXELS_RGBA_F); |
---|
61 | else if(img->last_modified == PIPI_PIXELS_Y_F |
---|
62 | && type == PIPI_PIXELS_BGR24) |
---|
63 | pipi_getpixels(img, PIPI_PIXELS_RGBA_F); |
---|
64 | |
---|
65 | /* Allocate pixels if necessary */ |
---|
66 | if(!img->p[type].pixels) |
---|
67 | { |
---|
68 | switch(type) |
---|
69 | { |
---|
70 | case PIPI_PIXELS_RGBA32: |
---|
71 | bytes = img->w * img->h * 4 * sizeof(uint8_t); |
---|
72 | bpp = 4 * sizeof(uint8_t); |
---|
73 | break; |
---|
74 | case PIPI_PIXELS_BGR24: |
---|
75 | bytes = img->w * img->h * 3 * sizeof(uint8_t); |
---|
76 | bpp = 3 * sizeof(uint8_t); |
---|
77 | break; |
---|
78 | case PIPI_PIXELS_RGBA_F: |
---|
79 | bytes = img->w * img->h * 4 * sizeof(float); |
---|
80 | bpp = 4 * sizeof(float); |
---|
81 | break; |
---|
82 | case PIPI_PIXELS_Y_F: |
---|
83 | bytes = img->w * img->h * sizeof(float); |
---|
84 | bpp = sizeof(float); |
---|
85 | break; |
---|
86 | default: |
---|
87 | return NULL; |
---|
88 | } |
---|
89 | |
---|
90 | img->p[type].pixels = malloc(bytes); |
---|
91 | img->p[type].bytes = bytes; |
---|
92 | img->p[type].bpp = bpp; |
---|
93 | } |
---|
94 | |
---|
95 | /* Convert pixels */ |
---|
96 | if(img->last_modified == PIPI_PIXELS_RGBA32 |
---|
97 | && type == PIPI_PIXELS_RGBA_F) |
---|
98 | { |
---|
99 | uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels; |
---|
100 | float *dest = (float *)img->p[type].pixels; |
---|
101 | |
---|
102 | init_tables(); |
---|
103 | |
---|
104 | for(y = 0; y < img->h; y++) |
---|
105 | for(x = 0; x < img->w; x++) |
---|
106 | for(i = 0; i < 4; i++) |
---|
107 | dest[4 * (y * img->w + x) + i] |
---|
108 | = u8tof32(src[4 * (y * img->w + x) + i]); |
---|
109 | } |
---|
110 | else if(img->last_modified == PIPI_PIXELS_BGR24 |
---|
111 | && type == PIPI_PIXELS_RGBA_F) |
---|
112 | { |
---|
113 | uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_BGR24].pixels; |
---|
114 | float *dest = (float *)img->p[type].pixels; |
---|
115 | |
---|
116 | init_tables(); |
---|
117 | |
---|
118 | for(y = 0; y < img->h; y++) |
---|
119 | for(x = 0; x < img->w; x++) |
---|
120 | { |
---|
121 | dest[4 * (y * img->w + x)] |
---|
122 | = u8tof32(src[3 * (y * img->w + x) + 2]); |
---|
123 | dest[4 * (y * img->w + x) + 1] |
---|
124 | = u8tof32(src[3 * (y * img->w + x) + 1]); |
---|
125 | dest[4 * (y * img->w + x) + 2] |
---|
126 | = u8tof32(src[3 * (y * img->w + x)]); |
---|
127 | dest[4 * (y * img->w + x) + 3] = 1.0; |
---|
128 | } |
---|
129 | } |
---|
130 | else if(img->last_modified == PIPI_PIXELS_RGBA_F |
---|
131 | && type == PIPI_PIXELS_RGBA32) |
---|
132 | { |
---|
133 | float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; |
---|
134 | uint8_t *dest = (uint8_t *)img->p[type].pixels; |
---|
135 | |
---|
136 | init_tables(); |
---|
137 | |
---|
138 | for(y = 0; y < img->h; y++) |
---|
139 | for(x = 0; x < img->w; x++) |
---|
140 | for(i = 0; i < 4; i++) |
---|
141 | { |
---|
142 | double p, e; |
---|
143 | uint8_t d; |
---|
144 | |
---|
145 | p = src[4 * (y * img->w + x) + i]; |
---|
146 | |
---|
147 | if(p < 0.) d = 0.; |
---|
148 | else if(p > 1.) d = 255; |
---|
149 | else d = (int)(255.999 * pow(p, 1. / GAMMA)); |
---|
150 | |
---|
151 | dest[4 * (y * img->w + x) + i] = d; |
---|
152 | |
---|
153 | e = p - u8tof32(d); |
---|
154 | if(x < img->w - 1) |
---|
155 | src[4 * (y * img->w + x + 1) + i] += e * .4375; |
---|
156 | if(y < img->h - 1) |
---|
157 | { |
---|
158 | if(x > 0) |
---|
159 | src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875; |
---|
160 | src[4 * ((y + 1) * img->w + x) + i] += e * .3125; |
---|
161 | if(x < img->w - 1) |
---|
162 | src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|
166 | else if(img->last_modified == PIPI_PIXELS_RGBA_F |
---|
167 | && type == PIPI_PIXELS_BGR24) |
---|
168 | { |
---|
169 | float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; |
---|
170 | uint8_t *dest = (uint8_t *)img->p[type].pixels; |
---|
171 | |
---|
172 | init_tables(); |
---|
173 | |
---|
174 | for(y = 0; y < img->h; y++) |
---|
175 | for(x = 0; x < img->w; x++) |
---|
176 | for(i = 0; i < 3; i++) |
---|
177 | { |
---|
178 | double p, e; |
---|
179 | uint8_t d; |
---|
180 | |
---|
181 | p = src[4 * (y * img->w + x) + i]; |
---|
182 | |
---|
183 | if(p < 0.) d = 0.; |
---|
184 | else if(p > 1.) d = 255; |
---|
185 | else d = (int)(255.999 * pow(p, 1. / GAMMA)); |
---|
186 | |
---|
187 | dest[3 * (y * img->w + x) + i] = d; |
---|
188 | |
---|
189 | e = p - u8tof32(d); |
---|
190 | if(x < img->w - 1) |
---|
191 | src[4 * (y * img->w + x + 1) + i] += e * .4375; |
---|
192 | if(y < img->h - 1) |
---|
193 | { |
---|
194 | if(x > 0) |
---|
195 | src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875; |
---|
196 | src[4 * ((y + 1) * img->w + x) + i] += e * .3125; |
---|
197 | if(x < img->w - 1) |
---|
198 | src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625; |
---|
199 | } |
---|
200 | } |
---|
201 | } |
---|
202 | else if(img->last_modified == PIPI_PIXELS_Y_F |
---|
203 | && type == PIPI_PIXELS_RGBA_F) |
---|
204 | { |
---|
205 | float *src = (float *)img->p[PIPI_PIXELS_Y_F].pixels; |
---|
206 | float *dest = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; |
---|
207 | |
---|
208 | init_tables(); |
---|
209 | |
---|
210 | for(y = 0; y < img->h; y++) |
---|
211 | for(x = 0; x < img->w; x++) |
---|
212 | { |
---|
213 | float p = src[y * img->w + x]; |
---|
214 | dest[4 * (y * img->w + x)] = p; |
---|
215 | dest[4 * (y * img->w + x) + 1] = p; |
---|
216 | dest[4 * (y * img->w + x) + 2] = p; |
---|
217 | dest[4 * (y * img->w + x) + 3] = 1.0; |
---|
218 | } |
---|
219 | } |
---|
220 | else if(img->last_modified == PIPI_PIXELS_RGBA_F |
---|
221 | && type == PIPI_PIXELS_Y_F) |
---|
222 | { |
---|
223 | float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; |
---|
224 | float *dest = (float *)img->p[PIPI_PIXELS_Y_F].pixels; |
---|
225 | |
---|
226 | init_tables(); |
---|
227 | |
---|
228 | for(y = 0; y < img->h; y++) |
---|
229 | for(x = 0; x < img->w; x++) |
---|
230 | { |
---|
231 | float p = 0.; |
---|
232 | p += 0.299 * src[4 * (y * img->w + x)]; |
---|
233 | p += 0.587 * src[4 * (y * img->w + x) + 1]; |
---|
234 | p += 0.114 * src[4 * (y * img->w + x) + 2]; |
---|
235 | dest[y * img->w + x] = p; |
---|
236 | } |
---|
237 | } |
---|
238 | else |
---|
239 | { |
---|
240 | memset(img->p[type].pixels, 0, bytes); |
---|
241 | } |
---|
242 | |
---|
243 | img->last_modified = type; |
---|
244 | |
---|
245 | return &img->p[type]; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | static void init_tables(void) |
---|
250 | { |
---|
251 | static int done = 0; |
---|
252 | int i; |
---|
253 | |
---|
254 | if(done) |
---|
255 | return; |
---|
256 | |
---|
257 | for(i = 0; i < 256; i++) |
---|
258 | u8tof32_table[i] = pow((double)i / 255., GAMMA); |
---|
259 | |
---|
260 | done = 1; |
---|
261 | } |
---|
262 | |
---|