aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/jpg/readyuv.c
blob: eae2764e7d6d19dca0d02781bb1bffacef6592f6 (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
/* readyuv.c - read an Abekas A66 style image file.   Steve Simon, 2003 */
#include <u.h>
#include <libc.h>
#include <bio.h>
#include <draw.h>
#include <ctype.h>
#include "imagefile.h"

/*
 * ITU/CCIR Rec601 states:
 *
 * R = y + 1.402 * Cr
 * B = Y + 1.77305 * Cb
 * G = Y - 0.72414 * Cr - 0.34414 * Cb
 *
 *	using 8 bit traffic
 * Y = 16 + 219 * Y
 * Cr = 128 + 224 * Cr
 * Cb = 128 + 224 * Cb
 * 	or, if 10bit is used
 * Y = 64 + 876 * Y
 * Cr = 512 + 896 * Cr
 * Cb = 512 + 896 * Cb
 */

enum {
	PAL = 576, NTSC = 486 };


static int lsbtab[] = { 6, 4, 2, 0};

static int 
clip(int x)
{
	x >>= 18;

	if (x > 255)
		return 0xff;
	if (x <= 0)
		return 0;
	return x;
}


Rawimage**
Breadyuv(Biobuf *bp, int colourspace)
{
	Dir * d;
	Rawimage * a, **array;
	char	*e, ebuf[128];
	ushort * mux, *end, *frm;
	uchar buf[720 * 2], *r, *g, *b;
	int	y1, y2, cb, cr, sz, c, l, w, base, bits, lines;

	frm = 0;
	if (colourspace != CYCbCr) {
		errstr(ebuf, sizeof ebuf);	/* throw it away */
		werrstr("ReadYUV: unknown colour space %d", colourspace);
		return nil;
	}

	if ((a = calloc(sizeof(Rawimage), 1)) == nil)
		sysfatal("no memory");

	if ((array = calloc(sizeof(Rawimage * ), 2)) == nil)
		sysfatal("no memory");
	array[0] = a;
	array[1] = nil;

	if ((d = dirfstat(Bfildes(bp))) != nil) {
		sz = d->length;
		free(d);
	} else {
		fprint(2, "cannot stat input, assuming 720x576x10bit\n");
		sz = 720 * PAL * 2L + (720 * PAL / 2L);
	}

	switch (sz) {
	case 720 * PAL * 2:				// 625 x 8bit
		bits = 8;
		lines = PAL;
		break;
	case 720 * NTSC * 2:				// 525 x 8bit
		bits = 8;
		lines = NTSC;
		break;
	case 720 * PAL * 2 + (720 * PAL / 2) :		// 625 x 10bit
			bits = 10;
		lines = PAL;
		break;
	case 720 * NTSC * 2 + (720 * NTSC / 2) :	// 525 x 10bit
			bits = 10;
		lines = NTSC;
		break;
	default:
		e = "unknown file size";
		goto Error;
	}

	//	print("bits=%d pixels=%d lines=%d\n", bits, 720, lines);
	//
	a->nchans = 3;
	a->chandesc = CRGB;
	a->chanlen = 720 * lines;
	a->r = Rect(0, 0, 720, lines);

	e = "no memory";
	if ((frm = malloc(720 * 2 * lines * sizeof(ushort))) == nil)
		goto Error;

	for (c = 0; c  < 3; c++)
		if ((a->chans[c] = malloc(720 * lines)) == nil)
			goto Error;

	e = "read file";
	for (l = 0; l < lines; l++) {
		if (Bread(bp, buf, 720 * 2) == -1)
			goto Error;

		base = l * 720 * 2;
		for (w = 0; w < 720 * 2; w++)
			frm[base + w] = ((ushort)buf[w]) << 2;
	}


	if (bits == 10)
		for (l = 0; l < lines; l++) {
			if (Bread(bp, buf, 720 / 2) == -1)
				goto Error;


			base = l * 720 * 2;
			for (w = 0; w < 720 * 2; w++)
				frm[base + w] |= buf[w / 4] >> lsbtab[w % 4];
		}

	mux = frm;
	end = frm + 720 * lines * 2;
	r = a->chans[0];
	g = a->chans[1];
	b = a->chans[2];

	while (mux < end) {
		cb = *mux++ - 512;
		y1 = (*mux++ - 64) * 76310;
		cr = *mux++ - 512;
		y2 = (*mux++ - 64) * 76310;

		*r++ = clip((104635 * cr) + y1);
		*g++ = clip((-25690 * cb + -53294 * cr) + y1);
		*b++ = clip((132278 * cb) + y1);

		*r++ = clip((104635 * cr) + y2);
		*g++ = clip((-25690 * cb + -53294 * cr) + y2);
		*b++ = clip((132278 * cb) + y2);
	}
	free(frm);
	return array;

Error:

	errstr(ebuf, sizeof ebuf);
	if (ebuf[0] == 0)
		strcpy(ebuf, e);
	errstr(ebuf, sizeof ebuf);

	for (c = 0; c < 3; c++)
		free(a->chans[c]);
	free(a->cmap);
	free(array[0]);
	free(array);
	free(frm);
	return nil;
}


Rawimage**
readyuv(int fd, int colorspace)
{
	Rawimage * *a;
	Biobuf b;

	if (Binit(&b, fd, OREAD) < 0)
		return nil;
	a = Breadyuv(&b, colorspace);
	Bterm(&b);
	return a;
}