image.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #include "error.h"
  24. #include "image.h"
  25. #include "refcnt.h"
  26. zbar_image_t *zbar_image_create ()
  27. {
  28. zbar_image_t *img = calloc(1, sizeof(zbar_image_t));
  29. _zbar_refcnt_init();
  30. _zbar_image_refcnt(img, 1);
  31. img->srcidx = -1;
  32. return(img);
  33. }
  34. void _zbar_image_free (zbar_image_t *img)
  35. {
  36. if(img->syms) {
  37. zbar_symbol_set_ref(img->syms, -1);
  38. img->syms = NULL;
  39. }
  40. free(img);
  41. }
  42. void zbar_image_destroy (zbar_image_t *img)
  43. {
  44. _zbar_image_refcnt(img, -1);
  45. }
  46. void zbar_image_ref (zbar_image_t *img,
  47. int refs)
  48. {
  49. _zbar_image_refcnt(img, refs);
  50. }
  51. unsigned long zbar_image_get_format (const zbar_image_t *img)
  52. {
  53. return(img->format);
  54. }
  55. unsigned zbar_image_get_sequence (const zbar_image_t *img)
  56. {
  57. return(img->seq);
  58. }
  59. unsigned zbar_image_get_width (const zbar_image_t *img)
  60. {
  61. return(img->width);
  62. }
  63. unsigned zbar_image_get_height (const zbar_image_t *img)
  64. {
  65. return(img->height);
  66. }
  67. const void *zbar_image_get_data (const zbar_image_t *img)
  68. {
  69. return(img->data);
  70. }
  71. unsigned long zbar_image_get_data_length (const zbar_image_t *img)
  72. {
  73. return(img->datalen);
  74. }
  75. void zbar_image_set_format (zbar_image_t *img,
  76. unsigned long fmt)
  77. {
  78. img->format = fmt;
  79. }
  80. void zbar_image_set_sequence (zbar_image_t *img,
  81. unsigned seq)
  82. {
  83. img->seq = seq;
  84. }
  85. void zbar_image_set_size (zbar_image_t *img,
  86. unsigned w,
  87. unsigned h)
  88. {
  89. img->width = w;
  90. img->height = h;
  91. }
  92. inline void zbar_image_free_data (zbar_image_t *img)
  93. {
  94. if(!img)
  95. return;
  96. if(img->src) {
  97. /* replace video image w/new copy */
  98. assert(img->refcnt); /* FIXME needs lock */
  99. zbar_image_t *newimg = zbar_image_create();
  100. memcpy(newimg, img, sizeof(zbar_image_t));
  101. /* recycle video image */
  102. newimg->cleanup(newimg);
  103. /* detach old image from src */
  104. img->cleanup = NULL;
  105. img->src = NULL;
  106. img->srcidx = -1;
  107. }
  108. else if(img->cleanup && img->data) {
  109. if(img->cleanup != zbar_image_free_data) {
  110. /* using function address to detect this case is a bad idea;
  111. * windows link libraries add an extra layer of indirection...
  112. * this works around that problem (bug #2796277)
  113. */
  114. zbar_image_cleanup_handler_t *cleanup = img->cleanup;
  115. img->cleanup = zbar_image_free_data;
  116. cleanup(img);
  117. }
  118. else
  119. {
  120. #ifdef ZBAR_TODO
  121. free((void*)img->data);
  122. #endif
  123. }
  124. }
  125. img->data = NULL;
  126. }
  127. void zbar_image_set_data (zbar_image_t *img,
  128. const void *data,
  129. unsigned long len,
  130. zbar_image_cleanup_handler_t *cleanup)
  131. {
  132. zbar_image_free_data(img);
  133. img->data = data;
  134. img->datalen = len;
  135. img->cleanup = cleanup;
  136. }
  137. void zbar_image_set_userdata (zbar_image_t *img,
  138. void *userdata)
  139. {
  140. img->userdata = userdata;
  141. }
  142. void *zbar_image_get_userdata (const zbar_image_t *img)
  143. {
  144. return(img->userdata);
  145. }
  146. zbar_image_t *zbar_image_copy (const zbar_image_t *src)
  147. {
  148. zbar_image_t *dst = zbar_image_create();
  149. dst->format = src->format;
  150. dst->width = src->width;
  151. dst->height = src->height;
  152. dst->datalen = src->datalen;
  153. dst->data = malloc(src->datalen);
  154. assert(dst->data);
  155. memcpy((void*)dst->data, src->data, src->datalen);
  156. dst->cleanup = zbar_image_free_data;
  157. return(dst);
  158. }
  159. const zbar_symbol_set_t *zbar_image_get_symbols (const zbar_image_t *img)
  160. {
  161. return(img->syms);
  162. }
  163. void zbar_image_set_symbols (zbar_image_t *img,
  164. const zbar_symbol_set_t *syms)
  165. {
  166. if(img->syms)
  167. zbar_symbol_set_ref(img->syms, -1);
  168. img->syms = (zbar_symbol_set_t*)syms;
  169. if(syms)
  170. zbar_symbol_set_ref(img->syms, 1);
  171. }
  172. const zbar_symbol_t *zbar_image_first_symbol (const zbar_image_t *img)
  173. {
  174. return((img->syms) ? img->syms->head : NULL);
  175. }
  176. typedef struct zimg_hdr_s {
  177. uint32_t magic, format;
  178. uint16_t width, height;
  179. uint32_t size;
  180. } zimg_hdr_t;
  181. #if 0
  182. int zbar_image_write (const zbar_image_t *img,
  183. const char *filebase)
  184. {
  185. int len = strlen(filebase) + 16;
  186. char filename[255];
  187. strcpy(filename, filebase);
  188. int n = 0;
  189. if(*(char*)&img->format >= ' ')
  190. n = snprintf(filename, len, "%s.%.4s.zimg",
  191. filebase, (char*)&img->format);
  192. else
  193. n = snprintf(filename, len, "%s.%08" PRIx32 ".zimg",
  194. filebase, img->format);
  195. assert(n < len);
  196. filename[len] = '\0';
  197. zprintf(1, "dumping %.4s(%08" PRIx32 ") image to %s\n",
  198. (char*)&img->format, img->format, filename);
  199. FILE *f = fopen(filename, "w");
  200. if(!f) {
  201. int rc = errno;
  202. zprintf(1, "ERROR opening %s: %s\n", filename, strerror(rc));
  203. return(rc);
  204. }
  205. zimg_hdr_t hdr;
  206. hdr.magic = 0x676d697a;
  207. hdr.format = img->format;
  208. hdr.width = img->width;
  209. hdr.height = img->height;
  210. hdr.size = img->datalen;
  211. if(fwrite(&hdr, sizeof(hdr), 1, f) != 1 ||
  212. fwrite(img->data, 1, img->datalen, f) != img->datalen) {
  213. int rc = errno;
  214. zprintf(1, "ERROR writing %s: %s\n", filename, strerror(rc));
  215. fclose(f);
  216. return(rc);
  217. }
  218. return(fclose(f));
  219. }
  220. #endif
  221. #ifdef DEBUG_SVG
  222. # include <png.h>
  223. int zbar_image_write_png (const zbar_image_t *img,
  224. const char *filename)
  225. {
  226. int rc = -1;
  227. FILE *file = NULL;
  228. png_struct *png = NULL;
  229. png_info *info = NULL;
  230. const uint8_t **rows = NULL;
  231. rows = malloc(img->height * sizeof(*rows));
  232. if(!rows)
  233. goto done;
  234. rows[0] = img->data;
  235. int y;
  236. for(y = 1; y < img->height; y++)
  237. rows[y] = rows[y - 1] + img->width;
  238. file = fopen(filename, "wb");
  239. if(!file)
  240. goto done;
  241. png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  242. if(!png)
  243. goto done;
  244. info = png_create_info_struct(png);
  245. if(!info)
  246. goto done;
  247. if(setjmp(png_jmpbuf(png)))
  248. goto done;
  249. png_init_io(png, file);
  250. png_set_compression_level(png, 9);
  251. png_set_IHDR(png, info, img->width, img->height, 8, PNG_COLOR_TYPE_GRAY,
  252. PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
  253. PNG_FILTER_TYPE_DEFAULT);
  254. png_set_rows(png, info, (void*)rows);
  255. png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
  256. png_write_end(png,info);
  257. rc = 0;
  258. done:
  259. if(png)
  260. png_destroy_write_struct(&png, &info);
  261. if(rows)
  262. free(rows);
  263. if(file)
  264. fclose(file);
  265. return(rc);
  266. }
  267. #endif