2 * Image Library -- Simple automatic tests
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include "lib/mempool.h"
14 #include "lib/fastbuf.h"
15 #include "images/images.h"
16 #include "images/color.h"
24 static uns want_image_iface;
25 static uns want_threads;
27 #define TRY(x) do { if (!(x)) ASSERT(0); } while (0)
30 test_image_iface(void)
33 struct image_context ctx;
34 struct image *i1, *i2;
38 image_context_init(&ctx);
40 /* Image allocation */
41 i1 = image_new(&ctx, 731, 327, COLOR_SPACE_RGB, NULL);
43 ASSERT(i1->pixel_size == 3);
46 /* Test invalid image size */
47 ctx.msg_callback = image_context_msg_silent;
48 i1 = image_new(&ctx, 2214, 0, COLOR_SPACE_RGB, NULL);
50 i1 = image_new(&ctx, 0xffffff, 0xffffff, COLOR_SPACE_RGB, NULL);
52 ctx.msg_callback = image_context_msg_default;
54 /* Various image allocatio parameters */
55 i1 = image_new(&ctx, 370, 100, COLOR_SPACE_GRAYSCALE, pool);
57 ASSERT(i1->pixel_size == 1);
61 i1 = image_new(&ctx, 373, 101, COLOR_SPACE_RGB | IMAGE_ALIGNED, NULL);
63 ASSERT(i1->pixel_size == 4);
64 ASSERT(IMAGE_SSE_ALIGN_SIZE >= 16);
65 ASSERT(!(i1->row_size & (IMAGE_SSE_ALIGN_SIZE - 1)));
66 ASSERT(!((addr_int_t)i1->pixels & (IMAGE_SSE_ALIGN_SIZE - 1)));
69 i1 = image_new(&ctx, 283, 329, COLOR_SPACE_RGB, NULL);
71 ASSERT(i1->pixel_size == 3);
73 /* Image structures cloning */
74 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB, NULL);
76 ASSERT(i2->pixel_size == 3);
79 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED, NULL);
81 ASSERT(i2->pixel_size == 4);
85 i2 = image_init_subimage(&ctx, &s1, i1, 29, 39, 283 - 29, 100);
91 image_context_cleanup(&ctx);
95 #define TEST_THREADS_COUNT 4
98 test_threads_thread(void *param UNUSED)
100 DBG("Starting thread");
101 struct image_context ctx;
103 image_context_init(&ctx);
104 TRY(image_io_init(&ctx, &io));
106 for (uns num = 0; num < 200; num++)
108 int r0 = random_max(100);
110 /* realloc context */
113 image_io_cleanup(&io);
114 image_context_cleanup(&ctx);
115 image_context_init(&ctx);
116 TRY(image_io_init(&ctx, &io));
120 else if ((r0 -= 2) < 0)
122 image_io_cleanup(&io);
123 TRY(image_io_init(&ctx, &io));
126 /* encode and decode random image */
131 TRY(img = image_new(&ctx, 10 + random_max(140), 10 + random_max(140), COLOR_SPACE_RGB, NULL));
132 image_clear(&ctx, img);
134 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
136 struct fastbuf *wfb = fbmem_create(10000);
141 switch (random_max(3))
144 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBMAGICK)
145 format = IMAGE_FORMAT_JPEG;
149 #if defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
150 format = IMAGE_FORMAT_PNG;
154 #if defined(CONFIG_IMAGES_LIBMAGICK)
155 format = IMAGE_FORMAT_GIF;
166 TRY(image_io_write(&io));
169 rfb = fbmem_clone_read(wfb);
172 TRY(image_io_read(&io, 0));
183 image_io_cleanup(&io);
184 image_context_cleanup(&ctx);
185 DBG("Stopping thread");
192 pthread_t threads[TEST_THREADS_COUNT - 1];
194 if (pthread_attr_init(&attr) < 0 ||
195 pthread_attr_setstacksize(&attr, 1 << 20) < 0)
197 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
199 if (pthread_create(threads + i, &attr, test_threads_thread, NULL) < 0)
200 die("Unable to create thread: %m");
202 test_threads_thread(NULL);
203 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
204 if (pthread_join(threads[i], NULL) < 0)
205 die("Cannot join thread: %m");
209 main(int argc, char **argv)
211 for (int i = 1; i < argc; i++)
212 if (!strcmp(argv[i], "image-iface"))
214 else if (!strcmp(argv[i], "threads"))
217 die("Invalid parameter");
219 srandom(time(NULL) ^ getpid());
221 if (want_image_iface)