From ff65a8d6a5787441bdb4b435a7066310a86746f2 Mon Sep 17 00:00:00 2001
From: "damian.boquetec" <damian.boquete-costa@etu.hesge.ch>
Date: Tue, 24 Nov 2020 21:40:00 +0100
Subject: [PATCH] crop function done but buggy

---
 pgm.c      | 28 +++++++++++-----------------
 pgm_main.c |  2 +-
 2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/pgm.c b/pgm.c
index 5aaccf2..020e5d2 100644
--- a/pgm.c
+++ b/pgm.c
@@ -133,30 +133,24 @@ pgm_error pgm_photomaton(pgm *photomaton, const pgm *const orig){
     }
     return success;
 }
-//index error
+
 pgm_error pgm_crop(pgm *crop, const pgm *const orig, int32_t x0, int32_t x1, int32_t y0, int32_t y1){
     if (crop == NULL || orig == NULL){
         return failure;
     }
+    if (x0 < 0 || y0 < 0 || x0 > x1 || y0 > y1 || x1 > orig->pixels.l || y1 > orig->pixels.l){
+        return failure;
+    }
+
+
     int l = x1 - x0;
     int c = y1 - y0;
-    int max = l + c/ 2;
-    printf("%d", max);
-    crop->max = max;
+    crop->max = 255;
     matrix_alloc(&crop->pixels, l, c);
-    printf("step");
-    int k = 0;
-    int m = 0;
-    for (int i = 0; i < orig->pixels.l; i++){
-        for (int j = 0; j < orig->pixels.c; j++){  
-            if (i >= x0 && i < x1 && j >= y0 && j < y1){
-                crop->pixels.data[k][m] = orig->pixels.data[i][j];
-                m++;
-                printf("%d %d", k, m);
-            }
-            if (i >= x0 && i < x1){
-               k++;
-            }
+
+    for (int i = x0, k = 0; i < x1; i++, k++){
+        for (int j = y0, m = 0; j < y1; j++, m++){  
+            crop->pixels.data[k][m] = orig->pixels.data[i][j];
         }
     }
     return success;
diff --git a/pgm_main.c b/pgm_main.c
index b76fdb7..2a4e000 100644
--- a/pgm_main.c
+++ b/pgm_main.c
@@ -30,7 +30,7 @@ int main(){
     printf("%d", pgm_photomaton(&photomatonx16, &photomatonx4));
     //printf("%d", pgm_write_to_file(&photomatonx16, "photomatonx16.pgm"));
 
-    printf("%d", pgm_crop(&cropped, &a, 7, 14, 7, 14));
+    printf("%d", pgm_crop(&cropped, &a, 100, 300, 100, 200));
     printf("%d", pgm_write_to_file(&cropped, "cropped.pgm"));
 
     matrix_destroy(&a.pixels);
-- 
GitLab