-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrid.c
More file actions
55 lines (52 loc) · 1.5 KB
/
grid.c
File metadata and controls
55 lines (52 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* CONSTANTINESCU Vlad 314CB */
#include "quadtree.h"
/* initializes the grid / pixel array to size * size */
TPixel **InitGrid(unsigned int size)
{
TPixel **grid = (TPixel **) malloc(size * sizeof(TPixel *));
if (!grid) {
printf("Error at malloc of grid\n");
return NULL;
}
for (int i = 0; i < size; i++) {
grid[i] = (TPixel *) malloc(size * sizeof(TPixel));
if (!grid[i]) {
printf("Error at malloc of grid[%d]\n", i);
return NULL;
}
}
return grid;
}
/* reads the size and grid from the ppm input file */
TPixel **readPPM(TPixel **grid, unsigned int *size, FILE* input)
{
/* uses dump_char to read the characters
* between the values from the header */
char *dump_char = (char *) malloc(4 * sizeof(char));
unsigned int max_pixel_value = 0;
if (!dump_char) {
printf("Error at malloc of dump\n");
return NULL;
}
fread(dump_char, sizeof(char), 3, input);
fscanf(input, "%d", size);
fscanf(input, "%d", size);
fscanf(input, "%d", &max_pixel_value);
fread(dump_char, sizeof(char), 1, input);
free(dump_char);
grid = InitGrid((*size));
for (int i = 0; i < (*size); i++)
for (int j = 0; j < (*size); j++) {
fread(&grid[i][j].R, sizeof(char), 1, input);
fread(&grid[i][j].G, sizeof(char), 1, input);
fread(&grid[i][j].B, sizeof(char), 1, input);
}
return grid;
}
/* frees the memory allocated for the grid/ pixel array */
void FreeGrid(TPixel **grid, unsigned int size)
{
for (int i = 0; i < size; i++)
free(grid[i]);
free(grid);
}