Return-Path: <awaresystems.be+caf_=filtered=awaresystems.be@gmail.com>
Delivered-To: filtered@awaresystems.be
Received: from mx-m.one.com (mx-m.one.com [91.198.169.60])
	by mail6.local (Postfix) with ESMTP id 8991A3001DBB10
	for <filtered@awaresystems.be>; Thu,  5 Jun 2014 11:33:30 +0000 (UTC)
X-Virus-Scanned: by one.com
Received-SPF: pass (mx-m: domain of awaresystems.be+caf_=filtered=awaresystems.be@gmail.com designates 209.85.215.41 as permitted sender)
X-Greylist: whitelisted by SQLgrey-1.8.0-rc2
Received: from mail-la0-f41.google.com (mail-la0-f41.google.com [209.85.215.41])
	by mx-m.one.com (Postfix) with ESMTP id 5BB56209C39C9
	for <filtered@awaresystems.be>; Thu,  5 Jun 2014 11:33:30 +0000 (UTC)
Received: by mail-la0-f41.google.com with SMTP id e16so484047lan.14
        for <filtered@awaresystems.be>; Thu, 05 Jun 2014 04:33:30 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-original-authentication-results:delivered-to:delivered-to:from:to
         :thread-topic:thread-index:date:message-id:accept-language
         :content-language:content-id:mime-version:subject:precedence:list-id
         :list-unsubscribe:list-archive:list-post:list-help:list-subscribe
         :content-type:content-transfer-encoding:sender:errors-to;
        bh=5eGGYYMD63sku2pMalPKkA6CyUKeP352g/LUffhk/Mg=;
        b=ggLXBoqyJ87gJd28jEi297ciBcXBxwQWRKzPaCVjzpHiKS+acxGcK3HzUBqlafdH02
         cYPVUpejhXXmNQtOU1tx17wEgNN20YTTUXByQNTMjwFvre9kQDAl+dvL3d0+Lc7vn/yn
         qtX/pGInOMX2YnWEdaKmVvOZkbIYmKJLsu5ZSCkYscdXqLLtsu/9G9hVJbDxzwMcsGmN
         YLY+IIt+xxfgztBI927uSK1eIsen0a0zp0wpQxue2GFHMalUWSjFgmMwYdOMrtm5KKGG
         6uB02CXXkHMO8TKis9Up9DxbHc/iAwbEWNxNN8+j4YBwUnceK3m8MPX8ysSCzEniI6uu
         dhWQ==
X-Original-Authentication-Results: mx.google.com;       spf=neutral (google.com: tiff-bounces@lists.maptools.org does not designate permitted sender hosts) smtp.mail=tiff-bounces@lists.maptools.org
X-Received: by 10.112.168.227 with SMTP id zz3mr19577578lbb.0.1401968009948;
        Thu, 05 Jun 2014 04:33:29 -0700 (PDT)
X-Forwarded-To: filtered@awaresystems.be
X-Forwarded-For: awaresystems.be@gmail.com filtered@awaresystems.be
Delivered-To: awaresystems.be@gmail.com
Received: by 10.114.18.137 with SMTP id w9csp626ldd;
        Thu, 5 Jun 2014 04:33:29 -0700 (PDT)
X-Received: by 10.152.36.99 with SMTP id p3mr1806095laj.61.1401968008416;
        Thu, 05 Jun 2014 04:33:28 -0700 (PDT)
Authentication-Results: mx.google.com;
       spf=neutral (google.com: tiff-bounces@lists.maptools.org does not designate permitted sender hosts) smtp.mail=tiff-bounces@lists.maptools.org
Received-SPF: none (google.com: tiff-bounces@lists.maptools.org does not designate permitted sender hosts) client-ip=91.198.169.248;
Received: by 10.152.134.243 with POP3 id pn19mf777405lab.19;
        Thu, 05 Jun 2014 04:33:28 -0700 (PDT)
X-Gmail-Fetch-Info: info@awaresystems.be 1 pop.awaresystems.be 110 info@awaresystems.be
Delivered-To: info@awaresystems.be
Received: from mx-q.one.com (mx-q.one.com [91.198.169.248])
	by mail6.local (Postfix) with ESMTP id 1F9793001DBB07
	for <info@awaresystems.be>; Thu,  5 Jun 2014 11:32:49 +0000 (UTC)
X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2
Received: from lists.maptools.org (ks365428.kimsufi.com [94.23.4.142])
	by mx-q.one.com (Postfix) with ESMTP id D51B8218682
	for <info@awaresystems.be>; Thu,  5 Jun 2014 11:32:48 +0000 (UTC)
Received: from [10.1.0.241] (localhost.localdomain [127.0.0.1])
	by lists.maptools.org (Postfix) with ESMTP id E075E35C023;
	Thu,  5 Jun 2014 06:32:05 -0500 (EST)
Received: from emea01-db3-obe.outbound.protection.outlook.com
	(mail-db3lp0081.outbound.protection.outlook.com [213.199.154.81])
	by lists.maptools.org (Postfix) with ESMTPS id 32E7935C023
	for <tiff@lists.maptools.org>; Thu,  5 Jun 2014 06:31:41 -0500 (EST)
Received: from DB3PR06MB092.eurprd06.prod.outlook.com (10.141.1.27) by
	DB3PR06MB089.eurprd06.prod.outlook.com (10.141.1.16) with Microsoft
	SMTP Server (TLS) id 15.0.954.9; Thu, 5 Jun 2014 11:31:41 +0000
Received: from DB3PR06MB092.eurprd06.prod.outlook.com ([169.254.4.25]) by
	DB3PR06MB092.eurprd06.prod.outlook.com ([169.254.4.25]) with mapi id
	15.00.0954.000; Thu, 5 Jun 2014 11:31:41 +0000
From: Stephen Morris <s-morris@n-eos.com>
To: "tiff@lists.maptools.org" <tiff@lists.maptools.org>
Thread-Topic: What's wrong with my code for adding custom tags to a file
Thread-Index: AQHPgLG4n3B3tVLDTkqfNRJXO5POWg==
Date: Thu, 5 Jun 2014 11:31:40 +0000
Message-ID: <CFB6138E.1A4BC%stephen@morris.net>
Accept-Language: en-GB, en-US
Content-Language: en-US
X-MS-Has-Attach: 
X-MS-TNEF-Correlator: 
x-originating-ip: [81.139.73.159]
x-microsoft-antispam: BL:0; ACTION:Default; RISK:Low; SCL:0; SPMLVL:NotSpam;
	PCL:0; RULEID:
x-forefront-prvs: 0233768B38
x-forefront-antispam-report: SFV:NSPM;
	SFS:(6009001)(428001)(189002)(199002)(53754006)(66654002)(243025005)(86362001)(54356999)(4396001)(36756003)(85852003)(50986999)(92566001)(83072002)(92726001)(2656002)(15975445006)(87936001)(81542001)(74662001)(15202345003)(81342001)(64706001)(66066001)(19580395003)(77982001)(21056001)(83322001)(79102001)(99396002)(46102001)(31966008)(20776003)(101416001);
	DIR:OUT; SFP:; SCL:1; SRVR:DB3PR06MB089;
	H:DB3PR06MB092.eurprd06.prod.outlook.com; FPR:; MLV:sfv;
	PTR:InfoNoRecords; MX:1; A:1; LANG:en; 
received-spf: None (: n-eos.com does not designate permitted sender hosts)
authentication-results: spf=none (sender IP is )
	smtp.mailfrom=s-morris@n-eos.com; 
Content-ID: <517E7D72FBF5CC4F8B215D2CCF5356C7@eurprd06.prod.outlook.com>
MIME-Version: 1.0
X-OriginatorOrg: n-eos.com
Subject: [Tiff] What's wrong with my code for adding custom tags to a file
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.9
Precedence: list
List-Id: TIFF and libtiff discussion <tiff.lists.maptools.org>
List-Unsubscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=unsubscribe>
List-Archive: <http://lists.maptools.org/mailman/private/tiff>
List-Post: <mailto:tiff@lists.maptools.org>
List-Help: <mailto:tiff-request@lists.maptools.org?subject=help>
List-Subscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=subscribe>
Content-Type: text/plain; charset="windows-1252"
Content-Transfer-Encoding: quoted-printable
Sender: tiff-bounces@lists.maptools.org
Errors-To: tiff-bounces@lists.maptools.org

Hello all; I=B9m quite new to libtiff, but I=B9ve managed to get tiff files
saving and opening without much trouble.

Now, I=B9m being ambitious and trying to add custom tags into my files. I=
=B9ve
read the documentation and written some test code which compiles without
errors, but fails at runtime with an access violation at the first call to
TIFFSetField that features a custom field (calls to TIFFSetField with
standard fields are fine).

My test code follows: just under 100 lines, with the only external
dependency (besides libtiff) being my code to open a test image from a
.pgm file.  Can anyone point out what I=B9m doing wrong?

Many thanks.
Stephen.

=8B=8B=8B=8B=8B=8B=8B=8B=8B

#include =B3stdafx.h=B2   // Visual Studio default
#include =B3PGM.h=B2      // Just for opening a test image
#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[] =3D {
    { 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 void registerCustomTIFFTags(TIFF *tif) {
    /* Install the extended Tag field info */
    TIFFMergeFieldInfo(tif, xtiffFieldInfo, N(xtiffFieldInfo));
}

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 =3D 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; if we comment out this next block
then everything
    // works as it should and we successfully save the image:
    registerCustomTIFFTags(tif);
    TIFFSetField(tif, TIFFTAG_INCIDENTPOWER, Power1);   // Access
violation here
    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);
}

int _tmain(int argc, _TCHAR* argv[])
{
    // Variables to hold the image
    int nx, ny;
    unsigned short *image =3D nullptr;

    // Information we'd like to put into our custom tags:
    int Power1 =3D 1000000;
    int Power2 =3D 8;
    float T1 =3D 23.5f;
    float T2 =3D -18.7f;
    float Humidity =3D 0.98f;
    float Exposure =3D 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);

    return 0;
}


 =


_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
