1 /* readyuv.c - read an Abekas A66 style image file. Steve Simon, 2003 */
10 * ITU/CCIR Rec601 states:
13 * B = Y + 1.77305 * Cb
14 * G = Y - 0.72414 * Cr - 0.34414 * Cb
20 * or, if 10bit is used
27 PAL = 576, NTSC = 486 };
30 static int lsbtab[] = { 6, 4, 2, 0};
46 Breadyuv(Biobuf *bp, int colourspace)
49 Rawimage * a, **array;
51 ushort * mux, *end, *frm;
52 uchar buf[720 * 2], *r, *g, *b;
53 int y1, y2, cb, cr, sz, c, l, w, base, bits, lines;
56 if (colourspace != CYCbCr) {
57 errstr(ebuf, sizeof ebuf); /* throw it away */
58 werrstr("ReadYUV: unknown colour space %d", colourspace);
62 if ((a = calloc(sizeof(Rawimage), 1)) == nil)
63 sysfatal("no memory");
65 if ((array = calloc(sizeof(Rawimage * ), 2)) == nil)
66 sysfatal("no memory");
70 if ((d = dirfstat(Bfildes(bp))) != nil) {
74 fprint(2, "cannot stat input, assuming 720x576x10bit\n");
75 sz = 720 * PAL * 2L + (720 * PAL / 2L);
79 case 720 * PAL * 2: /* 625 x 8bit */
83 case 720 * NTSC * 2: /* 525 x 8bit */
87 case 720 * PAL * 2 + (720 * PAL / 2) : /* 625 x 10bit */
91 case 720 * NTSC * 2 + (720 * NTSC / 2) : /* 525 x 10bit */
96 e = "unknown file size";
100 /* print("bits=%d pixels=%d lines=%d\n", bits, 720, lines); */
104 a->chanlen = 720 * lines;
105 a->r = Rect(0, 0, 720, lines);
108 if ((frm = malloc(720 * 2 * lines * sizeof(ushort))) == nil)
111 for (c = 0; c < 3; c++)
112 if ((a->chans[c] = malloc(720 * lines)) == nil)
116 for (l = 0; l < lines; l++) {
117 if (Bread(bp, buf, 720 * 2) == -1)
121 for (w = 0; w < 720 * 2; w++)
122 frm[base + w] = ((ushort)buf[w]) << 2;
127 for (l = 0; l < lines; l++) {
128 if (Bread(bp, buf, 720 / 2) == -1)
133 for (w = 0; w < 720 * 2; w++)
134 frm[base + w] |= buf[w / 4] >> lsbtab[w % 4];
138 end = frm + 720 * lines * 2;
145 y1 = (*mux++ - 64) * 76310;
147 y2 = (*mux++ - 64) * 76310;
149 *r++ = clip((104635 * cr) + y1);
150 *g++ = clip((-25690 * cb + -53294 * cr) + y1);
151 *b++ = clip((132278 * cb) + y1);
153 *r++ = clip((104635 * cr) + y2);
154 *g++ = clip((-25690 * cb + -53294 * cr) + y2);
155 *b++ = clip((132278 * cb) + y2);
162 errstr(ebuf, sizeof ebuf);
165 errstr(ebuf, sizeof ebuf);
167 for (c = 0; c < 3; c++)
178 readyuv(int fd, int colorspace)
183 if (Binit(&b, fd, OREAD) < 0)
185 a = Breadyuv(&b, colorspace);