diff --git a/.gitignore b/.gitignore index 91c798f..d2a3277 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ debug *.out \.idea/ cmake-build-debug/ +*.su diff --git a/img/asterix.ppm b/img/asterix.ppm index e25688a..526e509 100644 Binary files a/img/asterix.ppm and b/img/asterix.ppm differ diff --git a/src/compress.c b/src/compress.c index 71c031c..9a87521 100644 --- a/src/compress.c +++ b/src/compress.c @@ -13,9 +13,11 @@ * \param[in] t_zone Zone à laquelle le pixel est éligible ou non * \return Valeur booléenne, `1` si le pixel est éligible, `0` sinon */ -bool sameColor(Pixel *t_pixel, Zone *t_zone) { - return t_pixel->r == t_zone->r && t_pixel->g == t_zone->g && - t_pixel->b == t_zone->b; +uint8_t sameColor(Pixel *t_pixel, Zone *t_zone) { + return (t_pixel->r == t_zone->r && t_pixel->g == t_zone->g && + t_pixel->b == t_zone->b) + ? 1 + : 0; } /** @@ -119,33 +121,45 @@ darray *imgToZones(Image *t_img) { const size_t nb_pixels = darraySize(t_img->pixels); int64_t i; zones = darrayNew(sizeof(Zone)); - - /* for each pixel, try to create a new zone */ for (i = 0; i < (int64_t)nb_pixels; ++i) { chooseZoneForPixel(t_img, i, zones); } return zones; } +void write_compressed_file(Image *t_img, FILE *t_output, darray *t_zones) { + size_t i, j; + Zone *current_zone; + Segment *segment; + fwrite(&t_img->x, sizeof(t_img->x), 2, t_output); + for (i = 0; i < darraySize(t_zones); ++i) { + current_zone = darrayGet(t_zones, i); + fwrite(¤t_zone->r, sizeof(current_zone->r) * 3, 1, t_output); + for (j = 0; j < darraySize(current_zone->segments); ++j) { + segment = darrayGet(current_zone->segments, j); + fwrite(&segment->right_limit, sizeof(Segment), 1, t_output); + } + darrayDelete(current_zone->segments); + } +} + /** * Convertit une image en zones puis écrit ces zones dans un fichier, * compressant ainsi l'image passée en argument. * - * \param[in] t_input_file Nom/chemin du fichier ppm d'entrée + * \param[in] t_input_file Nom/chemin du fichier `.ppm` d'entrée + * \param[in] t_output_file Nom/chemin du fichier `.su` de sortie */ -void compress(const char *t_input_file) { +void compress(const char *t_input_file, const char *t_output_file) { Image *img; darray *zones; - Zone *current_zone; - size_t i; + FILE *output_file; img = newImage(); imageLoadPPM(t_input_file, img); + output_file = get_file(t_output_file, "wb"); zones = imgToZones(img); - + write_compressed_file(img, output_file, zones); deleteImage(img); - for (i = 0; i < darraySize(zones); ++i) { - current_zone = darrayGet(zones, i); - darrayDelete(current_zone->segments); - } darrayDelete(zones); + fclose(output_file); } diff --git a/src/compress.h b/src/compress.h index c5ace1e..7aa31b6 100644 --- a/src/compress.h +++ b/src/compress.h @@ -9,7 +9,7 @@ #include "ppm.h" /// Teste l’éligibilité d’un pixel à une zone -bool sameColor(Pixel *t_pixel, Zone *t_zone); +uint8_t sameColor(Pixel *t_pixel, Zone *t_zone); /// Ajoute un pixel et ses pixels connexes à une zone void addPixelToSelectedZone(Image *t_img, int64_t t_idx, Zone *t_zone); /// Sélectionne la zone correspondant à la couleur d'un pixel @@ -17,6 +17,6 @@ void chooseZoneForPixel(Image *t_img, int64_t t_idx, darray *zones); /// Créé les zones d'une image darray *imgToZones(Image *t_img); /// Compresse l'image d'entrée -void compress(const char *t_input_file); +void compress(const char *t_input_file, const char *t_output_file); #endif /* SRC_COMPRESS_H_ */ diff --git a/src/main.c b/src/main.c index aca41a8..a524b28 100644 --- a/src/main.c +++ b/src/main.c @@ -132,7 +132,7 @@ int main(int argc, char **argv) { printf("input: %s\noutput: %s\n", argresults.input, argresults.output); } if(argresults.compress) { - compress(argresults.input); + compress(argresults.input, argresults.output); } else { puts("Uncompressing..."); } diff --git a/src/utilities.c b/src/utilities.c index c9f0997..4cd1798 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -24,7 +24,7 @@ Pixel *newPixel(uint8_t t_r, uint8_t t_g, uint8_t t_b) { res->r = t_r; res->g = t_g; res->b = t_b; - res->visited = false; + res->visited = 0; return res; } diff --git a/src/utilities.h b/src/utilities.h index bb17a0d..b023d2d 100644 --- a/src/utilities.h +++ b/src/utilities.h @@ -11,7 +11,6 @@ #define SRC_UTILITIES_H_ #include "darray.h" -#include #include #include