Magnum::Trade::JpegImageConverter class

JPEG image converter plugin.

Creates JPEG (*.jpg, *.jpe, *.jpeg) files using the libJPEG library. You can use JpegImporter to import images in this format.


This plugin depends on the Trade and libJPEG libraries and is built if MAGNUM_WITH_JPEGIMAGECONVERTER is enabled when building Magnum Plugins. To use as a dynamic plugin, load "JpegImageConverter" via Corrade::PluginManager::Manager.

Additionally, if you're using Magnum as a CMake subproject, bundle the magnum-plugins repository and do the following. Using libJPEG itself as a CMake subproject isn't tested at the moment, so you need to provide it as a system dependency and point CMAKE_PREFIX_PATH to its installation dir if necessary.

add_subdirectory(magnum-plugins EXCLUDE_FROM_ALL)

# So the dynamically loaded plugin gets built implicitly
add_dependencies(your-app MagnumPlugins::JpegImageConverter)

To use as a static plugin or as a dependency of another plugin with CMake, put FindMagnumPlugins.cmake into your modules/ directory, request the JpegImageConverter component of the MagnumPlugins package and link to the MagnumPlugins::JpegImageConverter target:

find_package(MagnumPlugins REQUIRED JpegImageConverter)

# ...
target_link_libraries(your-app PRIVATE MagnumPlugins::JpegImageConverter)

See Downloading and building plugins, Plugin usage with CMake, Loading and using plugins and File format support for more information.

Behavior and limitations

Accepts 2D images in PixelFormat::R8Unorm or PixelFormat::RGB8Unorm. Images in PixelFormat::RGBA8Unorm are supported only if you use libJPEG Turbo instead of vanilla libJPEG and the alpha channel gets ignored (with a warning printed to Error). PixelFormat::RG8Unorm can't be easily supported, see StbImageConverter for an alternative with a possibility to export RG images as a grayscale JPEG.

The JPEG file format doesn't have a way to distinguish between 2D and 1D array images. If an image has ImageFlag2D::Array set, a warning is printed and the file is saved as a regular 2D image.

While JPEG files can have several extensions, extension() always returns "jpg" as that's the most common one.

The plugin recognizes ImageConverterFlag::Quiet, which will cause all conversion warnings to be suppressed.

libJPEG implementations

While some systems (such as macOS) still ship only with the vanilla libJPEG, you can get a much better performance and/or quality/size ratios by using other implementations:

  • libjpeg-turbo, optimized for compression and decompression speed, though not necessarily the best quality/size ratio
  • MozJPEG, optimized for quality/size ratio, though generally much slower than libjpeg-turbo

Arithmetic JPEG encoding

Libjpeg has a switch to enable arithmetic coding instead of Huffman, however it's currently not exposed in the plugin.

Plugin-specific configuration

It's possible to tune various output options through configuration(). See below for all options and their default values:

# Compression quality (0 - 1, 1 is the best)

See Editing plugin-specific configuration for more information and an example showing how to edit the configuration values.

Base classes

class AbstractImageConverter
Base for image converter plugins.

Constructors, destructors, conversion operators

JpegImageConverter() explicit
Default constructor.
JpegImageConverter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin) explicit
Plugin manager constructor.