4 #include "lib/mempool.h"
5 #include "lib/fastbuf.h"
6 #include "images/images.h"
7 #include "images/color.h"
15 static uns want_image_iface;
16 static uns want_threads;
18 #define TRY(x) do { if (!(x)) ASSERT(0); } while (0)
21 test_image_iface(void)
24 struct image_context ctx;
25 struct image *i1, *i2;
29 image_context_init(&ctx);
31 /* Image allocation */
32 i1 = image_new(&ctx, 731, 327, COLOR_SPACE_RGB, NULL);
34 ASSERT(i1->pixel_size == 3);
37 /* Test invalid image size */
38 ctx.msg_callback = image_context_msg_silent;
39 i1 = image_new(&ctx, 2214, 0, COLOR_SPACE_RGB, NULL);
41 i1 = image_new(&ctx, 0xffffff, 0xffffff, COLOR_SPACE_RGB, NULL);
43 ctx.msg_callback = image_context_msg_default;
45 /* Various image allocatio parameters */
46 i1 = image_new(&ctx, 370, 100, COLOR_SPACE_GRAYSCALE, pool);
48 ASSERT(i1->pixel_size == 1);
52 i1 = image_new(&ctx, 373, 101, COLOR_SPACE_RGB | IMAGE_ALIGNED, NULL);
54 ASSERT(i1->pixel_size == 4);
55 ASSERT(IMAGE_SSE_ALIGN_SIZE >= 16);
56 ASSERT(!(i1->row_size & (IMAGE_SSE_ALIGN_SIZE - 1)));
57 ASSERT(!((addr_int_t)i1->pixels & (IMAGE_SSE_ALIGN_SIZE - 1)));
60 i1 = image_new(&ctx, 283, 329, COLOR_SPACE_RGB, NULL);
62 ASSERT(i1->pixel_size == 3);
64 /* Image structures cloning */
65 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB, NULL);
67 ASSERT(i2->pixel_size == 3);
70 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED, NULL);
72 ASSERT(i2->pixel_size == 4);
76 i2 = image_init_subimage(&ctx, &s1, i1, 29, 39, 283 - 29, 100);
82 image_context_cleanup(&ctx);
86 #define TEST_THREADS_COUNT 4
89 test_threads_thread(void *param UNUSED)
91 DBG("Starting thread");
92 struct image_context ctx;
94 image_context_init(&ctx);
95 TRY(image_io_init(&ctx, &io));
97 for (uns num = 0; num < 200; num++)
99 int r0 = random_max(100);
101 /* realloc context */
104 image_io_cleanup(&io);
105 image_context_cleanup(&ctx);
106 image_context_init(&ctx);
107 TRY(image_io_init(&ctx, &io));
111 else if ((r0 -= 2) < 0)
113 image_io_cleanup(&io);
114 TRY(image_io_init(&ctx, &io));
117 /* encode and decode random image */
122 TRY(img = image_new(&ctx, 10 + random_max(140), 10 + random_max(140), COLOR_SPACE_RGB, NULL));
123 image_clear(&ctx, img);
125 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
127 struct fastbuf *wfb = fbmem_create(10000);
132 switch (random_max(3))
135 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBMAGICK)
136 format = IMAGE_FORMAT_JPEG;
140 #if defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
141 format = IMAGE_FORMAT_PNG;
145 #if defined(CONFIG_IMAGES_LIBMAGICK)
146 format = IMAGE_FORMAT_GIF;
157 TRY(image_io_write(&io));
160 rfb = fbmem_clone_read(wfb);
163 TRY(image_io_read(&io, 0));
174 image_io_cleanup(&io);
175 image_context_cleanup(&ctx);
176 DBG("Stopping thread");
183 pthread_t threads[TEST_THREADS_COUNT - 1];
185 if (pthread_attr_init(&attr) < 0 ||
186 pthread_attr_setstacksize(&attr, 1 << 20) < 0)
188 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
190 if (pthread_create(threads + i, &attr, test_threads_thread, NULL) < 0)
191 die("Unable to create thread: %m");
193 test_threads_thread(NULL);
194 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
195 if (pthread_join(threads[i], NULL) < 0)
196 die("Cannot join thread: %m");
200 main(int argc, char **argv)
202 for (int i = 1; i < argc; i++)
203 if (!strcmp(argv[i], "image-iface"))
205 else if (!strcmp(argv[i], "threads"))
208 die("Invalid parameter");
210 srandom(time(NULL) ^ getpid());
212 if (want_image_iface)