Eos*_*ern 5 c c++ tiff libtiff
我\xe2\x80\x99m对libtiff相当陌生,但我\xe2\x80\x99已经设法获得tiff文件\n保存和打开没有太多麻烦。
\n\n现在,我\xe2\x80\x99m雄心勃勃,并尝试将自定义标签添加到我的文件中。我\xe2\x80\x99ve\n阅读了文档(请参阅此处)并编写了一些测试代码,这些代码编译时没有\n错误,但在运行时失败,并在第一次调用\n具有自定义字段的 TIFFSetField 时发生访问冲突(调用 TIFFSetField带有\n标准字段就可以了)。
\n\n我的测试代码如下:不到 100 行,唯一的外部依赖项(除了 libtiff)是我从 .pgm 文件打开测试图像的代码。谁能指出我\xe2\x80\x99m 做错了什么?顺便说一句,我正在使用 libtiff 4.0.3。
\n\n#include "stdafx.h"\n#include "PGM.h" // Just for reading in the test image\n#include "tiffio.h"\n\n// There are a number of TIFF-related definitions we need to make in order to support the custom tags\n// that we want to include in our files. The form of these definitions and subroutines comes straight\n// out of the libtiff documentation, and the values of the custom tags themselves come from the \n// range (65000-75535) defined in the TIFF specification as "reusable" and suitable for private use\n// within organisations. See http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf\n#define N(a) (sizeof(a) / sizeof (a[0]))\n#define TIFFTAG_INCIDENTPOWER 65000\n#define TIFFTAG_REFLECTEDPOWER 65001\n#define TIFFTAG_T1 65002\n#define TIFFTAG_T2 65003\n#define TIFFTAG_HUMIDITY 65004\n#define TIFFTAG_EXPOSURE 65005\n\nstatic const TIFFFieldInfo xtiffFieldInfo[] = {\n { TIFFTAG_INCIDENTPOWER, -1, -1, TIFF_LONG, FIELD_CUSTOM, 0, 1, const_cast<char*>("LaserIncidentPower") },\n { TIFFTAG_REFLECTEDPOWER, -1, -1, TIFF_LONG, FIELD_CUSTOM, 0, 1, const_cast<char*>("LaserReflectedPower") },\n { TIFFTAG_T1, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1, const_cast<char*>("Temperature_1") },\n { TIFFTAG_T2, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1, const_cast<char*>("Temperature_2") },\n { TIFFTAG_HUMIDITY, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1, const_cast<char*>("Humidity") },\n { TIFFTAG_EXPOSURE, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1, const_cast<char*>("ExposureTime(ms)") }\n}; // The casts are necessary because the\n // string literals are inherently const,\n // but the definition of TIFFFieldInfo\n // requires a non-const string pointer.\n // The Intel and Microsoft compilers\n // tolerate this, but gcc doesn\'t.\n\nstatic void registerCustomTIFFTags(TIFF *tif)\n{\n /* Install the extended Tag field info */\n TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));\n}\n\nvoid saveAsTiff(int nx, int ny, unsigned short *image, const char* filename,\n int Power1, int Power2, float T1, float T2, float Humidity, float Exposure)\n{\n // Create the TIFF directory object:\n TIFF* tif = TIFFOpen(filename, "w");\n\n // Set the tags: first the standard ones...\n TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, nx);\n TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ny);\n TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);\n TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);\n TIFFSetField(tif, TIFFTAG_ZIPQUALITY, 6); // Takes maximum advantage of Intel ZLIB enhancements\n TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);\n TIFFSetField(tif, TIFFTAG_ORIENTATION, static_cast<int>(ORIENTATION_TOPLEFT));\n TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);\n TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, ny);\n // Saving the whole image in a single block makes heavy\n // demands on memory but should enable optimum compression\n TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n\n // ... and now our own custom ones; if we comment out this next block then everything\n // works as it should and we successfully save the image:\n registerCustomTIFFTags(tif);\n TIFFSetField(tif, TIFFTAG_INCIDENTPOWER, Power1); // Access violation here\n TIFFSetField(tif, TIFFTAG_REFLECTEDPOWER, Power2);\n TIFFSetField(tif, TIFFTAG_T1, T1);\n TIFFSetField(tif, TIFFTAG_T2, T2);\n TIFFSetField(tif, TIFFTAG_HUMIDITY, Humidity);\n TIFFSetField(tif, TIFFTAG_EXPOSURE, Exposure);\n\n TIFFWriteEncodedStrip(tif, 0, static_cast<void *>(image), nx * ny * 2); \n // The \'* 2\' is present because each pixel\n // has 16 bits, hence two bytes\n // Write the directory to a file, and close it:\n TIFFWriteDirectory(tif);\n TIFFClose(tif);\n}\n\nint _tmain(int argc, _TCHAR* argv[])\n{\n // Variables to hold the image\n int nx, ny;\n unsigned short *image = nullptr;\n\n // Information we\'d like to put into our custom tags:\n int Power1 = 1000000;\n int Power2 = 8;\n float T1 = 23.5f;\n float T2 = -18.7f;\n float Humidity = 0.98f;\n float Exposure = 0.001f;\n\n // Read the test image in from a PGM file:\n readFromPGM(&nx, &ny, &image, "TestImage.pgm");\n\n // Save the image as a TIF file:\n saveAsTiff(nx, ny, image, "TestImage.tif", Power1, Power2, T1, T2, Humidity, Exposure);\n return 0;\n}\nRun Code Online (Sandbox Code Playgroud)\n
根据 TIFF 用户邮件列表上一位名叫 Paul Heckbert 的好心绅士的指导,以下是最终对我有用的代码。它有两个关键:
(1) 正确定义 TIFFFieldInfo 字段,其方式与文档中给出的示例不同。(2) 使用“extender”模式封装对 TIFFMergeFieldInfo 的调用,但在打开文件之前执行此操作。如果我在打开文件后才离开通话,那么当读取带有自定义标记的文件时,打开文件时将立即发出许多“警告,未知字段”消息。
这是代码:
#include "stdafx.h"
#include "PGM.h"
#include "tiffio.h"
// There are a number of TIFF-related definitions we need to make in order to support the custom tags
// that we want to include in our files. The form of these definitions and subroutines comes straight
// out of the libtiff documentation, and the values of the custom tags themselves come from the
// range (65000-75535) defined in the TIFF specification as "reusable" and suitable for private use
// within organisations. See http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf
#define N(a) (sizeof(a) / sizeof (a[0]))
#define TIFFTAG_INCIDENTPOWER 65000
#define TIFFTAG_REFLECTEDPOWER 65001
#define TIFFTAG_T1 65002
#define TIFFTAG_T2 65003
#define TIFFTAG_HUMIDITY 65004
#define TIFFTAG_EXPOSURE 65005
static const TIFFFieldInfo xtiffFieldInfo[] = {
{ TIFFTAG_INCIDENTPOWER, 1, 1, TIFF_LONG, FIELD_CUSTOM, 0, 0, const_cast<char*>("LaserIncidentPower") },
{ TIFFTAG_REFLECTEDPOWER, 1, 1, TIFF_LONG, FIELD_CUSTOM, 0, 0, const_cast<char*>("LaserReflectedPower") },
{ TIFFTAG_T1, 1, 1, TIFF_FLOAT, FIELD_CUSTOM, 0, 0, const_cast<char*>("Temperature_1") },
{ TIFFTAG_T2, 1, 1, TIFF_FLOAT, FIELD_CUSTOM, 0, 0, const_cast<char*>("Temperature_2") },
{ TIFFTAG_HUMIDITY, 1, 1, TIFF_FLOAT, FIELD_CUSTOM, 0, 0, const_cast<char*>("Humidity") },
{ TIFFTAG_EXPOSURE, 1, 1, TIFF_FLOAT, FIELD_CUSTOM, 0, 0, const_cast<char*>("ExposureTime(ms)") }
}; // The casts are necessary because the
// string literals are inherently const,
// but the definition of TIFFFieldInfo
// requires a non-const string pointer.
// The Intel and Microsoft compilers
// tolerate this, but gcc doesn't.
static TIFFExtendProc parent_extender = NULL; // In case we want a chain of extensions
static void registerCustomTIFFTags(TIFF *tif)
{
/* Install the extended Tag field info */
int error = TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
if (parent_extender)
(*parent_extender)(tif);
}
static void augment_libtiff_with_custom_tags() {
static bool first_time = true;
if (!first_time) return;
first_time = false;
parent_extender = TIFFSetTagExtender(registerCustomTIFFTags);
}
void saveAsTiff(int nx, int ny, unsigned short *image, const char* filename,
int Power1, int Power2, float T1, float T2, float Humidity, float Exposure)
{
// Create the TIFF directory object:
augment_libtiff_with_custom_tags();
TIFF* tif = TIFFOpen(filename, "w");
// Set the tags: first the standard ones...
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, nx);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, ny);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 16);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
TIFFSetField(tif, TIFFTAG_ZIPQUALITY, 6); // Takes maximum advantage of Intel ZLIB enhancements
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
TIFFSetField(tif, TIFFTAG_ORIENTATION, static_cast<int>(ORIENTATION_TOPLEFT));
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, ny);
// Saving the whole image in a single block makes heavy
// demands on memory but should enable optimum compression
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
// ... and now our own custom ones:
TIFFSetField(tif, TIFFTAG_INCIDENTPOWER, Power1);
TIFFSetField(tif, TIFFTAG_REFLECTEDPOWER, Power2);
TIFFSetField(tif, TIFFTAG_T1, T1);
TIFFSetField(tif, TIFFTAG_T2, T2);
TIFFSetField(tif, TIFFTAG_HUMIDITY, Humidity);
TIFFSetField(tif, TIFFTAG_EXPOSURE, Exposure);
TIFFWriteEncodedStrip(tif, 0, static_cast<void *>(image), nx * ny * 2);
// The '* 2' is present because each pixel
// has 16 bits, hence two bytes
// Write the directory to a file, and close it:
TIFFWriteDirectory(tif);
TIFFClose(tif);
}
void readFromTiff(int *nx, int *ny, unsigned short **image, const char* filename,
int *Power1, int *Power2, float *T1, float *T2, float *Humidity, float *Exposure)
{
// Create the TIFF directory object:
augment_libtiff_with_custom_tags();
TIFF* tif = TIFFOpen(filename, "r");
if (NULL == tif)
{
*nx = -1;
*ny = -1;
return;
}
// Read in the image size and metadata:
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, nx);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, ny);
TIFFGetField(tif, TIFFTAG_INCIDENTPOWER, Power1);
TIFFGetField(tif, TIFFTAG_REFLECTEDPOWER, Power2);
TIFFGetField(tif, TIFFTAG_T1, T1);
TIFFGetField(tif, TIFFTAG_T2, T2);
TIFFGetField(tif, TIFFTAG_HUMIDITY, Humidity);
TIFFGetField(tif, TIFFTAG_EXPOSURE, Exposure);
// Dimension the buffer, and read in the image data itself:
*image = new unsigned short[(*nx) * (*ny)];
TIFFReadEncodedStrip(tif, static_cast<tstrip_t>(0), static_cast<void *>(*image), static_cast<tsize_t>(-1));
TIFFClose(tif);
}
int _tmain(int argc, _TCHAR* argv[])
{
// Variables to hold the image
int nx, ny;
unsigned short *image = nullptr;
// Information we'd like to put into our custom tags:
int Power1 = 1000000;
int Power2 = 8;
float T1 = 23.5f;
float T2 = -18.7f;
float Humidity = 0.98f;
float Exposure = 0.001f;
// Read the test image in from a PGM file:
readFromPGM(&nx, &ny, &image, "TestImage.pgm");
// Save the image as a TIF file:
saveAsTiff(nx, ny, image, "TestImage.tif", Power1, Power2, T1, T2, Humidity, Exposure);
// Prepare to read back in: first clear the various fields
Power1 = 0;
Power2 = 0;
T1 = 0.0f;
T2 = 0.0f;
Humidity = 0.0f;
Exposure = 0.0f;
nx = 0;
ny = 0;
delete [] image;
readFromTiff(&nx, &ny, &image, "TestImage.tif", &Power1, &Power2, &T1, &T2, &Humidity, &Exposure);
printf("Image size = %d, %d\r\n", ny, ny);
printf("Power1 = %d\r\n", Power1);
printf("Power2 = %d\r\n", Power2);
printf("T1 = %6.3f\r\n", T1);
printf("T2 = %6.3f\r\n", T2);
printf("Humidity = %6.3f\r\n", Humidity);
printf("Exposure = %6.3f\r\n", Exposure);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6575 次 |
| 最近记录: |