zbar_pipeline.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include "zbar_pipeline.h"
  2. #include "pipeline.h"
  3. #include "main.h"
  4. #include "io_pipeline.h"
  5. #include <zbar.h>
  6. #include <assert.h>
  7. struct _MPZBarImage {
  8. uint8_t *data;
  9. MPPixelFormat pixel_format;
  10. int width;
  11. int height;
  12. int rotation;
  13. bool mirrored;
  14. _Atomic int ref_count;
  15. };
  16. static MPPipeline *pipeline;
  17. static volatile int frames_processed = 0;
  18. static volatile int frames_received = 0;
  19. static zbar_image_scanner_t *scanner;
  20. static void setup(MPPipeline *pipeline, const void *data)
  21. {
  22. scanner = zbar_image_scanner_create();
  23. zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
  24. }
  25. void
  26. mp_zbar_pipeline_start()
  27. {
  28. pipeline = mp_pipeline_new();
  29. mp_pipeline_invoke(pipeline, setup, NULL, 0);
  30. }
  31. void
  32. mp_zbar_pipeline_stop()
  33. {
  34. mp_pipeline_free(pipeline);
  35. }
  36. static bool
  37. is_3d_code(zbar_symbol_type_t type)
  38. {
  39. switch (type) {
  40. case ZBAR_EAN2:
  41. case ZBAR_EAN5:
  42. case ZBAR_EAN8:
  43. case ZBAR_UPCE:
  44. case ZBAR_ISBN10:
  45. case ZBAR_UPCA:
  46. case ZBAR_EAN13:
  47. case ZBAR_ISBN13:
  48. case ZBAR_I25:
  49. case ZBAR_DATABAR:
  50. case ZBAR_DATABAR_EXP:
  51. case ZBAR_CODABAR:
  52. case ZBAR_CODE39:
  53. case ZBAR_CODE93:
  54. case ZBAR_CODE128:
  55. return false;
  56. case ZBAR_COMPOSITE:
  57. case ZBAR_PDF417:
  58. case ZBAR_QRCODE:
  59. case ZBAR_SQCODE:
  60. return true;
  61. default:
  62. return false;
  63. }
  64. }
  65. static inline void
  66. map_coords(int *x, int *y, int width, int height, int rotation, bool mirrored)
  67. {
  68. int x_r, y_r;
  69. if (rotation == 0) {
  70. x_r = *x;
  71. y_r = *y;
  72. } else if (rotation == 90) {
  73. x_r = *y;
  74. y_r = height - *x - 1;
  75. } else if (rotation == 270) {
  76. x_r = width - *y - 1;
  77. y_r = *x;
  78. } else {
  79. x_r = width - *x - 1;
  80. y_r = height - *y - 1;
  81. }
  82. if (mirrored) {
  83. x_r = width - x_r - 1;
  84. }
  85. *x = x_r;
  86. *y = y_r;
  87. }
  88. static MPZBarCode
  89. process_symbol(const MPZBarImage *image, int width, int height, const zbar_symbol_t *symbol)
  90. {
  91. if (image->rotation == 90 || image->rotation == 270) {
  92. int tmp = width;
  93. width = height;
  94. height = tmp;
  95. }
  96. MPZBarCode code;
  97. unsigned loc_size = zbar_symbol_get_loc_size(symbol);
  98. assert(loc_size > 0);
  99. zbar_symbol_type_t type = zbar_symbol_get_type(symbol);
  100. if (is_3d_code(type) && loc_size == 4) {
  101. for (unsigned i = 0; i < loc_size; ++i) {
  102. code.bounds_x[i] = zbar_symbol_get_loc_x(symbol, i);
  103. code.bounds_y[i] = zbar_symbol_get_loc_y(symbol, i);
  104. }
  105. } else {
  106. int min_x = zbar_symbol_get_loc_x(symbol, 0);
  107. int min_y = zbar_symbol_get_loc_y(symbol, 0);
  108. int max_x = min_x, max_y = min_y;
  109. for (unsigned i = 1; i < loc_size; ++i) {
  110. int x = zbar_symbol_get_loc_x(symbol, i);
  111. int y = zbar_symbol_get_loc_y(symbol, i);
  112. min_x = MIN(min_x, x);
  113. min_y = MIN(min_y, y);
  114. max_x = MAX(max_x, x);
  115. max_y = MAX(max_y, y);
  116. }
  117. code.bounds_x[0] = min_x;
  118. code.bounds_y[0] = min_y;
  119. code.bounds_x[1] = max_x;
  120. code.bounds_y[1] = min_y;
  121. code.bounds_x[2] = max_x;
  122. code.bounds_y[2] = max_y;
  123. code.bounds_x[3] = min_x;
  124. code.bounds_y[3] = max_y;
  125. }
  126. for (uint8_t i = 0; i < 4; ++i) {
  127. map_coords(&code.bounds_x[i], &code.bounds_y[i], width, height, image->rotation, image->mirrored);
  128. }
  129. const char *data = zbar_symbol_get_data(symbol);
  130. unsigned int data_size = zbar_symbol_get_data_length(symbol);
  131. code.type = zbar_get_symbol_name(type);
  132. code.data = strndup(data, data_size+1);
  133. code.data[data_size] = 0;
  134. return code;
  135. }
  136. static void
  137. process_image(MPPipeline *pipeline, MPZBarImage **_image)
  138. {
  139. MPZBarImage *image = *_image;
  140. assert(image->pixel_format == MP_PIXEL_FMT_BGGR8
  141. || image->pixel_format == MP_PIXEL_FMT_GBRG8
  142. || image->pixel_format == MP_PIXEL_FMT_GRBG8
  143. || image->pixel_format == MP_PIXEL_FMT_RGGB8);
  144. // Create a grayscale image for scanning from the current preview.
  145. // Rotate/mirror correctly.
  146. int width = image->width / 2;
  147. int height = image->height / 2;
  148. uint8_t *data = malloc(width * height * sizeof(uint8_t));
  149. size_t i = 0;
  150. for (int y = 0; y < image->height; y += 2) {
  151. for (int x = 0; x < image->width; x += 2) {
  152. data[++i] = image->data[x + image->width * y];
  153. }
  154. }
  155. // Create image for zbar
  156. zbar_image_t *zbar_image = zbar_image_create();
  157. zbar_image_set_format(zbar_image, zbar_fourcc('Y', '8', '0', '0'));
  158. zbar_image_set_size(zbar_image, width, height);
  159. zbar_image_set_data(zbar_image, data, width * height * sizeof(uint8_t), NULL);
  160. int res = zbar_scan_image(scanner, zbar_image);
  161. assert(res >= 0);
  162. if (res > 0) {
  163. MPZBarScanResult *result = malloc(sizeof(MPZBarScanResult));
  164. result->size = res;
  165. const zbar_symbol_t *symbol = zbar_image_first_symbol(zbar_image);
  166. for (int i = 0; i < MIN(res, 8); ++i) {
  167. assert(symbol != NULL);
  168. result->codes[i] = process_symbol(image, width, height, symbol);
  169. symbol = zbar_symbol_next(symbol);
  170. }
  171. mp_main_set_zbar_result(result);
  172. } else {
  173. mp_main_set_zbar_result(NULL);
  174. }
  175. zbar_image_destroy(zbar_image);
  176. mp_zbar_image_unref(image);
  177. ++frames_processed;
  178. }
  179. void
  180. mp_zbar_pipeline_process_image(MPZBarImage *image)
  181. {
  182. // If we haven't processed the previous frame yet, drop this one
  183. if (frames_received != frames_processed) {
  184. mp_zbar_image_unref(image);
  185. return;
  186. }
  187. ++frames_received;
  188. mp_pipeline_invoke(pipeline, (MPPipelineCallback)process_image, &image,
  189. sizeof(MPZBarImage *));
  190. }
  191. MPZBarImage *
  192. mp_zbar_image_new(uint8_t *data,
  193. MPPixelFormat pixel_format,
  194. int width,
  195. int height,
  196. int rotation,
  197. bool mirrored)
  198. {
  199. MPZBarImage *image = malloc(sizeof(MPZBarImage));
  200. image->data = data;
  201. image->pixel_format = pixel_format;
  202. image->width = width;
  203. image->height = height;
  204. image->rotation = rotation;
  205. image->mirrored = mirrored;
  206. image->ref_count = 1;
  207. return image;
  208. }
  209. MPZBarImage *
  210. mp_zbar_image_ref(MPZBarImage *image)
  211. {
  212. ++image->ref_count;
  213. return image;
  214. }
  215. void
  216. mp_zbar_image_unref(MPZBarImage *image)
  217. {
  218. if (--image->ref_count == 0) {
  219. free(image->data);
  220. free(image);
  221. }
  222. }