port upstream fix for local buffer overflow

r1425

Fix Huffman local buffer overrun discovered by Debian developers when
attempting to transform a junk image using ImageMagick:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=768369
Merge pull request #136 from tony2001/master

port upstream fix for local buffer overflow
No scan opt if custom scan

Disables scan pattern optimization (JBOOLEAN_OPTIMIZE_SCANS extension
parameter) if a custom scan pattern is provided.
Fixes #137
Temp fix for #138

Disable scan optimization and trellis quantization when arithmetic
coding is used
Partial fix for #138

Fix pass number computation in scan optimization to support case where
Huffman table optimization is not done, e.g. when arithmetic coding is
used
Enable combination of arithmetic coding and scan optimization
(previously disabled)