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 #ifdef __ANDROID__
38  if (!LoadFromPNG(file_path)) {
39  LOGE("Texture initialing error");
40  }
41 #endif
42 }
43 #ifdef __ANDROID__
44 bool Texture::LoadFromPNG(const char* file_path) {
45  FILE* file = fopen(file_path, "rb");
46 
47  if (file == NULL) {
48  LOGE("fp not loaded: %s", strerror(errno));
49  return false;
50  }
51 
52  fseek(file, 8, SEEK_CUR);
53 
54  png_structp png_ptr =
55  png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
56  png_infop info_ptr = png_create_info_struct(png_ptr);
57 
58  png_init_io(png_ptr, file);
59  png_set_sig_bytes(png_ptr, 8);
60  png_read_info(png_ptr, info_ptr);
61  png_get_IHDR(png_ptr, info_ptr, &width_, &height_, &bit_depth_, &color_type_,
62  NULL, NULL, NULL);
63 
64  width_ = RoundUpPowerOfTwo(width_);
65  height_ = RoundUpPowerOfTwo(height_);
66  int row = width_ * (color_type_ == PNG_COLOR_TYPE_RGBA ? 4 : 3);
67  byte_data_ = new char[row * height_];
68 
69  png_bytep* row_pointers = new png_bytep[height_];
70  for (uint i = 0; i < height_; ++i) {
71  row_pointers[i] = (png_bytep)(byte_data_ + i * row);
72  }
73  png_read_image(png_ptr, row_pointers);
74  png_destroy_read_struct(&png_ptr, &info_ptr, 0);
75 
76  glGenTextures(1, &texture_id_);
77  glBindTexture(GL_TEXTURE_2D, texture_id_);
78  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
79  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
80  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
81  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
82  util::CheckGlError("glBindTexture");
83  if (color_type_ == PNG_COLOR_TYPE_RGBA) {
84  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width_, height_, 0, GL_RGBA,
85  GL_UNSIGNED_BYTE, byte_data_);
86  } else {
87  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width_, height_, 0, GL_RGB,
88  GL_UNSIGNED_BYTE, byte_data_);
89  }
90  util::CheckGlError("glTexImage2D");
91  glBindTexture(GL_TEXTURE_2D, 0);
92 
93  fclose(file);
94  delete[] row_pointers;
95  delete[] byte_data_;
96 
97  return true;
98 }
99 #endif
101 
103  if (byte_data_ != NULL) {
104  delete[] byte_data_;
105  }
106  byte_data_ = NULL;
107 }
108 
109 } // 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
unsigned int GLuint
Definition: dummy.cpp:78
GLuint texture_id_
Definition: texture.h:47
char * byte_data_
Definition: texture.h:46
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
GLuint GetTextureID() const
Definition: texture.cpp:100
static const int kMaxExponentiation
Definition: texture.cpp:21


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:57