Apollo 11.0
自动驾驶开放平台
texture.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16
18
19#include <iostream>
20
22 : is_size_changed_(false),
23 is_dirty_(false),
24 texture_format_(0),
25 image_width_(0),
26 image_height_(0),
27 data_size_(0),
28 data_(nullptr) {}
29
30bool Texture::UpdateData(const QImage& img) {
31 if (data_size_ < img.byteCount()) {
32 if (!data_) {
33 delete[] data_;
34 }
35
36 data_ = new GLubyte[img.byteCount()];
37 if (data_ == nullptr) {
38 data_size_ = 0;
39 return false;
40 }
41 data_size_ = img.byteCount();
42 is_size_changed_ = true;
43 }
44
45 image_height_ = img.height();
46 image_width_ = img.width();
47
48 memcpy(data_, img.bits(), img.byteCount());
49 is_dirty_ = true;
50
51 texture_format_ = GL_RGBA;
52 return true;
53}
54
56 const std::shared_ptr<const apollo::drivers::Image>& imgData) {
57 std::size_t imgSize = imgData->width() * imgData->height() * 3;
58
59 if (static_cast<std::size_t>(data_size_) < imgSize) {
60 if (!data_) {
61 delete[] data_;
62 }
63
64 data_ = new GLubyte[imgSize];
65 if (data_ == nullptr) {
66 data_size_ = 0;
67 return false;
68 }
69 data_size_ = static_cast<GLsizei>(imgSize);
70 is_size_changed_ = true;
71 }
72
73 texture_format_ = GL_RGB;
74 if (imgData->encoding() == std::string("yuyv")) {
75 const GLubyte* src =
76 reinterpret_cast<const GLubyte*>(imgData->data().c_str());
77
78 GLubyte* dst = data_;
79 for (std::size_t i = 0; i < imgData->data().size(); i += 4) {
80 int y = 298 * (src[i] - 16);
81 int u = src[i + 1] - 128;
82 int u1 = 516 * u;
83 int v = src[i + 3] - 128;
84 int v1 = 208 * v;
85
86 u *= 100;
87 v *= 409;
88
89#define CLAMP(v) static_cast<GLubyte>((v) > 255 ? 255 : ((v) < 0 ? 0 : v))
90 *dst++ = CLAMP((y + v + 128) >> 8);
91 *dst++ = CLAMP((y - u - v1 + 128) >> 8);
92 *dst++ = CLAMP((y + u1 + 128) >> 8);
93
94 y = 298 * (src[i + 2] - 16);
95
96 *dst++ = CLAMP((y + v + 128) >> 8);
97 *dst++ = CLAMP((y - u - v1 + 128) >> 8);
98 *dst++ = CLAMP((y + u1 + 128) >> 8);
99#undef CLAMP
100 }
101 } else if (imgData->encoding() == std::string("rgb8")) {
102 memcpy(data_, imgData->data().c_str(), imgSize);
103 } else if (imgData->encoding() == std::string("bgr8")) {
104 memcpy(data_, imgData->data().c_str(), imgSize);
105 texture_format_ = GL_BGR;
106 } else {
107 memset(data_, 0, imgSize);
108 std::cerr << "Cannot support this format (" << imgData->encoding()
109 << ") image" << std::endl;
110 }
111 is_dirty_ = true;
112
113 image_height_ = imgData->height();
114 image_width_ = imgData->width();
115
116 return true;
117}
bool UpdateData(const QImage &img)
Definition texture.cc:30
Texture(void)
Definition texture.cc:21
bool is_size_changed_
Definition texture.h:53
bool is_dirty_
Definition texture.h:54
#define CLAMP(v)