Newer
Older
/*!
* \file map.c
* \brief Map Source File
* \authors Tin
* \version 1
* \date 16/02/2021
*
* Map function implementation.
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/*!
* \fn int initmap(MAP *map,char *path)
* \brief This function initialize the map : allocate the map's name and call the maptomatrix
*
* \param [in] map the map pointer
* \param [in] path the map image path
* \return 0 if the initialization went right.
*/
int lenpath = strlen(path); //get the path lenght
map->path = (char *)malloc((lenpath)*sizeof(char)); //allocate the path
strcpy(map->path,path); //copy the path given to map's path
map->functional = maptomatrix(map); //fill the map's functional matrix
/*!
* \fn int **maptomatrix(MAP *map)
* \brief This function gets the matrix's length and width and creates a functional matrix from the bmp matrix.
*
* \param [in] map the map pointer
* \return NULL if there is an error when opening the map, fonc otherwise.
*/
int **maptomatrix(MAP *map){
char identity[2] ;
unsigned char color[3];
uint32_t file_size;
char application_id[4] ;
uint32_t raster_address;
uint32_t size_DIBHeader;
uint32_t image_width;
uint32_t image_height;
uint16_t nbColorPlanes;
uint16_t nbBitPerPixel;
uint32_t typeCompression;
uint32_t size_raw_image;
int32_t hResolution;
int32_t vResolution;
uint32_t nbUsedColours;
uint32_t nbImportantColours;
int linematrix;
int colummatrix;
int rmalloc,cmalloc;
int **fonc ;
int i;
FILE *bin;
printf("Open : %s \n",map->path);
if(!(bin=fopen(map->path,"rb"))){
puts("Error to open the map !");
}
//Read Every Information of Map
fread(identity,sizeof(identity),1,bin);
fread(&file_size,sizeof(file_size),1,bin);
fread(application_id,sizeof(application_id),1,bin);
fread(&raster_address,sizeof(raster_address),1,bin);
fread(&size_DIBHeader,sizeof(size_DIBHeader),1,bin);
fread(&image_width,sizeof(image_width),1,bin);
fread(&image_height,sizeof(image_height),1,bin);
fread(&nbColorPlanes,sizeof(nbColorPlanes),1,bin);
fread(&nbBitPerPixel,sizeof(nbBitPerPixel),1,bin);
fread(&typeCompression,sizeof(typeCompression),1,bin);
fread(&size_raw_image,sizeof(size_raw_image),1,bin);
fread(&hResolution,sizeof(hResolution),1,bin);
fread(&vResolution,sizeof(vResolution),1,bin);
fread(&nbUsedColours,sizeof(nbUsedColours),1,bin);
fread(&nbImportantColours,sizeof(nbImportantColours),1,bin);
cmalloc = image_width;
rmalloc = image_height;
fonc = (int **)malloc(rmalloc * sizeof(int *));
for(i=0;i<rmalloc;i++)
{
fonc[i] = (int *)malloc(cmalloc*sizeof(int));
}
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//get map height and width
map->height = image_height;
map->width = image_width;
//fill the functional matrix
linematrix = image_height -1;
colummatrix = 0;
for(i = 0 ; i < image_height*image_width ; i++){
fread(color,sizeof(color),1,bin);
if(color[2] == 255 && color[1] == 0 && color[0] == 0)
{
fonc[linematrix][colummatrix] = 2;
}
else if (color[2] == 0 && color[1] == 255 && color[0] == 0)
{
fonc[linematrix][colummatrix] = 1;
}
else {
fonc[linematrix][colummatrix] = 0;
}
if(colummatrix<image_width-1) colummatrix ++;
else {
colummatrix = 0;
linematrix --;
}
}
return fonc;
}