isbn 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,533 @@
1
+ /*
2
+ This is a Optical-Character-Recognition program
3
+ Copyright (C) 2000-2009 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
+ v0.1.0 initial version (stdin added)
22
+ v0.2.0 popen added
23
+ v0.2.7 review by Bruno Barberi Gnecco
24
+ v0.39 autoconf
25
+ v0.41 fix integer and heap overflow, change color output
26
+ v0.46 fix blank spaces problem in filenames
27
+ */
28
+
29
+ #include <stdlib.h>
30
+ #include <stdio.h>
31
+ #include <string.h>
32
+ #include <assert.h>
33
+ #ifdef HAVE_UNISTD_H
34
+ /* #include <unistd.h> */
35
+ #endif
36
+
37
+ /* Windows needs extra code to work fine, ^Z in BMP's will stop input else.
38
+ * I do not have any idea when this text mode will be an advantage
39
+ * but the MS community seems to like to do simple things in a complex way. */
40
+ #if defined(O_BINARY) && (defined(__WIN32) || defined(__WIN32__)\
41
+ || defined(__WIN64) || defined(__WIN64__) || defined(__MSDOS__))
42
+ # include <fcntl.h>
43
+ # define SET_BINARY(_f) do {if (!isatty(_f)) setmode (_f, O_BINARY);} while (0)
44
+ #else
45
+ # define SET_BINARY(f) (void)0
46
+ #endif
47
+
48
+ #include "pnm.h"
49
+ #ifdef HAVE_PAM_H
50
+ # include <pam.h>
51
+ # include <sys/types.h>
52
+ # include <sys/stat.h>
53
+ # include <fcntl.h>
54
+ #else
55
+ # include <ctype.h>
56
+ #endif
57
+
58
+ #define EE() fprintf(stderr,"\nERROR "__FILE__" L%d: ",__LINE__)
59
+ #define E0(x0) {EE();fprintf(stderr,x0 "\n"); }
60
+ #define F0(x0) {EE();fprintf(stderr,x0 "\n"); exit(1);}
61
+ #define F1(x0,x1) {EE();fprintf(stderr,x0 "\n",x1); exit(1);}
62
+
63
+ /*
64
+ * Weights to use for the different colours when converting a ppm
65
+ * to greyscale. These weights should sum to 1.0
66
+ *
67
+ * The below values have been chosen to reflect the fact that paper
68
+ * goes a reddish-yellow as it ages.
69
+ *
70
+ * v0.41: for better performance, we use integer instead of double
71
+ * this integer value divided by 1024 (2^10) gives the factor
72
+ */
73
+ #define PPM_RED_WEIGHT 511 /* .499 */
74
+ #define PPM_GREEN_WEIGHT 396 /* .387 */
75
+ #define PPM_BLUE_WEIGHT 117 /* .114 */
76
+
77
+ /*
78
+ feel free to expand this list of usable converting programs
79
+ Note 1: the last field must be NULL.
80
+ Note 2: "smaller" extensions must come later: ".pnm.gz" must come
81
+ before ".pnm".
82
+ calling external programs is a security risk
83
+ ToDo: for better security replace gzip by /usr/bin/gzip !
84
+ */
85
+ char *xlist[]={
86
+ ".pnm.gz", "gzip -cd", /* compressed pnm-files, gzip package */
87
+ ".pbm.gz", "gzip -cd",
88
+ ".pgm.gz", "gzip -cd",
89
+ ".ppm.gz", "gzip -cd",
90
+ ".pnm.bz2", "bzip2 -cd",
91
+ ".pbm.bz2", "bzip2 -cd",
92
+ ".pgm.bz2", "bzip2 -cd",
93
+ ".ppm.bz2", "bzip2 -cd",
94
+ ".jpg", "djpeg -gray -pnm", /* JPG/JPEG, jpeg package */
95
+ ".jpeg", "djpeg -gray -pnm",
96
+ ".gif", "giftopnm -image=all", /* GIF, netpbm package */
97
+ ".bmp", "bmptoppm",
98
+ ".tiff", "tifftopnm",
99
+ ".png", "pngtopnm", /* Portable Network Graphics (PNG) format */
100
+ ".ps", "pstopnm -stdout -portrait -pgm", /* postscript */
101
+ ".eps", "pstopnm -stdout -portrait -pgm", /* encapsulated postscript */
102
+ /* gs -sDEVICE=pgmraw -sOutputFile=- -g609x235 -r141x141 -q -dNOPAUSE */
103
+ ".fig", "fig2dev -L ppm -m 3", /* xfig files, transfig package */
104
+ NULL
105
+ };
106
+
107
+ /* return a pointer to command converting file to pnm or NULL */
108
+ char *testsuffix(char *name){
109
+ int i; char *rr;
110
+
111
+ for(i = 0; xlist[i] != NULL; i += 2 ) {
112
+ if((rr=strstr(name, xlist[i])) != NULL)
113
+ if(strlen(rr)==strlen(xlist[i])) /* handle *.eps.pbm correct */
114
+ return xlist[i+1];
115
+ }
116
+ return NULL;
117
+ }
118
+
119
+
120
+ char read_char(FILE *f1){ // filter #-comments
121
+ char c;
122
+ int m;
123
+ for(m=0;;){
124
+ c=fgetc(f1);
125
+ if( feof(f1) ) E0("read feof");
126
+ if( ferror(f1) ) F0("read ferror");
127
+ if( c == '#' ) { m = 1; continue; }
128
+ if( m == 0 ) return c;
129
+ if( c == '\n' ) m = 0;
130
+ }
131
+ }
132
+
133
+
134
+ /*
135
+ for simplicity only PAM of netpbm is used, the older formats
136
+ PBM, PGM and PPM can be handled implicitly by PAM routines (js05)
137
+ v0.43: return 1 if multiple file (hold it open), 0 otherwise
138
+ */
139
+ #ifdef HAVE_PAM_H
140
+ int readpgm(char *name, pix * p, int vvv) {
141
+ static FILE *fp=NULL;
142
+ static char *pip;
143
+ char magic1, magic2;
144
+ int i, j, sample, minv = 0, maxv = 0, eofP=0;
145
+ struct pam inpam;
146
+ tuple *tuplerow;
147
+
148
+ assert(p);
149
+
150
+ if (!fp) { // fp!=0 for multi-pnm and idx>0
151
+ /* open file; test if conversion is needed. */
152
+ if (name[0] == '-' && name[1] == '\0') {
153
+ fp = stdin;
154
+ SET_BINARY (fileno(fp)); // Windows needs it for correct work
155
+ }
156
+ else {
157
+ pip = testsuffix(name);
158
+ if (!pip) {
159
+ fp = fopen(name, "rb");
160
+ if (!fp)
161
+ F1("opening file %s", name);
162
+ }
163
+ else {
164
+ char *buf = (char *)malloc((strlen(pip)+strlen(name)+4));
165
+ sprintf(buf, "%s \"%s\"", pip, name); /* allow spaces in filename */
166
+ if (vvv) {
167
+ fprintf(stderr, "# popen( %s )\n", buf);
168
+ }
169
+ #ifdef HAVE_POPEN
170
+ /* potential security vulnerability, if name contains tricks */
171
+ /* example: gunzip -c dummy | rm -rf * */
172
+ /* windows needs "rb" for correct work, linux not, cygwin? */
173
+ /* ToDo: do you have better code to go arround this? */
174
+ #if defined(__WIN32) || defined(__WIN32__) || defined(__WIN64) || defined(__WIN64__)
175
+ fp = popen(buf, "rb"); /* ToDo: may fail, please report */
176
+ if (!fp) fp = popen(buf, "r"); /* 2nd try, the gnu way */
177
+ #else
178
+ fp = popen(buf, "r");
179
+ #endif
180
+ #else
181
+ F0("sorry, compile with HAVE_POPEN to use pipes");
182
+ #endif
183
+ if (!fp)
184
+ F1("opening pipe %s", buf);
185
+ free(buf);
186
+ }
187
+ }
188
+ }
189
+
190
+ /* netpbm 0.10.36 tries to write a comment to nonzero char** comment_p */
191
+ /* patch by C.P.Schmidt 21Nov06 */
192
+ memset (&inpam, 0, sizeof(inpam));
193
+
194
+ /* read pgm-header */
195
+ /* struct pam may change between netpbm-versions, causing problems? */
196
+ #ifdef PAM_STRUCT_SIZE /* ok for netpbm-10.35 */
197
+ /* new-and-better? but PAM_STRUCT_SIZE is not defined in netpbm-10.18 */
198
+ pnm_readpaminit(fp, &inpam, PAM_STRUCT_SIZE(tuple_type));
199
+ #else /* ok for netpbm-10.18 old-and-bad for new netpbms */
200
+ pnm_readpaminit(fp, &inpam, sizeof(inpam));
201
+ #endif
202
+
203
+ p->x = inpam.width;
204
+ p->y = inpam.height;
205
+ magic1=(inpam.format >> 8) & 255; /* 'P' for PNM,PAM */
206
+ magic2=(inpam.format ) & 255; /* '7' for PAM */
207
+ minv=inpam.maxval;
208
+ if (vvv) {
209
+ fprintf(stderr, "# readpam: format=0x%04x=%c%c h*w(d*b)=%d*%d(%d*%d)\n",
210
+ inpam.format, /* magic1*256+magic2 */
211
+ ((magic1>31 && magic1<127)?magic1:'.'),
212
+ ((magic2>31 && magic2<127)?magic2:'.'),
213
+ inpam.height,
214
+ inpam.width,
215
+ inpam.depth,
216
+ inpam.bytes_per_sample);
217
+ }
218
+ if ( (1.*(p->x*p->y))!=((1.*p->x)*p->y) )
219
+ F0("Error integer overflow");
220
+ if ( !(p->p = (unsigned char *)malloc(p->x*p->y)) )
221
+ F1("Error at malloc: p->p: %d bytes", p->x*p->y);
222
+ tuplerow = pnm_allocpamrow(&inpam);
223
+ for ( i=0; i < inpam.height; i++ ) {
224
+ pnm_readpamrow(&inpam, tuplerow); /* exit on error */
225
+ for ( j = 0; j < inpam.width; j++ ) {
226
+ if (inpam.depth>=3)
227
+ /* tuplerow is unsigned long (see pam.h sample) */
228
+ /* we expect 8bit or 16bit integers,
229
+ no overflow up to 32-10-2=20 bits */
230
+ sample
231
+ = ((PPM_RED_WEIGHT * tuplerow[j][0] + 511)>>10)
232
+ + ((PPM_GREEN_WEIGHT * tuplerow[j][1] + 511)>>10)
233
+ + ((PPM_BLUE_WEIGHT * tuplerow[j][2] + 511)>>10);
234
+ else
235
+ sample = tuplerow[j][0];
236
+ sample = 255 * sample / inpam.maxval; /* normalize to 8 bit */
237
+ p->p[i*inpam.width+j] = sample;
238
+ if (maxv<sample) maxv=sample;
239
+ if (minv>sample) minv=sample;
240
+ }
241
+ }
242
+ pnm_freepamrow(tuplerow);
243
+ pnm_nextimage(fp,&eofP);
244
+ if (vvv)
245
+ fprintf(stderr,"# readpam: min=%d max=%d eof=%d\n", minv, maxv, eofP);
246
+ p->bpp = 1;
247
+ if (eofP) {
248
+ if (!pip) fclose(fp);
249
+ #ifdef HAVE_POPEN
250
+ else pclose(fp); /* close pipe (v0.43) */
251
+ #endif
252
+ fp=NULL; return 0;
253
+ }
254
+ return 1; /* multiple image = concatenated pnm */
255
+ }
256
+
257
+ #else
258
+ /*
259
+ if PAM not installed, here is the fallback routine,
260
+ which is not so powerful but needs no dependencies from other libs
261
+ */
262
+ static int fread_num(char *buf, int bps, FILE *f1) {
263
+ int mode, j2, j3; char c1;
264
+ for (j2=0;j2<bps;j2++) buf[j2]=0; // initialize value to zero
265
+ for(mode=0;!feof(f1);){ // mod=0 means skip leading spaces, 1 scan digits
266
+ c1=read_char(f1);
267
+ if (isspace(c1)) { if (mode==0) continue; else break; }
268
+ mode=1; // digits scan mode
269
+ if( !isdigit(c1) ) F0("unexpected char");
270
+ for (j3=j2=0;j2<bps;j2++) { // multiply bps*bytes by 10
271
+ j3 = buf[j2]*10 + j3; // j3 is used as result and carry
272
+ buf[j2]=j3 & 255; j3>>=8;
273
+ }
274
+ buf[0] += c1-'0';
275
+ }
276
+ return 0;
277
+ }
278
+
279
+ /*
280
+ * read image file, used to read the OCR-image and database images,
281
+ * image file can be PBM/PGM/PPM in RAW or TEXT
282
+ * name: filename of image (input)
283
+ * p: pointer where to store the loaded image (input)
284
+ * vvv: verbose mode (input)
285
+ * return: 0=ok, 1=further image follows (multiple image), -1 on error
286
+ * this is the fall back routine if libpnm cant be used
287
+ */
288
+ int readpgm( char *name, pix *p, int vvv){
289
+ static char c1, c2; /* magic bytes, file type */
290
+ static char *pip; // static to survive multiple calls
291
+ int nx,ny,nc,mod,i,j; // buffer
292
+ static FILE *f1=NULL; // trigger read new file or multi image file
293
+ unsigned char *pic;
294
+ char buf[512];
295
+ int lx, ly, dx;
296
+ int bps=1; /* bytes per sample (0..255..65535...) */
297
+
298
+ if (!f1) { /* first of multiple image, on MultipleImageFiles c1 was read */
299
+ pip=NULL;
300
+ if (name[0]=='-' && name[1]==0) {
301
+ f1=stdin; /* is this correct ??? */
302
+ SET_BINARY (fileno(f1)); // Windows needs it for correct work
303
+ } else {
304
+ pip=testsuffix(name);
305
+ if (!pip) {
306
+ f1=fopen(name,"rb"); if (!f1) F1("opening file %s",name);
307
+ } else {
308
+ sprintf(buf,"%s \"%s\"",pip,name); /* ToDo: how to prevent OVL ? */
309
+ if (vvv) { fprintf(stderr,"# popen( %s )\n",buf); }
310
+ #ifdef HAVE_POPEN
311
+ #if defined(__WIN32) || defined(__WIN32__) || defined(__WIN64) || defined(__WIN64__)
312
+ f1 = popen(buf, "rb"); /* ToDo: may fail, please report */
313
+ if (!f1) f1 = popen(buf, "r"); /* 2nd try, the gnu way */
314
+ #else
315
+ f1=popen(buf,"r");
316
+ #endif
317
+ #else
318
+ F0("only PNM files supported (compiled without HAVE_POPEN)");
319
+ #endif
320
+ if (!f1) F1("opening pipe %s",buf);
321
+ }
322
+ }
323
+ c1=fgetc(f1); if (feof(f1)) { E0("unexpected EOF"); return -1; }
324
+ }
325
+ c2=fgetc(f1); if (feof(f1)) { E0("unexpected EOF"); return -1; }
326
+ // check the first two bytes of the PNM file
327
+ // PBM PGM PPM
328
+ // TXT P1 P2 P3
329
+ // RAW P4 P5 P6
330
+ if (c1!='P' || c2 <'1' || c2 >'6') {
331
+ fprintf(stderr,"\nread-PNM-error: file number is %2d,"
332
+ " position %ld", fileno(f1), ftell(f1));
333
+ fprintf(stderr,"\nread-PNM-error: bad magic bytes, expect 0x50 0x3[1-6]"
334
+ " but got 0x%02x 0x%02x", 255&c1, 255&c2);
335
+ if (f1) fclose(f1); f1=NULL; return(-1);
336
+ }
337
+ nx=ny=nc=0; if (c2=='4' || c2=='1') nc=1;
338
+ for(mod=0;((c2=='5' || c2=='2') && (mod&7)<6)
339
+ || ((c2=='6' || c2=='3') && (mod&7)<6)
340
+ || ((c2=='4' || c2=='1') && (mod&7)<4);)
341
+ { // mode: 0,2,4=[ |\t|\r|\n]
342
+ // 1=nx 3=ny 5=nc 8-13=#rem
343
+ c1=read_char(f1); // former: # mod|=8
344
+ if( (mod & 1)==0 ) // whitespaces
345
+ if( !isspace(c1) ) mod++;
346
+ if( (mod & 1)==1 ) {
347
+ if( !isdigit(c1) ) {
348
+ if( !isspace(c1) )F0("unexpected character");
349
+ mod++; }
350
+ else if(mod==1) nx=nx*10+c1-'0';
351
+ else if(mod==3) ny=ny*10+c1-'0';
352
+ else if(mod==5) nc=nc*10+c1-'0';
353
+ }
354
+ }
355
+ if(vvv)
356
+ fprintf(stderr,"# PNM P%c h*w=%d*%d c=%d head=%ld",c2,ny,nx,nc,ftell(f1));
357
+ if( c2=='4' && (nx&7)!=0 ){
358
+ /* nx=(nx+7)&~7;*/ if(vvv)fprintf(stderr," PBM2PGM nx %d",(nx+7)&~7);
359
+ }
360
+ if (nc>> 8) bps=2; // bytes per color and pixel
361
+ if (nc>>16) bps=3;
362
+ if (nc>>24) bps=4;
363
+ fflush(stdout);
364
+ if ( (1.*(nx*ny))!=((1.*nx)*ny) )
365
+ F0("Error integer overflow");
366
+ pic=(unsigned char *)malloc( nx*ny );
367
+ if(pic==NULL)F0("memory failed"); // no memory
368
+ for (i=0;i<nx*ny;i++) pic[i]=255; // init to white if reading fails
369
+ /* this is a slow but short routine for P1 to P6 formats */
370
+ if( c2=='5' || c2=='2' ) /* slow PGM-RAW/ASC read pixelwise */
371
+ for (i=0;i<nx*ny;i++) {
372
+ if (c2=='5') { if(bps!=(int)fread(buf,1,bps,f1)) {
373
+ fprintf(stderr," ERROR reading at head+%d*%d\n", bps, i); break; } }
374
+ else for (j=0;j<3;j++) fread_num(buf+j*bps, bps, f1);
375
+ pic[i]=buf[bps-1]; /* store the most significant byte */
376
+ }
377
+ // we want to normalize brightness to 0..255
378
+ if (c2=='6' || c2=='3') { // PPM-RAW/ASC
379
+ for (i=0;i<nx*ny;i++) {
380
+ if (c2=='6') { if (3*bps!=(int)fread(buf,1,3*bps,f1)){
381
+ fprintf(stderr," ERROR reading at head+3*%d*%d\n", bps, i); break; } }
382
+ else for (j=0;j<3;j++) fread_num(buf+j*bps, bps, f1);
383
+ pic[i]
384
+ = ((PPM_RED_WEIGHT * (unsigned char)buf[ bps-1] + 511)>>10)
385
+ + ((PPM_GREEN_WEIGHT * (unsigned char)buf[2*bps-1] + 511)>>10)
386
+ + ((PPM_BLUE_WEIGHT * (unsigned char)buf[3*bps-1] + 511)>>10);
387
+ /* normalized to 0..255 */
388
+ }
389
+ }
390
+ if( c2=='1' )
391
+ for(mod=j=i=0,nc=255;i<nx*ny && !feof(f1);){ // PBM-ASCII 0001100
392
+ c1=read_char(f1);
393
+ if( isdigit(c1) ) { pic[i]=((c1=='0')?255:0); i++; }
394
+ else if( !isspace(c1) )F0("unexpected char");
395
+ }
396
+ if( c2=='4' ){ // PBM-RAW
397
+ dx=(nx+7)&~7; // dx (mod 8)
398
+ if(ny!=(int)fread(pic,dx>>3,ny,f1))F0("read"); // read all bytes
399
+ for(ly=ny-1;ly>=0;ly--)
400
+ for(lx=nx-1;lx>=0;lx--)
401
+ pic[lx+ly*nx]=( (128 & (pic[(lx+ly*dx)>>3]<<(lx & 7))) ? 0 : 255 );
402
+ nc=255;
403
+ }
404
+ {
405
+ int minc=255, maxc=0;
406
+ for (i=0;i<nx*ny;i++) {
407
+ if (pic[i]>maxc) maxc=pic[i];
408
+ if (pic[i]<minc) minc=pic[i];
409
+ }
410
+ if (vvv) fprintf(stderr," min=%d max=%d", minc, maxc);
411
+ }
412
+ p->p=pic; p->x=nx; p->y=ny; p->bpp=1;
413
+ if (vvv) fprintf(stderr,"\n");
414
+ c1=0; c1=fgetc(f1); /* needed to trigger feof() */
415
+ if (feof(f1) || c1!='P') { /* EOF ^Z or not 'P' -> single image */
416
+ if (vvv) fprintf(stderr,"# PNM EOF\n");
417
+ if(name[0]!='-' || name[1]!=0){ /* do not close stdin */
418
+ if(!pip) fclose(f1);
419
+ #ifdef HAVE_POPEN
420
+ else pclose(f1); /* close pipe (Jul00) */
421
+ #endif
422
+ }
423
+ f1=NULL; /* set file is closed flag */
424
+ return 0;
425
+ }
426
+ return 1; /* multiple image = concatenated pnm's */
427
+ }
428
+ #endif /* HAVE_PAM_H */
429
+
430
+ int writepgm(char *nam,pix *p){// P5 raw-pgm
431
+ FILE *f1;int a,x,y;
432
+ f1=fopen(nam,"wb");if(!f1)F0("open"); // open-error
433
+ fprintf(f1,"P5\n%d %d\n255\n",p->x,p->y);
434
+ if(p->bpp==3)
435
+ for(y=0;y<p->y;y++)
436
+ for(x=0;x<p->x;x++){ // set bit
437
+ a=x+y*p->x;
438
+ p->p[a]=(p->p[3*a+0]+p->p[3*a+1]+p->p[3*a+2])/3;
439
+ }
440
+ if(p->y!=(int)fwrite(p->p,p->x,p->y,f1))F0("write"); // write all lines
441
+ fclose(f1);
442
+ return 0;
443
+ }
444
+
445
+ /* adding colours, care about range */
446
+ void addrgb(unsigned char rgb[3], int sr, int sg, int sb) {
447
+ int add[3], i;
448
+ /* add colour on dark pixels, subtract on white pixels */
449
+ add[0]=2*sr; add[1]=2*sg; add[2]=2*sb;
450
+ if (((int)rgb[0])+((int)rgb[1])+((int)rgb[2])>=3*160)
451
+ { add[0]=(-sg-sb); add[1]=(-sr-sb); add[2]=(-sr-sg); } // rgb/2?
452
+ /* care about colour range */
453
+ for (i=0;i<3;i++)
454
+ if (add[i]<0) rgb[i]-=(( rgb[i]<-add[i])? rgb[i]:-add[i]);
455
+ else rgb[i]+=((255-rgb[i]< add[i])?255-rgb[i]: add[i]);
456
+ }
457
+ /*
458
+ * pgmtoppm or pnmtopng, use last 3 bits for farbcoding
459
+ * replaces old writebmp variant
460
+ */
461
+ int writeppm(char *nam, pix *p){ /* P6 raw-ppm */
462
+ FILE *f1=NULL; int x,y,f1t=0; unsigned char rgb[3], gray, bits;
463
+ char buf[128];
464
+ if (strchr(nam,'|')) return -1; /* no nasty code */
465
+ if (strstr(nam,".ppm")) { f1=fopen(nam,"wb"); }
466
+ #ifdef HAVE_POPEN
467
+ /* be sure that nam contains hacker code like "dummy | rm -rf *" */
468
+ if (!f1) {
469
+ strncpy(buf,"pnmtopng > ",12); /* no spaces within filenames allowed! */
470
+ strncpy(buf+11,nam,111); buf[123]=0;
471
+ strncpy(buf+strlen(buf),".png",5);
472
+ /* we dont care about win "wb" here, never debug on win systems */
473
+ f1 = popen(buf, "w"); if(f1) f1t=1; else E0("popen pnmtopng");
474
+ }
475
+ if (!f1) {
476
+ strncpy(buf,"gzip -c > ",11);
477
+ strncpy(buf+10,nam,109); buf[120]=0;
478
+ strncpy(buf+strlen(buf),".ppm.gz",8);
479
+ /* we dont care about win "wb" here, never debug on win systems */
480
+ f1 = popen(buf, "w"); if(f1) f1t=1; else E0("popen gzip -c");
481
+ }
482
+ #endif
483
+ if (!f1) {
484
+ strncpy(buf,nam,113); buf[114]=0;
485
+ strncpy(buf+strlen(buf),".ppm",5);
486
+ f1=fopen(buf,"wb");
487
+ }
488
+ if (!f1) F0("open"); /* open-error */
489
+ fprintf(f1,"P6\n%d %d\n255\n",p->x,p->y);
490
+ if ( p->bpp==1 )
491
+ for (y=0;y<p->y;y++)
492
+ for (x=0;x<p->x;x++){
493
+ gray=p->p[x+y*p->x];
494
+ bits=(gray&0x0F); /* save marker bits */
495
+ /* replace used bits to get max. contrast, 160=0xA0 */
496
+ gray = ((gray<160) ? (gray&~0x0F)>>1 : 0xC3|(gray>>1) );
497
+ rgb[0] = rgb[1] = rgb[2] = gray;
498
+ if ((bits & 1)==1) { addrgb(rgb,0,0,8+8*((x+y)&1)); } /* dark blue */
499
+ if ((bits & 8)==8) { addrgb(rgb,0,0, 16); } /* blue (low priority) */
500
+ if ((bits & 6)==6) { addrgb(rgb,0,0, 32); } /* blue */
501
+ if ((bits & 6)==4) { addrgb(rgb,0,48,0); } /* green */
502
+ if ((bits & 6)==2) { addrgb(rgb,32,0,0); } /* red */
503
+ if ( 1!=(int)fwrite(rgb,3,1,f1) ) { E0("write"); y=p->y; break; }
504
+ }
505
+ if ( p->bpp==3 )
506
+ if ( p->y!=(int)fwrite(p->p,3*p->x,p->y,f1) ) E0("write");
507
+ #ifdef HAVE_POPEN
508
+ if (f1t) { pclose (f1); f1=NULL; }
509
+ #endif
510
+ if (f1) fclose(f1);
511
+ return 0;
512
+ }
513
+
514
+ // high bit = first,
515
+ int writepbm(char *nam,pix *p){// P4 raw-pbm
516
+ FILE *f1;int x,y,a,b,dx,i;
517
+ dx=(p->x+7)&~7; // enlarge to a factor of 8
518
+ for(y=0;y<p->y;y++)
519
+ for(x=0;x<p->x;x++){ // set bit
520
+ a=(x+y*dx)>>3;b=7-(x&7); // adress an bitisnumber
521
+ i=x+y*p->x;
522
+ if(p->bpp==3) i=(p->p[3*i+0]+p->p[3*i+1]+p->p[3*i+2])/3;
523
+ else i= p->p[ i ];
524
+ i=((i>127)?0:1);
525
+ p->p[a]=(p->p[a] & (~1<<b)) | (i<<b);
526
+ }
527
+ f1=fopen(nam,"wb");if(!f1)F0("open"); // open-error
528
+ fprintf(f1,"P4\n%d %d\n",p->x,p->y);
529
+ if(p->y!=(int)fwrite(p->p,dx>>3,p->y,f1))F0("write"); // write all lines
530
+ fclose(f1);
531
+ return 0;
532
+ }
533
+ // ------------------------------------------------------------------------