texture.cc 3.7 KB
Newer Older
1
/******************************************************************************
G
gruminions 已提交
2
 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

17
#include "modules/tools/visualizer/texture.h"
18 19 20 21 22 23 24 25 26 27 28
#include <iostream>

Texture::Texture()
    : is_size_changed_(false),
      is_dirty_(false),
      texture_format_(0),
      image_width_(0),
      image_height_(0),
      data_size_(0),
      data_(nullptr) {}

29 30 31
bool Texture::UpdateData(const QImage& img) {
  std::cout << "Image: ImageSize = " << img.byteCount()
            << ", data_size_ = " << data_size_;
32 33
  if (data_size_ < img.byteCount()) {
    if (!data_) {
34
      delete[] data_;
35 36 37 38 39 40 41 42 43 44 45
    }

    data_ = new GLubyte[img.byteCount()];
    if (data_ == nullptr) {
      data_size_ = 0;
      return false;
    }
    data_size_ = img.byteCount();
    is_size_changed_ = true;
  }

46 47 48
  image_height_ = img.height();
  image_width_ = img.width();

49 50 51
  memcpy(data_, img.bits(), img.byteCount());
  is_dirty_ = true;

52 53 54 55 56
  std::cout << ", imgWidth = " << image_width_
            << ", imgHeight = " << image_height_
            << ", w * h * sizeof(GL_RGB) = "
            << image_width_ * image_height_ * sizeof(GL_RGB)
            << ",format = " << img.pixelFormat().colorModel() << std::endl;
57 58 59 60

  texture_format_ = GL_RGBA;
  return true;
}
61 62 63 64 65

bool Texture::UpdateData(
    const std::shared_ptr<const apollo::drivers::Image>& imgData) {
    std::size_t imgSize = imgData->width() * imgData->height() * 3;

66 67 68 69 70 71 72 73 74 75
    if (static_cast<std::size_t>(data_size_) < imgSize) {
      if (!data_) {
        delete[] data_;
      }

      data_ = new GLubyte[imgSize];
      if (data_ == nullptr) {
        data_size_ = 0;
        return false;
      }
76
      data_size_ = static_cast<GLsizei>(imgSize);
77 78 79
      is_size_changed_ = true;
    }

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
    if(imgData->encoding() == std::string("yuyv")){
      const GLubyte* src = reinterpret_cast<const GLubyte*>(imgData->data().c_str());

      auto clamp = [](int v) -> GLubyte {
        int ret = v;
        if(v < 0) { ret = 0; }
        if(v > 255) { ret = 255; }

        return static_cast<GLubyte>(ret);
      };

      GLubyte* dst = data_;
      for(std::size_t i = 0; i < imgData->data().size(); i += 4){
          int y = 298 * (src[i] - 16);
          int u = src[i + 1] - 128;
          int u1 = 516 * u;
          int v = src[i + 3] - 128;
          int v1 = 208 * v;

          u *= 100;
          v *= 409;

          *dst++ = clamp((y + v + 128) >> 8);
          *dst++ = clamp((y - u - v1 + 128) >> 8);
          *dst++ = clamp((y + u1 + 128) >> 8);

          y = 298 * (src[i + 2] - 16);

          *dst++ = clamp((y + v + 128) >> 8);
          *dst++ = clamp((y - u - v1 + 128) >> 8);
          *dst++ = clamp((y + u1 + 128) >> 8);         
      }

    } else if (imgData->encoding() == std::string("rgb8")){
      memcpy(data_, imgData->data().c_str(), imgSize);
    } else {
      memset(data_, 0, imgSize);
      std::cerr << "Cannot support this format (" 
        << imgData->encoding() << ") image" << std::endl;
    }
120 121 122
    is_dirty_ = true;

    image_height_ = imgData->height();
123
    image_width_ = imgData->width();
124 125 126 127

    texture_format_ = GL_RGB;
    return true;
}