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.
Files changed (290) hide show
  1. data/README +1 -1
  2. data/Rakefile +0 -18
  3. data/VERSION +1 -0
  4. data/isbn.gemspec +290 -7
  5. data/lib/isbn.rb +6 -6
  6. data/src/gocr-0.48/.cvsignore +6 -0
  7. data/src/gocr-0.48/AUTHORS +7 -0
  8. data/src/gocr-0.48/BUGS +55 -0
  9. data/src/gocr-0.48/CREDITS +17 -0
  10. data/src/gocr-0.48/HISTORY +243 -0
  11. data/src/gocr-0.48/INSTALL +83 -0
  12. data/src/gocr-0.48/Makefile +193 -0
  13. data/src/gocr-0.48/Makefile.in +193 -0
  14. data/src/gocr-0.48/README +165 -0
  15. data/src/gocr-0.48/READMEde.txt +80 -0
  16. data/src/gocr-0.48/REMARK.txt +18 -0
  17. data/src/gocr-0.48/REVIEW +538 -0
  18. data/src/gocr-0.48/TODO +65 -0
  19. data/src/gocr-0.48/bin/.cvsignore +2 -0
  20. data/src/gocr-0.48/bin/create_db +38 -0
  21. data/src/gocr-0.48/bin/gocr.tcl +527 -0
  22. data/src/gocr-0.48/bin/gocr_chk.sh +44 -0
  23. data/src/gocr-0.48/configure +4689 -0
  24. data/src/gocr-0.48/configure.in +71 -0
  25. data/src/gocr-0.48/doc/.#Makefile.1.6 +39 -0
  26. data/src/gocr-0.48/doc/.cvsignore +2 -0
  27. data/src/gocr-0.48/doc/Makefile +39 -0
  28. data/src/gocr-0.48/doc/Makefile.in +39 -0
  29. data/src/gocr-0.48/doc/example.dtd +53 -0
  30. data/src/gocr-0.48/doc/example.xml +21 -0
  31. data/src/gocr-0.48/doc/examples.txt +67 -0
  32. data/src/gocr-0.48/doc/gocr.html +578 -0
  33. data/src/gocr-0.48/doc/unicode.txt +57 -0
  34. data/src/gocr-0.48/examples/.#Makefile.1.22 +166 -0
  35. data/src/gocr-0.48/examples/4x6.png +0 -0
  36. data/src/gocr-0.48/examples/4x6.txt +2 -0
  37. data/src/gocr-0.48/examples/5x7.png +0 -0
  38. data/src/gocr-0.48/examples/5x7.png.txt +2 -0
  39. data/src/gocr-0.48/examples/5x8.png +0 -0
  40. data/src/gocr-0.48/examples/5x8.png.txt +2 -0
  41. data/src/gocr-0.48/examples/Makefile +166 -0
  42. data/src/gocr-0.48/examples/color.fig +20 -0
  43. data/src/gocr-0.48/examples/ex.fig +16 -0
  44. data/src/gocr-0.48/examples/font.tex +22 -0
  45. data/src/gocr-0.48/examples/font1.tex +46 -0
  46. data/src/gocr-0.48/examples/font2.fig +27 -0
  47. data/src/gocr-0.48/examples/font_nw.tex +24 -0
  48. data/src/gocr-0.48/examples/handwrt1.jpg +0 -0
  49. data/src/gocr-0.48/examples/handwrt1.txt +10 -0
  50. data/src/gocr-0.48/examples/inverse.fig +20 -0
  51. data/src/gocr-0.48/examples/matrix.jpg +0 -0
  52. data/src/gocr-0.48/examples/ocr-a-subset.png +0 -0
  53. data/src/gocr-0.48/examples/ocr-a-subset.png.txt +4 -0
  54. data/src/gocr-0.48/examples/ocr-a.png +0 -0
  55. data/src/gocr-0.48/examples/ocr-a.txt +6 -0
  56. data/src/gocr-0.48/examples/ocr-b.png +0 -0
  57. data/src/gocr-0.48/examples/ocr-b.png.txt +4 -0
  58. data/src/gocr-0.48/examples/polish.tex +28 -0
  59. data/src/gocr-0.48/examples/rotate45.fig +14 -0
  60. data/src/gocr-0.48/examples/score +36 -0
  61. data/src/gocr-0.48/examples/text.tex +28 -0
  62. data/src/gocr-0.48/gocr.spec +143 -0
  63. data/src/gocr-0.48/gpl.html +537 -0
  64. data/src/gocr-0.48/include/.cvsignore +2 -0
  65. data/src/gocr-0.48/include/config.h +36 -0
  66. data/src/gocr-0.48/include/config.h.in +36 -0
  67. data/src/gocr-0.48/include/version.h +2 -0
  68. data/src/gocr-0.48/install-sh +3 -0
  69. data/src/gocr-0.48/make.bat +57 -0
  70. data/src/gocr-0.48/man/.cvsignore +2 -0
  71. data/src/gocr-0.48/man/Makefile +29 -0
  72. data/src/gocr-0.48/man/Makefile.in +29 -0
  73. data/src/gocr-0.48/man/man1/gocr.1 +166 -0
  74. data/src/gocr-0.48/src/.cvsignore +4 -0
  75. data/src/gocr-0.48/src/Makefile +132 -0
  76. data/src/gocr-0.48/src/Makefile.in +132 -0
  77. data/src/gocr-0.48/src/amiga.h +31 -0
  78. data/src/gocr-0.48/src/barcode.c +846 -0
  79. data/src/gocr-0.48/src/barcode.c.orig +593 -0
  80. data/src/gocr-0.48/src/barcode.h +11 -0
  81. data/src/gocr-0.48/src/box.c +372 -0
  82. data/src/gocr-0.48/src/database.c +462 -0
  83. data/src/gocr-0.48/src/detect.c +943 -0
  84. data/src/gocr-0.48/src/gocr.c +373 -0
  85. data/src/gocr-0.48/src/gocr.h +288 -0
  86. data/src/gocr-0.48/src/jconv.c +168 -0
  87. data/src/gocr-0.48/src/job.c +84 -0
  88. data/src/gocr-0.48/src/lines.c +350 -0
  89. data/src/gocr-0.48/src/list.c +334 -0
  90. data/src/gocr-0.48/src/list.h +90 -0
  91. data/src/gocr-0.48/src/ocr0.c +6756 -0
  92. data/src/gocr-0.48/src/ocr0.h +63 -0
  93. data/src/gocr-0.48/src/ocr0n.c +1475 -0
  94. data/src/gocr-0.48/src/ocr1.c +85 -0
  95. data/src/gocr-0.48/src/ocr1.h +3 -0
  96. data/src/gocr-0.48/src/otsu.c +289 -0
  97. data/src/gocr-0.48/src/otsu.h +23 -0
  98. data/src/gocr-0.48/src/output.c +289 -0
  99. data/src/gocr-0.48/src/output.h +37 -0
  100. data/src/gocr-0.48/src/pcx.c +153 -0
  101. data/src/gocr-0.48/src/pcx.h +9 -0
  102. data/src/gocr-0.48/src/pgm2asc.c +2893 -0
  103. data/src/gocr-0.48/src/pgm2asc.h +105 -0
  104. data/src/gocr-0.48/src/pixel.c +537 -0
  105. data/src/gocr-0.48/src/pnm.c +533 -0
  106. data/src/gocr-0.48/src/pnm.h +35 -0
  107. data/src/gocr-0.48/src/progress.c +87 -0
  108. data/src/gocr-0.48/src/progress.h +42 -0
  109. data/src/gocr-0.48/src/remove.c +703 -0
  110. data/src/gocr-0.48/src/tga.c +87 -0
  111. data/src/gocr-0.48/src/tga.h +6 -0
  112. data/src/gocr-0.48/src/unicode.c +1314 -0
  113. data/src/gocr-0.48/src/unicode.h +1257 -0
  114. data/src/jpeg-7/Makefile.am +133 -0
  115. data/src/jpeg-7/Makefile.in +1089 -0
  116. data/src/jpeg-7/README +322 -0
  117. data/src/jpeg-7/aclocal.m4 +8990 -0
  118. data/src/jpeg-7/ansi2knr.1 +36 -0
  119. data/src/jpeg-7/ansi2knr.c +739 -0
  120. data/src/jpeg-7/cderror.h +132 -0
  121. data/src/jpeg-7/cdjpeg.c +181 -0
  122. data/src/jpeg-7/cdjpeg.h +187 -0
  123. data/src/jpeg-7/change.log +270 -0
  124. data/src/jpeg-7/cjpeg.1 +325 -0
  125. data/src/jpeg-7/cjpeg.c +616 -0
  126. data/src/jpeg-7/ckconfig.c +402 -0
  127. data/src/jpeg-7/coderules.txt +118 -0
  128. data/src/jpeg-7/config.guess +1561 -0
  129. data/src/jpeg-7/config.sub +1686 -0
  130. data/src/jpeg-7/configure +17139 -0
  131. data/src/jpeg-7/configure.ac +317 -0
  132. data/src/jpeg-7/depcomp +630 -0
  133. data/src/jpeg-7/djpeg.1 +251 -0
  134. data/src/jpeg-7/djpeg.c +617 -0
  135. data/src/jpeg-7/example.c +433 -0
  136. data/src/jpeg-7/filelist.txt +215 -0
  137. data/src/jpeg-7/install-sh +520 -0
  138. data/src/jpeg-7/install.txt +1097 -0
  139. data/src/jpeg-7/jaricom.c +148 -0
  140. data/src/jpeg-7/jcapimin.c +282 -0
  141. data/src/jpeg-7/jcapistd.c +161 -0
  142. data/src/jpeg-7/jcarith.c +921 -0
  143. data/src/jpeg-7/jccoefct.c +453 -0
  144. data/src/jpeg-7/jccolor.c +459 -0
  145. data/src/jpeg-7/jcdctmgr.c +482 -0
  146. data/src/jpeg-7/jchuff.c +1612 -0
  147. data/src/jpeg-7/jcinit.c +65 -0
  148. data/src/jpeg-7/jcmainct.c +293 -0
  149. data/src/jpeg-7/jcmarker.c +667 -0
  150. data/src/jpeg-7/jcmaster.c +770 -0
  151. data/src/jpeg-7/jcomapi.c +106 -0
  152. data/src/jpeg-7/jconfig.bcc +48 -0
  153. data/src/jpeg-7/jconfig.cfg +45 -0
  154. data/src/jpeg-7/jconfig.dj +38 -0
  155. data/src/jpeg-7/jconfig.mac +43 -0
  156. data/src/jpeg-7/jconfig.manx +43 -0
  157. data/src/jpeg-7/jconfig.mc6 +52 -0
  158. data/src/jpeg-7/jconfig.sas +43 -0
  159. data/src/jpeg-7/jconfig.st +42 -0
  160. data/src/jpeg-7/jconfig.txt +155 -0
  161. data/src/jpeg-7/jconfig.vc +45 -0
  162. data/src/jpeg-7/jconfig.vms +37 -0
  163. data/src/jpeg-7/jconfig.wat +38 -0
  164. data/src/jpeg-7/jcparam.c +632 -0
  165. data/src/jpeg-7/jcprepct.c +358 -0
  166. data/src/jpeg-7/jcsample.c +545 -0
  167. data/src/jpeg-7/jctrans.c +381 -0
  168. data/src/jpeg-7/jdapimin.c +396 -0
  169. data/src/jpeg-7/jdapistd.c +275 -0
  170. data/src/jpeg-7/jdarith.c +762 -0
  171. data/src/jpeg-7/jdatadst.c +151 -0
  172. data/src/jpeg-7/jdatasrc.c +212 -0
  173. data/src/jpeg-7/jdcoefct.c +736 -0
  174. data/src/jpeg-7/jdcolor.c +396 -0
  175. data/src/jpeg-7/jdct.h +393 -0
  176. data/src/jpeg-7/jddctmgr.c +382 -0
  177. data/src/jpeg-7/jdhuff.c +1309 -0
  178. data/src/jpeg-7/jdinput.c +384 -0
  179. data/src/jpeg-7/jdmainct.c +512 -0
  180. data/src/jpeg-7/jdmarker.c +1360 -0
  181. data/src/jpeg-7/jdmaster.c +663 -0
  182. data/src/jpeg-7/jdmerge.c +400 -0
  183. data/src/jpeg-7/jdpostct.c +290 -0
  184. data/src/jpeg-7/jdsample.c +361 -0
  185. data/src/jpeg-7/jdtrans.c +136 -0
  186. data/src/jpeg-7/jerror.c +252 -0
  187. data/src/jpeg-7/jerror.h +304 -0
  188. data/src/jpeg-7/jfdctflt.c +174 -0
  189. data/src/jpeg-7/jfdctfst.c +230 -0
  190. data/src/jpeg-7/jfdctint.c +4348 -0
  191. data/src/jpeg-7/jidctflt.c +242 -0
  192. data/src/jpeg-7/jidctfst.c +368 -0
  193. data/src/jpeg-7/jidctint.c +5137 -0
  194. data/src/jpeg-7/jinclude.h +91 -0
  195. data/src/jpeg-7/jmemansi.c +167 -0
  196. data/src/jpeg-7/jmemdos.c +638 -0
  197. data/src/jpeg-7/jmemdosa.asm +379 -0
  198. data/src/jpeg-7/jmemmac.c +289 -0
  199. data/src/jpeg-7/jmemmgr.c +1118 -0
  200. data/src/jpeg-7/jmemname.c +276 -0
  201. data/src/jpeg-7/jmemnobs.c +109 -0
  202. data/src/jpeg-7/jmemsys.h +198 -0
  203. data/src/jpeg-7/jmorecfg.h +369 -0
  204. data/src/jpeg-7/jpegint.h +395 -0
  205. data/src/jpeg-7/jpeglib.h +1135 -0
  206. data/src/jpeg-7/jpegtran.1 +272 -0
  207. data/src/jpeg-7/jpegtran.c +546 -0
  208. data/src/jpeg-7/jquant1.c +856 -0
  209. data/src/jpeg-7/jquant2.c +1310 -0
  210. data/src/jpeg-7/jutils.c +179 -0
  211. data/src/jpeg-7/jversion.h +14 -0
  212. data/src/jpeg-7/libjpeg.map +4 -0
  213. data/src/jpeg-7/libjpeg.txt +3067 -0
  214. data/src/jpeg-7/ltmain.sh +8406 -0
  215. data/src/jpeg-7/makcjpeg.st +36 -0
  216. data/src/jpeg-7/makdjpeg.st +36 -0
  217. data/src/jpeg-7/makeadsw.vc6 +77 -0
  218. data/src/jpeg-7/makeasln.vc9 +33 -0
  219. data/src/jpeg-7/makecdep.vc6 +82 -0
  220. data/src/jpeg-7/makecdsp.vc6 +130 -0
  221. data/src/jpeg-7/makecmak.vc6 +159 -0
  222. data/src/jpeg-7/makecvcp.vc9 +186 -0
  223. data/src/jpeg-7/makeddep.vc6 +82 -0
  224. data/src/jpeg-7/makeddsp.vc6 +130 -0
  225. data/src/jpeg-7/makedmak.vc6 +159 -0
  226. data/src/jpeg-7/makedvcp.vc9 +186 -0
  227. data/src/jpeg-7/makefile.ansi +220 -0
  228. data/src/jpeg-7/makefile.bcc +291 -0
  229. data/src/jpeg-7/makefile.dj +226 -0
  230. data/src/jpeg-7/makefile.manx +220 -0
  231. data/src/jpeg-7/makefile.mc6 +255 -0
  232. data/src/jpeg-7/makefile.mms +224 -0
  233. data/src/jpeg-7/makefile.sas +258 -0
  234. data/src/jpeg-7/makefile.unix +234 -0
  235. data/src/jpeg-7/makefile.vc +217 -0
  236. data/src/jpeg-7/makefile.vms +142 -0
  237. data/src/jpeg-7/makefile.wat +239 -0
  238. data/src/jpeg-7/makejdep.vc6 +423 -0
  239. data/src/jpeg-7/makejdsp.vc6 +285 -0
  240. data/src/jpeg-7/makejdsw.vc6 +29 -0
  241. data/src/jpeg-7/makejmak.vc6 +425 -0
  242. data/src/jpeg-7/makejsln.vc9 +17 -0
  243. data/src/jpeg-7/makejvcp.vc9 +328 -0
  244. data/src/jpeg-7/makeproj.mac +213 -0
  245. data/src/jpeg-7/makerdep.vc6 +6 -0
  246. data/src/jpeg-7/makerdsp.vc6 +78 -0
  247. data/src/jpeg-7/makermak.vc6 +110 -0
  248. data/src/jpeg-7/makervcp.vc9 +133 -0
  249. data/src/jpeg-7/maketdep.vc6 +43 -0
  250. data/src/jpeg-7/maketdsp.vc6 +122 -0
  251. data/src/jpeg-7/maketmak.vc6 +131 -0
  252. data/src/jpeg-7/maketvcp.vc9 +178 -0
  253. data/src/jpeg-7/makewdep.vc6 +6 -0
  254. data/src/jpeg-7/makewdsp.vc6 +78 -0
  255. data/src/jpeg-7/makewmak.vc6 +110 -0
  256. data/src/jpeg-7/makewvcp.vc9 +133 -0
  257. data/src/jpeg-7/makljpeg.st +68 -0
  258. data/src/jpeg-7/maktjpeg.st +30 -0
  259. data/src/jpeg-7/makvms.opt +4 -0
  260. data/src/jpeg-7/missing +376 -0
  261. data/src/jpeg-7/rdbmp.c +439 -0
  262. data/src/jpeg-7/rdcolmap.c +253 -0
  263. data/src/jpeg-7/rdgif.c +38 -0
  264. data/src/jpeg-7/rdjpgcom.1 +63 -0
  265. data/src/jpeg-7/rdjpgcom.c +515 -0
  266. data/src/jpeg-7/rdppm.c +459 -0
  267. data/src/jpeg-7/rdrle.c +387 -0
  268. data/src/jpeg-7/rdswitch.c +365 -0
  269. data/src/jpeg-7/rdtarga.c +500 -0
  270. data/src/jpeg-7/structure.txt +945 -0
  271. data/src/jpeg-7/testimg.bmp +0 -0
  272. data/src/jpeg-7/testimg.jpg +0 -0
  273. data/src/jpeg-7/testimg.ppm +4 -0
  274. data/src/jpeg-7/testimgp.jpg +0 -0
  275. data/src/jpeg-7/testorig.jpg +0 -0
  276. data/src/jpeg-7/testprog.jpg +0 -0
  277. data/src/jpeg-7/transupp.c +1533 -0
  278. data/src/jpeg-7/transupp.h +205 -0
  279. data/src/jpeg-7/usage.txt +605 -0
  280. data/src/jpeg-7/wizard.txt +211 -0
  281. data/src/jpeg-7/wrbmp.c +442 -0
  282. data/src/jpeg-7/wrgif.c +399 -0
  283. data/src/jpeg-7/wrjpgcom.1 +103 -0
  284. data/src/jpeg-7/wrjpgcom.c +583 -0
  285. data/src/jpeg-7/wrppm.c +269 -0
  286. data/src/jpeg-7/wrrle.c +305 -0
  287. data/src/jpeg-7/wrtarga.c +253 -0
  288. metadata +287 -6
  289. data/LICENSE +0 -20
  290. data/VERSION.yml +0 -4
@@ -0,0 +1,275 @@
1
+ /*
2
+ * jdapistd.c
3
+ *
4
+ * Copyright (C) 1994-1996, Thomas G. Lane.
5
+ * This file is part of the Independent JPEG Group's software.
6
+ * For conditions of distribution and use, see the accompanying README file.
7
+ *
8
+ * This file contains application interface code for the decompression half
9
+ * of the JPEG library. These are the "standard" API routines that are
10
+ * used in the normal full-decompression case. They are not used by a
11
+ * transcoding-only application. Note that if an application links in
12
+ * jpeg_start_decompress, it will end up linking in the entire decompressor.
13
+ * We thus must separate this file from jdapimin.c to avoid linking the
14
+ * whole decompression library into a transcoder.
15
+ */
16
+
17
+ #define JPEG_INTERNALS
18
+ #include "jinclude.h"
19
+ #include "jpeglib.h"
20
+
21
+
22
+ /* Forward declarations */
23
+ LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
24
+
25
+
26
+ /*
27
+ * Decompression initialization.
28
+ * jpeg_read_header must be completed before calling this.
29
+ *
30
+ * If a multipass operating mode was selected, this will do all but the
31
+ * last pass, and thus may take a great deal of time.
32
+ *
33
+ * Returns FALSE if suspended. The return value need be inspected only if
34
+ * a suspending data source is used.
35
+ */
36
+
37
+ GLOBAL(boolean)
38
+ jpeg_start_decompress (j_decompress_ptr cinfo)
39
+ {
40
+ if (cinfo->global_state == DSTATE_READY) {
41
+ /* First call: initialize master control, select active modules */
42
+ jinit_master_decompress(cinfo);
43
+ if (cinfo->buffered_image) {
44
+ /* No more work here; expecting jpeg_start_output next */
45
+ cinfo->global_state = DSTATE_BUFIMAGE;
46
+ return TRUE;
47
+ }
48
+ cinfo->global_state = DSTATE_PRELOAD;
49
+ }
50
+ if (cinfo->global_state == DSTATE_PRELOAD) {
51
+ /* If file has multiple scans, absorb them all into the coef buffer */
52
+ if (cinfo->inputctl->has_multiple_scans) {
53
+ #ifdef D_MULTISCAN_FILES_SUPPORTED
54
+ for (;;) {
55
+ int retcode;
56
+ /* Call progress monitor hook if present */
57
+ if (cinfo->progress != NULL)
58
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
59
+ /* Absorb some more input */
60
+ retcode = (*cinfo->inputctl->consume_input) (cinfo);
61
+ if (retcode == JPEG_SUSPENDED)
62
+ return FALSE;
63
+ if (retcode == JPEG_REACHED_EOI)
64
+ break;
65
+ /* Advance progress counter if appropriate */
66
+ if (cinfo->progress != NULL &&
67
+ (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
68
+ if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
69
+ /* jdmaster underestimated number of scans; ratchet up one scan */
70
+ cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
71
+ }
72
+ }
73
+ }
74
+ #else
75
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
76
+ #endif /* D_MULTISCAN_FILES_SUPPORTED */
77
+ }
78
+ cinfo->output_scan_number = cinfo->input_scan_number;
79
+ } else if (cinfo->global_state != DSTATE_PRESCAN)
80
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
81
+ /* Perform any dummy output passes, and set up for the final pass */
82
+ return output_pass_setup(cinfo);
83
+ }
84
+
85
+
86
+ /*
87
+ * Set up for an output pass, and perform any dummy pass(es) needed.
88
+ * Common subroutine for jpeg_start_decompress and jpeg_start_output.
89
+ * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
90
+ * Exit: If done, returns TRUE and sets global_state for proper output mode.
91
+ * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
92
+ */
93
+
94
+ LOCAL(boolean)
95
+ output_pass_setup (j_decompress_ptr cinfo)
96
+ {
97
+ if (cinfo->global_state != DSTATE_PRESCAN) {
98
+ /* First call: do pass setup */
99
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
100
+ cinfo->output_scanline = 0;
101
+ cinfo->global_state = DSTATE_PRESCAN;
102
+ }
103
+ /* Loop over any required dummy passes */
104
+ while (cinfo->master->is_dummy_pass) {
105
+ #ifdef QUANT_2PASS_SUPPORTED
106
+ /* Crank through the dummy pass */
107
+ while (cinfo->output_scanline < cinfo->output_height) {
108
+ JDIMENSION last_scanline;
109
+ /* Call progress monitor hook if present */
110
+ if (cinfo->progress != NULL) {
111
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
112
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
113
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
114
+ }
115
+ /* Process some data */
116
+ last_scanline = cinfo->output_scanline;
117
+ (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
118
+ &cinfo->output_scanline, (JDIMENSION) 0);
119
+ if (cinfo->output_scanline == last_scanline)
120
+ return FALSE; /* No progress made, must suspend */
121
+ }
122
+ /* Finish up dummy pass, and set up for another one */
123
+ (*cinfo->master->finish_output_pass) (cinfo);
124
+ (*cinfo->master->prepare_for_output_pass) (cinfo);
125
+ cinfo->output_scanline = 0;
126
+ #else
127
+ ERREXIT(cinfo, JERR_NOT_COMPILED);
128
+ #endif /* QUANT_2PASS_SUPPORTED */
129
+ }
130
+ /* Ready for application to drive output pass through
131
+ * jpeg_read_scanlines or jpeg_read_raw_data.
132
+ */
133
+ cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
134
+ return TRUE;
135
+ }
136
+
137
+
138
+ /*
139
+ * Read some scanlines of data from the JPEG decompressor.
140
+ *
141
+ * The return value will be the number of lines actually read.
142
+ * This may be less than the number requested in several cases,
143
+ * including bottom of image, data source suspension, and operating
144
+ * modes that emit multiple scanlines at a time.
145
+ *
146
+ * Note: we warn about excess calls to jpeg_read_scanlines() since
147
+ * this likely signals an application programmer error. However,
148
+ * an oversize buffer (max_lines > scanlines remaining) is not an error.
149
+ */
150
+
151
+ GLOBAL(JDIMENSION)
152
+ jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
153
+ JDIMENSION max_lines)
154
+ {
155
+ JDIMENSION row_ctr;
156
+
157
+ if (cinfo->global_state != DSTATE_SCANNING)
158
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
159
+ if (cinfo->output_scanline >= cinfo->output_height) {
160
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
161
+ return 0;
162
+ }
163
+
164
+ /* Call progress monitor hook if present */
165
+ if (cinfo->progress != NULL) {
166
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
167
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
168
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
169
+ }
170
+
171
+ /* Process some data */
172
+ row_ctr = 0;
173
+ (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
174
+ cinfo->output_scanline += row_ctr;
175
+ return row_ctr;
176
+ }
177
+
178
+
179
+ /*
180
+ * Alternate entry point to read raw data.
181
+ * Processes exactly one iMCU row per call, unless suspended.
182
+ */
183
+
184
+ GLOBAL(JDIMENSION)
185
+ jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
186
+ JDIMENSION max_lines)
187
+ {
188
+ JDIMENSION lines_per_iMCU_row;
189
+
190
+ if (cinfo->global_state != DSTATE_RAW_OK)
191
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
192
+ if (cinfo->output_scanline >= cinfo->output_height) {
193
+ WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
194
+ return 0;
195
+ }
196
+
197
+ /* Call progress monitor hook if present */
198
+ if (cinfo->progress != NULL) {
199
+ cinfo->progress->pass_counter = (long) cinfo->output_scanline;
200
+ cinfo->progress->pass_limit = (long) cinfo->output_height;
201
+ (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
202
+ }
203
+
204
+ /* Verify that at least one iMCU row can be returned. */
205
+ lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size;
206
+ if (max_lines < lines_per_iMCU_row)
207
+ ERREXIT(cinfo, JERR_BUFFER_SIZE);
208
+
209
+ /* Decompress directly into user's buffer. */
210
+ if (! (*cinfo->coef->decompress_data) (cinfo, data))
211
+ return 0; /* suspension forced, can do nothing more */
212
+
213
+ /* OK, we processed one iMCU row. */
214
+ cinfo->output_scanline += lines_per_iMCU_row;
215
+ return lines_per_iMCU_row;
216
+ }
217
+
218
+
219
+ /* Additional entry points for buffered-image mode. */
220
+
221
+ #ifdef D_MULTISCAN_FILES_SUPPORTED
222
+
223
+ /*
224
+ * Initialize for an output pass in buffered-image mode.
225
+ */
226
+
227
+ GLOBAL(boolean)
228
+ jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
229
+ {
230
+ if (cinfo->global_state != DSTATE_BUFIMAGE &&
231
+ cinfo->global_state != DSTATE_PRESCAN)
232
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
233
+ /* Limit scan number to valid range */
234
+ if (scan_number <= 0)
235
+ scan_number = 1;
236
+ if (cinfo->inputctl->eoi_reached &&
237
+ scan_number > cinfo->input_scan_number)
238
+ scan_number = cinfo->input_scan_number;
239
+ cinfo->output_scan_number = scan_number;
240
+ /* Perform any dummy output passes, and set up for the real pass */
241
+ return output_pass_setup(cinfo);
242
+ }
243
+
244
+
245
+ /*
246
+ * Finish up after an output pass in buffered-image mode.
247
+ *
248
+ * Returns FALSE if suspended. The return value need be inspected only if
249
+ * a suspending data source is used.
250
+ */
251
+
252
+ GLOBAL(boolean)
253
+ jpeg_finish_output (j_decompress_ptr cinfo)
254
+ {
255
+ if ((cinfo->global_state == DSTATE_SCANNING ||
256
+ cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
257
+ /* Terminate this pass. */
258
+ /* We do not require the whole pass to have been completed. */
259
+ (*cinfo->master->finish_output_pass) (cinfo);
260
+ cinfo->global_state = DSTATE_BUFPOST;
261
+ } else if (cinfo->global_state != DSTATE_BUFPOST) {
262
+ /* BUFPOST = repeat call after a suspension, anything else is error */
263
+ ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
264
+ }
265
+ /* Read markers looking for SOS or EOI */
266
+ while (cinfo->input_scan_number <= cinfo->output_scan_number &&
267
+ ! cinfo->inputctl->eoi_reached) {
268
+ if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
269
+ return FALSE; /* Suspend, come back later */
270
+ }
271
+ cinfo->global_state = DSTATE_BUFIMAGE;
272
+ return TRUE;
273
+ }
274
+
275
+ #endif /* D_MULTISCAN_FILES_SUPPORTED */
@@ -0,0 +1,762 @@
1
+ /*
2
+ * jdarith.c
3
+ *
4
+ * Developed 1997 by Guido Vollbeding.
5
+ * This file is part of the Independent JPEG Group's software.
6
+ * For conditions of distribution and use, see the accompanying README file.
7
+ *
8
+ * This file contains portable arithmetic entropy decoding routines for JPEG
9
+ * (implementing the ISO/IEC IS 10918-1 and CCITT Recommendation ITU-T T.81).
10
+ *
11
+ * Both sequential and progressive modes are supported in this single module.
12
+ *
13
+ * Suspension is not currently supported in this module.
14
+ */
15
+
16
+ #define JPEG_INTERNALS
17
+ #include "jinclude.h"
18
+ #include "jpeglib.h"
19
+
20
+
21
+ /* Expanded entropy decoder object for arithmetic decoding. */
22
+
23
+ typedef struct {
24
+ struct jpeg_entropy_decoder pub; /* public fields */
25
+
26
+ INT32 c; /* C register, base of coding interval + input bit buffer */
27
+ INT32 a; /* A register, normalized size of coding interval */
28
+ int ct; /* bit shift counter, # of bits left in bit buffer part of C */
29
+ /* init: ct = -16 */
30
+ /* run: ct = 0..7 */
31
+ /* error: ct = -1 */
32
+ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */
33
+ int dc_context[MAX_COMPS_IN_SCAN]; /* context index for DC conditioning */
34
+
35
+ unsigned int restarts_to_go; /* MCUs left in this restart interval */
36
+
37
+ /* Pointers to statistics areas (these workspaces have image lifespan) */
38
+ unsigned char * dc_stats[NUM_ARITH_TBLS];
39
+ unsigned char * ac_stats[NUM_ARITH_TBLS];
40
+ } arith_entropy_decoder;
41
+
42
+ typedef arith_entropy_decoder * arith_entropy_ptr;
43
+
44
+ /* The following two definitions specify the allocation chunk size
45
+ * for the statistics area.
46
+ * According to sections F.1.4.4.1.3 and F.1.4.4.2, we need at least
47
+ * 49 statistics bins for DC, and 245 statistics bins for AC coding.
48
+ * Note that we use one additional AC bin for codings with fixed
49
+ * probability (0.5), thus the minimum number for AC is 246.
50
+ *
51
+ * We use a compact representation with 1 byte per statistics bin,
52
+ * thus the numbers directly represent byte sizes.
53
+ * This 1 byte per statistics bin contains the meaning of the MPS
54
+ * (more probable symbol) in the highest bit (mask 0x80), and the
55
+ * index into the probability estimation state machine table
56
+ * in the lower bits (mask 0x7F).
57
+ */
58
+
59
+ #define DC_STAT_BINS 64
60
+ #define AC_STAT_BINS 256
61
+
62
+
63
+ LOCAL(int)
64
+ get_byte (j_decompress_ptr cinfo)
65
+ /* Read next input byte; we do not support suspension in this module. */
66
+ {
67
+ struct jpeg_source_mgr * src = cinfo->src;
68
+
69
+ if (src->bytes_in_buffer == 0)
70
+ if (! (*src->fill_input_buffer) (cinfo))
71
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
72
+ src->bytes_in_buffer--;
73
+ return GETJOCTET(*src->next_input_byte++);
74
+ }
75
+
76
+
77
+ /*
78
+ * The core arithmetic decoding routine (common in JPEG and JBIG).
79
+ * This needs to go as fast as possible.
80
+ * Machine-dependent optimization facilities
81
+ * are not utilized in this portable implementation.
82
+ * However, this code should be fairly efficient and
83
+ * may be a good base for further optimizations anyway.
84
+ *
85
+ * Return value is 0 or 1 (binary decision).
86
+ *
87
+ * Note: I've changed the handling of the code base & bit
88
+ * buffer register C compared to other implementations
89
+ * based on the standards layout & procedures.
90
+ * While it also contains both the actual base of the
91
+ * coding interval (16 bits) and the next-bits buffer,
92
+ * the cut-point between these two parts is floating
93
+ * (instead of fixed) with the bit shift counter CT.
94
+ * Thus, we also need only one (variable instead of
95
+ * fixed size) shift for the LPS/MPS decision, and
96
+ * we can get away with any renormalization update
97
+ * of C (except for new data insertion, of course).
98
+ *
99
+ * I've also introduced a new scheme for accessing
100
+ * the probability estimation state machine table,
101
+ * derived from Markus Kuhn's JBIG implementation.
102
+ */
103
+
104
+ LOCAL(int)
105
+ arith_decode (j_decompress_ptr cinfo, unsigned char *st)
106
+ {
107
+ extern const INT32 jaritab[];
108
+ register arith_entropy_ptr e = (arith_entropy_ptr) cinfo->entropy;
109
+ register unsigned char nl, nm;
110
+ register INT32 qe, temp;
111
+ register int sv, data;
112
+
113
+ /* Renormalization & data input per section D.2.6 */
114
+ while (e->a < 0x8000L) {
115
+ if (--e->ct < 0) {
116
+ /* Need to fetch next data byte */
117
+ if (cinfo->unread_marker)
118
+ data = 0; /* stuff zero data */
119
+ else {
120
+ data = get_byte(cinfo); /* read next input byte */
121
+ if (data == 0xFF) { /* zero stuff or marker code */
122
+ do data = get_byte(cinfo);
123
+ while (data == 0xFF); /* swallow extra 0xFF bytes */
124
+ if (data == 0)
125
+ data = 0xFF; /* discard stuffed zero byte */
126
+ else {
127
+ /* Note: Different from the Huffman decoder, hitting
128
+ * a marker while processing the compressed data
129
+ * segment is legal in arithmetic coding.
130
+ * The convention is to supply zero data
131
+ * then until decoding is complete.
132
+ */
133
+ cinfo->unread_marker = data;
134
+ data = 0;
135
+ }
136
+ }
137
+ }
138
+ e->c = (e->c << 8) | data; /* insert data into C register */
139
+ if ((e->ct += 8) < 0) /* update bit shift counter */
140
+ /* Need more initial bytes */
141
+ if (++e->ct == 0)
142
+ /* Got 2 initial bytes -> re-init A and exit loop */
143
+ e->a = 0x8000L; /* => e->a = 0x10000L after loop exit */
144
+ }
145
+ e->a <<= 1;
146
+ }
147
+
148
+ /* Fetch values from our compact representation of Table D.2:
149
+ * Qe values and probability estimation state machine
150
+ */
151
+ sv = *st;
152
+ qe = jaritab[sv & 0x7F]; /* => Qe_Value */
153
+ nl = qe & 0xFF; qe >>= 8; /* Next_Index_LPS + Switch_MPS */
154
+ nm = qe & 0xFF; qe >>= 8; /* Next_Index_MPS */
155
+
156
+ /* Decode & estimation procedures per sections D.2.4 & D.2.5 */
157
+ temp = e->a - qe;
158
+ e->a = temp;
159
+ temp <<= e->ct;
160
+ if (e->c >= temp) {
161
+ e->c -= temp;
162
+ /* Conditional LPS (less probable symbol) exchange */
163
+ if (e->a < qe) {
164
+ e->a = qe;
165
+ *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
166
+ } else {
167
+ e->a = qe;
168
+ *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
169
+ sv ^= 0x80; /* Exchange LPS/MPS */
170
+ }
171
+ } else if (e->a < 0x8000L) {
172
+ /* Conditional MPS (more probable symbol) exchange */
173
+ if (e->a < qe) {
174
+ *st = (sv & 0x80) ^ nl; /* Estimate_after_LPS */
175
+ sv ^= 0x80; /* Exchange LPS/MPS */
176
+ } else {
177
+ *st = (sv & 0x80) ^ nm; /* Estimate_after_MPS */
178
+ }
179
+ }
180
+
181
+ return sv >> 7;
182
+ }
183
+
184
+
185
+ /*
186
+ * Check for a restart marker & resynchronize decoder.
187
+ */
188
+
189
+ LOCAL(void)
190
+ process_restart (j_decompress_ptr cinfo)
191
+ {
192
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
193
+ int ci;
194
+ jpeg_component_info * compptr;
195
+
196
+ /* Advance past the RSTn marker */
197
+ if (! (*cinfo->marker->read_restart_marker) (cinfo))
198
+ ERREXIT(cinfo, JERR_CANT_SUSPEND);
199
+
200
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
201
+ compptr = cinfo->cur_comp_info[ci];
202
+ /* Re-initialize statistics areas */
203
+ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
204
+ MEMZERO(entropy->dc_stats[compptr->dc_tbl_no], DC_STAT_BINS);
205
+ /* Reset DC predictions to 0 */
206
+ entropy->last_dc_val[ci] = 0;
207
+ entropy->dc_context[ci] = 0;
208
+ }
209
+ if (cinfo->progressive_mode == 0 || cinfo->Ss) {
210
+ MEMZERO(entropy->ac_stats[compptr->ac_tbl_no], AC_STAT_BINS);
211
+ }
212
+ }
213
+
214
+ /* Reset arithmetic decoding variables */
215
+ entropy->c = 0;
216
+ entropy->a = 0;
217
+ entropy->ct = -16; /* force reading 2 initial bytes to fill C */
218
+
219
+ /* Reset restart counter */
220
+ entropy->restarts_to_go = cinfo->restart_interval;
221
+ }
222
+
223
+
224
+ /*
225
+ * Arithmetic MCU decoding.
226
+ * Each of these routines decodes and returns one MCU's worth of
227
+ * arithmetic-compressed coefficients.
228
+ * The coefficients are reordered from zigzag order into natural array order,
229
+ * but are not dequantized.
230
+ *
231
+ * The i'th block of the MCU is stored into the block pointed to by
232
+ * MCU_data[i]. WE ASSUME THIS AREA IS INITIALLY ZEROED BY THE CALLER.
233
+ */
234
+
235
+ /*
236
+ * MCU decoding for DC initial scan (either spectral selection,
237
+ * or first pass of successive approximation).
238
+ */
239
+
240
+ METHODDEF(boolean)
241
+ decode_mcu_DC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
242
+ {
243
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
244
+ JBLOCKROW block;
245
+ unsigned char *st;
246
+ int blkn, ci, tbl, sign;
247
+ int v, m;
248
+
249
+ /* Process restart marker if needed */
250
+ if (cinfo->restart_interval) {
251
+ if (entropy->restarts_to_go == 0)
252
+ process_restart(cinfo);
253
+ entropy->restarts_to_go--;
254
+ }
255
+
256
+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
257
+
258
+ /* Outer loop handles each block in the MCU */
259
+
260
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
261
+ block = MCU_data[blkn];
262
+ ci = cinfo->MCU_membership[blkn];
263
+ tbl = cinfo->cur_comp_info[ci]->dc_tbl_no;
264
+
265
+ /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
266
+
267
+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
268
+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
269
+
270
+ /* Figure F.19: Decode_DC_DIFF */
271
+ if (arith_decode(cinfo, st) == 0)
272
+ entropy->dc_context[ci] = 0;
273
+ else {
274
+ /* Figure F.21: Decoding nonzero value v */
275
+ /* Figure F.22: Decoding the sign of v */
276
+ sign = arith_decode(cinfo, st + 1);
277
+ st += 2; st += sign;
278
+ /* Figure F.23: Decoding the magnitude category of v */
279
+ if ((m = arith_decode(cinfo, st)) != 0) {
280
+ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
281
+ while (arith_decode(cinfo, st)) {
282
+ if ((m <<= 1) == 0x8000) {
283
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
284
+ entropy->ct = -1; /* magnitude overflow */
285
+ return TRUE;
286
+ }
287
+ st += 1;
288
+ }
289
+ }
290
+ /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
291
+ if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
292
+ entropy->dc_context[ci] = 0; /* zero diff category */
293
+ else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
294
+ entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
295
+ else
296
+ entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
297
+ v = m;
298
+ /* Figure F.24: Decoding the magnitude bit pattern of v */
299
+ st += 14;
300
+ while (m >>= 1)
301
+ if (arith_decode(cinfo, st)) v |= m;
302
+ v += 1; if (sign) v = -v;
303
+ entropy->last_dc_val[ci] += v;
304
+ }
305
+
306
+ /* Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) */
307
+ (*block)[0] = (JCOEF) (entropy->last_dc_val[ci] << cinfo->Al);
308
+ }
309
+
310
+ return TRUE;
311
+ }
312
+
313
+
314
+ /*
315
+ * MCU decoding for AC initial scan (either spectral selection,
316
+ * or first pass of successive approximation).
317
+ */
318
+
319
+ METHODDEF(boolean)
320
+ decode_mcu_AC_first (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
321
+ {
322
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
323
+ JBLOCKROW block;
324
+ unsigned char *st;
325
+ int tbl, sign, k;
326
+ int v, m;
327
+
328
+ /* Process restart marker if needed */
329
+ if (cinfo->restart_interval) {
330
+ if (entropy->restarts_to_go == 0)
331
+ process_restart(cinfo);
332
+ entropy->restarts_to_go--;
333
+ }
334
+
335
+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
336
+
337
+ /* There is always only one block per MCU */
338
+ block = MCU_data[0];
339
+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
340
+
341
+ /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
342
+
343
+ /* Figure F.20: Decode_AC_coefficients */
344
+ for (k = cinfo->Ss; k <= cinfo->Se; k++) {
345
+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
346
+ if (arith_decode(cinfo, st)) break; /* EOB flag */
347
+ while (arith_decode(cinfo, st + 1) == 0) {
348
+ st += 3; k++;
349
+ if (k > cinfo->Se) {
350
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
351
+ entropy->ct = -1; /* spectral overflow */
352
+ return TRUE;
353
+ }
354
+ }
355
+ /* Figure F.21: Decoding nonzero value v */
356
+ /* Figure F.22: Decoding the sign of v */
357
+ entropy->ac_stats[tbl][245] = 0;
358
+ sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
359
+ st += 2;
360
+ /* Figure F.23: Decoding the magnitude category of v */
361
+ if ((m = arith_decode(cinfo, st)) != 0) {
362
+ if (arith_decode(cinfo, st)) {
363
+ m <<= 1;
364
+ st = entropy->ac_stats[tbl] +
365
+ (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
366
+ while (arith_decode(cinfo, st)) {
367
+ if ((m <<= 1) == 0x8000) {
368
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
369
+ entropy->ct = -1; /* magnitude overflow */
370
+ return TRUE;
371
+ }
372
+ st += 1;
373
+ }
374
+ }
375
+ }
376
+ v = m;
377
+ /* Figure F.24: Decoding the magnitude bit pattern of v */
378
+ st += 14;
379
+ while (m >>= 1)
380
+ if (arith_decode(cinfo, st)) v |= m;
381
+ v += 1; if (sign) v = -v;
382
+ /* Scale and output coefficient in natural (dezigzagged) order */
383
+ (*block)[jpeg_natural_order[k]] = (JCOEF) (v << cinfo->Al);
384
+ }
385
+
386
+ return TRUE;
387
+ }
388
+
389
+
390
+ /*
391
+ * MCU decoding for DC successive approximation refinement scan.
392
+ */
393
+
394
+ METHODDEF(boolean)
395
+ decode_mcu_DC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
396
+ {
397
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
398
+ unsigned char st[4];
399
+ int p1, blkn;
400
+
401
+ /* Process restart marker if needed */
402
+ if (cinfo->restart_interval) {
403
+ if (entropy->restarts_to_go == 0)
404
+ process_restart(cinfo);
405
+ entropy->restarts_to_go--;
406
+ }
407
+
408
+ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
409
+
410
+ /* Outer loop handles each block in the MCU */
411
+
412
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
413
+ st[0] = 0; /* use fixed probability estimation */
414
+ /* Encoded data is simply the next bit of the two's-complement DC value */
415
+ if (arith_decode(cinfo, st))
416
+ MCU_data[blkn][0][0] |= p1;
417
+ }
418
+
419
+ return TRUE;
420
+ }
421
+
422
+
423
+ /*
424
+ * MCU decoding for AC successive approximation refinement scan.
425
+ */
426
+
427
+ METHODDEF(boolean)
428
+ decode_mcu_AC_refine (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
429
+ {
430
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
431
+ JBLOCKROW block;
432
+ JCOEFPTR thiscoef;
433
+ unsigned char *st;
434
+ int tbl, k, kex;
435
+ int p1, m1;
436
+
437
+ /* Process restart marker if needed */
438
+ if (cinfo->restart_interval) {
439
+ if (entropy->restarts_to_go == 0)
440
+ process_restart(cinfo);
441
+ entropy->restarts_to_go--;
442
+ }
443
+
444
+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
445
+
446
+ /* There is always only one block per MCU */
447
+ block = MCU_data[0];
448
+ tbl = cinfo->cur_comp_info[0]->ac_tbl_no;
449
+
450
+ p1 = 1 << cinfo->Al; /* 1 in the bit position being coded */
451
+ m1 = (-1) << cinfo->Al; /* -1 in the bit position being coded */
452
+
453
+ /* Establish EOBx (previous stage end-of-block) index */
454
+ for (kex = cinfo->Se + 1; kex > 1; kex--)
455
+ if ((*block)[jpeg_natural_order[kex - 1]]) break;
456
+
457
+ for (k = cinfo->Ss; k <= cinfo->Se; k++) {
458
+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
459
+ if (k >= kex)
460
+ if (arith_decode(cinfo, st)) break; /* EOB flag */
461
+ for (;;) {
462
+ thiscoef = *block + jpeg_natural_order[k];
463
+ if (*thiscoef) { /* previously nonzero coef */
464
+ if (arith_decode(cinfo, st + 2)) {
465
+ if (*thiscoef < 0)
466
+ *thiscoef += m1;
467
+ else
468
+ *thiscoef += p1;
469
+ }
470
+ break;
471
+ }
472
+ if (arith_decode(cinfo, st + 1)) { /* newly nonzero coef */
473
+ entropy->ac_stats[tbl][245] = 0;
474
+ if (arith_decode(cinfo, entropy->ac_stats[tbl] + 245))
475
+ *thiscoef = m1;
476
+ else
477
+ *thiscoef = p1;
478
+ break;
479
+ }
480
+ st += 3; k++;
481
+ if (k > cinfo->Se) {
482
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
483
+ entropy->ct = -1; /* spectral overflow */
484
+ return TRUE;
485
+ }
486
+ }
487
+ }
488
+
489
+ return TRUE;
490
+ }
491
+
492
+
493
+ /*
494
+ * Decode one MCU's worth of arithmetic-compressed coefficients.
495
+ */
496
+
497
+ METHODDEF(boolean)
498
+ decode_mcu (j_decompress_ptr cinfo, JBLOCKROW *MCU_data)
499
+ {
500
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
501
+ jpeg_component_info * compptr;
502
+ JBLOCKROW block;
503
+ unsigned char *st;
504
+ int blkn, ci, tbl, sign, k;
505
+ int v, m;
506
+
507
+ /* Process restart marker if needed */
508
+ if (cinfo->restart_interval) {
509
+ if (entropy->restarts_to_go == 0)
510
+ process_restart(cinfo);
511
+ entropy->restarts_to_go--;
512
+ }
513
+
514
+ if (entropy->ct == -1) return TRUE; /* if error do nothing */
515
+
516
+ /* Outer loop handles each block in the MCU */
517
+
518
+ for (blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++) {
519
+ block = MCU_data[blkn];
520
+ ci = cinfo->MCU_membership[blkn];
521
+ compptr = cinfo->cur_comp_info[ci];
522
+
523
+ /* Sections F.2.4.1 & F.1.4.4.1: Decoding of DC coefficients */
524
+
525
+ tbl = compptr->dc_tbl_no;
526
+
527
+ /* Table F.4: Point to statistics bin S0 for DC coefficient coding */
528
+ st = entropy->dc_stats[tbl] + entropy->dc_context[ci];
529
+
530
+ /* Figure F.19: Decode_DC_DIFF */
531
+ if (arith_decode(cinfo, st) == 0)
532
+ entropy->dc_context[ci] = 0;
533
+ else {
534
+ /* Figure F.21: Decoding nonzero value v */
535
+ /* Figure F.22: Decoding the sign of v */
536
+ sign = arith_decode(cinfo, st + 1);
537
+ st += 2; st += sign;
538
+ /* Figure F.23: Decoding the magnitude category of v */
539
+ if ((m = arith_decode(cinfo, st)) != 0) {
540
+ st = entropy->dc_stats[tbl] + 20; /* Table F.4: X1 = 20 */
541
+ while (arith_decode(cinfo, st)) {
542
+ if ((m <<= 1) == 0x8000) {
543
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
544
+ entropy->ct = -1; /* magnitude overflow */
545
+ return TRUE;
546
+ }
547
+ st += 1;
548
+ }
549
+ }
550
+ /* Section F.1.4.4.1.2: Establish dc_context conditioning category */
551
+ if (m < (int) (((INT32) 1 << cinfo->arith_dc_L[tbl]) >> 1))
552
+ entropy->dc_context[ci] = 0; /* zero diff category */
553
+ else if (m > (int) (((INT32) 1 << cinfo->arith_dc_U[tbl]) >> 1))
554
+ entropy->dc_context[ci] = 12 + (sign * 4); /* large diff category */
555
+ else
556
+ entropy->dc_context[ci] = 4 + (sign * 4); /* small diff category */
557
+ v = m;
558
+ /* Figure F.24: Decoding the magnitude bit pattern of v */
559
+ st += 14;
560
+ while (m >>= 1)
561
+ if (arith_decode(cinfo, st)) v |= m;
562
+ v += 1; if (sign) v = -v;
563
+ entropy->last_dc_val[ci] += v;
564
+ }
565
+
566
+ (*block)[0] = (JCOEF) entropy->last_dc_val[ci];
567
+
568
+ /* Sections F.2.4.2 & F.1.4.4.2: Decoding of AC coefficients */
569
+
570
+ tbl = compptr->ac_tbl_no;
571
+
572
+ /* Figure F.20: Decode_AC_coefficients */
573
+ for (k = 1; k < DCTSIZE2; k++) {
574
+ st = entropy->ac_stats[tbl] + 3 * (k - 1);
575
+ if (arith_decode(cinfo, st)) break; /* EOB flag */
576
+ while (arith_decode(cinfo, st + 1) == 0) {
577
+ st += 3; k++;
578
+ if (k >= DCTSIZE2) {
579
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
580
+ entropy->ct = -1; /* spectral overflow */
581
+ return TRUE;
582
+ }
583
+ }
584
+ /* Figure F.21: Decoding nonzero value v */
585
+ /* Figure F.22: Decoding the sign of v */
586
+ entropy->ac_stats[tbl][245] = 0;
587
+ sign = arith_decode(cinfo, entropy->ac_stats[tbl] + 245);
588
+ st += 2;
589
+ /* Figure F.23: Decoding the magnitude category of v */
590
+ if ((m = arith_decode(cinfo, st)) != 0) {
591
+ if (arith_decode(cinfo, st)) {
592
+ m <<= 1;
593
+ st = entropy->ac_stats[tbl] +
594
+ (k <= cinfo->arith_ac_K[tbl] ? 189 : 217);
595
+ while (arith_decode(cinfo, st)) {
596
+ if ((m <<= 1) == 0x8000) {
597
+ WARNMS(cinfo, JWRN_ARITH_BAD_CODE);
598
+ entropy->ct = -1; /* magnitude overflow */
599
+ return TRUE;
600
+ }
601
+ st += 1;
602
+ }
603
+ }
604
+ }
605
+ v = m;
606
+ /* Figure F.24: Decoding the magnitude bit pattern of v */
607
+ st += 14;
608
+ while (m >>= 1)
609
+ if (arith_decode(cinfo, st)) v |= m;
610
+ v += 1; if (sign) v = -v;
611
+ (*block)[jpeg_natural_order[k]] = (JCOEF) v;
612
+ }
613
+ }
614
+
615
+ return TRUE;
616
+ }
617
+
618
+
619
+ /*
620
+ * Initialize for an arithmetic-compressed scan.
621
+ */
622
+
623
+ METHODDEF(void)
624
+ start_pass (j_decompress_ptr cinfo)
625
+ {
626
+ arith_entropy_ptr entropy = (arith_entropy_ptr) cinfo->entropy;
627
+ int ci, tbl;
628
+ jpeg_component_info * compptr;
629
+
630
+ if (cinfo->progressive_mode) {
631
+ /* Validate progressive scan parameters */
632
+ if (cinfo->Ss == 0) {
633
+ if (cinfo->Se != 0)
634
+ goto bad;
635
+ } else {
636
+ /* need not check Ss/Se < 0 since they came from unsigned bytes */
637
+ if (cinfo->Se < cinfo->Ss || cinfo->Se >= DCTSIZE2)
638
+ goto bad;
639
+ /* AC scans may have only one component */
640
+ if (cinfo->comps_in_scan != 1)
641
+ goto bad;
642
+ }
643
+ if (cinfo->Ah != 0) {
644
+ /* Successive approximation refinement scan: must have Al = Ah-1. */
645
+ if (cinfo->Ah-1 != cinfo->Al)
646
+ goto bad;
647
+ }
648
+ if (cinfo->Al > 13) { /* need not check for < 0 */
649
+ bad:
650
+ ERREXIT4(cinfo, JERR_BAD_PROGRESSION,
651
+ cinfo->Ss, cinfo->Se, cinfo->Ah, cinfo->Al);
652
+ }
653
+ /* Update progression status, and verify that scan order is legal.
654
+ * Note that inter-scan inconsistencies are treated as warnings
655
+ * not fatal errors ... not clear if this is right way to behave.
656
+ */
657
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
658
+ int coefi, cindex = cinfo->cur_comp_info[ci]->component_index;
659
+ int *coef_bit_ptr = & cinfo->coef_bits[cindex][0];
660
+ if (cinfo->Ss && coef_bit_ptr[0] < 0) /* AC without prior DC scan */
661
+ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, 0);
662
+ for (coefi = cinfo->Ss; coefi <= cinfo->Se; coefi++) {
663
+ int expected = (coef_bit_ptr[coefi] < 0) ? 0 : coef_bit_ptr[coefi];
664
+ if (cinfo->Ah != expected)
665
+ WARNMS2(cinfo, JWRN_BOGUS_PROGRESSION, cindex, coefi);
666
+ coef_bit_ptr[coefi] = cinfo->Al;
667
+ }
668
+ }
669
+ /* Select MCU decoding routine */
670
+ if (cinfo->Ah == 0) {
671
+ if (cinfo->Ss == 0)
672
+ entropy->pub.decode_mcu = decode_mcu_DC_first;
673
+ else
674
+ entropy->pub.decode_mcu = decode_mcu_AC_first;
675
+ } else {
676
+ if (cinfo->Ss == 0)
677
+ entropy->pub.decode_mcu = decode_mcu_DC_refine;
678
+ else
679
+ entropy->pub.decode_mcu = decode_mcu_AC_refine;
680
+ }
681
+ } else {
682
+ /* Check that the scan parameters Ss, Se, Ah/Al are OK for sequential JPEG.
683
+ * This ought to be an error condition, but we make it a warning because
684
+ * there are some baseline files out there with all zeroes in these bytes.
685
+ */
686
+ if (cinfo->Ss != 0 || cinfo->Se != DCTSIZE2-1 ||
687
+ cinfo->Ah != 0 || cinfo->Al != 0)
688
+ WARNMS(cinfo, JWRN_NOT_SEQUENTIAL);
689
+ /* Select MCU decoding routine */
690
+ entropy->pub.decode_mcu = decode_mcu;
691
+ }
692
+
693
+ for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
694
+ compptr = cinfo->cur_comp_info[ci];
695
+ /* Allocate & initialize requested statistics areas */
696
+ if (cinfo->progressive_mode == 0 || (cinfo->Ss == 0 && cinfo->Ah == 0)) {
697
+ tbl = compptr->dc_tbl_no;
698
+ if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
699
+ ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
700
+ if (entropy->dc_stats[tbl] == NULL)
701
+ entropy->dc_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
702
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, DC_STAT_BINS);
703
+ MEMZERO(entropy->dc_stats[tbl], DC_STAT_BINS);
704
+ /* Initialize DC predictions to 0 */
705
+ entropy->last_dc_val[ci] = 0;
706
+ entropy->dc_context[ci] = 0;
707
+ }
708
+ if (cinfo->progressive_mode == 0 || cinfo->Ss) {
709
+ tbl = compptr->ac_tbl_no;
710
+ if (tbl < 0 || tbl >= NUM_ARITH_TBLS)
711
+ ERREXIT1(cinfo, JERR_NO_ARITH_TABLE, tbl);
712
+ if (entropy->ac_stats[tbl] == NULL)
713
+ entropy->ac_stats[tbl] = (unsigned char *) (*cinfo->mem->alloc_small)
714
+ ((j_common_ptr) cinfo, JPOOL_IMAGE, AC_STAT_BINS);
715
+ MEMZERO(entropy->ac_stats[tbl], AC_STAT_BINS);
716
+ }
717
+ }
718
+
719
+ /* Initialize arithmetic decoding variables */
720
+ entropy->c = 0;
721
+ entropy->a = 0;
722
+ entropy->ct = -16; /* force reading 2 initial bytes to fill C */
723
+
724
+ /* Initialize restart counter */
725
+ entropy->restarts_to_go = cinfo->restart_interval;
726
+ }
727
+
728
+
729
+ /*
730
+ * Module initialization routine for arithmetic entropy decoding.
731
+ */
732
+
733
+ GLOBAL(void)
734
+ jinit_arith_decoder (j_decompress_ptr cinfo)
735
+ {
736
+ arith_entropy_ptr entropy;
737
+ int i;
738
+
739
+ entropy = (arith_entropy_ptr)
740
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
741
+ SIZEOF(arith_entropy_decoder));
742
+ cinfo->entropy = (struct jpeg_entropy_decoder *) entropy;
743
+ entropy->pub.start_pass = start_pass;
744
+
745
+ /* Mark tables unallocated */
746
+ for (i = 0; i < NUM_ARITH_TBLS; i++) {
747
+ entropy->dc_stats[i] = NULL;
748
+ entropy->ac_stats[i] = NULL;
749
+ }
750
+
751
+ if (cinfo->progressive_mode) {
752
+ /* Create progression status table */
753
+ int *coef_bit_ptr, ci;
754
+ cinfo->coef_bits = (int (*)[DCTSIZE2])
755
+ (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
756
+ cinfo->num_components*DCTSIZE2*SIZEOF(int));
757
+ coef_bit_ptr = & cinfo->coef_bits[0][0];
758
+ for (ci = 0; ci < cinfo->num_components; ci++)
759
+ for (i = 0; i < DCTSIZE2; i++)
760
+ *coef_bit_ptr++ = -1;
761
+ }
762
+ }