-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathImageYuvRender.cpp
More file actions
110 lines (87 loc) · 2.65 KB
/
Copy pathImageYuvRender.cpp
File metadata and controls
110 lines (87 loc) · 2.65 KB
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
#include "ImageYuvRender.h"
#include "libyuv.h"
#include <QPainter>
#include <QDebug>
void ImageYuvRender::paintEvent(QPaintEvent* event) {
if (yuvData == NULL || rgbaData == NULL)
{
return;
}
QPainter painter(this);
QImage rgbImage(rgbaData,width_, height_, QImage::Format_RGBA8888);
int window_with = size().width();
int window_height = size().height();
float window_rate = (float)window_with / (float)window_height;
float imag_rate = (float)width_ / (float)height_;
int target_width;
int target_height;
int x;
int y;
if (window_rate > imag_rate)
{
target_height = window_height;
target_width = target_height * imag_rate;
x = (window_with - target_width)/2;
y = 0;
}
else
{
target_width = window_with;
target_height = target_width * height_ / width_;
y = (window_height - target_height) / 2;
x = 0;
}
QSize tszie(target_width,target_height);
//qDebug() << "size() " << x << " , " << y <<" w " << target_width << " h " << target_height;
//qDebug() << "size() " << x << " , " << y << " w " << size().width() << " h " << size().height();
// 在窗口中绘制RGB图像
painter.drawImage(x, y, rgbImage.scaled(tszie, Qt::KeepAspectRatioByExpanding));
}
void ImageYuvRender::initData(int w, int h,int stride_width)
{
width_ = w;
height_ = h;
stride_width_ = stride_width;
allocData();
qDebug() << "initData ok";
}
void ImageYuvRender::updateYuv(uint8_t* y, uint8_t* u, uint8_t* v)
{
memcpy(yuvData, y, stride_width_ * height_);
memcpy(yuvData + stride_width_ * height_, v, stride_width_ * height_ / 4);
memcpy(yuvData + stride_width_ * height_ * 5 / 4, u, stride_width_ * height_ / 4);
int size = stride_width_ * height_;
libyuv::I420ToARGB(yuvData, stride_width_, yuvData + size, stride_width_ / 2,
yuvData + size * 5 / 4, stride_width_ / 2,
rgbaData, width_ * 4,
width_, height_);
this->update();
}
void ImageYuvRender::Close()
{
if (yuvData)
{
delete[] yuvData;
yuvData = nullptr;
}
if (rgbaData)
{
delete[] rgbaData;
rgbaData = nullptr;
}
this->update();
}
void ImageYuvRender::allocData()
{
const int rgbaSize = width_ * height_ * 4;
if (yuvData == NULL)
{
const int size = stride_width_ * height_ * 3 / 2; // YUV420p //yuv考虑跨距
yuvData = new uint8_t[size];
memset(yuvData, 5, size);
const int rgbaSize = width_ * height_ * 4;
rgbaData = new uint8_t[rgbaSize];
memset(rgbaData, 5, rgbaSize);
}
qDebug() << "initData ok";
}