OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Image.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 <cstdio>
22 #include <cstdlib>
23 #include <cstring>
24 
25 #include "Skybox.h"
26 #include "Image.h"
27 
28 /* Please see the header for specification. */
29 ImageFile load_ppm_file(const char* ppm_file_name)
30 {
31  ImageFile image = { 0, 0, NULL };
32 
33  /* Constant numbers. */
34  int max_color_value = 255;
35  int num_of_bytes_per_pixel = 3;
36  int read_buffer_length = 256;
37 
38  /* Constant strings. */
39  const char comment_id = '#';
40  const char pixmap_mark[] = "P6\n";
41 
42  int id_255 = 0;
43  int height = 0;
44  char* line_str = NULL;
45  int n_loaded_pixels = 0;
46  char* pixels = NULL;
47  char* returned_str = NULL;
48  int returned_value = 0;
49  int width = 0;
50 
51  FILE* pFile = fopen(ppm_file_name, "rb");
52 
53  if (pFile == NULL)
54  {
55  LOGF("Error opening .ppm file.");
56 
57  exit(EXIT_FAILURE);
58  }
59 
60  MALLOC_CHECK(char*, line_str, read_buffer_length);
61 
62  /* Read the first line. */
63  returned_str = fgets(line_str, read_buffer_length, pFile);
64 
65  if (returned_str == NULL)
66  {
67  LOGF("Error reading .ppm file.");
68 
69  exit(EXIT_FAILURE);
70  }
71 
72  /* Verify whether the file begins with a "magic number" identifying the .ppm file type. */
73  returned_value = strncmp(line_str, pixmap_mark, sizeof(pixmap_mark));
74 
75  if (returned_value != 0)
76  {
77  LOGF("File does not contain P6 string in the header.");
78 
79  exit(EXIT_FAILURE);
80  }
81 
82  returned_str = fgets(line_str, read_buffer_length, pFile);
83 
84  /* Ignore any comments after P6 identifier, beginning with '#' */
85  while (strncmp(line_str, &comment_id, sizeof(comment_id)) == 0)
86  {
87  returned_str = fgets(line_str, read_buffer_length, pFile);
88 
89  if (returned_str == NULL)
90  {
91  LOGF("Error reading .ppm file.");
92 
93  exit(EXIT_FAILURE);
94  }
95  }
96 
97  /* Read the pixmap dimensions. */
98  returned_value = sscanf(line_str, "%d %d", &width, &height);
99 
100  /* Make sure both width and height have been read correctly. */
101  if (returned_value != 2)
102  {
103  LOGF("Error reading image width/height from the .ppm file.");
104 
105  exit(EXIT_FAILURE);
106  }
107 
108  /* Check if the maximum color value is 255. */
109  returned_value = fscanf(pFile, "%d", &id_255);
110 
111  if (!(returned_value == 1 && id_255 == max_color_value))
112  {
113  LOGF("Error reading 255 mark in the .ppm file.");
114 
115  exit(EXIT_FAILURE);
116  }
117 
118  fseek(pFile, 1, SEEK_CUR);
119 
120  /* Each pixel consists of 3 bytes for GL_RGB storage. */
121  pixels = (char*) calloc(width * height, num_of_bytes_per_pixel);
122 
123  if (pixels == NULL)
124  {
125  LOGF("Error allocating memory for pixels buffer.");
126 
127  exit(EXIT_FAILURE);
128  }
129 
130  /* Load image into the pixel buffer. */
131  n_loaded_pixels = fread(pixels, num_of_bytes_per_pixel, width * height, pFile);
132 
133  if (n_loaded_pixels != width * height)
134  {
135  LOGF("Error reading .ppm file.");
136 
137  exit(EXIT_FAILURE);
138  }
139 
140  /* Finally, put all needed info into the Image struct. */
141  image.width = width;
142  image.height = height;
143  image.pixels = pixels;
144 
145  FREE_CHECK(line_str);
146 
147  return image;
148 }
#define MALLOC_CHECK(ptr_type, ptr, size)
Definition: AstcTextures.h:33
ImageFile load_ppm_file(const char *ppm_file_name)
Definition: Image.cpp:29
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum const void * pixels
Definition: gl2ext.h:572
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
#define LOGF(...)
Definition: AstcTextures.h:31
int width
Definition: Image.h:30
Struct representing texture image.
Definition: Image.h:27
char * pixels
Definition: Image.h:34
GLeglImageOES image
Definition: gl2ext.h:231
int height
Definition: Image.h:32
GLint GLsizei width
Definition: gl2ext.h:179
#define FREE_CHECK(ptr)
Definition: AstcTextures.h:53