static int
rotateImage(uint16 rotation, struct image_data *image, uint32 *img_width,
            uint32 *img_length, unsigned char **ibuff_ptr)
  {
  int      shift_width;
  uint32   bytes_per_pixel, bytes_per_sample;
  uint32   row, rowsize, src_offset, dst_offset;
  uint32   i, col, width, length;
  uint32   colsize, buffsize, col_offset, pix_offset;
  unsigned char *ibuff;
  unsigned char *src;
  unsigned char *dst;
  uint16   spp, bps;
  float    res_temp;
  unsigned char *rbuff = NULL;
  width  = *img_width;
  length = *img_length;
  spp = image->spp;
  bps = image->bps;
  rowsize = ((bps * spp * width) + 7) / 8;
  colsize = ((bps * spp * length) + 7) / 8;
  if ((colsize * width) > (rowsize * length))
    buffsize = (colsize + 1) * width;
  else
    buffsize = (rowsize + 1) * length;
  bytes_per_sample = (bps + 7) / 8;
  bytes_per_pixel  = ((bps * spp) + 7) / 8;
  if (bytes_per_pixel < (bytes_per_sample + 1))
    shift_width = bytes_per_pixel;
  else
    shift_width = bytes_per_sample + 1;
  switch (rotation)
    {
    case 0:
    case 360: return (0);
    case 90:
    case 180:
    case 270: break;
    default:  TIFFError("rotateImage", "Invalid rotation angle %d", rotation);
              return (-1);
    }
  if (!(rbuff = (unsigned char *)_TIFFmalloc(buffsize)))
    {
    TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize);
    return (-1);
    }
  _TIFFmemset(rbuff, '\0', buffsize);
  ibuff = *ibuff_ptr;
  switch (rotation)
    {
    case 180: if ((bps % 8) == 0)
                {
                src = ibuff;
                pix_offset = (spp * bps) / 8;
                for (row = 0; row < length; row++)
                   {
		   dst_offset = (length - row - 1) * rowsize;
                   for (col = 0; col < width; col++)
                     {
		     col_offset = (width - col - 1) * pix_offset;
                     dst = rbuff + dst_offset + col_offset;
		     for (i = 0; i  < bytes_per_pixel; i++)
		       *dst++ = *src++;
                     }
                   }
                }
	      else
                {
                for (row = 0; row < length; row++)
                  {
		  src_offset = row * rowsize;
		  dst_offset = (length - row - 1) * rowsize;
		  src = ibuff + src_offset;
                  dst = rbuff + dst_offset;
                  switch (shift_width)
                    {
                    case 1: if (bps == 1)
			      {
                              if (reverseSamples8bits(spp, bps, width, src, dst))
                                {
		                _TIFFfree(rbuff);
                                return (-1);
                                }
                              break;
                              }
                            if (reverseSamples16bits(spp, bps, width, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    case 2: if (reverseSamples24bits(spp, bps, width, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    case 3:
                    case 4:
                    case 5: if (reverseSamples32bits(spp, bps, width, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);
		             _TIFFfree(rbuff);
                             return (-1);
                    }
		  }
		}
              _TIFFfree(ibuff);
              *(ibuff_ptr) = rbuff;
              break;
    case 90:  if ((bps % 8) == 0)
                {
                for (col = 0; col < width; col++)
                  {
		  src_offset = ((length - 1) * rowsize) + (col * bytes_per_pixel);
                  dst_offset = col * colsize;
		  src = ibuff + src_offset;
		  dst = rbuff + dst_offset;
                  for (row = length; row > 0; row--)
                    {
                    for (i = 0; i < bytes_per_pixel; i++)
                      *dst++ = *(src + i);
		    src -= rowsize;
                    }
		  }
		}
              else
                {
                for (col = 0; col < width; col++)
                  {
		  src_offset = (length - 1) * rowsize;
                  dst_offset = col * colsize;
		  src = ibuff + src_offset;
		  dst = rbuff + dst_offset;
                  switch (shift_width)
                    {
                    case 1: if (bps == 1)
			      {
                              if (rotateContigSamples8bits(rotation, spp, bps, width,
				   	                 length, col, src, dst))
                                {
		                _TIFFfree(rbuff);
                                return (-1);
                                }
                              break;
                              }
                            if (rotateContigSamples16bits(rotation, spp, bps, width,
				   	                 length, col, src, dst))
                              {
	                      _TIFFfree(rbuff);
                              return (-1);
		              }
		            break;
                    case 2: if (rotateContigSamples24bits(rotation, spp, bps, width,
					                  length, col, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    case 3:
                    case 4:
                    case 5: if (rotateContigSamples32bits(rotation, spp, bps, width,
					                  length, col, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);
		             _TIFFfree(rbuff);
                             return (-1);
		    }
		  }
		}
              _TIFFfree(ibuff);
              *(ibuff_ptr) = rbuff;
              *img_width = length;
              *img_length = width;
              image->width = length;
              image->length = width;
              res_temp = image->xres;
              image->xres = image->yres;
              image->yres = res_temp;
	      break;
    case 270: if ((bps % 8) == 0)
                {
                for (col = 0; col < width; col++)
                  {
		  src_offset = col * bytes_per_pixel;
                  dst_offset = (width - col - 1) * colsize;
		  src = ibuff + src_offset;
		  dst = rbuff + dst_offset;
                  for (row = length; row > 0; row--)
                    {
                    for (i = 0; i < bytes_per_pixel; i++)
                      *dst++ = *(src + i);
		    src += rowsize;
                    }
		  }
		}
              else
                {
                for (col = 0; col < width; col++)
                  {
		  src_offset = 0;
                  dst_offset = (width - col - 1) * colsize;
		  src = ibuff + src_offset;
		  dst = rbuff + dst_offset;
                  switch (shift_width)
                    {
                    case 1: if (bps == 1)
			      {
                              if (rotateContigSamples8bits(rotation, spp, bps, width,
				   	                 length, col, src, dst))
                                {
		                _TIFFfree(rbuff);
                                return (-1);
                                }
                              break;
                              }
                            if (rotateContigSamples16bits(rotation, spp, bps, width,
				   	                 length, col, src, dst))
                              {
	                      _TIFFfree(rbuff);
                              return (-1);
		              }
		            break;
                    case 2: if (rotateContigSamples24bits(rotation, spp, bps, width,
					                  length, col, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    case 3:
                    case 4:
                    case 5: if (rotateContigSamples32bits(rotation, spp, bps, width,
					                  length, col, src, dst))
                              {
		              _TIFFfree(rbuff);
                              return (-1);
                              }
                             break;
                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);
		             _TIFFfree(rbuff);
                             return (-1);
		    }
		  }
		}
              _TIFFfree(ibuff);
              *(ibuff_ptr) = rbuff;
              *img_width = length;
              *img_length = width;
              image->width = length;
              image->length = width;
              res_temp = image->xres;
              image->xres = image->yres;
              image->yres = res_temp;
              break;
    default:
              break;
    }
  return (0);
  }
['static int\nrotateImage(uint16 rotation, struct image_data *image, uint32 *img_width,\n            uint32 *img_length, unsigned char **ibuff_ptr)\n  {\n  int      shift_width;\n  uint32   bytes_per_pixel, bytes_per_sample;\n  uint32   row, rowsize, src_offset, dst_offset;\n  uint32   i, col, width, length;\n  uint32   colsize, buffsize, col_offset, pix_offset;\n  unsigned char *ibuff;\n  unsigned char *src;\n  unsigned char *dst;\n  uint16   spp, bps;\n  float    res_temp;\n  unsigned char *rbuff = NULL;\n  width  = *img_width;\n  length = *img_length;\n  spp = image->spp;\n  bps = image->bps;\n  rowsize = ((bps * spp * width) + 7) / 8;\n  colsize = ((bps * spp * length) + 7) / 8;\n  if ((colsize * width) > (rowsize * length))\n    buffsize = (colsize + 1) * width;\n  else\n    buffsize = (rowsize + 1) * length;\n  bytes_per_sample = (bps + 7) / 8;\n  bytes_per_pixel  = ((bps * spp) + 7) / 8;\n  if (bytes_per_pixel < (bytes_per_sample + 1))\n    shift_width = bytes_per_pixel;\n  else\n    shift_width = bytes_per_sample + 1;\n  switch (rotation)\n    {\n    case 0:\n    case 360: return (0);\n    case 90:\n    case 180:\n    case 270: break;\n    default:  TIFFError("rotateImage", "Invalid rotation angle %d", rotation);\n              return (-1);\n    }\n  if (!(rbuff = (unsigned char *)_TIFFmalloc(buffsize)))\n    {\n    TIFFError("rotateImage", "Unable to allocate rotation buffer of %1u bytes", buffsize);\n    return (-1);\n    }\n  _TIFFmemset(rbuff, \'\\0\', buffsize);\n  ibuff = *ibuff_ptr;\n  switch (rotation)\n    {\n    case 180: if ((bps % 8) == 0)\n                {\n                src = ibuff;\n                pix_offset = (spp * bps) / 8;\n                for (row = 0; row < length; row++)\n                   {\n\t\t   dst_offset = (length - row - 1) * rowsize;\n                   for (col = 0; col < width; col++)\n                     {\n\t\t     col_offset = (width - col - 1) * pix_offset;\n                     dst = rbuff + dst_offset + col_offset;\n\t\t     for (i = 0; i  < bytes_per_pixel; i++)\n\t\t       *dst++ = *src++;\n                     }\n                   }\n                }\n\t      else\n                {\n                for (row = 0; row < length; row++)\n                  {\n\t\t  src_offset = row * rowsize;\n\t\t  dst_offset = (length - row - 1) * rowsize;\n\t\t  src = ibuff + src_offset;\n                  dst = rbuff + dst_offset;\n                  switch (shift_width)\n                    {\n                    case 1: if (bps == 1)\n\t\t\t      {\n                              if (reverseSamples8bits(spp, bps, width, src, dst))\n                                {\n\t\t                _TIFFfree(rbuff);\n                                return (-1);\n                                }\n                              break;\n                              }\n                            if (reverseSamples16bits(spp, bps, width, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    case 2: if (reverseSamples24bits(spp, bps, width, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    case 3:\n                    case 4:\n                    case 5: if (reverseSamples32bits(spp, bps, width, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);\n\t\t             _TIFFfree(rbuff);\n                             return (-1);\n                    }\n\t\t  }\n\t\t}\n              _TIFFfree(ibuff);\n              *(ibuff_ptr) = rbuff;\n              break;\n    case 90:  if ((bps % 8) == 0)\n                {\n                for (col = 0; col < width; col++)\n                  {\n\t\t  src_offset = ((length - 1) * rowsize) + (col * bytes_per_pixel);\n                  dst_offset = col * colsize;\n\t\t  src = ibuff + src_offset;\n\t\t  dst = rbuff + dst_offset;\n                  for (row = length; row > 0; row--)\n                    {\n                    for (i = 0; i < bytes_per_pixel; i++)\n                      *dst++ = *(src + i);\n\t\t    src -= rowsize;\n                    }\n\t\t  }\n\t\t}\n              else\n                {\n                for (col = 0; col < width; col++)\n                  {\n\t\t  src_offset = (length - 1) * rowsize;\n                  dst_offset = col * colsize;\n\t\t  src = ibuff + src_offset;\n\t\t  dst = rbuff + dst_offset;\n                  switch (shift_width)\n                    {\n                    case 1: if (bps == 1)\n\t\t\t      {\n                              if (rotateContigSamples8bits(rotation, spp, bps, width,\n\t\t\t\t   \t                 length, col, src, dst))\n                                {\n\t\t                _TIFFfree(rbuff);\n                                return (-1);\n                                }\n                              break;\n                              }\n                            if (rotateContigSamples16bits(rotation, spp, bps, width,\n\t\t\t\t   \t                 length, col, src, dst))\n                              {\n\t                      _TIFFfree(rbuff);\n                              return (-1);\n\t\t              }\n\t\t            break;\n                    case 2: if (rotateContigSamples24bits(rotation, spp, bps, width,\n\t\t\t\t\t                  length, col, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    case 3:\n                    case 4:\n                    case 5: if (rotateContigSamples32bits(rotation, spp, bps, width,\n\t\t\t\t\t                  length, col, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);\n\t\t             _TIFFfree(rbuff);\n                             return (-1);\n\t\t    }\n\t\t  }\n\t\t}\n              _TIFFfree(ibuff);\n              *(ibuff_ptr) = rbuff;\n              *img_width = length;\n              *img_length = width;\n              image->width = length;\n              image->length = width;\n              res_temp = image->xres;\n              image->xres = image->yres;\n              image->yres = res_temp;\n\t      break;\n    case 270: if ((bps % 8) == 0)\n                {\n                for (col = 0; col < width; col++)\n                  {\n\t\t  src_offset = col * bytes_per_pixel;\n                  dst_offset = (width - col - 1) * colsize;\n\t\t  src = ibuff + src_offset;\n\t\t  dst = rbuff + dst_offset;\n                  for (row = length; row > 0; row--)\n                    {\n                    for (i = 0; i < bytes_per_pixel; i++)\n                      *dst++ = *(src + i);\n\t\t    src += rowsize;\n                    }\n\t\t  }\n\t\t}\n              else\n                {\n                for (col = 0; col < width; col++)\n                  {\n\t\t  src_offset = 0;\n                  dst_offset = (width - col - 1) * colsize;\n\t\t  src = ibuff + src_offset;\n\t\t  dst = rbuff + dst_offset;\n                  switch (shift_width)\n                    {\n                    case 1: if (bps == 1)\n\t\t\t      {\n                              if (rotateContigSamples8bits(rotation, spp, bps, width,\n\t\t\t\t   \t                 length, col, src, dst))\n                                {\n\t\t                _TIFFfree(rbuff);\n                                return (-1);\n                                }\n                              break;\n                              }\n                            if (rotateContigSamples16bits(rotation, spp, bps, width,\n\t\t\t\t   \t                 length, col, src, dst))\n                              {\n\t                      _TIFFfree(rbuff);\n                              return (-1);\n\t\t              }\n\t\t            break;\n                    case 2: if (rotateContigSamples24bits(rotation, spp, bps, width,\n\t\t\t\t\t                  length, col, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    case 3:\n                    case 4:\n                    case 5: if (rotateContigSamples32bits(rotation, spp, bps, width,\n\t\t\t\t\t                  length, col, src, dst))\n                              {\n\t\t              _TIFFfree(rbuff);\n                              return (-1);\n                              }\n                             break;\n                    default: TIFFError("rotateImage","Unsupported bit depth %d", bps);\n\t\t             _TIFFfree(rbuff);\n                             return (-1);\n\t\t    }\n\t\t  }\n\t\t}\n              _TIFFfree(ibuff);\n              *(ibuff_ptr) = rbuff;\n              *img_width = length;\n              *img_length = width;\n              image->width = length;\n              image->length = width;\n              res_temp = image->xres;\n              image->xres = image->yres;\n              image->yres = res_temp;\n              break;\n    default:\n              break;\n    }\n  return (0);\n  }', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'void\n_TIFFmemset(void* p, int v, tmsize_t c)\n{\n\tmemset(p, v, (size_t) c);\n}']