用libpng创建16位图像

时间:2014-10-01 22:23:13

标签: c++ libpng 16-bit

我尝试使用libpng编写16位RGB图像,其中每个点颜色来自输入"数组"实例。以下代码确实有效,但产生8位。

template<typename T> void savePNG(Array2<Vec3<T> > *arrayImg, const std::string filename){

    /* create file */
    FILE *fp = fopen(filename.c_str(), "wb");
    if (!fp){
        std::runtime_error("[write_png_file] File could not be opened for writing");
        return;
    }


    /* initialize stuff */
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

    if (!png_ptr){
        std::runtime_error("[write_png_file] png_create_write_struct failed");
        return;
    }

    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr){
        std::runtime_error("[write_png_file] png_create_info_struct failed");
        return;
    }

    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during init_io");
        return;
    }

    png_init_io(png_ptr, fp);


    /* write header */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during writing header");
        return;
    }

    png_set_IHDR(png_ptr, info_ptr,
            (png_uint_32) arrayImg->dimension[0], (png_uint_32) arrayImg->dimension[1],
            (png_byte) 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

    png_write_info(png_ptr, info_ptr);


    /* write bytes */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during writing bytes");
        return;
    }

    /* Initialize rows of PNG. */
    png_bytepp row_pointers = (png_bytepp) malloc (sizeof(png_bytep)*arrayImg->dimension[1]);
    for (size_t y = 0; y < arrayImg->dimension[1]; ++y) {
        row_pointers[y] = (png_bytep) malloc (png_get_rowbytes(png_ptr,info_ptr));
        for (size_t x = 0; x < arrayImg->dimension[0]; ++x) {
            for (size_t k = 0; k < 3; k++) {
                row_pointers[y][x*3+k] = (uint8_t) (clamp((*arrayImg)[y][x][k],T(0.0),T(1.0))*T(255));
            }
        }
    }

    /* Writes PNG. */
    png_write_image(png_ptr, (png_bytepp) row_pointers);


    /* end write */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during end of write");
        return;
    }

    png_write_end(png_ptr, NULL);

    /* cleanup heap allocation */
    for (size_t y=0; y<arrayImg->dimension[1]; y++){
        free(row_pointers[y]);
    }
    free(row_pointers);

    fclose(fp);
}

以下代码应生成16位版本。它生成的图片具有正确的尺寸,但数据都向左移动:

template<typename T> void savePNG(Array2<Vec3<T> > *arrayImg, const std::string filename){

    /* create file */
    FILE *fp = fopen(filename.c_str(), "wb");
    if (!fp){
        std::runtime_error("[write_png_file] File could not be opened for writing");
        return;
    }


    /* initialize stuff */
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

    if (!png_ptr){
        std::runtime_error("[write_png_file] png_create_write_struct failed");
        return;
    }

    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr){
        std::runtime_error("[write_png_file] png_create_info_struct failed");
        return;
    }

    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during init_io");
        return;
    }

    png_init_io(png_ptr, fp);


    /* write header */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during writing header");
        return;
    }

    png_set_IHDR(png_ptr, info_ptr,
            (png_uint_32) arrayImg->dimension[0], (png_uint_32) arrayImg->dimension[1],
            (png_byte) 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
            PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

    png_write_info(png_ptr, info_ptr);


    /* write bytes */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during writing bytes");
        return;
    }

    /* Initialize rows of PNG. */
    png_bytepp row_pointers = (png_bytepp) malloc (sizeof(png_bytep)*arrayImg->dimension[1]);
    for (size_t y = 0; y < arrayImg->dimension[1]; ++y) {
        row_pointers[y] = (png_bytep) malloc (png_get_rowbytes(png_ptr,info_ptr));
        for (size_t x = 0; x < arrayImg->dimension[0]; ++x) {
            for (size_t k = 0; k < 3; k++) {
                row_pointers[y][x*3+k] = (uint16_t) (clamp((*arrayImg)[y][x][k],T(0.0),T(1.0))*T(65535));
            }
        }
    }

    /* Writes PNG. */
    png_write_image(png_ptr, (png_bytepp) row_pointers);


    /* end write */
    if (setjmp(png_jmpbuf(png_ptr))){
        std::runtime_error("[write_png_file] Error during end of write");
        return;
    }

    png_write_end(png_ptr, NULL);

    /* cleanup heap allocation */
    for (size_t y=0; y<arrayImg->dimension[1]; y++){
        free(row_pointers[y]);
    }
    free(row_pointers);

    fclose(fp);
}

我对该问题的理解是,由于png_byte,png_bytep和/或png_bytepp类型,数据当前被截断。如何将16位数据写入两个8位?

2 个答案:

答案 0 :(得分:0)

如果有人遇到过这样的问题。以下是几个选项:

  • 尝试阅读API ...
  • 使用函数void png_save_uint_16 (png_bytep buf, unsigned int i);

答案 1 :(得分:0)

你的“k”计算字节数,每个像素有6个字节,而不是3个。

循环

for (size_t k = 0; k < 3; k++) {
    row_pointers[y][x*3+k] = (uint16_t)clamp(...);
}

应该是

for (size_t k = 0; k < 6; k+=2) {
    png_save_uint_16(row_pointers[y][x*6+k],(unsigned int)clamp(...);
}

这将一次推送两个字节(“clamp()”操作的结果) 在row_pointers [y]的缓冲区中。