#include <iostream> #ifdef _DEBUG #pragma comment(lib, "tiffd.lib") #else #pragma comment(lib, "tiff.lib") #endif #include <tiffio.h> #include <vector> #include <cassert> #pragma warning(disable:4996) int main() { // 保存する画像のサイズ std::uint32_t width = 2; std::uint32_t height = 4; // 画像を作成 std::vector<std::uint8_t> mydata(width * height * 3); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { int pos = y * width + x; mydata[pos * 3 + 0] = float(pos) / (width*height) * 255; mydata[pos * 3 + 1] = 0; mydata[pos * 3 + 2] = 0; } } // tiffファイルの書き込み開始 TIFF* out = TIFFOpen("D:\\Data\\test.tif", "w"); TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width); TIFFSetField(out, TIFFTAG_IMAGELENGTH, height); TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, 3); // RGB color TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8); // Per channel TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT /*ORIENTATION_BOTRIGHT*/); TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);//色の格納方法 RGBRGBRGBかRRRGGGBBBか等 TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);//ピクセルデータの解釈方法 tsize_t scanlinebytes = 3 * width;// mydataのスキャンライン計算 TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(out, scanlinebytes )); for (std::uint32_t row = 0; row < height; row++) { // row 行目の先頭アドレスを取得 uint8_t* ptr = &mydata[0] + row * scanlinebytes; if (TIFFWriteScanline(out, ptr, row, 0) < 0) // 書き込み break; } TIFFClose(out); return 0; }