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 "Texture.h"
22 
23 namespace MaliSDK
24 {
25  /* Please see header for the specification. */
26  void Texture::loadBmpImageData(const char* fileName,
27  int* imageWidthPtr,
28  int* imageHeightPtr,
29  unsigned char** textureDataPtrPtr)
30  {
31  ASSERT(fileName != NULL,
32  "Invalid file name.");
33  ASSERT(textureDataPtrPtr != NULL,
34  "Cannot use NULL pointer to store image data.");
35 
36  tagBITMAPFILEHEADER bitmapFileHeader;
37  tagBITMAPINFOHEADER bitmapInfoHeader;
38  FILE* file = NULL;
39  unsigned char* loadedTexture = NULL;
40 
41  /* Try to open file. */
42  file = fopen(fileName, "rb");
43 
44  ASSERT(file != NULL, "Failed to open file");
45 
46  /* Try to read the bitmap file header. */
47  readBitmapFileHeader(file, &bitmapFileHeader);
48 
49  /* Try to read the bitmap info header. */
50  readBitmapInforHeader(file, &bitmapInfoHeader);
51 
52  /* Try to allocate memory to store texture image data. */
53  loadedTexture = (unsigned char*) malloc(bitmapInfoHeader.biSizeImage);
54 
55  ASSERT(loadedTexture != NULL, "Could not allocate memory to store texture image data.");
56 
57  /* Move the file pointer to the begging of the bitmap image data. */
58  fseek(file, bitmapFileHeader.bfOffBits, 0);
59 
60  /* Read in the image data. */
61  fread(loadedTexture, bitmapInfoHeader.biSizeImage, 1, file);
62 
63  unsigned char tempElement;
64 
65  /* As data in bmp file is stored in BGR, we need to convert it into RGB. */
66  for (unsigned int imageIdx = 0;
67  imageIdx < bitmapInfoHeader.biSizeImage;
68  imageIdx += 3)
69  {
70  tempElement = loadedTexture[imageIdx];
71  loadedTexture[imageIdx] = loadedTexture[imageIdx + 2];
72  loadedTexture[imageIdx + 2] = tempElement;
73  }
74 
75  /* At the end, close the file. */
76  fclose(file);
77 
78  /* Return retrieved data. */
79  *textureDataPtrPtr = loadedTexture;
80 
81  /* Store the image dimensions if requested. */
82  if (imageHeightPtr != NULL)
83  {
84  *imageHeightPtr = bitmapInfoHeader.biHeight;
85  }
86 
87  if (imageWidthPtr != NULL)
88  {
89  *imageWidthPtr = bitmapInfoHeader.biWidth;
90  }
91  }
92 
93  /* Please see header for the specification. */
94  void Texture::readBitmapFileHeader(FILE* filePtr, tagBITMAPFILEHEADER* bitmapFileHeaderPtr)
95  {
96  ASSERT(filePtr != NULL &&
97  bitmapFileHeaderPtr != NULL,
98  "Invalid arguments used to read bitmap file header.");
99 
100  fread(&bitmapFileHeaderPtr->bfType, sizeof(bitmapFileHeaderPtr->bfType), 1, filePtr);
101  fread(&bitmapFileHeaderPtr->bfSize, sizeof(bitmapFileHeaderPtr->bfSize), 1, filePtr);
102  fread(&bitmapFileHeaderPtr->bfReserved1, sizeof(bitmapFileHeaderPtr->bfReserved1), 1, filePtr);
103  fread(&bitmapFileHeaderPtr->bfReserved2, sizeof(bitmapFileHeaderPtr->bfReserved2), 1, filePtr);
104  fread(&bitmapFileHeaderPtr->bfOffBits, sizeof(bitmapFileHeaderPtr->bfOffBits), 1, filePtr);
105 
106  /* Make sure that file type is valid. */
107  ASSERT(bitmapFileHeaderPtr->bfType == 0x4D42,
108  "Invalid file type read");
109  }
110 
111  /* Please see header for the specification. */
112  void Texture::readBitmapInforHeader(FILE* filePtr, tagBITMAPINFOHEADER* bitmapInfoHeaderPtr)
113  {
114  ASSERT(filePtr != NULL &&
115  bitmapInfoHeaderPtr != NULL,
116  "Invalid arguments used to read bitmap info header.");
117 
118  fread(&bitmapInfoHeaderPtr->biSize, sizeof(bitmapInfoHeaderPtr->biSize), 1, filePtr);
119  fread(&bitmapInfoHeaderPtr->biWidth, sizeof(bitmapInfoHeaderPtr->biWidth), 1, filePtr);
120  fread(&bitmapInfoHeaderPtr->biHeight, sizeof(bitmapInfoHeaderPtr->biHeight), 1, filePtr);
121  fread(&bitmapInfoHeaderPtr->biPlanes, sizeof(bitmapInfoHeaderPtr->biPlanes), 1, filePtr);
122  fread(&bitmapInfoHeaderPtr->biBitCount, sizeof(bitmapInfoHeaderPtr->biBitCount), 1, filePtr);
123  fread(&bitmapInfoHeaderPtr->biCompression, sizeof(bitmapInfoHeaderPtr->biCompression), 1, filePtr);
124  fread(&bitmapInfoHeaderPtr->biSizeImage, sizeof(bitmapInfoHeaderPtr->biSizeImage), 1, filePtr);
125  fread(&bitmapInfoHeaderPtr->biXPelsPerMeter, sizeof(bitmapInfoHeaderPtr->biXPelsPerMeter), 1, filePtr);
126  fread(&bitmapInfoHeaderPtr->biYPelsPerMeter, sizeof(bitmapInfoHeaderPtr->biYPelsPerMeter), 1, filePtr);
127  fread(&bitmapInfoHeaderPtr->biClrUsed, sizeof(bitmapInfoHeaderPtr->biClrUsed), 1, filePtr);
128  fread(&bitmapInfoHeaderPtr->biClrImportant, sizeof(bitmapInfoHeaderPtr->biClrImportant), 1, filePtr);
129  }
130 }
static void readBitmapFileHeader(FILE *filePtr, tagBITMAPFILEHEADER *bitmapFileHeaderPtr)
Read BMP file header.
Definition: Texture.cpp:94
static void readBitmapInforHeader(FILE *filePtr, tagBITMAPINFOHEADER *bitmapInfoHeaderPtr)
Read BMP info header.
Definition: Texture.cpp:112
#define ASSERT(x, s)
Definition: common.h:45
static void loadBmpImageData(const char *fileName, int *imageWidthPtr, int *imageHeightPtr, unsigned char **textureDataPtrPtr)
Load BMP texture data from a file into memory.
Definition: Texture.cpp:26