zbar_pipeline.c 9.4 KB

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