[英]How to write a 16 bit PNG_COLOR_TYPE_GRAY with libpng?
我编写了以下代码来生成一个黑白 PNG 图像,其中每个像素的值为 16 位(0 = 黑色,0xFFFF = 白色)。 这是一个简单的 640x480 测试图像,其中所有线条都相同,左侧的每条线条都有最大的黑色,向右渐变为白色。 由于每条线的宽度为 640 像素,因此我希望在右侧看到一个几乎全黑的图像,其最大为 640/65535 的白色。 相反,我得到的图像达到纯白色 2 倍,对应于 0x00ff 和 0x01ff 值。 这表明 libpng 不使用每个像素的最高有效字节。 有人能告诉我我错在哪里吗? 总之谢谢大家。 再见
系统及编译详情:
系统配备 OS X 10.11.6 的 MacBook Pro(Retina,15 英寸,2013 年底)
PNG > gcc --version
配置为:--prefix = / Applications / Xcode.app / Contents / Developer / usr --with-gxx-include-dir = / Applications / Xcode.app / Contents / Developer / Platforms / MacOSX.platform / Developer / SDKs / MacOSX10.12.sdk/usr/include/c++/4.2.1 Apple LLVM version 8.0.0 (clang-800.0.42.1) 目标:x86_64-apple-darwin15.6.0 线程模型:posix InstalledDir:/Applications/Xcode。 app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
PNG> gcc -I /opt/X11/include/ -L /opt/X11/lib/ -l png -lz writeTest16bit.c
/* This is the test code upper described */
#include <png.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define ERROR -1
#define GOTO_ERROR {line = __LINE__; goto error;}
#define width 640
#define height 460
#define bit_depth 16
unsigned short int image[height][width];
void setBitMapImageGray (void);
int main (int argc, char **argv)
{
char *pngFileName = "file.png";
FILE *pngFile = NULL;
png_structp pngStruct = NULL;
png_infop pngInfo = NULL;
int line = __LINE__;
int i;
setBitMapImageGray ();
if (NULL == (pngFile = fopen (pngFileName, "wb"))) GOTO_ERROR;
if (NULL == (pngStruct = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))) GOTO_ERROR;
if (NULL == (pngInfo = png_create_info_struct (pngStruct))) GOTO_ERROR;
// setting long jump: posponed
png_init_io(pngStruct, pngFile);
png_set_IHDR (pngStruct, pngInfo, width, height, bit_depth,
PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info (pngStruct, pngInfo);
for (i=0; i<height; i++)
png_write_row (pngStruct, (png_const_bytep)&image[i][0]);
png_write_end (pngStruct, NULL);
png_destroy_write_struct (&pngStruct, (png_infopp)NULL);
fclose (pngFile);
return 0;
error:
printf ("Error in line %d\n", line);
if (pngStruct) png_destroy_write_struct (&pngStruct, (png_infopp)NULL);
if (pngFile) fclose (pngFile);
return (ERROR);
}
void setBitMapImageGray (void)
{
int x,y;
unsigned short int const black=0, step=0x10000/width;
for (y=0; y<height; y++)
for (x=0; x<width; x++)
// image[y][x] = 0xFF00;
image[y][x] = x;
}
你几乎是对的 - 它没有忽略高字节,但你有一个字节序问题。
您的代码工作正常,如果你添加png_set_swap()
后png_write_info()
是这样的:
...
png_set_IHDR (pngStruct, pngInfo, width, height, bit_depth,
PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(pngStruct, pngInfo);
png_set_swap(pngStruct); // <--- THIS LINE ADDED
...
关键词: PNG, libpng, 16 位, 16 位, 灰度, endian, endianness, 字节顺序, little endian, big-endian, write, image, image processing, C.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.