我编写了以下代码来生成黑白 PNG 图像,其中每个像素的值为 16 位(0 = 黑色,0xFFFF = 白色)。这是一个简单的 640x480 测试图像,其中所有线条都相同,并且左侧的每条线条都有最大的黑色,然后逐渐变为右侧的白色。由于每条线都是 640 像素宽,我本来希望看到一个几乎完全黑色的图像,右侧的白色最大达到 640/65535。相反,我得到的图像与值 0x00ff 和 0x01ff 相对应,达到纯白色 2 次。这表明 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 版本 8.0.0 (clang-800.0.42.1) 目标:x86_64-apple-darwin15.6.0 线程模型:posix InstalledDir: /Applications/Xcode。应用程序/内容/开发人员/工具链/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;
}