-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYuvReader.cpp
97 lines (78 loc) · 2.09 KB
/
YuvReader.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
#include "YuvReader.h"
#include "opencv2\opencv.hpp"
YuvReader::YuvReader(const char * filePath, const int width, const int height, bool debug) {
this->debug = debug;
if (width % 2 == 1 || height % 2 == 1) {
log("Width or Height is invalid");
}
this->width = width;
this->height = height;
open(filePath);
}
void YuvReader::open(const char * filePath) {
this->file = fopen(filePath, "rb");
if (file == NULL) {
printf("Cant open file");
exit(EXIT_FAILURE);
}
}
bool YuvReader::read(cv::OutputArray image, bool convertBGR) {
initMatrix();
size_t nPixels = width * height;
size_t byteRead = fread(this->Y.data, sizeof(uint8_t), nPixels, file);
if (byteRead == 0) {
//EOF
return false;
}else if (byteRead != nPixels) {
//IO error
return false;
}
byteRead = fread(this->Cb.data, sizeof(uint8_t), nPixels / 4, file);
if (byteRead != nPixels / 4) {
//IO error
return false;
}
byteRead = fread(this->Cr.data, sizeof(uint8_t), nPixels / 4, file);
if (byteRead != nPixels / 4) {
//IO error
return false;
}
resize(this->Cb, this->Cb, Size(), 2.0, 2.0, InterpolationFlags::INTER_CUBIC);
resize(this->Cr, this->Cr, Size(), 2.0, 2.0, InterpolationFlags::INTER_CUBIC);
//if (Cb.rows >= 480) {
// log("a");
//}
std::vector<Mat> array;
array.push_back(this->Y);
array.push_back(this->Cb);
array.push_back(this->Cr);
image.create(Size(width, height), CV_8UC3);
merge(array, image);
if (convertBGR) {
cvtColor(image, image, ColorConversionCodes::COLOR_YCrCb2BGR);
}
return true;
}
void YuvReader::release() {
if (this->file != NULL) {
fclose(file);
file = NULL;
}
this->Y.release();
this->Cb.release();
this->Cr.release();
}
YuvReader::~YuvReader() {
log("DeAllocate YuvReader Object");
this->release();
}
void YuvReader::log(char * str) {
if (this->debug) {
printf(str);
}
}
void YuvReader::initMatrix() {
this->Y.create(Size(width, height), CV_8UC1);
this->Cb.create(Size(width / 2, height / 2), CV_8UC1);
this->Cr.create(Size(width / 2, height / 2), CV_8UC1);
}