- 2014.06.06 11:11 "Re: [Tiff] What's wrong with my code for adding custom tags to a file", by Stephen Morris
2014.06.07 06:15 "Re: [Tiff] What's wrong with my code for adding custom tags to a file", by Paul Heckbert
You might want to put asserts (or tests of some kind) on the return value of all your libtiff calls, to help you detect where things are going wrong.
For examining TIFF and EXIF tags in files, I recommend exiftool, in particular “exiftool -htmldump”. Maybe you already know about it. See http://www.sno.phy.queensu.ca/~phil/exiftool
On 2014/6/6, at 7:11 AM, Stephen Morris <s-morris@n-eos.com> wrote:
Thank you Paul. I incorporated the TiffSetTagExtender call in the way that you showed (which helped me a lot to make sense of the documentation) to produce the code shown below. Sure enough, the access violation has gone away but I still don¹t get the custom tags in the file. As the routine runs, I get error messages in my console to the effect of:
TIFFSetField: TestImage.tif: Unknown tag 65000.
TIFFSetField: TestImage.tif: Unknown tag 65001.
TIFFSetField: TestImage.tif: Unknown tag 65002.
TIFFSetField: TestImage.tif: Unknown tag 65003.
TIFFSetField: TestImage.tif: Unknown tag 65004.
TIFFSetField: TestImage.tif: Unknown tag 65005.
Šso there¹s obviously something that I¹m still missing. Here¹s my new code:
#include "stdafx.h"
#include "PGM.h"#define TIFFTAG_INCIDENTPOWER 65000
#define TIFFTAG_REFLECTEDPOWER 65001
#define TIFFTAG_T1 65002
#define TIFFTAG_T2 65003
#define TIFFTAG_HUMIDITY 65004
#define TIFFTAG_EXPOSURE 65005static const TIFFFieldInfo xtiffFieldInfo[] = {
{ TIFFTAG_INCIDENTPOWER, -1, -1, TIFF_LONG, FIELD_CUSTOM, 0, 1,
const_cast<char*>("LaserIncidentPower") },
{ TIFFTAG_REFLECTEDPOWER, -1, -1, TIFF_LONG, FIELD_CUSTOM, 0, 1,
const_cast<char*>("LaserReflectedPower") },
{ TIFFTAG_T1, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1,
const_cast<char*>("Temperature_1") },
{ TIFFTAG_T2, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1,
const_cast<char*>("Temperature_2") },
{ TIFFTAG_HUMIDITY, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1,
const_cast<char*>("Humidity") },
{ TIFFTAG_EXPOSURE, -1, -1, TIFF_FLOAT, FIELD_CUSTOM, 0, 1,
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 */
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:
TIFF* tif = TIFFOpen(filename, "w");
> // Prepare for writFC