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; |
---|
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 | /* Allocate pixels if necessary */ |
---|
52 | if(!img->p[type].pixels) |
---|
53 | { |
---|
54 | switch(type) |
---|
55 | { |
---|
56 | case PIPI_PIXELS_RGBA32: |
---|
57 | bytes = img->w * img->h * 4 * sizeof(uint8_t); |
---|
58 | break; |
---|
59 | case PIPI_PIXELS_RGBA_F: |
---|
60 | bytes = img->w * img->h * 4 * sizeof(float); |
---|
61 | break; |
---|
62 | default: |
---|
63 | return NULL; |
---|
64 | } |
---|
65 | |
---|
66 | img->p[type].pixels = malloc(bytes); |
---|
67 | } |
---|
68 | |
---|
69 | /* Convert pixels */ |
---|
70 | if(img->last_modified == PIPI_PIXELS_RGBA32 |
---|
71 | && type == PIPI_PIXELS_RGBA_F) |
---|
72 | { |
---|
73 | uint8_t *src = (uint8_t *)img->p[PIPI_PIXELS_RGBA32].pixels; |
---|
74 | float *dest = (float *)img->p[type].pixels; |
---|
75 | |
---|
76 | init_tables(); |
---|
77 | |
---|
78 | for(y = 0; y < img->h; y++) |
---|
79 | for(x = 0; x < img->w; x++) |
---|
80 | for(i = 0; i < 4; i++) |
---|
81 | dest[4 * (y * img->w + x) + i] |
---|
82 | = u8tof32(src[4 * (y * img->w + x) + i]); |
---|
83 | } |
---|
84 | else if(img->last_modified == PIPI_PIXELS_RGBA_F |
---|
85 | && type == PIPI_PIXELS_RGBA32) |
---|
86 | { |
---|
87 | float *src = (float *)img->p[PIPI_PIXELS_RGBA_F].pixels; |
---|
88 | uint8_t *dest = (uint8_t *)img->p[type].pixels; |
---|
89 | |
---|
90 | init_tables(); |
---|
91 | |
---|
92 | for(y = 0; y < img->h; y++) |
---|
93 | for(x = 0; x < img->w; x++) |
---|
94 | for(i = 0; i < 4; i++) |
---|
95 | { |
---|
96 | double p, e; |
---|
97 | uint8_t d; |
---|
98 | |
---|
99 | p = src[4 * (y * img->w + x) + i]; |
---|
100 | |
---|
101 | if(p < 0.) d = 0.; |
---|
102 | else if(p > 1.) d = 255; |
---|
103 | else d = (int)(255.999 * pow(p, 1. / GAMMA)); |
---|
104 | |
---|
105 | dest[4 * (y * img->w + x) + i] = d; |
---|
106 | |
---|
107 | e = p - u8tof32(d); |
---|
108 | if(x < img->w - 1) |
---|
109 | src[4 * (y * img->w + x + 1) + i] += e * .4375; |
---|
110 | if(y < img->h - 1) |
---|
111 | { |
---|
112 | if(x < img->w - 1) |
---|
113 | src[4 * ((y + 1) * img->w + x - 1) + i] += e * .1875; |
---|
114 | src[4 * ((y + 1) * img->w + x) + i] += e * .3125; |
---|
115 | if(x < img->w - 1) |
---|
116 | src[4 * ((y + 1) * img->w + x + 1) + i] += e * .0625; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | else |
---|
121 | { |
---|
122 | memset(img->p[type].pixels, 0, bytes); |
---|
123 | } |
---|
124 | |
---|
125 | img->last_modified = type; |
---|
126 | |
---|
127 | return &img->p[type]; |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | static void init_tables(void) |
---|
132 | { |
---|
133 | static int done = 0; |
---|
134 | int i; |
---|
135 | |
---|
136 | if(done) |
---|
137 | return; |
---|
138 | |
---|
139 | for(i = 0; i < 256; i++) |
---|
140 | u8tof32_table[i] = pow((double)i / 255., GAMMA); |
---|
141 | |
---|
142 | done = 1; |
---|
143 | } |
---|
144 | |
---|