2 * readV210.c - read single uncompressed Quicktime YUV image.
3 * http://developer.apple.com/quicktime/icefloe/dispatch019.html#v210
11 #include "imagefile.h"
21 looksize(char *file, vlong size, int *pixels, int *lines, int *chunk)
28 * This may not always work, there could be an alias between file
29 * sizes of different standards stored in 8bits and 10 bits.
31 if((bp = Bopen(file, OREAD)) == nil)
33 while((s = Brdstr(bp, '\n', 1)) != nil){
34 if(tokenize(s, a, nelem(a)) < 3)
58 x >>= Shift + 2; /* +2 as we assume all input images are 10 bit */
67 BreadV210(Biobuf *bp, int colourspace)
72 ushort *mux, *end, *frm, *wr;
73 uchar *buf, *r, *g, *b;
75 int y1, y2, cb, cr, c, l, rd;
76 int chunk, lines, pixels;
80 if(colourspace != CYCbCr){
81 werrstr("BreadV210: unknown colour space %d", colourspace);
85 if((d = dirfstat(Bfildes(bp))) != nil){
90 fprint(2, "cannot stat input, assuming pixelsx576x10bit\n");
91 sz = Pixels * R601pal * 2L + (pixels * R601pal / 2L);
94 if(looksize("/lib/video.specs", sz, &pixels, &lines, &chunk) == -1){
95 werrstr("file spec not in /lib/video.specs\n");
99 if((a = calloc(sizeof(Rawimage), 1)) == nil)
100 sysfatal("no memory");
102 if((array = calloc(sizeof(Rawimage * ), 2)) == nil)
103 sysfatal("no memory");
109 a->chanlen = pixels * lines;
110 a->r = Rect(0, 0, pixels, lines);
112 if((frm = malloc(pixels*2*lines*sizeof(ushort))) == nil)
115 for(c = 0; c < 3; c++)
116 if((a->chans[c] = malloc(pixels*lines)) == nil)
119 if((buf = malloc(chunk)) == nil)
122 for(l = 0; l < lines; l++){
123 if(Bread(bp, buf, chunk) == -1)
127 wr = &frm[l*pixels*2];
128 end = &frm[(l+1)*pixels*2];
131 for(i = 0; i < 4; i++)
132 t += buf[rd+i] << 8*i;
134 *wr++ = t>>10 & 0x3ff;
135 *wr++ = t>>20 & 0x3ff;
141 end = frm + pixels * lines * 2;
146 if(pixels == Pixels && lines != R601pal){ // 625
147 F1 = floor(1.402 * (1 << Shift));
148 F2 = floor(0.34414 * (1 << Shift));
149 F3 = floor(0.71414 * (1 << Shift));
150 F4 = floor(1.772 * (1 << Shift));
153 F1 = floor(1.5748 * (1 << Shift));
154 F2 = floor(0.1874 * (1 << Shift));
155 F3 = floor(0.4681 * (1 << Shift));
156 F4 = floor(1.8560 * (1 << Shift));
160 * Fixme: fixed colourspace conversion at present
165 y1 = (int)*mux++ << Shift;
167 y2 = (int)*mux++ << Shift;
169 *r++ = clip(y1 + F1*cr);
170 *g++ = clip(y1 - F2*cb - F3*cr);
171 *b++ = clip((y1 + F4*cb));
173 *r++ = clip(y2 + F1*cr);
174 *g++ = clip(y2 - F2*cb - F3*cr);
175 *b++ = clip((y2 + F4*cb));
182 for(c = 0; c < 3; c++)
193 readV210(int fd, int colorspace)
198 if(Binit(&b, fd, OREAD) < 0)
200 a = BreadV210(&b, colorspace);