qc_is_parameter qc_perform_substitutions navigation bar

Table of Contents

qc_jpeg_decompress

Decompresses a JPEG image from a file or memory.

Syntax

[img, result] = qc_jpeg_decompress(jpeg_bytes, colorspace, downscaling) % decompress vector of bytes, override colorspace and downscale
[img, result] = qc_jpeg_decompress(filename, colorspace, downscaling)   % decompress file, override colorspace and downscale
[img, result] = qc_jpeg_decompress(jpeg_bytes, colorspace)              % decompress vector of bytes, override colorspace
[img, result] = qc_jpeg_decompress(filename, colorspace)                % decompress file, override colorspace
[img, result] = qc_jpeg_decompress(jpeg_bytes, downscaling)             % decompress vector of bytes, downscaling image
[img, result] = qc_jpeg_decompress(filename, downscaling)               % decompress file, override downscaling image
[img, result] = qc_jpeg_decompress(jpeg_bytes)                          % decompress vector of bytes
[img, result] = qc_jpeg_decompress(filename)                            % decompress file
    

Description

Decompresses a JPEG file or an array of bytes containing a JPEG image. This function differs from the imread function in that it can handle an array of bytes (e.g. transmitted over a network) or a file. It can also change the color space of the image or perform simple downscaling of the image. It only works with JPEG images.

If a COLORSPACE argument is provided it must be 'grayscale', 'greyscale' or 'rgb' (case-insensitive), indicating the desired colorspace of the output image. This setting will override the format of the image so that color JPEG images can be converted to grayscale images and vice versa during decompression. If no COLORSPACE argument is provided then the decompressed image will match the format in the JPEG file (typically RGB).

If the DOWNSCALING option is provided it must be an integer scalar (typically 1, 2, 4 or 8). The width and height of the image will be shrunk by the DOWNSCALING factor, so specifying a value of 2 will result in the output image being half the width and height of the compressed image. If no DOWNSCALING option is provided then the decompressed image will be the same size as the compressed image.

Parameters

jpeg_bytes

An array of bytes (uint8 or int8) containing the compressed JPEG image.

colorspace

Either the string 'grayscale' or 'rgb' to indicate the colorspace for the resulting image.

downscaling

An integer factor by which the image will be downscaled during decompression. Only values of 1, 2, 4 or 8 are supported.

Outputs

img

The decompressed image as either a grayscale HxW matrix, or an RGB 3xHxW matrix of uint8.

result

Returns the number of scan lines decoded, which is the height of the model upon success. If an error occurs then a negative error code is returned instead. See qc_error and qc_get_error_message for handling error codes. If no result output is assigned it issues an error on failure.

Examples

% Decompress a JPEG file
result = qc_jpeg_decompress('image.jpg'); % Decompresses the 'image.jpg' file.

% Decompress a vector of bytes containing a JPEG image
f = fopen('image.jpg', 'rb');
b = fread(f, inf, '*uint8'); % Read in a JPEG image into a vector of bytes
fclose(f);

result = qc_jpeg_decompress(b); % Decompresses the vector of bytes containing the JPEG image.
    

See Also

 

navigation bar