removed stdbool and now writes compressed file

parent 0ab3192f
Pipeline #67 passed with stage
in 1 minute and 6 seconds
......@@ -5,3 +5,4 @@ debug
*.out
\.idea/
cmake-build-debug/
*.su
No preview for this file type
......@@ -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(&current_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);
}
......@@ -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_ */
......@@ -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...");
}
......
......@@ -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;
}
......
......@@ -11,7 +11,6 @@
#define SRC_UTILITIES_H_
#include "darray.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment