/ tests / src / imgs / img.cpp
img.cpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   img.cpp                                            :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: lfiorell <lfiorell@student.42.fr>          +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2025/02/13 14:06:04 by lfiorell          #+#    #+#             */
  9  /*   Updated: 2025/02/13 22:26:47 by lfiorell         ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #include "CUnit/Basic.h"
 14  
 15  extern "C"
 16  {
 17  #include "img/img.h"
 18  #include "mlx.h"
 19  }
 20  
 21  void test_crust_img_new(void)
 22  {
 23    void *mlx = mlx_init();
 24    t_img *img = crust_img_new(mlx, 1024, 768);
 25  
 26    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
 27    CU_ASSERT_PTR_NOT_NULL(img->mlx_ptr);
 28    CU_ASSERT_PTR_NOT_NULL(img->img_ptr);
 29  
 30    CU_ASSERT_EQUAL(img->width, 1024);
 31    CU_ASSERT_EQUAL(img->height, 768);
 32  
 33    crust_img_drop(img);
 34  }
 35  
 36  void test_crust_img_from_xpm(void)
 37  {
 38    char *path = (char *)"data/image.xpm";
 39    void *owo = mlx_init();
 40  
 41    t_img *img = crust_img_from_xpm(owo, path);
 42  
 43    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
 44    CU_ASSERT_PTR_NOT_NULL(img->mlx_ptr);
 45    CU_ASSERT_PTR_NOT_NULL(img->img_ptr);
 46  
 47    CU_ASSERT_EQUAL(img->width, 1024);
 48    CU_ASSERT_EQUAL(img->height, 768);
 49  
 50    crust_img_drop(img);
 51  }
 52  
 53  void test_img_put_pixel(void)
 54  {
 55    void *mlx = mlx_init();
 56    t_img *img = crust_img_new(mlx, 3, 1);
 57    t_rgba color;
 58    color.r = 255;
 59    color.g = 0;
 60    color.b = 0;
 61    color.a = 0;
 62    t_2d pos = {1, 0};
 63  
 64    crust_img_put_pixel(img, pos, color);
 65  
 66    t_rgba pixel = crust_img_get_pixel(img, pos);
 67  
 68    CU_ASSERT_EQUAL(pixel.r, color.r);
 69    CU_ASSERT_EQUAL(pixel.g, color.g);
 70    CU_ASSERT_EQUAL(pixel.b, color.b);
 71  
 72    crust_img_drop(img);
 73  }
 74  
 75  void test_img_get_pixel(void)
 76  {
 77    char *path = (char *)"data/image.xpm";
 78    void *owo = mlx_init();
 79  
 80    t_img *img = crust_img_from_xpm(owo, path);
 81  
 82    t_rgba expected_color;
 83    expected_color.r = 0x2B;
 84    expected_color.g = 0x4E;
 85    expected_color.b = 0x7E;
 86  
 87    for (int x = 0; x < 20; ++x)
 88    {
 89      t_2d pos = {x, 1};
 90      t_rgba pixel = crust_img_get_pixel(img, pos);
 91  
 92      CU_ASSERT_EQUAL(pixel.r, expected_color.r);
 93      CU_ASSERT_EQUAL(pixel.g, expected_color.g);
 94      CU_ASSERT_EQUAL(pixel.b, expected_color.b);
 95    }
 96  
 97    crust_img_drop(img);
 98  }
 99  
100  void test_img_cpy_full(void)
101  {
102    char *path = (char *)"data/image.xpm";
103    void *owo = mlx_init();
104  
105    t_img *img = crust_img_from_xpm(owo, path);
106    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
107  
108    t_img *dst = crust_img_new(owo, img->width, img->height);
109    CU_ASSERT_PTR_NOT_NULL_FATAL(dst);
110  
111    t_2d pos = {0, 0};
112    t_2d size = {img->width, img->height};
113    crust_img_cpy(dst, img, pos, size);
114  
115    for (int y = 0; y < img->height; ++y)
116    {
117      for (int x = 0; x < img->width; ++x)
118      {
119        t_2d pos = {x, y};
120        t_rgba src_pixel = crust_img_get_pixel(img, pos);
121        t_rgba dst_pixel = crust_img_get_pixel(dst, pos);
122  
123        CU_ASSERT_EQUAL(src_pixel.r, dst_pixel.r);
124        CU_ASSERT_EQUAL(src_pixel.g, dst_pixel.g);
125        CU_ASSERT_EQUAL(src_pixel.b, dst_pixel.b);
126      }
127    }
128  
129    crust_img_drop(img);
130    crust_img_drop(dst);
131  }
132  
133  void test_img_cpy_partial(void)
134  {
135    char *path = (char *)"data/image.xpm";
136    void *owo = mlx_init();
137  
138    t_img *img = crust_img_from_xpm(owo, path);
139    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
140  
141    t_img *dst = crust_img_new(owo, img->width / 2, img->height / 2);
142    CU_ASSERT_PTR_NOT_NULL_FATAL(dst);
143  
144    t_2d pos = {0, 0};
145    t_2d size = {img->width / 2, img->height / 2};
146    crust_img_cpy(dst, img, pos, size);
147  
148    for (int y = 0; y < dst->height; ++y)
149    {
150      for (int x = 0; x < dst->width; ++x)
151      {
152        t_2d pos = {x, y};
153        t_rgba src_pixel = crust_img_get_pixel(img, pos);
154        t_rgba dst_pixel = crust_img_get_pixel(dst, pos);
155  
156        CU_ASSERT_EQUAL(src_pixel.r, dst_pixel.r);
157        CU_ASSERT_EQUAL(src_pixel.g, dst_pixel.g);
158        CU_ASSERT_EQUAL(src_pixel.b, dst_pixel.b);
159      }
160    }
161  
162    crust_img_drop(img);
163    crust_img_drop(dst);
164  }
165  
166  void test_img_crop(void)
167  {
168    char *path = (char *)"data/image.xpm";
169    void *owo = mlx_init();
170  
171    t_img *img = crust_img_from_xpm(owo, path);
172    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
173  
174    t_2d pos = {0, 0};
175    t_2d size = {img->width / 2, img->height / 2};
176    t_img *dst = crust_img_crop(img, pos, size);
177    CU_ASSERT_PTR_NOT_NULL_FATAL(dst);
178  
179    for (int y = 0; y < dst->height; ++y)
180    {
181      for (int x = 0; x < dst->width; ++x)
182      {
183        t_2d pos = {x, y};
184        t_rgba src_pixel = crust_img_get_pixel(img, pos);
185        t_rgba dst_pixel = crust_img_get_pixel(dst, pos);
186  
187        CU_ASSERT_EQUAL(src_pixel.r, dst_pixel.r);
188        CU_ASSERT_EQUAL(src_pixel.g, dst_pixel.g);
189        CU_ASSERT_EQUAL(src_pixel.b, dst_pixel.b);
190      }
191    }
192  
193    crust_img_drop(img);
194    crust_img_drop(dst);
195  }
196  
197  void test_img_crop_off(void)
198  {
199    char *path = (char *)"data/image.xpm";
200    void *owo = mlx_init();
201  
202    t_img *img = crust_img_from_xpm(owo, path);
203    CU_ASSERT_PTR_NOT_NULL_FATAL(img);
204  
205    t_2d pos = {1, 0};    // offset position in source image
206    t_2d size = {10, 10}; // size of cropped area
207    t_img *dst = crust_img_crop(img, pos, size);
208    CU_ASSERT_PTR_NOT_NULL_FATAL(dst);
209  
210    for (int y = 0; y < dst->height; ++y)
211    {
212      for (int x = 0; x < dst->width; ++x)
213      {
214        t_2d src_pos = {x + pos.x, y + pos.y};
215        t_rgba src_pixel = crust_img_get_pixel(img, src_pos);
216        t_2d dst_pos = {x, y};
217        t_rgba dst_pixel = crust_img_get_pixel(dst, dst_pos);
218  
219        if (src_pixel.r != dst_pixel.r || src_pixel.g != dst_pixel.g || src_pixel.b != dst_pixel.b)
220        {
221          printf("Mismatch: expected (%d, %d, %d), got (%d, %d, %d) at pos (%d, %d) from (%d, %d)\n",
222                 src_pixel.r, src_pixel.g, src_pixel.b,
223                 dst_pixel.r, dst_pixel.g, dst_pixel.b,
224                 dst_pos.x, dst_pos.y,
225                 src_pos.x, src_pos.y);
226        }
227  
228        CU_ASSERT_EQUAL(src_pixel.r, dst_pixel.r);
229        CU_ASSERT_EQUAL(src_pixel.g, dst_pixel.g);
230        CU_ASSERT_EQUAL(src_pixel.b, dst_pixel.b);
231      }
232    }
233  
234    crust_img_drop(img);
235    crust_img_drop(dst);
236  }
237  
238  void run_img_tests(void)
239  {
240    CU_pSuite pSuite = CU_add_suite("Imgs", NULL, NULL);
241  
242    CU_add_test(pSuite, "crust_img_new", test_crust_img_new);
243    CU_add_test(pSuite, "crust_img_from_xpm", test_crust_img_from_xpm);
244    CU_add_test(pSuite, "crust_img_put_pixel", test_img_put_pixel);
245    CU_add_test(pSuite, "crust_img_get_pixel", test_img_get_pixel);
246    CU_add_test(pSuite, "crust_img_cpy_full", test_img_cpy_full);
247    CU_add_test(pSuite, "crust_img_cpy_partial", test_img_cpy_partial);
248    CU_add_test(pSuite, "crust_img_crop", test_img_crop);
249    CU_add_test(pSuite, "crust_img_crop_off", test_img_crop_off);
250  }