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 
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 
29 namespace MaliSDK
30 {
31  void Texture::getCompressedTextureFormats(GLint** textureFormats, int* numberOfTextureFormats)
32  {
33  GL_CHECK(glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, numberOfTextureFormats));
34 
35  *textureFormats = (GLint *)calloc(*numberOfTextureFormats, sizeof(GLint));
36  if(*textureFormats == NULL)
37  {
38  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
39  exit(1);
40  }
41 
42  GL_CHECK(glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, *textureFormats));
43  }
44 
45  bool Texture::isETCSupported(bool verbose)
46  {
47  return true;
48  }
49 
50  void Texture::createTexture(unsigned int width, unsigned int height, GLvoid **textureData)
51  {
52  unsigned char *randomTexture = new unsigned char [width * height * 4];
53  if(randomTexture == NULL)
54  {
55  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
56  exit(1);
57  }
58 
59  /* Initialize texture with random shades. */
60  for(unsigned int allTexels = 0; allTexels < width * height; allTexels ++)
61  {
62  /* Use 255 (fully opaque) for the alpha value */
63  randomTexture[allTexels * 4 + 3] = 255;
64  /* Set each colour component (Red, Green, Blue) of the texel to a different random number. */
65  for (int allChannels = 0; allChannels < 3; allChannels++)
66  {
67  /* Generate a random number between 0 and 255 */
68  int randomNumber = (int)(255 * (rand() / (float)RAND_MAX));
69  randomTexture[allTexels * 4 + allChannels] = randomNumber;
70  }
71  }
72 
73  *textureData = randomTexture;
74  }
75 
76  void Texture::createTexture(unsigned int width, unsigned int height, unsigned int red, GLvoid **textureData)
77  {
78  unsigned char* newTexture = new unsigned char [width * height];
79 
80  if(newTexture == NULL)
81  {
82  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
83  exit(1);
84  }
85 
86  for (unsigned int texelIndex = 0; texelIndex < width * height; ++texelIndex)
87  {
88  newTexture[texelIndex] = red;
89  }
90  *textureData = newTexture;
91  }
92 
93  void Texture::createTexture(unsigned int width, unsigned int height, short red, short **textureData)
94  {
95  *textureData = new short [width * height];
96 
97  if (*textureData == NULL)
98  {
99  LOGE("Out of memory at %s:%i\n", __FILE__, __LINE__);
100  exit(1);
101  }
102 
103  for (unsigned int texelIndex = 0; texelIndex < width * height; ++texelIndex)
104  {
105  (*textureData)[texelIndex] = red;
106  }
107  }
108 
109  void Texture::deleteTextureData(GLvoid** textureData)
110  {
111  delete[] (unsigned char*)*textureData;
112  }
113 
114  /* [Load data from file] */
115  void Texture::loadData(const char *filename, unsigned char **textureData)
116  {
117  FILE *file = fopen(filename, "rb");
118 
119  if(file == NULL)
120  {
121  LOGE("Failed to open '%s'\n", filename);
122  exit(1);
123  }
124 
125  fseek(file, 0, SEEK_END);
126 
127  unsigned int length = ftell(file);
128  unsigned char *loadedTexture = (unsigned char *)calloc(length, sizeof(unsigned char));
129 
130  ASSERT(loadedTexture != NULL, "Could not allocate memory to store PKM file data.")
131 
132  fseek(file, 0, SEEK_SET);
133 
134  size_t read = fread(loadedTexture, sizeof(unsigned char), length, file);
135 
136  ASSERT(read == length, "Failed to read PKM file.");
137 
138  fclose(file);
139 
140  *textureData = loadedTexture;
141  }
142  /* [Load data from file] */
143 
144  void Texture::reversePixelLine(float* destination, const float* source, int lineWidth)
145  {
146  const int rgbComponentsCount = 3;
147 
148  for (int pixelIndex = 0; pixelIndex < lineWidth; ++pixelIndex)
149  {
150  memcpy(destination + pixelIndex * rgbComponentsCount,
151  source + (lineWidth - pixelIndex - 1) * rgbComponentsCount,
152  rgbComponentsCount * sizeof(float));
153  }
154  }
155 }
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