次のコードを書いて、各ピクセルの値が 16 ビット (0 = 黒、0xFFFF = 白) の白黒 PNG 画像を生成しました。これは単純な 640x480 のテスト イメージで、すべての線が同じで、左側の各線が最大の黒を持ち、右に行くほど白にフェードします。すべての線は 640 ピクセル幅なので、右側の白の最大値が 640/65535 に達する、ほぼ完全に黒の画像が表示されると予想していました。代わりに、値 0x00ff と 0x01ff に対応して 2 回純粋な白に達する画像を取得します。これは、各ピクセルの最上位バイトが libpng によって使用されていないことを示しています。誰かが私が間違っている場所を教えてもらえますか? とにかくみんなに感謝します。さよなら
システムとコンパイルの詳細:
システム MacBook Pro (Retina、15 インチ、2013 年終了)、OS X 10.11.6
PNG > gcc --バージョン
構成: --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 インストール ディレクトリ: /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;
}