Add facility to read frames in RGB & RGBA format
[imageviewer.git] / rawIOYV12.cpp
blobc7809c09946bf91fc418d2d50cac5b1804a9e87d
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$
5 * The MIT License
7 * Copyright (c) 2008 BBC Research
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
27 * ***** END LICENSE BLOCK ***** */
29 #include <assert.h>
30 #include "rawIOYV12.h"
32 // yv12 is 4:2:0 planar Y, Cr, Cb
34 RawFrame& RawReaderYV12::read(RawFrame& f)
36 assert(f.chroma == RawFrame::Cr420);
38 size_t linesRead;
39 unsigned char *inLine = new unsigned char[f.luma.width()];
41 //luminance
42 for (int y = 0; y < f.luma.height(); y++) {
44 linesRead = fread(inLine, f.luma.width(), 1, infile);
46 for (int x = 0; x < f.luma.width(); x++)
47 f.luma[y][x] = inLine[x];
49 if (linesRead < 1)
50 goto exit;
53 //Cr
54 for (int y = 0; y < f.cr.height(); y++) {
56 linesRead = fread(inLine, f.cr.width(), 1, infile);
58 for (int x = 0; x < f.cr.width(); x++)
59 f.cr[y][x] = inLine[x];
61 if (linesRead < 1)
62 goto exit;
65 //Cb
66 for (int y = 0; y < f.cb.height(); y++) {
68 linesRead = fread(inLine, f.cb.width(), 1, infile);
70 for (int x = 0; x < f.cb.width(); x++)
71 f.cb[y][x] = inLine[x];
73 if (linesRead < 1)
74 goto exit;
77 exit:
79 free(inLine);
80 return f;
83 RawFrame& RawReaderYV12::read()
85 throw /* unknown size */;
88 void RawWriterYV12::write(const RawFrame& f) const
90 assert(f.chroma == RawFrame::Cr420);
91 for (int y = 0; y < f.luma.height(); y++)
92 for (int x = 0; x < f.luma.width(); x++)
93 fputc(f.luma[y][x], outfile);
95 for (int y = 0; y < f.cr.height(); y++)
96 for (int x = 0; x < f.cr.width(); x++)
97 fputc(f.cr[y][x], outfile);
99 for (int y = 0; y < f.cb.height(); y++)
100 for (int x = 0; x < f.cb.width(); x++)
101 fputc(f.cb[y][x], outfile);