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

The namespace imageOperations gathers static methods for handling, analyzing and manipulating QImages. More...

Enumerations

enum  imageDataFilter { identity , PNG }
 Image Data Filters. More...
 

Functions

QImage deAlpha (const QImage &image, QRgb background=0xFFFFFFFF)
 Removes alpha channel from image. More...
 
QVector< QRgb > colors (const QImage &image)
 Finds the set of all colors used in a given image. More...
 
bool isBlackAndWhite (const QImage &image)
 Determine whether an image is black-and-white only. More...
 
bool isOpaque (const QImage &image)
 Check if the image is opaque. More...
 
QImage optimizedFormat (const QImage &inputImage)
 Convert image to memory-saving format. More...
 
QImage simplifyTransparentPixels (const QImage &image)
 Replaces all fully transparent pixels by pixels of color 0x00FFFFFF (="fully transparent white") More...
 
QByteArray QImageToData (const QImage &image, imageDataFilter filter=identity)
 Returns the image raw data as a consecutive QByteArray. More...
 
QImage DataToQImage (const QByteArray &data, int width, int height, QImage::Format format, imageDataFilter filter=identity)
 Reconstructs an image from raw data, as written by QImageToData. More...
 

Detailed Description

The namespace imageOperations gathers static methods for handling, analyzing and manipulating QImages.

Enumeration Type Documentation

◆ imageDataFilter

Image Data Filters.

Several image storage formats, including PDF, PNG and TIFF apply losless filter algorithms to the image data before compression. The purpose of these filters is to prepare the image data for optimum compression. The scantools library supports a number of these.

Enumerator
identity 

The image data is returned without modification.

PNG 

Filter defined in the PNG specification.

This filter is used in PNG graphics and in PDF files. It combines five methods specified in the PNG Specification ('None', 'Sub', 'Up', 'Average', 'Paeth') and chooses one of these filters for each scanline according to the heurestics 'sum of absolute differences'.

See also
Chapter 3.3 in the PDF reference
Chapter 6.6 in the PNG (Portable Network Graphics) Specification, Version 1.2

Definition at line 146 of file imageOperations.h.

Function Documentation

◆ colors()

QVector<QRgb> imageOperations::colors ( const QImage &  image)

Finds the set of all colors used in a given image.

This method returns a vector containing the colors that used in a given image. No color will appear twice in the vector returned. In particular, if 'image' uses a color index, the method returns a vector that contains only those colors that are actually used. If more than 256 colors are used, an empty vector is returned.

Warning
This method is slow. This method considers fully transparent colors different if their RGB-components differ. For instance, the colors 0x00FF00FF and 0x00AABBCC are considered different, even though their visual appearance will be identical.
Parameters
imageImage to be analyzed
Returns
Vector with all colors that are actually used
See also
simplifyTransparentPixels()

◆ DataToQImage()

QImage imageOperations::DataToQImage ( const QByteArray &  data,
int  width,
int  height,
QImage::Format  format,
imageDataFilter  filter = identity 
)

Reconstructs an image from raw data, as written by QImageToData.

Reconstructs a QImage from raw data, as written by QImageToData.

Parameters
dataQByteArray holding the image data. The size of the array must match the size of the data exactly.
widthWidth of the original image
heightHeight of the original image
formatFormat of the original image
filterFilter that was used when the raw data was generated.
Returns
Reconstructed image. Note that the method does NOT restore image metadata, nor does it store the color vector of indexed images. In case of error, a NULL image is returned.

◆ deAlpha()

QImage imageOperations::deAlpha ( const QImage &  image,
QRgb  background = 0xFFFFFFFF 
)

Removes alpha channel from image.

This method replaces transparent colors with a suitable opaque colors, effectively rendering the image over an opaque image with the given background color.

Parameters
imageImage whose alpha channel will be removed
backgroundBackground color
Returns
Copy of the image, with alpha channel removed

◆ isBlackAndWhite()

bool imageOperations::isBlackAndWhite ( const QImage &  image)

Determine whether an image is black-and-white only.

This method determines whether the RGB-part of all pixel colors are 0x000000 of 0xFFFFFF.

Parameters
imageImage to be analyzed
Returns
True if the image contains black-and-white only
Warning
This method is slow. An image that uses a fully transparent color whose RGB-component is not black or white, such as 0x00FF00FF, is not considered a black-and-white image.
See also
simplifyTransparentPixels()

◆ isOpaque()

bool imageOperations::isOpaque ( const QImage &  image)

Check if the image is opaque.

Parameters
imageImage to be analyzed
Returns
Returns 'true' if the image contains only pixels whose alpha-component is different from 0xFF = 'opaque'. In particular, if the image has no alpha channel, then 'true' is returned.
Warning
This method is slow. For images with a color table, this method can return 'true' even if the color table contains transparent colors, as it might happen that these transparent colore are simply not used in the acutal image.

◆ optimizedFormat()

QImage imageOperations::optimizedFormat ( const QImage &  inputImage)

Convert image to memory-saving format.

This method analyzes the colors in a given image, determines the most memory-efficient format for storing the image and converts it to that format. The output image is guaranteed to be in one of the following formats.

  • QImage::Format_Mono, with a color table that contains one or two colors. In case of two colors, the lighter color is guaranteed to come first.
  • QImage::Format_Indexed8, with a color table, where all colors are actually used, and no color appears twice.
  • QImage::Format_Grayscale8.
  • QImage::Format_RGB32
  • QImage::Format_ARGB32
Parameters
inputImageInput image.
Returns
An image equivalent to inputImage in one of the formats listed above.
Warning
This method is very slow.

◆ QImageToData()

QByteArray imageOperations::QImageToData ( const QImage &  image,
imageDataFilter  filter = identity 
)

Returns the image raw data as a consecutive QByteArray.

QImage organises images in scanlines. For efficiency reasons, the size of a scanline can be larger than the number of bytes actually needed to store the pixel data. According to Qt documentation, there is no guarantee that the image data is stored in one connected memory block.

For images in one of the supported formats, this method returns the image data in one consecutive block, with the minimum number of bytes required. If desired, the method applys a lossless filter algorithm on the data, to prepare the image data for optimum compression. The supportes image formats are exactly those returned by optimizedFormat().

  • QImage::Format_Mono: The number of bytes in the output QByteArray is round-up(width/8)*height.
  • QImage::Format_Indexed8: The number of bytes in the output QByteArray is width*height.
  • QImage::Format_ARGB32: The number of bytes in the output QByteArray is 4*width*height, plus additional bytes if PNG filters are used (see below).
  • QImage::Format_RGB888: The number of bytes in the output QByteArray is 3*width*height, plus additional bytes if PNG filters are used (see below).
  • QImage::Format_Grayscale8: The number of bytes in the output QByteArray is width*height, plus additional bytes if PNG filters are used (see below).
Parameters
imageQImage whose data is returned.
filterThe filter to be used.
Returns
A QByteArray containing the (filtered) image data, or an empty array if the image was empty, if the image format was not supported, or if the filter was not available for the given format.

◆ simplifyTransparentPixels()

QImage imageOperations::simplifyTransparentPixels ( const QImage &  image)

Replaces all fully transparent pixels by pixels of color 0x00FFFFFF (="fully transparent white")

Parameters
imageImage to be optimized
Returns
A copy of the image where all fully transparent pixels are replaced by pixels of color 0x00FFFFFF (="fully transparent white")
Warning
This method is slow on images that do support an alpha channel, and do not use a color table.