// Example on how to use the library
int main()
{
  unsigned int width = 1024;
  unsigned int height = 768;
  unsigned int pages = 4;
  unsigned int pixel_bit_depth = 16;
  void* buffer;

  char* output_path = "output.tif";
  char* artist = "Artist";
  char* copyright = "Copyright";
  char* make = "Camera Manufacturer";
  char* model = "Camera Model";
  char* software = "Software";
  char* image_desc = "Created as a dynamic library";

  // Uses global tiffWritePtr, which either points to tiffWrite from a
  // linked file or from a dynamic library.

  TRYFUNC(opendl(), "Could not use dynamic library.")

  TRYFUNC(calculateImageArrays(width, height, pages, pixel_bit_depth, &buffer),
          "Could not calclate buffer.")
  DEBUGP("Calculated buffer.")

  TRYFUNC((*tiffWritePtr) (width, height, pages, pixel_bit_depth,
                           artist, copyright, make, model,
                           software, image_desc,
                           output_path, buffer),
          "Could not create tiff.")
  DEBUGP("Wrote TIFF.")

  TRYFUNC(closedl(), "Could not close dynamic library.")

  return 0;
}
// Example on how to use the library
int main(void)
{
  unsigned int width = 1024;
  unsigned int height = 768;
  unsigned int pages = 7;
  unsigned int pixel_type = CTIFF_PIXEL_UINT16;
  int pixel_bit_depth = ((pixel_type >> 4) + 0x01) << 3;
  void* buffer;
  unsigned int k;
  int retval = 0;
  const void *buf;

  char *output_path    = "output.tif";
  char *artist         = "Artist";
  char *copyright      = "Copyright";
  char *make           = "Camera Manufacturer";
  char *model          = "Camera Model";
  char *software       = "Software";
  char *image_desc     = "Created through include statements.";
  char *meta_ptr;
  char  metadata[][80] = {"{\"key with spaces\": \r\n\t \"data with spaces 1\"}",
                           "{\"numeric_data\": 1337 }",
                           "{\"boolean data\": true}",
                           "{\"array data\": [ [ 1, 2, 3], [4, 5, 6], [7, 8, 9]]}",
                           "{ \"bad json\" 42}",
                           ""};

  TRYFUNC(calculateImageArrays(width, height, pages, pixel_bit_depth, &buffer),
          "Could not calclate buffer.")
  DEBUGP("Calculated buffer.")



  CTIFF ctiff = CTIFFNew(output_path);
  CTIFFWriteEvery(ctiff, 1);
  CTIFFSetStyle(ctiff, width, height, pixel_type, false);
  CTIFFSetRes(ctiff, 72, 72);

  // Not needed, defaults to strict = true
  CTIFFSetStrict(ctiff,true);

  CTIFFSetBasicMeta(ctiff,
                    artist, copyright, make, model, software, image_desc);

  for (k = 0; k < pages; k++){
    buf = moveArrayPtr(buffer, k*width*height, pixel_bit_depth);

    meta_ptr = (k == 0) ? NULL : metadata[k-1];

    if ((retval = CTIFFAddNewPage(ctiff, buf, "meta", meta_ptr)) != 0){
      printf("Could not add image\n");
      CTIFFClose(ctiff);
      return retval;
    }

    // This will return an error because at least one of the pages has already
    // been written to disk.
    /*
     * if (CTIFFSetStrict(ctiff,false) != 0) {
     *   printf("Could not change the strictness of the CTIFF.\n");
     * } */
  }

  retval = CTIFFWrite(ctiff);
  CTIFFClose(ctiff);
  DEBUGP("Wrote TIFF.")

  destroyBuffer(buffer);

  return retval;
}