1 /*
2 * libng filter -- gamma correction
3 *
4 * (c) 2002 Gerd Knorr <kraxel@bytesex.org>
5 *
6 */
7
8 #include "config.h"
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <math.h>
13 #include <pthread.h>
14
15 #include "grab-ng.h"
16
17 /* ------------------------------------------------------------------- */
18
19 static unsigned char lut[256];
20 static int g = 100;
21
22 static void inline
23 gamma_bytes(unsigned char *dst, unsigned char *src, int bytes)
24 {
25 while (bytes--)
26 *(dst++) = lut[ *(src++) ];
27 }
28
29 static void inline
30 gamma_native_rgb15(void *d, void *s, int pixels)
31 {
32 unsigned short *dst = d;
33 unsigned short *src = s;
34 unsigned short r,g,b;
35
36 while (pixels--) {
37 r = lut[ ((*src >> 7) & 0xf8) ] & 0xf8;
38 g = lut[ ((*src >> 2) & 0xf8) ] & 0xf8;
39 b = lut[ ((*src << 3) & 0xf8) ] & 0xf8;
40 *dst = (r << 7) | (g << 2) | (b >> 3);
41 src++; dst++;
42 }
43 }
44
45 static void inline
46 gamma_native_rgb16(void *d, void *s, int pixels)
47 {
48 unsigned short *dst = d;
49 unsigned short *src = s;
50 unsigned short r,g,b;
51
52 while (pixels--) {
53 r = lut[ ((*src >> 8) & 0xf8) ] & 0xf8;
54 g = lut[ ((*src >> 3) & 0xfc) ] & 0xfc;
55 b = lut[ ((*src << 3) & 0xf8) ] & 0xf8;
56 *dst = (r << 8) | (g << 3) | (b >> 3);
57 src++; dst++;
58 }
59 }
60
61 /* ------------------------------------------------------------------- */
62
63 static void *init(struct ng_video_fmt *out)
64 {
65 /* don't have to carry around status info */
66 static int dummy;
67 return &dummy;
68 }
69
70 static struct ng_video_buf*
71 frame(void *handle, struct ng_video_buf *in)
72 {
73 struct ng_video_buf *out;
74 unsigned char *dst;
75 unsigned char *src;
76 unsigned int y,cnt;
77
78 out = ng_malloc_video_buf(&in->fmt, in->fmt.height * in->fmt.bytesperline);
79 out->info = in->info;
80
81 dst = out->data;
82 src = in->data;
83 cnt = in->fmt.width * ng_vfmt_to_depth[in->fmt.fmtid] / 8;
84 for (y = 0; y < in->fmt.height; y++) {
85 switch (in->fmt.fmtid) {
86 case VIDEO_GRAY:
87 case VIDEO_BGR24:
88 case VIDEO_RGB24:
89 case VIDEO_BGR32:
90 case VIDEO_RGB32:
91 gamma_bytes(dst,src,cnt);
92 break;
93 case VIDEO_RGB15_NATIVE:
94 gamma_native_rgb15(dst,src,in->fmt.width);
95 break;
96 case VIDEO_RGB16_NATIVE:
97 gamma_native_rgb16(dst,src,in->fmt.width);
98 break;
99 }
100 dst += out->fmt.bytesperline;
101 src += in->fmt.bytesperline;
102 }
103
104 ng_release_video_buf(in);
105 return out;
106 }
107
108 static void fini(void *handle)
109 {
110 /* nothing to clean up */
111 }
112
113 /* ------------------------------------------------------------------- */
114
115 static void calc_lut(void)
116 {
117 int i,val;
118
119 for (i = 0; i < 256; i++) {
120 val = 255 * pow((float)i/255, 100.0/g);
121 if (val < 0) val = 0;
122 if (val > 255) val = 255;
123 lut[i] = val;
124 }
125 }
126
127 static int read_attr(struct ng_attribute *attr)
128 {
129 return g;
130 }
131
132 static void write_attr(struct ng_attribute *attr, int value)
133 {
134 g = value;
135 calc_lut();
136 }
137
138 /* ------------------------------------------------------------------- */
139
140 static struct ng_attribute attrs[] = {
141 {
142 id: 0,
143 name: "gamma value",
144 type: ATTR_TYPE_INTEGER,
145 defval: 100,
146 min: 1,
147 max: 500,
148 points: 2,
149 read: read_attr,
150 write: write_attr,
151 },{
152 /* end of list */
153 }
154 };
155
156 static struct ng_filter filter = {
157 name: "gamma",
158 attrs: attrs,
159 fmts:
160 (1 << VIDEO_GRAY) |
161 (1 << VIDEO_RGB15_NATIVE) |
162 (1 << VIDEO_RGB16_NATIVE) |
163 (1 << VIDEO_BGR24) |
164 (1 << VIDEO_RGB24) |
165 (1 << VIDEO_BGR32) |
166 (1 << VIDEO_RGB32),
167 init: init,
168 frame: frame,
169 fini: fini,
170 };
171
172 extern void ng_plugin_init(void);
173 void ng_plugin_init(void)
174 {
175 calc_lut();
176 ng_filter_register(NG_PLUGIN_MAGIC,__FILE__,&filter);
177 }
178
|
This page was automatically generated by the
LXR engine.
|