-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathreadframedpx.cpp
483 lines (428 loc) · 11.8 KB
/
readframedpx.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//-----------------------------------------------------------------------------
// This file is part of AEO-Light
//
// Copyright (c) 2016 University of South Carolina
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your
// option) any later version.
//
// AEO-Light is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Funding for AEO-Light development was provided through a grant from the
// National Endowment for the Humanities
//-----------------------------------------------------------------------------
#include <iostream>
#include <sstream>
#include <string>
#include <cmath>
#include <errno.h>
#include "DPX.h"
#include "readframedpx.h"
#include "aeoexception.h"
using namespace dpx;
/* ReadFrameDPX - read a single frame from a DPX file
* arguments:
* dpxfn: the filename of the dpx file
* buf: an existing target buffer of sufficient size, or NULL
*
* The DPX image is returned in buf (which is also returned by the function)
* in row-major order, top-to-bottom, left-to-right
*/
double *ReadFrameDPX(const char *dpxfn, double *buf=NULL)
{
InStream img;
// keep this around, since all the frames will be the same size
static unsigned char *byteBuf;
if(!img.Open(dpxfn))
{
QString msg;
msg += "ReadFrameDPX: Cannot open ";
msg += dpxfn;
msg += "\n";
if(errno)
msg += strerror(errno);
throw AeoException(msg);
}
dpx::Reader dpx;
dpx.SetInStream(&img);
if(!dpx.ReadHeader())
{
QString msg;
msg += "ReadFrameDPX: Cannot parse DPX header of ";
msg += dpxfn;
msg += "\n";
if(errno)
msg += strerror(errno);
img.Close();
throw AeoException(msg);
}
dpx::DataSize size = dpx.header.ComponentDataSize(0);
int bitDepth = dpx.header.ComponentByteCount(0) * 8;
int numChannels = dpx.header.ImageElementComponentCount(0);
if(buf == NULL)
{
buf = new double [dpx.header.Width() * dpx.header.Height()];
if(buf == NULL)
{
throw AeoException("Out of Memory: DPX buf");
}
}
if(byteBuf == NULL)
{
byteBuf = new unsigned char
[dpx.header.Width() * dpx.header.Height() *
numChannels * dpx.header.ComponentByteCount(0)];
if(byteBuf == NULL)
{
throw AeoException("Out of Memory: DPX byteBuf");
}
}
dpx.ReadImage(byteBuf);
// Convert from uint8 [0-255] or unit16 [0-65535] to double [0-1]
// Note that the color-to-grayscale isn't weighted to give green
// more influence: so the picture area may not "look nice" to human
// perception, but the soundtrack area isn't in color anyway, and the
// portions of the code that care about the picture area will work
// just as well with this alternate response scale (equal weight) as
// with the human perception scale, and the computation is faster.
long i;
double scale;
if(bitDepth == 8)
{
scale = 1.0/(numChannels * 0x00FF);
if(numChannels == 1)
{
for(i=0; i< dpx.header.Width() * dpx.header.Height(); i++)
buf[i] = double(byteBuf[i]) * scale;
}
else
{
for(i=0; i< dpx.header.Width() * dpx.header.Height(); i++)
buf[i] =
(int(byteBuf[3*i]) +
int(byteBuf[3*i+1]) +
int(byteBuf[3*i+2]))
* scale;
}
}
else if(bitDepth == 16)
{
scale = 1.0/(numChannels * 0x00FFFF);
if(numChannels == 1)
{
for(i=0; i< dpx.header.Width() * dpx.header.Height(); i++)
buf[i] = double(byteBuf[2*i] + 0x0100*byteBuf[2*i+1]) * scale;
}
else
{
int c;
double rgb[3];
for(i=0; i< dpx.header.Width() * dpx.header.Height(); i++)
{
for(c=0; c<3; c++)
rgb[c] = byteBuf[6*i+2*c] + 0x0100*byteBuf[6*i+2*c+1];
buf[i] = (rgb[0] + rgb[1] + rgb[2]) * scale;
}
}
}
else
{
fprintf(stderr, "ERROR: Unsupported bit depth: %d in '%s'\n",
bitDepth, dpxfn);
exit(1);
}
img.Close();
return buf;
}
unsigned char* ReadFrameDPX_ImageData(const char *dpxfn, unsigned char *buf,
int &bufSize, int &width,int &height,bool &endian,
GLenum &pix_fmt,int &num_components)
{
InStream img;
// keep this around, since all the frames will be the same size
static unsigned char *byteBuf;
if(!img.Open(dpxfn))
{
QString msg;
msg += "ReadFrameDPX_ImageData: Cannot open ";
msg += dpxfn;
msg += "\n";
if(errno) msg += strerror(errno);
throw AeoException(msg);
}
dpx::Reader dpx;
dpx.SetInStream(&img);
if(!dpx.ReadHeader())
{
img.Close();
QString msg;
msg += "ReadFrameDPX_ImageData: Cannot parse DPX header of ";
msg += dpxfn;
msg += "\n";
if(errno) msg += strerror(errno);
throw AeoException(msg);
}
dpx::DataSize size = dpx.header.ComponentDataSize(0);
int bitDepth = dpx.header.ComponentByteCount(0) * 8;
int numChannels = dpx.header.ImageElementComponentCount(0);
int pixel_size; //in bytes;
bool doRawRead(false);
int remainder(0);
width=dpx.header.Width();
height= dpx.header.Height();
if(dpx.header.BitDepth(0) == 10 && numChannels == 3)
{
pix_fmt = GL_UNSIGNED_INT_10_10_10_2;
bufSize = width * height * 4;
num_components=4;
pixel_size=4;
doRawRead = true;
}
else if (dpx.header.BitDepth(0) == 16 && numChannels ==3)
{
// XXX: Why 8 instead of pixel_size of 6?
bufSize = width * height * 8;
pix_fmt = GL_UNSIGNED_SHORT;
num_components=3;
pixel_size = 6;
doRawRead = true;
}
else if(dpx.header.BitDepth(0) == 16 && numChannels == 1) //16bit luma
{
bufSize = width * height * 2;
pix_fmt = GL_UNSIGNED_SHORT;
num_components=1;
pixel_size = 2;
doRawRead = true;
}
else
{
if(
dpx.header.ImageElementComponentCount(0) == 1 &&
dpx.header.BitDepth(0)==10 &&
dpx.header.ImagePacking(0) == 1 &&
dpx.header.Width() % 3 != 0 )
{
remainder = 3 - (width % 3);
}
bufSize = width * height * numChannels * 2;
num_components = numChannels;
pix_fmt = GL_UNSIGNED_SHORT;
pixel_size = 2;
doRawRead = false;
}
if(buf == NULL)
{
buf = new unsigned char [bufSize];
if(buf == NULL)
throw AeoException("Out of Memory: DPX image buf");
}
if(doRawRead)
{
endian=dpx.header.RequiresByteSwap();
dpx.fd->Seek( dpx.header.imageOffset,dpx.fd->kStart);
dpx.fd->Read(buf,dpx.header.Width() * dpx.header.Height() * pixel_size);
}
else
{
if(!remainder)
{
if(!dpx.ReadImage(buf, kWord, dpx.header.ImageDescriptor(0)))
throw("This DPX encoding is not supported (e.g., RLE)");
}
else
{
// remainder non-zero means that the DPX is grayscale with
// 10-bit samples packed three-to-a-word, but the image width
// is not divisible by 3
// opendpx cannot read this properly (it requires the scanlines
// to break on word boundaries), so we have to read it ourselves
// and unpack it manually.
int numWords = ceil(double(width*height)/3.0);
static uint32_t *wbuf(NULL);
if(wbuf == NULL)
{
// make a buffer to hold the 32-bit words
wbuf = new uint32_t[numWords];
if(wbuf == NULL)
throw AeoException("Out of Memory: DPX word buf");
}
// read in the whole array of 32-bit words from the DPX file
dpx.fd->Seek(dpx.header.imageOffset, dpx.fd->kStart);
dpx.fd->Read(wbuf, numWords*sizeof(uint32_t));
// swap the bytes in the words if necessary
if(dpx.header.RequiresByteSwap())
{
for(int w=0; w<numWords; ++w)
{
uint32_t *cp = wbuf+w;
*cp = (*cp >> 16) | (*cp << 16);
*cp = ((*cp & 0xFF00FF00) >> 8) | ((*cp & 0x00FF00FF) << 8);
}
}
// use a proxy to more easily enter the extracted sample values
// as 16-bit words in the buf array.
uint16_t *ubuf;
ubuf = reinterpret_cast<uint16_t *>(buf);
uint32_t *word; // pointer into wbuf
// double scale = 65535.0 / 1023.0; // scale 10-bit to 16-bit
int nSample = 0; // number of samples remaining in the current word
word = wbuf; // start at the beginning of the buffer
for(int i=0; i<width*height; ++i)
{
if(nSample==0)
{
word++;
nSample = 3;
*word >>=2; // remove the fill bits
}
// unpack the 10-bit word and rescale it to 16 bits
// ubuf[i] = uint16_t(((*word) & 0x03FF) * scale);
// approximate this scaling with bit operations for speed:
ubuf[i] = *word & 0x03FF;
ubuf[i] = (ubuf[i] << 6) | (ubuf[i] >> 4);
// shift the 32-bit word down and update the packed count
(*word) >>= 10;
nSample--;
}
}
// either way, we've already swapped the bytes if it was needed.
endian = false;
}
img.Close();
return buf;
}
#ifdef USEBOOST
/* ReadFrameDPX - read a single frame from a DPX file
* arguments:
* dpxfn: the filename of the dpx file
* buf: an existing target buffer of sufficient size, or NULL
*
* The DPX image is returned in buf (which is also returned by the function)
* in row-major order, top-to-bottom, left-to-right
*/
boost::numeric::ublas::matrix<double> ReadFrameDPX(const char *dpxfn)
{
InStream img;
boost::numeric::ublas::matrix<double> buf;
// keep this around, since all the frames will be the same size
static unsigned char *byteBuf;
if(!img.Open(dpxfn))
{
QString msg;
msg += "ReadFrameDPX: Cannot open ";
msg += dpxfn;
msg += "\n";
if(errno) msg += strerror(errno);
throw AeoException(msg);
}
dpx::Reader dpx;
dpx.SetInStream(&img);
if(!dpx.ReadHeader())
{
img.Close();
QString msg;
msg += "ReadFrameDPX: Cannot parse DPX header of ";
msg += dpxfn;
msg += "\n";
if(errno) msg += strerror(errno);
throw AeoException(msg);
}
dpx::DataSize size = dpx.header.ComponentDataSize(0);
int bitDepth = dpx.header.ComponentByteCount(0) * 8;
int numChannels = dpx.header.ImageElementComponentCount(0);
buf.resize(dpx.header.Height(), dpx.header.Width(), false);
if(byteBuf == NULL)
{
byteBuf = new unsigned char
[dpx.header.Width() * dpx.header.Height() *
numChannels * dpx.header.ComponentByteCount(0)];
if(byteBuf == NULL)
{
throw AeoException("Out of Memory: DPX byteBuf");
}
}
dpx.ReadImage(byteBuf);
// Convert from uint8 [0-255] or unit16 [0-65535] to double [0-1]
// Note that the color-to-grayscale isn't weighted to give green
// more influence: so the picture area may not "look nice" to human
// perception, but the soundtrack area isn't in color anyway, and the
// portions of the code that care about the picture area will work
// just as well with this alternate response scale (equal weight) as
// with the human perception scale, and the computation is faster.
long i,r,c;
double scale;
if(bitDepth == 8)
{
scale = 1.0/(numChannels * 0x00FF);
if(numChannels == 1)
{
i = 0;
for(r=0; r<dpx.header.Height(); r++)
for(c=0; c<dpx.header.Width(); c++)
{
buf(r,c) = double(byteBuf[i++]) * scale;
}
}
else
{
i = 0;
for(r=0; r<dpx.header.Height(); r++)
for(c=0; c<dpx.header.Width(); c++)
{
buf(r,c) =
(int(byteBuf[3*i]) +
int(byteBuf[3*i+1]) +
int(byteBuf[3*i+2]))
* scale;
i++;
}
}
}
else if(bitDepth == 16)
{
scale = 1.0/(numChannels * 0x00FFFF);
if(numChannels == 1)
{
i = 0;
for(r=0; r<dpx.header.Height(); r++)
for(c=0; c<dpx.header.Width(); c++)
{
buf(r,c) = double(byteBuf[2*i] + 0x0100*byteBuf[2*i+1]) * scale;
i++;
}
}
else
{
int ch;
double rgb[3];
i = 0;
for(r=0; r<dpx.header.Height(); r++)
for(c=0; c<dpx.header.Width(); c++)
{
for(ch=0; ch<3; ch++)
rgb[ch] = byteBuf[6*i+2*ch] + 0x0100*byteBuf[6*i+2*ch+1];
buf(r,c)= (rgb[0] + rgb[1] + rgb[2]) * scale;
i++;
}
}
}
else
{
fprintf(stderr, "ERROR: Unsupported bit depth: %d in '%s'\n",
bitDepth, dpxfn);
exit(1);
}
img.Close();
return buf;
}
#endif