tools/tiffcrop.c:6927: error: Integer Overflow L2
  ([0, 4294967294] + [0, 4294967294]):unsigned32 by call to `writeCroppedImage`.

tools/tiffcrop.c:6891:1: Parameter `(*out)->tif_dir.td_imagewidth`
6889.   } /* end extractImageSection */
6890. 
6891. static int 
      ^
6892. writeSelections(TIFF *in, TIFF **out, struct crop_mask *crop, 
6893.                 struct image_data *image, struct dump_opts *dump,

tools/tiffcrop.c:6921:14: Call
6919.     case ONE_FILE_SEPARATED: /* Regions as separated images */
6920.          autoindex = 0;
6921.          if (update_output_file (out, mp, autoindex, filename, page))
                   ^
6922.            return (1);
6923.          page_count = crop->selections * total_pages;

tools/tiffcrop.c:2106:1: Parameter `(*tiffout)->tif_dir.td_imagewidth`
  2104.  * so TIFFTAG PAGENUM will be correct in image.
  2105.  */
  2106. static int
        ^
  2107. update_output_file (TIFF **tiffout, char *mode, int autoindex,
  2108.                     char *outname, unsigned int *page)

tools/tiffcrop.c:6927:16: Call
6925.            {
6926.            crop_buff = seg_buffs[i].buffer;
6927.            if (writeCroppedImage(in, *out, image, dump,
                     ^
6928.                                  crop->regionlist[i].width, 
6929.                                  crop->regionlist[i].length, 

tools/tiffcrop.c:7734:1: Parameter `out->tif_dir.td_imagewidth`
  7732.  * then passed in as an argument.
  7733.  */
  7734. static int  
        ^
  7735. writeCroppedImage(TIFF *in, TIFF *out, struct image_data *image, 
  7736.                   struct dump_opts *dump, uint32 width, uint32 length, 

tools/tiffcrop.c:7978:11: Call
  7976.     if (config == PLANARCONFIG_CONTIG)
  7977.       {
  7978.       if (writeBufferToContigTiles (out, crop_buff, length, width, spp, dump))
                  ^
  7979.         TIFFError("","Unable to write contiguous tile data for page %d", pagenum);
  7980.       }

tools/tiffcrop.c:1227:1: Parameter `out->tif_dir.td_imagewidth`
    1225.  * to be written out as a tile.
    1226.  */
    1227. static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,
          ^
    1228. 				       uint32 imagewidth, tsample_t spp, 
    1229.                                        struct dump_opts* dump)

tools/tiffcrop.c:1304:11: Call
    1302.         }
    1303. 
    1304.       if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0)
                    ^
    1305.         {
    1306. 	TIFFError("writeBufferToContigTiles",

libtiff/tif_write.c:363:1: Parameter `tif->tif_dir.td_imagewidth`
      361.  * tile is selected by the (x,y,z,s) coordinates.
      362.  */
      363. tmsize_t
           ^
      364. TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
      365. {

libtiff/tif_write.c:366:7: Call
      364. TIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)
      365. {
      366. 	if (!TIFFCheckTile(tif, x, y, z, s))
                 ^
      367. 		return ((tmsize_t)(-1));
      368. 	/*

libtiff/tif_tile.c:72:1: Parameter `x`
        70.  * against the image bounds.
        71.  */
        72. int
            ^
        73. TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
        74. {

libtiff/tif_write.c:375:6: Call
      373. 	 */
      374. 	return (TIFFWriteEncodedTile(tif,
      375. 	    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));
                ^
      376. }
      377. 

libtiff/tif_tile.c:35:1: <LHS trace>
33.  * Compute which tile an (x,y,z,s) value is in.
34.  */
35. uint32
    ^
36. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
37. {

libtiff/tif_tile.c:35:1: Parameter `tif->tif_dir.td_imagewidth`
        33.  * Compute which tile an (x,y,z,s) value is in.
        34.  */
        35. uint32
            ^
        36. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
        37. {

libtiff/tif_tile.c:35:1: <RHS trace>
33.  * Compute which tile an (x,y,z,s) value is in.
34.  */
35. uint32
    ^
36. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
37. {

libtiff/tif_tile.c:35:1: Parameter `tif->tif_dir.td_imagewidth`
        33.  * Compute which tile an (x,y,z,s) value is in.
        34.  */
        35. uint32
            ^
        36. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
        37. {

libtiff/tif_tile.c:53:16: Binary operation: ([0, 4294967294] + [0, 4294967294]):unsigned32 by call to `writeCroppedImage` 
        51. 		dz = td->td_imagedepth;
        52. 	if (dx != 0 && dy != 0 && dz != 0) {
        53. 		uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
                           ^
        54. 		uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
        55. 		uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);

uint32
TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
{
	TIFFDirectory *td = &tif->tif_dir;
	uint32 dx = td->td_tilewidth;
	uint32 dy = td->td_tilelength;
	uint32 dz = td->td_tiledepth;
	uint32 tile = 1;
	if (td->td_imagedepth == 1)
		z = 0;
	if (dx == (uint32) -1)
		dx = td->td_imagewidth;
	if (dy == (uint32) -1)
		dy = td->td_imagelength;
	if (dz == (uint32) -1)
		dz = td->td_imagedepth;
	if (dx != 0 && dy != 0 && dz != 0) {
		uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
		uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
		uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
		if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
			tile = (xpt*ypt*zpt)*s +
			     (xpt*ypt)*(z/dz) +
			     xpt*(y/dy) +
			     x/dx;
		else
			tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
	}
	return (tile);
}
['static int\nwriteSelections(TIFF *in, TIFF **out, struct crop_mask *crop,\n                struct image_data *image, struct dump_opts *dump,\n                struct buffinfo seg_buffs[], char *mp, char *filename,\n                unsigned int *page, unsigned int total_pages)\n  {\n  int i, page_count;\n  int autoindex = 0;\n  unsigned char *crop_buff = NULL;\n  switch (crop->exp_mode)\n    {\n    case ONE_FILE_COMPOSITE:\n         autoindex = 0;\n         crop_buff = seg_buffs[0].buffer;\n         if (update_output_file (out, mp, autoindex, filename, page))\n           return (1);\n         page_count = total_pages;\n         if (writeCroppedImage(in, *out, image, dump,\n                               crop->combined_width,\n                               crop->combined_length,\n                               crop_buff, *page, total_pages))\n            {\n             TIFFError("writeRegions", "Unable to write new image");\n             return (-1);\n             }\n\t break;\n    case ONE_FILE_SEPARATED:\n         autoindex = 0;\n         if (update_output_file (out, mp, autoindex, filename, page))\n           return (1);\n         page_count = crop->selections * total_pages;\n         for (i = 0; i < crop->selections; i++)\n           {\n           crop_buff = seg_buffs[i].buffer;\n           if (writeCroppedImage(in, *out, image, dump,\n                                 crop->regionlist[i].width,\n                                 crop->regionlist[i].length,\n                                 crop_buff, *page, page_count))\n             {\n             TIFFError("writeRegions", "Unable to write new image");\n             return (-1);\n             }\n\t   }\n         break;\n    case FILE_PER_IMAGE_COMPOSITE:\n         autoindex = 1;\n         if (update_output_file (out, mp, autoindex, filename, page))\n           return (1);\n         crop_buff = seg_buffs[0].buffer;\n         if (writeCroppedImage(in, *out, image, dump,\n                               crop->combined_width,\n                               crop->combined_length,\n                               crop_buff, *page, total_pages))\n           {\n           TIFFError("writeRegions", "Unable to write new image");\n           return (-1);\n           }\n         break;\n    case FILE_PER_IMAGE_SEPARATED:\n         autoindex = 1;\n         page_count = crop->selections;\n         if (update_output_file (out, mp, autoindex, filename, page))\n           return (1);\n         for (i = 0; i < crop->selections; i++)\n           {\n           crop_buff = seg_buffs[i].buffer;\n           if (writeCroppedImage(in, *out, image, dump,\n                                 crop->regionlist[i].width,\n                                 crop->regionlist[i].length,\n                                 crop_buff, *page, page_count))\n             {\n             TIFFError("writeRegions", "Unable to write new image");\n             return (-1);\n             }\n           }\n         break;\n    case FILE_PER_SELECTION:\n         autoindex = 1;\n\t page_count = 1;\n         for (i = 0; i < crop->selections; i++)\n           {\n           if (update_output_file (out, mp, autoindex, filename, page))\n             return (1);\n           crop_buff = seg_buffs[i].buffer;\n           if (writeCroppedImage(in, *out, image, dump,\n                                 crop->regionlist[i].width,\n                                 crop->regionlist[i].length,\n                                 crop_buff, *page, page_count))\n             {\n             TIFFError("writeRegions", "Unable to write new image");\n             return (-1);\n             }\n           }\n\t break;\n    default: return (1);\n    }\n  return (0);\n  }', 'static int\nupdate_output_file (TIFF **tiffout, char *mode, int autoindex,\n                    char *outname, unsigned int *page)\n  {\n  static int findex = 0;\n  size_t basename_len;\n  char  *sep;\n  char   export_ext[16];\n  char   exportname[PATH_MAX];\n  if (autoindex && (*tiffout != NULL))\n    {\n    TIFFClose (*tiffout);\n    *tiffout = NULL;\n    }\n  memcpy (export_ext, ".tiff", 6);\n  memset (exportname, \'\\0\', sizeof(exportname));\n  #define FILENUM_MAX_LENGTH (1+6+1+4+1)\n  strncpy (exportname, outname, sizeof(exportname) - FILENUM_MAX_LENGTH);\n  if (*tiffout == NULL)\n    {\n    if (autoindex)\n      {\n      findex++;\n      if ((sep = strstr(exportname, ".tif")) || (sep = strstr(exportname, ".TIF")))\n        {\n        strncpy (export_ext, sep, 5);\n        *sep = \'\\0\';\n        }\n      else\n        memcpy (export_ext, ".tiff", 5);\n      export_ext[5] = \'\\0\';\n      basename_len = strlen(exportname);\n      if (findex > MAX_EXPORT_PAGES)\n        {\n        TIFFError("update_output_file", "Maximum of %d pages per file exceeded", MAX_EXPORT_PAGES);\n        return 1;\n        }\n      snprintf(exportname + basename_len, sizeof(exportname) - basename_len, "-%03d%.5s", findex, export_ext);\n      }\n    exportname[sizeof(exportname) - 1] = \'\\0\';\n    *tiffout = TIFFOpen(exportname, mode);\n    if (*tiffout == NULL)\n      {\n      TIFFError("update_output_file", "Unable to open output file %s", exportname);\n      return 1;\n      }\n    *page = 0;\n    return 0;\n    }\n  else\n    (*page)++;\n  return 0;\n  }', 'static int\nwriteCroppedImage(TIFF *in, TIFF *out, struct image_data *image,\n                  struct dump_opts *dump, uint32 width, uint32 length,\n                  unsigned char *crop_buff, int pagenum, int total_pages)\n  {\n  uint16 bps, spp;\n  uint16 input_compression, input_photometric;\n  uint16 input_planar;\n  struct cpTag* p;\n  input_compression = image->compression;\n  input_photometric = image->photometric;\n  spp = image->spp;\n  bps = image->bps;\n  TIFFSetField(out, TIFFTAG_IMAGEWIDTH, width);\n  TIFFSetField(out, TIFFTAG_IMAGELENGTH, length);\n  TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, bps);\n  TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, spp);\n#ifdef DEBUG2\n  TIFFError("writeCroppedImage", "Input compression: %s",\n\t    (input_compression == COMPRESSION_OJPEG) ? "Old Jpeg" :\n\t    ((input_compression == COMPRESSION_JPEG) ?  "New Jpeg" : "Non Jpeg"));\n#endif\n  if (compression != (uint16)-1)\n    TIFFSetField(out, TIFFTAG_COMPRESSION, compression);\n  else\n    {\n    if (input_compression == COMPRESSION_OJPEG)\n      {\n      compression = COMPRESSION_JPEG;\n      jpegcolormode = JPEGCOLORMODE_RAW;\n      TIFFSetField(out, TIFFTAG_COMPRESSION, COMPRESSION_JPEG);\n      }\n    else\n      CopyField(TIFFTAG_COMPRESSION, compression);\n    }\n  if (compression == COMPRESSION_JPEG)\n    {\n    if ((input_photometric == PHOTOMETRIC_PALETTE) ||\n        (input_photometric == PHOTOMETRIC_MASK))\n      {\n      TIFFError ("writeCroppedImage",\n                 "JPEG compression cannot be used with %s image data",\n      \t        (input_photometric == PHOTOMETRIC_PALETTE) ?\n                 "palette" : "mask");\n      return (-1);\n      }\n    if ((input_photometric == PHOTOMETRIC_RGB) &&\n\t(jpegcolormode == JPEGCOLORMODE_RGB))\n      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_YCBCR);\n    else\n\tTIFFSetField(out, TIFFTAG_PHOTOMETRIC, input_photometric);\n    }\n  else\n    {\n    if (compression == COMPRESSION_SGILOG || compression == COMPRESSION_SGILOG24)\n      {\n      TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\tPHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n      }\n    else\n      {\n      if (input_compression == COMPRESSION_SGILOG ||\n          input_compression == COMPRESSION_SGILOG24)\n        {\n        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, spp == 1 ?\n\t\t\t  PHOTOMETRIC_LOGL : PHOTOMETRIC_LOGLUV);\n        }\n      else\n        TIFFSetField(out, TIFFTAG_PHOTOMETRIC, image->photometric);\n      }\n    }\n  if (((input_photometric == PHOTOMETRIC_LOGL) ||\n       (input_photometric ==  PHOTOMETRIC_LOGLUV)) &&\n      ((compression != COMPRESSION_SGILOG) &&\n       (compression != COMPRESSION_SGILOG24)))\n    {\n    TIFFError("writeCroppedImage",\n              "LogL and LogLuv source data require SGI_LOG or SGI_LOG24 compression");\n    return (-1);\n    }\n  if (fillorder != 0)\n    TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);\n  else\n    CopyTag(TIFFTAG_FILLORDER, 1, TIFF_SHORT);\n  TIFFSetField(out, TIFFTAG_ORIENTATION, image->orientation);\n  if (outtiled == -1)\n    outtiled = TIFFIsTiled(in);\n  if (outtiled) {\n    if (tilewidth == (uint32) 0)\n      TIFFGetField(in, TIFFTAG_TILEWIDTH, &tilewidth);\n    if (tilelength == (uint32) 0)\n      TIFFGetField(in, TIFFTAG_TILELENGTH, &tilelength);\n    if (tilewidth == 0 || tilelength == 0)\n      TIFFDefaultTileSize(out, &tilewidth, &tilelength);\n    TIFFSetField(out, TIFFTAG_TILEWIDTH, tilewidth);\n    TIFFSetField(out, TIFFTAG_TILELENGTH, tilelength);\n    } else {\n\tif (rowsperstrip == (uint32) 0)\n          {\n\t  if (!TIFFGetField(in, TIFFTAG_ROWSPERSTRIP, &rowsperstrip))\n\t    rowsperstrip = TIFFDefaultStripSize(out, rowsperstrip);\n          if (compression != COMPRESSION_JPEG)\n            {\n  \t    if (rowsperstrip > length)\n\t      rowsperstrip = length;\n\t    }\n\t  }\n\telse\n          if (rowsperstrip == (uint32) -1)\n\t    rowsperstrip = length;\n\tTIFFSetField(out, TIFFTAG_ROWSPERSTRIP, rowsperstrip);\n\t}\n  TIFFGetFieldDefaulted(in, TIFFTAG_PLANARCONFIG, &input_planar);\n  if (config != (uint16) -1)\n    TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);\n  else\n    CopyField(TIFFTAG_PLANARCONFIG, config);\n  if (spp <= 4)\n    CopyTag(TIFFTAG_TRANSFERFUNCTION, 4, TIFF_SHORT);\n  CopyTag(TIFFTAG_COLORMAP, 4, TIFF_SHORT);\n  switch (compression) {\n    case COMPRESSION_JPEG:\n         if (((bps % 8) == 0) || ((bps % 12) == 0))\n\t   {\n           TIFFSetField(out, TIFFTAG_JPEGQUALITY, quality);\n\t   TIFFSetField(out, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);\n           }\n         else\n           {\n\t   TIFFError("writeCroppedImage",\n                     "JPEG compression requires 8 or 12 bits per sample");\n           return (-1);\n           }\n\t break;\n   case COMPRESSION_LZW:\n   case COMPRESSION_ADOBE_DEFLATE:\n   case COMPRESSION_DEFLATE:\n\tif (predictor != (uint16)-1)\n          TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);\n\telse\n\t  CopyField(TIFFTAG_PREDICTOR, predictor);\n\tbreak;\n   case COMPRESSION_CCITTFAX3:\n   case COMPRESSION_CCITTFAX4:\n        if (bps != 1)\n          {\n\t  TIFFError("writeCroppedImage",\n            "Group 3/4 compression is not usable with bps > 1");\n          return (-1);\n\t  }\n\tif (compression == COMPRESSION_CCITTFAX3) {\n          if (g3opts != (uint32) -1)\n\t    TIFFSetField(out, TIFFTAG_GROUP3OPTIONS, g3opts);\n\t  else\n\t    CopyField(TIFFTAG_GROUP3OPTIONS, g3opts);\n\t} else {\n\t    CopyTag(TIFFTAG_GROUP4OPTIONS, 1, TIFF_LONG);\n        }\n        CopyTag(TIFFTAG_BADFAXLINES, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_CLEANFAXDATA, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_CONSECUTIVEBADFAXLINES, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXRECVPARAMS, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXRECVTIME, 1, TIFF_LONG);\n        CopyTag(TIFFTAG_FAXSUBADDRESS, 1, TIFF_ASCII);\n        break;\n    case COMPRESSION_NONE:\n         break;\n    default: break;\n   }\n   { uint32 len32;\n     void** data;\n     if (TIFFGetField(in, TIFFTAG_ICCPROFILE, &len32, &data))\n       TIFFSetField(out, TIFFTAG_ICCPROFILE, len32, data);\n   }\n   { uint16 ninks;\n     const char* inknames;\n     if (TIFFGetField(in, TIFFTAG_NUMBEROFINKS, &ninks)) {\n       TIFFSetField(out, TIFFTAG_NUMBEROFINKS, ninks);\n       if (TIFFGetField(in, TIFFTAG_INKNAMES, &inknames)) {\n\t int inknameslen = strlen(inknames) + 1;\n\t const char* cp = inknames;\n\t while (ninks > 1) {\n\t   cp = strchr(cp, \'\\0\');\n\t   if (cp) {\n\t     cp++;\n\t     inknameslen += (strlen(cp) + 1);\n\t   }\n\t   ninks--;\n         }\n\t TIFFSetField(out, TIFFTAG_INKNAMES, inknameslen, inknames);\n       }\n     }\n   }\n   {\n   unsigned short pg0, pg1;\n   if (TIFFGetField(in, TIFFTAG_PAGENUMBER, &pg0, &pg1)) {\n     TIFFSetField(out, TIFFTAG_PAGENUMBER, pagenum, total_pages);\n     }\n   }\n  for (p = tags; p < &tags[NTAGS]; p++)\n\t\tCopyTag(p->tag, p->count, p->type);\n  if (outtiled)\n    {\n    if (config == PLANARCONFIG_CONTIG)\n      {\n      if (writeBufferToContigTiles (out, crop_buff, length, width, spp, dump))\n        TIFFError("","Unable to write contiguous tile data for page %d", pagenum);\n      }\n    else\n      {\n      if (writeBufferToSeparateTiles (out, crop_buff, length, width, spp, dump))\n        TIFFError("","Unable to write separate tile data for page %d", pagenum);\n      }\n    }\n  else\n    {\n    if (config == PLANARCONFIG_CONTIG)\n      {\n      if (writeBufferToContigStrips (out, crop_buff, length))\n        TIFFError("","Unable to write contiguous strip data for page %d", pagenum);\n      }\n    else\n      {\n      if (writeBufferToSeparateStrips(out, crop_buff, length, width, spp, dump))\n        TIFFError("","Unable to write separate strip data for page %d", pagenum);\n      }\n    }\n  if (!TIFFWriteDirectory(out))\n    {\n    TIFFError("","Failed to write IFD for page number %d", pagenum);\n    return (-1);\n    }\n  return (0);\n  }', 'static int writeBufferToContigTiles (TIFF* out, uint8* buf, uint32 imagelength,\n\t\t\t\t       uint32 imagewidth, tsample_t spp,\n                                       struct dump_opts* dump)\n  {\n  uint16 bps;\n  uint32 tl, tw;\n  uint32 row, col, nrow, ncol;\n  uint32 src_rowsize, col_offset;\n  uint32 tile_rowsize  = TIFFTileRowSize(out);\n  uint8* bufp = (uint8*) buf;\n  tsize_t tile_buffsize = 0;\n  tsize_t tilesize = TIFFTileSize(out);\n  unsigned char *tilebuf = NULL;\n  if( !TIFFGetField(out, TIFFTAG_TILELENGTH, &tl) ||\n      !TIFFGetField(out, TIFFTAG_TILEWIDTH, &tw) ||\n      !TIFFGetField(out, TIFFTAG_BITSPERSAMPLE, &bps) )\n      return 1;\n  if (tilesize == 0 || tile_rowsize == 0 || tl == 0 || tw == 0)\n  {\n    TIFFError("writeBufferToContigTiles", "Tile size, tile row size, tile width, or tile length is zero");\n    exit(-1);\n  }\n  tile_buffsize = tilesize;\n  if (tilesize < (tsize_t)(tl * tile_rowsize))\n    {\n#ifdef DEBUG2\n    TIFFError("writeBufferToContigTiles",\n\t      "Tilesize %lu is too small, using alternate calculation %u",\n              tilesize, tl * tile_rowsize);\n#endif\n    tile_buffsize = tl * tile_rowsize;\n    if (tl != tile_buffsize / tile_rowsize)\n    {\n\tTIFFError("writeBufferToContigTiles", "Integer overflow when calculating buffer size");\n\texit(-1);\n    }\n    }\n  if( imagewidth == 0 ||\n      (uint32)bps * (uint32)spp > TIFF_UINT32_MAX / imagewidth ||\n      bps * spp * imagewidth > TIFF_UINT32_MAX - 7U )\n  {\n      TIFFError(TIFFFileName(out),\n            "Error, uint32 overflow when computing (imagewidth * bps * spp) + 7");\n      return 1;\n  }\n  src_rowsize = ((imagewidth * spp * bps) + 7U) / 8;\n  tilebuf = _TIFFmalloc(tile_buffsize);\n  if (tilebuf == 0)\n    return 1;\n  for (row = 0; row < imagelength; row += tl)\n    {\n    nrow = (row + tl > imagelength) ? imagelength - row : tl;\n    for (col = 0; col < imagewidth; col += tw)\n      {\n      if (col + tw > imagewidth)\n\tncol = imagewidth - col;\n      else\n        ncol = tw;\n      col_offset = (((col * bps * spp) + 7) / 8);\n      bufp = buf + (row * src_rowsize) + col_offset;\n      if (extractContigSamplesToTileBuffer(tilebuf, bufp, nrow, ncol, imagewidth,\n\t\t\t\t\t   tw, 0, spp, spp, bps, dump) > 0)\n        {\n\tTIFFError("writeBufferToContigTiles",\n                  "Unable to extract data to tile for row %lu, col %lu",\n                  (unsigned long) row, (unsigned long)col);\n\t_TIFFfree(tilebuf);\n\treturn 1;\n        }\n      if (TIFFWriteTile(out, tilebuf, col, row, 0, 0) < 0)\n        {\n\tTIFFError("writeBufferToContigTiles",\n\t          "Cannot write tile at %lu %lu",\n\t          (unsigned long) col, (unsigned long) row);\n\t _TIFFfree(tilebuf);\n\treturn 1;\n\t}\n      }\n    }\n  _TIFFfree(tilebuf);\n  return 0;\n  }', 'tmsize_t\nTIFFWriteTile(TIFF* tif, void* buf, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tif (!TIFFCheckTile(tif, x, y, z, s))\n\t\treturn ((tmsize_t)(-1));\n\treturn (TIFFWriteEncodedTile(tif,\n\t    TIFFComputeTile(tif, x, y, z, s), buf, (tmsize_t)(-1)));\n}', 'int\nTIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tif (x >= td->td_imagewidth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Col out of range, max %lu",\n\t\t\t     (unsigned long) x,\n\t\t\t     (unsigned long) (td->td_imagewidth - 1));\n\t\treturn (0);\n\t}\n\tif (y >= td->td_imagelength) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Row out of range, max %lu",\n\t\t\t     (unsigned long) y,\n\t\t\t     (unsigned long) (td->td_imagelength - 1));\n\t\treturn (0);\n\t}\n\tif (z >= td->td_imagedepth) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Depth out of range, max %lu",\n\t\t\t     (unsigned long) z,\n\t\t\t     (unsigned long) (td->td_imagedepth - 1));\n\t\treturn (0);\n\t}\n\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE &&\n\t    s >= td->td_samplesperpixel) {\n\t\tTIFFErrorExt(tif->tif_clientdata, tif->tif_name,\n\t\t\t     "%lu: Sample out of range, max %lu",\n\t\t\t     (unsigned long) s,\n\t\t\t     (unsigned long) (td->td_samplesperpixel - 1));\n\t\treturn (0);\n\t}\n\treturn (1);\n}', 'uint32\nTIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)\n{\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 dx = td->td_tilewidth;\n\tuint32 dy = td->td_tilelength;\n\tuint32 dz = td->td_tiledepth;\n\tuint32 tile = 1;\n\tif (td->td_imagedepth == 1)\n\t\tz = 0;\n\tif (dx == (uint32) -1)\n\t\tdx = td->td_imagewidth;\n\tif (dy == (uint32) -1)\n\t\tdy = td->td_imagelength;\n\tif (dz == (uint32) -1)\n\t\tdz = td->td_imagedepth;\n\tif (dx != 0 && dy != 0 && dz != 0) {\n\t\tuint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);\n\t\tuint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);\n\t\tuint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);\n\t\tif (td->td_planarconfig == PLANARCONFIG_SEPARATE)\n\t\t\ttile = (xpt*ypt*zpt)*s +\n\t\t\t     (xpt*ypt)*(z/dz) +\n\t\t\t     xpt*(y/dy) +\n\t\t\t     x/dx;\n\t\telse\n\t\t\ttile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;\n\t}\n\treturn (tile);\n}']