aboutsummaryrefslogtreecommitdiffstats
path: root/yuv2rgb.c
blob: bc36a64b657f06feacc8048435a041c9d3488010 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pcd.h"

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

#define RANGE       320

#define RED_NUL     137
#define BLUE_NUL    156

#define LUN_MUL     360
#define RED_MUL     512
#define BLUE_MUL    512
#define GREEN1_MUL  (-RED_MUL/2)
#define GREEN2_MUL  (-BLUE_MUL/6)

#define RED_ADD     (-RED_NUL*RED_MUL)
#define BLUE_ADD    (-BLUE_NUL*BLUE_MUL)
#define GREEN1_ADD  (-RED_ADD/2)
#define GREEN2_ADD  (-BLUE_ADD/6)

static int          LUT_flag = 0;
static uint_fast16_t LUT_gray_char[256];
static uint_fast16_t LUT_gray_int[256];
static uint_fast16_t LUT_red[256];
static uint_fast16_t LUT_blue[256];
static uint_fast16_t LUT_green1[256];
static uint_fast16_t LUT_green2[256];

int32_t    LUT_range[256 + 2 * RANGE];

uint32_t   LUT_15_red[256];
uint32_t   LUT_15_green[256];
uint32_t   LUT_15_blue[256];

uint32_t   LUT_16_red[256];
uint32_t   LUT_16_green[256];
uint32_t   LUT_16_blue[256];

uint32_t   LUT_24_red[256];
uint32_t   LUT_24_green[256];
uint32_t   LUT_24_blue[256];

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

void
pcd_get_LUT_init()
{
    register int    i;

    /* only once needed */
    if (LUT_flag)
	return;
    LUT_flag = 1;

    /* init Lookup tables */
    for (i = 0; i < 256; i++) {
	LUT_gray_int[i] = i * LUN_MUL >> 8;
	LUT_red[i] = (RED_ADD + i * RED_MUL) >> 8;
	LUT_blue[i] = (BLUE_ADD + i * BLUE_MUL) >> 8;
	LUT_green1[i] = (GREEN1_ADD + i * GREEN1_MUL) >> 8;
	LUT_green2[i] = (GREEN2_ADD + i * GREEN2_MUL) >> 8;
	LUT_gray_char[i] = LUT_gray_int[i] > 255 ? 255 : LUT_gray_int[i];

	LUT_15_red[i] = (i & 0xf8) << 7;	/* bits -rrrrr-- -------- */
	LUT_15_green[i] = (i & 0xf8) << 2;	/* bits------gg ggg----- */
	LUT_15_blue[i] = (i & 0xf8) >> 3;	/* bits-------- ---bbbbb */

	LUT_16_red[i] = (i & 0xf8) << 8;	/* bits rrrrr--- -------- */
	LUT_16_green[i] = (i & 0xfc) << 3;	/* bits -----ggg ggg----- */
	LUT_16_blue[i] = (i & 0xf8) >> 3;	/* bits -------- ---bbbbb */

	LUT_24_red[i] = i << 16;	/* byte -r-- */
	LUT_24_green[i] = i << 8;	/* byte --g- */
	LUT_24_blue[i] = i;		/* byte ---b */
    }
    for (i = 0; i < RANGE; i++)
	LUT_range[i] = 0;
    for (; i < RANGE + 256; i++)
	LUT_range[i] = i - RANGE;
    for (; i < 2 * RANGE + 256; i++)
	LUT_range[i] = 255;
}

void
pcd_set_lookup(struct PCD_IMAGE *img, uint32_t *red,
	       uint32_t *green, uint32_t *blue)
{
    img->lut_red = red;
    img->lut_green = green;
    img->lut_blue = blue;
}

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

#define GET_RED   (LUT_range[RANGE + gray + \
			    LUT_red[red[x]]])
#define GET_GREEN (LUT_range[RANGE + gray + \
			    LUT_green1[red[x]] + \
			    LUT_green2[blue[x]]])
#define GET_BLUE  (LUT_range[RANGE + gray + \
			    LUT_blue[blue[x]]])

static int
pcd_get_image_line_0(struct PCD_IMAGE *img, int y,
		     unsigned char *dest, int type, int scale)
{
    unsigned char red[3072];
    unsigned char blue[3072];
    int           bytes, maxx;

    switch (type) {
    case PCD_TYPE_GRAY:
	bytes = 1;
	break;
    case PCD_TYPE_RGB:
	bytes = 3;
	break;
    case PCD_TYPE_BGR:
	bytes = 3;
	break;
    case PCD_TYPE_LUT_SHORT:
	bytes = 2;
	break;
    case PCD_TYPE_LUT_LONG:
	bytes = 4;
	break;
    default:
	fprintf(stderr, "Oops: invalid type (%i) for output format\n", type);
	exit(1);
    }

    if (img->rot & 2) {
	y = (img->height >> scale) - y - 1;
	dest += ((img->width >> scale) - 1) * bytes;
	bytes = -bytes;
    }
    if (type != PCD_TYPE_GRAY && !scale) {
	register int    x;
	register unsigned char *src1, *src2;

	maxx = (img->width >> 1) - 1;
	if (y & 1) {
	    src1 = img->blue[y >> 1];
	    src2 = img->blue[(y + 1 == img->height ? y : y + 1) >> 1];
	    for (x = 0; x < maxx; x++) {
		blue[x * 2] = (src1[x] + src2[x] + 1) >> 1;
		blue[x * 2 + 1] = (src1[x] + src1[x + 1] + src2[x] + src2[x + 1] + 2) >> 2;
	    }
	    blue[x * 2 + 1] = blue[x * 2] = (src1[x] + src2[x] + 1) >> 1;

	    src1 = img->red[y >> 1];
	    src2 = img->red[(y + 1 == img->height ? y : y + 1) >> 1];
	    for (x = 0; x < maxx; x++) {
		red[x * 2] = (src1[x] + src2[x] + 1) >> 1;
		red[x * 2 + 1] = (src1[x] + src1[x + 1] + src2[x] + src2[x + 1] + 2) >> 2;
	    }
	    red[x * 2 + 1] = red[x * 2] = (src1[x] + src2[x] + 1) >> 1;
	} else {
	    src1 = img->blue[y >> 1];
	    for (x = 0; x < maxx; x++) {
		blue[x * 2] = src1[x];
		blue[x * 2 + 1] = (src1[x] + src1[x + 1] + 1) >> 1;
	    }
	    blue[x * 2 + 1] = blue[x * 2] = src1[x];

	    src1 = img->red[y >> 1];
	    for (x = 0; x < maxx; x++) {
		red[x * 2] = src1[x];
		red[x * 2 + 1] = (src1[x] + src1[x + 1] + 1) >> 1;
	    }
	    red[x * 2 + 1] = red[x * 2] = src1[x];
	}
    }
    if (type != PCD_TYPE_GRAY && scale) {
	memcpy(blue, img->blue[y], img->width >> 1);
	memcpy(red, img->red[y], img->width >> 1);
    }
    maxx = img->width >> scale;
    switch (type) {
    case PCD_TYPE_GRAY:
	{
	    register int    x;
	    register unsigned char *luma = img->luma[y << scale];

	    for (x = 0; x < maxx; x++, dest += bytes)
		*dest = LUT_gray_char[luma[x << scale]];
	}
	break;
    case PCD_TYPE_RGB:
	{
	    register int    x, gray;
	    register unsigned char *luma = img->luma[y << scale];

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale]];
		dest[0] = GET_RED;
		dest[1] = GET_GREEN;
		dest[2] = GET_BLUE;
	    }
	}
	break;
    case PCD_TYPE_BGR:
	{
	    register int    x, gray;
	    register unsigned char *luma = img->luma[y << scale];

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale]];
		dest[0] = GET_BLUE;
		dest[1] = GET_GREEN;
		dest[2] = GET_RED;
	    }
	}
	break;
    case PCD_TYPE_LUT_SHORT:
	{
	    register int    x, gray;
	    register unsigned char *luma = img->luma[y << scale];
	    uint32_t  *lr = img->lut_red;
	    uint32_t  *lg = img->lut_green;
	    uint32_t  *lb = img->lut_blue;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale]];
		*((uint16_t *) dest) =
		    lr[GET_RED] | lg[GET_GREEN] | lb[GET_BLUE];
	    }
	}
	break;
    case PCD_TYPE_LUT_LONG:
	{
	    register int    x, gray;
	    register unsigned char *luma = img->luma[y << scale];
	    uint32_t  *lr = img->lut_red;
	    uint32_t  *lg = img->lut_green;
	    uint32_t  *lb = img->lut_blue;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale]];
		*((uint32_t *) dest) =
		    lr[GET_RED] | lg[GET_GREEN] | lb[GET_BLUE];
	    }
	}
	break;
    default:
	exit(1);
    }
    return 0;
}

static int
pcd_get_image_line_90(struct PCD_IMAGE *img, int y,
		      unsigned char *dest, int type, int scale)
{
    unsigned char red[3072];
    unsigned char blue[3072];
    unsigned char **luma = img->luma;
    int             bytes, maxx, y1, y2;

    switch (type) {
    case PCD_TYPE_GRAY:
	bytes = 1;
	break;
    case PCD_TYPE_RGB:
	bytes = 3;
	break;
    case PCD_TYPE_BGR:
	bytes = 3;
	break;
    case PCD_TYPE_LUT_SHORT:
	bytes = 2;
	break;
    case PCD_TYPE_LUT_LONG:
	bytes = 4;
	break;
    default:
	fprintf(stderr, "Oops: invalid type (%i) for output format\n", type);
	exit(1);
    }

    if (!(img->rot & 2)) {
	y = (img->width >> scale) - y - 1;
    } else {
	dest += ((img->height >> scale) - 1) * bytes;
	bytes = -bytes;
    }

    if (type != PCD_TYPE_GRAY && !scale) {
	register int    x;
	register unsigned char **src;

	y1 = y >> 1;
	y2 = (y + 1 == img->width ? y : y + 1) >> 1;
	maxx = (img->height >> 1) - 1;

	if (y & 1) {
	    src = img->blue;
	    for (x = 0; x < maxx; x++) {
		blue[x * 2] = (src[x][y1] + src[x][y2] + 1) >> 1;
		blue[x * 2 + 1] = (src[x][y1] + src[x][y2] +
			       src[x + 1][y1] + src[x + 1][y2] + 2) >> 2;
	    }
	    blue[x * 2 + 1] = blue[x * 2] = (src[x][y1] + src[x][y2] + 1) >> 1;

	    src = img->red;
	    for (x = 0; x < maxx; x++) {
		red[x * 2] = (src[x][y1] + src[x][y2] + 1) >> 1;
		red[x * 2 + 1] = (src[x][y1] + src[x][y2] +
			       src[x + 1][y1] + src[x + 1][y2] + 2) >> 2;
	    }
	    red[x * 2 + 1] = red[x * 2] = (src[x][y1] + src[x][y2] + 1) >> 1;
	} else {
	    src = img->blue;
	    for (x = 0; x < maxx; x++) {
		blue[x * 2] = src[x][y1];
		blue[x * 2 + 1] = (src[x][y1] + src[x + 1][y1] + 1) >> 1;
	    }
	    blue[x * 2 + 1] = blue[x * 2] = src[x][y1];

	    src = img->red;
	    for (x = 0; x < maxx; x++) {
		red[x * 2] = src[x][y1];
		red[x * 2 + 1] = (src[x][y1] + src[x + 1][y1] + 1) >> 1;
	    }
	    red[x * 2 + 1] = red[x * 2] = src[x][y1];
	}
    }
    if (type != PCD_TYPE_GRAY && scale) {
	register int    x;
	register unsigned char **src;

	maxx = (img->height >> 1);

	src = img->blue;
	for (x = 0; x < maxx; x++)
	    blue[x] = src[x][y];

	src = img->red;
	for (x = 0; x < maxx; x++)
	    red[x] = src[x][y];
    }
    maxx = (img->height >> scale);
    switch (type) {
    case PCD_TYPE_GRAY:
	{
	    register int    x;

	    for (x = 0; x < maxx; x++, dest += bytes)
		*dest = LUT_gray_char[luma[x << scale][y << scale]];
	}
	break;
    case PCD_TYPE_RGB:
	{
	    register int    x, gray;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale][y << scale]];
		dest[0] = GET_RED;
		dest[1] = GET_GREEN;
		dest[2] = GET_BLUE;
	    }
	}
	break;
    case PCD_TYPE_BGR:
	{
	    register int    x, gray;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale][y << scale]];
		dest[0] = GET_BLUE;
		dest[1] = GET_GREEN;
		dest[2] = GET_RED;
	    }
	}
	break;
    case PCD_TYPE_LUT_SHORT:
	{
	    register int    x, gray;
	    uint32_t  *lr = img->lut_red;
	    uint32_t  *lg = img->lut_green;
	    uint32_t  *lb = img->lut_blue;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale][y << scale]];
		*((uint16_t *) dest) =
		    lr[GET_RED] | lg[GET_GREEN] | lb[GET_BLUE];
	    }
	}
	break;
    case PCD_TYPE_LUT_LONG:
	{
	    register int    x, gray;
	    uint32_t  *lr = img->lut_red;
	    uint32_t  *lg = img->lut_green;
	    uint32_t  *lb = img->lut_blue;

	    for (x = 0; x < maxx; x++, dest += bytes) {
		gray = LUT_gray_int[luma[x << scale][y << scale]];
		*((uint32_t *) dest) =
		    lr[GET_RED] | lg[GET_GREEN] | lb[GET_BLUE];
	    }
	}
	break;
    default:
	exit(1);
    }
    return 0;
}

int
pcd_get_image_line(struct PCD_IMAGE *img, int y,
		   unsigned char *dest, int type, int scale)
{
    if (img->res == 0) {
	fprintf(stderr, "Oops: invalid res %i, have you called pcd_select()?\n",
		img->res);
	exit(1);
    }
    if (img->rot & 1)
	return pcd_get_image_line_90(img, y, dest, type, scale);
    else
	return pcd_get_image_line_0(img, y, dest, type, scale);
}

int
pcd_get_image(struct PCD_IMAGE *img, unsigned char *dest, int type, int scale)
{
    int             y, maxx, maxy, bytes;

    if (img->res == 0) {
	fprintf(stderr, "Oops: invalid res %i, have you called pcd_select()?\n",
		img->res);
	exit(1);
    }
    switch (type) {
    case PCD_TYPE_GRAY:
	bytes = 1;
	break;
    case PCD_TYPE_RGB:
	bytes = 3;
	break;
    case PCD_TYPE_BGR:
	bytes = 3;
	break;
    case PCD_TYPE_LUT_SHORT:
	bytes = 2;
	break;
    case PCD_TYPE_LUT_LONG:
	bytes = 4;
	break;
    default:
	fprintf(stderr, "Oops: invalid type (%i) for output format\n", type);
	exit(1);
    }
    maxx = (img->rot & 1 ? img->height : img->width) >> scale;
    maxy = (img->rot & 1 ? img->width : img->height) >> scale;

    for (y = 0; y < maxy; y++, dest += bytes * maxx) {
	ROTOR(y);
	pcd_get_image_line(img, y, dest, type, scale);
    }
    TELL('*');

    return 0;
}