zbar_pipeline.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. #include "zbar_pipeline.h"
  2. #include "io_pipeline.h"
  3. #include "main.h"
  4. #include "pipeline.h"
  5. #include <assert.h>
  6. #include <zbar.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
  21. setup(MPPipeline *pipeline, const void *data)
  22. {
  23. scanner = zbar_image_scanner_create();
  24. zbar_image_scanner_set_config(scanner, 0, ZBAR_CFG_ENABLE, 1);
  25. }
  26. void
  27. mp_zbar_pipeline_start()
  28. {
  29. pipeline = mp_pipeline_new();
  30. mp_pipeline_invoke(pipeline, setup, NULL, 0);
  31. }
  32. void
  33. mp_zbar_pipeline_stop()
  34. {
  35. mp_pipeline_free(pipeline);
  36. }
  37. static bool
  38. is_3d_code(zbar_symbol_type_t type)
  39. {
  40. switch (type) {
  41. case ZBAR_EAN2:
  42. case ZBAR_EAN5:
  43. case ZBAR_EAN8:
  44. case ZBAR_UPCE:
  45. case ZBAR_ISBN10:
  46. case ZBAR_UPCA:
  47. case ZBAR_EAN13:
  48. case ZBAR_ISBN13:
  49. case ZBAR_I25:
  50. case ZBAR_DATABAR:
  51. case ZBAR_DATABAR_EXP:
  52. case ZBAR_CODABAR:
  53. case ZBAR_CODE39:
  54. case ZBAR_CODE93:
  55. case ZBAR_CODE128:
  56. return false;
  57. case ZBAR_COMPOSITE:
  58. case ZBAR_PDF417:
  59. case ZBAR_QRCODE:
  60. case ZBAR_SQCODE:
  61. return true;
  62. default:
  63. return false;
  64. }
  65. }
  66. static inline void
  67. map_coords(int *x, int *y, int width, int height, int rotation, bool mirrored)
  68. {
  69. int x_r, y_r;
  70. if (rotation == 0) {
  71. x_r = *x;
  72. y_r = *y;
  73. } else if (rotation == 90) {
  74. x_r = *y;
  75. y_r = height - *x - 1;
  76. } else if (rotation == 270) {
  77. x_r = width - *y - 1;
  78. y_r = *x;
  79. } else {
  80. x_r = width - *x - 1;
  81. y_r = height - *y - 1;
  82. }
  83. if (mirrored) {
  84. x_r = width - x_r - 1;
  85. }
  86. *x = x_r;
  87. *y = y_r;
  88. }
  89. static MPZBarCode
  90. process_symbol(const MPZBarImage *image,
  91. int width,
  92. int height,
  93. const zbar_symbol_t *symbol)
  94. {
  95. if (image->rotation == 90 || image->rotation == 270) {
  96. int tmp = width;
  97. width = height;
  98. height = tmp;
  99. }
  100. MPZBarCode code;
  101. unsigned loc_size = zbar_symbol_get_loc_size(symbol);
  102. assert(loc_size > 0);
  103. zbar_symbol_type_t type = zbar_symbol_get_type(symbol);
  104. if (is_3d_code(type) && loc_size == 4) {
  105. for (unsigned i = 0; i < loc_size; ++i) {
  106. code.bounds_x[i] = zbar_symbol_get_loc_x(symbol, i);
  107. code.bounds_y[i] = zbar_symbol_get_loc_y(symbol, i);
  108. }
  109. } else {
  110. int min_x = zbar_symbol_get_loc_x(symbol, 0);
  111. int min_y = zbar_symbol_get_loc_y(symbol, 0);
  112. int max_x = min_x, max_y = min_y;
  113. for (unsigned i = 1; i < loc_size; ++i) {
  114. int x = zbar_symbol_get_loc_x(symbol, i);
  115. int y = zbar_symbol_get_loc_y(symbol, i);
  116. min_x = MIN(min_x, x);
  117. min_y = MIN(min_y, y);
  118. max_x = MAX(max_x, x);
  119. max_y = MAX(max_y, y);
  120. }
  121. code.bounds_x[0] = min_x;
  122. code.bounds_y[0] = min_y;
  123. code.bounds_x[1] = max_x;
  124. code.bounds_y[1] = min_y;
  125. code.bounds_x[2] = max_x;
  126. code.bounds_y[2] = max_y;
  127. code.bounds_x[3] = min_x;
  128. code.bounds_y[3] = max_y;
  129. }
  130. for (uint8_t i = 0; i < 4; ++i) {
  131. map_coords(&code.bounds_x[i],
  132. &code.bounds_y[i],
  133. width,
  134. height,
  135. image->rotation,
  136. image->mirrored);
  137. }
  138. const char *data = zbar_symbol_get_data(symbol);
  139. unsigned int data_size = zbar_symbol_get_data_length(symbol);
  140. code.type = zbar_get_symbol_name(type);
  141. code.data = strndup(data, data_size + 1);
  142. code.data[data_size] = 0;
  143. return code;
  144. }
  145. static void
  146. process_image(MPPipeline *pipeline, MPZBarImage **_image)
  147. {
  148. MPZBarImage *image = *_image;
  149. assert(image->pixel_format == MP_PIXEL_FMT_BGGR8 ||
  150. image->pixel_format == MP_PIXEL_FMT_GBRG8 ||
  151. image->pixel_format == MP_PIXEL_FMT_GRBG8 ||
  152. image->pixel_format == MP_PIXEL_FMT_RGGB8 ||
  153. image->pixel_format == MP_PIXEL_FMT_BGGR10P ||
  154. image->pixel_format == MP_PIXEL_FMT_GBRG10P ||
  155. image->pixel_format == MP_PIXEL_FMT_GRBG10P ||
  156. image->pixel_format == MP_PIXEL_FMT_RGGB10P);
  157. // Create a grayscale image for scanning from the current preview.
  158. // Rotate/mirror correctly.
  159. int width = image->width / 2;
  160. int height = image->height / 2;
  161. uint8_t *data = malloc(width * height * sizeof(uint8_t));
  162. size_t row_length =
  163. mp_pixel_format_width_to_bytes(image->pixel_format, image->width);
  164. int padding_bytes =
  165. mp_pixel_format_width_to_padding(image->pixel_format, image->width);
  166. size_t i = 0, padding_offset = 0;
  167. size_t offset;
  168. switch (image->pixel_format) {
  169. case MP_PIXEL_FMT_BGGR8:
  170. case MP_PIXEL_FMT_GBRG8:
  171. case MP_PIXEL_FMT_GRBG8:
  172. case MP_PIXEL_FMT_RGGB8:
  173. for (int y = 0; y < image->height; y += 2) {
  174. for (int x = 0; x < row_length; x += 2) {
  175. data[i++] = image->data[x + row_length * y];
  176. }
  177. }
  178. break;
  179. case MP_PIXEL_FMT_BGGR10P:
  180. case MP_PIXEL_FMT_GBRG10P:
  181. case MP_PIXEL_FMT_GRBG10P:
  182. case MP_PIXEL_FMT_RGGB10P:
  183. // Skip 5th byte of each 4-pixel segment by incrementing an
  184. // offset every time a 5th byte is reached, making the
  185. // X coordinate land on the next byte:
  186. //
  187. // image->data | | | | X | | | | X | | | | X | | | | X | ...
  188. // x 0 2 4 6 8 10 12 14 16 18 20 ...
  189. // offset 0 1 2 3 4 5 ...
  190. // > ---> -----> ------->
  191. // x + offset 0 2 4 6 8 10 12 16 18 ...
  192. for (int y = 0; y < image->height; y += 2) {
  193. offset = 0;
  194. for (int x = 0; x < image->width; x += 2) {
  195. if (x % 4 == 0)
  196. offset += 1;
  197. data[i++] = image->data[x + offset + padding_offset +
  198. row_length * y];
  199. }
  200. // Skip padding
  201. padding_offset += padding_bytes * 2;
  202. }
  203. break;
  204. default:
  205. assert(0);
  206. }
  207. // Create image for zbar
  208. zbar_image_t *zbar_image = zbar_image_create();
  209. zbar_image_set_format(zbar_image, zbar_fourcc('Y', '8', '0', '0'));
  210. zbar_image_set_size(zbar_image, width, height);
  211. zbar_image_set_data(zbar_image,
  212. data,
  213. width * height * sizeof(uint8_t),
  214. zbar_image_free_data);
  215. int res = zbar_scan_image(scanner, zbar_image);
  216. assert(res >= 0);
  217. if (res > 0) {
  218. MPZBarScanResult *result = malloc(sizeof(MPZBarScanResult));
  219. result->size = res;
  220. const zbar_symbol_t *symbol = zbar_image_first_symbol(zbar_image);
  221. for (int i = 0; i < MIN(res, 8); ++i) {
  222. assert(symbol != NULL);
  223. result->codes[i] =
  224. process_symbol(image, width, height, symbol);
  225. symbol = zbar_symbol_next(symbol);
  226. }
  227. mp_main_set_zbar_result(result);
  228. } else {
  229. mp_main_set_zbar_result(NULL);
  230. }
  231. zbar_image_destroy(zbar_image);
  232. mp_zbar_image_unref(image);
  233. ++frames_processed;
  234. }
  235. void
  236. mp_zbar_pipeline_process_image(MPZBarImage *image)
  237. {
  238. // If we haven't processed the previous frame yet, drop this one
  239. if (frames_received != frames_processed) {
  240. mp_zbar_image_unref(image);
  241. return;
  242. }
  243. ++frames_received;
  244. mp_pipeline_invoke(pipeline,
  245. (MPPipelineCallback)process_image,
  246. &image,
  247. sizeof(MPZBarImage *));
  248. }
  249. MPZBarImage *
  250. mp_zbar_image_new(uint8_t *data,
  251. MPPixelFormat pixel_format,
  252. int width,
  253. int height,
  254. int rotation,
  255. bool mirrored)
  256. {
  257. MPZBarImage *image = malloc(sizeof(MPZBarImage));
  258. image->data = data;
  259. image->pixel_format = pixel_format;
  260. image->width = width;
  261. image->height = height;
  262. image->rotation = rotation;
  263. image->mirrored = mirrored;
  264. image->ref_count = 1;
  265. return image;
  266. }
  267. MPZBarImage *
  268. mp_zbar_image_ref(MPZBarImage *image)
  269. {
  270. ++image->ref_count;
  271. return image;
  272. }
  273. void
  274. mp_zbar_image_unref(MPZBarImage *image)
  275. {
  276. if (--image->ref_count == 0) {
  277. free(image->data);
  278. free(image);
  279. }
  280. }