coding style

This commit is contained in:
Phuntsok Drak-pa 2018-11-03 16:35:06 +01:00
parent a8441053ad
commit 477f702870
5 changed files with 159 additions and 153 deletions

View File

@ -7,9 +7,6 @@
#include <stdio.h>
#include <string.h>
/// Constante pour les erreurs liées à \ref darray_t
#define PTR_ERROR 2
/**
* `darrayNew` permet de créer un nouvel objet de type \ref darray_t ne
* contenant aucun élément. Le seul paramètre, `element_size`, est utilisé afin
@ -19,12 +16,12 @@
* \param element_size Taille des objets stockés
* \return Pointeur sur le nouvel objet \ref darray_t
*/
darray_t *darrayNew(size_t element_size) {
darray_t *ret;
ret = (darray_t *)malloc(sizeof(darray_t));
darray_t darrayNew(size_t t_element_size) {
darray_t ret;
ret = (darray_t)malloc(sizeof(darray_t));
ret->begin = NULL;
ret->end = ret->begin;
ret->element_size = element_size;
ret->element_size = t_element_size;
ret->capacity = 0;
return ret;
}
@ -39,17 +36,18 @@ darray_t *darrayNew(size_t element_size) {
* \param pos Position à laquelle on souhaite insérer un nouvel élément
* \param elem Élément que lon souhaite insérer
*/
void darrayInsert(darray_t *self, void *pos, void *elem) {
void darrayInsert(darray_t t_self, void *t_pos, void *t_elem) {
char *itr;
long pos_aux;
pos_aux = (char *)pos - (char *)self->begin;
if (darraySize(self) >= self->capacity) {
darrayExtend(self);
pos_aux = (char *)t_pos - (char *)t_self->begin;
if (darraySize(t_self) >= t_self->capacity) {
darrayExtend(t_self);
}
itr = (char *)self->begin + pos_aux;
memmove(itr + self->element_size, itr, (unsigned long)((char *)self->end - itr));
memcpy(itr, elem, self->element_size);
self->end = (char *)self->end + self->element_size;
itr = (char *)t_self->begin + pos_aux;
memmove(itr + t_self->element_size, itr,
(unsigned long)((char *)t_self->end - itr));
memcpy(itr, t_elem, t_self->element_size);
(*t_self).end = (char *)t_self->end + t_self->element_size;
}
/**
@ -59,20 +57,20 @@ void darrayInsert(darray_t *self, void *pos, void *elem) {
*
* \param self L'objet darray_t à étendre
*/
void darrayExtend(darray_t *self) {
void darrayExtend(darray_t t_self) {
void *new_array;
size_t size;
size = darraySize(self);
size = darraySize(t_self);
new_array =
realloc(self->begin, (darraySize(self) + 1) * self->element_size);
realloc(t_self->begin, (darraySize(t_self) + 1) * t_self->element_size);
if (!new_array) {
fprintf(stderr, "Failed memory reallocation at %s:%d\nAborting...",
__FILE__, __LINE__ - 2);
exit(PTR_ERROR);
}
self->begin = new_array;
++self->capacity;
self->end = (char *)self->begin + size * self->element_size;
(*t_self).begin = new_array;
++(*t_self).capacity;
(*t_self).end = (char *)t_self->begin + size * t_self->element_size;
}
/**
@ -84,10 +82,11 @@ void darrayExtend(darray_t *self) {
* \param self Objet \ref `darray_t` dont on souhaite supprimer un élément
* \param pos Élément de `self` que lon souhaite supprimer
*/
void darrayErase(darray_t *self, void *pos) {
memmove(pos, (char *)pos + self->element_size,
(unsigned long)(((char *)self->end - self->element_size) - (char *)pos));
self->end = (char *)self->end - self->element_size;
void darrayErase(darray_t t_self, void *t_pos) {
memmove(t_pos, (char *)t_pos + t_self->element_size,
(unsigned long)(((char *)t_self->end - t_self->element_size) -
(char *)t_pos));
(*t_self).end = (char *)t_self->end - t_self->element_size;
}
/**
@ -96,8 +95,8 @@ void darrayErase(darray_t *self, void *pos) {
* \param self Objet \ref darray_t à la fin duquel on souhaite ajouter un nouvel élément
* \param elem Élément que lon souhaite ajouter à la fin de `self`
*/
void darrayPushBack(darray_t *self, void *elem) {
darrayInsert(self, self->end, elem);
void darrayPushBack(darray_t t_self, void *t_elem) {
darrayInsert(t_self, t_self->end, t_elem);
}
/**
@ -106,8 +105,8 @@ void darrayPushBack(darray_t *self, void *elem) {
* dernier objet.
* \param self Objet dont on souhaite supprimer le dernier élément
*/
void darrayPopBack(darray_t *self) {
darrayErase(self, (char *)self->end - self->element_size);
void darrayPopBack(darray_t t_self) {
darrayErase(t_self, (char *)t_self->end - t_self->element_size);
}
/**
@ -117,9 +116,9 @@ void darrayPopBack(darray_t *self) {
* fonction.
* \param self Objet \ref darray_t à supprimer
*/
void darrayDelete(darray_t *self) {
free(self->begin);
free(self);
void darrayDelete(darray_t t_self) {
free(t_self->begin);
free(t_self);
}
/**
@ -129,8 +128,9 @@ void darrayDelete(darray_t *self) {
* \param self Objet \ref darray_t dont on souhaite connaître le nombre déléments
* \return Nombre déléments contenus dans `self`
*/
size_t darraySize(darray_t *self) {
return (size_t)((char *)self->end - (char *)self->begin) / self->element_size;
size_t darraySize(darray_t t_self) {
return (size_t)((char *)t_self->end - (char *)t_self->begin) /
t_self->element_size;
}
/**
@ -143,8 +143,8 @@ size_t darraySize(darray_t *self) {
* \param idx Index de lélément que lon souhaite récupérer
* \return Pointeur de type `void*` pointant sur lélément si lindex est valide, sur NULL sinon.
*/
void *darrayGet(darray_t *self, size_t idx) {
void *darrayGet(darray_t t_self, size_t t_idx) {
void *itr;
itr = (char *)self->begin + idx * self->element_size;
itr = (char *)t_self->begin + t_idx * t_self->element_size;
return itr;
}

View File

@ -20,35 +20,28 @@ typedef struct darray_s {
void *end; /*!< Pointeur sur lélément situé immédiatement après le dernier élément du tableau */
size_t element_size; /*!< Taille des éléments stockés dans le tableau */
size_t capacity; /*!< Capacité maximale du tableau actuel (non destinée à lutilisateur) */
} darray_t;
} darray;
typedef darray *darray_t;
/// \brief Créé un nouvel objet \ref darray_t vide
darray_t *darrayNew(size_t element_size);
/// \brief Augmente la capacité d'un \ref darray_t
void darrayExtend(darray_t *self);
/// \brief Insère un élément à lendroit pointé dans un \ref darray_t
void darrayInsert(darray_t *self, void *pos, void *elem);
/// \brief Supprime lélément pointé dans lobjet \ref darray_t
void darrayErase(darray_t *self, void *pos);
/// \brief Retourne lélément du \ref darray_t au idx-ème index
void *darrayGet(darray_t *self, size_t idx);
/// \brief Insère un élément à la fin de lélément \ref darray_t
void darrayPushBack(darray_t *self, void *elem);
/// \brief Supprime le dernier élément de lélément \ref darray_t
void darrayPopBack(darray_t *self);
/// \brief Détruit lélément \ref darray_t
void darrayDelete(darray_t *self);
size_t darraySize(darray_t *self);
/// \brief Renvoie la taille de lélément \ref darray_t
size_t darrayElemSize(darray_t *self);
/// \brief Créé un nouvel objet \ref darray vide
darray *darrayNew(size_t element_size);
/// \brief Augmente la capacité d'un \ref darray
void darrayExtend(darray *self);
/// \brief Insère un élément à lendroit pointé dans un \ref darray
void darrayInsert(darray *self, void *pos, void *elem);
/// \brief Supprime lélément pointé dans lobjet \ref darray
void darrayErase(darray *self, void *pos);
/// \brief Retourne lélément du \ref darray au idx-ème index
void *darrayGet(darray *self, size_t idx);
/// \brief Insère un élément à la fin de lélément \ref darray
void darrayPushBack(darray *self, void *elem);
/// \brief Supprime le dernier élément de lélément \ref darray
void darrayPopBack(darray *self);
/// \brief Détruit lélément \ref darray
void darrayDelete(darray *self);
/// \brief Renvoie la taille de lélément \ref darray
size_t darraySize(darray *self);
/// \brief Renvoie la taille de lélément \ref darray
size_t darrayElemSize(darray *self);
#endif /* DARRAY_H */

123
src/ppm.c
View File

@ -9,7 +9,7 @@
#include <assert.h>
#define RGB_COMPONENT_COLOR 255
#define CREATOR "Cartier"
#define CREATOR "CL"
/**
* \brief function description
@ -23,11 +23,11 @@
* \param[in] mode Mode du fichier à ouvrir
* \return Pointeur de fichier
*/
FILE* get_file(char *filename, const char* mode) {
FILE* fp = fopen(filename, mode);
FILE* get_file(char *t_filename, const char* t_mode) {
FILE* fp = fopen(t_filename, t_mode);
if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", filename);
exit(1);
fprintf(stderr, "Unable to open file '%s'\n", t_filename);
exit(FILE_IO_ERROR);
}
return fp;
}
@ -39,16 +39,16 @@ FILE* get_file(char *filename, const char* mode) {
* \param[in] fb Fichier ppm lire les données
* \param[in] filename Nom du fichier ouvert
*/
void read_file_format(FILE* fp, char* filename) {
void read_file_format(FILE* t_fp, char* t_filename) {
char buff[16];
if (!fgets(buff, sizeof(buff), fp)) {
perror(filename);
exit(1);
if (!fgets(buff, sizeof(buff), t_fp)) {
perror(t_filename);
exit(FILE_IO_ERROR);
}
/* check file format */
if (buff[0] != 'P' || buff[1] != '6') {
fprintf(stderr, "Invalid image format (must be 'P6')\n");
exit(1);
exit(FILE_FORMAT_ERROR);
}
}
@ -57,15 +57,15 @@ void read_file_format(FILE* fp, char* filename) {
*
* \param[in] fp Fichier ppm lire les données
*/
void check_for_comments(FILE* fp) {
void check_for_comments(FILE* t_fp) {
char c;
c = (char)getc(fp);
c = (char)getc(t_fp);
while (c == '#') {
while (getc(fp) != '\n')
while (getc(t_fp) != '\n')
;
c = (char)getc(fp);
c = (char)getc(t_fp);
}
ungetc(c, fp);
ungetc(c, t_fp);
}
/**
@ -76,10 +76,10 @@ void check_for_comments(FILE* fp) {
* \param[out] img Conteneur dimage écrire les résultats
* \param[in] filename Nom du fichier ouvert
*/
void read_file_size(FILE* fp, Image* img, char* filename) {
if (fscanf(fp, "%lu %lu", &img->x, &img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", filename);
exit(1);
void read_file_size(FILE* t_fp, Image* t_img, char* t_filename) {
if (fscanf(t_fp, "%lu %lu", &t_img->x, &t_img->y) != 2) {
fprintf(stderr, "Invalid image size (error loading '%s')\n", t_filename);
exit(FILE_FORMAT_ERROR);
}
}
@ -90,35 +90,36 @@ void read_file_size(FILE* fp, Image* img, char* filename) {
* \param[in] fp Fichier ppm lire les données
* \param[in] filename Nom du fichier ouvert
*/
void read_rgb(FILE* fp, char* filename) {
void read_rgb(FILE* t_fp, char* t_filename) {
char d;
int rgb_comp_color;
/* read rgb component */
if (fscanf(fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", filename);
exit(1);
if (fscanf(t_fp, "%d", &rgb_comp_color) != 1) {
fprintf(stderr, "Invalid rgb component (error loading '%s')\n", t_filename);
exit(FILE_FORMAT_ERROR);
}
fscanf(fp, "%c ", &d);
fscanf(t_fp, "%c ", &d);
/* check rgb component depth */
if (rgb_comp_color != RGB_COMPONENT_COLOR) {
fprintf(stderr, "'%s' does not have 8-bits components\n", filename);
exit(1);
fprintf(stderr, "'%s' does not have 8-bits components\n", t_filename);
exit(FILE_FORMAT_ERROR);
}
}
unsigned long read_data(FILE *fp, Image *img, GLubyte *data, char *filename) {
unsigned long read_data(FILE *t_fp, Image *t_img, GLubyte *t_data,
char *t_filename) {
unsigned long size;
/* allocation memoire */
size = img->x * img->y * 3;
printf("Size image %lu %lu => %lu\n", img->x, img->y, size);
data = (GLubyte *)malloc((size_t)size * sizeof(GLubyte));
assert(data);
size = t_img->x * t_img->y * 3;
printf("Size image %lu %lu => %lu\n", t_img->x, t_img->y, size);
t_data = (GLubyte *)malloc((size_t)size * sizeof(GLubyte));
assert(t_data);
/* read pixel data from file */
if (!fread(data, (size_t)1, (size_t)size, fp)) {
fprintf(stderr, "Error loading image '%s'\n", filename);
exit(1);
if (!fread(t_data, (size_t)1, (size_t)size, t_fp)) {
fprintf(stderr, "Error loading image '%s'\n", t_filename);
exit(FILE_IO_ERROR);
}
free(data);
free(t_data);
return size;
}
@ -133,12 +134,14 @@ unsigned long read_data(FILE *fp, Image *img, GLubyte *data, char *filename) {
* \param[in] size Taille du tableau de `GLubyte`
* \return return type
*/
void dataToImage(Image *img, GLubyte *data, unsigned long size) {
void dataToImage(Image *t_img, GLubyte *t_data, unsigned long t_size) {
unsigned long i;
img->pixels = darrayNew(sizeof(Pixel));
for (i = 0; i < size; i += 3) {
darrayPushBack(img->pixels, newPixel(data[i], data[i+1], data[i+2]));
darrayPushBack(img->pixels, newPixel(data[i], data[i+1], data[i+2]));
t_img->pixels = darrayNew(sizeof(Pixel));
for (i = 0; i < t_size; i += 3) {
darrayPushBack(t_img->pixels,
newPixel(t_data[i], t_data[i + 1], t_data[i + 2]));
darrayPushBack(t_img->pixels,
newPixel(t_data[i], t_data[i + 1], t_data[i + 2]));
}
}
@ -150,14 +153,14 @@ void dataToImage(Image *img, GLubyte *data, unsigned long size) {
* \param[in] img Conteneur dimage contenant les pixels à convertir
* \return Tableau de pointeurs de `GLuint`
*/
GLubyte *imageToData(Image_t img) {
GLubyte *imageToData(Image_t t_img) {
Pixel_t pixel;
GLubyte *data, size;
unsigned long i;
size = (GLubyte)darraySize(img->pixels);
size = (GLubyte)darraySize(t_img->pixels);
data = (GLubyte *)malloc(3 * sizeof(GLubyte) * size);
for(i = 0; i < size; i += 3) {
pixel = darrayGet(img->pixels, i / 3);
pixel = darrayGet(t_img->pixels, i / 3);
data[i] = pixel->r;
data[i + 1] = pixel->g;
data[i + 2] = pixel->b;
@ -175,17 +178,17 @@ GLubyte *imageToData(Image_t img) {
* \param[out] img Objet \ref Image manipulable
* \return Retourne 1 en cas de succès
*/
int ImageLoadPPM(char *filename, Image *img) {
int ImageLoadPPM(char *t_filename, Image *t_img) {
FILE *fp;
unsigned long size;
GLubyte* data = NULL;
fp = get_file(filename, "rb"); /* open PPM file for reading */
read_file_format(fp, filename); /* read image format */
check_for_comments(fp); /* check for comments */
read_file_size(fp, img, filename); /* read image size information */
read_rgb(fp, filename); /* read rgb component */
size = read_data(fp, img, data, filename); /* read data from file */
dataToImage(img, data, size);
GLubyte *data = NULL;
fp = get_file(t_filename, "rb"); /* open PPM file for reading */
read_file_format(fp, t_filename); /* read image format */
check_for_comments(fp); /* check for comments */
read_file_size(fp, t_img, t_filename); /* read image size information */
read_rgb(fp, t_filename); /* read rgb component */
size = read_data(fp, t_img, data, t_filename); /* read data from file */
dataToImage(t_img, data, size);
fclose(fp);
return 1;
}
@ -197,17 +200,17 @@ int ImageLoadPPM(char *filename, Image *img) {
* \param[in] filename Nom du fichier image à ouvrir
* \param[in] img Objet \ref Image à écrire
*/
void imagesavePPM(char *filename, Image_t img) {
void imagesavePPM(char *t_filename, Image_t t_img) {
FILE *fp;
GLubyte *data;
fp = get_file(filename, "wb"); /* open file for output */
fp = get_file(t_filename, "wb"); /* open file for output */
/* write the header file */
fprintf(fp, "P6\n"); /* image format */
fprintf(fp, "# Created by %s\n", CREATOR); /* comments */
fprintf(fp, "%lu %lu\n", img->y, img->y); /* image size */
fprintf(fp, "%d\n", RGB_COMPONENT_COLOR); /* rgb component depth */
data = imageToData(img); /* pixel data */
fwrite(data, (size_t)1, (size_t)(3 * img->x * img->y), fp);
fprintf(fp, "P6\n"); /* image format */
fprintf(fp, "# Created by %s\n", CREATOR); /* comments */
fprintf(fp, "%lu %lu\n", t_img->y, t_img->y); /* image size */
fprintf(fp, "%d\n", RGB_COMPONENT_COLOR); /* rgb component depth */
data = imageToData(t_img); /* pixel data */
fwrite(data, (size_t)1, (size_t)(3 * t_img->x * t_img->y), fp);
free(data);
fclose(fp);
}

View File

@ -18,12 +18,12 @@
* \param[in] b Valeur bleue du pixel
* \return Pointeur sur une structure de type \ref Pixel
*/
Pixel_t newPixel(uint8_t r, uint8_t g, uint8_t b) {
Pixel_t newPixel(uint8_t t_r, uint8_t t_g, uint8_t t_b) {
Pixel_t res;
res = (Pixel_t)malloc(sizeof(Pixel));
res->r = r;
res->g = g;
res->b = b;
res->r = t_r;
res->g = t_g;
res->b = t_b;
res->visited = false;
return res;
}
@ -36,8 +36,8 @@ Pixel_t newPixel(uint8_t r, uint8_t g, uint8_t b) {
*
* \param[in] self Pointeur vers le pixel à détruire
*/
void deletePixel(Pixel_t self) {
free(self);
void deletePixel(Pixel_t t_self) {
free(t_self);
}
/**
@ -64,13 +64,13 @@ Image_t newImage() {
*
* \param[in] self Conteneur dimage à détruire
*/
void deleteImage(Image_t self) {
void deleteImage(Image_t t_self) {
unsigned long i;
for(i = 0; i < darraySize(self->pixels); ++i) {
deletePixel(darrayGet(self->pixels, i));
for(i = 0; i < darraySize(t_self->pixels); ++i) {
deletePixel(darrayGet(t_self->pixels, i));
}
darrayDelete(self->pixels);
free(self);
darrayDelete(t_self->pixels);
free(t_self);
}
/**
@ -83,12 +83,12 @@ void deleteImage(Image_t self) {
* \param[in] xg Abscisse extrême gauche du segment
* \return Pointeur sur un conteneur de segment
*/
Segment_t newSegment(uint16_t y, uint16_t xd, uint16_t xg) {
Segment_t newSegment(uint16_t t_y, uint16_t t_xd, uint16_t t_xg) {
Segment_t res;
res = (Segment_t)malloc(sizeof(Segment));
res->y = y;
res->xd = xd;
res->xg = xg;
res->y = t_y;
res->xd = t_xd;
res->xg = t_xg;
return res;
}
@ -99,8 +99,8 @@ Segment_t newSegment(uint16_t y, uint16_t xd, uint16_t xg) {
*
* \param[in] self Conteneur de segment à détruire
*/
void deleteSegment(Segment_t self) {
free(self);
void deleteSegment(Segment_t t_self) {
free(t_self);
}
/**
@ -115,12 +115,12 @@ void deleteSegment(Segment_t self) {
* \param[in] b Valeur bleue de la couleur de la zone
* \return Pointeur vers la structure créée
*/
Zone_t newZone(uint8_t r, uint8_t g, uint8_t b) {
Zone_t newZone(uint8_t t_r, uint8_t t_g, uint8_t t_b) {
Zone_t res;
res = (Zone_t)malloc(sizeof(Zone));
res->r = r;
res->g = g;
res->b = b;
res->r = t_r;
res->g = t_g;
res->b = t_b;
res->segments = darrayNew(sizeof(Segment));
return res;
}
@ -133,11 +133,11 @@ Zone_t newZone(uint8_t r, uint8_t g, uint8_t b) {
*
* \param[in] self Conteneur de zone à détruire
*/
void deleteZone(Zone_t self) {
void deleteZone(Zone_t t_self) {
unsigned long i;
for(i = 0; i < darraySize(self->segments); ++i) {
deleteSegment(darrayGet(self->segments, i));
for(i = 0; i < darraySize(t_self->segments); ++i) {
deleteSegment(darrayGet(t_self->segments, i));
}
darrayDelete(self->segments);
free(self);
darrayDelete(t_self->segments);
free(t_self);
}

View File

@ -32,6 +32,16 @@
printf
#endif
/// Pas derreur
#define NOERROR 0
/// Constante pour les erreurs liées aux arguments
#define ARGERROR 1
/// Constante pour les erreurs liées à \ref darray_t
#define PTR_ERROR 2
/// Constante pour les erreurs liées à la lecture et écriture de fichiers
#define FILE_IO_ERROR 3
#define FILE_FORMAT_ERROR 4
/*****************************************************************************/
/* STRUCT DECLARATION */
/*****************************************************************************/
@ -64,7 +74,7 @@ typedef Segment *Segment_t;
struct Image {
uint64_t x; /*!< Largeur de limage */
uint64_t y; /*!< Hauteur de limage */
darray_t *pixels; /*!< Vecteur de pixels */
darray_t pixels; /*!< Vecteur de pixels */
};
/**
@ -91,7 +101,7 @@ struct Zone {
uint8_t r; /*!< Couleur rouge de la zone */
uint8_t g; /*!< Couleur verte de la zone */
uint8_t b; /*!< Couleur bleue de la zone */
darray_t *segments; /*!< Vecteur de segments */
darray_t segments; /*!< Vecteur de segments */
};
/**