From d9604e398bacb02e6000825243b875e5cfc007e4 Mon Sep 17 00:00:00 2001 From: kartofen Date: Wed, 10 Aug 2022 19:04:15 +0300 Subject: works with colors --- src/ppm.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) (limited to 'src/ppm.c') diff --git a/src/ppm.c b/src/ppm.c index 24216f5..7bc8bb7 100644 --- a/src/ppm.c +++ b/src/ppm.c @@ -21,12 +21,10 @@ void save_as_ppm(char* file_path, small_t *t, size_t width, size_t height, size_ for(size_t i = 0; i < height * scaler; i++) for(size_t j = 0; j < width * scaler; j++) - { - char c = (t[(i/scaler) * width + (j/scaler)] == 0) ? 255 : 0; - for(int j = 0; j < 3; j++) + for(int k = 0; k < 3; k++) { + char c = t[((i/scaler) * width + (j/scaler))*3 + k]; fwrite(&c, 1, 1, fp); - } - + } fclose(fp); } @@ -52,16 +50,9 @@ small_t *load_from_ppm(char *file_path, size_t *width, size_t *height) *height = atoi(strtok(NULL, " ")); (void)strtok(NULL, " "); - char *pixels = malloc((*width) * (*height ) * 3); - - fread(pixels, *width*3, *height, fp); - - small_t *t = malloc((*width) * (*height) * sizeof(small_t)); - for(size_t i = 0; i < *height; i++) - for(size_t j = 0; j < *width; j++) - t[i * *width + j] = (pixels[(i * *width + j) * 3] == 0) ? 1 : 0; + small_t *t = malloc((*width) * (*height ) * 3); - free(pixels); + fread(t, *width*3, *height, fp); fclose(fp); -- cgit v1.2.3