How can JPEG decode directly to a half-size preview?
Decode a 17 × 9 grayscale JPEG and trace how libjpeg-turbo produces 4 × 4 pixels from an 8 × 8 coefficient block.
Example result libjpeg-turbo 3.2.1 decodes the same grayscale JPEG to 17 × 9 and 9 × 5. The first row’s dark-to-light boundary moves from column 8 to 4, counting from zero.
A smaller JPEG preview for the asset browser
An engraving editor needs small previews in its asset browser. It can read the photograph at the intended sampling size when the user brings it onto the canvas. The libjpeg API in libjpeg-turbo accepts a scale at decode time. This experiment uses version 3.2.1 for full- and half-size decoding. It builds the libjpeg API library, which upstream covers under the IJG license. See the full license file for the terms covering the TurboJPEG API and other components. libjpeg-turbo project home
Reducing the image in two inverse-transform passes
After reading the header, the example sets scale_num to 1 and uses scale_denom values of 1 and 2 before calling jpeg_start_decompress. The half-size branch in jpeg_core_output_dimensions selects four output samples per block dimension. It rounds each image dimension upward, yielding 9 × 5 from 17 × 9.
The IDCT manager chooses a transform according to the component’s output block size. With SIMD disabled here, half-scale grayscale reaches _jpeg_idct_4x4. In the column pass the routine multiplies coefficients by their corresponding quantization values and uses fixed-point arithmetic to combine eight coefficients into four intermediate values. It then combines the intermediates across each row, emits four samples, and rescales, rounds, and limits them to the sample range.
The one-dimensional 8-to-4 calculation is derived by averaging adjacent outputs of the ordinary inverse transform. Frequency term 4 cancels and is skipped, while other higher-frequency terms can still contribute. The synthetic stripe changes shade on a DCT block boundary so each block is constant, making output size and sampling easy to observe. Color JPEGs add concerns such as chroma subsampling and color conversion.
Output dimensions · 4 × 4 IDCT selection · The two-pass integer IDCT
Build and run locally
Use a terminal on macOS or Linux with curl, tar, CMake 3.15+, and a C++17 toolchain. The example encodes a quality-90 grayscale JPEG in memory and links libjpeg statically from the upstream source. Tested on Apple Silicon macOS.
Download the C++17 example · Plain-text build commands
Read the example code
#include <array>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <jpeglib.h>
#define STRINGIFY_VERSION_(version) #version
#define STRINGIFY_VERSION(version) STRINGIFY_VERSION_(version)
constexpr JDIMENSION width = 17;
constexpr JDIMENSION height = 9;
void make_jpeg(unsigned char** encoded, unsigned long* size) {
jpeg_compress_struct writer{};
jpeg_error_mgr error{};
writer.err = jpeg_std_error(&error);
jpeg_create_compress(&writer);
jpeg_mem_dest(&writer, encoded, size);
writer.image_width = width;
writer.image_height = height;
writer.input_components = 1;
writer.in_color_space = JCS_GRAYSCALE;
jpeg_set_defaults(&writer);
jpeg_set_quality(&writer, 90, TRUE);
writer.dct_method = JDCT_ISLOW;
std::array<JSAMPLE, width> pixels{};
for (JDIMENSION x = 0; x < width; ++x)
pixels[x] = x < 8 ? 32 : 224;
jpeg_start_compress(&writer, TRUE);
while (writer.next_scanline < writer.image_height) {
JSAMPROW row = pixels.data();
jpeg_write_scanlines(&writer, &row, 1);
}
jpeg_finish_compress(&writer);
jpeg_destroy_compress(&writer);
}
void decode_jpeg(const unsigned char* encoded, unsigned long size, unsigned denominator) {
jpeg_decompress_struct reader{};
jpeg_error_mgr error{};
reader.err = jpeg_std_error(&error);
jpeg_create_decompress(&reader);
jpeg_mem_src(&reader, encoded, size);
jpeg_read_header(&reader, TRUE);
reader.scale_num = 1;
reader.scale_denom = denominator;
reader.out_color_space = JCS_GRAYSCALE;
reader.dct_method = JDCT_ISLOW;
jpeg_start_decompress(&reader);
std::cout << "1/" << denominator << ": " << reader.output_width << 'x'
<< reader.output_height << ", first row:";
std::array<JSAMPLE, width> pixels{};
while (reader.output_scanline < reader.output_height) {
const bool first_row = reader.output_scanline == 0;
JSAMPROW row = pixels.data();
jpeg_read_scanlines(&reader, &row, 1);
if (first_row)
for (JDIMENSION x = 0; x < reader.output_width; ++x)
std::cout << ' ' << +pixels[x];
}
std::cout << '\n';
jpeg_finish_decompress(&reader);
jpeg_destroy_decompress(&reader);
}
int main() {
unsigned char* encoded = nullptr;
unsigned long size = 0;
make_jpeg(&encoded, &size);
std::cout << "libjpeg-turbo " << STRINGIFY_VERSION(LIBJPEG_TURBO_VERSION) << '\n';
decode_jpeg(encoded, size, 1);
decode_jpeg(encoded, size, 2);
std::free(encoded);
}mkdir jpeg-idct-study && cd jpeg-idct-study
curl -fsSL https://codeload.github.com/libjpeg-turbo/libjpeg-turbo/tar.gz/refs/tags/3.2.1 -o jpeg.tgz
tar -xzf jpeg.tgz
curl -fsSLO https://www.juyaoai.com/examples/libjpeg-turbo-scaled-idct/demo.cpp
cmake -S libjpeg-turbo-3.2.1 -B jpeg-build -DCMAKE_BUILD_TYPE=Release \
-DENABLE_SHARED=OFF -DWITH_SIMD=OFF -DWITH_TURBOJPEG=OFF
cmake --build jpeg-build --target jpeg-static --parallel 2
c++ -std=c++17 -I jpeg-build -I libjpeg-turbo-3.2.1/src demo.cpp jpeg-build/libjpeg.a -o demo
./demoOutput and a follow-up experiment
libjpeg-turbo 3.2.1
1/1: 17x9, first row: 32 32 32 32 32 32 32 32 224 224 224 224 224 224 224 224 224
1/2: 9x5, first row: 32 32 32 32 224 224 224 224 224Change the boundary from x < 8 to x < 7 and the half-size first row shows an intermediate edge value of 128. The break now falls inside a block, creating AC coefficients and making the edge sensitive to both quantization and scaling. An engraving editor can generate its final raster separately at target dimensions to control resampling and dithering.