isbn 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (291) hide show
  1. data/.gitignore +4 -0
  2. data/README +9 -0
  3. data/Rakefile +13 -0
  4. data/VERSION +1 -0
  5. data/isbn.gemspec +329 -0
  6. data/lib/isbn.rb +90 -0
  7. data/src/gocr-0.48/.cvsignore +6 -0
  8. data/src/gocr-0.48/AUTHORS +7 -0
  9. data/src/gocr-0.48/BUGS +55 -0
  10. data/src/gocr-0.48/CREDITS +17 -0
  11. data/src/gocr-0.48/HISTORY +243 -0
  12. data/src/gocr-0.48/INSTALL +83 -0
  13. data/src/gocr-0.48/Makefile +193 -0
  14. data/src/gocr-0.48/Makefile.in +193 -0
  15. data/src/gocr-0.48/README +165 -0
  16. data/src/gocr-0.48/READMEde.txt +80 -0
  17. data/src/gocr-0.48/REMARK.txt +18 -0
  18. data/src/gocr-0.48/REVIEW +538 -0
  19. data/src/gocr-0.48/TODO +65 -0
  20. data/src/gocr-0.48/bin/.cvsignore +2 -0
  21. data/src/gocr-0.48/bin/create_db +38 -0
  22. data/src/gocr-0.48/bin/gocr.tcl +527 -0
  23. data/src/gocr-0.48/bin/gocr_chk.sh +44 -0
  24. data/src/gocr-0.48/configure +4689 -0
  25. data/src/gocr-0.48/configure.in +71 -0
  26. data/src/gocr-0.48/doc/.#Makefile.1.6 +39 -0
  27. data/src/gocr-0.48/doc/.cvsignore +2 -0
  28. data/src/gocr-0.48/doc/Makefile +39 -0
  29. data/src/gocr-0.48/doc/Makefile.in +39 -0
  30. data/src/gocr-0.48/doc/example.dtd +53 -0
  31. data/src/gocr-0.48/doc/example.xml +21 -0
  32. data/src/gocr-0.48/doc/examples.txt +67 -0
  33. data/src/gocr-0.48/doc/gocr.html +578 -0
  34. data/src/gocr-0.48/doc/unicode.txt +57 -0
  35. data/src/gocr-0.48/examples/.#Makefile.1.22 +166 -0
  36. data/src/gocr-0.48/examples/4x6.png +0 -0
  37. data/src/gocr-0.48/examples/4x6.txt +2 -0
  38. data/src/gocr-0.48/examples/5x7.png +0 -0
  39. data/src/gocr-0.48/examples/5x7.png.txt +2 -0
  40. data/src/gocr-0.48/examples/5x8.png +0 -0
  41. data/src/gocr-0.48/examples/5x8.png.txt +2 -0
  42. data/src/gocr-0.48/examples/Makefile +166 -0
  43. data/src/gocr-0.48/examples/color.fig +20 -0
  44. data/src/gocr-0.48/examples/ex.fig +16 -0
  45. data/src/gocr-0.48/examples/font.tex +22 -0
  46. data/src/gocr-0.48/examples/font1.tex +46 -0
  47. data/src/gocr-0.48/examples/font2.fig +27 -0
  48. data/src/gocr-0.48/examples/font_nw.tex +24 -0
  49. data/src/gocr-0.48/examples/handwrt1.jpg +0 -0
  50. data/src/gocr-0.48/examples/handwrt1.txt +10 -0
  51. data/src/gocr-0.48/examples/inverse.fig +20 -0
  52. data/src/gocr-0.48/examples/matrix.jpg +0 -0
  53. data/src/gocr-0.48/examples/ocr-a-subset.png +0 -0
  54. data/src/gocr-0.48/examples/ocr-a-subset.png.txt +4 -0
  55. data/src/gocr-0.48/examples/ocr-a.png +0 -0
  56. data/src/gocr-0.48/examples/ocr-a.txt +6 -0
  57. data/src/gocr-0.48/examples/ocr-b.png +0 -0
  58. data/src/gocr-0.48/examples/ocr-b.png.txt +4 -0
  59. data/src/gocr-0.48/examples/polish.tex +28 -0
  60. data/src/gocr-0.48/examples/rotate45.fig +14 -0
  61. data/src/gocr-0.48/examples/score +36 -0
  62. data/src/gocr-0.48/examples/text.tex +28 -0
  63. data/src/gocr-0.48/gocr.spec +143 -0
  64. data/src/gocr-0.48/gpl.html +537 -0
  65. data/src/gocr-0.48/include/.cvsignore +2 -0
  66. data/src/gocr-0.48/include/config.h +36 -0
  67. data/src/gocr-0.48/include/config.h.in +36 -0
  68. data/src/gocr-0.48/include/version.h +2 -0
  69. data/src/gocr-0.48/install-sh +3 -0
  70. data/src/gocr-0.48/make.bat +57 -0
  71. data/src/gocr-0.48/man/.cvsignore +2 -0
  72. data/src/gocr-0.48/man/Makefile +29 -0
  73. data/src/gocr-0.48/man/Makefile.in +29 -0
  74. data/src/gocr-0.48/man/man1/gocr.1 +166 -0
  75. data/src/gocr-0.48/src/.cvsignore +4 -0
  76. data/src/gocr-0.48/src/Makefile +132 -0
  77. data/src/gocr-0.48/src/Makefile.in +132 -0
  78. data/src/gocr-0.48/src/amiga.h +31 -0
  79. data/src/gocr-0.48/src/barcode.c +846 -0
  80. data/src/gocr-0.48/src/barcode.c.orig +593 -0
  81. data/src/gocr-0.48/src/barcode.h +11 -0
  82. data/src/gocr-0.48/src/box.c +372 -0
  83. data/src/gocr-0.48/src/database.c +462 -0
  84. data/src/gocr-0.48/src/detect.c +943 -0
  85. data/src/gocr-0.48/src/gocr.c +373 -0
  86. data/src/gocr-0.48/src/gocr.h +288 -0
  87. data/src/gocr-0.48/src/jconv.c +168 -0
  88. data/src/gocr-0.48/src/job.c +84 -0
  89. data/src/gocr-0.48/src/lines.c +350 -0
  90. data/src/gocr-0.48/src/list.c +334 -0
  91. data/src/gocr-0.48/src/list.h +90 -0
  92. data/src/gocr-0.48/src/ocr0.c +6756 -0
  93. data/src/gocr-0.48/src/ocr0.h +63 -0
  94. data/src/gocr-0.48/src/ocr0n.c +1475 -0
  95. data/src/gocr-0.48/src/ocr1.c +85 -0
  96. data/src/gocr-0.48/src/ocr1.h +3 -0
  97. data/src/gocr-0.48/src/otsu.c +289 -0
  98. data/src/gocr-0.48/src/otsu.h +23 -0
  99. data/src/gocr-0.48/src/output.c +289 -0
  100. data/src/gocr-0.48/src/output.h +37 -0
  101. data/src/gocr-0.48/src/pcx.c +153 -0
  102. data/src/gocr-0.48/src/pcx.h +9 -0
  103. data/src/gocr-0.48/src/pgm2asc.c +2893 -0
  104. data/src/gocr-0.48/src/pgm2asc.h +105 -0
  105. data/src/gocr-0.48/src/pixel.c +537 -0
  106. data/src/gocr-0.48/src/pnm.c +533 -0
  107. data/src/gocr-0.48/src/pnm.h +35 -0
  108. data/src/gocr-0.48/src/progress.c +87 -0
  109. data/src/gocr-0.48/src/progress.h +42 -0
  110. data/src/gocr-0.48/src/remove.c +703 -0
  111. data/src/gocr-0.48/src/tga.c +87 -0
  112. data/src/gocr-0.48/src/tga.h +6 -0
  113. data/src/gocr-0.48/src/unicode.c +1314 -0
  114. data/src/gocr-0.48/src/unicode.h +1257 -0
  115. data/src/jpeg-7/Makefile.am +133 -0
  116. data/src/jpeg-7/Makefile.in +1089 -0
  117. data/src/jpeg-7/README +322 -0
  118. data/src/jpeg-7/aclocal.m4 +8990 -0
  119. data/src/jpeg-7/ansi2knr.1 +36 -0
  120. data/src/jpeg-7/ansi2knr.c +739 -0
  121. data/src/jpeg-7/cderror.h +132 -0
  122. data/src/jpeg-7/cdjpeg.c +181 -0
  123. data/src/jpeg-7/cdjpeg.h +187 -0
  124. data/src/jpeg-7/change.log +270 -0
  125. data/src/jpeg-7/cjpeg.1 +325 -0
  126. data/src/jpeg-7/cjpeg.c +616 -0
  127. data/src/jpeg-7/ckconfig.c +402 -0
  128. data/src/jpeg-7/coderules.txt +118 -0
  129. data/src/jpeg-7/config.guess +1561 -0
  130. data/src/jpeg-7/config.sub +1686 -0
  131. data/src/jpeg-7/configure +17139 -0
  132. data/src/jpeg-7/configure.ac +317 -0
  133. data/src/jpeg-7/depcomp +630 -0
  134. data/src/jpeg-7/djpeg.1 +251 -0
  135. data/src/jpeg-7/djpeg.c +617 -0
  136. data/src/jpeg-7/example.c +433 -0
  137. data/src/jpeg-7/filelist.txt +215 -0
  138. data/src/jpeg-7/install-sh +520 -0
  139. data/src/jpeg-7/install.txt +1097 -0
  140. data/src/jpeg-7/jaricom.c +148 -0
  141. data/src/jpeg-7/jcapimin.c +282 -0
  142. data/src/jpeg-7/jcapistd.c +161 -0
  143. data/src/jpeg-7/jcarith.c +921 -0
  144. data/src/jpeg-7/jccoefct.c +453 -0
  145. data/src/jpeg-7/jccolor.c +459 -0
  146. data/src/jpeg-7/jcdctmgr.c +482 -0
  147. data/src/jpeg-7/jchuff.c +1612 -0
  148. data/src/jpeg-7/jcinit.c +65 -0
  149. data/src/jpeg-7/jcmainct.c +293 -0
  150. data/src/jpeg-7/jcmarker.c +667 -0
  151. data/src/jpeg-7/jcmaster.c +770 -0
  152. data/src/jpeg-7/jcomapi.c +106 -0
  153. data/src/jpeg-7/jconfig.bcc +48 -0
  154. data/src/jpeg-7/jconfig.cfg +45 -0
  155. data/src/jpeg-7/jconfig.dj +38 -0
  156. data/src/jpeg-7/jconfig.mac +43 -0
  157. data/src/jpeg-7/jconfig.manx +43 -0
  158. data/src/jpeg-7/jconfig.mc6 +52 -0
  159. data/src/jpeg-7/jconfig.sas +43 -0
  160. data/src/jpeg-7/jconfig.st +42 -0
  161. data/src/jpeg-7/jconfig.txt +155 -0
  162. data/src/jpeg-7/jconfig.vc +45 -0
  163. data/src/jpeg-7/jconfig.vms +37 -0
  164. data/src/jpeg-7/jconfig.wat +38 -0
  165. data/src/jpeg-7/jcparam.c +632 -0
  166. data/src/jpeg-7/jcprepct.c +358 -0
  167. data/src/jpeg-7/jcsample.c +545 -0
  168. data/src/jpeg-7/jctrans.c +381 -0
  169. data/src/jpeg-7/jdapimin.c +396 -0
  170. data/src/jpeg-7/jdapistd.c +275 -0
  171. data/src/jpeg-7/jdarith.c +762 -0
  172. data/src/jpeg-7/jdatadst.c +151 -0
  173. data/src/jpeg-7/jdatasrc.c +212 -0
  174. data/src/jpeg-7/jdcoefct.c +736 -0
  175. data/src/jpeg-7/jdcolor.c +396 -0
  176. data/src/jpeg-7/jdct.h +393 -0
  177. data/src/jpeg-7/jddctmgr.c +382 -0
  178. data/src/jpeg-7/jdhuff.c +1309 -0
  179. data/src/jpeg-7/jdinput.c +384 -0
  180. data/src/jpeg-7/jdmainct.c +512 -0
  181. data/src/jpeg-7/jdmarker.c +1360 -0
  182. data/src/jpeg-7/jdmaster.c +663 -0
  183. data/src/jpeg-7/jdmerge.c +400 -0
  184. data/src/jpeg-7/jdpostct.c +290 -0
  185. data/src/jpeg-7/jdsample.c +361 -0
  186. data/src/jpeg-7/jdtrans.c +136 -0
  187. data/src/jpeg-7/jerror.c +252 -0
  188. data/src/jpeg-7/jerror.h +304 -0
  189. data/src/jpeg-7/jfdctflt.c +174 -0
  190. data/src/jpeg-7/jfdctfst.c +230 -0
  191. data/src/jpeg-7/jfdctint.c +4348 -0
  192. data/src/jpeg-7/jidctflt.c +242 -0
  193. data/src/jpeg-7/jidctfst.c +368 -0
  194. data/src/jpeg-7/jidctint.c +5137 -0
  195. data/src/jpeg-7/jinclude.h +91 -0
  196. data/src/jpeg-7/jmemansi.c +167 -0
  197. data/src/jpeg-7/jmemdos.c +638 -0
  198. data/src/jpeg-7/jmemdosa.asm +379 -0
  199. data/src/jpeg-7/jmemmac.c +289 -0
  200. data/src/jpeg-7/jmemmgr.c +1118 -0
  201. data/src/jpeg-7/jmemname.c +276 -0
  202. data/src/jpeg-7/jmemnobs.c +109 -0
  203. data/src/jpeg-7/jmemsys.h +198 -0
  204. data/src/jpeg-7/jmorecfg.h +369 -0
  205. data/src/jpeg-7/jpegint.h +395 -0
  206. data/src/jpeg-7/jpeglib.h +1135 -0
  207. data/src/jpeg-7/jpegtran.1 +272 -0
  208. data/src/jpeg-7/jpegtran.c +546 -0
  209. data/src/jpeg-7/jquant1.c +856 -0
  210. data/src/jpeg-7/jquant2.c +1310 -0
  211. data/src/jpeg-7/jutils.c +179 -0
  212. data/src/jpeg-7/jversion.h +14 -0
  213. data/src/jpeg-7/libjpeg.map +4 -0
  214. data/src/jpeg-7/libjpeg.txt +3067 -0
  215. data/src/jpeg-7/ltmain.sh +8406 -0
  216. data/src/jpeg-7/makcjpeg.st +36 -0
  217. data/src/jpeg-7/makdjpeg.st +36 -0
  218. data/src/jpeg-7/makeadsw.vc6 +77 -0
  219. data/src/jpeg-7/makeasln.vc9 +33 -0
  220. data/src/jpeg-7/makecdep.vc6 +82 -0
  221. data/src/jpeg-7/makecdsp.vc6 +130 -0
  222. data/src/jpeg-7/makecmak.vc6 +159 -0
  223. data/src/jpeg-7/makecvcp.vc9 +186 -0
  224. data/src/jpeg-7/makeddep.vc6 +82 -0
  225. data/src/jpeg-7/makeddsp.vc6 +130 -0
  226. data/src/jpeg-7/makedmak.vc6 +159 -0
  227. data/src/jpeg-7/makedvcp.vc9 +186 -0
  228. data/src/jpeg-7/makefile.ansi +220 -0
  229. data/src/jpeg-7/makefile.bcc +291 -0
  230. data/src/jpeg-7/makefile.dj +226 -0
  231. data/src/jpeg-7/makefile.manx +220 -0
  232. data/src/jpeg-7/makefile.mc6 +255 -0
  233. data/src/jpeg-7/makefile.mms +224 -0
  234. data/src/jpeg-7/makefile.sas +258 -0
  235. data/src/jpeg-7/makefile.unix +234 -0
  236. data/src/jpeg-7/makefile.vc +217 -0
  237. data/src/jpeg-7/makefile.vms +142 -0
  238. data/src/jpeg-7/makefile.wat +239 -0
  239. data/src/jpeg-7/makejdep.vc6 +423 -0
  240. data/src/jpeg-7/makejdsp.vc6 +285 -0
  241. data/src/jpeg-7/makejdsw.vc6 +29 -0
  242. data/src/jpeg-7/makejmak.vc6 +425 -0
  243. data/src/jpeg-7/makejsln.vc9 +17 -0
  244. data/src/jpeg-7/makejvcp.vc9 +328 -0
  245. data/src/jpeg-7/makeproj.mac +213 -0
  246. data/src/jpeg-7/makerdep.vc6 +6 -0
  247. data/src/jpeg-7/makerdsp.vc6 +78 -0
  248. data/src/jpeg-7/makermak.vc6 +110 -0
  249. data/src/jpeg-7/makervcp.vc9 +133 -0
  250. data/src/jpeg-7/maketdep.vc6 +43 -0
  251. data/src/jpeg-7/maketdsp.vc6 +122 -0
  252. data/src/jpeg-7/maketmak.vc6 +131 -0
  253. data/src/jpeg-7/maketvcp.vc9 +178 -0
  254. data/src/jpeg-7/makewdep.vc6 +6 -0
  255. data/src/jpeg-7/makewdsp.vc6 +78 -0
  256. data/src/jpeg-7/makewmak.vc6 +110 -0
  257. data/src/jpeg-7/makewvcp.vc9 +133 -0
  258. data/src/jpeg-7/makljpeg.st +68 -0
  259. data/src/jpeg-7/maktjpeg.st +30 -0
  260. data/src/jpeg-7/makvms.opt +4 -0
  261. data/src/jpeg-7/missing +376 -0
  262. data/src/jpeg-7/rdbmp.c +439 -0
  263. data/src/jpeg-7/rdcolmap.c +253 -0
  264. data/src/jpeg-7/rdgif.c +38 -0
  265. data/src/jpeg-7/rdjpgcom.1 +63 -0
  266. data/src/jpeg-7/rdjpgcom.c +515 -0
  267. data/src/jpeg-7/rdppm.c +459 -0
  268. data/src/jpeg-7/rdrle.c +387 -0
  269. data/src/jpeg-7/rdswitch.c +365 -0
  270. data/src/jpeg-7/rdtarga.c +500 -0
  271. data/src/jpeg-7/structure.txt +945 -0
  272. data/src/jpeg-7/testimg.bmp +0 -0
  273. data/src/jpeg-7/testimg.jpg +0 -0
  274. data/src/jpeg-7/testimg.ppm +4 -0
  275. data/src/jpeg-7/testimgp.jpg +0 -0
  276. data/src/jpeg-7/testorig.jpg +0 -0
  277. data/src/jpeg-7/testprog.jpg +0 -0
  278. data/src/jpeg-7/transupp.c +1533 -0
  279. data/src/jpeg-7/transupp.h +205 -0
  280. data/src/jpeg-7/usage.txt +605 -0
  281. data/src/jpeg-7/wizard.txt +211 -0
  282. data/src/jpeg-7/wrbmp.c +442 -0
  283. data/src/jpeg-7/wrgif.c +399 -0
  284. data/src/jpeg-7/wrjpgcom.1 +103 -0
  285. data/src/jpeg-7/wrjpgcom.c +583 -0
  286. data/src/jpeg-7/wrppm.c +269 -0
  287. data/src/jpeg-7/wrrle.c +305 -0
  288. data/src/jpeg-7/wrtarga.c +253 -0
  289. data/test/isbn_test.rb +7 -0
  290. data/test/test_helper.rb +7 -0
  291. metadata +345 -0
@@ -0,0 +1,105 @@
1
+ /*
2
+ This is a Optical-Character-Recognition program
3
+ Copyright (C) 2000-2006 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
+ */
22
+
23
+ #ifndef PGM2ASC_H
24
+ #define PGM2ASC_H 1
25
+
26
+ #include "pnm.h"
27
+ #include "output.h"
28
+ #include "list.h"
29
+ #include "unicode.h"
30
+
31
+ #define pixel_at(pic, xx, yy) (pic).p[(xx)+((yy)*((pic).x))]
32
+ #define pixel_atp(pic, xx, yy) (pic)->p[(xx)+((yy)*((pic)->x))]
33
+
34
+ #ifndef HAVE_WCHAR_H
35
+ wchar_t *wcschr (const wchar_t *wcs, wchar_t wc);
36
+ wchar_t *wcscpy (wchar_t *dest, const wchar_t *src);
37
+ size_t wcslen (const wchar_t *s);
38
+ #endif
39
+ #ifndef HAVE_WCSDUP
40
+ wchar_t * wcsdup (const wchar_t *WS); /* its a gnu extension */
41
+ #endif
42
+
43
+ /* declared in pgm2asc.c */
44
+ /* set alternate chars and its weight, called from the engine
45
+ if a char is recognized to (weight) percent */
46
+ int setas(struct box *b, char *as, int weight); /* string + xml */
47
+ int setac(struct box *b, wchar_t ac, int weight); /* wchar */
48
+
49
+ /* for qsort() call */
50
+ int intcompare (const void *vr, const void *vs);
51
+
52
+ /* declared in box.c */
53
+ int box_gt(struct box *box1, struct box *box2);
54
+ int reset_box_ac(struct box *box); /* reset and free char table */
55
+ struct box *malloc_box( struct box *inibox ); /* alloc memory for a box */
56
+ int free_box( struct box *box ); /* free memory of a box */
57
+ int copybox( pix *p, int x0, int y0, int dx, int dy, pix *b, int len);
58
+ int reduce_vectors ( struct box *box1, int mode );
59
+ int merge_boxes( struct box *box1, struct box *box2 );
60
+ int cut_box( struct box *box1);
61
+
62
+
63
+ /* declared in database.c */
64
+ int load_db(void);
65
+ wchar_t ocr_db(struct box *box1);
66
+
67
+ /* declared in detect.c */
68
+ int detect_lines1(pix * p, int x0, int y0, int dx, int dy);
69
+ int detect_lines2(pix *p,int x0,int y0,int dx,int dy,int r);
70
+ int detect_rotation_angle(job_t *job);
71
+ int detect_text_lines(pix * pp, int mo);
72
+ int adjust_text_lines(pix * pp, int mo);
73
+ int detect_pictures(job_t *job);
74
+
75
+ /* declared in lines.c */
76
+ void store_boxtree_lines( int mo );
77
+ /* free memory for internal stored textlines.
78
+ * Needs to be called _after_ having retrieved the text.
79
+ * After freeing, no call to getTextLine is possible any
80
+ * more
81
+ */
82
+ void free_textlines( void );
83
+
84
+ /* get result of ocr for a given line number.
85
+ * If the line is out of range, the function returns 0,
86
+ * otherwise a pointer to a complete line.
87
+ */
88
+ const char *getTextLine( int );
89
+
90
+ /* declared in remove.c */
91
+ int remove_dust( job_t *job );
92
+ int remove_pictures( job_t *job);
93
+ int remove_melted_serifs( pix *pp );
94
+ int remove_rest_of_dust();
95
+ int smooth_borders( job_t *job );
96
+
97
+ /* declared in pixel.c */
98
+ int marked(pix * p, int x, int y);
99
+ int pixel(pix *p, int x, int y);
100
+ void put(pix * p, int x, int y, int ia, int io);
101
+
102
+ /* start ocr on a image in job.src.p */
103
+ int pgm2asc(job_t *job);
104
+
105
+ #endif
@@ -0,0 +1,537 @@
1
+ /*
2
+ This is a Optical-Character-Recognition program
3
+ Copyright (C) 2000-2006 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
+ Joerg.Schulenburg@physik.uni-magdeburg.de */
20
+
21
+ /* Filter by tree, filter by number methods added by
22
+ * William Webber, william@williamwebber.com. */
23
+
24
+ #include "pgm2asc.h"
25
+ #include <assert.h>
26
+ #include <string.h>
27
+
28
+ /*
29
+ * Defining this causes assert() calls to be turned off runtime.
30
+ *
31
+ * This is normally taken care of by make.
32
+ */
33
+ /* #define NDEBUG */
34
+
35
+ // ------------------ (&~7)-pixmap-functions ------------------------
36
+
37
+ /* test if pixel marked?
38
+ * Returns: 0 if not marked, least 3 bits if marked.
39
+ */
40
+ int marked (pix * p, int x, int y) {
41
+ if (x < 0 || y < 0 || x >= p->x || y >= p->y)
42
+ return 0;
43
+ return (pixel_atp(p, x, y) & 7);
44
+ }
45
+
46
+ #define Nfilt3 6 /* number of 3x3 filter */
47
+ /*
48
+ * Filters to correct possible scanning or image errors.
49
+ *
50
+ * Each of these filters represents a 3x3 pixel area.
51
+ * 0 represents a white or background pixel, 1 a black or
52
+ * foreground pixel, and 2 represents a pixel of either value.
53
+ * Note that this differs from the meaning of pixel values in
54
+ * the image, where a high value means "white" (background),
55
+ * and a low value means "black" (foreground).
56
+ *
57
+ * These filters are applied to the 3x3 environment of a pixel
58
+ * to be retrieved from the image, centered around that pixel
59
+ * (that is, the to-be-retrieved pixel corresponds with the
60
+ * the fifth position of the filter).
61
+ * If the filter matches that pixel environment, then
62
+ * the returned value of the pixel is inverted (black->white
63
+ * or white->black).
64
+ *
65
+ * So, for instance, the second filter below matches this
66
+ * pattern:
67
+ *
68
+ * 000
69
+ * X0X
70
+ * 000
71
+ *
72
+ * and "fills in" the middle (retrieved) pixel to rejoin a line
73
+ * that may have been broken by a scanning or image error.
74
+ */
75
+ const char filt3[Nfilt3][9]={
76
+ {0,0,0, 0,0,1, 1,0,0}, /* (-1,-1) (0,-1) (1,-1) (-1,0) (0,0) ... */
77
+ {0,0,0, 1,0,1, 0,0,0},
78
+ {1,0,0, 0,0,1, 0,0,0},
79
+ {1,1,0, 0,1,0, 2,1,1},
80
+ {0,0,1, 0,0,0, 2,1,0},
81
+ {0,1,0, 0,0,0, 1,2,0}
82
+ };
83
+ /* 2=ignore_pixel, 0=white_background, 1=black_pixel */
84
+
85
+
86
+ /*
87
+ * Filter by matrix uses the above matrix of filters directly. Pixel
88
+ * environments to be filtered are compared pixel by pixel against
89
+ * these filters.
90
+ *
91
+ * Filter by number converts these filters into integer representations
92
+ * and stores them in a table. Pixel environments are similarly
93
+ * converted to integers, and looked up in the table.
94
+ *
95
+ * Filter by tree converts these filters into a binary tree. Pixel
96
+ * environments are matched by traversing the tree.
97
+ *
98
+ * A typical performance ratio for these three methods is 20:9:7
99
+ * respectively (i.e., the tree method takes around 35% of the
100
+ * time of the matrix method).
101
+ */
102
+ #define FILTER_BY_MATRIX 0
103
+ #define FILTER_BY_NUMBER 1
104
+ #define FILTER_BY_TREE 2
105
+
106
+ #define FILTER_METHOD FILTER_BY_TREE
107
+
108
+ /*
109
+ * Defining FILTER_CHECKED causes filter results from either the tree
110
+ * or the number method to be checked against results of the other
111
+ * two methods to ensure correctness. This is for bug checking purposes
112
+ * only.
113
+ */
114
+ /* #define FILTER_CHECKED */
115
+
116
+ /*
117
+ * Defining FILTER_STATISTICS causes statistics to be kept on how many
118
+ * times the filters are tried, how many times a filter matches, and
119
+ * of these matches how many flip a black pixel to white, and how many
120
+ * the reverse. These statistics are printed to stderr at the end of
121
+ * the program run. Currently, statistics are only kept if the tree
122
+ * filter method is being used.
123
+ */
124
+ /* #define FILTER_STATISTICS */
125
+
126
+ #ifdef FILTER_STATISTICS
127
+ static int filter_tries = 0;
128
+ static int filter_matches = 0;
129
+ static int filter_blackened = 0;
130
+ static int filter_whitened = 0;
131
+ #endif
132
+
133
+ #ifdef FILTER_STATISTICS
134
+ void print_filter_stats() {
135
+ fprintf(stderr, "\n# Error filter statistics: tries %d, matches %d, "
136
+ "blackened %d, whitened %d\n",
137
+ filter_tries, filter_matches, filter_blackened, filter_whitened);
138
+ }
139
+ #endif
140
+
141
+ #if FILTER_METHOD == FILTER_BY_MATRIX || defined(FILTER_CHECKED)
142
+ /*
143
+ * Filter the pixel at (x,y) by directly applying the matrix.
144
+ */
145
+ int pixel_filter_by_matrix(pix * p, int x, int y) {
146
+ int i;
147
+ static char c33[9];
148
+ memset(c33, 0, sizeof(c33));
149
+ /* copy environment of a point (only highest bit)
150
+ bbg: FASTER now. It has 4 ifs less at least, 8 at most. */
151
+ if (x > 0) { c33[3] = pixel_atp(p,x-1, y )>>7;
152
+ if (y > 0) c33[0] = pixel_atp(p,x-1,y-1)>>7;
153
+ if (y+1 < p->y) c33[6] = pixel_atp(p,x-1,y+1)>>7;
154
+ }
155
+ if (x+1 < p->x) { c33[5] = pixel_atp(p,x+1, y )>>7;
156
+ if (y > 0) c33[2] = pixel_atp(p,x+1,y-1)>>7;
157
+ if (y+1 < p->y) c33[8] = pixel_atp(p,x+1,y+1)>>7;
158
+ }
159
+ if (y > 0) c33[1] = pixel_atp(p, x ,y-1)>>7;
160
+ c33[4] = pixel_atp(p, x , y )>>7;
161
+ if (y+1 < p->y) c33[7] = pixel_atp(p, x ,y+1)>>7;
162
+
163
+ /* do filtering */
164
+ for (i = 0; i < Nfilt3; i++)
165
+ if( ( (filt3[i][0]>>1) || c33[0]!=(1 & filt3[i][0]) )
166
+ && ( (filt3[i][1]>>1) || c33[1]!=(1 & filt3[i][1]) )
167
+ && ( (filt3[i][2]>>1) || c33[2]!=(1 & filt3[i][2]) )
168
+ && ( (filt3[i][3]>>1) || c33[3]!=(1 & filt3[i][3]) )
169
+ && ( (filt3[i][4]>>1) || c33[4]!=(1 & filt3[i][4]) )
170
+ && ( (filt3[i][5]>>1) || c33[5]!=(1 & filt3[i][5]) )
171
+ && ( (filt3[i][6]>>1) || c33[6]!=(1 & filt3[i][6]) )
172
+ && ( (filt3[i][7]>>1) || c33[7]!=(1 & filt3[i][7]) )
173
+ && ( (filt3[i][8]>>1) || c33[8]!=(1 & filt3[i][8]) ) ) {
174
+ return ((filt3[i][4])?JOB->cfg.cs:0);
175
+ }
176
+ return pixel_atp(p, x, y) & ~7;
177
+ }
178
+ #endif
179
+
180
+ #if FILTER_METHOD == FILTER_BY_NUMBER || defined(FILTER_CHECKED)
181
+
182
+ #define NUM_TABLE_SIZE 512 /* max value of 9-bit value */
183
+ /*
184
+ * Recursively generates entries in the number table for a matrix filter.
185
+ *
186
+ * gen_num_filt is the number representation of the matrix filter.
187
+ * This generation is handled recursively because this is the easiest
188
+ * way to handle 2 (either value) entries in the filter, which lead
189
+ * to 2 distinct entries in the number table (one for each alternate
190
+ * value).
191
+ */
192
+ void rec_generate_number_table(char * num_table, const char * filter,
193
+ int i, unsigned short gen_num_filt) {
194
+ if (i == 9) {
195
+ /* Invert the value of the number representation, to reflect the
196
+ * fact that the "white" is 0 in the filter, 1 (high) in the image. */
197
+ gen_num_filt = ~gen_num_filt;
198
+ gen_num_filt &= 0x01ff;
199
+ assert(gen_num_filt < NUM_TABLE_SIZE);
200
+ num_table[gen_num_filt] = 1;
201
+ } else {
202
+ if (filter[i] == 0 || filter[i] == 2)
203
+ rec_generate_number_table(num_table, filter, i + 1, gen_num_filt);
204
+ if (filter[i] == 1 || filter[i] == 2) {
205
+ gen_num_filt |= (1 << (8 - i));
206
+ rec_generate_number_table(num_table, filter, i + 1, gen_num_filt);
207
+ }
208
+ }
209
+ }
210
+
211
+ /*
212
+ * Filter the pixel at (x, y) using a number table.
213
+ *
214
+ * Each filter can be converted into a 9-bit representation, where
215
+ * filters containing 2 (either value) pixels are converted into
216
+ * a separate numerical representation for each pixel, where position
217
+ * i in the filter corresponds to bit i in the number. Each resulting
218
+ * numerical representation N is represented as a 1 value in the Nth
219
+ * position of a lookup table. A pixel's environment is converted in
220
+ * the same way to a numeric representation P, and that environment
221
+ * matches a filter if num_table[P] == 1.
222
+ */
223
+ int pixel_filter_by_number(pix * p, int x, int y) {
224
+ unsigned short val = 0;
225
+ static char num_table[NUM_TABLE_SIZE];
226
+ static int num_table_generated = 0;
227
+ if (!num_table_generated) {
228
+ int f;
229
+ memset(num_table, 0, sizeof(num_table));
230
+ for (f = 0; f < Nfilt3; f++)
231
+ rec_generate_number_table(num_table, filt3[f], 0, 0);
232
+ num_table_generated = 1;
233
+ }
234
+
235
+ /* calculate a numeric value for the 3x3 square around the pixel. */
236
+ if (x > 0) { val |= (pixel_atp(p,x-1, y )>>7) << (8 - 3);
237
+ if (y > 0) val |= (pixel_atp(p,x-1,y-1)>>7) << (8 - 0);
238
+ if (y+1 < p->y) val |= (pixel_atp(p,x-1,y+1)>>7) << (8 - 6);
239
+ }
240
+ if (x+1 < p->x) { val |= (pixel_atp(p,x+1, y )>>7) << (8 - 5);
241
+ if (y > 0) val |= (pixel_atp(p,x+1,y-1)>>7) << (8 - 2);
242
+ if (y+1 < p->y) val |= (pixel_atp(p,x+1,y+1)>>7) << (8 - 8);
243
+ }
244
+ if (y > 0) val |= (pixel_atp(p, x ,y-1)>>7) << (8 - 1);
245
+ val |= (pixel_atp(p, x , y )>>7) << (8 - 4);
246
+ if (y+1 < p->y) val |= (pixel_atp(p, x ,y+1)>>7) << (8 - 7);
247
+ assert(val < NUM_TABLE_SIZE);
248
+
249
+ if (num_table[val])
250
+ return (val & (1 << 4)) ? 0 : JOB->cfg.cs;
251
+ else
252
+ return pixel_atp(p, x, y) & ~7;
253
+ }
254
+ #endif
255
+
256
+ #if FILTER_METHOD == FILTER_BY_TREE || defined(FILTER_CHECKED)
257
+
258
+ #define TREE_ARRAY_SIZE 1024
259
+ /* 1+ number of nodes in a complete binary tree of height 10 */
260
+
261
+ /*
262
+ * Recursively generate a tree representation of a filter.
263
+ */
264
+ void rec_generate_tree(char * tree, const char * filter, int i, int n) {
265
+ assert(i >= 0 && i <= 9);
266
+ assert(n < TREE_ARRAY_SIZE);
267
+ if (i == 9) {
268
+ if (filter[4] == 0)
269
+ tree[n] = 2;
270
+ else
271
+ tree[n] = 1;
272
+ return;
273
+ }
274
+ /* first iteration has n == -1, does not set any values of the tree,
275
+ just to find whether to start to the left or the right */
276
+ if (n != -1)
277
+ tree[n] = 1;
278
+ if (filter[i] == 0)
279
+ rec_generate_tree(tree, filter, i + 1, n * 2 + 2);
280
+ else if (filter[i] == 1)
281
+ rec_generate_tree(tree, filter, i + 1, n * 2 + 3);
282
+ else {
283
+ rec_generate_tree(tree, filter, i + 1, n * 2 + 2);
284
+ rec_generate_tree(tree, filter, i + 1, n * 2 + 3);
285
+ }
286
+ }
287
+
288
+ /*
289
+ * Filter the pixel at (x, y) using the tree method.
290
+ *
291
+ * Each filter is represented by a single branch of a binary
292
+ * tree, except for filters contain "either value" entries, which
293
+ * bifurcate at that point in the branch. Each white pixel in the filter
294
+ * is a left branch in the tree, each black pixel a right branch. The
295
+ * final node of a branch indicates whether this filter turns a white
296
+ * pixel black, or a black one white.
297
+ *
298
+ * We match a pixel's environment against this tree by similarly
299
+ * using the pixels in that environment to traverse the tree. If
300
+ * we run out of nodes before getting to the end of a branch, then
301
+ * the environment doesn't match against any of the filters represented
302
+ * by the tree. Otherwise, we return the value specified by the
303
+ * final node.
304
+ *
305
+ * Since the total tree size, even including missing nodes, is small
306
+ * (2 ^ 10), we can use a standard array representation of a binary
307
+ * tree, where for the node tree[n], the left child is tree[2n + 2],
308
+ * and the right tree[2n + 3]. The only information we want
309
+ * from a non-leaf node is whether it exists (that is, is part of
310
+ * a filter-representing branch). We represent this with the value
311
+ * 1 at the node's slot in the array, the contrary by 0. For the
312
+ * leaf node, 0 again represents non-existence, 1 that the filter
313
+ * represented by this branch turns a black pixel white, and 2 a
314
+ * white pixel black.
315
+ */
316
+ int pixel_filter_by_tree(pix * p, int x, int y) {
317
+ static char tree[TREE_ARRAY_SIZE];
318
+ static int tree_generated = 0;
319
+ int n;
320
+ int pixel_val = pixel_atp(p, x, y) & ~7;
321
+ #ifdef FILTER_STATISTICS
322
+ static int registered_filter_stats = 0;
323
+ if (!registered_filter_stats) {
324
+ atexit(print_filter_stats);
325
+ registered_filter_stats = 1;
326
+ }
327
+ filter_tries++;
328
+ #endif /* FILTER_STATISTICS */
329
+ if (!tree_generated) {
330
+ int f;
331
+ memset(tree, 0, sizeof(tree));
332
+ for (f = 0; f < Nfilt3; f++) {
333
+ const char * filter = filt3[f];
334
+ rec_generate_tree(tree, filter, 0, -1);
335
+ }
336
+ tree_generated = 1;
337
+ }
338
+ n = -1;
339
+
340
+ /* Note that for the image, low is black, high is white, whereas
341
+ * for the filter, 0 is white, 1 is black. For the image, then,
342
+ * high (white) means go left, low (black) means go right. */
343
+
344
+ #define IS_BLACK(_dx,_dy) !(pixel_atp(p, x + (_dx), y + (_dy)) >> 7)
345
+ #define IS_WHITE(_dx,_dy) (pixel_atp(p, x + (_dx), y + (_dy)) >> 7)
346
+ #define GO_LEFT n = n * 2 + 2
347
+ #define GO_RIGHT n = n * 2 + 3
348
+ #define CHECK_NO_MATCH if (tree[n] == 0) return pixel_val
349
+
350
+ /* Top row */
351
+ if (y == 0) {
352
+ /* top 3 pixels off edge == black == right
353
+ n = 2 * (2 * (2 * -1 + 3) + 3) + 3 = 13 */
354
+ n = 13;
355
+ } else {
356
+ if (x == 0 || IS_BLACK(-1, -1))
357
+ GO_RIGHT;
358
+ else
359
+ GO_LEFT;
360
+
361
+ if (IS_WHITE(0, -1))
362
+ GO_LEFT;
363
+ else
364
+ GO_RIGHT;
365
+ CHECK_NO_MATCH;
366
+
367
+ if (x + 1 == p->x || IS_BLACK(+1, -1))
368
+ GO_RIGHT;
369
+ else
370
+ GO_LEFT;
371
+ CHECK_NO_MATCH;
372
+ }
373
+
374
+ /* Second row */
375
+ if (x == 0 || IS_BLACK(-1, 0))
376
+ GO_RIGHT;
377
+ else
378
+ GO_LEFT;
379
+ CHECK_NO_MATCH;
380
+
381
+ if (IS_WHITE(0, 0))
382
+ GO_LEFT;
383
+ else
384
+ GO_RIGHT;
385
+ CHECK_NO_MATCH;
386
+
387
+ if (x + 1 == p->x || IS_BLACK(+1, 0))
388
+ GO_RIGHT;
389
+ else
390
+ GO_LEFT;
391
+ CHECK_NO_MATCH;
392
+
393
+ /* bottom row */
394
+ if (y + 1 == p->y) {
395
+ /* bottom 3 pixels off edge == black == right
396
+ n' = 2 * (2 * (2n + 3) + 3) + 3
397
+ = 2 * (4n + 9) + 3
398
+ = 8n + 21 */
399
+ n = 8 * n + 21;
400
+ } else {
401
+ if (x == 0 || IS_BLACK(-1, +1))
402
+ GO_RIGHT;
403
+ else
404
+ GO_LEFT;
405
+ CHECK_NO_MATCH;
406
+
407
+ if (IS_WHITE(0, 1))
408
+ GO_LEFT;
409
+ else
410
+ GO_RIGHT;
411
+ CHECK_NO_MATCH;
412
+
413
+ if (x + 1 == p->x || IS_BLACK(+1, +1))
414
+ GO_RIGHT;
415
+ else
416
+ GO_LEFT;
417
+ }
418
+ assert(n < TREE_ARRAY_SIZE);
419
+ assert(tree[n] == 0 || tree[n] == 1 || tree[n] == 2);
420
+ CHECK_NO_MATCH;
421
+ #ifdef FILTER_STATISTICS
422
+ filter_matches++;
423
+ #endif
424
+ if (tree[n] == 1) {
425
+ #ifdef FILTER_STATISTICS
426
+ if (pixel_atp(p, x, y) < JOB->cfg.cs)
427
+ filter_whitened++;
428
+ #endif
429
+ return JOB->cfg.cs;
430
+ } else {
431
+ #ifdef FILTER_STATISTICS
432
+ if (pixel_atp(p, x, y) >= JOB->cfg.cs)
433
+ filter_blackened++;
434
+ #endif
435
+ return 0;
436
+ }
437
+ }
438
+ #endif /* FILTER_METHOD == FILTER_BY_TREE */
439
+
440
+ /*
441
+ * This simple filter attempts to correct "fax"-like scan errors.
442
+ */
443
+ int pixel_faxfilter(pix *p, int x, int y) {
444
+ int r; // filter
445
+ r = pixel_atp(p,x,y)&~7;
446
+ /* {2,2,2, 2,0,1, 2,1,0} */
447
+ if ((r&128) && (~pixel_atp(p,x+1, y )&128)
448
+ && (~pixel_atp(p, x ,y+1)&128)
449
+ && ( pixel_atp(p,x+1,y+1)&128))
450
+ r = 64; /* faxfilter */
451
+
452
+ else
453
+ /* {2,2,2, 1,0,2, 0,1,2} */
454
+ if ((r&128) && (~pixel_atp(p,x-1, y )&128)
455
+ && (~pixel_atp(p, x ,y+1)&128)
456
+ && ( pixel_atp(p,x-1,y+1)&128))
457
+ r = 64; /* faxfilter */
458
+ return r & ~7;
459
+ }
460
+
461
+ #ifdef FILTER_CHECKED
462
+ /*
463
+ * Print out the 3x3 environment of a pixel as a 9-bit binary.
464
+ *
465
+ * For debugging purposes only.
466
+ */
467
+ void print_pixel_env(FILE * out, pix * p, int x, int y) {
468
+ int x0, y0;
469
+ for (y0 = y - 1; y0 < y + 2; y0++) {
470
+ for (x0 = x - 1; x0 < x + 2; x0++) {
471
+ if (x0 < 0 || x0 >= p->x || y0 < 0 || y0 >= p->y)
472
+ fputc('?', out);
473
+ else if (pixel_atp(p, x0, y0) >> 7)
474
+ fputc('0', out);
475
+ else
476
+ fputc('1', out);
477
+ }
478
+ }
479
+ }
480
+ #endif
481
+
482
+ /* this function is heavily used
483
+ * test if pixel was set, remove low bits (marks) --- later with error-correction
484
+ * result depends on n_run, if n_run>0 filter are used
485
+ * Returns: pixel-color (without marks)
486
+ */
487
+ int getpixel(pix *p, int x, int y){
488
+ if ( x < 0 || y < 0 || x >= p->x || y >= p->y )
489
+ return 255 & ~7;
490
+
491
+ /* filter will be used only once later, when vectorization replaces pixel
492
+ * processing
493
+ */
494
+ if (JOB->tmp.n_run > 0) { /* use the filters (correction of errors) */
495
+ #if FILTER_METHOD == FILTER_BY_NUMBER
496
+ int pix = pixel_filter_by_number(p, x, y);
497
+ #ifdef FILTER_CHECKED
498
+ int pix2 = pixel_filter_by_matrix(p, x, y);
499
+ if (pix != pix2) {
500
+ fprintf(stderr,
501
+ "# BUG: pixel_filter: by number: %d; by matrix: %d, "
502
+ "by atp %d; env: ", pix, pix2, pixel_atp(p, x, y) & ~7);
503
+ print_pixel_env(stderr, p, x, y);
504
+ fputc('\n', stderr);
505
+ }
506
+ #endif /* FILTER_CHECKED */
507
+ return pix;
508
+ #elif FILTER_METHOD == FILTER_BY_MATRIX
509
+ return pixel_filter_by_matrix(p, x, y);
510
+ #elif FILTER_METHOD == FILTER_BY_TREE
511
+ int pix = pixel_filter_by_tree(p, x, y);
512
+ #ifdef FILTER_CHECKED
513
+ int pix2 = pixel_filter_by_matrix(p, x, y);
514
+ int pix3 = pixel_filter_by_number(p, x, y);
515
+ if (pix != pix2 || pix != pix3) {
516
+ fprintf(stderr,
517
+ "# BUG: pixel_filter: tree: %d; matrix: %d, "
518
+ "number: %d, atp %d; env: ", pix, pix2, pix3,
519
+ pixel_atp(p, x, y) & ~7);
520
+ print_pixel_env(stderr, p, x, y);
521
+ fputc('\n', stderr);
522
+ }
523
+ #endif /* FILTER_CHECKED */
524
+ return pix;
525
+ #else
526
+ #error FILTER_METHOD not defined
527
+ #endif /* FILTER_BY_NUMBER */
528
+ }
529
+
530
+ return (pixel_atp(p,x,y) & ~7);
531
+ }
532
+
533
+ /* modify pixel, test if out of range */
534
+ void put(pix * p, int x, int y, int ia, int io) {
535
+ if (x < p->x && x >= 0 && y >= 0 && y < p->y)
536
+ pixel_atp(p, x, y) = (pixel_atp(p, x, y) & ia) | io;
537
+ }