home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 February / PCWorld_2002-02_cd.bin / Software / Vyzkuste / pdflib / pdflib-4.0.1.sit / pdflib-4.0.1 / tiff / tif_getimage.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-07-04  |  60.0 KB  |  2,299 lines  |  [TEXT/CWIE]

  1. /*
  2.  * Copyright (c) 1991-1997 Sam Leffler
  3.  * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and 
  6.  * its documentation for any purpose is hereby granted without fee, provided
  7.  * that (i) the above copyright notices and this permission notice appear in
  8.  * all copies of the software and related documentation, and (ii) the names of
  9.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10.  * publicity relating to the software without the specific, prior written
  11.  * permission of Sam Leffler and Silicon Graphics.
  12.  * 
  13.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  14.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  15.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  16.  * 
  17.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  21.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  22.  * OF THIS SOFTWARE.
  23.  */
  24.  
  25. /*
  26.  * TIFF Library
  27.  *
  28.  * Read and return a packed RGBA image.
  29.  */
  30.  
  31. /* $Id: tif_getimage.c,v 1.6 2001/03/21 17:16:25 rjs Exp $ */
  32.  
  33. #include "tiffiop.h"
  34. #include <stdio.h>
  35.  
  36. static    int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
  37. static    int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
  38. static    int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
  39. static    int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
  40. static    int pickTileContigCase(TIFFRGBAImage*);
  41. static    int pickTileSeparateCase(TIFFRGBAImage*);
  42.  
  43. static    const char photoTag[] = "PhotometricInterpretation";
  44.  
  45. /*
  46.  * Check the image to see if TIFFReadRGBAImage can deal with it.
  47.  * 1/0 is returned according to whether or not the image can
  48.  * be handled.  If 0 is returned, emsg contains the reason
  49.  * why it is being rejected.
  50.  */
  51. int
  52. TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
  53. {
  54.     TIFFDirectory* td = &tif->tif_dir;
  55.     uint16 photometric;
  56.     int colorchannels;
  57.  
  58.     switch (td->td_bitspersample) {
  59.     case 1: case 2: case 4:
  60.     case 8: case 16:
  61.     break;
  62.     default:
  63.     sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
  64.         td->td_bitspersample);
  65.     return (0);
  66.     }
  67.     colorchannels = td->td_samplesperpixel - td->td_extrasamples;
  68.     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
  69.     switch (colorchannels) {
  70.     case 1:
  71.         photometric = PHOTOMETRIC_MINISBLACK;
  72.         break;
  73.     case 3:
  74.         photometric = PHOTOMETRIC_RGB;
  75.         break;
  76.     default:
  77.         sprintf(emsg, "Missing needed %s tag", photoTag);
  78.         return (0);
  79.     }
  80.     }
  81.     switch (photometric) {
  82.     case PHOTOMETRIC_MINISWHITE:
  83.     case PHOTOMETRIC_MINISBLACK:
  84.     case PHOTOMETRIC_PALETTE:
  85.     if (td->td_planarconfig == PLANARCONFIG_CONTIG 
  86.                 && td->td_samplesperpixel != 1) {
  87.         sprintf(emsg,
  88.         "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
  89.         photoTag, photometric,
  90.         "Samples/pixel", td->td_samplesperpixel);
  91.         return (0);
  92.     }
  93.     break;
  94.     case PHOTOMETRIC_YCBCR:
  95.     if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
  96.         sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
  97.         "Planarconfiguration", td->td_planarconfig);
  98.         return (0);
  99.     }
  100.     break;
  101.     case PHOTOMETRIC_RGB: 
  102.     if (colorchannels < 3) {
  103.         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  104.         "Color channels", colorchannels);
  105.         return (0);
  106.     }
  107.     break;
  108. #ifdef CMYK_SUPPORT
  109.     case PHOTOMETRIC_SEPARATED:
  110.     if (td->td_inkset != INKSET_CMYK) {
  111.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  112.         "InkSet", td->td_inkset);
  113.         return (0);
  114.     }
  115.     if (td->td_samplesperpixel != 4) {
  116.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  117.         "Samples/pixel", td->td_samplesperpixel);
  118.         return (0);
  119.     }
  120.     break;
  121. #endif
  122.     case PHOTOMETRIC_LOGL:
  123.     if (td->td_compression != COMPRESSION_SGILOG) {
  124.         sprintf(emsg, "Sorry, LogL data must have %s=%d",
  125.         "Compression", COMPRESSION_SGILOG);
  126.         return (0);
  127.     }
  128.     break;
  129.     case PHOTOMETRIC_LOGLUV:
  130.     if (td->td_compression != COMPRESSION_SGILOG &&
  131.         td->td_compression != COMPRESSION_SGILOG24) {
  132.         sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
  133.         "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
  134.         return (0);
  135.     }
  136.     if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
  137.         sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
  138.         "Planarconfiguration", td->td_planarconfig);
  139.         return (0);
  140.     }
  141.     break;
  142.     default:
  143.     sprintf(emsg, "Sorry, can not handle image with %s=%d",
  144.         photoTag, photometric);
  145.     return (0);
  146.     }
  147.     return (1);
  148. }
  149.  
  150. void
  151. TIFFRGBAImageEnd(TIFFRGBAImage* img)
  152. {
  153.     TIFF* tif = img->tif;
  154.  
  155.     if (img->Map)
  156.     _TIFFfree(tif, img->Map), img->Map = NULL;
  157.     if (img->BWmap)
  158.     _TIFFfree(tif, img->BWmap), img->BWmap = NULL;
  159.     if (img->PALmap)
  160.     _TIFFfree(tif, img->PALmap), img->PALmap = NULL;
  161.     if (img->ycbcr)
  162.     _TIFFfree(tif, img->ycbcr), img->ycbcr = NULL;
  163.  
  164.     if( img->redcmap ) {
  165.         _TIFFfree(tif, img->redcmap );
  166.         _TIFFfree(tif, img->greencmap );
  167.         _TIFFfree(tif, img->bluecmap );
  168.     }
  169. }
  170.  
  171. static int
  172. isCCITTCompression(TIFF* tif)
  173. {
  174.     uint16 compress;
  175.     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  176.     return (compress == COMPRESSION_CCITTFAX3 ||
  177.         compress == COMPRESSION_CCITTFAX4 ||
  178.         compress == COMPRESSION_CCITTRLE ||
  179.         compress == COMPRESSION_CCITTRLEW);
  180. }
  181.  
  182. int
  183. TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
  184. {
  185.     uint16* sampleinfo;
  186.     uint16 extrasamples;
  187.     uint16 planarconfig;
  188.     uint16 compress;
  189.     int colorchannels;
  190.     uint16    *red_orig, *green_orig, *blue_orig;
  191.     int        n_color;
  192.  
  193.     /* Initialize to normal values */
  194.     img->row_offset = 0;
  195.     img->col_offset = 0;
  196.     img->redcmap = NULL;
  197.     img->greencmap = NULL;
  198.     img->bluecmap = NULL;
  199.     
  200.     img->tif = tif;
  201.     img->stoponerr = stop;
  202.     TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
  203.     switch (img->bitspersample) {
  204.     case 1: case 2: case 4:
  205.     case 8: case 16:
  206.     break;
  207.     default:
  208.     sprintf(emsg, "Sorry, can not image with %d-bit samples",
  209.         img->bitspersample);
  210.     return (0);
  211.     }
  212.     img->alpha = 0;
  213.     TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
  214.     TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  215.     &extrasamples, &sampleinfo);
  216.     if (extrasamples == 1)
  217.     switch (sampleinfo[0]) {
  218.     case EXTRASAMPLE_ASSOCALPHA:    /* data is pre-multiplied */
  219.     case EXTRASAMPLE_UNASSALPHA:    /* data is not pre-multiplied */
  220.         img->alpha = sampleinfo[0];
  221.         break;
  222.     }
  223.     colorchannels = img->samplesperpixel - extrasamples;
  224.     TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
  225.     TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
  226.     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
  227.     switch (colorchannels) {
  228.     case 1:
  229.         if (isCCITTCompression(tif))
  230.         img->photometric = PHOTOMETRIC_MINISWHITE;
  231.         else
  232.         img->photometric = PHOTOMETRIC_MINISBLACK;
  233.         break;
  234.     case 3:
  235.         img->photometric = PHOTOMETRIC_RGB;
  236.         break;
  237.     default:
  238.         sprintf(emsg, "Missing needed %s tag", photoTag);
  239.         return (0);
  240.     }
  241.     }
  242.     switch (img->photometric) {
  243.     case PHOTOMETRIC_PALETTE:
  244.     if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
  245.         &red_orig, &green_orig, &blue_orig)) {
  246.         TIFFError(TIFFFileName(tif), "Missing required \"Colormap\" tag");
  247.         return (0);
  248.     }
  249.  
  250.         /* copy the colormaps so we can modify them */
  251.         n_color = (1L << img->bitspersample);
  252.         img->redcmap = (uint16 *) _TIFFmalloc(tif, sizeof(uint16)*n_color);
  253.         img->greencmap = (uint16 *) _TIFFmalloc(tif, sizeof(uint16)*n_color);
  254.         img->bluecmap = (uint16 *) _TIFFmalloc(tif, sizeof(uint16)*n_color);
  255.         if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
  256.         TIFFError(TIFFFileName(tif), "Out of memory for colormap copy");
  257.         return (0);
  258.         }
  259.  
  260.         memcpy( img->redcmap, red_orig, n_color * 2 );
  261.         memcpy( img->greencmap, green_orig, n_color * 2 );
  262.         memcpy( img->bluecmap, blue_orig, n_color * 2 );
  263.         
  264.     /* fall thru... */
  265.     case PHOTOMETRIC_MINISWHITE:
  266.     case PHOTOMETRIC_MINISBLACK:
  267.     if (planarconfig == PLANARCONFIG_CONTIG && img->samplesperpixel != 1) {
  268.         sprintf(emsg,
  269.         "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
  270.         photoTag, img->photometric,
  271.         "Samples/pixel", img->samplesperpixel);
  272.         return (0);
  273.     }
  274.     break;
  275.     case PHOTOMETRIC_YCBCR:
  276.     if (planarconfig != PLANARCONFIG_CONTIG) {
  277.         sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
  278.         "Planarconfiguration", planarconfig);
  279.         return (0);
  280.     }
  281.     /* It would probably be nice to have a reality check here. */
  282.     if (compress == COMPRESSION_JPEG 
  283.             && planarconfig == PLANARCONFIG_CONTIG) {
  284.         /* can rely on libjpeg to convert to RGB */
  285.         /* XXX should restore current state on exit */
  286.         TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
  287.         img->photometric = PHOTOMETRIC_RGB;
  288.     }
  289.     break;
  290.     case PHOTOMETRIC_RGB: 
  291.     if (colorchannels < 3) {
  292.         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  293.         "Color channels", colorchannels);
  294.         return (0);
  295.     }
  296.     break;
  297.     case PHOTOMETRIC_SEPARATED: {
  298.     uint16 inkset;
  299.     TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  300.     if (inkset != INKSET_CMYK) {
  301.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  302.         "InkSet", inkset);
  303.         return (0);
  304.     }
  305.     if (img->samplesperpixel != 4) {
  306.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  307.         "Samples/pixel", img->samplesperpixel);
  308.         return (0);
  309.     }
  310.     break;
  311.     }
  312.     case PHOTOMETRIC_LOGL:
  313.     if (compress != COMPRESSION_SGILOG) {
  314.         sprintf(emsg, "Sorry, LogL data must have %s=%d",
  315.         "Compression", COMPRESSION_SGILOG);
  316.         return (0);
  317.     }
  318.     TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  319.     img->photometric = PHOTOMETRIC_MINISBLACK;    /* little white lie */
  320.     img->bitspersample = 8;
  321.     break;
  322.     case PHOTOMETRIC_LOGLUV:
  323.     if (compress != COMPRESSION_SGILOG
  324.             && compress != COMPRESSION_SGILOG24) {
  325.         sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
  326.         "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
  327.         return (0);
  328.     }
  329.     if (planarconfig != PLANARCONFIG_CONTIG) {
  330.         sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
  331.         "Planarconfiguration", planarconfig);
  332.         return (0);
  333.     }
  334.     TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
  335.     img->photometric = PHOTOMETRIC_RGB;        /* little white lie */
  336.     img->bitspersample = 8;
  337.     break;
  338.     default:
  339.     sprintf(emsg, "Sorry, can not handle image with %s=%d",
  340.         photoTag, img->photometric);
  341.     return (0);
  342.     }
  343.     img->Map = NULL;
  344.     img->BWmap = NULL;
  345.     img->PALmap = NULL;
  346.     img->ycbcr = NULL;
  347.     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
  348.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
  349.     TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
  350.     img->isContig =
  351.     !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
  352.     if (img->isContig) {
  353.     img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
  354.     (void) pickTileContigCase(img);
  355.     } else {
  356.     img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
  357.     (void) pickTileSeparateCase(img);
  358.     }
  359.     return (1);
  360. }
  361.  
  362. int
  363. TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  364. {
  365.     if (img->get == NULL) {
  366.     TIFFError(TIFFFileName(img->tif), "No \"get\" routine setup");
  367.     return (0);
  368.     }
  369.     if (img->put.any == NULL) {
  370.     TIFFError(TIFFFileName(img->tif),
  371.         "No \"put\" routine setupl; probably can not handle image format");
  372.     return (0);
  373.     }
  374.     return (*img->get)(img, raster, w, h);
  375. }
  376.  
  377. /*
  378.  * Read the specified image into an ABGR-format raster.
  379.  */
  380. int
  381. TIFFReadRGBAImage(TIFF* tif,
  382.     uint32 rwidth, uint32 rheight, uint32* raster, int stop)
  383. {
  384.     char emsg[1024];
  385.     TIFFRGBAImage img;
  386.     int ok;
  387.  
  388.     if (TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
  389.     /* XXX verify rwidth and rheight against width and height */
  390.     ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
  391.         rwidth, img.height);
  392.     TIFFRGBAImageEnd(&img);
  393.     } else {
  394.     TIFFError(TIFFFileName(tif), emsg);
  395.     ok = 0;
  396.     }
  397.     return (ok);
  398. }
  399.  
  400. static uint32
  401. setorientation(TIFFRGBAImage* img, uint32 h)
  402. {
  403.     TIFF* tif = img->tif;
  404.     uint32 y;
  405.  
  406.     switch (img->orientation) {
  407.     case ORIENTATION_BOTRIGHT:
  408.     case ORIENTATION_RIGHTBOT:    /* XXX */
  409.     case ORIENTATION_LEFTBOT:    /* XXX */
  410.     TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
  411.     img->orientation = ORIENTATION_BOTLEFT;
  412.     /* fall thru... */
  413.     case ORIENTATION_BOTLEFT:
  414.     y = 0;
  415.     break;
  416.     case ORIENTATION_TOPRIGHT:
  417.     case ORIENTATION_RIGHTTOP:    /* XXX */
  418.     case ORIENTATION_LEFTTOP:    /* XXX */
  419.     default:
  420.     TIFFWarning(TIFFFileName(tif), "using top-left orientation");
  421.     img->orientation = ORIENTATION_TOPLEFT;
  422.     /* fall thru... */
  423.     case ORIENTATION_TOPLEFT:
  424.     y = h-1;
  425.     break;
  426.     }
  427.     return (y);
  428. }
  429.  
  430. /*
  431.  * Get an tile-organized image that has
  432.  *    PlanarConfiguration contiguous if SamplesPerPixel > 1
  433.  * or
  434.  *    SamplesPerPixel == 1
  435.  */    
  436. static int
  437. gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  438. {
  439.     TIFF* tif = img->tif;
  440.     tileContigRoutine put = img->put.contig;
  441.     uint16 orientation;
  442.     uint32 col, row, y;
  443.     uint32 tw, th;
  444.     u_char* buf;
  445.     int32 fromskew, toskew;
  446.     uint32 nrow;
  447.  
  448.     buf = (u_char*) _TIFFmalloc(tif, TIFFTileSize(tif));
  449.     if (buf == 0) {
  450.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  451.     return (0);
  452.     }
  453.     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  454.     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  455.     y = setorientation(img, h);
  456.     orientation = img->orientation;
  457.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? tw+w : tw-w);
  458.     for (row = 0; row < h; row += th) {
  459.     nrow = (row + th > h ? h - row : th);
  460.     for (col = 0; col < w; col += tw) {
  461.         if (TIFFReadTile(tif, buf, col+img->col_offset,
  462.                              row+img->row_offset, 0, 0) < 0 && img->stoponerr)
  463.         break;
  464.         if (col + tw > w) {
  465.         /*
  466.          * Tile is clipped horizontally.  Calculate
  467.          * visible portion and skewing factors.
  468.          */
  469.         uint32 npix = w - col;
  470.         fromskew = tw - npix;
  471.         (*put)(img, raster+y*w+col, col, y,
  472.             npix, nrow, fromskew, toskew + fromskew, buf);
  473.         } else {
  474.         (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf);
  475.         }
  476.     }
  477.     y += (orientation == ORIENTATION_TOPLEFT ?
  478.         -(int32) nrow : (int32) nrow);
  479.     }
  480.     _TIFFfree(tif, buf);
  481.     return (1);
  482. }
  483.  
  484. /*
  485.  * Get an tile-organized image that has
  486.  *     SamplesPerPixel > 1
  487.  *     PlanarConfiguration separated
  488.  * We assume that all such images are RGB.
  489.  */    
  490. static int
  491. gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  492. {
  493.     TIFF* tif = img->tif;
  494.     tileSeparateRoutine put = img->put.separate;
  495.     uint16 orientation;
  496.     uint32 col, row, y;
  497.     uint32 tw, th;
  498.     u_char* buf;
  499.     u_char* r;
  500.     u_char* g;
  501.     u_char* b;
  502.     u_char* a;
  503.     tsize_t tilesize;
  504.     int32 fromskew, toskew;
  505.     int alpha = img->alpha;
  506.     uint32 nrow;
  507.  
  508.     tilesize = TIFFTileSize(tif);
  509.     buf = (u_char*) _TIFFmalloc(tif, 4*tilesize);
  510.     if (buf == 0) {
  511.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  512.     return (0);
  513.     }
  514.     r = buf;
  515.     g = r + tilesize;
  516.     b = g + tilesize;
  517.     a = b + tilesize;
  518.     if (!alpha)
  519.     memset(a, 0xff, tilesize);
  520.     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  521.     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  522.     y = setorientation(img, h);
  523.     orientation = img->orientation;
  524.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? tw+w : tw-w);
  525.     for (row = 0; row < h; row += th) {
  526.     nrow = (row + th > h ? h - row : th);
  527.     for (col = 0; col < w; col += tw) {
  528.         if (TIFFReadTile(tif, r, col+img->col_offset,
  529.                              row+img->row_offset,0,0) < 0 && img->stoponerr)
  530.         break;
  531.         if (TIFFReadTile(tif, g, col+img->col_offset,
  532.                              row+img->row_offset,0,1) < 0 && img->stoponerr)
  533.         break;
  534.         if (TIFFReadTile(tif, b, col+img->col_offset,
  535.                              row+img->row_offset,0,2) < 0 && img->stoponerr)
  536.         break;
  537.         if (alpha && TIFFReadTile(tif,a,col+img->col_offset,
  538.                                row+img->row_offset,0,3) < 0 && img->stoponerr)
  539.         break;
  540.         if (col + tw > w) {
  541.         /*
  542.          * Tile is clipped horizontally.  Calculate
  543.          * visible portion and skewing factors.
  544.          */
  545.         uint32 npix = w - col;
  546.         fromskew = tw - npix;
  547.         (*put)(img, raster+y*w+col, col, y,
  548.             npix, nrow, fromskew, toskew + fromskew, r, g, b, a);
  549.         } else {
  550.         (*put)(img, raster+y*w+col, col, y,
  551.             tw, nrow, 0, toskew, r, g, b, a);
  552.         }
  553.     }
  554.     y += (orientation == ORIENTATION_TOPLEFT ?
  555.         -(int32) nrow : (int32) nrow);
  556.     }
  557.     _TIFFfree(tif, buf);
  558.     return (1);
  559. }
  560.  
  561. /*
  562.  * Get a strip-organized image that has
  563.  *    PlanarConfiguration contiguous if SamplesPerPixel > 1
  564.  * or
  565.  *    SamplesPerPixel == 1
  566.  */    
  567. static int
  568. gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  569. {
  570.     TIFF* tif = img->tif;
  571.     tileContigRoutine put = img->put.contig;
  572.     uint16 orientation;
  573.     uint32 row, y, nrow;
  574.     u_char* buf;
  575.     uint32 rowsperstrip;
  576.     uint32 imagewidth = img->width;
  577.     tsize_t scanline;
  578.     int32 fromskew, toskew;
  579.  
  580.     buf = (u_char*) _TIFFmalloc(tif, TIFFStripSize(tif));
  581.     if (buf == 0) {
  582.     TIFFError(TIFFFileName(tif), "No space for strip buffer");
  583.     return (0);
  584.     }
  585.     y = setorientation(img, h);
  586.     orientation = img->orientation;
  587.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);
  588.     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  589.     scanline = TIFFScanlineSize(tif);
  590.     fromskew = (w < imagewidth ? imagewidth - w : 0);
  591.     for (row = 0; row < h; row += rowsperstrip) {
  592.     nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  593.     if (TIFFReadEncodedStrip(tif,
  594.                                  TIFFComputeStrip(tif,row+img->row_offset, 0),
  595.                                  buf, nrow*scanline) < 0
  596.             && img->stoponerr)
  597.         break;
  598.     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf);
  599.     y += (orientation == ORIENTATION_TOPLEFT ?
  600.         -(int32) nrow : (int32) nrow);
  601.     }
  602.     _TIFFfree(tif, buf);
  603.     return (1);
  604. }
  605.  
  606. /*
  607.  * Get a strip-organized image with
  608.  *     SamplesPerPixel > 1
  609.  *     PlanarConfiguration separated
  610.  * We assume that all such images are RGB.
  611.  */
  612. static int
  613. gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  614. {
  615.     TIFF* tif = img->tif;
  616.     tileSeparateRoutine put = img->put.separate;
  617.     uint16 orientation;
  618.     u_char *buf;
  619.     u_char *r, *g, *b, *a;
  620.     uint32 row, y, nrow;
  621.     tsize_t scanline;
  622.     uint32 rowsperstrip, offset_row;
  623.     uint32 imagewidth = img->width;
  624.     tsize_t stripsize;
  625.     int32 fromskew, toskew;
  626.     int alpha = img->alpha;
  627.  
  628.     stripsize = TIFFStripSize(tif);
  629.     r = buf = (u_char *)_TIFFmalloc(tif, 4*stripsize);
  630.     if (buf == 0) {
  631.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  632.     return (0);
  633.     }
  634.     g = r + stripsize;
  635.     b = g + stripsize;
  636.     a = b + stripsize;
  637.     if (!alpha)
  638.     memset(a, 0xff, stripsize);
  639.     y = setorientation(img, h);
  640.     orientation = img->orientation;
  641.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);
  642.     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  643.     scanline = TIFFScanlineSize(tif);
  644.     fromskew = (w < imagewidth ? imagewidth - w : 0);
  645.     for (row = 0; row < h; row += rowsperstrip) {
  646.     nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  647.         offset_row = row + img->row_offset;
  648.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
  649.         r, nrow*scanline) < 0 && img->stoponerr)
  650.         break;
  651.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
  652.         g, nrow*scanline) < 0 && img->stoponerr)
  653.         break;
  654.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
  655.         b, nrow*scanline) < 0 && img->stoponerr)
  656.         break;
  657.     if (alpha &&
  658.         (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),
  659.         a, nrow*scanline) < 0 && img->stoponerr))
  660.         break;
  661.     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r, g, b, a);
  662.     y += (orientation == ORIENTATION_TOPLEFT ?
  663.         -(int32) nrow : (int32) nrow);
  664.     }
  665.     _TIFFfree(tif, buf);
  666.     return (1);
  667. }
  668.  
  669. /*
  670.  * The following routines move decoded data returned
  671.  * from the TIFF library into rasters filled with packed
  672.  * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  673.  *
  674.  * The routines have been created according to the most
  675.  * important cases and optimized.  pickTileContigCase and
  676.  * pickTileSeparateCase analyze the parameters and select
  677.  * the appropriate "put" routine to use.
  678.  */
  679. #define    REPEAT8(op)    REPEAT4(op); REPEAT4(op)
  680. #define    REPEAT4(op)    REPEAT2(op); REPEAT2(op)
  681. #define    REPEAT2(op)    op; op
  682. #define    CASE8(x,op)            \
  683.     switch (x) {            \
  684.     case 7: op; case 6: op; case 5: op;    \
  685.     case 4: op; case 3: op; case 2: op;    \
  686.     case 1: op;                \
  687.     }
  688. #define    CASE4(x,op)    switch (x) { case 3: op; case 2: op; case 1: op; }
  689. #define    NOP
  690.  
  691. #define    UNROLL8(w, op1, op2) {        \
  692.     uint32 _x;                \
  693.     for (_x = w; _x >= 8; _x -= 8) {    \
  694.     op1;                \
  695.     REPEAT8(op2);            \
  696.     }                    \
  697.     if (_x > 0) {            \
  698.     op1;                \
  699.     CASE8(_x,op2);            \
  700.     }                    \
  701. }
  702. #define    UNROLL4(w, op1, op2) {        \
  703.     uint32 _x;                \
  704.     for (_x = w; _x >= 4; _x -= 4) {    \
  705.     op1;                \
  706.     REPEAT4(op2);            \
  707.     }                    \
  708.     if (_x > 0) {            \
  709.     op1;                \
  710.     CASE4(_x,op2);            \
  711.     }                    \
  712. }
  713. #define    UNROLL2(w, op1, op2) {        \
  714.     uint32 _x;                \
  715.     for (_x = w; _x >= 2; _x -= 2) {    \
  716.     op1;                \
  717.     REPEAT2(op2);            \
  718.     }                    \
  719.     if (_x) {                \
  720.     op1;                \
  721.     op2;                \
  722.     }                    \
  723. }
  724.     
  725. #define    SKEW(r,g,b,skew)    { r += skew; g += skew; b += skew; }
  726. #define    SKEW4(r,g,b,a,skew)    { r += skew; g += skew; b += skew; a+= skew; }
  727.  
  728. #define A1 ((uint32)(0xffL<<24))
  729. #define    PACK(r,g,b)    \
  730.     ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
  731. #define    PACK4(r,g,b,a)    \
  732.     ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
  733. #define W2B(v) (((v)>>8)&0xff)
  734. #define    PACKW(r,g,b)    \
  735.     ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
  736. #define    PACKW4(r,g,b,a)    \
  737. ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
  738.  
  739. #define    DECLAREContigPutFunc(name) \
  740. static void name(\
  741.     TIFFRGBAImage* img, \
  742.     uint32* cp, \
  743.     uint32 x, uint32 y, \
  744.     uint32 w, uint32 h, \
  745.     int32 fromskew, int32 toskew, \
  746.     u_char* pp \
  747. )
  748.  
  749. /*
  750.  * 8-bit palette => colormap/RGB
  751.  */
  752. DECLAREContigPutFunc(put8bitcmaptile)
  753. {
  754.     uint32** PALmap = img->PALmap;
  755.  
  756.     (void) x; (void) y;
  757.     while (h-- > 0) {
  758.     UNROLL8(w, NOP, *cp++ = PALmap[*pp++][0]);
  759.     cp += toskew;
  760.     pp += fromskew;
  761.     }
  762. }
  763.  
  764. /*
  765.  * 4-bit palette => colormap/RGB
  766.  */
  767. DECLAREContigPutFunc(put4bitcmaptile)
  768. {
  769.     uint32** PALmap = img->PALmap;
  770.  
  771.     (void) x; (void) y;
  772.     fromskew /= 2;
  773.     while (h-- > 0) {
  774.     uint32* bw;
  775.     UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
  776.     cp += toskew;
  777.     pp += fromskew;
  778.     }
  779. }
  780.  
  781. /*
  782.  * 2-bit palette => colormap/RGB
  783.  */
  784. DECLAREContigPutFunc(put2bitcmaptile)
  785. {
  786.     uint32** PALmap = img->PALmap;
  787.  
  788.     (void) x; (void) y;
  789.     fromskew /= 4;
  790.     while (h-- > 0) {
  791.     uint32* bw;
  792.     UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
  793.     cp += toskew;
  794.     pp += fromskew;
  795.     }
  796. }
  797.  
  798. /*
  799.  * 1-bit palette => colormap/RGB
  800.  */
  801. DECLAREContigPutFunc(put1bitcmaptile)
  802. {
  803.     uint32** PALmap = img->PALmap;
  804.  
  805.     (void) x; (void) y;
  806.     fromskew /= 8;
  807.     while (h-- > 0) {
  808.     uint32* bw;
  809.     UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
  810.     cp += toskew;
  811.     pp += fromskew;
  812.     }
  813. }
  814.  
  815. /*
  816.  * 8-bit greyscale => colormap/RGB
  817.  */
  818. DECLAREContigPutFunc(putgreytile)
  819. {
  820.     uint32** BWmap = img->BWmap;
  821.  
  822.     (void) y;
  823.     while (h-- > 0) {
  824.     for (x = w; x-- > 0;)
  825.         *cp++ = BWmap[*pp++][0];
  826.     cp += toskew;
  827.     pp += fromskew;
  828.     }
  829. }
  830.  
  831. /*
  832.  * 1-bit bilevel => colormap/RGB
  833.  */
  834. DECLAREContigPutFunc(put1bitbwtile)
  835. {
  836.     uint32** BWmap = img->BWmap;
  837.  
  838.     (void) x; (void) y;
  839.     fromskew /= 8;
  840.     while (h-- > 0) {
  841.     uint32* bw;
  842.     UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
  843.     cp += toskew;
  844.     pp += fromskew;
  845.     }
  846. }
  847.  
  848. /*
  849.  * 2-bit greyscale => colormap/RGB
  850.  */
  851. DECLAREContigPutFunc(put2bitbwtile)
  852. {
  853.     uint32** BWmap = img->BWmap;
  854.  
  855.     (void) x; (void) y;
  856.     fromskew /= 4;
  857.     while (h-- > 0) {
  858.     uint32* bw;
  859.     UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
  860.     cp += toskew;
  861.     pp += fromskew;
  862.     }
  863. }
  864.  
  865. /*
  866.  * 4-bit greyscale => colormap/RGB
  867.  */
  868. DECLAREContigPutFunc(put4bitbwtile)
  869. {
  870.     uint32** BWmap = img->BWmap;
  871.  
  872.     (void) x; (void) y;
  873.     fromskew /= 2;
  874.     while (h-- > 0) {
  875.     uint32* bw;
  876.     UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
  877.     cp += toskew;
  878.     pp += fromskew;
  879.     }
  880. }
  881.  
  882. /*
  883.  * 8-bit packed samples, no Map => RGB
  884.  */
  885. DECLAREContigPutFunc(putRGBcontig8bittile)
  886. {
  887.     int samplesperpixel = img->samplesperpixel;
  888.  
  889.     (void) x; (void) y;
  890.     fromskew *= samplesperpixel;
  891.     while (h-- > 0) {
  892.     UNROLL8(w, NOP,
  893.         *cp++ = PACK(pp[0], pp[1], pp[2]);
  894.         pp += samplesperpixel);
  895.     cp += toskew;
  896.     pp += fromskew;
  897.     }
  898. }
  899.  
  900. /*
  901.  * 8-bit packed samples, w/ Map => RGB
  902.  */
  903. DECLAREContigPutFunc(putRGBcontig8bitMaptile)
  904. {
  905.     TIFFRGBValue* Map = img->Map;
  906.     int samplesperpixel = img->samplesperpixel;
  907.  
  908.     (void) y;
  909.     fromskew *= samplesperpixel;
  910.     while (h-- > 0) {
  911.     for (x = w; x-- > 0;) {
  912.         *cp++ = PACK(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
  913.         pp += samplesperpixel;
  914.     }
  915.     pp += fromskew;
  916.     cp += toskew;
  917.     }
  918. }
  919.  
  920. /*
  921.  * 8-bit packed samples => RGBA w/ associated alpha
  922.  * (known to have Map == NULL)
  923.  */
  924. DECLAREContigPutFunc(putRGBAAcontig8bittile)
  925. {
  926.     int samplesperpixel = img->samplesperpixel;
  927.  
  928.     (void) x; (void) y;
  929.     fromskew *= samplesperpixel;
  930.     while (h-- > 0) {
  931.     UNROLL8(w, NOP,
  932.         *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
  933.         pp += samplesperpixel);
  934.     cp += toskew;
  935.     pp += fromskew;
  936.     }
  937. }
  938.  
  939. /*
  940.  * 8-bit packed samples => RGBA w/ unassociated alpha
  941.  * (known to have Map == NULL)
  942.  */
  943. DECLAREContigPutFunc(putRGBUAcontig8bittile)
  944. {
  945.     int samplesperpixel = img->samplesperpixel;
  946.  
  947.     (void) y;
  948.     fromskew *= samplesperpixel;
  949.     while (h-- > 0) {
  950.     uint32 r, g, b, a;
  951.     for (x = w; x-- > 0;) {
  952.         a = pp[3];
  953.         r = (pp[0] * a) / 255;
  954.         g = (pp[1] * a) / 255;
  955.         b = (pp[2] * a) / 255;
  956.         *cp++ = PACK4(r,g,b,a);
  957.         pp += samplesperpixel;
  958.     }
  959.     cp += toskew;
  960.     pp += fromskew;
  961.     }
  962. }
  963.  
  964. /*
  965.  * 16-bit packed samples => RGB
  966.  */
  967. DECLAREContigPutFunc(putRGBcontig16bittile)
  968. {
  969.     int samplesperpixel = img->samplesperpixel;
  970.     uint16 *wp = (uint16 *)pp;
  971.  
  972.     (void) y;
  973.     fromskew *= samplesperpixel;
  974.     while (h-- > 0) {
  975.     for (x = w; x-- > 0;) {
  976.         *cp++ = PACKW(wp[0], wp[1], wp[2]);
  977.         wp += samplesperpixel;
  978.     }
  979.     cp += toskew;
  980.     wp += fromskew;
  981.     }
  982. }
  983.  
  984. /*
  985.  * 16-bit packed samples => RGBA w/ associated alpha
  986.  * (known to have Map == NULL)
  987.  */
  988. DECLAREContigPutFunc(putRGBAAcontig16bittile)
  989. {
  990.     int samplesperpixel = img->samplesperpixel;
  991.     uint16 *wp = (uint16 *)pp;
  992.  
  993.     (void) y;
  994.     fromskew *= samplesperpixel;
  995.     while (h-- > 0) {
  996.     for (x = w; x-- > 0;) {
  997.         *cp++ = PACKW4(wp[0], wp[1], wp[2], wp[3]);
  998.         wp += samplesperpixel;
  999.     }
  1000.     cp += toskew;
  1001.     wp += fromskew;
  1002.     }
  1003. }
  1004.  
  1005. /*
  1006.  * 16-bit packed samples => RGBA w/ unassociated alpha
  1007.  * (known to have Map == NULL)
  1008.  */
  1009. DECLAREContigPutFunc(putRGBUAcontig16bittile)
  1010. {
  1011.     int samplesperpixel = img->samplesperpixel;
  1012.     uint16 *wp = (uint16 *)pp;
  1013.  
  1014.     (void) y;
  1015.     fromskew *= samplesperpixel;
  1016.     while (h-- > 0) {
  1017.     uint32 r,g,b,a;
  1018.     /*
  1019.      * We shift alpha down four bits just in case unsigned
  1020.      * arithmetic doesn't handle the full range.
  1021.      * We still have plenty of accuracy, since the output is 8 bits.
  1022.      * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
  1023.      * Since we want r*a * 0xff for eight bit output,
  1024.      * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
  1025.      */
  1026.     for (x = w; x-- > 0;) {
  1027.         a = wp[3] >> 4; 
  1028.         r = (wp[0] * a) / 0x10eff;
  1029.         g = (wp[1] * a) / 0x10eff;
  1030.         b = (wp[2] * a) / 0x10eff;
  1031.         *cp++ = PACK4(r,g,b,a);
  1032.         wp += samplesperpixel;
  1033.     }
  1034.     cp += toskew;
  1035.     wp += fromskew;
  1036.     }
  1037. }
  1038.  
  1039. /*
  1040.  * 8-bit packed CMYK samples w/o Map => RGB
  1041.  *
  1042.  * NB: The conversion of CMYK->RGB is *very* crude.
  1043.  */
  1044. DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
  1045. {
  1046.     int samplesperpixel = img->samplesperpixel;
  1047.     uint16 r, g, b, k;
  1048.  
  1049.     (void) x; (void) y;
  1050.     fromskew *= samplesperpixel;
  1051.     while (h-- > 0) {
  1052.     UNROLL8(w, NOP,
  1053.         k = 255 - pp[3];
  1054.         r = (k*(255-pp[0]))/255;
  1055.         g = (k*(255-pp[1]))/255;
  1056.         b = (k*(255-pp[2]))/255;
  1057.         *cp++ = PACK(r, g, b);
  1058.         pp += samplesperpixel);
  1059.     cp += toskew;
  1060.     pp += fromskew;
  1061.     }
  1062. }
  1063.  
  1064. /*
  1065.  * 8-bit packed CMYK samples w/Map => RGB
  1066.  *
  1067.  * NB: The conversion of CMYK->RGB is *very* crude.
  1068.  */
  1069. DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
  1070. {
  1071.     int samplesperpixel = img->samplesperpixel;
  1072.     TIFFRGBValue* Map = img->Map;
  1073.     uint16 r, g, b, k;
  1074.  
  1075.     (void) y;
  1076.     fromskew *= samplesperpixel;
  1077.     while (h-- > 0) {
  1078.     for (x = w; x-- > 0;) {
  1079.         k = 255 - pp[3];
  1080.         r = (k*(255-pp[0]))/255;
  1081.         g = (k*(255-pp[1]))/255;
  1082.         b = (k*(255-pp[2]))/255;
  1083.         *cp++ = PACK(Map[r], Map[g], Map[b]);
  1084.         pp += samplesperpixel;
  1085.     }
  1086.     pp += fromskew;
  1087.     cp += toskew;
  1088.     }
  1089. }
  1090.  
  1091. #define    DECLARESepPutFunc(name) \
  1092. static void name(\
  1093.     TIFFRGBAImage* img,\
  1094.     uint32* cp,\
  1095.     uint32 x, uint32 y, \
  1096.     uint32 w, uint32 h,\
  1097.     int32 fromskew, int32 toskew,\
  1098.     u_char* r, u_char* g, u_char* b, u_char* a\
  1099. )
  1100.  
  1101. /*
  1102.  * 8-bit unpacked samples => RGB
  1103.  */
  1104. DECLARESepPutFunc(putRGBseparate8bittile)
  1105. {
  1106.     (void) img; (void) x; (void) y; (void) a;
  1107.     while (h-- > 0) {
  1108.     UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
  1109.     SKEW(r, g, b, fromskew);
  1110.     cp += toskew;
  1111.     }
  1112. }
  1113.  
  1114. /*
  1115.  * 8-bit unpacked samples => RGB
  1116.  */
  1117. DECLARESepPutFunc(putRGBseparate8bitMaptile)
  1118. {
  1119.     TIFFRGBValue* Map = img->Map;
  1120.  
  1121.     (void) y; (void) a;
  1122.     while (h-- > 0) {
  1123.     for (x = w; x > 0; x--)
  1124.         *cp++ = PACK(Map[*r++], Map[*g++], Map[*b++]);
  1125.     SKEW(r, g, b, fromskew);
  1126.     cp += toskew;
  1127.     }
  1128. }
  1129.  
  1130. /*
  1131.  * 8-bit unpacked samples => RGBA w/ associated alpha
  1132.  */
  1133. DECLARESepPutFunc(putRGBAAseparate8bittile)
  1134. {
  1135.     (void) img; (void) x; (void) y;
  1136.     while (h-- > 0) {
  1137.     UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
  1138.     SKEW4(r, g, b, a, fromskew);
  1139.     cp += toskew;
  1140.     }
  1141. }
  1142.  
  1143. /*
  1144.  * 8-bit unpacked samples => RGBA w/ unassociated alpha
  1145.  */
  1146. DECLARESepPutFunc(putRGBUAseparate8bittile)
  1147. {
  1148.     (void) img; (void) y;
  1149.     while (h-- > 0) {
  1150.     uint32 rv, gv, bv, av;
  1151.     for (x = w; x-- > 0;) {
  1152.         av = *a++;
  1153.         rv = (*r++ * av) / 255;
  1154.         gv = (*g++ * av) / 255;
  1155.         bv = (*b++ * av) / 255;
  1156.         *cp++ = PACK4(rv,gv,bv,av);
  1157.     }
  1158.     SKEW4(r, g, b, a, fromskew);
  1159.     cp += toskew;
  1160.     }
  1161. }
  1162.  
  1163. /*
  1164.  * 16-bit unpacked samples => RGB
  1165.  */
  1166. DECLARESepPutFunc(putRGBseparate16bittile)
  1167. {
  1168.     uint16 *wr = (uint16*) r;
  1169.     uint16 *wg = (uint16*) g;
  1170.     uint16 *wb = (uint16*) b;
  1171.  
  1172.     (void) img; (void) y; (void) a;
  1173.     while (h-- > 0) {
  1174.     for (x = 0; x < w; x++)
  1175.         *cp++ = PACKW(*wr++, *wg++, *wb++);
  1176.     SKEW(wr, wg, wb, fromskew);
  1177.     cp += toskew;
  1178.     }
  1179. }
  1180.  
  1181. /*
  1182.  * 16-bit unpacked samples => RGBA w/ associated alpha
  1183.  */
  1184. DECLARESepPutFunc(putRGBAAseparate16bittile)
  1185. {
  1186.     uint16 *wr = (uint16*) r;
  1187.     uint16 *wg = (uint16*) g;
  1188.     uint16 *wb = (uint16*) b;
  1189.     uint16 *wa = (uint16*) a;
  1190.  
  1191.     (void) img; (void) y;
  1192.     while (h-- > 0) {
  1193.     for (x = 0; x < w; x++)
  1194.         *cp++ = PACKW4(*wr++, *wg++, *wb++, *wa++);
  1195.     SKEW4(wr, wg, wb, wa, fromskew);
  1196.     cp += toskew;
  1197.     }
  1198. }
  1199.  
  1200. /*
  1201.  * 16-bit unpacked samples => RGBA w/ unassociated alpha
  1202.  */
  1203. DECLARESepPutFunc(putRGBUAseparate16bittile)
  1204. {
  1205.     uint16 *wr = (uint16*) r;
  1206.     uint16 *wg = (uint16*) g;
  1207.     uint16 *wb = (uint16*) b;
  1208.     uint16 *wa = (uint16*) a;
  1209.  
  1210.     (void) img; (void) y;
  1211.     while (h-- > 0) {
  1212.     uint32 r,g,b,a;
  1213.     /*
  1214.      * We shift alpha down four bits just in case unsigned
  1215.      * arithmetic doesn't handle the full range.
  1216.      * We still have plenty of accuracy, since the output is 8 bits.
  1217.      * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
  1218.      * Since we want r*a * 0xff for eight bit output,
  1219.      * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
  1220.      */
  1221.     for (x = w; x-- > 0;) {
  1222.         a = *wa++ >> 4; 
  1223.         r = (*wr++ * a) / 0x10eff;
  1224.         g = (*wg++ * a) / 0x10eff;
  1225.         b = (*wb++ * a) / 0x10eff;
  1226.         *cp++ = PACK4(r,g,b,a);
  1227.     }
  1228.     SKEW4(wr, wg, wb, wa, fromskew);
  1229.     cp += toskew;
  1230.     }
  1231. }
  1232.  
  1233. /*
  1234.  * YCbCr -> RGB conversion and packing routines.  The colorspace
  1235.  * conversion algorithm comes from the IJG v5a code; see below
  1236.  * for more information on how it works.
  1237.  */
  1238.  
  1239. #define    YCbCrtoRGB(dst, yc) {                        \
  1240.     int Y = (yc);                            \
  1241.     dst = PACK(                                \
  1242.     clamptab[Y+Crrtab[Cr]],                        \
  1243.     clamptab[Y + (int)((Cbgtab[Cb]+Crgtab[Cr])>>16)],        \
  1244.     clamptab[Y+Cbbtab[Cb]]);                    \
  1245. }
  1246. #define    YCbCrSetup                            \
  1247.     TIFFYCbCrToRGB* ycbcr = img->ycbcr;                    \
  1248.     int* Crrtab = ycbcr->Cr_r_tab;                    \
  1249.     int* Cbbtab = ycbcr->Cb_b_tab;                    \
  1250.     int32* Crgtab = ycbcr->Cr_g_tab;                    \
  1251.     int32* Cbgtab = ycbcr->Cb_g_tab;                    \
  1252.     TIFFRGBValue* clamptab = ycbcr->clamptab
  1253.  
  1254. /*
  1255.  * 8-bit packed YCbCr samples => RGB 
  1256.  * This function is generic for different sampling sizes, 
  1257.  * and can handle blocks sizes that aren't multiples of the
  1258.  * sampling size.  However, it is substantially less optimized
  1259.  * than the specific sampling cases.  It is used as a fallback
  1260.  * for difficult blocks.
  1261.  */
  1262. #ifdef notdef
  1263. static void putcontig8bitYCbCrGenericTile( 
  1264.     TIFFRGBAImage* img, 
  1265.     uint32* cp, 
  1266.     uint32 x, uint32 y, 
  1267.     uint32 w, uint32 h, 
  1268.     int32 fromskew, int32 toskew, 
  1269.     u_char* pp,
  1270.     int h_group, 
  1271.     int v_group )
  1272.  
  1273. {
  1274.     YCbCrSetup;
  1275.     
  1276.     uint32* cp1 = cp+w+toskew;
  1277.     uint32* cp2 = cp1+w+toskew;
  1278.     uint32* cp3 = cp2+w+toskew;
  1279.     int32 incr = 3*w+4*toskew;
  1280.     int     Cb, Cr;
  1281.     int     group_size = v_group * h_group + 2;
  1282.  
  1283.     (void) y;
  1284.     fromskew = (fromskew * group_size) / h_group;
  1285.  
  1286.     for( yy = 0; yy < h; yy++ )
  1287.     {
  1288.         u_char *pp_line;
  1289.         int     y_line_group = yy / v_group;
  1290.         int     y_remainder = yy - y_line_group * v_group;
  1291.  
  1292.         pp_line = pp + v_line_group * 
  1293.  
  1294.         
  1295.         for( xx = 0; xx < w; xx++ )
  1296.         {
  1297.             Cb = pp
  1298.         }
  1299.     }
  1300.     for (; h >= 4; h -= 4) {
  1301.     x = w>>2;
  1302.     do {
  1303.         Cb = pp[16];
  1304.         Cr = pp[17];
  1305.  
  1306.         YCbCrtoRGB(cp [0], pp[ 0]);
  1307.         YCbCrtoRGB(cp [1], pp[ 1]);
  1308.         YCbCrtoRGB(cp [2], pp[ 2]);
  1309.         YCbCrtoRGB(cp [3], pp[ 3]);
  1310.         YCbCrtoRGB(cp1[0], pp[ 4]);
  1311.         YCbCrtoRGB(cp1[1], pp[ 5]);
  1312.         YCbCrtoRGB(cp1[2], pp[ 6]);
  1313.         YCbCrtoRGB(cp1[3], pp[ 7]);
  1314.         YCbCrtoRGB(cp2[0], pp[ 8]);
  1315.         YCbCrtoRGB(cp2[1], pp[ 9]);
  1316.         YCbCrtoRGB(cp2[2], pp[10]);
  1317.         YCbCrtoRGB(cp2[3], pp[11]);
  1318.         YCbCrtoRGB(cp3[0], pp[12]);
  1319.         YCbCrtoRGB(cp3[1], pp[13]);
  1320.         YCbCrtoRGB(cp3[2], pp[14]);
  1321.         YCbCrtoRGB(cp3[3], pp[15]);
  1322.  
  1323.         cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
  1324.         pp += 18;
  1325.     } while (--x);
  1326.     cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1327.     pp += fromskew;
  1328.     }
  1329. }
  1330. #endif
  1331.  
  1332. /*
  1333.  * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
  1334.  */
  1335. DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
  1336. {
  1337.     YCbCrSetup;
  1338.     uint32* cp1 = cp+w+toskew;
  1339.     uint32* cp2 = cp1+w+toskew;
  1340.     uint32* cp3 = cp2+w+toskew;
  1341.     int32 incr = 3*w+4*toskew;
  1342.  
  1343.     (void) y;
  1344.     /* adjust fromskew */
  1345.     fromskew = (fromskew * 18) / 4;
  1346.     if ((h & 3) == 0 && (w & 3) == 0) {                        
  1347.         for (; h >= 4; h -= 4) {
  1348.             x = w>>2;
  1349.             do {
  1350.                 int Cb = pp[16];
  1351.                 int Cr = pp[17];
  1352.  
  1353.                 YCbCrtoRGB(cp [0], pp[ 0]);
  1354.                 YCbCrtoRGB(cp [1], pp[ 1]);
  1355.                 YCbCrtoRGB(cp [2], pp[ 2]);
  1356.                 YCbCrtoRGB(cp [3], pp[ 3]);
  1357.                 YCbCrtoRGB(cp1[0], pp[ 4]);
  1358.                 YCbCrtoRGB(cp1[1], pp[ 5]);
  1359.                 YCbCrtoRGB(cp1[2], pp[ 6]);
  1360.                 YCbCrtoRGB(cp1[3], pp[ 7]);
  1361.                 YCbCrtoRGB(cp2[0], pp[ 8]);
  1362.                 YCbCrtoRGB(cp2[1], pp[ 9]);
  1363.                 YCbCrtoRGB(cp2[2], pp[10]);
  1364.                 YCbCrtoRGB(cp2[3], pp[11]);
  1365.                 YCbCrtoRGB(cp3[0], pp[12]);
  1366.                 YCbCrtoRGB(cp3[1], pp[13]);
  1367.                 YCbCrtoRGB(cp3[2], pp[14]);
  1368.                 YCbCrtoRGB(cp3[3], pp[15]);
  1369.  
  1370.                 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
  1371.                 pp += 18;
  1372.             } while (--x);
  1373.             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1374.             pp += fromskew;
  1375.         }
  1376.     } else {
  1377.         while (h > 0) {
  1378.             for (x = w; x > 0;) {
  1379.                 int Cb = pp[16];
  1380.                 int Cr = pp[17];
  1381.                 switch (x) {
  1382.                 default:
  1383.                     switch (h) {
  1384.                     default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
  1385.                     case 3:  YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
  1386.                     case 2:  YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
  1387.                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
  1388.                     }                                    /* FALLTHROUGH */
  1389.                 case 3:
  1390.                     switch (h) {
  1391.                     default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
  1392.                     case 3:  YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
  1393.                     case 2:  YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
  1394.                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
  1395.                     }                                    /* FALLTHROUGH */
  1396.                 case 2:
  1397.                     switch (h) {
  1398.                     default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
  1399.                     case 3:  YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
  1400.                     case 2:  YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
  1401.                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
  1402.                     }                                    /* FALLTHROUGH */
  1403.                 case 1:
  1404.                     switch (h) {
  1405.                     default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
  1406.                     case 3:  YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
  1407.                     case 2:  YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
  1408.                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
  1409.                     }                                    /* FALLTHROUGH */
  1410.                 }
  1411.                 if (x < 4) {
  1412.                     cp += x; cp1 += x; cp2 += x; cp3 += x;
  1413.                     x = 0;
  1414.                 }
  1415.                 else {
  1416.                     cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
  1417.                     x -= 4;
  1418.                 }
  1419.                 pp += 18;
  1420.             }
  1421.             if (h <= 4)
  1422.                 break;
  1423.             h -= 4;
  1424.             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1425.             pp += fromskew;
  1426.         }
  1427.     }
  1428. }
  1429.  
  1430. /*
  1431.  * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
  1432.  */
  1433. DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
  1434. {
  1435.     YCbCrSetup;
  1436.     uint32* cp1 = cp+w+toskew;
  1437.     int32 incr = 2*toskew+w;
  1438.  
  1439.     (void) y;
  1440.     fromskew = (fromskew * 10) / 4;
  1441.     if ((h & 3) == 0 && (w & 1) == 0) {
  1442.         for (; h >= 2; h -= 2) {
  1443.             x = w>>2;
  1444.             do {
  1445.                 int Cb = pp[8];
  1446.                 int Cr = pp[9];
  1447.                 
  1448.                 YCbCrtoRGB(cp [0], pp[0]);
  1449.                 YCbCrtoRGB(cp [1], pp[1]);
  1450.                 YCbCrtoRGB(cp [2], pp[2]);
  1451.                 YCbCrtoRGB(cp [3], pp[3]);
  1452.                 YCbCrtoRGB(cp1[0], pp[4]);
  1453.                 YCbCrtoRGB(cp1[1], pp[5]);
  1454.                 YCbCrtoRGB(cp1[2], pp[6]);
  1455.                 YCbCrtoRGB(cp1[3], pp[7]);
  1456.                 
  1457.                 cp += 4, cp1 += 4;
  1458.                 pp += 10;
  1459.             } while (--x);
  1460.             cp += incr, cp1 += incr;
  1461.             pp += fromskew;
  1462.         }
  1463.     } else {
  1464.         while (h > 0) {
  1465.             for (x = w; x > 0;) {
  1466.                 int Cb = pp[8];
  1467.                 int Cr = pp[9];
  1468.                 switch (x) {
  1469.                 default:
  1470.                     switch (h) {
  1471.                     default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
  1472.                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
  1473.                     }                                    /* FALLTHROUGH */
  1474.                 case 3:
  1475.                     switch (h) {
  1476.                     default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
  1477.                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
  1478.                     }                                    /* FALLTHROUGH */
  1479.                 case 2:
  1480.                     switch (h) {
  1481.                     default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
  1482.                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
  1483.                     }                                    /* FALLTHROUGH */
  1484.                 case 1:
  1485.                     switch (h) {
  1486.                     default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
  1487.                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
  1488.                     }                                    /* FALLTHROUGH */
  1489.                 }
  1490.                 if (x < 4) {
  1491.                     cp += x; cp1 += x;
  1492.                     x = 0;
  1493.                 }
  1494.                 else {
  1495.                     cp += 4; cp1 += 4;
  1496.                     x -= 4;
  1497.                 }
  1498.                 pp += 10;
  1499.             }
  1500.             if (h <= 2)
  1501.                 break;
  1502.             h -= 2;
  1503.             cp += incr, cp1 += incr;
  1504.             pp += fromskew;
  1505.         }
  1506.     }
  1507. }
  1508.  
  1509. /*
  1510.  * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
  1511.  */
  1512. DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  1513. {
  1514.     YCbCrSetup;
  1515.  
  1516.     (void) y;
  1517.     /* XXX adjust fromskew */
  1518.     do {
  1519.     x = w>>2;
  1520.     do {
  1521.         int Cb = pp[4];
  1522.         int Cr = pp[5];
  1523.  
  1524.         YCbCrtoRGB(cp [0], pp[0]);
  1525.         YCbCrtoRGB(cp [1], pp[1]);
  1526.         YCbCrtoRGB(cp [2], pp[2]);
  1527.         YCbCrtoRGB(cp [3], pp[3]);
  1528.  
  1529.         cp += 4;
  1530.         pp += 6;
  1531.     } while (--x);
  1532.  
  1533.         if( (w&3) != 0 )
  1534.         {
  1535.         int Cb = pp[4];
  1536.         int Cr = pp[5];
  1537.  
  1538.             switch( (w&3) ) {
  1539.               case 3: YCbCrtoRGB(cp [2], pp[2]);
  1540.               case 2: YCbCrtoRGB(cp [1], pp[1]);
  1541.               case 1: YCbCrtoRGB(cp [0], pp[0]);
  1542.               case 0: break;
  1543.             }
  1544.  
  1545.             cp += (w&3);
  1546.             pp += 6;
  1547.         }
  1548.  
  1549.     cp += toskew;
  1550.     pp += fromskew;
  1551.     } while (--h);
  1552.  
  1553. }
  1554.  
  1555. /*
  1556.  * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
  1557.  */
  1558. DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  1559. {
  1560.     YCbCrSetup;
  1561.     uint32* cp1 = cp+w+toskew;
  1562.     int32 incr = 2*toskew+w;
  1563.  
  1564.     (void) y;
  1565.     fromskew = (fromskew * 6) / 2;
  1566.     if ((h & 1) == 0 && (w & 1) == 0) {
  1567.         for (; h >= 2; h -= 2) {
  1568.             x = w>>1;
  1569.             do {
  1570.                 int Cb = pp[4];
  1571.                 int Cr = pp[5];
  1572.  
  1573.                 YCbCrtoRGB(cp [0], pp[0]);
  1574.                 YCbCrtoRGB(cp [1], pp[1]);
  1575.                 YCbCrtoRGB(cp1[0], pp[2]);
  1576.                 YCbCrtoRGB(cp1[1], pp[3]);
  1577.  
  1578.                 cp += 2, cp1 += 2;
  1579.                 pp += 6;
  1580.             } while (--x);
  1581.             cp += incr, cp1 += incr;
  1582.             pp += fromskew;
  1583.         }
  1584.     } else {
  1585.         while (h > 0) {
  1586.             for (x = w; x > 0;) {
  1587.                 int Cb = pp[4];
  1588.                 int Cr = pp[5];
  1589.                 switch (x) {
  1590.                 default:
  1591.                     switch (h) {
  1592.                     default: YCbCrtoRGB(cp1[1], pp[ 3]); /* FALLTHROUGH */
  1593.                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
  1594.                     }                                    /* FALLTHROUGH */
  1595.                 case 1:
  1596.                     switch (h) {
  1597.                     default: YCbCrtoRGB(cp1[0], pp[ 2]); /* FALLTHROUGH */
  1598.                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
  1599.                     }                                    /* FALLTHROUGH */
  1600.                 }
  1601.                 if (x < 2) {
  1602.                     cp += x; cp1 += x;
  1603.                     x = 0;
  1604.                 }
  1605.                 else {
  1606.                     cp += 2; cp1 += 2;
  1607.                     x -= 2;
  1608.                 }
  1609.                 pp += 6;
  1610.             }
  1611.             if (h <= 2)
  1612.                 break;
  1613.             h -= 2;
  1614.             cp += incr, cp1 += incr;
  1615.             pp += fromskew;
  1616.         }
  1617.     }
  1618. }
  1619.  
  1620. /*
  1621.  * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
  1622.  */
  1623. DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  1624. {
  1625.     YCbCrSetup;
  1626.  
  1627.     (void) y;
  1628.     fromskew = (fromskew * 4) / 2;
  1629.     do {
  1630.     x = w>>1;
  1631.     do {
  1632.         int Cb = pp[2];
  1633.         int Cr = pp[3];
  1634.  
  1635.         YCbCrtoRGB(cp[0], pp[0]); 
  1636.         YCbCrtoRGB(cp[1], pp[1]);
  1637.  
  1638.         cp += 2;
  1639.         pp += 4;
  1640.     } while (--x);
  1641.  
  1642.         if( (w&1) != 0 )
  1643.         {
  1644.         int Cb = pp[2];
  1645.         int Cr = pp[3];
  1646.             
  1647.             YCbCrtoRGB(cp [0], pp[0]);
  1648.  
  1649.         cp += 1;
  1650.         pp += 4;
  1651.         }
  1652.  
  1653.     cp += toskew;
  1654.     pp += fromskew;
  1655.     } while (--h);
  1656. }
  1657.  
  1658. /*
  1659.  * 8-bit packed YCbCr samples w/ no subsampling => RGB
  1660.  */
  1661. DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
  1662. {
  1663.     YCbCrSetup;
  1664.  
  1665.     (void) y;
  1666.     fromskew *= 3;
  1667.     do {
  1668.         x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */ 
  1669.     do {
  1670.         int Cb = pp[1];
  1671.         int Cr = pp[2];
  1672.  
  1673.         YCbCrtoRGB(*cp++, pp[0]);
  1674.  
  1675.         pp += 3;
  1676.     } while (--x);
  1677.     cp += toskew;
  1678.     pp += fromskew;
  1679.     } while (--h);
  1680. }
  1681. #undef    YCbCrSetup
  1682. #undef    YCbCrtoRGB
  1683.  
  1684. #define    LumaRed            coeffs[0]
  1685. #define    LumaGreen        coeffs[1]
  1686. #define    LumaBlue        coeffs[2]
  1687. #define    SHIFT            16
  1688. #define    FIX(x)            ((int32)((x) * (1L<<SHIFT) + 0.5))
  1689. #define    ONE_HALF        ((int32)(1<<(SHIFT-1)))
  1690.  
  1691. /*
  1692.  * Initialize the YCbCr->RGB conversion tables.  The conversion
  1693.  * is done according to the 6.0 spec:
  1694.  *
  1695.  *    R = Y + Cr*(2 - 2*LumaRed)
  1696.  *    B = Y + Cb*(2 - 2*LumaBlue)
  1697.  *    G =   Y
  1698.  *        - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
  1699.  *        - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
  1700.  *
  1701.  * To avoid floating point arithmetic the fractional constants that
  1702.  * come out of the equations are represented as fixed point values
  1703.  * in the range 0...2^16.  We also eliminate multiplications by
  1704.  * pre-calculating possible values indexed by Cb and Cr (this code
  1705.  * assumes conversion is being done for 8-bit samples).
  1706.  */
  1707. static void
  1708. TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, TIFF* tif)
  1709. {
  1710.     TIFFRGBValue* clamptab;
  1711.     float* coeffs;
  1712.     int i;
  1713.  
  1714.     clamptab = (TIFFRGBValue*)(
  1715.     (tidata_t) ycbcr+TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long)));
  1716.     _TIFFmemset(clamptab, 0, 256);        /* v < 0 => 0 */
  1717.     ycbcr->clamptab = (clamptab += 256);
  1718.     for (i = 0; i < 256; i++)
  1719.     clamptab[i] = i;
  1720.     _TIFFmemset(clamptab+256, 255, 2*256);    /* v > 255 => 255 */
  1721.     TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &coeffs);
  1722.     _TIFFmemcpy(ycbcr->coeffs, coeffs, 3*sizeof (float));
  1723.     { float f1 = 2-2*LumaRed;        int32 D1 = FIX(f1);
  1724.       float f2 = LumaRed*f1/LumaGreen;    int32 D2 = -FIX(f2);
  1725.       float f3 = 2-2*LumaBlue;        int32 D3 = FIX(f3);
  1726.       float f4 = LumaBlue*f3/LumaGreen;    int32 D4 = -FIX(f4);
  1727.       int x;
  1728.  
  1729.       ycbcr->Cr_r_tab = (int*) (clamptab + 3*256);
  1730.       ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256;
  1731.       ycbcr->Cr_g_tab = (int32*) (ycbcr->Cb_b_tab + 256);
  1732.       ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256;
  1733.       /*
  1734.        * i is the actual input pixel value in the range 0..255
  1735.        * Cb and Cr values are in the range -128..127 (actually
  1736.        * they are in a range defined by the ReferenceBlackWhite
  1737.        * tag) so there is some range shifting to do here when
  1738.        * constructing tables indexed by the raw pixel data.
  1739.        *
  1740.        * XXX handle ReferenceBlackWhite correctly to calculate
  1741.        *     Cb/Cr values to use in constructing the tables.
  1742.        */
  1743.       for (i = 0, x = -128; i < 256; i++, x++) {
  1744.       ycbcr->Cr_r_tab[i] = (int)((D1*x + ONE_HALF)>>SHIFT);
  1745.       ycbcr->Cb_b_tab[i] = (int)((D3*x + ONE_HALF)>>SHIFT);
  1746.       ycbcr->Cr_g_tab[i] = D2*x;
  1747.       ycbcr->Cb_g_tab[i] = D4*x + ONE_HALF;
  1748.       }
  1749.     }
  1750. }
  1751. #undef    SHIFT
  1752. #undef    ONE_HALF
  1753. #undef    FIX
  1754. #undef    LumaBlue
  1755. #undef    LumaGreen
  1756. #undef    LumaRed
  1757.  
  1758. static tileContigRoutine
  1759. initYCbCrConversion(TIFFRGBAImage* img)
  1760. {
  1761.     uint16 hs, vs;
  1762.     TIFF* tif = img->tif;
  1763.  
  1764.     if (img->ycbcr == NULL) {
  1765.     img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(tif,
  1766.           TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long))
  1767.         + 4*256*sizeof (TIFFRGBValue)
  1768.         + 2*256*sizeof (int)
  1769.         + 2*256*sizeof (int32)
  1770.     );
  1771.     if (img->ycbcr == NULL) {
  1772.         TIFFError(TIFFFileName(img->tif),
  1773.         "No space for YCbCr->RGB conversion state");
  1774.         return (NULL);
  1775.     }
  1776.     TIFFYCbCrToRGBInit(img->ycbcr, img->tif);
  1777.     } else {
  1778.     float* coeffs;
  1779.  
  1780.     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &coeffs);
  1781.     if (_TIFFmemcmp(coeffs, img->ycbcr->coeffs, 3*sizeof (float)) != 0)
  1782.         TIFFYCbCrToRGBInit(img->ycbcr, img->tif);
  1783.     }
  1784.     /*
  1785.      * The 6.0 spec says that subsampling must be
  1786.      * one of 1, 2, or 4, and that vertical subsampling
  1787.      * must always be <= horizontal subsampling; so
  1788.      * there are only a few possibilities and we just
  1789.      * enumerate the cases.
  1790.      */
  1791.     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
  1792.     switch ((hs<<4)|vs) {
  1793.     case 0x44: return ((tileContigRoutine) putcontig8bitYCbCr44tile);
  1794.     case 0x42: return ((tileContigRoutine) putcontig8bitYCbCr42tile);
  1795.     case 0x41: return ((tileContigRoutine) putcontig8bitYCbCr41tile);
  1796.     case 0x22: return ((tileContigRoutine) putcontig8bitYCbCr22tile);
  1797.     case 0x21: return ((tileContigRoutine) putcontig8bitYCbCr21tile);
  1798.     case 0x11: return ((tileContigRoutine) putcontig8bitYCbCr11tile);
  1799.     }
  1800.     return (NULL);
  1801. }
  1802.  
  1803. /*
  1804.  * Greyscale images with less than 8 bits/sample are handled
  1805.  * with a table to avoid lots of shifts and masks.  The table
  1806.  * is setup so that put*bwtile (below) can retrieve 8/bitspersample
  1807.  * pixel values simply by indexing into the table with one
  1808.  * number.
  1809.  */
  1810. static int
  1811. makebwmap(TIFFRGBAImage* img)
  1812. {
  1813.     TIFFRGBValue* Map = img->Map;
  1814.     int bitspersample = img->bitspersample;
  1815.     int nsamples = 8 / bitspersample;
  1816.     int i;
  1817.     uint32* p;
  1818.     TIFF* tif = img->tif;
  1819.  
  1820.     img->BWmap = (uint32**) _TIFFmalloc(tif,
  1821.     256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  1822.     if (img->BWmap == NULL) {
  1823.     TIFFError(TIFFFileName(img->tif), "No space for B&W mapping table");
  1824.     return (0);
  1825.     }
  1826.     p = (uint32*)(img->BWmap + 256);
  1827.     for (i = 0; i < 256; i++) {
  1828.     TIFFRGBValue c;
  1829.     img->BWmap[i] = p;
  1830.     switch (bitspersample) {
  1831. #define    GREY(x)    c = Map[x]; *p++ = PACK(c,c,c);
  1832.     case 1:
  1833.         GREY(i>>7);
  1834.         GREY((i>>6)&1);
  1835.         GREY((i>>5)&1);
  1836.         GREY((i>>4)&1);
  1837.         GREY((i>>3)&1);
  1838.         GREY((i>>2)&1);
  1839.         GREY((i>>1)&1);
  1840.         GREY(i&1);
  1841.         break;
  1842.     case 2:
  1843.         GREY(i>>6);
  1844.         GREY((i>>4)&3);
  1845.         GREY((i>>2)&3);
  1846.         GREY(i&3);
  1847.         break;
  1848.     case 4:
  1849.         GREY(i>>4);
  1850.         GREY(i&0xf);
  1851.         break;
  1852.     case 8:
  1853.         GREY(i);
  1854.         break;
  1855.     }
  1856. #undef    GREY
  1857.     }
  1858.     return (1);
  1859. }
  1860.  
  1861. /*
  1862.  * Construct a mapping table to convert from the range
  1863.  * of the data samples to [0,255] --for display.  This
  1864.  * process also handles inverting B&W images when needed.
  1865.  */ 
  1866. static int
  1867. setupMap(TIFFRGBAImage* img)
  1868. {
  1869.     int32 x, range;
  1870.     TIFF* tif = img->tif;
  1871.  
  1872.     range = (int32)((1L<<img->bitspersample)-1);
  1873.     img->Map = (TIFFRGBValue*)
  1874.             _TIFFmalloc(tif, (range+1) * sizeof (TIFFRGBValue));
  1875.     if (img->Map == NULL) {
  1876.     TIFFError(TIFFFileName(img->tif),
  1877.         "No space for photometric conversion table");
  1878.     return (0);
  1879.     }
  1880.     if (img->photometric == PHOTOMETRIC_MINISWHITE) {
  1881.     for (x = 0; x <= range; x++)
  1882.         img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
  1883.     } else {
  1884.     for (x = 0; x <= range; x++)
  1885.         img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
  1886.     }
  1887.     if (img->bitspersample <= 8 &&
  1888.     (img->photometric == PHOTOMETRIC_MINISBLACK ||
  1889.      img->photometric == PHOTOMETRIC_MINISWHITE)) {
  1890.     /*
  1891.      * Use photometric mapping table to construct
  1892.      * unpacking tables for samples <= 8 bits.
  1893.      */
  1894.     if (!makebwmap(img))
  1895.         return (0);
  1896.     /* no longer need Map, free it */
  1897.     _TIFFfree(tif, img->Map), img->Map = NULL;
  1898.     }
  1899.     return (1);
  1900. }
  1901.  
  1902. static int
  1903. checkcmap(TIFFRGBAImage* img)
  1904. {
  1905.     uint16* r = img->redcmap;
  1906.     uint16* g = img->greencmap;
  1907.     uint16* b = img->bluecmap;
  1908.     long n = 1L<<img->bitspersample;
  1909.  
  1910.     while (n-- > 0)
  1911.     if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  1912.         return (16);
  1913.     return (8);
  1914. }
  1915.  
  1916. static void
  1917. cvtcmap(TIFFRGBAImage* img)
  1918. {
  1919.     uint16* r = img->redcmap;
  1920.     uint16* g = img->greencmap;
  1921.     uint16* b = img->bluecmap;
  1922.     long i;
  1923.  
  1924.     for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
  1925. #define    CVT(x)        ((uint16)((x)>>8))
  1926.     r[i] = CVT(r[i]);
  1927.     g[i] = CVT(g[i]);
  1928.     b[i] = CVT(b[i]);
  1929. #undef    CVT
  1930.     }
  1931. }
  1932.  
  1933. /*
  1934.  * Palette images with <= 8 bits/sample are handled
  1935.  * with a table to avoid lots of shifts and masks.  The table
  1936.  * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
  1937.  * pixel values simply by indexing into the table with one
  1938.  * number.
  1939.  */
  1940. static int
  1941. makecmap(TIFFRGBAImage* img)
  1942. {
  1943.     int bitspersample = img->bitspersample;
  1944.     int nsamples = 8 / bitspersample;
  1945.     uint16* r = img->redcmap;
  1946.     uint16* g = img->greencmap;
  1947.     uint16* b = img->bluecmap;
  1948.     uint32 *p;
  1949.     int i;
  1950.     TIFF* tif = img->tif;
  1951.  
  1952.     img->PALmap = (uint32**) _TIFFmalloc(tif,
  1953.     256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  1954.     if (img->PALmap == NULL) {
  1955.     TIFFError(TIFFFileName(img->tif), "No space for Palette mapping table");
  1956.     return (0);
  1957.     }
  1958.     p = (uint32*)(img->PALmap + 256);
  1959.     for (i = 0; i < 256; i++) {
  1960.     TIFFRGBValue c;
  1961.     img->PALmap[i] = p;
  1962. #define    CMAP(x)    c = x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
  1963.     switch (bitspersample) {
  1964.     case 1:
  1965.         CMAP(i>>7);
  1966.         CMAP((i>>6)&1);
  1967.         CMAP((i>>5)&1);
  1968.         CMAP((i>>4)&1);
  1969.         CMAP((i>>3)&1);
  1970.         CMAP((i>>2)&1);
  1971.         CMAP((i>>1)&1);
  1972.         CMAP(i&1);
  1973.         break;
  1974.     case 2:
  1975.         CMAP(i>>6);
  1976.         CMAP((i>>4)&3);
  1977.         CMAP((i>>2)&3);
  1978.         CMAP(i&3);
  1979.         break;
  1980.     case 4:
  1981.         CMAP(i>>4);
  1982.         CMAP(i&0xf);
  1983.         break;
  1984.     case 8:
  1985.         CMAP(i);
  1986.         break;
  1987.     }
  1988. #undef CMAP
  1989.     }
  1990.     return (1);
  1991. }
  1992.  
  1993. /* 
  1994.  * Construct any mapping table used
  1995.  * by the associated put routine.
  1996.  */
  1997. static int
  1998. buildMap(TIFFRGBAImage* img)
  1999. {
  2000.     switch (img->photometric) {
  2001.     case PHOTOMETRIC_RGB:
  2002.     case PHOTOMETRIC_YCBCR:
  2003.     case PHOTOMETRIC_SEPARATED:
  2004.     if (img->bitspersample == 8)
  2005.         break;
  2006.     /* fall thru... */
  2007.     case PHOTOMETRIC_MINISBLACK:
  2008.     case PHOTOMETRIC_MINISWHITE:
  2009.     if (!setupMap(img))
  2010.         return (0);
  2011.     break;
  2012.     case PHOTOMETRIC_PALETTE:
  2013.     /*
  2014.      * Convert 16-bit colormap to 8-bit (unless it looks
  2015.      * like an old-style 8-bit colormap).
  2016.      */
  2017.     if (checkcmap(img) == 16)
  2018.         cvtcmap(img);
  2019.     else
  2020.         TIFFWarning(TIFFFileName(img->tif), "Assuming 8-bit colormap");
  2021.     /*
  2022.      * Use mapping table and colormap to construct
  2023.      * unpacking tables for samples < 8 bits.
  2024.      */
  2025.     if (img->bitspersample <= 8 && !makecmap(img))
  2026.         return (0);
  2027.     break;
  2028.     }
  2029.     return (1);
  2030. }
  2031.  
  2032. /*
  2033.  * Select the appropriate conversion routine for packed data.
  2034.  */
  2035. static int
  2036. pickTileContigCase(TIFFRGBAImage* img)
  2037. {
  2038.     tileContigRoutine put = 0;
  2039.  
  2040.     if (buildMap(img)) {
  2041.     switch (img->photometric) {
  2042.     case PHOTOMETRIC_RGB:
  2043.         switch (img->bitspersample) {
  2044.         case 8:
  2045.         if (!img->Map) {
  2046.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2047.             put = putRGBAAcontig8bittile;
  2048.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2049.             put = putRGBUAcontig8bittile;
  2050.             else
  2051.             put = putRGBcontig8bittile;
  2052.         } else
  2053.             put = putRGBcontig8bitMaptile;
  2054.         break;
  2055.         case 16:
  2056.         put = putRGBcontig16bittile;
  2057.         if (!img->Map) {
  2058.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2059.             put = putRGBAAcontig16bittile;
  2060.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2061.             put = putRGBUAcontig16bittile;
  2062.         }
  2063.         break;
  2064.         }
  2065.         break;
  2066.     case PHOTOMETRIC_SEPARATED:
  2067.         if (img->bitspersample == 8) {
  2068.         if (!img->Map)
  2069.             put = putRGBcontig8bitCMYKtile;
  2070.         else
  2071.             put = putRGBcontig8bitCMYKMaptile;
  2072.         }
  2073.         break;
  2074.     case PHOTOMETRIC_PALETTE:
  2075.         switch (img->bitspersample) {
  2076.         case 8:    put = put8bitcmaptile; break;
  2077.         case 4: put = put4bitcmaptile; break;
  2078.         case 2: put = put2bitcmaptile; break;
  2079.         case 1: put = put1bitcmaptile; break;
  2080.         }
  2081.         break;
  2082.     case PHOTOMETRIC_MINISWHITE:
  2083.     case PHOTOMETRIC_MINISBLACK:
  2084.         switch (img->bitspersample) {
  2085.         case 8:    put = putgreytile; break;
  2086.         case 4: put = put4bitbwtile; break;
  2087.         case 2: put = put2bitbwtile; break;
  2088.         case 1: put = put1bitbwtile; break;
  2089.         }
  2090.         break;
  2091.     case PHOTOMETRIC_YCBCR:
  2092.         if (img->bitspersample == 8)
  2093.         put = initYCbCrConversion(img);
  2094.         break;
  2095.     }
  2096.     }
  2097.     return ((img->put.contig = put) != 0);
  2098. }
  2099.  
  2100. /*
  2101.  * Select the appropriate conversion routine for unpacked data.
  2102.  *
  2103.  * NB: we assume that unpacked single channel data is directed
  2104.  *     to the "packed routines.
  2105.  */
  2106. static int
  2107. pickTileSeparateCase(TIFFRGBAImage* img)
  2108. {
  2109.     tileSeparateRoutine put = 0;
  2110.  
  2111.     if (buildMap(img)) {
  2112.     switch (img->photometric) {
  2113.     case PHOTOMETRIC_RGB:
  2114.         switch (img->bitspersample) {
  2115.         case 8:
  2116.         if (!img->Map) {
  2117.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2118.             put = putRGBAAseparate8bittile;
  2119.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2120.             put = putRGBUAseparate8bittile;
  2121.             else
  2122.             put = putRGBseparate8bittile;
  2123.         } else
  2124.             put = putRGBseparate8bitMaptile;
  2125.         break;
  2126.         case 16:
  2127.         put = putRGBseparate16bittile;
  2128.         if (!img->Map) {
  2129.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  2130.             put = putRGBAAseparate16bittile;
  2131.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  2132.             put = putRGBUAseparate16bittile;
  2133.         }
  2134.         break;
  2135.         }
  2136.         break;
  2137.     }
  2138.     }
  2139.     return ((img->put.separate = put) != 0);
  2140. }
  2141.  
  2142. /*
  2143.  * Read a whole strip off data from the file, and convert to RGBA form.
  2144.  * If this is the last strip, then it will only contain the portion of
  2145.  * the strip that is actually within the image space.  The result is
  2146.  * organized in bottom to top form.
  2147.  */
  2148.  
  2149.  
  2150. int
  2151. TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
  2152.  
  2153. {
  2154.     char     emsg[1024];
  2155.     TIFFRGBAImage img;
  2156.     int     ok;
  2157.     uint32    rowsperstrip, rows_to_read;
  2158.  
  2159.     if( TIFFIsTiled( tif ) )
  2160.     {
  2161.         TIFFError(TIFFFileName(tif),
  2162.                   "Can't use TIFFReadRGBAStrip() with tiled file.");
  2163.     return (0);
  2164.     }
  2165.     
  2166.     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  2167.     if( (row % rowsperstrip) != 0 )
  2168.     {
  2169.         TIFFError(TIFFFileName(tif),
  2170.                 "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
  2171.     return (0);
  2172.     }
  2173.  
  2174.     if (TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
  2175.  
  2176.         img.row_offset = row;
  2177.         img.col_offset = 0;
  2178.  
  2179.         if( row + rowsperstrip > img.height )
  2180.             rows_to_read = img.height - row;
  2181.         else
  2182.             rows_to_read = rowsperstrip;
  2183.         
  2184.     ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
  2185.         
  2186.     TIFFRGBAImageEnd(&img);
  2187.     } else {
  2188.     TIFFError(TIFFFileName(tif), emsg);
  2189.     ok = 0;
  2190.     }
  2191.     
  2192.     return (ok);
  2193. }
  2194.  
  2195. /*
  2196.  * Read a whole tile off data from the file, and convert to RGBA form.
  2197.  * The returned RGBA data is organized from bottom to top of tile,
  2198.  * and may include zeroed areas if the tile extends off the image.
  2199.  */
  2200.  
  2201. int
  2202. TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
  2203.  
  2204. {
  2205.     char     emsg[1024];
  2206.     TIFFRGBAImage img;
  2207.     int     ok;
  2208.     uint32    tile_xsize, tile_ysize;
  2209.     uint32    read_xsize, read_ysize;
  2210.     uint32    i_row;
  2211.  
  2212.     /*
  2213.      * Verify that our request is legal - on a tile file, and on a
  2214.      * tile boundary.
  2215.      */
  2216.     
  2217.     if( !TIFFIsTiled( tif ) )
  2218.     {
  2219.         TIFFError(TIFFFileName(tif),
  2220.                   "Can't use TIFFReadRGBATile() with stripped file.");
  2221.     return (0);
  2222.     }
  2223.     
  2224.     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
  2225.     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
  2226.     if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
  2227.     {
  2228.         TIFFError(TIFFFileName(tif),
  2229.                   "Row/col passed to TIFFReadRGBATile() must be top"
  2230.                   "left corner of a tile.");
  2231.     return (0);
  2232.     }
  2233.  
  2234.     /*
  2235.      * Setup the RGBA reader.
  2236.      */
  2237.     
  2238.     if ( !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
  2239.     TIFFError(TIFFFileName(tif), emsg);
  2240.         return( 0 );
  2241.     }
  2242.  
  2243.     /*
  2244.      * The TIFFRGBAImageGet() function doesn't allow us to get off the
  2245.      * edge of the image, even to fill an otherwise valid tile.  So we
  2246.      * figure out how much we can read, and fix up the tile buffer to
  2247.      * a full tile configuration afterwards.
  2248.      */
  2249.  
  2250.     if( row + tile_ysize > img.height )
  2251.         read_ysize = img.height - row;
  2252.     else
  2253.         read_ysize = tile_ysize;
  2254.     
  2255.     if( col + tile_xsize > img.width )
  2256.         read_xsize = img.width - col;
  2257.     else
  2258.         read_xsize = tile_xsize;
  2259.  
  2260.     /*
  2261.      * Read the chunk of imagery.
  2262.      */
  2263.     
  2264.     img.row_offset = row;
  2265.     img.col_offset = col;
  2266.  
  2267.     ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
  2268.         
  2269.     TIFFRGBAImageEnd(&img);
  2270.  
  2271.     /*
  2272.      * If our read was incomplete we will need to fix up the tile by
  2273.      * shifting the data around as if a full tile of data is being returned.
  2274.      *
  2275.      * This is all the more complicated because the image is organized in
  2276.      * bottom to top format. 
  2277.      */
  2278.  
  2279.     if( read_xsize == tile_xsize && read_ysize == tile_ysize )
  2280.         return( ok );
  2281.  
  2282.     for( i_row = 0; i_row < read_ysize; i_row++ )
  2283.     {
  2284.         _TIFFmemcpy( raster + (tile_ysize - i_row - 1) * tile_xsize,
  2285.                      raster + (read_ysize - i_row - 1) * read_xsize,
  2286.                      read_xsize * sizeof(uint32) );
  2287.         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
  2288.                      0, sizeof(uint32) * (tile_xsize - read_xsize) );
  2289.     }
  2290.  
  2291.     for( i_row = read_ysize; i_row < tile_ysize; i_row++ )
  2292.     {
  2293.         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
  2294.                      0, sizeof(uint32) * tile_xsize );
  2295.     }
  2296.     
  2297.     return (ok);
  2298. }
  2299.