You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_mat_pixel.cpp 6.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "mat.h"
  15. #include "prng.h"
  16. #include <string.h>
  17. static struct prng_rand_t g_prng_rand_state;
  18. #define SRAND(seed) prng_srand(seed, &g_prng_rand_state)
  19. #define RAND() prng_rand(&g_prng_rand_state)
  20. static ncnn::Mat RandomMat(int w, int h, int elempack)
  21. {
  22. ncnn::Mat m(w, h, (size_t)elempack, elempack);
  23. unsigned char* p = m;
  24. for (int i = 0; i < w * h * elempack; i++)
  25. {
  26. p[i] = RAND() % 256;
  27. }
  28. return m;
  29. }
  30. static int test_mat_pixel_gray(int w, int h)
  31. {
  32. int pixel_type_from[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_GRAY2RGB, ncnn::Mat::PIXEL_GRAY2BGR, ncnn::Mat::PIXEL_GRAY2RGBA, ncnn::Mat::PIXEL_GRAY2BGRA};
  33. int pixel_type_to[5] = {ncnn::Mat::PIXEL_GRAY, ncnn::Mat::PIXEL_RGB2GRAY, ncnn::Mat::PIXEL_BGR2GRAY, ncnn::Mat::PIXEL_RGBA2GRAY, ncnn::Mat::PIXEL_BGRA2GRAY};
  34. ncnn::Mat a = RandomMat(w, h, 1);
  35. // FIXME enable more convert types
  36. for (int i = 0; i < 1; i++)
  37. {
  38. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  39. ncnn::Mat b(w, h, 1u, 1);
  40. m.to_pixels(b, pixel_type_to[i]);
  41. if (memcmp(a, b, w * h * 1) != 0)
  42. {
  43. fprintf(stderr, "test_mat_pixel_gray failed w=%d h=%d pixel_type=%d\n", w, h, i);
  44. return -1;
  45. }
  46. }
  47. return 0;
  48. }
  49. static int test_mat_pixel_rgb(int w, int h)
  50. {
  51. int pixel_type_from[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGB2RGBA, ncnn::Mat::PIXEL_RGB2BGRA};
  52. int pixel_type_to[4] = {ncnn::Mat::PIXEL_RGB, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_RGBA2RGB, ncnn::Mat::PIXEL_BGRA2RGB};
  53. ncnn::Mat a = RandomMat(w, h, 3);
  54. // FIXME enable more convert types
  55. for (int i = 0; i < 2; i++)
  56. {
  57. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  58. ncnn::Mat b(w, h, 3u, 3);
  59. m.to_pixels(b, pixel_type_to[i]);
  60. if (memcmp(a, b, w * h * 3) != 0)
  61. {
  62. fprintf(stderr, "test_mat_pixel_rgb failed w=%d h=%d pixel_type=%d\n", w, h, i);
  63. return -1;
  64. }
  65. }
  66. return 0;
  67. }
  68. static int test_mat_pixel_bgr(int w, int h)
  69. {
  70. int pixel_type_from[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_BGR2RGB, ncnn::Mat::PIXEL_BGR2RGBA, ncnn::Mat::PIXEL_BGR2BGRA};
  71. int pixel_type_to[4] = {ncnn::Mat::PIXEL_BGR, ncnn::Mat::PIXEL_RGB2BGR, ncnn::Mat::PIXEL_RGBA2BGR, ncnn::Mat::PIXEL_BGRA2BGR};
  72. ncnn::Mat a = RandomMat(w, h, 3);
  73. // FIXME enable more convert types
  74. for (int i = 0; i < 2; i++)
  75. {
  76. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  77. ncnn::Mat b(w, h, 3u, 3);
  78. m.to_pixels(b, pixel_type_to[i]);
  79. if (memcmp(a, b, w * h * 3) != 0)
  80. {
  81. fprintf(stderr, "test_mat_pixel_bgr failed w=%d h=%d pixel_type=%d\n", w, h, i);
  82. return -1;
  83. }
  84. }
  85. return 0;
  86. }
  87. static int test_mat_pixel_rgba(int w, int h)
  88. {
  89. int pixel_type_from[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_RGBA2BGRA};
  90. int pixel_type_to[2] = {ncnn::Mat::PIXEL_RGBA, ncnn::Mat::PIXEL_BGRA2RGBA};
  91. ncnn::Mat a = RandomMat(w, h, 4);
  92. for (int i = 0; i < 2; i++)
  93. {
  94. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  95. ncnn::Mat b(w, h, 4u, 4);
  96. m.to_pixels(b, pixel_type_to[i]);
  97. if (memcmp(a, b, w * h * 4) != 0)
  98. {
  99. fprintf(stderr, "test_mat_pixel_rgba failed w=%d h=%d pixel_type=%d\n", w, h, i);
  100. return -1;
  101. }
  102. }
  103. return 0;
  104. }
  105. static int test_mat_pixel_bgra(int w, int h)
  106. {
  107. int pixel_type_from[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_BGRA2RGBA};
  108. int pixel_type_to[2] = {ncnn::Mat::PIXEL_BGRA, ncnn::Mat::PIXEL_RGBA2BGRA};
  109. ncnn::Mat a = RandomMat(w, h, 4);
  110. for (int i = 0; i < 2; i++)
  111. {
  112. ncnn::Mat m = ncnn::Mat::from_pixels(a, pixel_type_from[i], w, h);
  113. ncnn::Mat b(w, h, 4u, 4);
  114. m.to_pixels(b, pixel_type_to[i]);
  115. if (memcmp(a, b, w * h * 4) != 0)
  116. {
  117. fprintf(stderr, "test_mat_pixel_bgra failed w=%d h=%d pixel_type=%d\n", w, h, i);
  118. return -1;
  119. }
  120. }
  121. return 0;
  122. }
  123. static int test_mat_pixel_0()
  124. {
  125. return 0
  126. || test_mat_pixel_gray(16, 16)
  127. || test_mat_pixel_rgb(16, 16)
  128. || test_mat_pixel_bgr(16, 16)
  129. || test_mat_pixel_rgba(16, 16)
  130. || test_mat_pixel_bgra(16, 16);
  131. }
  132. static int test_mat_pixel_1()
  133. {
  134. return 0
  135. || test_mat_pixel_gray(15, 15)
  136. || test_mat_pixel_rgb(15, 15)
  137. || test_mat_pixel_bgr(15, 15)
  138. || test_mat_pixel_rgba(15, 15)
  139. || test_mat_pixel_bgra(15, 15);
  140. }
  141. static int test_mat_pixel_2()
  142. {
  143. return 0
  144. || test_mat_pixel_gray(1, 1)
  145. || test_mat_pixel_rgb(1, 1)
  146. || test_mat_pixel_bgr(1, 1)
  147. || test_mat_pixel_rgba(1, 1)
  148. || test_mat_pixel_bgra(1, 1);
  149. }
  150. static int test_mat_pixel_3()
  151. {
  152. return 0
  153. || test_mat_pixel_gray(3, 3)
  154. || test_mat_pixel_rgb(3, 3)
  155. || test_mat_pixel_bgr(3, 3)
  156. || test_mat_pixel_rgba(3, 3)
  157. || test_mat_pixel_bgra(3, 3);
  158. }
  159. int main()
  160. {
  161. SRAND(7767517);
  162. return 0
  163. || test_mat_pixel_0()
  164. || test_mat_pixel_1()
  165. || test_mat_pixel_2()
  166. || test_mat_pixel_3();
  167. }