aboutsummaryrefslogtreecommitdiffstats
path: root/libng/color_lut.c
blob: e9bc3d96ff2ac9ca0b6307e29c7a02924a680a6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * colorspace conversion functions
 *    -- translate RGB using lookup tables
 *
 *  (c) 1998-2001 Gerd Knorr <kraxel@bytesex.org>
 *
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
#ifdef HAVE_ENDIAN_H
# include <endian.h>
#endif

#include "grab-ng.h"
#include "byteswap.h"

static unsigned long   lut_red[256];
static unsigned long   lut_green[256];
static unsigned long   lut_blue[256];

/* ------------------------------------------------------------------- */

static int
rgb24_to_lut2(unsigned char *dest, unsigned char *src, int p)
{
    unsigned short *d = (unsigned short*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[0]] | lut_green[src[1]] | lut_blue[src[2]];
	src += 3;
    }
    return ((unsigned char*)d-dest);
}

static int
bgr24_to_lut2(unsigned char *dest, unsigned char *src, int p)
{
    unsigned short *d = (unsigned short*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[2]] | lut_green[src[1]] | lut_blue[src[0]];
	src += 3;
    }
    return ((unsigned char*)d-dest);
}

static int
rgb32_to_lut2(unsigned char *dest, unsigned char *src, int p)
{
    unsigned short *d = (unsigned short*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[1]] | lut_green[src[2]] | lut_blue[src[3]];
	src += 4;
    }
    return ((unsigned char*)d-dest);
}

static int
bgr32_to_lut2(unsigned char *dest, unsigned char *src, int p)
{
    unsigned short *d = (unsigned short*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[3]] | lut_green[src[2]] | lut_blue[src[1]];
	src += 4;
    }
    return ((unsigned char*)d-dest);
}

static int
gray_to_lut2(unsigned char *dest, unsigned char *src, int p)
{
    unsigned short *d = (unsigned short*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[*src] | lut_green[*src] | lut_blue[*src];
	src++;
    }
    return ((unsigned char*)d-dest);
}

/* ------------------------------------------------------------------- */

static int
rgb24_to_lut4(unsigned char *dest, unsigned char *src, int p)
{
    unsigned int *d = (unsigned int*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[0]] | lut_green[src[1]] | lut_blue[src[2]];
	src += 3;
    }
    return ((unsigned char*)d-dest);
}

static int
bgr24_to_lut4(unsigned char *dest, unsigned char *src, int p)
{
    unsigned int *d = (unsigned int*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[2]] | lut_green[src[1]] | lut_blue[src[0]];
	src += 3;
    }
    return ((unsigned char*)d-dest);
}

static int
rgb32_to_lut4(unsigned char *dest, unsigned char *src, int p)
{
    unsigned int *d = (unsigned int*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[1]] | lut_green[src[2]] | lut_blue[src[3]];
	src += 4;
    }
    return ((unsigned char*)d-dest);
}

static int
bgr32_to_lut4(unsigned char *dest, unsigned char *src, int p)
{
    unsigned int *d = (unsigned int*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[src[3]] | lut_green[src[2]] | lut_blue[src[1]];
	src += 4;
    }
    return ((unsigned char*)d-dest);
}

static int
gray_to_lut4(unsigned char *dest, unsigned char *src, int p)
{
    unsigned int *d = (unsigned int*)dest;

    while (p-- > 0) {
	*(d++) = lut_red[*src] | lut_green[*src] | lut_blue[*src];
	src++;
    }
    return ((unsigned char*)d-dest);
}

/* ------------------------------------------------------------------- */

static struct ng_video_conv lut2_list[] = {
    {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_RGB24,
	priv:		rgb24_to_lut2,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_BGR24,
	priv:		bgr24_to_lut2,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_RGB32,
	priv:		rgb32_to_lut2,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_BGR32,
	priv:		bgr32_to_lut2,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_GRAY,
	priv:		gray_to_lut2,
    }
};

static struct ng_video_conv lut4_list[] = {
    {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_RGB24,
	priv:		rgb24_to_lut4,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_BGR24,
	priv:		bgr24_to_lut4,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_RGB32,
	priv:		rgb32_to_lut4,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_BGR32,
	priv:		bgr32_to_lut4,
    }, {
	NG_GENERIC_PACKED,
	fmtid_in:	VIDEO_GRAY,
	priv:		gray_to_lut4,
    }
};

static const int nconv2 = sizeof(lut2_list)/sizeof(struct ng_video_conv);
static const int nconv4 = sizeof(lut4_list)/sizeof(struct ng_video_conv);

void
ng_lut_init(unsigned long red_mask, unsigned long green_mask,
	    unsigned long blue_mask, int fmtid, int swap)
{
    static int      once=0;
    int             rgb_red_bits = 0;
    int             rgb_red_shift = 0;
    int             rgb_green_bits = 0;
    int             rgb_green_shift = 0;
    int             rgb_blue_bits = 0;
    int             rgb_blue_shift = 0;
    unsigned int    i;
    unsigned int    mask;

    if (once++) {
	fprintf(stderr,"panic: ng_lut_init called twice\n");
	exit(1);
    }

    for (i = 0; i < 32; i++) {
        mask = (1 << i);
        if (red_mask & mask)
            rgb_red_bits++;
        else if (!rgb_red_bits)
            rgb_red_shift++;
        if (green_mask & mask)
            rgb_green_bits++;
        else if (!rgb_green_bits)
            rgb_green_shift++;
        if (blue_mask & mask)
            rgb_blue_bits++;
        else if (!rgb_blue_bits)
            rgb_blue_shift++;
    }
#if 0
    printf("color: bits shift\n");
    printf("red  : %04i %05i\n", rgb_red_bits, rgb_red_shift);
    printf("green: %04i %05i\n", rgb_green_bits, rgb_green_shift);
    printf("blue : %04i %05i\n", rgb_blue_bits, rgb_blue_shift);
#endif
    
    if (rgb_red_bits > 8)
	for (i = 0; i < 256; i++)
	    lut_red[i] = (i << (rgb_red_bits + rgb_red_shift - 8));
    else
	for (i = 0; i < 256; i++)
	    lut_red[i] = (i >> (8 - rgb_red_bits)) << rgb_red_shift;
    
    if (rgb_green_bits > 8)
	for (i = 0; i < 256; i++)
	    lut_green[i] = (i << (rgb_green_bits + rgb_green_shift - 8));
    else
	for (i = 0; i < 256; i++)
	    lut_green[i] = (i >> (8 - rgb_green_bits)) << rgb_green_shift;
    
    if (rgb_blue_bits > 8)
	for (i = 0; i < 256; i++)
	    lut_blue[i] = (i << (rgb_blue_bits + rgb_blue_shift - 8));
    else
	for (i = 0; i < 256; i++)
	    lut_blue[i] = (i >> (8 - rgb_blue_bits)) << rgb_blue_shift;

    switch (ng_vfmt_to_depth[fmtid]) {
    case 16:
	if (swap) {
	    for (i = 0; i < 256; i++) {
		lut_red[i] = SWAP2(lut_red[i]);
		lut_green[i] = SWAP2(lut_green[i]);
		lut_blue[i] = SWAP2(lut_blue[i]);
	    }
	}
	for (i = 0; i < nconv2; i++)
	    lut2_list[i].fmtid_out = fmtid;
	ng_conv_register(lut2_list,nconv2);
	break;
    case 32:
	if (swap) {
	    for (i = 0; i < 256; i++) {
		lut_red[i] = SWAP4(lut_red[i]);
		lut_green[i] = SWAP4(lut_green[i]);
		lut_blue[i] = SWAP4(lut_blue[i]);
	    }
	}
	for (i = 0; i < nconv4; i++)
	    lut4_list[i].fmtid_out = fmtid;
	ng_conv_register(lut4_list,nconv4);
	break;
    }
}

Privacy Policy