Add facility to read frames in RGB & RGBA format
[imageviewer.git] / rawIO16P4.cpp
blob17ff74c41a8ee89331e198b4c74b79822edb123f
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 "rawIO16P4.h"
32 RawFrame& RawReader16P4::read(RawFrame& f)
34 assert(f.chroma == RawFrame::Cr444);
35 const int line_len = f.luma.width() * 2;
36 unsigned char* in_line = new unsigned char[line_len];
37 unsigned char* ptr;
39 /* read luma plane */
40 for (int y = 0; y < f.luma.height(); y++) {
41 if (fread(in_line, line_len, 1, file) < 1)
42 break;
43 ptr = in_line;
44 for (int x = 0; x < f.luma.width(); x++, ptr+=2)
45 f.luma[y][x] = ((ptr[0] << 8) | ptr[1]);
48 /* read c1 plane */
49 const int chroma_line_len = line_len;
50 for (int y = 0; y < f.cb.height(); y++) {
51 if (fread(in_line, chroma_line_len, 1, file) < 1)
52 break;
53 ptr = in_line;
54 for (int x = 0; x < f.cb.width(); x++, ptr+=2)
55 f.cb[y][x] = ((ptr[0] << 8) | ptr[1]);
58 /* read c2 plane */
59 for (int y = 0; y < f.cr.height(); y++) {
60 if (fread(in_line, chroma_line_len, 1, file) < 1)
61 break;
62 ptr = in_line;
63 for (int x = 0; x < f.cr.width(); x++, ptr+=2)
64 f.cr[y][x] = ((ptr[0] << 8) | ptr[1]);
67 free(in_line);
68 return f;
71 RawFrame& RawReader16P4::read()
73 throw;
76 void RawWriter16P4::write(const RawFrame& f) const
78 assert(f.chroma == RawFrame::Cr444);
79 const int line_len = f.luma.width() * 2;
80 unsigned char* out_line = new unsigned char[line_len];
81 unsigned char* ptr;
83 /* write luma plane */
84 for (int y = 0; y < f.luma.height(); y++) {
85 ptr = out_line;
86 for (int x = 0; x < f.luma.width(); x++, ptr+=2) {
87 int v = f.luma[y][x];
88 ptr[0] = v >> 8;
89 ptr[1] = v & 0xff;
91 fwrite(out_line, line_len, 1, file);
94 /* write c1 plane */
95 const int chroma_line_len = line_len;
96 for (int y = 0; y < f.cb.height(); y++) {
97 ptr = out_line;
98 for (int x = 0; x < f.cb.width(); x++, ptr+=2) {
99 int v = f.cb[y][x];
100 ptr[0] = v >> 8;
101 ptr[1] = v & 0xff;
103 fwrite(out_line, chroma_line_len, 1, file);
106 /* write c2 plane */
107 for (int y = 0; y < f.cr.height(); y++) {
108 ptr = out_line;
109 for (int x = 0; x < f.cr.width(); x++, ptr+=2) {
110 int v = f.cr[y][x];
111 ptr[0] = v >> 8;
112 ptr[1] = v & 0xff;
114 fwrite(out_line, chroma_line_len, 1, file);
117 free(out_line);