scantools  1.0.4
Graphics manipulation with a view towards scanned documents
Enumerations | Functions
compression Namespace Reference

This namespace gathers static methods for data compression and decompression. More...

Enumerations

enum  zlibCompressionStrategy { standard , filtered }
 Compression strategy. More...
 

Functions

QImage faxG4Decode (const QByteArray &data, int width, int heightEstimate=0)
 Decompresses a bitonal image following the CCITT Group 4 compression standard. More...
 
QByteArray faxG4Encode (const QImage &image)
 Compresses a bitonal image following the CCITT Group 4 compression standard. More...
 
QByteArray zlibCompress (const QByteArray &src, zlibCompressionStrategy strategy=standard)
 Compresses a QByteArray using zlib. More...
 
QByteArray zlibUncompress (const QByteArray &src, quint64 estimatedOutputSize=0)
 Uncompresses a QByteArray using zlib. More...
 
QByteArray zopfliCompress (const QByteArray &src)
 Compresses a QByteArray using zopfli. More...
 

Detailed Description

This namespace gathers static methods for data compression and decompression.

Enumeration Type Documentation

◆ zlibCompressionStrategy

Compression strategy.

The zlib compression library allows to tune the compression algorithm, to achieve better compression rates depending on the type of input data.

Enumerator
standard 

The default strategy is best for general data.

filtered 

Strategy for data thar consists mostly of small values with a somewhat random distribution.

This compression algorithm works well for images whose data has been filtered.

Definition at line 36 of file compression.h.

Function Documentation

◆ faxG4Decode()

QImage compression::faxG4Decode ( const QByteArray &  data,
int  width,
int  heightEstimate = 0 
)

Decompresses a bitonal image following the CCITT Group 4 compression standard.

This method reconstructs a bitonal image from the CCITT Group 4 compressed data.

Parameters
dataData that is to be decoded.
widthWidth of the encoded image. Note that image size cannot be deducted from FAX data.
heightEstimateEstimate for the height of the encoded image. The height can be deducted from the FAX data, so that this data is optional. Decompression speed is, however, slightly better if the correct height is known in advance.
Returns
An image of format QImage::Format_Mono without color table. In case of error, an empty image is returned.

◆ faxG4Encode()

QByteArray compression::faxG4Encode ( const QImage &  image)

Compresses a bitonal image following the CCITT Group 4 compression standard.

This method compresses a bitonal image following the CCITT Group 4 compression standard (also known as "Fax G4" compression, defined in ITU-T T.6). This compression method is lossless, typically achieves a 20:1 compression ratio and is used widely in FAX machines, as well as in TIFF and PDF files.

Note
Note that the image colors and size is not stored. To restore the image later, this information needs to be stored separately.
The compression algorithm works best if color 0 is the background color of the image – the algorithm expect long runs of color 0 pixels and comparatively short runs of pixels of color 1.
The implementation has been tested for correctness, but has not (yet) been optimized for speed. Unlike all other free implementations of FAX compression that I have seen (i.e. libtiff and derivatives), this implementation is not base on ancient code of Sam Leffler and Silicon Graphics, Inc. dating back to the early 1990's. I can therefore guarantee with confidence that the method works well in a threaded environment.
See also
Wikipedia page on Group 4 compression
Parameters
imageThe image that is to be encoded. The method works best if the image is in format QImage::Format_Mono and if color 0 is the black and color 1 is white. If these conditions are not met, a local copy of the image is made and transformed to match these conditions; this will use extra processing time.
Returns
A QByteArray containing the encoded data, or an empty QByteArray in case of error.

◆ zlibCompress()

QByteArray compression::zlibCompress ( const QByteArray &  src,
zlibCompressionStrategy  strategy = standard 
)

Compresses a QByteArray using zlib.

Compresses the contents of a QByteArray using zlib. In contrast to Qt's method "qCompress", the output of this method is a valid zLib data stream.

Parameters
srcQByteArray holding uncompressed data
strategyStrategy used by zlib in the compression
Returns
A QByteArray containing the compressed data, or an empty QByteArray in case of error.

◆ zlibUncompress()

QByteArray compression::zlibUncompress ( const QByteArray &  src,
quint64  estimatedOutputSize = 0 
)

Uncompresses a QByteArray using zlib.

Compresses the contents of a QByteArray using zlib.

Warning
The zlib data stream does not contain any information concerning the size of the uncompressed data. If the size of the uncompressed data is known, this information should be passed on using the parameter 'estimatedOutputSize'. If no estimated output size the method reserves a buffer that is four times the input size and doubles the buffer size whenever uncompression fails with 'insufficient buffer size'. This trial-and-error approach is, however, far from optimal and can lead to substantial slowdown.
Parameters
srcQByteArray holding compressed data
estimatedOutputSizeExpected size of the uncompressed data, in bytes.
Returns
A QByteArray containing the uncompressed data, or an empty QByteArray in case of error.

◆ zopfliCompress()

QByteArray compression::zopfliCompress ( const QByteArray &  src)

Compresses a QByteArray using zopfli.

Compresses the contents of a QByteArray using Google's 'zopfli' implementation, which produces better results than zlib, but is slower by a factor of almost 100x. The output of this method is a valid zLib data stream.

Parameters
srcQByteArray holding uncompressed data
Returns
A QByteArray containing the compressed data, or an empty QByteArray in case of error.