home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 4 / DATAFILE_PDCD4.iso / utilities / utilsf / libtiff / c / tif_getima < prev    next >
Encoding:
Text File  |  1995-10-12  |  44.1 KB  |  1,807 lines

  1. /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_getimage.c,v 1.38 1995/07/17 01:27:18 sam Exp $ */
  2.  
  3. /*
  4.  * Copyright (c) 1991-1995 Sam Leffler
  5.  * Copyright (c) 1991-1995 Silicon Graphics, Inc.
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software and 
  8.  * its documentation for any purpose is hereby granted without fee, provided
  9.  * that (i) the above copyright notices and this permission notice appear in
  10.  * all copies of the software and related documentation, and (ii) the names of
  11.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  12.  * publicity relating to the software without the specific, prior written
  13.  * permission of Sam Leffler and Silicon Graphics.
  14.  * 
  15.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  16.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  17.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  18.  * 
  19.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  20.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  21.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  22.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  23.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  24.  * OF THIS SOFTWARE.
  25.  */
  26.  
  27. /*
  28.  * TIFF Library
  29.  *
  30.  * Read and return a packed RGBA image.
  31.  */
  32. #include "tiffiop.h"
  33. #include <assert.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 && td->td_samplesperpixel != 1) {
  86.         sprintf(emsg,
  87.         "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
  88.         photoTag, photometric,
  89.         "Samples/pixel", td->td_samplesperpixel);
  90.         return (0);
  91.     }
  92.     break;
  93.     case PHOTOMETRIC_YCBCR:
  94.     if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
  95.         sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
  96.         "Planarconfiguration", td->td_planarconfig);
  97.         return (0);
  98.     }
  99.     break;
  100.     case PHOTOMETRIC_RGB: 
  101.     if (colorchannels < 3) {
  102.         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  103.         "Color channels", colorchannels);
  104.         return (0);
  105.     }
  106.     break;
  107. #ifdef CMYK_SUPPORT
  108.     case PHOTOMETRIC_SEPARATED:
  109.     if (td->td_inkset != INKSET_CMYK) {
  110.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  111.         "InkSet", td->td_inkset);
  112.         return (0);
  113.     }
  114.     if (td->td_samplesperpixel != 4) {
  115.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  116.         "Samples/pixel", td->td_samplesperpixel);
  117.         return (0);
  118.     }
  119.     break;
  120. #endif
  121.     default:
  122.     sprintf(emsg, "Sorry, can not handle image with %s=%d",
  123.         photoTag, photometric);
  124.     return (0);
  125.     }
  126.     return (1);
  127. }
  128.  
  129. void
  130. TIFFRGBAImageEnd(TIFFRGBAImage* img)
  131. {
  132.     if (img->Map)
  133.     _TIFFfree(img->Map), img->Map = NULL;
  134.     if (img->BWmap)
  135.     _TIFFfree(img->BWmap), img->BWmap = NULL;
  136.     if (img->PALmap)
  137.     _TIFFfree(img->PALmap), img->PALmap = NULL;
  138.     if (img->ycbcr)
  139.     _TIFFfree(img->ycbcr), img->ycbcr = NULL;
  140. }
  141.  
  142. static int
  143. isCCITTCompression(TIFF* tif)
  144. {
  145.     uint16 compress;
  146.     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  147.     return (compress == COMPRESSION_CCITTFAX3 ||
  148.         compress == COMPRESSION_CCITTFAX4 ||
  149.         compress == COMPRESSION_CCITTRLE ||
  150.         compress == COMPRESSION_CCITTRLEW);
  151. }
  152.  
  153. int
  154. TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
  155. {
  156.     uint16* sampleinfo;
  157.     uint16 extrasamples;
  158.     uint16 planarconfig;
  159.     int colorchannels;
  160.  
  161.     img->tif = tif;
  162.     img->stoponerr = stop;
  163.     TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
  164.     switch (img->bitspersample) {
  165.     case 1: case 2: case 4:
  166.     case 8: case 16:
  167.     break;
  168.     default:
  169.     sprintf(emsg, "Sorry, can not image with %d-bit samples",
  170.         img->bitspersample);
  171.     return (0);
  172.     }
  173.     img->alpha = 0;
  174.     TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
  175.     TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
  176.     &extrasamples, &sampleinfo);
  177.     if (extrasamples == 1)
  178.     switch (sampleinfo[0]) {
  179.     case EXTRASAMPLE_ASSOCALPHA:    /* data is pre-multiplied */
  180.     case EXTRASAMPLE_UNASSALPHA:    /* data is not pre-multiplied */
  181.         img->alpha = sampleinfo[0];
  182.         break;
  183.     }
  184.     colorchannels = img->samplesperpixel - extrasamples;
  185.     TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
  186.     if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
  187.     switch (colorchannels) {
  188.     case 1:
  189.         if (isCCITTCompression(tif))
  190.         img->photometric = PHOTOMETRIC_MINISWHITE;
  191.         else
  192.         img->photometric = PHOTOMETRIC_MINISBLACK;
  193.         break;
  194.     case 3:
  195.         img->photometric = PHOTOMETRIC_RGB;
  196.         break;
  197.     default:
  198.         sprintf(emsg, "Missing needed %s tag", photoTag);
  199.         return (0);
  200.     }
  201.     }
  202.     switch (img->photometric) {
  203.     case PHOTOMETRIC_PALETTE:
  204.     if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
  205.         &img->redcmap, &img->greencmap, &img->bluecmap)) {
  206.         TIFFError(TIFFFileName(tif), "Missing required \"Colormap\" tag");
  207.         return (0);
  208.     }
  209.     /* fall thru... */
  210.     case PHOTOMETRIC_MINISWHITE:
  211.     case PHOTOMETRIC_MINISBLACK:
  212.     if (planarconfig == PLANARCONFIG_CONTIG && img->samplesperpixel != 1) {
  213.         sprintf(emsg,
  214.         "Sorry, can not handle contiguous data with %s=%d, and %s=%d",
  215.         photoTag, img->photometric,
  216.         "Samples/pixel", img->samplesperpixel);
  217.         return (0);
  218.     }
  219.     break;
  220.     case PHOTOMETRIC_YCBCR:
  221.     if (planarconfig != PLANARCONFIG_CONTIG) {
  222.         sprintf(emsg, "Sorry, can not handle YCbCr images with %s=%d",
  223.         "Planarconfiguration", planarconfig);
  224.         return (0);
  225.     }
  226.     /* It would probably be nice to have a reality check here. */
  227.     { uint16 compress;
  228.       TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
  229.       if (compress == COMPRESSION_JPEG && planarconfig == PLANARCONFIG_CONTIG) {
  230.         /* can rely on libjpeg to convert to RGB */
  231.         /* XXX should restore current state on exit */
  232.         TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
  233.         img->photometric = PHOTOMETRIC_RGB;
  234.       }
  235.     }
  236.     break;
  237.     case PHOTOMETRIC_RGB: 
  238.     if (colorchannels < 3) {
  239.         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
  240.         "Color channels", colorchannels);
  241.         return (0);
  242.     }
  243.     break;
  244.     case PHOTOMETRIC_SEPARATED: {
  245.     uint16 inkset;
  246.     TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
  247.     if (inkset != INKSET_CMYK) {
  248.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  249.         "InkSet", inkset);
  250.         return (0);
  251.     }
  252.     if (img->samplesperpixel != 4) {
  253.         sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
  254.         "Samples/pixel", img->samplesperpixel);
  255.         return (0);
  256.     }
  257.     break;
  258.     }
  259.     default:
  260.     sprintf(emsg, "Sorry, can not handle image with %s=%d",
  261.         photoTag, img->photometric);
  262.     return (0);
  263.     }
  264.     img->Map = NULL;
  265.     img->BWmap = NULL;
  266.     img->PALmap = NULL;
  267.     img->ycbcr = NULL;
  268.     TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
  269.     TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
  270.     TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
  271.     img->isContig =
  272.     !(planarconfig == PLANARCONFIG_SEPARATE && colorchannels > 1);
  273.     if (img->isContig) {
  274.     img->get = TIFFIsTiled(tif) ? gtTileContig : gtStripContig;
  275.     (void) pickTileContigCase(img);
  276.     } else {
  277.     img->get = TIFFIsTiled(tif) ? gtTileSeparate : gtStripSeparate;
  278.     (void) pickTileSeparateCase(img);
  279.     }
  280.     return (1);
  281. }
  282.  
  283. int
  284. TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  285. {
  286.     if (img->get == NULL) {
  287.     TIFFError(TIFFFileName(img->tif), "No \"get\" routine setup");
  288.     return (0);
  289.     }
  290.     if (img->put.any == NULL) {
  291.     TIFFError(TIFFFileName(img->tif),
  292.         "No \"put\" routine setupl; probably can not handle image format");
  293.     return (0);
  294.     }
  295.     return (*img->get)(img, raster, w, h);
  296. }
  297.  
  298. /*
  299.  * Read the specified image into an ABGR-format raster.
  300.  */
  301. int
  302. TIFFReadRGBAImage(TIFF* tif,
  303.     uint32 rwidth, uint32 rheight, uint32* raster, int stop)
  304. {
  305.     char emsg[1024];
  306.     TIFFRGBAImage img;
  307.     int ok;
  308.  
  309.     if (TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
  310.     /* XXX verify rwidth and rheight against width and height */
  311.     ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
  312.         rwidth, img.height);
  313.     TIFFRGBAImageEnd(&img);
  314.     } else {
  315.     TIFFError(TIFFFileName(tif), emsg);
  316.     ok = 0;
  317.     }
  318.     return (ok);
  319. }
  320.  
  321. static uint32
  322. setorientation(TIFFRGBAImage* img, uint32 h)
  323. {
  324.     TIFF* tif = img->tif;
  325.     uint32 y;
  326.  
  327.     switch (img->orientation) {
  328.     case ORIENTATION_BOTRIGHT:
  329.     case ORIENTATION_RIGHTBOT:    /* XXX */
  330.     case ORIENTATION_LEFTBOT:    /* XXX */
  331.     TIFFWarning(TIFFFileName(tif), "using bottom-left orientation");
  332.     img->orientation = ORIENTATION_BOTLEFT;
  333.     /* fall thru... */
  334.     case ORIENTATION_BOTLEFT:
  335.     y = 0;
  336.     break;
  337.     case ORIENTATION_TOPRIGHT:
  338.     case ORIENTATION_RIGHTTOP:    /* XXX */
  339.     case ORIENTATION_LEFTTOP:    /* XXX */
  340.     default:
  341.     TIFFWarning(TIFFFileName(tif), "using top-left orientation");
  342.     img->orientation = ORIENTATION_TOPLEFT;
  343.     /* fall thru... */
  344.     case ORIENTATION_TOPLEFT:
  345.     y = h-1;
  346.     break;
  347.     }
  348.     return (y);
  349. }
  350.  
  351. /*
  352.  * Get an tile-organized image that has
  353.  *    PlanarConfiguration contiguous if SamplesPerPixel > 1
  354.  * or
  355.  *    SamplesPerPixel == 1
  356.  */    
  357. static int
  358. gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  359. {
  360.     TIFF* tif = img->tif;
  361.     tileContigRoutine put = img->put.contig;
  362.     uint16 orientation;
  363.     uint32 col, row, y;
  364.     uint32 tw, th;
  365.     u_char* buf;
  366.     int32 fromskew, toskew;
  367.     uint32 nrow;
  368.  
  369.     buf = (u_char*) _TIFFmalloc(TIFFTileSize(tif));
  370.     if (buf == 0) {
  371.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  372.     return (0);
  373.     }
  374.     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  375.     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  376.     y = setorientation(img, h);
  377.     orientation = img->orientation;
  378.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? tw+w : tw-w);
  379.     for (row = 0; row < h; row += th) {
  380.     nrow = (row + th > h ? h - row : th);
  381.     for (col = 0; col < w; col += tw) {
  382.         if (TIFFReadTile(tif, buf, col, row, 0, 0) < 0 && img->stoponerr)
  383.         break;
  384.         if (col + tw > w) {
  385.         /*
  386.          * Tile is clipped horizontally.  Calculate
  387.          * visible portion and skewing factors.
  388.          */
  389.         uint32 npix = w - col;
  390.         fromskew = tw - npix;
  391.         (*put)(img, raster+y*w+col, col, y,
  392.             npix, nrow, fromskew, toskew + fromskew, buf);
  393.         } else {
  394.         (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf);
  395.         }
  396.     }
  397.     y += (orientation == ORIENTATION_TOPLEFT ?
  398.         -(int32) nrow : (int32) nrow);
  399.     }
  400.     _TIFFfree(buf);
  401.     return (1);
  402. }
  403.  
  404. /*
  405.  * Get an tile-organized image that has
  406.  *     SamplesPerPixel > 1
  407.  *     PlanarConfiguration separated
  408.  * We assume that all such images are RGB.
  409.  */    
  410. static int
  411. gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  412. {
  413.     TIFF* tif = img->tif;
  414.     tileSeparateRoutine put = img->put.separate;
  415.     uint16 orientation;
  416.     uint32 col, row, y;
  417.     uint32 tw, th;
  418.     u_char* buf;
  419.     u_char* r;
  420.     u_char* g;
  421.     u_char* b;
  422.     u_char* a;
  423.     tsize_t tilesize;
  424.     int32 fromskew, toskew;
  425.     int alpha = img->alpha;
  426.     uint32 nrow;
  427.  
  428.     tilesize = TIFFTileSize(tif);
  429.     buf = (u_char*) _TIFFmalloc(4*tilesize);
  430.     if (buf == 0) {
  431.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  432.     return (0);
  433.     }
  434.     r = buf;
  435.     g = r + tilesize;
  436.     b = g + tilesize;
  437.     a = b + tilesize;
  438.     if (!alpha)
  439.     memset(a, 0xff, tilesize);
  440.     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
  441.     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
  442.     y = setorientation(img, h);
  443.     orientation = img->orientation;
  444.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? tw+w : tw-w);
  445.     for (row = 0; row < h; row += th) {
  446.     nrow = (row + th > h ? h - row : th);
  447.     for (col = 0; col < w; col += tw) {
  448.         if (TIFFReadTile(tif, r, col, row,0,0) < 0 && img->stoponerr)
  449.         break;
  450.         if (TIFFReadTile(tif, g, col, row,0,1) < 0 && img->stoponerr)
  451.         break;
  452.         if (TIFFReadTile(tif, b, col, row,0,2) < 0 && img->stoponerr)
  453.         break;
  454.         if (alpha && TIFFReadTile(tif,a,col,row,0,3) < 0 && img->stoponerr)
  455.         break;
  456.         if (col + tw > w) {
  457.         /*
  458.          * Tile is clipped horizontally.  Calculate
  459.          * visible portion and skewing factors.
  460.          */
  461.         uint32 npix = w - col;
  462.         fromskew = tw - npix;
  463.         (*put)(img, raster+y*w+col, col, y,
  464.             npix, nrow, fromskew, toskew + fromskew, r, g, b, a);
  465.         } else {
  466.         (*put)(img, raster+y*w+col, col, y,
  467.             tw, nrow, 0, toskew, r, g, b, a);
  468.         }
  469.     }
  470.     y += (orientation == ORIENTATION_TOPLEFT ?
  471.         -(int32) nrow : (int32) nrow);
  472.     }
  473.     _TIFFfree(buf);
  474.     return (1);
  475. }
  476.  
  477. /*
  478.  * Get a strip-organized image that has
  479.  *    PlanarConfiguration contiguous if SamplesPerPixel > 1
  480.  * or
  481.  *    SamplesPerPixel == 1
  482.  */    
  483. static int
  484. gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  485. {
  486.     TIFF* tif = img->tif;
  487.     tileContigRoutine put = img->put.contig;
  488.     uint16 orientation;
  489.     uint32 row, y, nrow;
  490.     u_char* buf;
  491.     uint32 rowsperstrip;
  492.     uint32 imagewidth = img->width;
  493.     tsize_t scanline;
  494.     int32 fromskew, toskew;
  495.  
  496.     buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif));
  497.     if (buf == 0) {
  498.     TIFFError(TIFFFileName(tif), "No space for strip buffer");
  499.     return (0);
  500.     }
  501.     y = setorientation(img, h);
  502.     orientation = img->orientation;
  503.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);
  504.     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  505.     scanline = TIFFScanlineSize(tif);
  506.     fromskew = (w < imagewidth ? imagewidth - w : 0);
  507.     for (row = 0; row < h; row += rowsperstrip) {
  508.     nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  509.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
  510.         buf, nrow*scanline) < 0 && img->stoponerr)
  511.         break;
  512.     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf);
  513.     y += (orientation == ORIENTATION_TOPLEFT ?
  514.         -(int32) nrow : (int32) nrow);
  515.     }
  516.     _TIFFfree(buf);
  517.     return (1);
  518. }
  519.  
  520. /*
  521.  * Get a strip-organized image with
  522.  *     SamplesPerPixel > 1
  523.  *     PlanarConfiguration separated
  524.  * We assume that all such images are RGB.
  525.  */
  526. static int
  527. gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
  528. {
  529.     TIFF* tif = img->tif;
  530.     tileSeparateRoutine put = img->put.separate;
  531.     uint16 orientation;
  532.     u_char *buf;
  533.     u_char *r, *g, *b, *a;
  534.     uint32 row, y, nrow;
  535.     tsize_t scanline;
  536.     uint32 rowsperstrip;
  537.     uint32 imagewidth = img->width;
  538.     tsize_t stripsize;
  539.     int32 fromskew, toskew;
  540.     int alpha = img->alpha;
  541.  
  542.     stripsize = TIFFStripSize(tif);
  543.     r = buf = (u_char *)_TIFFmalloc(4*stripsize);
  544.     if (buf == 0) {
  545.     TIFFError(TIFFFileName(tif), "No space for tile buffer");
  546.     return (0);
  547.     }
  548.     g = r + stripsize;
  549.     b = g + stripsize;
  550.     a = b + stripsize;
  551.     if (!alpha)
  552.     memset(a, 0xff, stripsize);
  553.     y = setorientation(img, h);
  554.     orientation = img->orientation;
  555.     toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);
  556.     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
  557.     scanline = TIFFScanlineSize(tif);
  558.     fromskew = (w < imagewidth ? imagewidth - w : 0);
  559.     for (row = 0; row < h; row += rowsperstrip) {
  560.     nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);
  561.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 0),
  562.         r, nrow*scanline) < 0 && img->stoponerr)
  563.         break;
  564.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 1),
  565.         g, nrow*scanline) < 0 && img->stoponerr)
  566.         break;
  567.     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 2),
  568.         b, nrow*scanline) < 0 && img->stoponerr)
  569.         break;
  570.     if (alpha &&
  571.         (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, row, 3),
  572.         a, nrow*scanline) < 0 && img->stoponerr))
  573.         break;
  574.     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r, g, b, a);
  575.     y += (orientation == ORIENTATION_TOPLEFT ?
  576.         -(int32) nrow : (int32) nrow);
  577.     }
  578.     _TIFFfree(buf);
  579.     return (1);
  580. }
  581.  
  582. /*
  583.  * The following routines move decoded data returned
  584.  * from the TIFF library into rasters filled with packed
  585.  * ABGR pixels (i.e. suitable for passing to lrecwrite.)
  586.  *
  587.  * The routines have been created according to the most
  588.  * important cases and optimized.  pickTileContigCase and
  589.  * pickTileSeparateCase analyze the parameters and select
  590.  * the appropriate "put" routine to use.
  591.  */
  592. #define    REPEAT8(op)    REPEAT4(op); REPEAT4(op)
  593. #define    REPEAT4(op)    REPEAT2(op); REPEAT2(op)
  594. #define    REPEAT2(op)    op; op
  595. #define    CASE8(x,op)            \
  596.     switch (x) {            \
  597.     case 7: op; case 6: op; case 5: op;    \
  598.     case 4: op; case 3: op; case 2: op;    \
  599.     case 1: op;                \
  600.     }
  601. #define    CASE4(x,op)    switch (x) { case 3: op; case 2: op; case 1: op; }
  602. #define    NOP
  603.  
  604. #define    UNROLL8(w, op1, op2) {        \
  605.     uint32 _x;                \
  606.     for (_x = w; _x >= 8; _x -= 8) {    \
  607.     op1;                \
  608.     REPEAT8(op2);            \
  609.     }                    \
  610.     if (_x > 0) {            \
  611.     op1;                \
  612.     CASE8(_x,op2);            \
  613.     }                    \
  614. }
  615. #define    UNROLL4(w, op1, op2) {        \
  616.     uint32 _x;                \
  617.     for (_x = w; _x >= 4; _x -= 4) {    \
  618.     op1;                \
  619.     REPEAT4(op2);            \
  620.     }                    \
  621.     if (_x > 0) {            \
  622.     op1;                \
  623.     CASE4(_x,op2);            \
  624.     }                    \
  625. }
  626. #define    UNROLL2(w, op1, op2) {        \
  627.     uint32 _x;                \
  628.     for (_x = w; _x >= 2; _x -= 2) {    \
  629.     op1;                \
  630.     REPEAT2(op2);            \
  631.     }                    \
  632.     if (_x) {                \
  633.     op1;                \
  634.     op2;                \
  635.     }                    \
  636. }
  637.     
  638. #define    SKEW(r,g,b,skew)    { r += skew; g += skew; b += skew; }
  639. #define    SKEW4(r,g,b,a,skew)    { r += skew; g += skew; b += skew; a+= skew; }
  640.  
  641. #define A1 ((uint32)(0xffL<<24))
  642. #define    PACK(r,g,b)    \
  643.     ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
  644. #define    PACK4(r,g,b,a)    \
  645.     ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
  646. #define W2B(v) (((v)>>8)&0xff)
  647. #define    PACKW(r,g,b)    \
  648.     ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
  649. #define    PACKW4(r,g,b,a)    \
  650.     ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
  651.  
  652. #define    DECLAREContigPutFunc(name) \
  653. static void name(\
  654.     TIFFRGBAImage* img, \
  655.     uint32* cp, \
  656.     uint32 x, uint32 y, \
  657.     uint32 w, uint32 h, \
  658.     int32 fromskew, int32 toskew, \
  659.     u_char* pp \
  660. )
  661.  
  662. /*
  663.  * 8-bit palette => colormap/RGB
  664.  */
  665. DECLAREContigPutFunc(put8bitcmaptile)
  666. {
  667.     uint32** PALmap = img->PALmap;
  668.  
  669.     (void) x; (void) y;
  670.     while (h-- > 0) {
  671.     UNROLL8(w, NOP, *cp++ = PALmap[*pp++][0]);
  672.     cp += toskew;
  673.     pp += fromskew;
  674.     }
  675. }
  676.  
  677. /*
  678.  * 4-bit palette => colormap/RGB
  679.  */
  680. DECLAREContigPutFunc(put4bitcmaptile)
  681. {
  682.     uint32** PALmap = img->PALmap;
  683.  
  684.     (void) x; (void) y;
  685.     fromskew /= 2;
  686.     while (h-- > 0) {
  687.     uint32* bw;
  688.     UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
  689.     cp += toskew;
  690.     pp += fromskew;
  691.     }
  692. }
  693.  
  694. /*
  695.  * 2-bit palette => colormap/RGB
  696.  */
  697. DECLAREContigPutFunc(put2bitcmaptile)
  698. {
  699.     uint32** PALmap = img->PALmap;
  700.  
  701.     (void) x; (void) y;
  702.     fromskew /= 4;
  703.     while (h-- > 0) {
  704.     uint32* bw;
  705.     UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
  706.     cp += toskew;
  707.     pp += fromskew;
  708.     }
  709. }
  710.  
  711. /*
  712.  * 1-bit palette => colormap/RGB
  713.  */
  714. DECLAREContigPutFunc(put1bitcmaptile)
  715. {
  716.     uint32** PALmap = img->PALmap;
  717.  
  718.     (void) x; (void) y;
  719.     fromskew /= 8;
  720.     while (h-- > 0) {
  721.     uint32* bw;
  722.     UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
  723.     cp += toskew;
  724.     pp += fromskew;
  725.     }
  726. }
  727.  
  728. /*
  729.  * 8-bit greyscale => colormap/RGB
  730.  */
  731. DECLAREContigPutFunc(putgreytile)
  732. {
  733.     uint32** BWmap = img->BWmap;
  734.  
  735.     (void) y;
  736.     while (h-- > 0) {
  737.     for (x = w; x-- > 0;)
  738.         *cp++ = BWmap[*pp++][0];
  739.     cp += toskew;
  740.     pp += fromskew;
  741.     }
  742. }
  743.  
  744. /*
  745.  * 1-bit bilevel => colormap/RGB
  746.  */
  747. DECLAREContigPutFunc(put1bitbwtile)
  748. {
  749.     uint32** BWmap = img->BWmap;
  750.  
  751.     (void) x; (void) y;
  752.     fromskew /= 8;
  753.     while (h-- > 0) {
  754.     uint32* bw;
  755.     UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
  756.     cp += toskew;
  757.     pp += fromskew;
  758.     }
  759. }
  760.  
  761. /*
  762.  * 2-bit greyscale => colormap/RGB
  763.  */
  764. DECLAREContigPutFunc(put2bitbwtile)
  765. {
  766.     uint32** BWmap = img->BWmap;
  767.  
  768.     (void) x; (void) y;
  769.     fromskew /= 4;
  770.     while (h-- > 0) {
  771.     uint32* bw;
  772.     UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
  773.     cp += toskew;
  774.     pp += fromskew;
  775.     }
  776. }
  777.  
  778. /*
  779.  * 4-bit greyscale => colormap/RGB
  780.  */
  781. DECLAREContigPutFunc(put4bitbwtile)
  782. {
  783.     uint32** BWmap = img->BWmap;
  784.  
  785.     (void) x; (void) y;
  786.     fromskew /= 2;
  787.     while (h-- > 0) {
  788.     uint32* bw;
  789.     UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
  790.     cp += toskew;
  791.     pp += fromskew;
  792.     }
  793. }
  794.  
  795. /*
  796.  * 8-bit packed samples, no Map => RGB
  797.  */
  798. DECLAREContigPutFunc(putRGBcontig8bittile)
  799. {
  800.     int samplesperpixel = img->samplesperpixel;
  801.  
  802.     (void) x; (void) y;
  803.     fromskew *= samplesperpixel;
  804.     while (h-- > 0) {
  805.     UNROLL8(w, NOP,
  806.         *cp++ = PACK(pp[0], pp[1], pp[2]);
  807.         pp += samplesperpixel);
  808.     cp += toskew;
  809.     pp += fromskew;
  810.     }
  811. }
  812.  
  813. /*
  814.  * 8-bit packed samples, w/ Map => RGB
  815.  */
  816. DECLAREContigPutFunc(putRGBcontig8bitMaptile)
  817. {
  818.     TIFFRGBValue* Map = img->Map;
  819.     int samplesperpixel = img->samplesperpixel;
  820.  
  821.     (void) y;
  822.     fromskew *= samplesperpixel;
  823.     while (h-- > 0) {
  824.     for (x = w; x-- > 0;) {
  825.         *cp++ = PACK(Map[pp[0]], Map[pp[1]], Map[pp[2]]);
  826.         pp += samplesperpixel;
  827.     }
  828.     pp += fromskew;
  829.     cp += toskew;
  830.     }
  831. }
  832.  
  833. /*
  834.  * 8-bit packed samples => RGBA w/ associated alpha
  835.  * (known to have Map == NULL)
  836.  */
  837. DECLAREContigPutFunc(putRGBAAcontig8bittile)
  838. {
  839.     int samplesperpixel = img->samplesperpixel;
  840.  
  841.     (void) x; (void) y;
  842.     fromskew *= samplesperpixel;
  843.     while (h-- > 0) {
  844.     UNROLL8(w, NOP,
  845.         *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
  846.         pp += samplesperpixel);
  847.     cp += toskew;
  848.     pp += fromskew;
  849.     }
  850. }
  851.  
  852. /*
  853.  * 8-bit packed samples => RGBA w/ unassociated alpha
  854.  * (known to have Map == NULL)
  855.  */
  856. DECLAREContigPutFunc(putRGBUAcontig8bittile)
  857. {
  858.     int samplesperpixel = img->samplesperpixel;
  859.  
  860.     (void) y;
  861.     fromskew *= samplesperpixel;
  862.     while (h-- > 0) {
  863.     uint32 r, g, b, a;
  864.     for (x = w; x-- > 0;) {
  865.         a = pp[3];
  866.         r = (pp[0] * a) / 255;
  867.         g = (pp[1] * a) / 255;
  868.         b = (pp[2] * a) / 255;
  869.         *cp++ = PACK4(r,g,b,a);
  870.         pp += samplesperpixel;
  871.     }
  872.     cp += toskew;
  873.     pp += fromskew;
  874.     }
  875. }
  876.  
  877. /*
  878.  * 16-bit packed samples => RGB
  879.  */
  880. DECLAREContigPutFunc(putRGBcontig16bittile)
  881. {
  882.     int samplesperpixel = img->samplesperpixel;
  883.     uint16 *wp = (uint16 *)pp;
  884.  
  885.     (void) y;
  886.     fromskew *= samplesperpixel;
  887.     while (h-- > 0) {
  888.     for (x = w; x-- > 0;) {
  889.         *cp++ = PACKW(wp[0], wp[1], wp[2]);
  890.         wp += samplesperpixel;
  891.     }
  892.     cp += toskew;
  893.     wp += fromskew;
  894.     }
  895. }
  896.  
  897. /*
  898.  * 16-bit packed samples => RGBA w/ associated alpha
  899.  * (known to have Map == NULL)
  900.  */
  901. DECLAREContigPutFunc(putRGBAAcontig16bittile)
  902. {
  903.     int samplesperpixel = img->samplesperpixel;
  904.     uint16 *wp = (uint16 *)pp;
  905.  
  906.     (void) y;
  907.     fromskew *= samplesperpixel;
  908.     while (h-- > 0) {
  909.     for (x = w; x-- > 0;) {
  910.         *cp++ = PACKW4(wp[0], wp[1], wp[2], wp[3]);
  911.         wp += samplesperpixel;
  912.     }
  913.     cp += toskew;
  914.     wp += fromskew;
  915.     }
  916. }
  917.  
  918. /*
  919.  * 16-bit packed samples => RGBA w/ unassociated alpha
  920.  * (known to have Map == NULL)
  921.  */
  922. DECLAREContigPutFunc(putRGBUAcontig16bittile)
  923. {
  924.     int samplesperpixel = img->samplesperpixel;
  925.     uint16 *wp = (uint16 *)pp;
  926.  
  927.     (void) y;
  928.     fromskew *= samplesperpixel;
  929.     while (h-- > 0) {
  930.     uint32 r,g,b,a;
  931.     /*
  932.      * We shift alpha down four bits just in case unsigned
  933.      * arithmetic doesn't handle the full range.
  934.      * We still have plenty of accuracy, since the output is 8 bits.
  935.      * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
  936.      * Since we want r*a * 0xff for eight bit output,
  937.      * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
  938.      */
  939.     for (x = w; x-- > 0;) {
  940.         a = wp[3] >> 4; 
  941.         r = (wp[0] * a) / 0x10eff;
  942.         g = (wp[1] * a) / 0x10eff;
  943.         b = (wp[2] * a) / 0x10eff;
  944.         *cp++ = PACK4(r,g,b,a);
  945.         wp += samplesperpixel;
  946.     }
  947.     cp += toskew;
  948.     wp += fromskew;
  949.     }
  950. }
  951.  
  952. /*
  953.  * 8-bit packed CMYK samples w/o Map => RGB
  954.  *
  955.  * NB: The conversion of CMYK->RGB is *very* crude.
  956.  */
  957. DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
  958. {
  959.     int samplesperpixel = img->samplesperpixel;
  960.     uint16 r, g, b, k;
  961.  
  962.     (void) x; (void) y;
  963.     fromskew *= samplesperpixel;
  964.     while (h-- > 0) {
  965.     UNROLL8(w, NOP,
  966.         k = 255 - pp[3];
  967.         r = (k*(255-pp[0]))/255;
  968.         g = (k*(255-pp[1]))/255;
  969.         b = (k*(255-pp[2]))/255;
  970.         *cp++ = PACK(r, g, b);
  971.         pp += samplesperpixel);
  972.     cp += toskew;
  973.     pp += fromskew;
  974.     }
  975. }
  976.  
  977. /*
  978.  * 8-bit packed CMYK samples w/Map => RGB
  979.  *
  980.  * NB: The conversion of CMYK->RGB is *very* crude.
  981.  */
  982. DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
  983. {
  984.     int samplesperpixel = img->samplesperpixel;
  985.     TIFFRGBValue* Map = img->Map;
  986.     uint16 r, g, b, k;
  987.  
  988.     (void) y;
  989.     fromskew *= samplesperpixel;
  990.     while (h-- > 0) {
  991.     for (x = w; x-- > 0;) {
  992.         k = 255 - pp[3];
  993.         r = (k*(255-pp[0]))/255;
  994.         g = (k*(255-pp[1]))/255;
  995.         b = (k*(255-pp[2]))/255;
  996.         *cp++ = PACK(Map[r], Map[g], Map[b]);
  997.         pp += samplesperpixel;
  998.     }
  999.     pp += fromskew;
  1000.     cp += toskew;
  1001.     }
  1002. }
  1003.  
  1004. #define    DECLARESepPutFunc(name) \
  1005. static void name(\
  1006.     TIFFRGBAImage* img,\
  1007.     uint32* cp,\
  1008.     uint32 x, uint32 y, \
  1009.     uint32 w, uint32 h,\
  1010.     int32 fromskew, int32 toskew,\
  1011.     u_char* r, u_char* g, u_char* b, u_char* a\
  1012. )
  1013.  
  1014. /*
  1015.  * 8-bit unpacked samples => RGB
  1016.  */
  1017. DECLARESepPutFunc(putRGBseparate8bittile)
  1018. {
  1019.     (void) img; (void) x; (void) y; (void) a;
  1020.     while (h-- > 0) {
  1021.     UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
  1022.     SKEW(r, g, b, fromskew);
  1023.     cp += toskew;
  1024.     }
  1025. }
  1026.  
  1027. /*
  1028.  * 8-bit unpacked samples => RGB
  1029.  */
  1030. DECLARESepPutFunc(putRGBseparate8bitMaptile)
  1031. {
  1032.     TIFFRGBValue* Map = img->Map;
  1033.  
  1034.     (void) y; (void) a;
  1035.     while (h-- > 0) {
  1036.     for (x = w; x > 0; x--)
  1037.         *cp++ = PACK(Map[*r++], Map[*g++], Map[*b++]);
  1038.     SKEW(r, g, b, fromskew);
  1039.     cp += toskew;
  1040.     }
  1041. }
  1042.  
  1043. /*
  1044.  * 8-bit unpacked samples => RGBA w/ associated alpha
  1045.  */
  1046. DECLARESepPutFunc(putRGBAAseparate8bittile)
  1047. {
  1048.     (void) img; (void) x; (void) y;
  1049.     while (h-- > 0) {
  1050.     UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
  1051.     SKEW4(r, g, b, a, fromskew);
  1052.     cp += toskew;
  1053.     }
  1054. }
  1055.  
  1056. /*
  1057.  * 8-bit unpacked samples => RGBA w/ unassociated alpha
  1058.  */
  1059. DECLARESepPutFunc(putRGBUAseparate8bittile)
  1060. {
  1061.     (void) img; (void) y;
  1062.     while (h-- > 0) {
  1063.     uint32 rv, gv, bv, av;
  1064.     for (x = w; x-- > 0;) {
  1065.         av = *a++;
  1066.         rv = (*r++ * av) / 255;
  1067.         gv = (*g++ * av) / 255;
  1068.         bv = (*b++ * av) / 255;
  1069.         *cp++ = PACK4(rv,gv,bv,av);
  1070.     }
  1071.     SKEW4(r, g, b, a, fromskew);
  1072.     cp += toskew;
  1073.     }
  1074. }
  1075.  
  1076. /*
  1077.  * 16-bit unpacked samples => RGB
  1078.  */
  1079. DECLARESepPutFunc(putRGBseparate16bittile)
  1080. {
  1081.     uint16 *wr = (uint16*) r;
  1082.     uint16 *wg = (uint16*) g;
  1083.     uint16 *wb = (uint16*) b;
  1084.  
  1085.     (void) img; (void) y; (void) a;
  1086.     while (h-- > 0) {
  1087.     for (x = 0; x < w; x++)
  1088.         *cp++ = PACKW(*wr++, *wg++, *wb++);
  1089.     SKEW(wr, wg, wb, fromskew);
  1090.     cp += toskew;
  1091.     }
  1092. }
  1093.  
  1094. /*
  1095.  * 16-bit unpacked samples => RGBA w/ associated alpha
  1096.  */
  1097. DECLARESepPutFunc(putRGBAAseparate16bittile)
  1098. {
  1099.     uint16 *wr = (uint16*) r;
  1100.     uint16 *wg = (uint16*) g;
  1101.     uint16 *wb = (uint16*) b;
  1102.     uint16 *wa = (uint16*) a;
  1103.  
  1104.     (void) img; (void) y;
  1105.     while (h-- > 0) {
  1106.     for (x = 0; x < w; x++)
  1107.         *cp++ = PACKW4(*wr++, *wg++, *wb++, *wa++);
  1108.     SKEW4(wr, wg, wb, wa, fromskew);
  1109.     cp += toskew;
  1110.     }
  1111. }
  1112.  
  1113. /*
  1114.  * 16-bit unpacked samples => RGBA w/ unassociated alpha
  1115.  */
  1116. DECLARESepPutFunc(putRGBUAseparate16bittile)
  1117. {
  1118.     uint16 *wr = (uint16*) r;
  1119.     uint16 *wg = (uint16*) g;
  1120.     uint16 *wb = (uint16*) b;
  1121.     uint16 *wa = (uint16*) a;
  1122.  
  1123.     (void) img; (void) y;
  1124.     while (h-- > 0) {
  1125.     uint32 r,g,b,a;
  1126.     /*
  1127.      * We shift alpha down four bits just in case unsigned
  1128.      * arithmetic doesn't handle the full range.
  1129.      * We still have plenty of accuracy, since the output is 8 bits.
  1130.      * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)
  1131.      * Since we want r*a * 0xff for eight bit output,
  1132.      * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.
  1133.      */
  1134.     for (x = w; x-- > 0;) {
  1135.         a = *wa++ >> 4; 
  1136.         r = (*wr++ * a) / 0x10eff;
  1137.         g = (*wg++ * a) / 0x10eff;
  1138.         b = (*wb++ * a) / 0x10eff;
  1139.         *cp++ = PACK4(r,g,b,a);
  1140.     }
  1141.     SKEW4(wr, wg, wb, wa, fromskew);
  1142.     cp += toskew;
  1143.     }
  1144. }
  1145.  
  1146. /*
  1147.  * YCbCr -> RGB conversion and packing routines.  The colorspace
  1148.  * conversion algorithm comes from the IJG v5a code; see below
  1149.  * for more information on how it works.
  1150.  */
  1151.  
  1152. #define    YCbCrtoRGB(dst, yc) {                        \
  1153.     int Y = (yc);                            \
  1154.     dst = PACK(                                \
  1155.     clamptab[Y+Crrtab[Cr]],                        \
  1156.     clamptab[Y + (int)((Cbgtab[Cb]+Crgtab[Cr])>>16)],        \
  1157.     clamptab[Y+Cbbtab[Cb]]);                    \
  1158. }
  1159. #define    YCbCrSetup                            \
  1160.     TIFFYCbCrToRGB* ycbcr = img->ycbcr;                    \
  1161.     int* Crrtab = ycbcr->Cr_r_tab;                    \
  1162.     int* Cbbtab = ycbcr->Cb_b_tab;                    \
  1163.     int32* Crgtab = ycbcr->Cr_g_tab;                    \
  1164.     int32* Cbgtab = ycbcr->Cb_g_tab;                    \
  1165.     TIFFRGBValue* clamptab = ycbcr->clamptab
  1166.  
  1167. /*
  1168.  * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
  1169.  */
  1170. DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
  1171. {
  1172.     YCbCrSetup;
  1173.     uint32* cp1 = cp+w+toskew;
  1174.     uint32* cp2 = cp1+w+toskew;
  1175.     uint32* cp3 = cp2+w+toskew;
  1176.     u_int incr = 3*w+4*toskew;
  1177.  
  1178.     (void) y;
  1179.     /* XXX adjust fromskew */
  1180.     for (; h >= 4; h -= 4) {
  1181.     x = w>>2;
  1182.     do {
  1183.         int Cb = pp[16];
  1184.         int Cr = pp[17];
  1185.  
  1186.         YCbCrtoRGB(cp [0], pp[ 0]);
  1187.         YCbCrtoRGB(cp [1], pp[ 1]);
  1188.         YCbCrtoRGB(cp [2], pp[ 2]);
  1189.         YCbCrtoRGB(cp [3], pp[ 3]);
  1190.         YCbCrtoRGB(cp1[0], pp[ 4]);
  1191.         YCbCrtoRGB(cp1[1], pp[ 5]);
  1192.         YCbCrtoRGB(cp1[2], pp[ 6]);
  1193.         YCbCrtoRGB(cp1[3], pp[ 7]);
  1194.         YCbCrtoRGB(cp2[0], pp[ 8]);
  1195.         YCbCrtoRGB(cp2[1], pp[ 9]);
  1196.         YCbCrtoRGB(cp2[2], pp[10]);
  1197.         YCbCrtoRGB(cp2[3], pp[11]);
  1198.         YCbCrtoRGB(cp3[0], pp[12]);
  1199.         YCbCrtoRGB(cp3[1], pp[13]);
  1200.         YCbCrtoRGB(cp3[2], pp[14]);
  1201.         YCbCrtoRGB(cp3[3], pp[15]);
  1202.  
  1203.         cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
  1204.         pp += 18;
  1205.     } while (--x);
  1206.     cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
  1207.     pp += fromskew;
  1208.     }
  1209. }
  1210.  
  1211. /*
  1212.  * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
  1213.  */
  1214. DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
  1215. {
  1216.     YCbCrSetup;
  1217.     uint32* cp1 = cp+w+toskew;
  1218.     u_int incr = 2*toskew+w;
  1219.  
  1220.     (void) y;
  1221.     /* XXX adjust fromskew */
  1222.     for (; h >= 2; h -= 2) {
  1223.     x = w>>2;
  1224.     do {
  1225.         int Cb = pp[8];
  1226.         int Cr = pp[9];
  1227.  
  1228.         YCbCrtoRGB(cp [0], pp[0]);
  1229.         YCbCrtoRGB(cp [1], pp[1]);
  1230.         YCbCrtoRGB(cp [2], pp[2]);
  1231.         YCbCrtoRGB(cp [3], pp[3]);
  1232.         YCbCrtoRGB(cp1[0], pp[4]);
  1233.         YCbCrtoRGB(cp1[1], pp[5]);
  1234.         YCbCrtoRGB(cp1[2], pp[6]);
  1235.         YCbCrtoRGB(cp1[3], pp[7]);
  1236.  
  1237.         cp += 4, cp1 += 4;
  1238.         pp += 10;
  1239.     } while (--x);
  1240.     cp += incr, cp1 += incr;
  1241.     pp += fromskew;
  1242.     }
  1243. }
  1244.  
  1245. /*
  1246.  * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
  1247.  */
  1248. DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
  1249. {
  1250.     YCbCrSetup;
  1251.  
  1252.     (void) y;
  1253.     /* XXX adjust fromskew */
  1254.     do {
  1255.     x = w>>2;
  1256.     do {
  1257.         int Cb = pp[4];
  1258.         int Cr = pp[5];
  1259.  
  1260.         YCbCrtoRGB(cp [0], pp[0]);
  1261.         YCbCrtoRGB(cp [1], pp[1]);
  1262.         YCbCrtoRGB(cp [2], pp[2]);
  1263.         YCbCrtoRGB(cp [3], pp[3]);
  1264.  
  1265.         cp += 4;
  1266.         pp += 6;
  1267.     } while (--x);
  1268.     cp += toskew;
  1269.     pp += fromskew;
  1270.     } while (--h);
  1271. }
  1272.  
  1273. /*
  1274.  * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
  1275.  */
  1276. DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
  1277. {
  1278.     YCbCrSetup;
  1279.     uint32* cp1 = cp+w+toskew;
  1280.     u_int incr = 2*toskew+w;
  1281.  
  1282.     (void) y;
  1283.     /* XXX adjust fromskew */
  1284.     for (; h >= 2; h -= 2) {
  1285.     x = w>>1;
  1286.     do {
  1287.         int Cb = pp[4];
  1288.         int Cr = pp[5];
  1289.  
  1290.         YCbCrtoRGB(cp [0], pp[0]);
  1291.         YCbCrtoRGB(cp [1], pp[1]);
  1292.         YCbCrtoRGB(cp1[0], pp[2]);
  1293.         YCbCrtoRGB(cp1[1], pp[3]);
  1294.  
  1295.         cp += 2, cp1 += 2;
  1296.         pp += 6;
  1297.     } while (--x);
  1298.     cp += incr, cp1 += incr;
  1299.     pp += fromskew;
  1300.     }
  1301. }
  1302.  
  1303. /*
  1304.  * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
  1305.  */
  1306. DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
  1307. {
  1308.     YCbCrSetup;
  1309.  
  1310.     (void) y;
  1311.     /* XXX adjust fromskew */
  1312.     do {
  1313.     x = w>>1;
  1314.     do {
  1315.         int Cb = pp[2];
  1316.         int Cr = pp[3];
  1317.  
  1318.         YCbCrtoRGB(cp[0], pp[0]);
  1319.         YCbCrtoRGB(cp[1], pp[1]);
  1320.  
  1321.         cp += 2;
  1322.         pp += 4;
  1323.     } while (--x);
  1324.     cp += toskew;
  1325.     pp += fromskew;
  1326.     } while (--h);
  1327. }
  1328.  
  1329. /*
  1330.  * 8-bit packed YCbCr samples w/ no subsampling => RGB
  1331.  */
  1332. DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
  1333. {
  1334.     YCbCrSetup;
  1335.  
  1336.     (void) y;
  1337.     /* XXX adjust fromskew */
  1338.     do {
  1339.     x = w>>1;
  1340.     do {
  1341.         int Cb = pp[1];
  1342.         int Cr = pp[2];
  1343.  
  1344.         YCbCrtoRGB(*cp++, pp[0]);
  1345.  
  1346.         pp += 3;
  1347.     } while (--x);
  1348.     cp += toskew;
  1349.     pp += fromskew;
  1350.     } while (--h);
  1351. }
  1352. #undef    YCbCrSetup
  1353. #undef    YCbCrtoRGB
  1354.  
  1355. #define    LumaRed            coeffs[0]
  1356. #define    LumaGreen        coeffs[1]
  1357. #define    LumaBlue        coeffs[2]
  1358. #define    SHIFT            16
  1359. #define    FIX(x)            ((int32)((x) * (1L<<SHIFT) + 0.5))
  1360. #define    ONE_HALF        ((int32)(1<<(SHIFT-1)))
  1361.  
  1362. /*
  1363.  * Initialize the YCbCr->RGB conversion tables.  The conversion
  1364.  * is done according to the 6.0 spec:
  1365.  *
  1366.  *    R = Y + Cr*(2 - 2*LumaRed)
  1367.  *    B = Y + Cb*(2 - 2*LumaBlue)
  1368.  *    G =   Y
  1369.  *        - LumaBlue*Cb*(2-2*LumaBlue)/LumaGreen
  1370.  *        - LumaRed*Cr*(2-2*LumaRed)/LumaGreen
  1371.  *
  1372.  * To avoid floating point arithmetic the fractional constants that
  1373.  * come out of the equations are represented as fixed point values
  1374.  * in the range 0...2^16.  We also eliminate multiplications by
  1375.  * pre-calculating possible values indexed by Cb and Cr (this code
  1376.  * assumes conversion is being done for 8-bit samples).
  1377.  */
  1378. static void
  1379. TIFFYCbCrToRGBInit(TIFFYCbCrToRGB* ycbcr, TIFF* tif)
  1380. {
  1381.     TIFFRGBValue* clamptab;
  1382.     float* coeffs;
  1383.     int i;
  1384.  
  1385.     clamptab = (TIFFRGBValue*)(
  1386.     (tidata_t) ycbcr+TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long)));
  1387.     _TIFFmemset(clamptab, 0, 256);        /* v < 0 => 0 */
  1388.     ycbcr->clamptab = (clamptab += 256);
  1389.     for (i = 0; i < 256; i++)
  1390.     clamptab[i] = i;
  1391.     _TIFFmemset(clamptab+256, 255, 2*256);    /* v > 255 => 255 */
  1392.     TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRCOEFFICIENTS, &coeffs);
  1393.     _TIFFmemcpy(ycbcr->coeffs, coeffs, 3*sizeof (float));
  1394.     { float f1 = 2-2*LumaRed;        int32 D1 = FIX(f1);
  1395.       float f2 = LumaRed*f1/LumaGreen;    int32 D2 = -FIX(f2);
  1396.       float f3 = 2-2*LumaBlue;        int32 D3 = FIX(f3);
  1397.       float f4 = LumaBlue*f3/LumaGreen;    int32 D4 = -FIX(f4);
  1398.       int x;
  1399.  
  1400.       ycbcr->Cr_r_tab = (int*) (clamptab + 3*256);
  1401.       ycbcr->Cb_b_tab = ycbcr->Cr_r_tab + 256;
  1402.       ycbcr->Cr_g_tab = (int32*) (ycbcr->Cb_b_tab + 256);
  1403.       ycbcr->Cb_g_tab = ycbcr->Cr_g_tab + 256;
  1404.       /*
  1405.        * i is the actual input pixel value in the range 0..255
  1406.        * Cb and Cr values are in the range -128..127 (actually
  1407.        * they are in a range defined by the ReferenceBlackWhite
  1408.        * tag) so there is some range shifting to do here when
  1409.        * constructing tables indexed by the raw pixel data.
  1410.        *
  1411.        * XXX handle ReferenceBlackWhite correctly to calculate
  1412.        *     Cb/Cr values to use in constructing the tables.
  1413.        */
  1414.       for (i = 0, x = -128; i < 256; i++, x++) {
  1415.       ycbcr->Cr_r_tab[i] = (int)((D1*x + ONE_HALF)>>SHIFT);
  1416.       ycbcr->Cb_b_tab[i] = (int)((D3*x + ONE_HALF)>>SHIFT);
  1417.       ycbcr->Cr_g_tab[i] = D2*x;
  1418.       ycbcr->Cb_g_tab[i] = D4*x + ONE_HALF;
  1419.       }
  1420.     }
  1421. }
  1422. #undef    SHIFT
  1423. #undef    ONE_HALF
  1424. #undef    FIX
  1425. #undef    LumaBlue
  1426. #undef    LumaGreen
  1427. #undef    LumaRed
  1428.  
  1429. static tileContigRoutine
  1430. initYCbCrConversion(TIFFRGBAImage* img)
  1431. {
  1432.     uint16 hs, vs;
  1433.  
  1434.     if (img->ycbcr == NULL) {
  1435.     img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
  1436.           TIFFroundup(sizeof (TIFFYCbCrToRGB), sizeof (long))
  1437.         + 4*256*sizeof (TIFFRGBValue)
  1438.         + 2*256*sizeof (int)
  1439.         + 2*256*sizeof (int32)
  1440.     );
  1441.     if (img->ycbcr == NULL) {
  1442.         TIFFError(TIFFFileName(img->tif),
  1443.         "No space for YCbCr->RGB conversion state");
  1444.         return (NULL);
  1445.     }
  1446.     TIFFYCbCrToRGBInit(img->ycbcr, img->tif);
  1447.     } else {
  1448.     float* coeffs;
  1449.  
  1450.     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &coeffs);
  1451.     if (_TIFFmemcmp(coeffs, img->ycbcr->coeffs, 3*sizeof (float)) != 0)
  1452.         TIFFYCbCrToRGBInit(img->ycbcr, img->tif);
  1453.     }
  1454.     /*
  1455.      * The 6.0 spec says that subsampling must be
  1456.      * one of 1, 2, or 4, and that vertical subsampling
  1457.      * must always be <= horizontal subsampling; so
  1458.      * there are only a few possibilities and we just
  1459.      * enumerate the cases.
  1460.      */
  1461.     TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
  1462.     switch ((hs<<4)|vs) {
  1463.     case 0x44: return (putcontig8bitYCbCr44tile);
  1464.     case 0x42: return (putcontig8bitYCbCr42tile);
  1465.     case 0x41: return (putcontig8bitYCbCr41tile);
  1466.     case 0x22: return (putcontig8bitYCbCr22tile);
  1467.     case 0x21: return (putcontig8bitYCbCr21tile);
  1468.     case 0x11: return (putcontig8bitYCbCr11tile);
  1469.     }
  1470.     return (NULL);
  1471. }
  1472.  
  1473. /*
  1474.  * Greyscale images with less than 8 bits/sample are handled
  1475.  * with a table to avoid lots of shifts and masks.  The table
  1476.  * is setup so that put*bwtile (below) can retrieve 8/bitspersample
  1477.  * pixel values simply by indexing into the table with one
  1478.  * number.
  1479.  */
  1480. static int
  1481. makebwmap(TIFFRGBAImage* img)
  1482. {
  1483.     TIFFRGBValue* Map = img->Map;
  1484.     int bitspersample = img->bitspersample;
  1485.     int nsamples = 8 / bitspersample;
  1486.     int i;
  1487.     uint32* p;
  1488.  
  1489.     img->BWmap = (uint32**) _TIFFmalloc(
  1490.     256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  1491.     if (img->BWmap == NULL) {
  1492.     TIFFError(TIFFFileName(img->tif), "No space for B&W mapping table");
  1493.     return (0);
  1494.     }
  1495.     p = (uint32*)(img->BWmap + 256);
  1496.     for (i = 0; i < 256; i++) {
  1497.     TIFFRGBValue c;
  1498.     img->BWmap[i] = p;
  1499.     switch (bitspersample) {
  1500. #define    GREY(x)    c = Map[x]; *p++ = PACK(c,c,c);
  1501.     case 1:
  1502.         GREY(i>>7);
  1503.         GREY((i>>6)&1);
  1504.         GREY((i>>5)&1);
  1505.         GREY((i>>4)&1);
  1506.         GREY((i>>3)&1);
  1507.         GREY((i>>2)&1);
  1508.         GREY((i>>1)&1);
  1509.         GREY(i&1);
  1510.         break;
  1511.     case 2:
  1512.         GREY(i>>6);
  1513.         GREY((i>>4)&3);
  1514.         GREY((i>>2)&3);
  1515.         GREY(i&3);
  1516.         break;
  1517.     case 4:
  1518.         GREY(i>>4);
  1519.         GREY(i&0xf);
  1520.         break;
  1521.     case 8:
  1522.         GREY(i);
  1523.         break;
  1524.     }
  1525. #undef    GREY
  1526.     }
  1527.     return (1);
  1528. }
  1529.  
  1530. /*
  1531.  * Construct a mapping table to convert from the range
  1532.  * of the data samples to [0,255] --for display.  This
  1533.  * process also handles inverting B&W images when needed.
  1534.  */ 
  1535. static int
  1536. setupMap(TIFFRGBAImage* img)
  1537. {
  1538.     int32 x, range;
  1539.  
  1540.     range = (int32)((1L<<img->bitspersample)-1);
  1541.     img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
  1542.     if (img->Map == NULL) {
  1543.     TIFFError(TIFFFileName(img->tif),
  1544.         "No space for photometric conversion table");
  1545.     return (0);
  1546.     }
  1547.     if (img->photometric == PHOTOMETRIC_MINISWHITE) {
  1548.     for (x = 0; x <= range; x++)
  1549.         img->Map[x] = ((range - x) * 255) / range;
  1550.     } else {
  1551.     for (x = 0; x <= range; x++)
  1552.         img->Map[x] = (x * 255) / range;
  1553.     }
  1554.     if (img->bitspersample <= 8 &&
  1555.     (img->photometric == PHOTOMETRIC_MINISBLACK ||
  1556.      img->photometric == PHOTOMETRIC_MINISWHITE)) {
  1557.     /*
  1558.      * Use photometric mapping table to construct
  1559.      * unpacking tables for samples <= 8 bits.
  1560.      */
  1561.     if (!makebwmap(img))
  1562.         return (0);
  1563.     /* no longer need Map, free it */
  1564.     _TIFFfree(img->Map), img->Map = NULL;
  1565.     }
  1566.     return (1);
  1567. }
  1568.  
  1569. static int
  1570. checkcmap(TIFFRGBAImage* img)
  1571. {
  1572.     uint16* r = img->redcmap;
  1573.     uint16* g = img->greencmap;
  1574.     uint16* b = img->bluecmap;
  1575.     long n = 1L<<img->bitspersample;
  1576.  
  1577.     while (n-- > 0)
  1578.     if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
  1579.         return (16);
  1580.     return (8);
  1581. }
  1582.  
  1583. static void
  1584. cvtcmap(TIFFRGBAImage* img)
  1585. {
  1586.     uint16* r = img->redcmap;
  1587.     uint16* g = img->greencmap;
  1588.     uint16* b = img->bluecmap;
  1589.     long i;
  1590.  
  1591.     for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
  1592. #define    CVT(x)        ((uint16)(((x) * 255) / ((1L<<16)-1)))
  1593.     r[i] = CVT(r[i]);
  1594.     g[i] = CVT(g[i]);
  1595.     b[i] = CVT(b[i]);
  1596. #undef    CVT
  1597.     }
  1598. }
  1599.  
  1600. /*
  1601.  * Palette images with <= 8 bits/sample are handled
  1602.  * with a table to avoid lots of shifts and masks.  The table
  1603.  * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
  1604.  * pixel values simply by indexing into the table with one
  1605.  * number.
  1606.  */
  1607. static int
  1608. makecmap(TIFFRGBAImage* img)
  1609. {
  1610.     int bitspersample = img->bitspersample;
  1611.     int nsamples = 8 / bitspersample;
  1612.     uint16* r = img->redcmap;
  1613.     uint16* g = img->greencmap;
  1614.     uint16* b = img->bluecmap;
  1615.     uint32 *p;
  1616.     int i;
  1617.  
  1618.     img->PALmap = (uint32**) _TIFFmalloc(
  1619.     256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
  1620.     if (img->PALmap == NULL) {
  1621.     TIFFError(TIFFFileName(img->tif), "No space for Palette mapping table");
  1622.     return (0);
  1623.     }
  1624.     p = (uint32*)(img->PALmap + 256);
  1625.     for (i = 0; i < 256; i++) {
  1626.     TIFFRGBValue c;
  1627.     img->PALmap[i] = p;
  1628. #define    CMAP(x)    c = x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
  1629.     switch (bitspersample) {
  1630.     case 1:
  1631.         CMAP(i>>7);
  1632.         CMAP((i>>6)&1);
  1633.         CMAP((i>>5)&1);
  1634.         CMAP((i>>4)&1);
  1635.         CMAP((i>>3)&1);
  1636.         CMAP((i>>2)&1);
  1637.         CMAP((i>>1)&1);
  1638.         CMAP(i&1);
  1639.         break;
  1640.     case 2:
  1641.         CMAP(i>>6);
  1642.         CMAP((i>>4)&3);
  1643.         CMAP((i>>2)&3);
  1644.         CMAP(i&3);
  1645.         break;
  1646.     case 4:
  1647.         CMAP(i>>4);
  1648.         CMAP(i&0xf);
  1649.         break;
  1650.     case 8:
  1651.         CMAP(i);
  1652.         break;
  1653.     }
  1654. #undef CMAP
  1655.     }
  1656.     return (1);
  1657. }
  1658.  
  1659. /* 
  1660.  * Construct any mapping table used
  1661.  * by the associated put routine.
  1662.  */
  1663. static int
  1664. buildMap(TIFFRGBAImage* img)
  1665. {
  1666.     switch (img->photometric) {
  1667.     case PHOTOMETRIC_RGB:
  1668.     case PHOTOMETRIC_YCBCR:
  1669.     case PHOTOMETRIC_SEPARATED:
  1670.     if (img->bitspersample == 8)
  1671.         break;
  1672.     /* fall thru... */
  1673.     case PHOTOMETRIC_MINISBLACK:
  1674.     case PHOTOMETRIC_MINISWHITE:
  1675.     if (!setupMap(img))
  1676.         return (0);
  1677.     break;
  1678.     case PHOTOMETRIC_PALETTE:
  1679.     /*
  1680.      * Convert 16-bit colormap to 8-bit (unless it looks
  1681.      * like an old-style 8-bit colormap).
  1682.      */
  1683.     if (checkcmap(img) == 16)
  1684.         cvtcmap(img);
  1685.     else
  1686.         TIFFWarning(TIFFFileName(img->tif), "Assuming 8-bit colormap");
  1687.     /*
  1688.      * Use mapping table and colormap to construct
  1689.      * unpacking tables for samples < 8 bits.
  1690.      */
  1691.     if (img->bitspersample <= 8 && !makecmap(img))
  1692.         return (0);
  1693.     break;
  1694.     }
  1695.     return (1);
  1696. }
  1697.  
  1698. /*
  1699.  * Select the appropriate conversion routine for packed data.
  1700.  */
  1701. static int
  1702. pickTileContigCase(TIFFRGBAImage* img)
  1703. {
  1704.     tileContigRoutine put = 0;
  1705.  
  1706.     if (buildMap(img)) {
  1707.     switch (img->photometric) {
  1708.     case PHOTOMETRIC_RGB:
  1709.         switch (img->bitspersample) {
  1710.         case 8:
  1711.         if (!img->Map) {
  1712.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  1713.             put = putRGBAAcontig8bittile;
  1714.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  1715.             put = putRGBUAcontig8bittile;
  1716.             else
  1717.             put = putRGBcontig8bittile;
  1718.         } else
  1719.             put = putRGBcontig8bitMaptile;
  1720.         break;
  1721.         case 16:
  1722.         put = putRGBcontig16bittile;
  1723.         if (!img->Map) {
  1724.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  1725.             put = putRGBAAcontig16bittile;
  1726.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  1727.             put = putRGBUAcontig16bittile;
  1728.         }
  1729.         break;
  1730.         }
  1731.         break;
  1732.     case PHOTOMETRIC_SEPARATED:
  1733.         if (img->bitspersample == 8) {
  1734.         if (!img->Map)
  1735.             put = putRGBcontig8bitCMYKtile;
  1736.         else
  1737.             put = putRGBcontig8bitCMYKMaptile;
  1738.         }
  1739.         break;
  1740.     case PHOTOMETRIC_PALETTE:
  1741.         switch (img->bitspersample) {
  1742.         case 8:    put = put8bitcmaptile; break;
  1743.         case 4: put = put4bitcmaptile; break;
  1744.         case 2: put = put2bitcmaptile; break;
  1745.         case 1: put = put1bitcmaptile; break;
  1746.         }
  1747.         break;
  1748.     case PHOTOMETRIC_MINISWHITE:
  1749.     case PHOTOMETRIC_MINISBLACK:
  1750.         switch (img->bitspersample) {
  1751.         case 8:    put = putgreytile; break;
  1752.         case 4: put = put4bitbwtile; break;
  1753.         case 2: put = put2bitbwtile; break;
  1754.         case 1: put = put1bitbwtile; break;
  1755.         }
  1756.         break;
  1757.     case PHOTOMETRIC_YCBCR:
  1758.         if (img->bitspersample == 8)
  1759.         put = initYCbCrConversion(img);
  1760.         break;
  1761.     }
  1762.     }
  1763.     return ((img->put.contig = put) != 0);
  1764. }
  1765.  
  1766. /*
  1767.  * Select the appropriate conversion routine for unpacked data.
  1768.  *
  1769.  * NB: we assume that unpacked single channel data is directed
  1770.  *     to the "packed routines.
  1771.  */
  1772. static int
  1773. pickTileSeparateCase(TIFFRGBAImage* img)
  1774. {
  1775.     tileSeparateRoutine put = 0;
  1776.  
  1777.     if (buildMap(img)) {
  1778.     switch (img->photometric) {
  1779.     case PHOTOMETRIC_RGB:
  1780.         switch (img->bitspersample) {
  1781.         case 8:
  1782.         if (!img->Map) {
  1783.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  1784.             put = putRGBAAseparate8bittile;
  1785.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  1786.             put = putRGBUAseparate8bittile;
  1787.             else
  1788.             put = putRGBseparate8bittile;
  1789.         } else
  1790.             put = putRGBseparate8bitMaptile;
  1791.         break;
  1792.         case 16:
  1793.         put = putRGBseparate16bittile;
  1794.         if (!img->Map) {
  1795.             if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
  1796.             put = putRGBAAseparate16bittile;
  1797.             else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
  1798.             put = putRGBUAseparate16bittile;
  1799.         }
  1800.         break;
  1801.         }
  1802.         break;
  1803.     }
  1804.     }
  1805.     return ((img->put.separate = put) != 0);
  1806. }
  1807.