Skip to content
Myr

png.c

This file

Path

png.c

222 lines · 6.4 KiB · C

Last changed by

Implement gamma-correct blendingDaniel Eklöfbefore adoption2d19826

History of this file

Take it

1#if defined(FUZZEL_ENABLE_PNG_LIBPNG)23#include "png-fuzzel.h"45#include <stdlib.h>6#include <stdio.h>7#include <stdbool.h>8#include <string.h>9#include <assert.h>1011#include <png.h>12#include <pixman.h>1314#define LOG_MODULE "png"15#define LOG_ENABLE_DBG 016#include "log.h"17#include "stride.h"18#include "xmalloc.h"1920static void21png_warning_cb(png_structp png_ptr, png_const_charp warning_msg)22{23    LOG_WARN("libpng: %s", warning_msg);24}2526pixman_image_t *27png_load(const char *path, bool gamma_correct)28{29    pixman_image_t *pix = NULL;3031    FILE *fp = NULL;32    png_structp png_ptr = NULL;33    png_infop info_ptr = NULL;34    png_bytepp row_pointers = NULL;35    uint8_t *image_data = NULL;3637    const pixman_format_code_t fmt_with_alpha =38        gamma_correct ? PIXMAN_a16b16g16r16 : PIXMAN_a8b8g8r8;39    const pixman_format_code_t fmt_without_alpha =40        gamma_correct ? PIXMAN_a16b16g16r16 : PIXMAN_x8b8g8r8;4142    /* open file and test for it being a png */43    if ((fp = fopen(path, "rbe")) == NULL) {44        //LOG_ERRNO("%s: failed to open", path);45        goto err;46    }4748    /* Verify PNG header */49    uint8_t header[8] = {0};50    if (fread(header, 1, 8, fp) != 8 || png_sig_cmp(header, 0, 8)) {51        // LOG_ERR("%s: not a PNG", path);52        goto err;53    }5455    /* Prepare for reading the PNG */56    if ((png_ptr = png_create_read_struct(57             PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)) == NULL ||58        (info_ptr = png_create_info_struct(png_ptr)) == NULL)59    {60        LOG_ERR("%s: failed to initialize libpng", path);61        goto err;62    }6364    if (setjmp(png_jmpbuf(png_ptr))) {65        LOG_ERR("%s: libpng error", path);66        goto err;67    }6869    /* Set custom “warning” function */70    png_set_error_fn(71        png_ptr, png_get_error_ptr(png_ptr), NULL, &png_warning_cb);7273    png_init_io(png_ptr, fp);74    png_set_sig_bytes(png_ptr, 8);7576    if (gamma_correct) {77        /*78         * 16-bit premultiplied linear pixels79         */80        png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, PNG_DEFAULT_sRGB);81        png_set_gamma(png_ptr, PNG_GAMMA_LINEAR, PNG_DEFAULT_sRGB);82        png_set_expand_16(png_ptr);83    } else {84        /*85         * 8-bit non-premultiplied sRGB pixels86         *87         * Setting alpha-mode == PREMULTIPLIED doesn't seem to work -88         * may be that libpng doesn't re-encode to sRGB. So, tell89         * libpng to generate non-premultiplied pixels, and then we90         * premultiply them ourselves later91         */92        png_set_alpha_mode(png_ptr, PNG_ALPHA_PNG, PNG_DEFAULT_sRGB);93        png_set_scale_16(png_ptr);94    }9596    /* Get meta data */97    png_read_info(png_ptr, info_ptr);98    int width = png_get_image_width(png_ptr, info_ptr);99    int height = png_get_image_height(png_ptr, info_ptr);100    png_byte color_type = png_get_color_type(png_ptr, info_ptr);101    png_byte bit_depth UNUSED = png_get_bit_depth(png_ptr, info_ptr);102    int channels UNUSED = png_get_channels(png_ptr, info_ptr);103104    LOG_DBG("%s: %dx%d@%hhubpp, %d channels", path, width, height, bit_depth, channels);105106    png_set_packing(png_ptr);107    png_set_interlace_handling(png_ptr);108109    /* pixman expects pre-multiplied alpha */110111    /* Tell libpng to expand to RGB(A) when necessary, and tell pixman112     * whether we have alpha or not */113    pixman_format_code_t format;114    switch (color_type) {115    case PNG_COLOR_TYPE_GRAY:116    case PNG_COLOR_TYPE_GRAY_ALPHA:117        LOG_DBG("%d-bit gray%s", bit_depth,118                color_type == PNG_COLOR_TYPE_GRAY_ALPHA ? "+alpha" : "");119120        if (bit_depth < 8)121            png_set_expand_gray_1_2_4_to_8(png_ptr);122123        png_set_gray_to_rgb(png_ptr);124125        if (color_type == PNG_COLOR_TYPE_GRAY)126            png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);127128        format = color_type == PNG_COLOR_TYPE_GRAY ? fmt_without_alpha : fmt_with_alpha;129        break;130131    case PNG_COLOR_TYPE_PALETTE:132        LOG_DBG("%d-bit colormap%s", bit_depth,133                png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) ? "+tRNS" : "");134135        png_set_palette_to_rgb(png_ptr);136137        if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {138            png_set_tRNS_to_alpha(png_ptr);139            format = fmt_with_alpha;140        } else {141            png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);142            format = fmt_without_alpha;143        }144        break;145146    case PNG_COLOR_TYPE_RGB:147        LOG_DBG("RGB");148        png_set_add_alpha(png_ptr, 0xff, PNG_FILLER_AFTER);149        format = fmt_without_alpha;150        break;151152    case PNG_COLOR_TYPE_RGBA:153        LOG_DBG("RGBA");154        format = fmt_with_alpha;155        break;156157    default:158        LOG_ERR("unhandled PNG color type: %d", color_type);159        goto err;160    }161162    png_read_update_info(png_ptr, info_ptr);163164    size_t row_bytes UNUSED = png_get_rowbytes(png_ptr, info_ptr);165    int stride = stride_for_format_and_width(format, width);166    image_data = xmalloc(height * stride);167168    LOG_DBG("stride=%d, row-bytes=%zu", stride, row_bytes);169    assert(stride >= row_bytes);170171    row_pointers = xmalloc(height * sizeof(png_bytep));172    for (int i = 0; i < height; i++)173        row_pointers[i] = &image_data[i * stride];174175    png_read_image(png_ptr, row_pointers);176177    if (!gamma_correct) {178        /* TODO: find a way to make libpng generate premultiplied,179           sRGB encoded pixels */180181        if (format == PIXMAN_a8b8g8r8) {182            for (int i = 0; i < height; i++) {183                uint32_t *p = (uint32_t *)row_pointers[i];184                for (int j = 0; j < width; j++, p++) {185                    uint8_t a = (*p >> 24) & 0xff;186                    uint8_t r = (*p >> 16) & 0xff;187                    uint8_t g = (*p >> 8) & 0xff;188                    uint8_t b = (*p >> 0) & 0xff;189190                    if (a == 0xff)191                        continue;192193                    if (a == 0) {194                        r = g = b = 0;195                    } else {196                        r = r * a / 0xff;197                        g = g * a / 0xff;198                        b = b * a / 0xff;199                    }200201                    *p = (uint32_t)a << 24 | r << 16 | g << 8 | b;202                }203            }204        }205    }206207    pix = pixman_image_create_bits_no_clear(208        format, width, height, (uint32_t *)image_data, stride);209210err:211    if (pix == NULL)212        free(image_data);213    free(row_pointers);214    if (png_ptr != NULL)215        png_destroy_read_struct(&png_ptr, &info_ptr, NULL);216    if (fp != NULL)217        fclose(fp);218219    return pix;220}221222#endif /* FUZZEL_ENABLE_PNG_LIBPNG */