Guangdong Juyao Intelligent Technology Co., Ltd. · Software & Algorithm Engineering
23°07′N 113°15′E · GUANGZHOU · PURE STATIC MONOGRAPH
Back to Open Source

pnggroup/libpng · 1.6.59 · libpng-2.0 · C++17

How does libpng composite a transparent edge?

Place three black pixels with different alpha values over white and black. Follow libpng source to see why the middle pixel becomes 186.

Example result With libpng 1.6.59, black pixels at alpha 0, 128, and 255 composite to 255, 186, and 0 per channel on white. All three produce 0 on black.

Why does an icon change when its backdrop changes?

An engraving editor importing a transparent PNG logo has to blend its edges with the canvas. If the importer drops alpha too early, hidden RGB values can reach the grayscale or dithering stage and leave an unwanted background block. This experiment uses the PNG reference library, libpng, to composite the same three black pixels over white and black. libpng project home

An editor can keep the original transparency, composite a copy for its preview, and apply the engraving layer’s own policy when deciding which pixels enter raster preprocessing. Both paths then start from the same source with an explicit treatment of transparency. Engraving basics · bitmaps and vectors

libpng · 1.6.59 · libpng-2.0

Three branches in the RGBA-to-RGB loop

Three black pixels composited on white and black backgrounds
Left to right, alpha is 0, 128 and 255. The output channel values on white are 255, 186 and 0. On black, all three are 0.

The example writes a 3 × 1 RGBA PNG in memory with the simplified API, then reads it twice. png_image_finish_read is called with PNG_FORMAT_RGB output and an explicit png_color backdrop. When the input has alpha and the requested output omits it, the decoder passes the supplied backdrop to png_set_background_fixed and identifies it as a color in the output gamma space.

In the 8-bit RGBA loop of png_do_compose, alpha 0 copies the backdrop and alpha 255 gamma-maps the foreground alone. In between, gamma_to_1 supplies a linear foreground value, which is mixed with the linear backdrop according to coverage. gamma_from_1 encodes the result. The linear blend in each channel is α × foreground + (1 − α) × backdrop, where α = A / 255.

Partially transparent black over white produces about 0.498 linear intensity. The gamma approximation in this path encodes it as 186. Weighting the 8-bit encoded values directly would give 127. This experiment follows the explicit-backdrop, 8-bit RGB output path. Other color types, depths, or backdrop parameters may select different transforms. PNG specification · alpha processing

Simplified API backdrop selection · The 8-bit RGBA compositing loop · Gamma table construction

Build and run locally

Use a terminal on macOS or Linux with curl, tar, CMake 3.14+, zlib development files, and a C++17 toolchain. The example links the static library built from the upstream source. It generates its own pixels. Tested on Apple Silicon macOS.

Download the C++17 example · Plain-text build commands

Read the example code
#include <array>
#include <iostream>
#include <stdexcept>
#include <vector>
#include <png.h>

std::vector<png_byte> make_png() {
    png_image image{};
    image.version = PNG_IMAGE_VERSION;
    image.width = 3;
    image.height = 1;
    image.format = PNG_FORMAT_RGBA;
    const std::array<png_byte, 12> pixels{
        0, 0, 0, 0,   0, 0, 0, 128,   0, 0, 0, 255
    };

    png_alloc_size_t size = 0;
    if (!png_image_write_to_memory(&image, nullptr, &size, 0, pixels.data(), 0, nullptr))
        throw std::runtime_error(image.message);
    std::vector<png_byte> encoded(size);
    if (!png_image_write_to_memory(&image, encoded.data(), &size, 0, pixels.data(), 0, nullptr))
        throw std::runtime_error(image.message);
    encoded.resize(size);
    return encoded;
}

void print_composite(const std::vector<png_byte>& encoded, const char* label,
                     const png_color& backdrop) {
    png_image image{};
    image.version = PNG_IMAGE_VERSION;
    if (!png_image_begin_read_from_memory(&image, encoded.data(), encoded.size()))
        throw std::runtime_error(image.message);

    image.format = PNG_FORMAT_RGB;
    std::array<png_byte, 9> rgb{};
    if (!png_image_finish_read(&image, &backdrop, rgb.data(), 0, nullptr))
        throw std::runtime_error(image.message);

    std::cout << label;
    for (std::size_t i = 0; i < rgb.size(); i += 3)
        std::cout << " (" << +rgb[i] << ',' << +rgb[i + 1] << ',' << +rgb[i + 2] << ')';
    std::cout << '\n';
}

int main() {
    try {
        const auto encoded = make_png();
        std::cout << "libpng " << png_get_libpng_ver(nullptr) << '\n';
        std::cout << "black foreground, alpha: 0 128 255\n";
        print_composite(encoded, "white backdrop:", {255, 255, 255});
        print_composite(encoded, "black backdrop:", {0, 0, 0});
    } catch (const std::exception& error) {
        std::cerr << error.what() << '\n';
        return 1;
    }
}
mkdir libpng-study && cd libpng-study
curl -fsSL https://codeload.github.com/pnggroup/libpng/tar.gz/refs/tags/v1.6.59 -o libpng.tgz
tar -xzf libpng.tgz
curl -fsSLO https://www.juyaoai.com/examples/libpng-alpha-compositing/demo.cpp

cmake -S libpng-1.6.59 -B png-build -DCMAKE_BUILD_TYPE=Release \
  -DPNG_SHARED=OFF -DPNG_FRAMEWORK=OFF -DPNG_TESTS=OFF -DPNG_TOOLS=OFF
cmake --build png-build --target png_static --parallel 2
c++ -std=c++17 -I png-build -I libpng-1.6.59 demo.cpp png-build/libpng16.a -lz -o demo
./demo

Output and a follow-up experiment

libpng 1.6.59
black foreground, alpha: 0 128 255
white backdrop: (255,255,255) (186,186,186) (0,0,0)
black backdrop: (0,0,0) (0,0,0) (0,0,0)

Change the middle pixel’s alpha from 128 to 64 and the middle value over white rises from 186 to 224. To compare two preview surfaces, keep the RGBA source fixed and record each backdrop. The reported RGB values characterize the software canvas. Material response calls for a separate measurement.