entangledstate-isbn 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README +1 -1
- data/Rakefile +0 -18
- data/VERSION +1 -0
- data/isbn.gemspec +290 -7
- data/lib/isbn.rb +6 -6
- data/src/gocr-0.48/.cvsignore +6 -0
- data/src/gocr-0.48/AUTHORS +7 -0
- data/src/gocr-0.48/BUGS +55 -0
- data/src/gocr-0.48/CREDITS +17 -0
- data/src/gocr-0.48/HISTORY +243 -0
- data/src/gocr-0.48/INSTALL +83 -0
- data/src/gocr-0.48/Makefile +193 -0
- data/src/gocr-0.48/Makefile.in +193 -0
- data/src/gocr-0.48/README +165 -0
- data/src/gocr-0.48/READMEde.txt +80 -0
- data/src/gocr-0.48/REMARK.txt +18 -0
- data/src/gocr-0.48/REVIEW +538 -0
- data/src/gocr-0.48/TODO +65 -0
- data/src/gocr-0.48/bin/.cvsignore +2 -0
- data/src/gocr-0.48/bin/create_db +38 -0
- data/src/gocr-0.48/bin/gocr.tcl +527 -0
- data/src/gocr-0.48/bin/gocr_chk.sh +44 -0
- data/src/gocr-0.48/configure +4689 -0
- data/src/gocr-0.48/configure.in +71 -0
- data/src/gocr-0.48/doc/.#Makefile.1.6 +39 -0
- data/src/gocr-0.48/doc/.cvsignore +2 -0
- data/src/gocr-0.48/doc/Makefile +39 -0
- data/src/gocr-0.48/doc/Makefile.in +39 -0
- data/src/gocr-0.48/doc/example.dtd +53 -0
- data/src/gocr-0.48/doc/example.xml +21 -0
- data/src/gocr-0.48/doc/examples.txt +67 -0
- data/src/gocr-0.48/doc/gocr.html +578 -0
- data/src/gocr-0.48/doc/unicode.txt +57 -0
- data/src/gocr-0.48/examples/.#Makefile.1.22 +166 -0
- data/src/gocr-0.48/examples/4x6.png +0 -0
- data/src/gocr-0.48/examples/4x6.txt +2 -0
- data/src/gocr-0.48/examples/5x7.png +0 -0
- data/src/gocr-0.48/examples/5x7.png.txt +2 -0
- data/src/gocr-0.48/examples/5x8.png +0 -0
- data/src/gocr-0.48/examples/5x8.png.txt +2 -0
- data/src/gocr-0.48/examples/Makefile +166 -0
- data/src/gocr-0.48/examples/color.fig +20 -0
- data/src/gocr-0.48/examples/ex.fig +16 -0
- data/src/gocr-0.48/examples/font.tex +22 -0
- data/src/gocr-0.48/examples/font1.tex +46 -0
- data/src/gocr-0.48/examples/font2.fig +27 -0
- data/src/gocr-0.48/examples/font_nw.tex +24 -0
- data/src/gocr-0.48/examples/handwrt1.jpg +0 -0
- data/src/gocr-0.48/examples/handwrt1.txt +10 -0
- data/src/gocr-0.48/examples/inverse.fig +20 -0
- data/src/gocr-0.48/examples/matrix.jpg +0 -0
- data/src/gocr-0.48/examples/ocr-a-subset.png +0 -0
- data/src/gocr-0.48/examples/ocr-a-subset.png.txt +4 -0
- data/src/gocr-0.48/examples/ocr-a.png +0 -0
- data/src/gocr-0.48/examples/ocr-a.txt +6 -0
- data/src/gocr-0.48/examples/ocr-b.png +0 -0
- data/src/gocr-0.48/examples/ocr-b.png.txt +4 -0
- data/src/gocr-0.48/examples/polish.tex +28 -0
- data/src/gocr-0.48/examples/rotate45.fig +14 -0
- data/src/gocr-0.48/examples/score +36 -0
- data/src/gocr-0.48/examples/text.tex +28 -0
- data/src/gocr-0.48/gocr.spec +143 -0
- data/src/gocr-0.48/gpl.html +537 -0
- data/src/gocr-0.48/include/.cvsignore +2 -0
- data/src/gocr-0.48/include/config.h +36 -0
- data/src/gocr-0.48/include/config.h.in +36 -0
- data/src/gocr-0.48/include/version.h +2 -0
- data/src/gocr-0.48/install-sh +3 -0
- data/src/gocr-0.48/make.bat +57 -0
- data/src/gocr-0.48/man/.cvsignore +2 -0
- data/src/gocr-0.48/man/Makefile +29 -0
- data/src/gocr-0.48/man/Makefile.in +29 -0
- data/src/gocr-0.48/man/man1/gocr.1 +166 -0
- data/src/gocr-0.48/src/.cvsignore +4 -0
- data/src/gocr-0.48/src/Makefile +132 -0
- data/src/gocr-0.48/src/Makefile.in +132 -0
- data/src/gocr-0.48/src/amiga.h +31 -0
- data/src/gocr-0.48/src/barcode.c +846 -0
- data/src/gocr-0.48/src/barcode.c.orig +593 -0
- data/src/gocr-0.48/src/barcode.h +11 -0
- data/src/gocr-0.48/src/box.c +372 -0
- data/src/gocr-0.48/src/database.c +462 -0
- data/src/gocr-0.48/src/detect.c +943 -0
- data/src/gocr-0.48/src/gocr.c +373 -0
- data/src/gocr-0.48/src/gocr.h +288 -0
- data/src/gocr-0.48/src/jconv.c +168 -0
- data/src/gocr-0.48/src/job.c +84 -0
- data/src/gocr-0.48/src/lines.c +350 -0
- data/src/gocr-0.48/src/list.c +334 -0
- data/src/gocr-0.48/src/list.h +90 -0
- data/src/gocr-0.48/src/ocr0.c +6756 -0
- data/src/gocr-0.48/src/ocr0.h +63 -0
- data/src/gocr-0.48/src/ocr0n.c +1475 -0
- data/src/gocr-0.48/src/ocr1.c +85 -0
- data/src/gocr-0.48/src/ocr1.h +3 -0
- data/src/gocr-0.48/src/otsu.c +289 -0
- data/src/gocr-0.48/src/otsu.h +23 -0
- data/src/gocr-0.48/src/output.c +289 -0
- data/src/gocr-0.48/src/output.h +37 -0
- data/src/gocr-0.48/src/pcx.c +153 -0
- data/src/gocr-0.48/src/pcx.h +9 -0
- data/src/gocr-0.48/src/pgm2asc.c +2893 -0
- data/src/gocr-0.48/src/pgm2asc.h +105 -0
- data/src/gocr-0.48/src/pixel.c +537 -0
- data/src/gocr-0.48/src/pnm.c +533 -0
- data/src/gocr-0.48/src/pnm.h +35 -0
- data/src/gocr-0.48/src/progress.c +87 -0
- data/src/gocr-0.48/src/progress.h +42 -0
- data/src/gocr-0.48/src/remove.c +703 -0
- data/src/gocr-0.48/src/tga.c +87 -0
- data/src/gocr-0.48/src/tga.h +6 -0
- data/src/gocr-0.48/src/unicode.c +1314 -0
- data/src/gocr-0.48/src/unicode.h +1257 -0
- data/src/jpeg-7/Makefile.am +133 -0
- data/src/jpeg-7/Makefile.in +1089 -0
- data/src/jpeg-7/README +322 -0
- data/src/jpeg-7/aclocal.m4 +8990 -0
- data/src/jpeg-7/ansi2knr.1 +36 -0
- data/src/jpeg-7/ansi2knr.c +739 -0
- data/src/jpeg-7/cderror.h +132 -0
- data/src/jpeg-7/cdjpeg.c +181 -0
- data/src/jpeg-7/cdjpeg.h +187 -0
- data/src/jpeg-7/change.log +270 -0
- data/src/jpeg-7/cjpeg.1 +325 -0
- data/src/jpeg-7/cjpeg.c +616 -0
- data/src/jpeg-7/ckconfig.c +402 -0
- data/src/jpeg-7/coderules.txt +118 -0
- data/src/jpeg-7/config.guess +1561 -0
- data/src/jpeg-7/config.sub +1686 -0
- data/src/jpeg-7/configure +17139 -0
- data/src/jpeg-7/configure.ac +317 -0
- data/src/jpeg-7/depcomp +630 -0
- data/src/jpeg-7/djpeg.1 +251 -0
- data/src/jpeg-7/djpeg.c +617 -0
- data/src/jpeg-7/example.c +433 -0
- data/src/jpeg-7/filelist.txt +215 -0
- data/src/jpeg-7/install-sh +520 -0
- data/src/jpeg-7/install.txt +1097 -0
- data/src/jpeg-7/jaricom.c +148 -0
- data/src/jpeg-7/jcapimin.c +282 -0
- data/src/jpeg-7/jcapistd.c +161 -0
- data/src/jpeg-7/jcarith.c +921 -0
- data/src/jpeg-7/jccoefct.c +453 -0
- data/src/jpeg-7/jccolor.c +459 -0
- data/src/jpeg-7/jcdctmgr.c +482 -0
- data/src/jpeg-7/jchuff.c +1612 -0
- data/src/jpeg-7/jcinit.c +65 -0
- data/src/jpeg-7/jcmainct.c +293 -0
- data/src/jpeg-7/jcmarker.c +667 -0
- data/src/jpeg-7/jcmaster.c +770 -0
- data/src/jpeg-7/jcomapi.c +106 -0
- data/src/jpeg-7/jconfig.bcc +48 -0
- data/src/jpeg-7/jconfig.cfg +45 -0
- data/src/jpeg-7/jconfig.dj +38 -0
- data/src/jpeg-7/jconfig.mac +43 -0
- data/src/jpeg-7/jconfig.manx +43 -0
- data/src/jpeg-7/jconfig.mc6 +52 -0
- data/src/jpeg-7/jconfig.sas +43 -0
- data/src/jpeg-7/jconfig.st +42 -0
- data/src/jpeg-7/jconfig.txt +155 -0
- data/src/jpeg-7/jconfig.vc +45 -0
- data/src/jpeg-7/jconfig.vms +37 -0
- data/src/jpeg-7/jconfig.wat +38 -0
- data/src/jpeg-7/jcparam.c +632 -0
- data/src/jpeg-7/jcprepct.c +358 -0
- data/src/jpeg-7/jcsample.c +545 -0
- data/src/jpeg-7/jctrans.c +381 -0
- data/src/jpeg-7/jdapimin.c +396 -0
- data/src/jpeg-7/jdapistd.c +275 -0
- data/src/jpeg-7/jdarith.c +762 -0
- data/src/jpeg-7/jdatadst.c +151 -0
- data/src/jpeg-7/jdatasrc.c +212 -0
- data/src/jpeg-7/jdcoefct.c +736 -0
- data/src/jpeg-7/jdcolor.c +396 -0
- data/src/jpeg-7/jdct.h +393 -0
- data/src/jpeg-7/jddctmgr.c +382 -0
- data/src/jpeg-7/jdhuff.c +1309 -0
- data/src/jpeg-7/jdinput.c +384 -0
- data/src/jpeg-7/jdmainct.c +512 -0
- data/src/jpeg-7/jdmarker.c +1360 -0
- data/src/jpeg-7/jdmaster.c +663 -0
- data/src/jpeg-7/jdmerge.c +400 -0
- data/src/jpeg-7/jdpostct.c +290 -0
- data/src/jpeg-7/jdsample.c +361 -0
- data/src/jpeg-7/jdtrans.c +136 -0
- data/src/jpeg-7/jerror.c +252 -0
- data/src/jpeg-7/jerror.h +304 -0
- data/src/jpeg-7/jfdctflt.c +174 -0
- data/src/jpeg-7/jfdctfst.c +230 -0
- data/src/jpeg-7/jfdctint.c +4348 -0
- data/src/jpeg-7/jidctflt.c +242 -0
- data/src/jpeg-7/jidctfst.c +368 -0
- data/src/jpeg-7/jidctint.c +5137 -0
- data/src/jpeg-7/jinclude.h +91 -0
- data/src/jpeg-7/jmemansi.c +167 -0
- data/src/jpeg-7/jmemdos.c +638 -0
- data/src/jpeg-7/jmemdosa.asm +379 -0
- data/src/jpeg-7/jmemmac.c +289 -0
- data/src/jpeg-7/jmemmgr.c +1118 -0
- data/src/jpeg-7/jmemname.c +276 -0
- data/src/jpeg-7/jmemnobs.c +109 -0
- data/src/jpeg-7/jmemsys.h +198 -0
- data/src/jpeg-7/jmorecfg.h +369 -0
- data/src/jpeg-7/jpegint.h +395 -0
- data/src/jpeg-7/jpeglib.h +1135 -0
- data/src/jpeg-7/jpegtran.1 +272 -0
- data/src/jpeg-7/jpegtran.c +546 -0
- data/src/jpeg-7/jquant1.c +856 -0
- data/src/jpeg-7/jquant2.c +1310 -0
- data/src/jpeg-7/jutils.c +179 -0
- data/src/jpeg-7/jversion.h +14 -0
- data/src/jpeg-7/libjpeg.map +4 -0
- data/src/jpeg-7/libjpeg.txt +3067 -0
- data/src/jpeg-7/ltmain.sh +8406 -0
- data/src/jpeg-7/makcjpeg.st +36 -0
- data/src/jpeg-7/makdjpeg.st +36 -0
- data/src/jpeg-7/makeadsw.vc6 +77 -0
- data/src/jpeg-7/makeasln.vc9 +33 -0
- data/src/jpeg-7/makecdep.vc6 +82 -0
- data/src/jpeg-7/makecdsp.vc6 +130 -0
- data/src/jpeg-7/makecmak.vc6 +159 -0
- data/src/jpeg-7/makecvcp.vc9 +186 -0
- data/src/jpeg-7/makeddep.vc6 +82 -0
- data/src/jpeg-7/makeddsp.vc6 +130 -0
- data/src/jpeg-7/makedmak.vc6 +159 -0
- data/src/jpeg-7/makedvcp.vc9 +186 -0
- data/src/jpeg-7/makefile.ansi +220 -0
- data/src/jpeg-7/makefile.bcc +291 -0
- data/src/jpeg-7/makefile.dj +226 -0
- data/src/jpeg-7/makefile.manx +220 -0
- data/src/jpeg-7/makefile.mc6 +255 -0
- data/src/jpeg-7/makefile.mms +224 -0
- data/src/jpeg-7/makefile.sas +258 -0
- data/src/jpeg-7/makefile.unix +234 -0
- data/src/jpeg-7/makefile.vc +217 -0
- data/src/jpeg-7/makefile.vms +142 -0
- data/src/jpeg-7/makefile.wat +239 -0
- data/src/jpeg-7/makejdep.vc6 +423 -0
- data/src/jpeg-7/makejdsp.vc6 +285 -0
- data/src/jpeg-7/makejdsw.vc6 +29 -0
- data/src/jpeg-7/makejmak.vc6 +425 -0
- data/src/jpeg-7/makejsln.vc9 +17 -0
- data/src/jpeg-7/makejvcp.vc9 +328 -0
- data/src/jpeg-7/makeproj.mac +213 -0
- data/src/jpeg-7/makerdep.vc6 +6 -0
- data/src/jpeg-7/makerdsp.vc6 +78 -0
- data/src/jpeg-7/makermak.vc6 +110 -0
- data/src/jpeg-7/makervcp.vc9 +133 -0
- data/src/jpeg-7/maketdep.vc6 +43 -0
- data/src/jpeg-7/maketdsp.vc6 +122 -0
- data/src/jpeg-7/maketmak.vc6 +131 -0
- data/src/jpeg-7/maketvcp.vc9 +178 -0
- data/src/jpeg-7/makewdep.vc6 +6 -0
- data/src/jpeg-7/makewdsp.vc6 +78 -0
- data/src/jpeg-7/makewmak.vc6 +110 -0
- data/src/jpeg-7/makewvcp.vc9 +133 -0
- data/src/jpeg-7/makljpeg.st +68 -0
- data/src/jpeg-7/maktjpeg.st +30 -0
- data/src/jpeg-7/makvms.opt +4 -0
- data/src/jpeg-7/missing +376 -0
- data/src/jpeg-7/rdbmp.c +439 -0
- data/src/jpeg-7/rdcolmap.c +253 -0
- data/src/jpeg-7/rdgif.c +38 -0
- data/src/jpeg-7/rdjpgcom.1 +63 -0
- data/src/jpeg-7/rdjpgcom.c +515 -0
- data/src/jpeg-7/rdppm.c +459 -0
- data/src/jpeg-7/rdrle.c +387 -0
- data/src/jpeg-7/rdswitch.c +365 -0
- data/src/jpeg-7/rdtarga.c +500 -0
- data/src/jpeg-7/structure.txt +945 -0
- data/src/jpeg-7/testimg.bmp +0 -0
- data/src/jpeg-7/testimg.jpg +0 -0
- data/src/jpeg-7/testimg.ppm +4 -0
- data/src/jpeg-7/testimgp.jpg +0 -0
- data/src/jpeg-7/testorig.jpg +0 -0
- data/src/jpeg-7/testprog.jpg +0 -0
- data/src/jpeg-7/transupp.c +1533 -0
- data/src/jpeg-7/transupp.h +205 -0
- data/src/jpeg-7/usage.txt +605 -0
- data/src/jpeg-7/wizard.txt +211 -0
- data/src/jpeg-7/wrbmp.c +442 -0
- data/src/jpeg-7/wrgif.c +399 -0
- data/src/jpeg-7/wrjpgcom.1 +103 -0
- data/src/jpeg-7/wrjpgcom.c +583 -0
- data/src/jpeg-7/wrppm.c +269 -0
- data/src/jpeg-7/wrrle.c +305 -0
- data/src/jpeg-7/wrtarga.c +253 -0
- metadata +287 -6
- data/LICENSE +0 -20
- data/VERSION.yml +0 -4
@@ -0,0 +1,593 @@
|
|
1
|
+
/*
|
2
|
+
This is a Optical-Character-Recognition program
|
3
|
+
Copyright (C) 2000-2008 Joerg Schulenburg
|
4
|
+
|
5
|
+
This program is free software; you can redistribute it and/or
|
6
|
+
modify it under the terms of the GNU General Public License
|
7
|
+
as published by the Free Software Foundation; either version 2
|
8
|
+
of the License, or (at your option) any later version.
|
9
|
+
|
10
|
+
This program is distributed in the hope that it will be useful,
|
11
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
GNU General Public License for more details.
|
14
|
+
|
15
|
+
You should have received a copy of the GNU General Public License
|
16
|
+
along with this program; if not, write to the Free Software
|
17
|
+
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
18
|
+
|
19
|
+
see README for email address
|
20
|
+
|
21
|
+
ToDo:
|
22
|
+
- transform special xml bar code symbols (<>&) to xml symbols (<>&)
|
23
|
+
|
24
|
+
*/
|
25
|
+
|
26
|
+
#include <stdlib.h>
|
27
|
+
#include <stdio.h>
|
28
|
+
#include <string.h>
|
29
|
+
#include "pgm2asc.h"
|
30
|
+
#include "gocr.h"
|
31
|
+
#include "pnm.h"
|
32
|
+
|
33
|
+
#ifndef DO_DEBUG /* can be defined outside (configure --with-debug) */
|
34
|
+
#define DO_DEBUG 0 /* 0 is the default */
|
35
|
+
#endif
|
36
|
+
|
37
|
+
#undef g_debug
|
38
|
+
#if DO_DEBUG
|
39
|
+
# define g_debug(a) a
|
40
|
+
#else
|
41
|
+
# define g_debug(a)
|
42
|
+
#endif
|
43
|
+
|
44
|
+
/*
|
45
|
+
detect barcode and add a XML-string to the box (obj-pointer, ToDo)
|
46
|
+
ToDo: barcode-output stderr->stdout
|
47
|
+
*/
|
48
|
+
|
49
|
+
double sqr(double x) { return(x*x); }
|
50
|
+
|
51
|
+
/* ----------------------------- code128 ---------------------------- *
|
52
|
+
* "BSBSBS", B=Bar, S=Space, better using 2*6=12bit-integer? */
|
53
|
+
#define Num128 107
|
54
|
+
const char *code128[Num128+1]={ /* can be generated by an algorithm? */
|
55
|
+
/* 00 */"212222","222122","222221","121223","121322","131222","122213","122312",
|
56
|
+
/* 08 */"132212","221213","221312","231212","112232","122132","122231","113222",
|
57
|
+
/* 16 */"123122","123221","223211","221132","221231","213212","223112","312131",
|
58
|
+
/* 24 */"311222","321122","321221","312212","322112","322211","212123","212321",
|
59
|
+
/* 32 */"232121","111323","131123","131321","112313","132113","132311","211313",
|
60
|
+
/* 40 */"231113","231311","112133","112331","132131","113123","113321","133121",
|
61
|
+
/* 48 */"313121","211331","231131","213113","213311","213131","311123","311321",
|
62
|
+
/* 56 */"331121","312113","312311","332111","314111","221411","431111","111224",
|
63
|
+
/* 64 */"111422","121124","121421","141122","141221","112214","112412","122114",
|
64
|
+
/* 72 */"122411","142112","142211","241211","221114","413111","241112","134111",
|
65
|
+
/* 80 */"111242","121142","121241","114212","124112","124211","411212","421112",
|
66
|
+
/* 88 */"421211","212141","214121","412121","111143","111341","131141","114113",
|
67
|
+
/* 96 */"114311","411113","411311","113141","114131","311141","411131","211412",
|
68
|
+
/*104 */"211214","211232","2331112","???"};
|
69
|
+
|
70
|
+
/*
|
71
|
+
code128: see code128.tex by Petr Olsak (108 codes)
|
72
|
+
quiet_zone: size=10 (before and after code128)
|
73
|
+
num_bars=3*(start+chars[N]+crc+stop)+1
|
74
|
+
B=bar S=space char=BSBSBS (size=11), stop=BSBSBSB (size=11+2)
|
75
|
+
Width: Bar,Space=1,[2,3,4] char=11 code=11*(N+3)+2 sumB=even,sumS=odd
|
76
|
+
startA="211412"=103 startB="211214"=104 startC="211232"=105(2dec_digits)
|
77
|
+
mode/code 0..95 96 97 98 99 100 101 102 103 104 105 106
|
78
|
+
1=A x20-x5f,0-x20 F3 F2 uB mC mB F4 F1 ^A ^B ^C $
|
79
|
+
2=B x20-x7f F3 F2 uA mC F4 mA F1 ^A ^B ^C $
|
80
|
+
3=C "00"-"95" "96" "97" "98" "99" mB mA F1 ^A ^B ^C $
|
81
|
+
uA,uB: switch mode for next char, mA,mB: switch mode permanently
|
82
|
+
crc=(start+1*char1+2*char2+3*char3+...+N*charN) mod 103
|
83
|
+
$=stop="2331112"=106 (4bars, with=13) => start/end==211
|
84
|
+
return num of chars or string
|
85
|
+
|
86
|
+
size B+W even-variants odd-variants num_codes
|
87
|
+
11 = 8+3 = (1+3+4,2+2+4,2+3+3)+(1+1+1) => (6+3+3)*(1) = 12
|
88
|
+
= 6+5 = (1+1+4,1+2+3,2+2+2)+(1+1+3,1+2+2) => (3+6+1)*(3+3) = 60
|
89
|
+
= 4+7 = (1+1+2)+(1+2+4,1+3+3,2+2+3) => (3)*(6+3+3) = 36
|
90
|
+
sum = 108
|
91
|
+
*/
|
92
|
+
/* example: barcode -E -e 128c -b 1434600120000884 >a.eps */
|
93
|
+
/* example: barcode -E -e 128b -b 14Test41 >a.eps */
|
94
|
+
/* example: barcode -E -e 128raw -b 105 17 14 30 >a.eps */
|
95
|
+
|
96
|
+
char *decode_code128(int *wb, int num_bars){
|
97
|
+
int i, w, i1, i2, i3=0, i4, i5=0, crc, mode=1;
|
98
|
+
double dww, dw, err, min_err; char cc, *buf;
|
99
|
+
char *result=NULL; /* malloc and store the result */
|
100
|
+
|
101
|
+
for(w=i=0;i<2*num_bars-1;i++) w+=wb[i]; /* summ all bars and spaces */
|
102
|
+
|
103
|
+
/* test code128 characteristics, ToDo: look for correct start/stop 211 seq. */
|
104
|
+
if ((num_bars-1)%3!=0 || num_bars<10 || w<11*(num_bars-1)/3+2)
|
105
|
+
return 0;
|
106
|
+
g_debug(fprintf(stderr,"\n code128 b%d s%d b%d",wb[0],wb[1],wb[2]);)
|
107
|
+
if (3*wb[0]<4* wb[1]
|
108
|
+
|| 3*wb[0]<4* wb[2]
|
109
|
+
|| 4*wb[0]<3*(wb[1]+wb[2])
|
110
|
+
|| 3*wb[0]>4*(wb[1]+wb[2])) return 0; /* 211 */
|
111
|
+
dw=3.0*w/((num_bars-1)*11+6);
|
112
|
+
/* get enough memory for all digits in longest mode C */
|
113
|
+
buf =(char *) malloc( (num_bars-7)/3*2+1); if (!buf) return result;
|
114
|
+
result=(char *) malloc(256+(num_bars-7)/3*2+1);
|
115
|
+
|
116
|
+
dww=crc=0;
|
117
|
+
for(i4=i1=0;i1<(num_bars-1)/3;i1++) {
|
118
|
+
for(min_err=1e8,i3=Num128,i5=0;i5<Num128;i5++){ /* get best fit */
|
119
|
+
for(err=i2=0;i2<6;i2++) err+=sqr(code128[i5][i2]-'0'-wb[i1*6+i2]/dw);
|
120
|
+
if (err<min_err) { min_err=err; i3=i5; }
|
121
|
+
} dww+=min_err;
|
122
|
+
g_debug(fprintf(stderr,"\n %7s %3d err=%.3f ",code128[i3],i3,min_err);)
|
123
|
+
if(i3<Num128){ /* valid symbol */
|
124
|
+
if(i1==0){ if (i3>102 && i3<106) mode=i3-103+1; crc=i3; } /* start */
|
125
|
+
if(i1>0 && i1<(num_bars-1)/3-2){
|
126
|
+
crc+=i3*(i1); cc=0; /* first * 1 + second * 2 + third * 3 ... */
|
127
|
+
i5=((mode>3)?mode>>2:mode&3); mode&=3; /* mode can be modified now */
|
128
|
+
switch (i5) { /* mode=1..3=modeA..modeC */
|
129
|
+
case 1: if (i3>=64 && i3<96) cc=i3-64; /* modeA: x20-x5f,0-x20 */
|
130
|
+
else cc=i3+32;
|
131
|
+
if (i3==101) mode=1; /* switch to mode A */
|
132
|
+
if (i3== 99) mode=3; /* switch to mode C */
|
133
|
+
if (i3== 98) mode|=2<<2; /* shift to mode B */
|
134
|
+
break;
|
135
|
+
case 2: cc=i3+32; /* modeB: x20-x7f */
|
136
|
+
if (i3==100) mode=2; /* switch to mode B */
|
137
|
+
if (i3== 99) mode=3; /* switch to mode C */
|
138
|
+
if (i3== 98) mode|=1<<2; /* shift to mode A */
|
139
|
+
break;
|
140
|
+
case 3:
|
141
|
+
if (i3==101) mode=1; /* switch to mode A */
|
142
|
+
if (i3==100) mode=2; /* switch to mode B */
|
143
|
+
}
|
144
|
+
if (i5==3) { buf[i4]='0'+i3/10; i4++;
|
145
|
+
buf[i4]='0'+i3%10; i4++; } /* modeC: two digits */
|
146
|
+
else {
|
147
|
+
if (cc>=0x20 && i3<=0x7f) { buf[i4]=cc; i4++; } /* modeA+B: one digit */
|
148
|
+
if (cc>=0 && cc< 0x20) { buf[i4]='^'; i4++;
|
149
|
+
buf[i4]=cc+'@'; i4++; }
|
150
|
+
}
|
151
|
+
}
|
152
|
+
if(i1==(num_bars-1)/3-2){ crc=(crc+103-i3)%103; }
|
153
|
+
if(i1==(num_bars-1)/3-1){ if(i3!=106) i3=-1; } /* stop code */
|
154
|
+
mode &= 3; /* remove shift */
|
155
|
+
}
|
156
|
+
else fprintf(stderr," %s=%02d? ",buf,i5);
|
157
|
+
}
|
158
|
+
buf[i4]=0; /* end of string */
|
159
|
+
if (result)
|
160
|
+
sprintf(result,"<barcode type=\"128\" chars=\"%d\" code=\"%s\" "
|
161
|
+
"crc=\"%d\" error=\"%.3f\" />",
|
162
|
+
i4,buf,crc,dww/((num_bars-1)));
|
163
|
+
free(buf);
|
164
|
+
return result;
|
165
|
+
}
|
166
|
+
|
167
|
+
/* -------------------------------------------------------------------- UPC
|
168
|
+
EAN 13 (UPC,(1+6+1+6+1)*2bars,size=3+6*7+5+6*7+3=95)
|
169
|
+
EAN 8 (UPC,(1+4+1+4+1)*2bars,size=3+4*7+5+4*7+3=67)
|
170
|
+
UPC: (10 codes)
|
171
|
+
BSB SBSB^n SBSBS BSBS^n BSB
|
172
|
+
bsb ...... sbsbs ...... bsb
|
173
|
+
111 ...... 11111 ...... 111
|
174
|
+
num_bars=2*(2*6+3) middle=SBSBS=11111 right/left=BSB=111="101"
|
175
|
+
char: left=SBSB right=BSBS (size=7) only_dec_digits
|
176
|
+
SS+BB = (S+S) + (B+B) => BB:SS = 5:2 or 3:4
|
177
|
+
size ev+odd even + odd => variants
|
178
|
+
7 = 2 + 5 = (1+1) + (1+4,2+3) => (1)*(2+2) = 4 codes
|
179
|
+
= 4 + 3 = (1+3,2+2) + (1+3) => (2+1)*(2) = 6 codes += 10 codes
|
180
|
+
ToDo: make it more robust
|
181
|
+
- return error as mean deviation
|
182
|
+
* -------------------------------------------------------------------- */
|
183
|
+
/* example: barcode -E -e upc -b 12345678901 >a.eps # ok */
|
184
|
+
/* example: barcode -E -e ean -b 123456789012 >a.eps # ok */
|
185
|
+
#define NumUPC 20
|
186
|
+
const char *codeUPC[NumUPC+1]={ /* 0..9, first n = SBSB, last n = BSBS */
|
187
|
+
"3211","2221","2122","1411","1132", /* 0,1,2,3,4 normal (+0bit) */
|
188
|
+
"1231","1114","1312","1213","3112", /* 5,6,7,8,9 */
|
189
|
+
"1123","1222","2212","1141","2311", /* 0,1,2,3,4 mirrored (+1bit) */
|
190
|
+
"1321","4111","2131","3121","2113", /* 5,6,7,8,9 */
|
191
|
+
"????"}; /* not found */
|
192
|
+
|
193
|
+
char *decode_UPC(int *wb, int num_bars){ /* ToDo: char *dest, int len */
|
194
|
+
int i, w, i1, i2, i3, i4, i5, crc, mirrored, ean;
|
195
|
+
double err, min_err, dw, dww=0.0; char digit;
|
196
|
+
char *result=NULL, *buf=NULL; /* malloc and store the result */
|
197
|
+
for(w=i=0;i<2*num_bars-1;i++) w+=wb[i];
|
198
|
+
|
199
|
+
dw=2.0*w/((num_bars-6)*7+2*11); /* or min(wb[]) */
|
200
|
+
crc=0;
|
201
|
+
if ((num_bars)%2!=0 || num_bars<10 || w<7*(num_bars-6)/2+11
|
202
|
+
|| ((num_bars-6)/2)%2!=0) return 0; /* should be balanced */
|
203
|
+
/* check front BSB, middle SBSBS and end BSB */
|
204
|
+
dww=0;
|
205
|
+
for (i=0;i<3;i++) { dww=sqr(wb[i ]/dw-1); if (dww>0.4) return 0; }
|
206
|
+
for (i=0;i<5;i++) { dww=sqr(wb[i+ num_bars-3]/dw-1); if (dww>0.4) return 0; }
|
207
|
+
for (i=0;i<3;i++) { dww=sqr(wb[i+2*num_bars-4]/dw-1); if (dww>0.4) return 0; }
|
208
|
+
buf =(char *)malloc( (num_bars-6)/2+1); if (!buf) return result;
|
209
|
+
result=(char *)malloc(256+(num_bars-6)/2+1);
|
210
|
+
|
211
|
+
for(ean=i5=0,i1=3;i1<2*num_bars-4;i1+=4) { /* each digit (2bars+2spaces) */
|
212
|
+
if (i1==num_bars-3) { i1++; continue; } /* skip middle sync SBSBS */
|
213
|
+
for (i4=NumUPC,mirrored=0,digit='?',min_err=16e8,i2=0;i2<NumUPC;i2++) {
|
214
|
+
for (err=0,i3=0;i3<4;i3++) err+=sqr(codeUPC[i2][i3]-'0'-wb[i1+i3]/dw);
|
215
|
+
if (err<min_err) { min_err=err; i4=i2; digit='0'+i2%10; mirrored=i2/10; }
|
216
|
+
} dww+=min_err; crc+=(digit-'0')*((i5&1)?1:3); /* even*3+odd, last char is even */
|
217
|
+
buf[i5++]=digit; if (i5<7) ean=(ean<<1)|mirrored;
|
218
|
+
/* ToDo: error as deviation wb from ideal */
|
219
|
+
g_debug(fprintf(stderr,"\nDBG: UPC digit=%c mirrored=%d err=%.3f err_m=%.3f ",
|
220
|
+
digit,mirrored,min_err/4,dww/(i5*4));)
|
221
|
+
}
|
222
|
+
/* EAN has a 13th leading digit build by 3 of 6 mirorred digits */
|
223
|
+
if (ean & 0x20) ean^=0x3f; /* UPC-E mirrored 1xxxxx => 0yyyyy */
|
224
|
+
switch (ean) {
|
225
|
+
case 11: ean=1; break;
|
226
|
+
case 13: ean=2; break;
|
227
|
+
case 14: ean=3; break;
|
228
|
+
case 19: ean=4; break;
|
229
|
+
case 25: ean=5; break;
|
230
|
+
case 28: ean=6; break;
|
231
|
+
case 21: ean=7; break;
|
232
|
+
case 22: ean=8; break;
|
233
|
+
case 26: ean=9; break;
|
234
|
+
default: ean=0; /* no or invalid EAN digit or UPC-extension */
|
235
|
+
} crc+=ean*1;
|
236
|
+
/* ToDo: fix possible buffer OVL, complement crc */
|
237
|
+
buf[i5]=0;
|
238
|
+
if (result)
|
239
|
+
sprintf(result,"<barcode type=\"UPC\" chars=\"%d\" code=\"%d%s\" "
|
240
|
+
"crc=\"%d\" error=\"%.3f\" />",
|
241
|
+
i5+1,ean,buf,(10-crc%10)%10,dww/((num_bars-6)*2));
|
242
|
+
free(buf);
|
243
|
+
return result;
|
244
|
+
}
|
245
|
+
|
246
|
+
/* EAN/UPC add-on is either 2 or 5 digits. It always starts with a
|
247
|
+
* guard bar BSB, followed by ([digit + SB] * (N-1)) + digit. Digit is
|
248
|
+
* SBSB. Two digit add-on's have 7 bars, and 5 digit add ons have 16.
|
249
|
+
*/
|
250
|
+
char *decode_UPC_addon(int *wb, int num_bars){ /* ToDo: char *dest, int len */
|
251
|
+
int i, w, i1, i2, i3, i4, i5, digits=num_bars/3;
|
252
|
+
double err, min_err, dw, dww=0.0; char digit;
|
253
|
+
char *result=NULL, *buf=NULL; /* malloc and store the result */
|
254
|
+
if (num_bars!=7 && num_bars!=16)
|
255
|
+
return 0;
|
256
|
+
for(w=i=0;i<2*num_bars-1;i++) w+=wb[i];
|
257
|
+
|
258
|
+
dw=1.0*w/(digits*7+4 + (digits-1)*2);
|
259
|
+
/* check front BSB, and delineators SB */
|
260
|
+
dww=0;
|
261
|
+
for (i=0;i<2;i++) { dww=sqr(wb[i]/dw-1); if (dww>0.4) return 0; }
|
262
|
+
dww=sqr(wb[i]*0.5/dw-1); if (dww>0.4) return 0;
|
263
|
+
for (i=1;i<digits; i++) {
|
264
|
+
for (i1=0; i1<2; i1++) {
|
265
|
+
dww = sqr(wb[i*6 + 1 + i1]/dw-1);
|
266
|
+
if (dww > 0.4) return 0;
|
267
|
+
}
|
268
|
+
}
|
269
|
+
buf =(char *)malloc( digits+1); if (!buf) return result;
|
270
|
+
result=(char *)malloc(256+digits+1);
|
271
|
+
|
272
|
+
for(i5=0,i1=3;i1<2*num_bars-1;i1+=6) { /* each digit (2bars+2spaces) */
|
273
|
+
for (i4=NumUPC,digit='?',min_err=16e8,i2=0;i2<NumUPC;i2++) {
|
274
|
+
for (err=0,i3=0;i3<4;i3++) err+=sqr(codeUPC[i2][i3]-'0'-wb[i1+i3]/dw);
|
275
|
+
if (err<min_err) { min_err=err; i4=i2; digit='0'+i2%10; }
|
276
|
+
}
|
277
|
+
dww+=min_err;
|
278
|
+
buf[i5++]=digit;
|
279
|
+
/* ToDo: error as deviation wb from ideal */
|
280
|
+
g_debug(fprintf(stderr,"\nDBG: UPC digit=%c err=%.3f err_m=%.3f ",
|
281
|
+
digit, min_err/4, dww/(i5*4));)
|
282
|
+
}
|
283
|
+
buf[i5]=0;
|
284
|
+
if (result)
|
285
|
+
sprintf(result, "<barcode type=\"UPC_addon\" chars=\"%d\" code=\"%s\" "
|
286
|
+
"error=\"%.3f\" />",
|
287
|
+
i5, buf, dww/((num_bars-6)*2));
|
288
|
+
free(buf);
|
289
|
+
return result;
|
290
|
+
}
|
291
|
+
|
292
|
+
|
293
|
+
/* --------------------------------------------------------- *
|
294
|
+
* code 3 of 9, 3 thick of 9 bars
|
295
|
+
* BSBSBSBSB<S> size=7+3*aw aw=2(3), sumS/sumB=2/1?
|
296
|
+
* two widths: size=1 or size=2or3, digit_width=13(16)
|
297
|
+
* 5 bars and 4(+1) spaces per digit, last space is not used
|
298
|
+
* with 2 (or 0) thick bars, 1 (or 3) thick spaces per digit
|
299
|
+
* => 3 of 9 => max=9*8*7=504
|
300
|
+
* evenBB=(0of5)+(2of5) oddSS=(1of4)+(3of4) max=44
|
301
|
+
* ToDo: better code -...-.-.. as 046 or 083 (even,even,odd)
|
302
|
+
*/
|
303
|
+
#define Num39 (40+4) /* (3of9)=(2of5)(1of4)+(0of5)(3of4), (2of5)(.-..)=0..9 */
|
304
|
+
const char *code39= /* rearranged to BBBBBSSSS<S> (bars,spaces) */
|
305
|
+
"0..--..-.."
|
306
|
+
"1-...-.-..""2.-..-.-..""3--....-..""4..-.-.-..""5-.-...-.."
|
307
|
+
"6.--...-..""7...--.-..""8-..-..-..""9.-.-..-.."
|
308
|
+
"A-...-..-.""B.-..-..-.""C--.....-.""D..-.-..-.""E-.-....-."
|
309
|
+
"F.--....-.""G...--..-.""H-..-...-.""I.-.-...-.""J..--...-."
|
310
|
+
"K-...-...-""L.-..-...-""M--......-""N..-.-...-""O-.-.....-"
|
311
|
+
"P.--.....-""Q...--...-""R-..-....-""S.-.-....-""T..--....-"
|
312
|
+
"U-...--...""V.-..--...""W--...-...""X..-.--...""Y-.-..-..."
|
313
|
+
"Z.--..-...""-...---..."".-..-.-..."" .-.-.-...""*..--.-..."
|
314
|
+
/* (0of5)(3of4)=(.....)(3of4) store only 3of4? */
|
315
|
+
"$.....---.""/.....--.-""+.....-.--""%......---"
|
316
|
+
"?xxxxxxxxx";
|
317
|
+
|
318
|
+
/* example: barcode -E -e 39 -b 123abc | gs -sDEVICE=pnggray -r100 */
|
319
|
+
|
320
|
+
/* return index[] according to sorted values[], big first */
|
321
|
+
void sort(int *value, int *idx, int len){
|
322
|
+
int i,j;
|
323
|
+
for (j=0;j<len;j++) idx[j]=j; /* initialize */
|
324
|
+
for(i=1;i;) { /* bubble sort, len...len^2 steps */
|
325
|
+
for(i=j=0;j<len-1;j++) if(value[idx[j]]<value[idx[j+1]]) {
|
326
|
+
i=idx[j]; idx[j]=idx[j+1]; idx[j+1]=i; i=1;
|
327
|
+
}
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
char *decode_39(int *wb, int num_bars){ /* ToDo: char *dest, int len */
|
332
|
+
int i, w, i1, i3, i5, crc, idx[10];
|
333
|
+
double dw,dww,err; char *buf;
|
334
|
+
char *result=NULL; /* malloc and store the result */
|
335
|
+
|
336
|
+
/* check for multiple of 5 bars and minimum start+1char+stop=15 bars */
|
337
|
+
if ((num_bars)%5!=0 || num_bars<15) return 0;
|
338
|
+
for(w=i=0; i<2*num_bars-1;i++ ) w+=wb[i]; /* summ width to w */
|
339
|
+
dw=w*1.0/(16*(num_bars/5)); /* threshold = 1.5..2 */
|
340
|
+
/* whats best and most rigorosely for dw=threshold_width?
|
341
|
+
* - (1.5..2)*mean_width of every 5th space
|
342
|
+
* - (1.5..2)*summ(5bars+5spaces)/(13..16)
|
343
|
+
* - 3/4*summ(three thickest)/3
|
344
|
+
*/
|
345
|
+
dww=crc=0; /* error and checksum (not supported yet) */
|
346
|
+
#if 0 /* should we exclude any non-standard code39? */
|
347
|
+
/* check for correct start and end symbol * or NwNnWnWnN Narrow+Wide */
|
348
|
+
i=2*num_bars-2;
|
349
|
+
if (wb[ 0]>dw*2 || wb[ 1]<=dw*2 || wb[ 2]> dw*2) return 0;
|
350
|
+
if (wb[i-0]>dw*2 || wb[i-1]> dw*2 || wb[i-2]<=dw*2) return 0;
|
351
|
+
#endif
|
352
|
+
g_debug(fprintf(stderr," code39 base=%.3f chars=%2d\n ",dw,(num_bars)/5);)
|
353
|
+
buf =(char *)malloc( 1+(num_bars)/5); if (!buf) return result;
|
354
|
+
result=(char *)malloc(256+(num_bars)/5);
|
355
|
+
|
356
|
+
for(i5=i1=0;i1<2*num_bars-3;i1+=10) {
|
357
|
+
/* ToDo: looking for three widest bars/gaps */
|
358
|
+
sort(wb+i1,idx,9);
|
359
|
+
for(err=0,i3=3;i3<9;i3++) err+=sqr(wb[i1+idx[i3]]/dw-1.0); /* narrow=1, wide=2..3 */
|
360
|
+
dww+=err;
|
361
|
+
for(i3=0;i3<Num39;i3++)
|
362
|
+
if (code39[10*i3+1+(idx[0]%2)*5+idx[0]/2]=='-'
|
363
|
+
&& code39[10*i3+1+(idx[1]%2)*5+idx[1]/2]=='-'
|
364
|
+
&& code39[10*i3+1+(idx[2]%2)*5+idx[2]/2]=='-') break;
|
365
|
+
if (i5>0 && i5<num_bars/5-2) crc+=i3;
|
366
|
+
buf[i5++]=code39[10*i3];
|
367
|
+
|
368
|
+
/* ToDo: check if wee have even number of black bars within 3 biggest */
|
369
|
+
g_debug(for(i3=0;i3<9;i3++)fprintf(stderr,"%02d ",wb[i1+i3]);
|
370
|
+
fprintf(stderr," posWide=%d,%d,%d %c err=%.3f\n ",
|
371
|
+
idx[0],idx[1],idx[2],buf[i5-1],err/6);)
|
372
|
+
}
|
373
|
+
buf[i5]=0;
|
374
|
+
if (result)
|
375
|
+
sprintf(result,"<barcode type=\"39\" chars=\"%d\" code=\"%s\" "
|
376
|
+
"crc=\"%c\" error=\"%.3f\" />",
|
377
|
+
i5,buf,code39[(crc%44)*10],dww/((num_bars/5)*6));
|
378
|
+
free(buf);
|
379
|
+
return result;
|
380
|
+
}
|
381
|
+
|
382
|
+
/* code interleaved 2 of 5
|
383
|
+
B B B B B <= digit 1,3,5,...
|
384
|
+
S S S S S <= digit 2,4,6,...
|
385
|
+
1 2 4 7 0 <= weight digit=sum(bit*weight)%11
|
386
|
+
*/
|
387
|
+
const char *code25= /* is the code sorted randomly? */
|
388
|
+
"1-...-2.-..-3--...4..-.-5-.-..6.--..7...--8-..-.9.-.-.0..--.";
|
389
|
+
|
390
|
+
/* example: barcode -E -e i25 -b 123456 >a.eps */
|
391
|
+
|
392
|
+
/* ToDo: decode_i25 (low priority) */
|
393
|
+
|
394
|
+
/*
|
395
|
+
decode barcode
|
396
|
+
- check main characteristics (num bars, min+max width, etc.)
|
397
|
+
- detect code type
|
398
|
+
- the current algorithm measures the width of bars and spaces
|
399
|
+
called by detect_barcode()
|
400
|
+
*/
|
401
|
+
char *decode_barcode(struct box *bb){ /* ToDo: char *dest, int len */
|
402
|
+
int i, num_bars, yy, w, ww, dx, xx, cs=JOB->cfg.cs, *wb;
|
403
|
+
char *result=NULL; /* store the result */
|
404
|
+
yy=(bb->y0+bb->y1)/2;
|
405
|
+
w=ww=bb->x1-bb->x0+1;
|
406
|
+
num_bars = num_cross(bb->x0,bb->x1,yy,yy,bb->p,JOB->cfg.cs);
|
407
|
+
if(JOB->cfg.verbose)
|
408
|
+
fprintf(stderr,"\n# ... detect bars=%3d w=%4d",num_bars,ww);
|
409
|
+
|
410
|
+
/* store width of bars and spaces to buffer wb */
|
411
|
+
wb=(int *)malloc(2*num_bars*sizeof(int)); if(!wb) return NULL;
|
412
|
+
xx=bb->x0;
|
413
|
+
xx-=loop(bb->p,xx,yy, 8,cs,1,LE);
|
414
|
+
xx+=loop(bb->p,xx,yy,ww,cs,0,RI); /* start with a bar! */
|
415
|
+
for (i=0;i<2*num_bars;i++) {
|
416
|
+
dx=loop(bb->p,xx,yy,w,cs,1^(i&1),RI);
|
417
|
+
xx+=dx;
|
418
|
+
w-=dx;
|
419
|
+
wb[i]=dx;
|
420
|
+
} wb[2*num_bars-1]=0;
|
421
|
+
|
422
|
+
/* ToDo: what about probability? if not unique
|
423
|
+
* - add argument char *result which can be modified or not,
|
424
|
+
* - or add box2? (would reuse of this code more difficult)
|
425
|
+
*/
|
426
|
+
/* test code128 characteristics, ToDo: look for correct start/stop 211 seq. */
|
427
|
+
if ((num_bars-1)%3==0 && num_bars>=10 && ww>=11*(num_bars-1)/3+2){
|
428
|
+
if (!result) result=decode_code128(wb,num_bars);
|
429
|
+
}
|
430
|
+
/* test UPC/EAN characteristics */
|
431
|
+
if ((num_bars)%2==0 && num_bars>=8 && ww>=7*(num_bars-6)/2+11
|
432
|
+
&& ((num_bars-6)/2)%2==0){ /* should be balanced */
|
433
|
+
if (!result) result=decode_UPC(wb,num_bars);
|
434
|
+
}
|
435
|
+
/* test UPC_addon by Michael van Rooyen, often on books */
|
436
|
+
if (num_bars==7 || num_bars==16)
|
437
|
+
if (!result) result=decode_UPC_addon(wb,num_bars);
|
438
|
+
|
439
|
+
/* test code39 characteristics */
|
440
|
+
if ((num_bars)%5==0 && num_bars>14){
|
441
|
+
if (!result) result=decode_39(wb,num_bars);
|
442
|
+
}
|
443
|
+
free(wb);
|
444
|
+
return result;
|
445
|
+
}
|
446
|
+
|
447
|
+
/*
|
448
|
+
* taking the list of boxes and search for groups of bars (1D-barcodes)
|
449
|
+
*/
|
450
|
+
int detect_barcode(job_t *job)
|
451
|
+
{
|
452
|
+
int j=0, j2=0, bx0, by0, bx1, by1, bdx, bdy, bbx, rm,
|
453
|
+
x0, y0, dx, dy, cs, y, yl0, yl1, yr0, yr1;
|
454
|
+
struct box *box2, *box3;
|
455
|
+
|
456
|
+
if(JOB->cfg.verbose)
|
457
|
+
fprintf(stderr,"# barcode.c detect_barcode ");
|
458
|
+
x0=y0=0; rm=0; dx=job->src.p.x; dy=job->src.p.y; cs=JOB->cfg.cs;
|
459
|
+
for_each_data(&(JOB->res.boxlist)) {
|
460
|
+
box2 = (struct box *)list_get_current(&(JOB->res.boxlist));
|
461
|
+
/* detect width (bdx) and height (bdy) of possible bar */
|
462
|
+
/* ToDo: better check for a line */
|
463
|
+
bdx=box2->x1-box2->x0+1 /* substract correction for skewed bars */
|
464
|
+
-loop(box2->p,box2->x1,(box2->y0+box2->y1)/2,box2->x1-box2->x0,cs,0,LE)
|
465
|
+
-loop(box2->p,box2->x0,(box2->y0+box2->y1)/2,box2->x1-box2->x0,cs,0,RI);
|
466
|
+
bdy=box2->y1-box2->y0+1;
|
467
|
+
if (box2->c == PICTURE || box2->c == UNKNOWN)
|
468
|
+
if (box2->y0 >= y0 && box2->y1 <= y0 + dy /* within frame? */
|
469
|
+
&& box2->x0 >= x0 && box2->x1 <= x0 + dx
|
470
|
+
&& box2->y1 - box2->y0 > 19 /* min. length */
|
471
|
+
&& box2->y1 - box2->y0 > 8 * bdx
|
472
|
+
) { /* a bar? */
|
473
|
+
j=1; /* number of bars */
|
474
|
+
bx0=box2->x0; bx1=box2->x1; /* initial values for barcode frame */
|
475
|
+
by0=box2->y0; by1=box2->y1;
|
476
|
+
bbx=bx1-bx0+2; /* width of bar */
|
477
|
+
/* this is for scans which are not exactly horizontal */
|
478
|
+
yl0=yr0=by0; /* left and right upper bound */
|
479
|
+
yl1=yr1=by1; /* left and right lower bound */
|
480
|
+
/* --- iteratively take into account next nearest bar ---
|
481
|
+
* this is important, because bar-boxes are not in right order */
|
482
|
+
for (j2=1;j2;) {
|
483
|
+
j2=0;
|
484
|
+
/* expand a frame around the potential barcode (bx0,by0,bx1,by1) */
|
485
|
+
for_each_data(&(JOB->res.boxlist)) {
|
486
|
+
box3 = (struct box *)list_get_current(&(JOB->res.boxlist));
|
487
|
+
/* bdy=box3->y1-box3->y0+1; */
|
488
|
+
if (box2!=box3)
|
489
|
+
if (box3->c == PICTURE || box3->c == UNKNOWN)
|
490
|
+
if (box3->y0 >= y0 && box3->y1 <= y0 + dy /* within image */
|
491
|
+
&& box3->x0 >= x0 && box3->x1 <= x0 + dx
|
492
|
+
&& box3->y1 - box3->y0 > 19 /* min. length */
|
493
|
+
&& box3->y1 - box3->y0 > 4 * (bdx) /* height > 4*width2 */
|
494
|
+
&& box3->x1 - box3->x0 < 4 * (bdy) /* width < height/4 = bar */
|
495
|
+
&& (( abs(box3->y0-by0)<bdy/16+4 /* within bar-box ? */
|
496
|
+
&& abs(box3->y1-by1)<bdy/2 ) /* lower ends of UPC could be longer */
|
497
|
+
||( abs(box3->y0-yl0)<bdy/16+4 /* left side of frame */
|
498
|
+
&& abs(box3->y1-yl1)<bdy/2
|
499
|
+
&& box3->x0 <= bx0 )
|
500
|
+
||( abs(box3->y0-yr0)<bdy/16+4 /* right side of frame */
|
501
|
+
&& abs(box3->y1-yr1)<bdy/2
|
502
|
+
&& box3->x0 >= bx1 ) )
|
503
|
+
&& box3->x0 > bx0 - 12*bbx /* startspace=5...10 */
|
504
|
+
&& box3->x1 < bx1 + 12*bbx
|
505
|
+
&& box3->x0 > bx0 - bdy/2 /* dont glue two barcodes together */
|
506
|
+
&& box3->x1 < bx1 + bdy/2 /* ex: ean13a.jpg */
|
507
|
+
/* dont check bars which already within the frame twice ? */
|
508
|
+
&& ( box3->x1 > bx1 || box3->x0 < bx0 )
|
509
|
+
) { /* a bar? -> extend barcode frame only in x direction */
|
510
|
+
/* take minimum of y to have valid barcode for all y */
|
511
|
+
if (box3->x0<bx0) { bx0=box3->x0; yl0=box3->y0; yl1=box3->y1; }
|
512
|
+
if (box3->x1>bx1) { bx1=box3->x1; yr0=box3->y0; yr1=box3->y1; }
|
513
|
+
if (4*(box3->y1-box3->y0)>3*(by1-by0)) { /* carefull reduce */
|
514
|
+
if (box3->y0>by0) by0=box3->y0; /* ToDo: fix for non-horizontal */
|
515
|
+
if (box3->y1<by1) by1=box3->y1;
|
516
|
+
}
|
517
|
+
j++; /* found a near bar and count to num bars */
|
518
|
+
j2=1; /* continue searching (endless loop?) */
|
519
|
+
}
|
520
|
+
} end_for_each(&(JOB->res.boxlist));
|
521
|
+
}
|
522
|
+
/* j is the num of bars found above, some inner bars are not counted */
|
523
|
+
/* ToDo: better iterative add next nearest bars from sorted list near bars? */
|
524
|
+
if (j>5) {
|
525
|
+
char *code=0;
|
526
|
+
box2->c=PICTURE; /* BARCODE */
|
527
|
+
box2->x0=bx0; box2->y0=by0;
|
528
|
+
box2->x1=bx1; box2->y1=by1;
|
529
|
+
/* ToDo: add pointer to decoded text */
|
530
|
+
|
531
|
+
y=(box2->y0+box2->y1)/2;
|
532
|
+
if (JOB->cfg.verbose){
|
533
|
+
int nbars;
|
534
|
+
nbars=num_cross(box2->x0,box2->x1,y,y,box2->p,JOB->cfg.cs);
|
535
|
+
fprintf(stderr,"\n# barcode at %3d %3d size %3d %3d nbars %d (%d)",
|
536
|
+
bx0,by0,bx1-bx0+1,by1-by0+1,nbars,j);
|
537
|
+
if (j!=nbars)
|
538
|
+
fprintf(stderr,"\n# ... trouble: num_found_bars != num_cross");
|
539
|
+
/* this is because some far bars are detected before near bars */
|
540
|
+
}
|
541
|
+
/* transport the info to the gocr-output (development) */
|
542
|
+
/* ToDo: decode and print/store barcode bars=j */
|
543
|
+
code=decode_barcode(box2); /* ToDo: char *dest, int len */
|
544
|
+
if (!code) { /* failed */
|
545
|
+
code=(char *)malloc(128);
|
546
|
+
/* ToDo: analyze and output num_bars, width of bars etc. */
|
547
|
+
if(code) strncpy(code,"<barcode type=\"unknown\" />",128);
|
548
|
+
}
|
549
|
+
if (JOB->cfg.verbose)
|
550
|
+
fprintf(stderr,"\n# ... decoded as: %s", code);
|
551
|
+
setas(box2,code,99); /* ToDo: set a better weight */
|
552
|
+
free(code);
|
553
|
+
|
554
|
+
/* remove inner boxes, only if sure!? (ToDo: use cfg.certainty) */
|
555
|
+
for_each_data(&(JOB->res.boxlist)) {
|
556
|
+
box3 = (struct box *)list_get_current(&(JOB->res.boxlist));
|
557
|
+
/* bdy=box3->y1-box3->y0+1; */
|
558
|
+
if (box2!=box3)
|
559
|
+
if (box3->c == PICTURE || box3->c == UNKNOWN)
|
560
|
+
if ( abs(box3->y0-by0)<bdy/16+4 /* within bar-box ? */
|
561
|
+
&& abs(box3->y1-by1)<bdy/2 /* lower ends of UPC could be longer */
|
562
|
+
&& box3->x1 <= bx1
|
563
|
+
&& box3->x0 >= bx0
|
564
|
+
) {
|
565
|
+
rm++; /* count removed boxes */
|
566
|
+
list_del(&(JOB->res.boxlist),box3);
|
567
|
+
free_box(box3);
|
568
|
+
}
|
569
|
+
} end_for_each(&(JOB->res.boxlist));
|
570
|
+
if (JOB->cfg.verbose)
|
571
|
+
fprintf(stderr,"\n# ... removed boxes: %d", rm);
|
572
|
+
rm=0;
|
573
|
+
}
|
574
|
+
}
|
575
|
+
} end_for_each(&(JOB->res.boxlist));
|
576
|
+
|
577
|
+
/* recalculate averages without bars */
|
578
|
+
JOB->res.numC=JOB->res.sumX=JOB->res.sumY=j2=0;
|
579
|
+
for_each_data(&(JOB->res.boxlist)) {
|
580
|
+
j2++;
|
581
|
+
box3 = (struct box *)list_get_current(&(JOB->res.boxlist));
|
582
|
+
if (box3->c==PICTURE) continue;
|
583
|
+
JOB->res.numC++; /* count remaining boxes */
|
584
|
+
JOB->res.sumX+=box3->x1-box3->x0+1;
|
585
|
+
JOB->res.sumY+=box3->y1-box3->y0+1;
|
586
|
+
} end_for_each(&(JOB->res.boxlist));
|
587
|
+
if(JOB->cfg.verbose)
|
588
|
+
fprintf(stderr,"\n# ... boxes %d nC %d\n",
|
589
|
+
j2, JOB->res.numC);
|
590
|
+
|
591
|
+
return 0;
|
592
|
+
}
|
593
|
+
|