test_quickpreview.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include "quickpreview.h"
  2. #include <assert.h>
  3. #include <glib.h>
  4. #include <stdio.h>
  5. static void
  6. test_quick_preview_fuzz()
  7. {
  8. for (size_t i = 0; i < 10000; ++i) {
  9. uint32_t width = rand() % 127 + 1;
  10. uint32_t height = rand() % 127 + 1;
  11. uint32_t format = rand() % (MP_PIXEL_FMT_MAX - 1) + 1;
  12. assert(width > 0 && height > 0);
  13. switch (format) {
  14. case MP_PIXEL_FMT_BGGR8:
  15. case MP_PIXEL_FMT_GBRG8:
  16. case MP_PIXEL_FMT_GRBG8:
  17. case MP_PIXEL_FMT_RGGB8:
  18. width += width % 2;
  19. height += height % 2;
  20. break;
  21. case MP_PIXEL_FMT_BGGR10P:
  22. case MP_PIXEL_FMT_GBRG10P:
  23. case MP_PIXEL_FMT_GRBG10P:
  24. case MP_PIXEL_FMT_RGGB10P:
  25. width += width % 2;
  26. height += height % 2;
  27. break;
  28. case MP_PIXEL_FMT_UYVY:
  29. case MP_PIXEL_FMT_YUYV:
  30. width += width % 4;
  31. break;
  32. default:
  33. assert(false);
  34. }
  35. int rotation;
  36. switch (rand() % 3) {
  37. case 0:
  38. rotation = 0;
  39. break;
  40. case 1:
  41. rotation = 90;
  42. break;
  43. case 2:
  44. rotation = 180;
  45. break;
  46. default:
  47. rotation = 270;
  48. break;
  49. }
  50. bool mirrored = rand() % 2;
  51. float matbuf[9];
  52. float *colormatrix = NULL;
  53. if (rand() % 2) {
  54. for (int j = 0; j < 9; ++j) {
  55. matbuf[j] = (double)rand() / RAND_MAX * 2.0;
  56. }
  57. colormatrix = matbuf;
  58. }
  59. uint8_t blacklevel = rand() % 3;
  60. size_t src_size = mp_pixel_format_width_to_bytes(format, width) * height;
  61. uint8_t *src = malloc(src_size);
  62. for (int j = 0; j < src_size; ++j) {
  63. src[j] = rand();
  64. }
  65. uint32_t preview_width = mp_pixel_format_width_to_colors(format, width);
  66. uint32_t preview_height = mp_pixel_format_height_to_colors(format, height);
  67. if (preview_width > 32 && preview_height > 32) {
  68. preview_width /= (1 + rand() % 2);
  69. preview_height /= (1 + rand() % 2);
  70. }
  71. uint32_t dst_width, dst_height, skip;
  72. quick_preview_size(
  73. &dst_width,
  74. &dst_height,
  75. &skip,
  76. preview_width,
  77. preview_height,
  78. width,
  79. height,
  80. format,
  81. rotation);
  82. uint32_t *dst = malloc(dst_width * dst_height * sizeof(uint32_t));
  83. quick_preview(
  84. dst,
  85. dst_width,
  86. dst_height,
  87. src,
  88. width,
  89. height,
  90. format,
  91. rotation,
  92. mirrored,
  93. colormatrix,
  94. blacklevel,
  95. skip);
  96. free(dst);
  97. free(src);
  98. }
  99. }
  100. int
  101. main(int argc, char *argv[])
  102. {
  103. g_test_init(&argc, &argv, NULL);
  104. g_test_add_func("/quick_preview/fuzz", test_quick_preview_fuzz);
  105. return g_test_run();
  106. }