使用 libpng 写入 1 位深度灰度 PNG 会导致图像不正确

Writing a 1 bit depth grayscale PNG with libpng results in incorrect image

所以我正在尝试使用 libpng 从位深度为 1 的 unsigned char 数组中写入 png。对于所有位来说,只有黑色和白色,作为灰度图像。我已经成功地为 8 位深度灰度 png 做到了这一点,但不是 1 位深度。这是我的代码:

extern int write_png_bwsq(const char* filename,
                    int dimen,
                    const unsigned char *buffer,
                    char* title)
{
        
    int yrow;
        int dim_bytes;
    int code = 1;
    FILE *fp = NULL;
    png_structp png_ptr = NULL;
    png_infop info_ptr = NULL;
    png_bytep row = NULL;
        dim_bytes = (dimen * dimen) / 8;

    
    // Open file for writing (binary mode)
    fp = fopen(filename, "wb");
    if (fp == NULL) {
        fprintf(stderr, "PNG ERROR: Could not open file %s for writing\n", filename);
        code = 0;
        goto finalise;
    }

    // Initialize write structure
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (png_ptr == NULL) {
        fprintf(stderr, "PNG ERROR: Could not allocate PNG write struct\n");
        code = 0;
        goto finalise;
    }

    // Initialize info structure
    info_ptr = png_create_info_struct(png_ptr);
    if (info_ptr == NULL) {
        fprintf(stderr, "PNG ERROR: Could not allocate PNG info struct\n");
        code = 0;
        goto finalise;
    }

    // Setup Exception handling
    if (setjmp(png_jmpbuf(png_ptr))) {
        fprintf(stderr, "PNG Error: Creating the PNG output failed.\n");
        code = 0;
        goto finalise;
    }

    png_init_io(png_ptr, fp);

    png_set_IHDR(png_ptr, info_ptr, dimen, dimen,
            1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);

    // Sets the title
    if (title != NULL) {
        png_text title_text;
        title_text.compression = PNG_TEXT_COMPRESSION_NONE;
        title_text.key = "Title";
        title_text.text = title;
        png_set_text(png_ptr, info_ptr, &title_text, 1);
    }

    png_write_info(png_ptr, info_ptr);

    row = (png_bytep) buffer;

    // Write image data
    for (yrow=0 ; yrow<dim_bytes ; yrow++) {
        png_write_row(png_ptr, row);
        ++row;
    }

    // End write operation
    png_write_end(png_ptr, NULL);

    finalise:
    if (fp != NULL) fclose(fp);
    if (info_ptr != NULL) png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
    if (png_ptr != NULL) png_destroy_write_struct(&png_ptr, (png_infopp)NULL);

    return code;
}

然后,我有一个单独的文件来准备图像

#include "write_pngs.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

#if CHAR_BIT != 8
#  error "CHAR_BIT is not usable on this platform"
#endif

#define PIC_SIZE_DIM 1080
#define PIC_SIZE_BW ((PIC_SIZE_DIM * PIC_SIZE_DIM) / CHAR_BIT) 
static unsigned char PIC_BITS[PIC_SIZE_BW] = {0};

static void __write_bits_pic(void)
{
     size_t i = 1000;
     for(;i < 140000;++i) {
        PIC_BITS[i] = ((i + 76) >> 5) & 0xff;
     }
}


int main(void) {
   __write_bits_pic();
   printf("Writing picture of %d bytes and %d bits\n", PIC_SIZE_BW, PIC_SIZE_BW * CHAR_BIT);
   return !write_png_bwsq("bwpixs.png",
                    PIC_SIZE_DIM,
                    PIC_BITS,
                    "bwpixs");
}

这会导致图像不正确,它不仅对于 png 来说非常大(仅 1080 x 1080 大约 5mb),而且只有图像的右下角从黑色变为黑色。

我在这里做错了什么? libpng 是否需要任何特殊步骤来编写我没有做的灰度位深度仅为 1 的 png?

您调用 png_write_row() 的次数太多了。你的

for (yrow=0 ; yrow<dim_bytes ; yrow++) {
    png_write_row(png_ptr, row);
    ++row;
}

应该是这样的:

for (yrow=0 ; yrow<dim_bytes ; yrow += dimen / 8) {
    png_write_row(png_ptr, row);
    row += dimen / 8;
}

为了写入 1080 像素(135 字节)的。您正在为每个字节调用 png_write_row(),就好像图像是八个像素宽一样。事实并非如此。