static int
reverseSamples24bits (uint16 spp, uint16 bps, uint32 width,
                      uint8 *ibuff, uint8 *obuff)
  {
  int      ready_bits = 0;
  uint32   col;
  uint32   src_byte = 0, src_bit = 0;
  uint32   bit_offset = 0;
  uint32   matchbits = 0, maskbits = 0;
  uint32   buff1 = 0, buff2 = 0;
  uint8    bytebuff1 = 0, bytebuff2 = 0;
  unsigned char *src;
  unsigned char *dst;
  unsigned char  swapbuff[4];
  tsample_t sample;
  if ((ibuff == NULL) || (obuff == NULL))
    {
    TIFFError("reverseSamples24bits","Invalid image or work buffer");
    return (1);
    }
  ready_bits = 0;
  maskbits =  (uint32)-1 >> (32 - bps);
  dst = obuff;
  for (col = width; col > 0; col--)
    {
    bit_offset = (col - 1) * bps * spp;
    for (sample = 0; sample < spp; sample++)
      {
      if (sample == 0)
        {
        src_byte = bit_offset / 8;
        src_bit  = bit_offset % 8;
        }
      else
        {
        src_byte = (bit_offset + (sample * bps)) / 8;
        src_bit  = (bit_offset + (sample * bps)) % 8;
        }
      src = ibuff + src_byte;
      matchbits = maskbits << (32 - src_bit - bps);
      if (little_endian)
        {
        swapbuff[3] = *src;
        swapbuff[2] = *(src + 1);
        swapbuff[1] = *(src + 2);
        swapbuff[0] = *(src + 3);
        }
      else
        {
        swapbuff[0] = *src;
        swapbuff[1] = *(src + 1);
        swapbuff[2] = *(src + 2);
        swapbuff[3] = *(src + 3);
	}
      buff1 = *((uint32 *)swapbuff);
      buff1 = (buff1 & matchbits) << (src_bit);
      if (ready_bits < 16)
        {
        bytebuff1 = bytebuff2 = 0;
        buff2 = (buff2 | (buff1 >> ready_bits));
        }
      else
        {
        bytebuff1 = (buff2 >> 24);
        *dst++ = bytebuff1;
        bytebuff2 = (buff2 >> 16);
        *dst++ = bytebuff2;
        ready_bits -= 16;
        buff2 = ((buff2 << 16) | (buff1 >> ready_bits));
        }
      ready_bits += bps;
      }
    }
  while (ready_bits > 0)
    {
    bytebuff1 = (buff2 >> 24);
    *dst++ = bytebuff1;
    buff2 = (buff2 << 8);
    bytebuff2 = bytebuff1;
    ready_bits -= 8;
    }
  return (0);
  }
['static int\nmirrorImage(uint16 spp, uint16 bps, uint16 mirror, uint32 width, uint32 length, unsigned char *ibuff)\n  {\n  int      shift_width;\n  uint32   bytes_per_pixel, bytes_per_sample;\n  uint32   row, rowsize, row_offset;\n  unsigned char *line_buff = NULL;\n  unsigned char *src;\n  unsigned char *dst;\n  src = ibuff;\n  rowsize = ((width * bps * spp) + 7) / 8;\n  switch (mirror)\n    {\n    case MIRROR_BOTH:\n    case MIRROR_VERT:\n             line_buff = (unsigned char *)_TIFFmalloc(rowsize);\n             if (line_buff == NULL)\n               {\n\t       TIFFError ("mirrorImage", "Unable to allocate mirror line buffer of %1u bytes", rowsize);\n               return (-1);\n               }\n             dst = ibuff + (rowsize * (length - 1));\n             for (row = 0; row < length / 2; row++)\n               {\n\t      _TIFFmemcpy(line_buff, src, rowsize);\n\t      _TIFFmemcpy(src, dst,  rowsize);\n\t      _TIFFmemcpy(dst, line_buff, rowsize);\n               src += (rowsize);\n               dst -= (rowsize);\n               }\n             if (line_buff)\n               _TIFFfree(line_buff);\n             if (mirror == MIRROR_VERT)\n               break;\n    case MIRROR_HORIZ :\n              if ((bps % 8) == 0)\n                {\n                for (row = 0; row < length; row++)\n                  {\n\t\t  row_offset = row * rowsize;\n                  src = ibuff + row_offset;\n                  dst = ibuff + row_offset + rowsize;\n                  if (reverseSamplesBytes(spp, bps, width, src, dst))\n                    {\n\t\t    return (-1);\n                    }\n\t\t  }\n\t\t}\n\t      else\n                {\n                if (!(line_buff = (unsigned char *)_TIFFmalloc(rowsize + 1)))\n                  {\n                  TIFFError("mirrorImage", "Unable to allocate mirror line buffer");\n                  return (-1);\n                  }\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                for (row = 0; row < length; row++)\n                  {\n\t\t  row_offset = row * rowsize;\n                  src = ibuff + row_offset;\n                  _TIFFmemset (line_buff, \'\\0\', rowsize);\n                  switch (shift_width)\n                    {\n                    case 1: if (reverseSamples8bits(spp, bps, width, src, line_buff))\n                              {\n\t\t              _TIFFfree(line_buff);\n                              return (-1);\n                              }\n                             _TIFFmemcpy (src, line_buff, rowsize);\n                             break;\n                    case 2: if (reverseSamples16bits(spp, bps, width, src, line_buff))\n                              {\n\t\t              _TIFFfree(line_buff);\n                              return (-1);\n                              }\n                             _TIFFmemcpy (src, line_buff, rowsize);\n                             break;\n                    case 3: if (reverseSamples24bits(spp, bps, width, src, line_buff))\n                              {\n\t\t              _TIFFfree(line_buff);\n                              return (-1);\n                              }\n                             _TIFFmemcpy (src, line_buff, rowsize);\n                             break;\n                    case 4:\n                    case 5: if (reverseSamples32bits(spp, bps, width, src, line_buff))\n                              {\n\t\t              _TIFFfree(line_buff);\n                              return (-1);\n                              }\n                             _TIFFmemcpy (src, line_buff, rowsize);\n                             break;\n                    default: TIFFError("mirrorImage","Unsupported bit depth %d", bps);\n\t\t             _TIFFfree(line_buff);\n                             return (-1);\n                    }\n\t\t  }\n                if (line_buff)\n                  _TIFFfree(line_buff);\n\t\t}\n             break;\n    default: TIFFError ("mirrorImage", "Invalid mirror axis %d", mirror);\n             return (-1);\n             break;\n    }\n  return (0);\n  }', 'void*\n_TIFFmalloc(tmsize_t s)\n{\n\treturn (malloc((size_t) s));\n}', 'static int\nreverseSamples24bits (uint16 spp, uint16 bps, uint32 width,\n                      uint8 *ibuff, uint8 *obuff)\n  {\n  int      ready_bits = 0;\n  uint32   col;\n  uint32   src_byte = 0, src_bit = 0;\n  uint32   bit_offset = 0;\n  uint32   matchbits = 0, maskbits = 0;\n  uint32   buff1 = 0, buff2 = 0;\n  uint8    bytebuff1 = 0, bytebuff2 = 0;\n  unsigned char *src;\n  unsigned char *dst;\n  unsigned char  swapbuff[4];\n  tsample_t sample;\n  if ((ibuff == NULL) || (obuff == NULL))\n    {\n    TIFFError("reverseSamples24bits","Invalid image or work buffer");\n    return (1);\n    }\n  ready_bits = 0;\n  maskbits =  (uint32)-1 >> (32 - bps);\n  dst = obuff;\n  for (col = width; col > 0; col--)\n    {\n    bit_offset = (col - 1) * bps * spp;\n    for (sample = 0; sample < spp; sample++)\n      {\n      if (sample == 0)\n        {\n        src_byte = bit_offset / 8;\n        src_bit  = bit_offset % 8;\n        }\n      else\n        {\n        src_byte = (bit_offset + (sample * bps)) / 8;\n        src_bit  = (bit_offset + (sample * bps)) % 8;\n        }\n      src = ibuff + src_byte;\n      matchbits = maskbits << (32 - src_bit - bps);\n      if (little_endian)\n        {\n        swapbuff[3] = *src;\n        swapbuff[2] = *(src + 1);\n        swapbuff[1] = *(src + 2);\n        swapbuff[0] = *(src + 3);\n        }\n      else\n        {\n        swapbuff[0] = *src;\n        swapbuff[1] = *(src + 1);\n        swapbuff[2] = *(src + 2);\n        swapbuff[3] = *(src + 3);\n\t}\n      buff1 = *((uint32 *)swapbuff);\n      buff1 = (buff1 & matchbits) << (src_bit);\n      if (ready_bits < 16)\n        {\n        bytebuff1 = bytebuff2 = 0;\n        buff2 = (buff2 | (buff1 >> ready_bits));\n        }\n      else\n        {\n        bytebuff1 = (buff2 >> 24);\n        *dst++ = bytebuff1;\n        bytebuff2 = (buff2 >> 16);\n        *dst++ = bytebuff2;\n        ready_bits -= 16;\n        buff2 = ((buff2 << 16) | (buff1 >> ready_bits));\n        }\n      ready_bits += bps;\n      }\n    }\n  while (ready_bits > 0)\n    {\n    bytebuff1 = (buff2 >> 24);\n    *dst++ = bytebuff1;\n    buff2 = (buff2 << 8);\n    bytebuff2 = bytebuff1;\n    ready_bits -= 8;\n    }\n  return (0);\n  }']