diff --git a/TODOs.org b/TODOs.org index 8a24572..6005e7d 100644 --- a/TODOs.org +++ b/TODOs.org @@ -1,4 +1,5 @@ * Development +** TODO Remove y axis on segments ** TODO Test if the zones detection works ** TODO Add compressed file output ** TODO Add file uncompression diff --git a/src/common.c b/src/common.c index caeb266..97c0938 100644 --- a/src/common.c +++ b/src/common.c @@ -61,22 +61,34 @@ void addPixelToSelectedZone(Image* t_img, int t_idx, Zone* t_zone) { } } -/* Selects the zone related to the pixel, skip tests if pixel has - already been visited */ +/* Selects the zone related to the pixel, skip tests if pixel has already been + * visited */ void chooseZoneForPixel(Image* t_img, int t_idx, darray *zones) { Zone* current_zone; Pixel* pixel; size_t i; + pixel = darrayGet(t_img->pixels, t_idx); - if (pixel->visited) + /* if the pixel has already been visited, no need to continue */ + if (pixel->visited) { return; + } + /* for each known zone, see if it matches the current pixel's color */ for (i = 0; i < darraySize(zones); ++i) { current_zone = darrayGet(zones, i); + /* if it does, add selected pixel and its neighbourging pixels of the same + * color */ if (sameColor(pixel, current_zone)) { addPixelToSelectedZone(t_img, t_idx, current_zone); - break; + return; } } + /* if none of the same color was found, create a new one, add it to the image + * and add the selected pixel and its neighbours of the same color to the zone + */ + current_zone = newZone(pixel->r, pixel->g, pixel->b); + darrayPushBack(zones, current_zone); + addPixelToSelectedZone(t_img, t_idx, current_zone); } /* converts an image to zones */ @@ -86,6 +98,7 @@ darray* imgToZones(Image* t_img) { size_t i; zones = darrayNew(sizeof(Zone)); + /* for each pixel, try to create a new zone */ for (i = 0; i < nb_pixels; ++i) { chooseZoneForPixel(t_img, i, zones); } @@ -98,9 +111,19 @@ void compress(const char *input_file) { img = newImage(); imageLoadPPM(input_file, img); zones = imgToZones(img); - darrayDelete(zones); printf("Detected %zu zones\n", darraySize(zones)); + for(size_t i = 0; i < darraySize(zones); ++i) { + Zone* zone = darrayGet(zones, i); + printf("\n=== Zone %zu (%d %d %d) ===\n", i, zone->r, zone->g, zone->b); + for(size_t j = 0; j < darraySize(zone->segments); ++j) { + Segment* segm = darrayGet(zone->segments, j); + printf("[%d: %d, %d]\t", segm->y, segm->xg, segm->xd); + } + } + printf("\n"); + deleteImage(img); + printf("test\n"); } diff --git a/src/utilities.c b/src/utilities.c index 895f28d..bb49a12 100644 --- a/src/utilities.c +++ b/src/utilities.c @@ -63,10 +63,7 @@ Image *newImage() { * \param[in] self Conteneur d’image à détruire */ void deleteImage(Image *t_self) { - unsigned long i; - for (i = 0; i < darraySize(t_self->pixels); ++i) { - deletePixel(darrayGet(t_self->pixels, i)); - } + printf("deleted all pixels\n"); darrayDelete(t_self->pixels); free(t_self); }