OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Texture.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2014-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include <GLES3/gl3.h>
22 #include "Common.h"
23 #include "Texture.h"
24 #include "ETCHeader.h"
25 
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 
30 namespace MaliSDK
31 {
32  void Texture::getCompressedTextureFormats(GLint** textureFormats, int* numberOfTextureFormats)
33  {
34  GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, numberOfTextureFormats));
35 
36  *textureFormats = (GLint *)calloc(*numberOfTextureFormats, sizeof(GLint));
37  if(*textureFormats == NULL)
38  {
39  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
40  exit(1);
41  }
42 
43  GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, *textureFormats));
44  }
45 
46  bool Texture::isETCSupported(bool verbose)
47  {
48  return true;
49  }
50 
51  void Texture::createTexture(unsigned int width, unsigned int height, GLvoid **textureData)
52  {
53  unsigned char *randomTexture = new unsigned char [width * height * 4];
54  if(randomTexture == NULL)
55  {
56  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
57  exit(1);
58  }
59 
60  /* Initialize texture with random shades. */
61  for(unsigned int allTexels = 0; allTexels < width * height; allTexels ++)
62  {
63  /* Use 255 (fully opaque) for the alpha value */
64  randomTexture[allTexels * 4 + 3] = 255;
65  /* Set each colour component (Red, Green, Blue) of the texel to a different random number. */
66  for (int allChannels = 0; allChannels < 3; allChannels++)
67  {
68  /* Generate a random number between 0 and 255 */
69  int randomNumber = (int)(255 * (rand() / (float)RAND_MAX));
70  randomTexture[allTexels * 4 + allChannels] = randomNumber;
71  }
72  }
73 
74  *textureData = randomTexture;
75  }
76 
77  void Texture::createTexture(unsigned int width, unsigned int height, unsigned int red, GLvoid **textureData)
78  {
79  unsigned char* newTexture = new unsigned char [width * height];
80 
81  if(newTexture == NULL)
82  {
83  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
84  exit(1);
85  }
86 
87  for (unsigned int texelIndex = 0; texelIndex < width * height; ++texelIndex)
88  {
89  newTexture[texelIndex] = red;
90  }
91  *textureData = newTexture;
92  }
93 
94  void Texture::createTexture(unsigned int width, unsigned int height, short red, short **textureData)
95  {
96  *textureData = new short [width * height];
97 
98  if (*textureData == NULL)
99  {
100  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
101  exit(1);
102  }
103 
104  for (unsigned int texelIndex = 0; texelIndex < width * height; ++texelIndex)
105  {
106  (*textureData)[texelIndex] = red;
107  }
108  }
109 
110  void Texture::deleteTextureData(GLvoid** textureData)
111  {
112  delete[] (unsigned char*)*textureData;
113  }
114 
115  /* [Load data from file] */
116  void Texture::loadData(const char *filename, unsigned char **textureData)
117  {
118  FILE *file = fopen(filename, "rb");
119 
120  if(file == NULL)
121  {
122  LOGE("Failed to open '%s'\n", filename);
123  exit(1);
124  }
125 
126  fseek(file, 0, SEEK_END);
127 
128  unsigned int length = ftell(file);
129  unsigned char *loadedTexture = (unsigned char *)calloc(length, sizeof(unsigned char));
130 
131  ASSERT(loadedTexture != NULL, "Could not allocate memory to store PKM file data.")
132 
133  fseek(file, 0, SEEK_SET);
134 
135  size_t read = fread(loadedTexture, sizeof(unsigned char), length, file);
136 
137  ASSERT(read == length, "Failed to read PKM file.");
138 
139  fclose(file);
140 
141  *textureData = loadedTexture;
142  }
143  /* [Load data from file] */
144 
145  /* [Load PKM data] */
146  void Texture::loadPKMData(const char *filename, ETCHeader* etcHeader, unsigned char **textureData)
147  {
148  /* PKM file consists of a header with information about image (stored in 16 first bits) and image data. */
149  const int sizeOfETCHeader = 16;
150  unsigned char* tempTextureData = NULL;
151 
152  loadData(filename, &tempTextureData);
153 
154  ASSERT(textureData != NULL, "textureData is a NULL pointer.");
155  ASSERT(etcHeader != NULL, "etcHeader is a NULL pointer.");
156  ASSERT(tempTextureData != NULL, "Could not load data from PKM file.");
157 
158  ETCHeader tempEtcHeader(tempTextureData);
159 
160  *etcHeader = tempEtcHeader;
161  *textureData = tempTextureData + sizeOfETCHeader;
162  }
163  /* [Load PKM data] */
164 
165  void Texture::reversePixelLine(float* destination, const float* source, int lineWidth)
166  {
167  const int rgbComponentsCount = 3;
168 
169  for (int pixelIndex = 0; pixelIndex < lineWidth; ++pixelIndex)
170  {
171  memcpy(destination + pixelIndex * rgbComponentsCount,
172  source + (lineWidth - pixelIndex - 1) * rgbComponentsCount,
173  rgbComponentsCount * sizeof(float));
174  }
175  }
176 }
static void loadPKMData(const char *filename, ETCHeader *etcHeader, unsigned char **textureData)
Load header and texture data from a pkm file into memory.
Definition: Texture.cpp:199
const int rgbComponentsCount
Definition: HDRImage.cpp:40
static void createTexture(unsigned int width, unsigned int height, GLvoid **textureData)
Create a texture using random data.
Definition: Texture.cpp:103
const GLenum textureFormats[]
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
static void getCompressedTextureFormats(GLint **textureFormats, int *numberOfTextureFormats)
Uses glGetIntegerv to get the number of compressed texture formats and the formats themselves...
Definition: Texture.cpp:38
precision highp int
Definition: hiz_cull.cs:38
static void deleteTextureData(GLvoid **textureData)
Deletes previously created texture.
Definition: Texture.cpp:162
GLsizei GLsizei GLchar * source
Definition: gl2ext.h:877
static void reversePixelLine(float *destination, const float *source, int lineWidth)
Copies float pixel data of one line of the image from source to destination in the reverse direction...
Definition: Texture.cpp:318
#define GL_CHECK(x)
Definition: AstcTextures.h:59
unsigned char * textureData
Definition: ThreadSync.cpp:109
#define LOGE(...)
Definition: Texture.cpp:32
static void loadData(const char *filename, unsigned char **textureData)
Load texture data from a file into memory.
Definition: Texture.cpp:167
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
#define ASSERT(x, s)
Definition: common.h:45
GLint GLsizei width
Definition: gl2ext.h:179
static bool isETCSupported(bool verbose=false)
Reports whether or not ETC (Ericsson Texture Compression) is supported.
Definition: Texture.cpp:46