title: How are TIFF string fields retrieved using LibTIFF's TIFFGetField?
body: <p>I've been trying to read string TIFF fields (such as TIFFTAG_MODEL) using LibTiff's TIFFGetField function, but all I'm getting back from it are pointers to zeroed memory. The <a href="http://www.libtiff.org/man/TIFFGetField.3t.html" rel="nofollow noreferrer">TIFFGetField man page</a> isn't much help, merely stating that it expects a <code>char**</code> argument for string fields, which I think implicitly suggests it will allocate memory, write the string to that memory, and return a pointer to it. But then it doesn't discuss responsibilities for freeing that memory.</p>
  
  <p>When I define a <code>char*</code> and initialise it to NULL, then pass the address of that variable to TIFFGetField, it does set it to a non-NULL address, but the memory pointed to by that address is all zeroes.</p>
  
  <p>In case it's relevant, I'm using LibTiff 4.0.2 on MacOS.</p>
  
  <p>Here's an MCVE with what I've tried so far. (The commented out code is where I was experimenting with passing in an already allocated buffer instead. That didn't work either.)</p>
  
  <pre><code>#include "tiffio.h"
  #include &lt;stdio.h&gt;
  
  const char* img_filename = "temp.tif";
  
  void WriteTestTIFF()
  {
      TIFF* tiff = TIFFOpen(img_filename, "w");
  
      const size_t width = 2;
      const size_t height = 2;
      unsigned char image[width * height] = { 0 };
  
      TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width);
      TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height);
      TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, height); // Single strip image
      TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
      TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8);
      TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
      TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
      TIFFSetField(tiff, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
      TIFFSetField(tiff, TIFFTAG_MINSAMPLEVALUE, 0);
      TIFFSetField(tiff, TIFFTAG_MAXSAMPLEVALUE, 255);
  
      TIFFSetField(tiff, TIFFTAG_SOFTWARE, "TestingTIFFStringFields");
      TIFFSetField(tiff, TIFFTAG_MODEL, "FakeCamera");
  
      TIFFWriteEncodedStrip(tiff, 0, image, height * width);
      TIFFClose(tiff);
  }
  
  void ReadTestTIFF()
  {
      TIFF* tiff = TIFFOpen(img_filename, "r");
      //char buffer[256] = "";
      char* char_ptr = NULL;
      uint32 width = 0;
      uint32 height = 0;
      printf("(void*)char_ptr=0x%016lX\n", (unsigned long)char_ptr);
      //printf("buffer='%s'\n", buffer);
      if ( /*TIFFGetField(tiff, TIFFTAG_MODEL, buffer) != 1
          ||*/ TIFFGetField(tiff, TIFFTAG_MODEL, &amp;char_ptr) != 1
          || TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &amp;width) != 1
          || TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &amp;height) != 1)
      {
          puts("One or more TIFFGetField calls failed.");
      }
      printf("width=%u\n", width);
      printf("height=%u\n", height);
      TIFFClose(tiff);
      printf("(void*)char_ptr=0x%016lX\n", (unsigned long)char_ptr);
      for (int n=0; n&lt;5; n++)
      {
          printf("char_ptr[%d]=0x%02X\n", n, (unsigned int)(char_ptr[n]));
      }
      //printf("buffer='%s'\n", buffer);
      printf("char_ptr='%s'\n", char_ptr);
  }
  
  int main()
  {
      WriteTestTIFF();
      ReadTestTIFF();
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>(void*)char_ptr=0x0000000000000000
  width=2
  height=2
  (void*)char_ptr=0x00007FA5B3400310
  char_ptr[0]=0x00
  char_ptr[1]=0x00
  char_ptr[2]=0x00
  char_ptr[3]=0x00
  char_ptr[4]=0x00
  char_ptr=''
  </code></pre>
  
  <p>And just to demonstrate that these fields are being written correctly, here's the resulting TIFF file:</p>
  
  <pre><code>0000000: 4949 2a00 0c00 0000 0000 0000 0e00 0001  II*.............
  0000010: 0300 0100 0000 0200 0000 0101 0300 0100  ................
  0000020: 0000 0200 0000 0201 0300 0100 0000 0800  ................
  0000030: 0000 0301 0300 0100 0000 0100 0000 0601  ................
  0000040: 0300 0100 0000 0100 0000 1001 0200 0b00  ................
  0000050: 0000 d200 0000 1101 0400 0100 0000 0800  ................
  0000060: 0000 1501 0300 0100 0000 0100 0000 1601  ................
  0000070: 0300 0100 0000 0200 0000 1701 0400 0100  ................
  0000080: 0000 0400 0000 1801 0300 0100 0000 0000  ................
  0000090: 0000 1901 0300 0100 0000 ff00 0000 3101  ..............1.
  00000a0: 0200 1800 0000 ba00 0000 5301 0300 0100  ..........S.....
  00000b0: 0000 0100 0000 0000 0000 5465 7374 696e  ..........Testin
  00000c0: 6754 4946 4653 7472 696e 6746 6965 6c64  gTIFFStringField
  00000d0: 7300 4661 6b65 4361 6d65 7261 000a       s.FakeCamera..
  </code></pre>