1 /* ***** BEGIN LICENSE BLOCK *****
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
27 * ***** END LICENSE BLOCK ***** */
34 void RawWriterpgm::writeCr42x(const RawFrame
& f
) const
39 fprintf(outfile
, "P5 %d %d %d\n", f
.luma
.width(), f
.luma
.height()
40 + f
.cr
.height(), (1 << depth
) - 1);
43 int mask
= (1 << (depth
- 8)) - 1;
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
);
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
94 assert(f
.chroma
== RawFrame::YOnly
);
96 fprintf(outfile
, "P5 %d %d %d\n", f
.luma
.width(), f
.luma
.height(), (1
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
);
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
);
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];
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
];
169 RawFrame
& RawReaderpgm::read()