Add facility to read frames in RGB & RGBA format
[imageviewer.git] / rawIOpgm.cpp
blob0feb7564f2e0301b2b43a5e8a57ab91dfdd41b8a
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 <math.h>
32 #include "rawIOpgm.h"
34 void RawWriterpgm::writeCr42x(const RawFrame& f) const
36 /* YYYY (Planar)
37 * CbCr
39 fprintf(outfile, "P5 %d %d %d\n", f.luma.width(), f.luma.height()
40 + f.cr.height(), (1 << depth) - 1);
42 if (depth > 8) {
43 int mask = (1 << (depth - 8)) - 1;
44 int zero_shift = 0;
45 /* luma first */
46 for (int y = 0; y < f.luma.height(); y++)
47 for (int x = 0; x < f.luma.width(); x++) {
48 fputc((f.luma[y][x] >> 8) & mask, outfile);
49 fputc((f.luma[y][x] & 0xff), outfile);
52 /* chroma second (side by side) */
53 for (int y = 0; y < f.cb.height(); y++) {
54 for (int x = 0; x < f.cb.width(); x++) {
55 int val = zero_shift + f.cb[y][x];
56 fputc((val >> 8) & mask, outfile);
57 fputc(val & 0xff, outfile);
60 for (int x = 0; x < f.cr.width(); x++) {
61 int val = zero_shift + f.cr[y][x];
62 fputc((val >> 8) & mask, outfile);
63 fputc(val & 0xff, outfile);
67 else {
68 int zero_shift = 0;
69 /* luma first */
70 for (int y = 0; y < f.luma.height(); y++)
71 for (int x = 0; x < f.luma.width(); x++) {
72 fputc((f.luma[y][x] & 0xff), outfile);
75 /* chroma second (side by side) */
76 for (int y = 0; y < f.cb.height(); y++) {
77 for (int x = 0; x < f.cb.width(); x++) {
78 fputc((zero_shift + f.cb[y][x]) & 0xff, outfile);
81 for (int x = 0; x < f.cr.width(); x++) {
82 fputc((zero_shift + f.cr[y][x]) & 0xff, outfile);
89 void RawWriterpgm::writeYOnly(const RawFrame& f) const
91 /* YYYY (Planar)
92 * CbCr
94 assert(f.chroma == RawFrame::YOnly);
96 fprintf(outfile, "P5 %d %d %d\n", f.luma.width(), f.luma.height(), (1
97 << depth) - 1);
99 if (depth > 8) {
100 int mask = (1 << (depth - 8)) - 1;
101 for (int y = 0; y < f.luma.height(); y++)
102 for (int x = 0; x < f.luma.width(); x++) {
103 fputc((f.luma[y][x] >> 8) & mask, outfile);
104 fputc((f.luma[y][x] & 0xff), outfile);
107 else {
108 for (int y = 0; y < f.luma.height(); y++)
109 for (int x = 0; x < f.luma.width(); x++) {
110 fputc((f.luma[y][x] & 0xff), outfile);
115 void RawReaderpgm::readHeader()
117 // This works for our files, but is not generalised:
118 fscanf(infile, "P5 %d %d %d\n", &_width, &_height, &_maxval);
120 _depth = (int)ceil(log((double)_maxval) / log(2.0));
123 RawFrame& RawReaderpgm::read(RawFrame &f)
125 //luminance data only
126 assert(f.chroma == RawFrame::YOnly);
128 size_t linesRead;
129 size_t lineLen;
131 if (_maxval > 255) {
132 lineLen = f.luma.width() * 2;
133 unsigned char *inLine = new unsigned char[lineLen];
135 for (int y = 0; y < _height; y++) {
137 linesRead = fread(inLine, lineLen, 1, infile);
139 for (int in=0, x=0; x < _width; x++, in+=2)
140 f.luma[y][x]=inLine[in]*256 + inLine[in+1];
142 if (linesRead < 1)
143 goto exit1;
145 exit1: free(inLine);
147 else {
148 lineLen = f.luma.width();
149 unsigned char *inLine = new unsigned char[lineLen];
151 for (int y = 0; y < _height; y++) {
153 linesRead = fread(inLine, lineLen, 1, infile);
155 for (int x = 0; x < _width; x++)
156 f.luma[y][x]=inLine[x];
158 if (linesRead < 1)
159 goto exit2;
161 exit2: free(inLine);
164 //if (feof(infile))
165 // throw;
166 return f;
169 RawFrame& RawReaderpgm::read()
171 throw;