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 "lib/threads.h"
16 #include "images/images.h"
17 #include "images/color.h"
25 static uns want_image_iface;
26 static uns want_threads;
28 #define TRY(x) do { if (!(x)) ASSERT(0); } while (0)
31 test_image_iface(void)
34 struct image_context ctx;
35 struct image *i1, *i2;
39 image_context_init(&ctx);
41 /* Image allocation */
42 i1 = image_new(&ctx, 731, 327, COLOR_SPACE_RGB, NULL);
44 ASSERT(i1->pixel_size == 3);
47 /* Test invalid image size */
48 ctx.msg_callback = image_context_msg_silent;
49 i1 = image_new(&ctx, 2214, 0, COLOR_SPACE_RGB, NULL);
51 i1 = image_new(&ctx, 0xffffff, 0xffffff, COLOR_SPACE_RGB, NULL);
53 ctx.msg_callback = image_context_msg_default;
55 /* Various image allocatio parameters */
56 i1 = image_new(&ctx, 370, 100, COLOR_SPACE_GRAYSCALE, pool);
58 ASSERT(i1->pixel_size == 1);
62 i1 = image_new(&ctx, 373, 101, COLOR_SPACE_RGB | IMAGE_ALIGNED, NULL);
64 ASSERT(i1->pixel_size == 4);
65 ASSERT(IMAGE_SSE_ALIGN_SIZE >= 16);
66 ASSERT(!(i1->row_size & (IMAGE_SSE_ALIGN_SIZE - 1)));
67 ASSERT(!((addr_int_t)i1->pixels & (IMAGE_SSE_ALIGN_SIZE - 1)));
70 i1 = image_new(&ctx, 283, 329, COLOR_SPACE_RGB, NULL);
72 ASSERT(i1->pixel_size == 3);
74 /* Image structures cloning */
75 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB, NULL);
77 ASSERT(i2->pixel_size == 3);
80 i2 = image_clone(&ctx, i1, COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED, NULL);
82 ASSERT(i2->pixel_size == 4);
86 i2 = image_init_subimage(&ctx, &s1, i1, 29, 39, 283 - 29, 100);
92 image_context_cleanup(&ctx);
96 #define TEST_THREADS_COUNT 4
99 test_threads_thread(void *param UNUSED)
101 DBG("Starting thread");
102 struct image_context ctx;
104 image_context_init(&ctx);
105 TRY(image_io_init(&ctx, &io));
107 for (uns num = 0; num < 200; num++)
109 int r0 = random_max(100);
111 /* realloc context */
114 image_io_cleanup(&io);
115 image_context_cleanup(&ctx);
116 image_context_init(&ctx);
117 TRY(image_io_init(&ctx, &io));
121 else if ((r0 -= 2) < 0)
123 image_io_cleanup(&io);
124 TRY(image_io_init(&ctx, &io));
127 /* encode and decode random image */
132 TRY(img = image_new(&ctx, 10 + random_max(140), 10 + random_max(140), COLOR_SPACE_RGB, NULL));
133 image_clear(&ctx, img);
135 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
137 struct fastbuf *wfb = fbmem_create(10000);
142 switch (random_max(3))
145 #if defined(CONFIG_IMAGES_LIBJPEG) || defined(CONFIG_IMAGES_LIBMAGICK)
146 format = IMAGE_FORMAT_JPEG;
150 #if defined(CONFIG_IMAGES_LIBPNG) || defined(CONFIG_IMAGES_LIBMAGICK)
151 format = IMAGE_FORMAT_PNG;
155 #if defined(CONFIG_IMAGES_LIBMAGICK)
156 format = IMAGE_FORMAT_GIF;
167 TRY(image_io_write(&io));
170 rfb = fbmem_clone_read(wfb);
173 TRY(image_io_read(&io, 0));
184 image_io_cleanup(&io);
185 image_context_cleanup(&ctx);
186 DBG("Stopping thread");
193 pthread_t threads[TEST_THREADS_COUNT - 1];
195 if (pthread_attr_init(&attr) < 0 ||
196 pthread_attr_setstacksize(&attr, default_thread_stack_size) < 0)
198 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
200 if (pthread_create(threads + i, &attr, test_threads_thread, NULL) < 0)
201 die("Unable to create thread: %m");
203 test_threads_thread(NULL);
204 for (uns i = 0; i < TEST_THREADS_COUNT - 1; i++)
205 if (pthread_join(threads[i], NULL) < 0)
206 die("Cannot join thread: %m");
210 main(int argc, char **argv)
212 for (int i = 1; i < argc; i++)
213 if (!strcmp(argv[i], "image-iface"))
215 else if (!strcmp(argv[i], "threads"))
218 die("Invalid parameter");
220 srandom(time(NULL) ^ getpid());
222 if (want_image_iface)