Add facility to read frames in RGB & RGBA format
[imageviewer.git] / rawIOi420.cpp
blob3f4e8510f58f88bfe437a2f3393917389f833e89
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 "rawIOi420.h"
32 // i420 is 4:2:0 planar Y, Cb, Cr
34 RawFrame& RawReaderi420::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 //Cb
54 for (int y = 0; y < f.cb.height(); y++) {
56 linesRead = fread(inLine, f.cb.width(), 1, infile);
58 for (int x = 0; x < f.cb.width(); x++)
59 f.cb[y][x] = inLine[x];
61 if (linesRead < 1)
62 goto exit;
65 //Cr
66 for (int y = 0; y < f.cr.height(); y++) {
68 linesRead = fread(inLine, f.cr.width(), 1, infile);
70 for (int x = 0; x < f.cr.width(); x++)
71 f.cr[y][x] = inLine[x];
73 if (linesRead < 1)
74 goto exit;
77 exit:
79 free(inLine);
80 return f;
83 RawFrame& RawReaderi420::read()
85 throw /* unknown size */;
88 void RawWriteri420::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.cb.height(); y++)
96 for (int x = 0; x < f.cb.width(); x++)
97 fputc(f.cb[y][x], outfile);
99 for (int y = 0; y < f.cr.height(); y++)
100 for (int x = 0; x < f.cr.width(); x++)
101 fputc(f.cr[y][x], outfile);