fixed... kinda? Works with valgrind, not vanilla

This commit is contained in:
Phuntsok Drak-pa 2018-11-11 18:17:33 +01:00
parent 0315cbf7dd
commit 4f62acd157
3 changed files with 12 additions and 13 deletions

View File

@ -7,7 +7,7 @@ set(TGT "surfaces-unies")
set(${TGT}_VERSION_MAJOR 0) set(${TGT}_VERSION_MAJOR 0)
set(${TGT}_VERSION_MINOR 1) set(${TGT}_VERSION_MINOR 1)
set(CC_COVERAGE_COMPILE_FLAGS "-pedantic -Wall -Wextra -Wfloat-equal -Wwrite-strings -Wpointer-arith -Wcast-align -Wshadow -Wredundant-decls -Wdouble-promotion -Winit-self -Wswitch-default -Wswitch-enum -Wundef -Winline") set(CC_COVERAGE_COMPILE_FLAGS "-Wall -Wextra -Wfloat-equal -Wwrite-strings -Wpointer-arith -Wcast-align -Wshadow -Wredundant-decls -Wdouble-promotion -Winit-self -Wswitch-default -Wswitch-enum -Wundef -Winline")
set(CMAKE_C_FLAGS_DEBUG "${CC_COVERAGE_COMPILE_FLAGS} -DDebug -g") set(CMAKE_C_FLAGS_DEBUG "${CC_COVERAGE_COMPILE_FLAGS} -DDebug -g")
set(CMAKE_C_FLAGS_RELEASE "${CC_COVERAGE_COMPILE_FLAGS} -O3") set(CMAKE_C_FLAGS_RELEASE "${CC_COVERAGE_COMPILE_FLAGS} -O3")

View File

@ -24,9 +24,7 @@
* \return Pointeur de fichier * \return Pointeur de fichier
*/ */
FILE* get_file(const char *t_filename, const char* t_mode) { FILE* get_file(const char *t_filename, const char* t_mode) {
printf("Opening, fopen(\"%s\", \"%s\")\n", t_filename, t_mode);
FILE* fp = fopen(t_filename, t_mode); FILE* fp = fopen(t_filename, t_mode);
printf("Opened file... maybe?\n");
if (!fp) { if (!fp) {
fprintf(stderr, "Unable to open file '%s'\n", t_filename); fprintf(stderr, "Unable to open file '%s'\n", t_filename);
exit(FILE_IO_ERROR); exit(FILE_IO_ERROR);
@ -108,20 +106,20 @@ void read_rgb(FILE* t_fp, const char* t_filename) {
} }
} }
unsigned long read_data(FILE *t_fp, Image *t_img, GLubyte *t_data, unsigned long read_data(FILE *t_fp, Image *t_img, GLubyte **t_data,
const char *t_filename) { const char *t_filename) {
unsigned long size; unsigned long size;
/* allocation memoire */ /* allocation memoire */
size = t_img->x * t_img->y * 3; size = t_img->x * t_img->y * 3;
printf("Size image %lu %lu => %lu\n", t_img->x, t_img->y, size); printf("Size image %lu %lu => %lu\n", t_img->x, t_img->y, size);
t_data = (GLubyte *)malloc((size_t)size * sizeof(GLubyte)); *t_data = (GLubyte *)malloc((size_t)size * sizeof(GLubyte));
assert(t_data); assert(*t_data);
/* read pixel data from file */ /* read pixel data from file */
if (!fread(t_data, (size_t)1, (size_t)size, t_fp)) { if (!fread(*t_data, (size_t)1, (size_t)size, t_fp)) {
fprintf(stderr, "Error loading image '%s'\n", t_filename); fprintf(stderr, "Error loading image '%s'\n", t_filename);
free(t_data);
exit(FILE_IO_ERROR); exit(FILE_IO_ERROR);
} }
free(t_data);
return size; return size;
} }
@ -138,10 +136,10 @@ unsigned long read_data(FILE *t_fp, Image *t_img, GLubyte *t_data,
*/ */
void dataToImage(Image *t_img, GLubyte *t_data, unsigned long t_size) { void dataToImage(Image *t_img, GLubyte *t_data, unsigned long t_size) {
unsigned long i; unsigned long i;
printf("Hey 1\n");
t_img->pixels = darrayNew(sizeof(Pixel)); t_img->pixels = darrayNew(sizeof(Pixel));
printf("Hey 2\n"); printf("Size of data: %lu\n", t_size);
for (i = 0; i < t_size; i += 3) { for (i = 0; i < t_size; i += 3) {
printf("%lu\t", i);
darrayPushBack(t_img->pixels, darrayPushBack(t_img->pixels,
newPixel(t_data[i], t_data[i + 1], t_data[i + 2])); newPixel(t_data[i], t_data[i + 1], t_data[i + 2]));
} }
@ -190,7 +188,7 @@ int imageLoadPPM(const char *t_filename, Image *t_img) {
check_for_comments(fp); /* check for comments */ check_for_comments(fp); /* check for comments */
read_file_size(fp, t_img, t_filename); /* read image size information */ read_file_size(fp, t_img, t_filename); /* read image size information */
read_rgb(fp, t_filename); /* read rgb component */ read_rgb(fp, t_filename); /* read rgb component */
size = read_data(fp, t_img, data, t_filename); /* read data from file */ size = read_data(fp, t_img, &data, t_filename); /* read data from file */
dataToImage(t_img, data, size); dataToImage(t_img, data, size);
fclose(fp); fclose(fp);
return 1; return 1;

View File

@ -23,7 +23,8 @@ void read_file_size(FILE *fp, Image *img, const char *filename);
/// \brief Lit et vérifie le format RGB du fichier ppm /// \brief Lit et vérifie le format RGB du fichier ppm
void read_rgb(FILE *fp, const char *filename); void read_rgb(FILE *fp, const char *filename);
/// \brief Lit dans le conteneur les données images du fichier ppm /// \brief Lit dans le conteneur les données images du fichier ppm
unsigned long read_data(FILE *fp, Image *img, GLubyte *data, const char *filename); unsigned long read_data(FILE *fp, Image *img, GLubyte **data,
const char *filename);
/// \brief Convertit les données brutes de fichier vers des conteneurs de pixels /// \brief Convertit les données brutes de fichier vers des conteneurs de pixels
void dataToImage(Image *img, GLubyte *data, unsigned long size); void dataToImage(Image *img, GLubyte *data, unsigned long size);
/// \brief Convertit les pixels dune image en tableau natif OpenGL /// \brief Convertit les pixels dune image en tableau natif OpenGL