INDEX
はじめに
今回は16bit(unsigned short int)グレースケールMulti TIFFの書き込みです。
Windows、Linuxで解析を行うので、両方でコンパイルできるようにしています。
環境・コンパイルなど
「OpenCVの環境構築」の記事を参照。
ソース
環境によっては「nodefaultlib」を付けなくてもコンパイルは通ります。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TEXT_MAX 128
//#define TIFF_WRITE_ALL // まとめて書き込む
#ifdef __GNUC__
#include <tiffio.h>
#endif
#ifdef _WIN32
#include "tiffio.h"
#ifdef _DEBUG
#pragma comment(linker, "/nodefaultlib:\"libcmtd.lib\"")
#else
#pragma comment(linker, "/nodefaultlib:\"libcmt.lib\"")
#endif
#pragma comment(lib, "libtiff_i.lib")
#endif
int main(int argc, char **argv)
{
int i, j;
char tiftitle[TEXT_MAX] = { '\0' };
float resolution;
uint16 samplesperpixel, bitspersample;
uint16 *data;
uint16 i_u16, j_u16;
uint16 page_max;
uint32 width, height;
uint32 rowsperstrip;
int32 tifsize; // tiffio.h
TIFF *tif;
// TIFFパラメータ
width = 512;
height = 256;
samplesperpixel = 1;
bitspersample = 16;
resolution = 72;
strcpy(tiftitle, "Test program");
// MultiTIFFのページ数
page_max = 5;
// 確認
printf("uint8: %d[bit] \n", 8 * sizeof( uint8 ));
printf("uint16: %d[bit] \n", 8 * sizeof( uint16 ));
printf("uint32: %d[bit] \n", 8 * sizeof( uint32 ));
// 画像の領域確保
tifsize = width * height * sizeof( uint16 );
data = ( uint16 * )malloc( tifsize );
if ( data == NULL )
{
printf("ERROR: Can not get memory [data] \n");
exit( EXIT_FAILURE );
}
// 画像データ
for (i = 0; i < height; i++)
{
for (j = 0; j < width; j++)
{
data[width * j + i] = ( uint16 )(i * j);
}
}
tif = TIFFOpen("out-uint16.tif", "w");
if ( tif == NULL )
{
printf("ERROR: Can not open tiff file \n");
return EXIT_FAILURE;
}
for (i_u16 = 0; i_u16 < page_max; i_u16++)
{
TIFFSetField(tif, TIFFTAG_SOFTWARE, tiftitle);
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width );
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
TIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_XRESOLUTION, resolution);
TIFFSetField(tif, TIFFTAG_YRESOLUTION, resolution);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
// rowsperstrip = TIFFDefaultStripSize(tif, height);
// TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, height);
// for multitiff
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
TIFFSetField(tif, TIFFTAG_PAGENUMBER, i_u16, page_max);
#ifdef TIFF_WRITE_ALL
// まとめて書き込み
TIFFWriteEncodedStrip(tif, 0, data, tifsize);
#else
// 1行ずつ書き込む
for (j_u16 = 0; j_u16 < height; j_u16++)
{
TIFFWriteScanline(tif, &data[j_u16 * width], j_u16, 0);
}
#endif
TIFFWriteDirectory( tif );
}
TIFFClose( tif );
free( data );
return EXIT_SUCCESS;
}
created by Rinker
¥295
(2026/01/09 18:34:18時点 楽天市場調べ-詳細)
created by Rinker
¥3,960
(2026/01/09 18:34:18時点 楽天市場調べ-詳細)
created by Rinker
¥3,630
(2026/01/08 21:34:22時点 楽天市場調べ-詳細)


Comments