34 int max_color_value = 255;
35 int num_of_bytes_per_pixel = 3;
36 int read_buffer_length = 256;
39 const char comment_id =
'#';
40 const char pixmap_mark[] =
"P6\n";
44 char* line_str = NULL;
45 int n_loaded_pixels = 0;
47 char* returned_str = NULL;
48 int returned_value = 0;
51 FILE* pFile = fopen(ppm_file_name,
"rb");
55 LOGF(
"Error opening .ppm file.");
63 returned_str = fgets(line_str, read_buffer_length, pFile);
65 if (returned_str == NULL)
67 LOGF(
"Error reading .ppm file.");
73 returned_value = strncmp(line_str, pixmap_mark,
sizeof(pixmap_mark));
75 if (returned_value != 0)
77 LOGF(
"File does not contain P6 string in the header.");
82 returned_str = fgets(line_str, read_buffer_length, pFile);
85 while (strncmp(line_str, &comment_id,
sizeof(comment_id)) == 0)
87 returned_str = fgets(line_str, read_buffer_length, pFile);
89 if (returned_str == NULL)
91 LOGF(
"Error reading .ppm file.");
98 returned_value = sscanf(line_str,
"%d %d", &width, &height);
101 if (returned_value != 2)
103 LOGF(
"Error reading image width/height from the .ppm file.");
109 returned_value = fscanf(pFile,
"%d", &id_255);
111 if (!(returned_value == 1 && id_255 == max_color_value))
113 LOGF(
"Error reading 255 mark in the .ppm file.");
118 fseek(pFile, 1, SEEK_CUR);
121 pixels = (
char*) calloc(width * height, num_of_bytes_per_pixel);
125 LOGF(
"Error allocating memory for pixels buffer.");
131 n_loaded_pixels = fread(pixels, num_of_bytes_per_pixel, width * height, pFile);
133 if (n_loaded_pixels != width * height)
135 LOGF(
"Error reading .ppm file.");
#define MALLOC_CHECK(ptr_type, ptr, size)
ImageFile load_ppm_file(const char *ppm_file_name)
GLint GLenum GLsizei GLsizei GLsizei GLint GLenum GLenum const void * pixels
GLint GLsizei GLsizei height
Struct representing texture image.