Merge branch 'fixmalloc' into 'master'
Fixmalloc See merge request phundrak/surfaces-unies!1
This commit is contained in:
commit
df4ad14ef1
@ -3,9 +3,17 @@ image: nvidia/opengl:devel
|
||||
stages:
|
||||
- build
|
||||
|
||||
cache:
|
||||
key: apt-cache
|
||||
paths:
|
||||
- apt-cache/
|
||||
|
||||
before_script:
|
||||
- export APT_CACHE_DIR=`pwd`/apt-cache && mkdir -pv $APT_CACHE_DIR
|
||||
- apt-get update -yq && apt-get -o dir::cache::archives="$APT_CACHE_DIR" install -y build-essential cmake
|
||||
|
||||
build:
|
||||
stage: build
|
||||
script:
|
||||
- apt update && apt install -y build-essential cmake
|
||||
- gcc --version
|
||||
- mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Release .. && make -j
|
||||
|
@ -7,8 +7,8 @@ set(TGT "surfaces-unies")
|
||||
set(${TGT}_VERSION_MAJOR 0)
|
||||
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(CMAKE_C_FLAGS_DEBUG "${CC_COVERAGE_COMPILE_FLAGS} -DDebug -g -pg")
|
||||
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_RELEASE "${CC_COVERAGE_COMPILE_FLAGS} -O3")
|
||||
|
||||
set(CMAKE_CC_STANDARD 11)
|
||||
|
@ -32,19 +32,19 @@ DOXYFILE_ENCODING = UTF-8
|
||||
# title of most generated pages and in a few other places.
|
||||
# The default value is: My Project.
|
||||
|
||||
PROJECT_NAME = "Image Taches"
|
||||
PROJECT_NAME = "Compression par surfaces unies"
|
||||
|
||||
# The PROJECT_NUMBER tag can be used to enter a project or revision number. This
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
# control system is used.
|
||||
|
||||
PROJECT_NUMBER = 0.9
|
||||
PROJECT_NUMBER = 0.1
|
||||
|
||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||
# for a project that appears at the top of each page and should give viewer a
|
||||
# quick idea about the purpose of the project. Keep the description short.
|
||||
|
||||
PROJECT_BRIEF = "Simple programme de détection de zones de couleur"
|
||||
PROJECT_BRIEF = "Programme de compression d'images par zones de couleurs similaires"
|
||||
|
||||
# With the PROJECT_LOGO tag one can specify a logo or an icon that is included
|
||||
# in the documentation. The maximum height of the logo should not exceed 55
|
||||
|
37
src/common.c
37
src/common.c
@ -1,14 +1,36 @@
|
||||
#include "common.h"
|
||||
#include "ppm.h"
|
||||
/**
|
||||
* \file common.c
|
||||
* \brief Implémentation de la (dé)compression d’images
|
||||
*/
|
||||
|
||||
#include "common.h"
|
||||
|
||||
/**
|
||||
* Cette fonction permet d’évaluer si le pixel passé en argument est éligible à
|
||||
* la zone passée également en argument.
|
||||
*
|
||||
* \param[in] t_pixel Pointeur vers le pixel dont l’éligibilité est testée
|
||||
* \param[in] t_zone Zone à laquelle le pixel est éligible ou non
|
||||
* \return Valeur booléenne, `true` si le pixel est éligible, `false` sinon
|
||||
*/
|
||||
bool sameColor(Pixel_t t_pixel, Zone_t t_zone) {
|
||||
return t_pixel->r == t_zone->r && t_pixel->g == t_zone->g &&
|
||||
t_pixel->b == t_zone->b;
|
||||
}
|
||||
|
||||
/* Adds a pixel to a zone, since the pixel isn't in a zone, then it isn't
|
||||
part of a segment either, hence we can confidently add pixels next to the
|
||||
original as parts of the segment and add the segment itself to the zone */
|
||||
/**
|
||||
* Ajoute un pixel à la zone passé en argument si le pixel à l’index passé en
|
||||
* argument est éligible à la zone. Si un pixel n’a pas encore été visité, cela
|
||||
* veut dire également qu’il ne fait partie d’aucun segment, il sera donc
|
||||
* ajouté à un nouveau segment auquel seront rajoutés tous les pixels connexes
|
||||
* éligibles à la zone. Ensuite, le segment est ajouté à la zone, et la
|
||||
* fonction actuelle est appelée sur tous les pixels supérieurs et inférieurs
|
||||
* aux pixels du segment.
|
||||
*
|
||||
* \param[in] t_img Image contenant les pixels explorés
|
||||
* \param[in] t_idx Index du pixel actuel dans l’image `t_img`
|
||||
* \param[out] t_zone Zone à laquelle sera potentiellement ajouté le pixel
|
||||
*/
|
||||
void addPixelToSelectedZone(Image_t t_img, int t_idx, Zone_t t_zone) {
|
||||
Pixel_t current_pixel;
|
||||
int xd, xg, y = t_idx / (int)t_img->x;
|
||||
@ -70,12 +92,15 @@ darray_t imgToZones(Image_t t_img) {
|
||||
return zones;
|
||||
}
|
||||
|
||||
void compress(char *input_file) {
|
||||
void compress(const char *input_file) {
|
||||
Image_t img;
|
||||
darray_t zones;
|
||||
img = newImage();
|
||||
imageLoadPPM(input_file, img);
|
||||
zones = imgToZones(img);
|
||||
darrayDelete(zones);
|
||||
|
||||
printf("Detected %zu zones\n", darraySize(zones));
|
||||
|
||||
deleteImage(img);
|
||||
}
|
||||
|
11
src/common.h
11
src/common.h
@ -1,10 +1,19 @@
|
||||
/**
|
||||
* \file common.h
|
||||
* \brief Déclaration pour la (dé)compression d’images
|
||||
*/
|
||||
|
||||
#ifndef COMMON_H
|
||||
#define COMMON_H
|
||||
|
||||
#include "utilities.h"
|
||||
#include "ppm.h"
|
||||
|
||||
/// Teste l’éligibilité d’un pixel à une zone
|
||||
bool sameColor(Pixel_t, Zone_t);
|
||||
/// Ajoute un pixel et ses pixels connexes à une zone
|
||||
void addPixelToSelectedZone(Image_t t_img, int t_idx, Zone_t t_zone);
|
||||
void chooseZoneForPixel(Image_t t_img, int t_idx, darray_t zones);
|
||||
darray_t imgToZones(Image_t t_img);
|
||||
void compress(const char*);
|
||||
|
||||
#endif /* COMMON_H */
|
||||
|
@ -18,7 +18,7 @@
|
||||
*/
|
||||
darray_t darrayNew(size_t t_element_size) {
|
||||
darray_t ret;
|
||||
ret = (darray_t)malloc(sizeof(darray_t));
|
||||
ret = (darray_t)malloc(sizeof(darray));
|
||||
ret->begin = NULL;
|
||||
ret->end = ret->begin;
|
||||
ret->element_size = t_element_size;
|
||||
|
@ -10,10 +10,10 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
/**
|
||||
* \struct darray_t
|
||||
* \struct darray_s
|
||||
* \brief Tableau dynamique
|
||||
*
|
||||
* Les objets `darray_t` offrent la possibilité d’avoir des tableaux à taille
|
||||
* Les objets `darray_s` offrent la possibilité d’avoir des tableaux à taille
|
||||
* variable en C, similairement aux objets `vector` en C++.
|
||||
*/
|
||||
typedef struct darray_s {
|
||||
@ -22,6 +22,7 @@ typedef struct darray_s {
|
||||
size_t element_size; /*!< Taille des éléments stockés dans le tableau */
|
||||
size_t capacity; /*!< Capacité maximale du tableau actuel (non destinée à l’utilisateur) */
|
||||
} darray;
|
||||
/// `darray_t` est un pointeur vers une structure de type \ref darray_s
|
||||
typedef darray *darray_t;
|
||||
|
||||
/// \brief Créé un nouvel objet \ref darray vide
|
||||
|
@ -8,7 +8,7 @@
|
||||
* des fonctions cœures du programme.
|
||||
*/
|
||||
|
||||
#include "utilities.h"
|
||||
#include "common.h"
|
||||
#include <getopt.h>
|
||||
#include <string.h>
|
||||
|
||||
@ -91,7 +91,7 @@ void get_args(Argres *t_args, int *t_c) {
|
||||
Argres process_args(const int t_argc, char *t_argv[]) {
|
||||
Argres res;
|
||||
res.input = NULL;
|
||||
res.output = NULL;
|
||||
res.output = "output.fs";
|
||||
while (true) {
|
||||
int option_index = 0;
|
||||
static struct option long_options[] = {
|
||||
@ -122,12 +122,9 @@ int main(int argc, char **argv) {
|
||||
fprintf(stderr, "ERROR: no input file.");
|
||||
help(ARGERROR);
|
||||
}
|
||||
if (!argresults.output) {
|
||||
argresults.output = "output.fs";
|
||||
}
|
||||
printf("input: %s\noutput: %s\n", argresults.input, argresults.output);
|
||||
if(argresults.compress) {
|
||||
puts("Compressing...");
|
||||
compress(argresults.input);
|
||||
} else {
|
||||
puts("Uncompressing...");
|
||||
}
|
||||
|
49
src/ppm.c
49
src/ppm.c
@ -23,7 +23,7 @@
|
||||
* \param[in] mode Mode du fichier à ouvrir
|
||||
* \return Pointeur de fichier
|
||||
*/
|
||||
FILE* get_file(char *t_filename, const char* t_mode) {
|
||||
FILE* get_file(const char *t_filename, const char* t_mode) {
|
||||
FILE* fp = fopen(t_filename, t_mode);
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Unable to open file '%s'\n", t_filename);
|
||||
@ -39,7 +39,7 @@ FILE* get_file(char *t_filename, const char* t_mode) {
|
||||
* \param[in] fb Fichier ppm où lire les données
|
||||
* \param[in] filename Nom du fichier ouvert
|
||||
*/
|
||||
void read_file_format(FILE* t_fp, char* t_filename) {
|
||||
void read_file_format(FILE* t_fp, const char* t_filename) {
|
||||
char buff[16];
|
||||
if (!fgets(buff, sizeof(buff), t_fp)) {
|
||||
perror(t_filename);
|
||||
@ -76,7 +76,7 @@ void check_for_comments(FILE* t_fp) {
|
||||
* \param[out] img Conteneur d’image où écrire les résultats
|
||||
* \param[in] filename Nom du fichier ouvert
|
||||
*/
|
||||
void read_file_size(FILE* t_fp, Image* t_img, char* t_filename) {
|
||||
void read_file_size(FILE* t_fp, Image* t_img, const 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,7 +90,7 @@ void read_file_size(FILE* t_fp, Image* t_img, char* t_filename) {
|
||||
* \param[in] fp Fichier ppm où lire les données
|
||||
* \param[in] filename Nom du fichier ouvert
|
||||
*/
|
||||
void read_rgb(FILE* t_fp, char* t_filename) {
|
||||
void read_rgb(FILE* t_fp, const char* t_filename) {
|
||||
char d;
|
||||
int rgb_comp_color;
|
||||
/* read rgb component */
|
||||
@ -106,40 +106,40 @@ void read_rgb(FILE* t_fp, char* t_filename) {
|
||||
}
|
||||
}
|
||||
|
||||
unsigned long read_data(FILE *t_fp, Image *t_img, GLubyte *t_data,
|
||||
char *t_filename) {
|
||||
unsigned long read_data(FILE *t_fp, Image *t_img, unsigned char **t_data,
|
||||
const char *t_filename) {
|
||||
unsigned long size;
|
||||
/* allocation memoire */
|
||||
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);
|
||||
*t_data = (unsigned char *)malloc((size_t)size * sizeof(unsigned char));
|
||||
assert(*t_data);
|
||||
/* 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);
|
||||
free(t_data);
|
||||
exit(FILE_IO_ERROR);
|
||||
}
|
||||
free(t_data);
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convertit vers un tableau de `GLubyte` les pixels contenus dans un conteneur
|
||||
* d’image. La taille du tableau de `GLubyte` est la taille du tableau de
|
||||
* Convertit vers un tableau de `unsigned char` les pixels contenus dans un conteneur
|
||||
* d’image. La taille du tableau de `unsigned char` est la taille du tableau de
|
||||
* pixels multipliée par trois du fait des trois emplacements séparés par
|
||||
* couleur.
|
||||
*
|
||||
* \param[out] img Image dont les pixels doivent être convertis
|
||||
* \param[in] data Données à convertir en structures \ref Pixel
|
||||
* \param[in] size Taille du tableau de `GLubyte`
|
||||
* \param[in] size Taille du tableau de `unsigned char`
|
||||
* \return return type
|
||||
*/
|
||||
void dataToImage(Image *t_img, GLubyte *t_data, unsigned long t_size) {
|
||||
void dataToImage(Image *t_img, unsigned char *t_data, unsigned long t_size) {
|
||||
unsigned long i;
|
||||
t_img->pixels = darrayNew(sizeof(Pixel));
|
||||
printf("Size of data: %lu\n", t_size);
|
||||
for (i = 0; i < t_size; i += 3) {
|
||||
darrayPushBack(t_img->pixels,
|
||||
newPixel(t_data[i], t_data[i + 1], t_data[i + 2]));
|
||||
printf("%lu\t", i);
|
||||
darrayPushBack(t_img->pixels,
|
||||
newPixel(t_data[i], t_data[i + 1], t_data[i + 2]));
|
||||
}
|
||||
@ -153,12 +153,12 @@ void dataToImage(Image *t_img, GLubyte *t_data, unsigned long t_size) {
|
||||
* \param[in] img Conteneur d’image contenant les pixels à convertir
|
||||
* \return Tableau de pointeurs de `GLuint`
|
||||
*/
|
||||
GLubyte *imageToData(Image_t t_img) {
|
||||
unsigned char *imageToData(Image_t t_img) {
|
||||
Pixel_t pixel;
|
||||
GLubyte *data, size;
|
||||
unsigned char *data, size;
|
||||
unsigned long i;
|
||||
size = (GLubyte)darraySize(t_img->pixels);
|
||||
data = (GLubyte *)malloc(3 * sizeof(GLubyte) * size);
|
||||
size = (unsigned char)darraySize(t_img->pixels);
|
||||
data = (unsigned char *)malloc(3 * sizeof(unsigned char) * size);
|
||||
for(i = 0; i < size; i += 3) {
|
||||
pixel = darrayGet(t_img->pixels, i / 3);
|
||||
data[i] = pixel->r;
|
||||
@ -178,16 +178,17 @@ GLubyte *imageToData(Image_t t_img) {
|
||||
* \param[out] img Objet \ref Image manipulable
|
||||
* \return Retourne 1 en cas de succès
|
||||
*/
|
||||
int imageLoadPPM(char *t_filename, Image *t_img) {
|
||||
int imageLoadPPM(const char *t_filename, Image *t_img) {
|
||||
FILE *fp;
|
||||
unsigned long size;
|
||||
GLubyte *data = NULL;
|
||||
unsigned char *data = NULL;
|
||||
printf("Hey auie\n");
|
||||
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 */
|
||||
size = read_data(fp, t_img, &data, t_filename); /* read data from file */
|
||||
dataToImage(t_img, data, size);
|
||||
fclose(fp);
|
||||
return 1;
|
||||
@ -202,7 +203,7 @@ int imageLoadPPM(char *t_filename, Image *t_img) {
|
||||
*/
|
||||
void imageSavePPM(char *t_filename, Image_t t_img) {
|
||||
FILE *fp;
|
||||
GLubyte *data;
|
||||
unsigned char *data;
|
||||
fp = get_file(t_filename, "wb"); /* open file for output */
|
||||
/* write the header file */
|
||||
fprintf(fp, "P6\n"); /* image format */
|
||||
|
17
src/ppm.h
17
src/ppm.h
@ -13,23 +13,24 @@
|
||||
#include "utilities.h"
|
||||
|
||||
/// \brief Ouvre un fichier avec les autorisations demandées
|
||||
FILE *get_file(char *filename, const char *mode);
|
||||
FILE *get_file(const char *filename, const char *mode);
|
||||
/// \brief Lit le format d’un fichier ppm ouvert
|
||||
void read_file_format(FILE* fp, char* filename);
|
||||
void read_file_format(FILE *fp, const char *filename);
|
||||
/// \brief Vérifie et ignore d’éventuels commentaires du header d’un fichier
|
||||
void check_for_comments(FILE *fp);
|
||||
/// \brief Lit les dimensions du fichier ppm ouvert
|
||||
void read_file_size(FILE *fp, Image *img, char *filename);
|
||||
void read_file_size(FILE *fp, Image *img, const char *filename);
|
||||
/// \brief Lit et vérifie le format RGB du fichier ppm
|
||||
void read_rgb(FILE *fp, char *filename);
|
||||
void read_rgb(FILE *fp, const char *filename);
|
||||
/// \brief Lit dans le conteneur les données images du fichier ppm
|
||||
unsigned long read_data(FILE *fp, Image *img, GLubyte *data, char *filename);
|
||||
unsigned long read_data(FILE *fp, Image *img, unsigned char **data,
|
||||
const char *filename);
|
||||
/// \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, unsigned char *data, unsigned long size);
|
||||
/// \brief Convertit les pixels d’une image en tableau natif OpenGL
|
||||
GLubyte *imageToData(Image_t img);
|
||||
unsigned char *imageToData(Image_t img);
|
||||
/// \brief Ouverture et lecture de l’image d’entrée
|
||||
int imageLoadPPM(char *filename, Image_t img);
|
||||
int imageLoadPPM(const char *filename, Image_t img);
|
||||
/// \brief Ouverture et écriture de l'image de sortie
|
||||
void imageSavePPM(char *filename, Image_t img);
|
||||
|
||||
|
@ -11,7 +11,7 @@
|
||||
#define UTILITIES_H
|
||||
|
||||
#include "darray.h"
|
||||
#include <GL/gl.h>
|
||||
/* #include <GL/gl.h> */
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
Loading…
Reference in New Issue
Block a user