我编写了以下代码来生成黑白PNG图像,其中每个像素的值为16位(0=黑色,0xFFFF=白色)。这是一个简单的640x480测试图像,其中所有的行都是相同的,左边的每一行都有最大的黑色,从右边变为白色。因为每一行640像素宽,所以我希望看到一个几乎完全黑色的图像,右边的最大值是640/65535白色。取而代之的是,我得到了一个与0x00ff和0x01ff值对应的图像,它达到纯白色2次。这表明libpng不使用每个像素的最高有效字节。
有人能告诉我哪里错了吗?
拜伊
系统和编译详细信息:
PNG>gcc—版本
苹果LLVM 8.0.0版(clang-800.0.42.1)
目标:x86_64-apple-darwin15.6.0
InstalledDir:/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
PNG>gcc-I/opt/X11/include/-L/opt/X11/lib/-L PNG-L z writeTest16bit.c
生成的图像(file.png)是:
/* This is the test code upper described */
#include
#include
#include
#include
#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
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
for (x=0; x
// image[y][x] = 0xFF00;
image[y][x] = x;
}