texture.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 Google Inc. 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 #include "tango-gl/texture.h"
17 #include "tango-gl/util.h"
18 
19 namespace tango_gl {
20 
21 static const int kMaxExponentiation = 12;
22 
23 static int RoundUpPowerOfTwo(int w) {
24  int start = 2;
25  for (int i = 0; i <= kMaxExponentiation; ++i) {
26  if (w < start) {
27  w = start;
28  break;
29  } else {
30  start = start << 1;
31  }
32  }
33  return w;
34 }
35 
36 Texture::Texture(const char* file_path) {
37  if (!LoadFromPNG(file_path)) {
38  LOGE("Texture initialing error");
39  }
40 }
41 
42 bool Texture::LoadFromPNG(const char* file_path) {
43  FILE* file = fopen(file_path, "rb");
44 
45  if (file == NULL) {
46  LOGE("fp not loaded: %s", strerror(errno));
47  return false;
48  }
49 
50  fseek(file, 8, SEEK_CUR);
51 
52  png_structp png_ptr =
53  png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
54  png_infop info_ptr = png_create_info_struct(png_ptr);
55 
56  png_init_io(png_ptr, file);
57  png_set_sig_bytes(png_ptr, 8);
58  png_read_info(png_ptr, info_ptr);
59  png_get_IHDR(png_ptr, info_ptr, &width_, &height_, &bit_depth_, &color_type_,
60  NULL, NULL, NULL);
61 
64  int row = width_ * (color_type_ == PNG_COLOR_TYPE_RGBA ? 4 : 3);
65  byte_data_ = new char[row * height_];
66 
67  png_bytep* row_pointers = new png_bytep[height_];
68  for (uint i = 0; i < height_; ++i) {
69  row_pointers[i] = (png_bytep)(byte_data_ + i * row);
70  }
71  png_read_image(png_ptr, row_pointers);
72  png_destroy_read_struct(&png_ptr, &info_ptr, 0);
73 
74  glGenTextures(1, &texture_id_);
75  glBindTexture(GL_TEXTURE_2D, texture_id_);
76  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
77  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
78  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
79  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80  util::CheckGlError("glBindTexture");
81  if (color_type_ == PNG_COLOR_TYPE_RGBA) {
82  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA,
83  GL_UNSIGNED_BYTE, byte_data_);
84  } else {
85  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB,
86  GL_UNSIGNED_BYTE, byte_data_);
87  }
88  util::CheckGlError("glTexImage2D");
89  glBindTexture(GL_TEXTURE_2D, 0);
90 
91  fclose(file);
92  delete[] row_pointers;
93  delete[] byte_data_;
94 
95  return true;
96 }
97 
99 
101  if (byte_data_ != NULL) {
102  delete[] byte_data_;
103  }
104  byte_data_ = NULL;
105 }
106 
107 } // namespace tango_gl
#define NULL
GLM_FUNC_DECL genType::row_type row(genType const &m, length_t const &index)
#define LOGE(...)
unsigned int uint
Definition: posegraph3.hh:51
GLuint GetTextureID() const
Definition: texture.cpp:98
unsigned int GLuint
Definition: dummy.cpp:78
bool LoadFromPNG(const char *file_path)
Definition: texture.cpp:42
png_uint_32 height_
Definition: texture.h:37
GLuint texture_id_
Definition: texture.h:40
char * byte_data_
Definition: texture.h:39
Texture(const char *file_path)
Definition: texture.cpp:36
void CheckGlError(const char *operation)
Definition: util.cpp:43
static int RoundUpPowerOfTwo(int w)
Definition: texture.cpp:23
png_uint_32 width_
Definition: texture.h:37
static const int kMaxExponentiation
Definition: texture.cpp:21


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Dec 14 2020 03:37:06