LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_getimage.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 1363 285 20.9 %
Date: 2012-12-26 Functions: 60 16 26.7 %

       1                 : /* $Id: tif_getimage.c,v 1.82 2012-06-06 00:17:49 fwarmerdam Exp $ */
       2                 : 
       3                 : /*
       4                 :  * Copyright (c) 1991-1997 Sam Leffler
       5                 :  * Copyright (c) 1991-1997 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 <stdio.h>
      34                 : 
      35                 : static int gtTileContig(TIFFRGBAImage*, uint32*, uint32, uint32);
      36                 : static int gtTileSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
      37                 : static int gtStripContig(TIFFRGBAImage*, uint32*, uint32, uint32);
      38                 : static int gtStripSeparate(TIFFRGBAImage*, uint32*, uint32, uint32);
      39                 : static int PickContigCase(TIFFRGBAImage*);
      40                 : static int PickSeparateCase(TIFFRGBAImage*);
      41                 : 
      42                 : static int BuildMapUaToAa(TIFFRGBAImage* img);
      43                 : static int BuildMapBitdepth16To8(TIFFRGBAImage* img);
      44                 : 
      45                 : static const char photoTag[] = "PhotometricInterpretation";
      46                 : 
      47                 : /* 
      48                 :  * Helper constants used in Orientation tag handling
      49                 :  */
      50                 : #define FLIP_VERTICALLY 0x01
      51                 : #define FLIP_HORIZONTALLY 0x02
      52                 : 
      53                 : /*
      54                 :  * Color conversion constants. We will define display types here.
      55                 :  */
      56                 : 
      57                 : static const TIFFDisplay display_sRGB = {
      58                 :   {     /* XYZ -> luminance matrix */
      59                 :     {  3.2410F, -1.5374F, -0.4986F },
      60                 :     {  -0.9692F, 1.8760F, 0.0416F },
      61                 :     {  0.0556F, -0.2040F, 1.0570F }
      62                 :   },  
      63                 :   100.0F, 100.0F, 100.0F, /* Light o/p for reference white */
      64                 :   255, 255, 255,    /* Pixel values for ref. white */
      65                 :   1.0F, 1.0F, 1.0F, /* Residual light o/p for black pixel */
      66                 :   2.4F, 2.4F, 2.4F, /* Gamma values for the three guns */
      67                 : };
      68                 : 
      69                 : /*
      70                 :  * Check the image to see if TIFFReadRGBAImage can deal with it.
      71                 :  * 1/0 is returned according to whether or not the image can
      72                 :  * be handled.  If 0 is returned, emsg contains the reason
      73                 :  * why it is being rejected.
      74                 :  */
      75                 : int
      76               9 : TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
      77                 : {
      78               9 :   TIFFDirectory* td = &tif->tif_dir;
      79                 :   uint16 photometric;
      80                 :   int colorchannels;
      81                 : 
      82               9 :   if (!tif->tif_decodestatus) {
      83               0 :     sprintf(emsg, "Sorry, requested compression method is not configured");
      84               0 :     return (0);
      85                 :   }
      86               9 :   switch (td->td_bitspersample) {
      87                 :     case 1:
      88                 :     case 2:
      89                 :     case 4:
      90                 :     case 8:
      91                 :     case 16:
      92                 :       break;
      93                 :     default:
      94               0 :       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
      95               0 :           td->td_bitspersample);
      96               0 :       return (0);
      97                 :   }
      98               9 :   colorchannels = td->td_samplesperpixel - td->td_extrasamples;
      99               9 :   if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric)) {
     100               0 :     switch (colorchannels) {
     101                 :       case 1:
     102               0 :         photometric = PHOTOMETRIC_MINISBLACK;
     103               0 :         break;
     104                 :       case 3:
     105               0 :         photometric = PHOTOMETRIC_RGB;
     106               0 :         break;
     107                 :       default:
     108               0 :         sprintf(emsg, "Missing needed %s tag", photoTag);
     109               0 :         return (0);
     110                 :     }
     111                 :   }
     112               9 :   switch (photometric) {
     113                 :     case PHOTOMETRIC_MINISWHITE:
     114                 :     case PHOTOMETRIC_MINISBLACK:
     115                 :     case PHOTOMETRIC_PALETTE:
     116               0 :       if (td->td_planarconfig == PLANARCONFIG_CONTIG
     117               0 :           && td->td_samplesperpixel != 1
     118               0 :           && td->td_bitspersample < 8 ) {
     119               0 :         sprintf(emsg,
     120                 :             "Sorry, can not handle contiguous data with %s=%d, "
     121                 :             "and %s=%d and Bits/Sample=%d",
     122                 :             photoTag, photometric,
     123               0 :             "Samples/pixel", td->td_samplesperpixel,
     124               0 :             td->td_bitspersample);
     125               0 :         return (0);
     126                 :       }
     127                 :       /*
     128                 :        * We should likely validate that any extra samples are either
     129                 :        * to be ignored, or are alpha, and if alpha we should try to use
     130                 :        * them.  But for now we won't bother with this.
     131                 :       */
     132               0 :       break;
     133                 :     case PHOTOMETRIC_YCBCR:
     134                 :       /*
     135                 :        * TODO: if at all meaningful and useful, make more complete
     136                 :        * support check here, or better still, refactor to let supporting
     137                 :        * code decide whether there is support and what meaningfull
     138                 :        * error to return
     139                 :        */
     140               2 :       break;
     141                 :     case PHOTOMETRIC_RGB:
     142               0 :       if (colorchannels < 3) {
     143               0 :         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
     144                 :             "Color channels", colorchannels);
     145               0 :         return (0);
     146                 :       }
     147               0 :       break;
     148                 :     case PHOTOMETRIC_SEPARATED:
     149                 :       {
     150                 :         uint16 inkset;
     151               5 :         TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
     152               5 :         if (inkset != INKSET_CMYK) {
     153               0 :           sprintf(emsg,
     154                 :               "Sorry, can not handle separated image with %s=%d",
     155                 :               "InkSet", inkset);
     156               0 :           return 0;
     157                 :         }
     158               5 :         if (td->td_samplesperpixel < 4) {
     159               0 :           sprintf(emsg,
     160                 :               "Sorry, can not handle separated image with %s=%d",
     161               0 :               "Samples/pixel", td->td_samplesperpixel);
     162               0 :           return 0;
     163                 :         }
     164               5 :         break;
     165                 :       }
     166                 :     case PHOTOMETRIC_LOGL:
     167               0 :       if (td->td_compression != COMPRESSION_SGILOG) {
     168               0 :         sprintf(emsg, "Sorry, LogL data must have %s=%d",
     169                 :             "Compression", COMPRESSION_SGILOG);
     170               0 :         return (0);
     171                 :       }
     172               0 :       break;
     173                 :     case PHOTOMETRIC_LOGLUV:
     174               0 :       if (td->td_compression != COMPRESSION_SGILOG &&
     175               0 :           td->td_compression != COMPRESSION_SGILOG24) {
     176               0 :         sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
     177                 :             "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
     178               0 :         return (0);
     179                 :       }
     180               0 :       if (td->td_planarconfig != PLANARCONFIG_CONTIG) {
     181               0 :         sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
     182               0 :             "Planarconfiguration", td->td_planarconfig);
     183               0 :         return (0);
     184                 :       }
     185               0 :       break;
     186                 :     case PHOTOMETRIC_CIELAB:
     187               2 :       break;
     188                 :     default:
     189               0 :       sprintf(emsg, "Sorry, can not handle image with %s=%d",
     190                 :           photoTag, photometric);
     191               0 :       return (0);
     192                 :   }
     193               9 :   return (1);
     194                 : }
     195                 : 
     196                 : void
     197               4 : TIFFRGBAImageEnd(TIFFRGBAImage* img)
     198                 : {
     199               4 :   if (img->Map)
     200               0 :     _TIFFfree(img->Map), img->Map = NULL;
     201               4 :   if (img->BWmap)
     202               0 :     _TIFFfree(img->BWmap), img->BWmap = NULL;
     203               4 :   if (img->PALmap)
     204               0 :     _TIFFfree(img->PALmap), img->PALmap = NULL;
     205               4 :   if (img->ycbcr)
     206               1 :     _TIFFfree(img->ycbcr), img->ycbcr = NULL;
     207               4 :   if (img->cielab)
     208               1 :     _TIFFfree(img->cielab), img->cielab = NULL;
     209               4 :   if (img->UaToAa)
     210               0 :     _TIFFfree(img->UaToAa), img->UaToAa = NULL;
     211               4 :   if (img->Bitdepth16To8)
     212               0 :     _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
     213                 : 
     214               4 :   if( img->redcmap ) {
     215               0 :     _TIFFfree( img->redcmap );
     216               0 :     _TIFFfree( img->greencmap );
     217               0 :     _TIFFfree( img->bluecmap );
     218               0 :                 img->redcmap = img->greencmap = img->bluecmap = NULL;
     219                 :   }
     220               4 : }
     221                 : 
     222                 : static int
     223               0 : isCCITTCompression(TIFF* tif)
     224                 : {
     225                 :     uint16 compress;
     226               0 :     TIFFGetField(tif, TIFFTAG_COMPRESSION, &compress);
     227               0 :     return (compress == COMPRESSION_CCITTFAX3 ||
     228               0 :       compress == COMPRESSION_CCITTFAX4 ||
     229               0 :       compress == COMPRESSION_CCITTRLE ||
     230               0 :       compress == COMPRESSION_CCITTRLEW);
     231                 : }
     232                 : 
     233                 : int
     234               4 : TIFFRGBAImageBegin(TIFFRGBAImage* img, TIFF* tif, int stop, char emsg[1024])
     235                 : {
     236                 :   uint16* sampleinfo;
     237                 :   uint16 extrasamples;
     238                 :   uint16 planarconfig;
     239                 :   uint16 compress;
     240                 :   int colorchannels;
     241                 :   uint16 *red_orig, *green_orig, *blue_orig;
     242                 :   int n_color;
     243                 : 
     244                 :   /* Initialize to normal values */
     245               4 :   img->row_offset = 0;
     246               4 :   img->col_offset = 0;
     247               4 :   img->redcmap = NULL;
     248               4 :   img->greencmap = NULL;
     249               4 :   img->bluecmap = NULL;
     250               4 :   img->req_orientation = ORIENTATION_BOTLEFT;     /* It is the default */
     251                 : 
     252               4 :   img->tif = tif;
     253               4 :   img->stoponerr = stop;
     254               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
     255               4 :   switch (img->bitspersample) {
     256                 :     case 1:
     257                 :     case 2:
     258                 :     case 4:
     259                 :     case 8:
     260                 :     case 16:
     261                 :       break;
     262                 :     default:
     263               0 :       sprintf(emsg, "Sorry, can not handle images with %d-bit samples",
     264               0 :           img->bitspersample);
     265               0 :       goto fail_return;
     266                 :   }
     267               4 :   img->alpha = 0;
     268               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
     269               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
     270                 :       &extrasamples, &sampleinfo);
     271               4 :   if (extrasamples >= 1)
     272                 :   {
     273               0 :     switch (sampleinfo[0]) {
     274                 :       case EXTRASAMPLE_UNSPECIFIED:          /* Workaround for some images without */
     275               0 :         if (img->samplesperpixel > 3)  /* correct info about alpha channel */
     276               0 :           img->alpha = EXTRASAMPLE_ASSOCALPHA;
     277               0 :         break;
     278                 :       case EXTRASAMPLE_ASSOCALPHA:           /* data is pre-multiplied */
     279                 :       case EXTRASAMPLE_UNASSALPHA:           /* data is not pre-multiplied */
     280               0 :         img->alpha = sampleinfo[0];
     281                 :         break;
     282                 :     }
     283                 :   }
     284                 : 
     285                 : #ifdef DEFAULT_EXTRASAMPLE_AS_ALPHA
     286               4 :   if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
     287               0 :     img->photometric = PHOTOMETRIC_MINISWHITE;
     288                 : 
     289              10 :   if( extrasamples == 0
     290               4 :       && img->samplesperpixel == 4
     291               6 :       && img->photometric == PHOTOMETRIC_RGB )
     292                 :   {
     293               0 :     img->alpha = EXTRASAMPLE_ASSOCALPHA;
     294               0 :     extrasamples = 1;
     295                 :   }
     296                 : #endif
     297                 : 
     298               4 :   colorchannels = img->samplesperpixel - extrasamples;
     299               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
     300               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
     301               4 :   if (!TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric)) {
     302               0 :     switch (colorchannels) {
     303                 :       case 1:
     304               0 :         if (isCCITTCompression(tif))
     305               0 :           img->photometric = PHOTOMETRIC_MINISWHITE;
     306                 :         else
     307               0 :           img->photometric = PHOTOMETRIC_MINISBLACK;
     308               0 :         break;
     309                 :       case 3:
     310               0 :         img->photometric = PHOTOMETRIC_RGB;
     311               0 :         break;
     312                 :       default:
     313               0 :         sprintf(emsg, "Missing needed %s tag", photoTag);
     314               0 :                                 goto fail_return;
     315                 :     }
     316                 :   }
     317               4 :   switch (img->photometric) {
     318                 :     case PHOTOMETRIC_PALETTE:
     319               0 :       if (!TIFFGetField(tif, TIFFTAG_COLORMAP,
     320                 :           &red_orig, &green_orig, &blue_orig)) {
     321               0 :         sprintf(emsg, "Missing required \"Colormap\" tag");
     322               0 :                                 goto fail_return;
     323                 :       }
     324                 : 
     325                 :       /* copy the colormaps so we can modify them */
     326               0 :       n_color = (1L << img->bitspersample);
     327               0 :       img->redcmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
     328               0 :       img->greencmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
     329               0 :       img->bluecmap = (uint16 *) _TIFFmalloc(sizeof(uint16)*n_color);
     330               0 :       if( !img->redcmap || !img->greencmap || !img->bluecmap ) {
     331               0 :         sprintf(emsg, "Out of memory for colormap copy");
     332               0 :                                 goto fail_return;
     333                 :       }
     334                 : 
     335               0 :       _TIFFmemcpy( img->redcmap, red_orig, n_color * 2 );
     336               0 :       _TIFFmemcpy( img->greencmap, green_orig, n_color * 2 );
     337               0 :       _TIFFmemcpy( img->bluecmap, blue_orig, n_color * 2 );
     338                 : 
     339                 :       /* fall thru... */
     340                 :     case PHOTOMETRIC_MINISWHITE:
     341                 :     case PHOTOMETRIC_MINISBLACK:
     342               0 :       if (planarconfig == PLANARCONFIG_CONTIG
     343               0 :           && img->samplesperpixel != 1
     344               0 :           && img->bitspersample < 8 ) {
     345               0 :         sprintf(emsg,
     346                 :             "Sorry, can not handle contiguous data with %s=%d, "
     347                 :             "and %s=%d and Bits/Sample=%d",
     348               0 :             photoTag, img->photometric,
     349               0 :             "Samples/pixel", img->samplesperpixel,
     350               0 :             img->bitspersample);
     351               0 :                                 goto fail_return;
     352                 :       }
     353               0 :       break;
     354                 :     case PHOTOMETRIC_YCBCR:
     355                 :       /* It would probably be nice to have a reality check here. */
     356               1 :       if (planarconfig == PLANARCONFIG_CONTIG)
     357                 :         /* can rely on libjpeg to convert to RGB */
     358                 :         /* XXX should restore current state on exit */
     359               1 :         switch (compress) {
     360                 :           case COMPRESSION_JPEG:
     361                 :             /*
     362                 :              * TODO: when complete tests verify complete desubsampling
     363                 :              * and YCbCr handling, remove use of TIFFTAG_JPEGCOLORMODE in
     364                 :              * favor of tif_getimage.c native handling
     365                 :              */
     366               0 :             TIFFSetField(tif, TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RGB);
     367               0 :             img->photometric = PHOTOMETRIC_RGB;
     368                 :             break;
     369                 :           default:
     370                 :             /* do nothing */;
     371                 :             break;
     372                 :         }
     373                 :       /*
     374                 :        * TODO: if at all meaningful and useful, make more complete
     375                 :        * support check here, or better still, refactor to let supporting
     376                 :        * code decide whether there is support and what meaningfull
     377                 :        * error to return
     378                 :        */
     379               1 :       break;
     380                 :     case PHOTOMETRIC_RGB:
     381               0 :       if (colorchannels < 3) {
     382               0 :         sprintf(emsg, "Sorry, can not handle RGB image with %s=%d",
     383                 :             "Color channels", colorchannels);
     384               0 :                                 goto fail_return;
     385                 :       }
     386               0 :       break;
     387                 :     case PHOTOMETRIC_SEPARATED:
     388                 :       {
     389                 :         uint16 inkset;
     390               2 :         TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
     391               2 :         if (inkset != INKSET_CMYK) {
     392               0 :           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
     393                 :               "InkSet", inkset);
     394               0 :                                         goto fail_return;
     395                 :         }
     396               2 :         if (img->samplesperpixel < 4) {
     397               0 :           sprintf(emsg, "Sorry, can not handle separated image with %s=%d",
     398               0 :               "Samples/pixel", img->samplesperpixel);
     399               0 :                                         goto fail_return;
     400                 :         }
     401                 :       }
     402               2 :       break;
     403                 :     case PHOTOMETRIC_LOGL:
     404               0 :       if (compress != COMPRESSION_SGILOG) {
     405               0 :         sprintf(emsg, "Sorry, LogL data must have %s=%d",
     406                 :             "Compression", COMPRESSION_SGILOG);
     407               0 :                                 goto fail_return;
     408                 :       }
     409               0 :       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
     410               0 :       img->photometric = PHOTOMETRIC_MINISBLACK; /* little white lie */
     411               0 :       img->bitspersample = 8;
     412               0 :       break;
     413                 :     case PHOTOMETRIC_LOGLUV:
     414               0 :       if (compress != COMPRESSION_SGILOG && compress != COMPRESSION_SGILOG24) {
     415               0 :         sprintf(emsg, "Sorry, LogLuv data must have %s=%d or %d",
     416                 :             "Compression", COMPRESSION_SGILOG, COMPRESSION_SGILOG24);
     417               0 :                                 goto fail_return;
     418                 :       }
     419               0 :       if (planarconfig != PLANARCONFIG_CONTIG) {
     420               0 :         sprintf(emsg, "Sorry, can not handle LogLuv images with %s=%d",
     421                 :             "Planarconfiguration", planarconfig);
     422               0 :         return (0);
     423                 :       }
     424               0 :       TIFFSetField(tif, TIFFTAG_SGILOGDATAFMT, SGILOGDATAFMT_8BIT);
     425               0 :       img->photometric = PHOTOMETRIC_RGB;    /* little white lie */
     426               0 :       img->bitspersample = 8;
     427               0 :       break;
     428                 :     case PHOTOMETRIC_CIELAB:
     429               1 :       break;
     430                 :     default:
     431               0 :       sprintf(emsg, "Sorry, can not handle image with %s=%d",
     432               0 :           photoTag, img->photometric);
     433               0 :                         goto fail_return;
     434                 :   }
     435               4 :   img->Map = NULL;
     436               4 :   img->BWmap = NULL;
     437               4 :   img->PALmap = NULL;
     438               4 :   img->ycbcr = NULL;
     439               4 :   img->cielab = NULL;
     440               4 :   img->UaToAa = NULL;
     441               4 :   img->Bitdepth16To8 = NULL;
     442               4 :   TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
     443               4 :   TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
     444               4 :   TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
     445               4 :   img->isContig =
     446               4 :       !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
     447               4 :   if (img->isContig) {
     448               4 :     if (!PickContigCase(img)) {
     449               0 :       sprintf(emsg, "Sorry, can not handle image");
     450               0 :       goto fail_return;
     451                 :     }
     452                 :   } else {
     453               0 :     if (!PickSeparateCase(img)) {
     454               0 :       sprintf(emsg, "Sorry, can not handle image");
     455               0 :       goto fail_return;
     456                 :     }
     457                 :   }
     458               4 :   return 1;
     459                 : 
     460                 :   fail_return:
     461               0 :         _TIFFfree( img->redcmap );
     462               0 :         _TIFFfree( img->greencmap );
     463               0 :         _TIFFfree( img->bluecmap );
     464               0 :         img->redcmap = img->greencmap = img->bluecmap = NULL;
     465               0 :         return 0;
     466                 : }
     467                 : 
     468                 : int
     469               4 : TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     470                 : {
     471               4 :     if (img->get == NULL) {
     472               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
     473               0 :     return (0);
     474                 :   }
     475               4 :   if (img->put.any == NULL) {
     476               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
     477                 :     "No \"put\" routine setupl; probably can not handle image format");
     478               0 :     return (0);
     479                 :     }
     480               4 :     return (*img->get)(img, raster, w, h);
     481                 : }
     482                 : 
     483                 : /*
     484                 :  * Read the specified image into an ABGR-format rastertaking in account
     485                 :  * specified orientation.
     486                 :  */
     487                 : int
     488               0 : TIFFReadRGBAImageOriented(TIFF* tif,
     489                 :         uint32 rwidth, uint32 rheight, uint32* raster,
     490                 :         int orientation, int stop)
     491                 : {
     492               0 :     char emsg[1024] = "";
     493                 :     TIFFRGBAImage img;
     494                 :     int ok;
     495                 : 
     496               0 :   if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, stop, emsg)) {
     497               0 :     img.req_orientation = orientation;
     498                 :     /* XXX verify rwidth and rheight against width and height */
     499               0 :     ok = TIFFRGBAImageGet(&img, raster+(rheight-img.height)*rwidth,
     500                 :       rwidth, img.height);
     501               0 :     TIFFRGBAImageEnd(&img);
     502                 :   } else {
     503               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
     504               0 :     ok = 0;
     505                 :     }
     506               0 :     return (ok);
     507                 : }
     508                 : 
     509                 : /*
     510                 :  * Read the specified image into an ABGR-format raster. Use bottom left
     511                 :  * origin for raster by default.
     512                 :  */
     513                 : int
     514               0 : TIFFReadRGBAImage(TIFF* tif,
     515                 :       uint32 rwidth, uint32 rheight, uint32* raster, int stop)
     516                 : {
     517               0 :   return TIFFReadRGBAImageOriented(tif, rwidth, rheight, raster,
     518                 :            ORIENTATION_BOTLEFT, stop);
     519                 : }
     520                 : 
     521                 : static int 
     522               4 : setorientation(TIFFRGBAImage* img)
     523                 : {
     524               4 :   switch (img->orientation) {
     525                 :     case ORIENTATION_TOPLEFT:
     526                 :     case ORIENTATION_LEFTTOP:
     527               8 :       if (img->req_orientation == ORIENTATION_TOPRIGHT ||
     528               4 :           img->req_orientation == ORIENTATION_RIGHTTOP)
     529               0 :         return FLIP_HORIZONTALLY;
     530               8 :       else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
     531               4 :           img->req_orientation == ORIENTATION_RIGHTBOT)
     532               0 :         return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
     533               4 :       else if (img->req_orientation == ORIENTATION_BOTLEFT ||
     534               0 :           img->req_orientation == ORIENTATION_LEFTBOT)
     535               4 :         return FLIP_VERTICALLY;
     536                 :       else
     537               0 :         return 0;
     538                 :     case ORIENTATION_TOPRIGHT:
     539                 :     case ORIENTATION_RIGHTTOP:
     540               0 :       if (img->req_orientation == ORIENTATION_TOPLEFT ||
     541               0 :           img->req_orientation == ORIENTATION_LEFTTOP)
     542               0 :         return FLIP_HORIZONTALLY;
     543               0 :       else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
     544               0 :           img->req_orientation == ORIENTATION_RIGHTBOT)
     545               0 :         return FLIP_VERTICALLY;
     546               0 :       else if (img->req_orientation == ORIENTATION_BOTLEFT ||
     547               0 :           img->req_orientation == ORIENTATION_LEFTBOT)
     548               0 :         return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
     549                 :       else
     550               0 :         return 0;
     551                 :     case ORIENTATION_BOTRIGHT:
     552                 :     case ORIENTATION_RIGHTBOT:
     553               0 :       if (img->req_orientation == ORIENTATION_TOPLEFT ||
     554               0 :           img->req_orientation == ORIENTATION_LEFTTOP)
     555               0 :         return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
     556               0 :       else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
     557               0 :           img->req_orientation == ORIENTATION_RIGHTTOP)
     558               0 :         return FLIP_VERTICALLY;
     559               0 :       else if (img->req_orientation == ORIENTATION_BOTLEFT ||
     560               0 :           img->req_orientation == ORIENTATION_LEFTBOT)
     561               0 :         return FLIP_HORIZONTALLY;
     562                 :       else
     563               0 :         return 0;
     564                 :     case ORIENTATION_BOTLEFT:
     565                 :     case ORIENTATION_LEFTBOT:
     566               0 :       if (img->req_orientation == ORIENTATION_TOPLEFT ||
     567               0 :           img->req_orientation == ORIENTATION_LEFTTOP)
     568               0 :         return FLIP_VERTICALLY;
     569               0 :       else if (img->req_orientation == ORIENTATION_TOPRIGHT ||
     570               0 :           img->req_orientation == ORIENTATION_RIGHTTOP)
     571               0 :         return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
     572               0 :       else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
     573               0 :           img->req_orientation == ORIENTATION_RIGHTBOT)
     574               0 :         return FLIP_HORIZONTALLY;
     575                 :       else
     576               0 :         return 0;
     577                 :     default:  /* NOTREACHED */
     578               0 :       return 0;
     579                 :   }
     580                 : }
     581                 : 
     582                 : /*
     583                 :  * Get an tile-organized image that has
     584                 :  *  PlanarConfiguration contiguous if SamplesPerPixel > 1
     585                 :  * or
     586                 :  *  SamplesPerPixel == 1
     587                 :  */ 
     588                 : static int
     589               1 : gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     590                 : {
     591               1 :     TIFF* tif = img->tif;
     592               1 :     tileContigRoutine put = img->put.contig;
     593                 :     uint32 col, row, y, rowstoread;
     594                 :     tmsize_t pos;
     595                 :     uint32 tw, th;
     596                 :     unsigned char* buf;
     597                 :     int32 fromskew, toskew;
     598                 :     uint32 nrow;
     599               1 :     int ret = 1, flip;
     600                 : 
     601               1 :     buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
     602               1 :     if (buf == 0) {
     603               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
     604               0 :     return (0);
     605                 :     }
     606               1 :     _TIFFmemset(buf, 0, TIFFTileSize(tif));
     607               1 :     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
     608               1 :     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
     609                 : 
     610               1 :     flip = setorientation(img);
     611               1 :     if (flip & FLIP_VERTICALLY) {
     612               1 :       y = h - 1;
     613               1 :       toskew = -(int32)(tw + w);
     614                 :     }
     615                 :     else {
     616               0 :       y = 0;
     617               0 :       toskew = -(int32)(tw - w);
     618                 :     }
     619                 :      
     620               2 :     for (row = 0; row < h; row += nrow)
     621                 :     {
     622               1 :         rowstoread = th - (row + img->row_offset) % th;
     623               1 :       nrow = (row + rowstoread > h ? h - row : rowstoread);
     624               2 :   for (col = 0; col < w; col += tw) 
     625                 :         {
     626               1 :       if (TIFFReadTile(tif, buf, col+img->col_offset,  
     627               1 :            row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
     628                 :             {
     629               0 :                 ret = 0;
     630               0 :                 break;
     631                 :             }
     632                 :       
     633               1 :       pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
     634                 : 
     635               1 :           if (col + tw > w) 
     636                 :             {
     637                 :                 /*
     638                 :                  * Tile is clipped horizontally.  Calculate
     639                 :                  * visible portion and skewing factors.
     640                 :                  */
     641               1 :                 uint32 npix = w - col;
     642               1 :                 fromskew = tw - npix;
     643               1 :                 (*put)(img, raster+y*w+col, col, y,
     644                 :                        npix, nrow, fromskew, toskew + fromskew, buf + pos);
     645                 :             }
     646                 :             else 
     647                 :             {
     648               0 :                 (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);
     649                 :             }
     650                 :         }
     651                 : 
     652               1 :         y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
     653                 :     }
     654               1 :     _TIFFfree(buf);
     655                 : 
     656               1 :     if (flip & FLIP_HORIZONTALLY) {
     657                 :       uint32 line;
     658                 : 
     659               0 :       for (line = 0; line < h; line++) {
     660               0 :         uint32 *left = raster + (line * w);
     661               0 :         uint32 *right = left + w - 1;
     662                 :         
     663               0 :         while ( left < right ) {
     664               0 :           uint32 temp = *left;
     665               0 :           *left = *right;
     666               0 :           *right = temp;
     667               0 :           left++, right--;
     668                 :         }
     669                 :       }
     670                 :     }
     671                 : 
     672               1 :     return (ret);
     673                 : }
     674                 : 
     675                 : /*
     676                 :  * Get an tile-organized image that has
     677                 :  *   SamplesPerPixel > 1
     678                 :  *   PlanarConfiguration separated
     679                 :  * We assume that all such images are RGB.
     680                 :  */ 
     681                 : static int
     682               0 : gtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     683                 : {
     684               0 :   TIFF* tif = img->tif;
     685               0 :   tileSeparateRoutine put = img->put.separate;
     686                 :   uint32 col, row, y, rowstoread;
     687                 :   tmsize_t pos;
     688                 :   uint32 tw, th;
     689                 :   unsigned char* buf;
     690                 :   unsigned char* p0;
     691                 :   unsigned char* p1;
     692                 :   unsigned char* p2;
     693                 :   unsigned char* pa;
     694                 :   tmsize_t tilesize;
     695                 :   tmsize_t bufsize;
     696                 :   int32 fromskew, toskew;
     697               0 :   int alpha = img->alpha;
     698                 :   uint32 nrow;
     699               0 :   int ret = 1, flip;
     700                 :         int colorchannels;
     701                 : 
     702               0 :   tilesize = TIFFTileSize(tif);  
     703               0 :   bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,tilesize);
     704               0 :   if (bufsize == 0) {
     705               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtTileSeparate");
     706               0 :     return (0);
     707                 :   }
     708               0 :   buf = (unsigned char*) _TIFFmalloc(bufsize);
     709               0 :   if (buf == 0) {
     710               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
     711               0 :     return (0);
     712                 :   }
     713               0 :   _TIFFmemset(buf, 0, bufsize);
     714               0 :   p0 = buf;
     715               0 :   p1 = p0 + tilesize;
     716               0 :   p2 = p1 + tilesize;
     717               0 :   pa = (alpha?(p2+tilesize):NULL);
     718               0 :   TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
     719               0 :   TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
     720                 : 
     721               0 :   flip = setorientation(img);
     722               0 :   if (flip & FLIP_VERTICALLY) {
     723               0 :     y = h - 1;
     724               0 :     toskew = -(int32)(tw + w);
     725                 :   }
     726                 :   else {
     727               0 :     y = 0;
     728               0 :     toskew = -(int32)(tw - w);
     729                 :   }
     730                 : 
     731               0 :         switch( img->photometric )
     732                 :         {
     733                 :           case PHOTOMETRIC_MINISWHITE:
     734                 :           case PHOTOMETRIC_MINISBLACK:
     735                 :           case PHOTOMETRIC_PALETTE:
     736               0 :             colorchannels = 1;
     737               0 :             p2 = p1 = p0;
     738               0 :             break;
     739                 : 
     740                 :           default:
     741               0 :             colorchannels = 3;
     742                 :             break;
     743                 :         }
     744                 : 
     745               0 :   for (row = 0; row < h; row += nrow)
     746                 :   {
     747               0 :     rowstoread = th - (row + img->row_offset) % th;
     748               0 :     nrow = (row + rowstoread > h ? h - row : rowstoread);
     749               0 :     for (col = 0; col < w; col += tw)
     750                 :     {
     751               0 :       if (TIFFReadTile(tif, p0, col+img->col_offset,  
     752               0 :           row+img->row_offset,0,0)==(tmsize_t)(-1) && img->stoponerr)
     753                 :       {
     754               0 :         ret = 0;
     755               0 :         break;
     756                 :       }
     757               0 :       if (colorchannels > 1 
     758                 :                             && TIFFReadTile(tif, p1, col+img->col_offset,  
     759                 :                                             row+img->row_offset,0,1) == (tmsize_t)(-1) 
     760               0 :                             && img->stoponerr)
     761                 :       {
     762               0 :         ret = 0;
     763               0 :         break;
     764                 :       }
     765               0 :       if (colorchannels > 1 
     766                 :                             && TIFFReadTile(tif, p2, col+img->col_offset,  
     767                 :                                             row+img->row_offset,0,2) == (tmsize_t)(-1) 
     768               0 :                             && img->stoponerr)
     769                 :       {
     770               0 :         ret = 0;
     771               0 :         break;
     772                 :       }
     773               0 :       if (alpha
     774                 :                             && TIFFReadTile(tif,pa,col+img->col_offset,  
     775                 :                                             row+img->row_offset,0,colorchannels) == (tmsize_t)(-1) 
     776               0 :                             && img->stoponerr)
     777                 :                         {
     778               0 :                             ret = 0;
     779               0 :                             break;
     780                 :       }
     781                 : 
     782               0 :       pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
     783                 : 
     784               0 :       if (col + tw > w)
     785                 :       {
     786                 :         /*
     787                 :          * Tile is clipped horizontally.  Calculate
     788                 :          * visible portion and skewing factors.
     789                 :          */
     790               0 :         uint32 npix = w - col;
     791               0 :         fromskew = tw - npix;
     792               0 :         (*put)(img, raster+y*w+col, col, y,
     793                 :             npix, nrow, fromskew, toskew + fromskew,
     794               0 :             p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
     795                 :       } else {
     796               0 :         (*put)(img, raster+y*w+col, col, y,
     797               0 :             tw, nrow, 0, toskew, p0 + pos, p1 + pos, p2 + pos, (alpha?(pa+pos):NULL));
     798                 :       }
     799                 :     }
     800                 : 
     801               0 :     y += (flip & FLIP_VERTICALLY ?-(int32) nrow : (int32) nrow);
     802                 :   }
     803                 : 
     804               0 :   if (flip & FLIP_HORIZONTALLY) {
     805                 :     uint32 line;
     806                 : 
     807               0 :     for (line = 0; line < h; line++) {
     808               0 :       uint32 *left = raster + (line * w);
     809               0 :       uint32 *right = left + w - 1;
     810                 : 
     811               0 :       while ( left < right ) {
     812               0 :         uint32 temp = *left;
     813               0 :         *left = *right;
     814               0 :         *right = temp;
     815               0 :         left++, right--;
     816                 :       }
     817                 :     }
     818                 :   }
     819                 : 
     820               0 :   _TIFFfree(buf);
     821               0 :   return (ret);
     822                 : }
     823                 : 
     824                 : /*
     825                 :  * Get a strip-organized image that has
     826                 :  *  PlanarConfiguration contiguous if SamplesPerPixel > 1
     827                 :  * or
     828                 :  *  SamplesPerPixel == 1
     829                 :  */ 
     830                 : static int
     831               3 : gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     832                 : {
     833               3 :   TIFF* tif = img->tif;
     834               3 :   tileContigRoutine put = img->put.contig;
     835                 :   uint32 row, y, nrow, nrowsub, rowstoread;
     836                 :   tmsize_t pos;
     837                 :   unsigned char* buf;
     838                 :   uint32 rowsperstrip;
     839                 :   uint16 subsamplinghor,subsamplingver;
     840               3 :   uint32 imagewidth = img->width;
     841                 :   tmsize_t scanline;
     842                 :   int32 fromskew, toskew;
     843               3 :   int ret = 1, flip;
     844                 : 
     845               3 :   buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
     846               3 :   if (buf == 0) {
     847               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
     848               0 :     return (0);
     849                 :   }
     850               3 :   _TIFFmemset(buf, 0, TIFFStripSize(tif));
     851                 : 
     852               3 :   flip = setorientation(img);
     853               3 :   if (flip & FLIP_VERTICALLY) {
     854               3 :     y = h - 1;
     855               3 :     toskew = -(int32)(w + w);
     856                 :   } else {
     857               0 :     y = 0;
     858               0 :     toskew = -(int32)(w - w);
     859                 :   }
     860                 : 
     861               3 :   TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
     862               3 :   TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
     863               3 :   scanline = TIFFScanlineSize(tif);
     864               3 :   fromskew = (w < imagewidth ? imagewidth - w : 0);
     865               6 :   for (row = 0; row < h; row += nrow)
     866                 :   {
     867               3 :     rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
     868               3 :     nrow = (row + rowstoread > h ? h - row : rowstoread);
     869               3 :     nrowsub = nrow;
     870               3 :     if ((nrowsub%subsamplingver)!=0)
     871               1 :       nrowsub+=subsamplingver-nrowsub%subsamplingver;
     872               3 :     if (TIFFReadEncodedStrip(tif,
     873                 :         TIFFComputeStrip(tif,row+img->row_offset, 0),
     874                 :         buf,
     875               3 :         ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
     876               3 :         && img->stoponerr)
     877                 :     {
     878               0 :       ret = 0;
     879               0 :       break;
     880                 :     }
     881                 : 
     882               3 :     pos = ((row + img->row_offset) % rowsperstrip) * scanline;
     883               3 :     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
     884               3 :     y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
     885                 :   }
     886                 : 
     887               3 :   if (flip & FLIP_HORIZONTALLY) {
     888                 :     uint32 line;
     889                 : 
     890               0 :     for (line = 0; line < h; line++) {
     891               0 :       uint32 *left = raster + (line * w);
     892               0 :       uint32 *right = left + w - 1;
     893                 : 
     894               0 :       while ( left < right ) {
     895               0 :         uint32 temp = *left;
     896               0 :         *left = *right;
     897               0 :         *right = temp;
     898               0 :         left++, right--;
     899                 :       }
     900                 :     }
     901                 :   }
     902                 : 
     903               3 :   _TIFFfree(buf);
     904               3 :   return (ret);
     905                 : }
     906                 : 
     907                 : /*
     908                 :  * Get a strip-organized image with
     909                 :  *   SamplesPerPixel > 1
     910                 :  *   PlanarConfiguration separated
     911                 :  * We assume that all such images are RGB.
     912                 :  */
     913                 : static int
     914               0 : gtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     915                 : {
     916               0 :   TIFF* tif = img->tif;
     917               0 :   tileSeparateRoutine put = img->put.separate;
     918                 :   unsigned char *buf;
     919                 :   unsigned char *p0, *p1, *p2, *pa;
     920                 :   uint32 row, y, nrow, rowstoread;
     921                 :   tmsize_t pos;
     922                 :   tmsize_t scanline;
     923                 :   uint32 rowsperstrip, offset_row;
     924               0 :   uint32 imagewidth = img->width;
     925                 :   tmsize_t stripsize;
     926                 :   tmsize_t bufsize;
     927                 :   int32 fromskew, toskew;
     928               0 :   int alpha = img->alpha;
     929               0 :   int ret = 1, flip, colorchannels;
     930                 : 
     931               0 :   stripsize = TIFFStripSize(tif);  
     932               0 :   bufsize = TIFFSafeMultiply(tmsize_t,alpha?4:3,stripsize);
     933               0 :   if (bufsize == 0) {
     934               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "Integer overflow in %s", "gtStripSeparate");
     935               0 :     return (0);
     936                 :   }
     937               0 :   p0 = buf = (unsigned char *)_TIFFmalloc(bufsize);
     938               0 :   if (buf == 0) {
     939               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for tile buffer");
     940               0 :     return (0);
     941                 :   }
     942               0 :   _TIFFmemset(buf, 0, bufsize);
     943               0 :   p1 = p0 + stripsize;
     944               0 :   p2 = p1 + stripsize;
     945               0 :   pa = (alpha?(p2+stripsize):NULL);
     946                 : 
     947               0 :   flip = setorientation(img);
     948               0 :   if (flip & FLIP_VERTICALLY) {
     949               0 :     y = h - 1;
     950               0 :     toskew = -(int32)(w + w);
     951                 :   }
     952                 :   else {
     953               0 :     y = 0;
     954               0 :     toskew = -(int32)(w - w);
     955                 :   }
     956                 : 
     957               0 :         switch( img->photometric )
     958                 :         {
     959                 :           case PHOTOMETRIC_MINISWHITE:
     960                 :           case PHOTOMETRIC_MINISBLACK:
     961                 :           case PHOTOMETRIC_PALETTE:
     962               0 :             colorchannels = 1;
     963               0 :             p2 = p1 = p0;
     964               0 :             break;
     965                 : 
     966                 :           default:
     967               0 :             colorchannels = 3;
     968                 :             break;
     969                 :         }
     970                 : 
     971               0 :   TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
     972               0 :   scanline = TIFFScanlineSize(tif);  
     973               0 :   fromskew = (w < imagewidth ? imagewidth - w : 0);
     974               0 :   for (row = 0; row < h; row += nrow)
     975                 :   {
     976               0 :     rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
     977               0 :     nrow = (row + rowstoread > h ? h - row : rowstoread);
     978               0 :     offset_row = row + img->row_offset;
     979               0 :     if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),
     980               0 :         p0, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
     981               0 :         && img->stoponerr)
     982                 :     {
     983               0 :       ret = 0;
     984               0 :       break;
     985                 :     }
     986               0 :     if (colorchannels > 1 
     987                 :                     && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),
     988               0 :                                             p1, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
     989               0 :         && img->stoponerr)
     990                 :     {
     991               0 :       ret = 0;
     992               0 :       break;
     993                 :     }
     994               0 :     if (colorchannels > 1 
     995                 :                     && TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),
     996               0 :                                             p2, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) == (tmsize_t)(-1)
     997               0 :         && img->stoponerr)
     998                 :     {
     999               0 :       ret = 0;
    1000               0 :       break;
    1001                 :     }
    1002               0 :     if (alpha)
    1003                 :     {
    1004               0 :       if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, colorchannels),
    1005               0 :           pa, ((row + img->row_offset)%rowsperstrip + nrow) * scanline)==(tmsize_t)(-1)
    1006               0 :           && img->stoponerr)
    1007                 :       {
    1008               0 :         ret = 0;
    1009               0 :         break;
    1010                 :       }
    1011                 :     }
    1012                 : 
    1013               0 :     pos = ((row + img->row_offset) % rowsperstrip) * scanline;
    1014               0 :     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, p0 + pos, p1 + pos,
    1015               0 :         p2 + pos, (alpha?(pa+pos):NULL));
    1016               0 :     y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
    1017                 :   }
    1018                 : 
    1019               0 :   if (flip & FLIP_HORIZONTALLY) {
    1020                 :     uint32 line;
    1021                 : 
    1022               0 :     for (line = 0; line < h; line++) {
    1023               0 :       uint32 *left = raster + (line * w);
    1024               0 :       uint32 *right = left + w - 1;
    1025                 : 
    1026               0 :       while ( left < right ) {
    1027               0 :         uint32 temp = *left;
    1028               0 :         *left = *right;
    1029               0 :         *right = temp;
    1030               0 :         left++, right--;
    1031                 :       }
    1032                 :     }
    1033                 :   }
    1034                 : 
    1035               0 :   _TIFFfree(buf);
    1036               0 :   return (ret);
    1037                 : }
    1038                 : 
    1039                 : /*
    1040                 :  * The following routines move decoded data returned
    1041                 :  * from the TIFF library into rasters filled with packed
    1042                 :  * ABGR pixels (i.e. suitable for passing to lrecwrite.)
    1043                 :  *
    1044                 :  * The routines have been created according to the most
    1045                 :  * important cases and optimized.  PickContigCase and
    1046                 :  * PickSeparateCase analyze the parameters and select
    1047                 :  * the appropriate "get" and "put" routine to use.
    1048                 :  */
    1049                 : #define REPEAT8(op) REPEAT4(op); REPEAT4(op)
    1050                 : #define REPEAT4(op) REPEAT2(op); REPEAT2(op)
    1051                 : #define REPEAT2(op) op; op
    1052                 : #define CASE8(x,op)     \
    1053                 :     switch (x) {      \
    1054                 :     case 7: op; case 6: op; case 5: op; \
    1055                 :     case 4: op; case 3: op; case 2: op; \
    1056                 :     case 1: op;       \
    1057                 :     }
    1058                 : #define CASE4(x,op) switch (x) { case 3: op; case 2: op; case 1: op; }
    1059                 : #define NOP
    1060                 : 
    1061                 : #define UNROLL8(w, op1, op2) {    \
    1062                 :     uint32 _x;        \
    1063                 :     for (_x = w; _x >= 8; _x -= 8) { \
    1064                 :   op1;        \
    1065                 :   REPEAT8(op2);     \
    1066                 :     }         \
    1067                 :     if (_x > 0) {      \
    1068                 :   op1;        \
    1069                 :   CASE8(_x,op2);      \
    1070                 :     }         \
    1071                 : }
    1072                 : #define UNROLL4(w, op1, op2) {    \
    1073                 :     uint32 _x;        \
    1074                 :     for (_x = w; _x >= 4; _x -= 4) { \
    1075                 :   op1;        \
    1076                 :   REPEAT4(op2);     \
    1077                 :     }         \
    1078                 :     if (_x > 0) {      \
    1079                 :   op1;        \
    1080                 :   CASE4(_x,op2);      \
    1081                 :     }         \
    1082                 : }
    1083                 : #define UNROLL2(w, op1, op2) {    \
    1084                 :     uint32 _x;        \
    1085                 :     for (_x = w; _x >= 2; _x -= 2) { \
    1086                 :   op1;        \
    1087                 :   REPEAT2(op2);     \
    1088                 :     }         \
    1089                 :     if (_x) {       \
    1090                 :   op1;        \
    1091                 :   op2;        \
    1092                 :     }         \
    1093                 : }
    1094                 :     
    1095                 : #define SKEW(r,g,b,skew)  { r += skew; g += skew; b += skew; }
    1096                 : #define SKEW4(r,g,b,a,skew) { r += skew; g += skew; b += skew; a+= skew; }
    1097                 : 
    1098                 : #define A1 (((uint32)0xffL)<<24)
    1099                 : #define PACK(r,g,b) \
    1100                 :   ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)
    1101                 : #define PACK4(r,g,b,a)  \
    1102                 :   ((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))
    1103                 : #define W2B(v) (((v)>>8)&0xff)
    1104                 : /* TODO: PACKW should have be made redundant in favor of Bitdepth16To8 LUT */
    1105                 : #define PACKW(r,g,b)  \
    1106                 :   ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)
    1107                 : #define PACKW4(r,g,b,a) \
    1108                 :   ((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))
    1109                 : 
    1110                 : #define DECLAREContigPutFunc(name) \
    1111                 : static void name(\
    1112                 :     TIFFRGBAImage* img, \
    1113                 :     uint32* cp, \
    1114                 :     uint32 x, uint32 y, \
    1115                 :     uint32 w, uint32 h, \
    1116                 :     int32 fromskew, int32 toskew, \
    1117                 :     unsigned char* pp \
    1118                 : )
    1119                 : 
    1120                 : /*
    1121                 :  * 8-bit palette => colormap/RGB
    1122                 :  */
    1123               0 : DECLAREContigPutFunc(put8bitcmaptile)
    1124                 : {
    1125               0 :     uint32** PALmap = img->PALmap;
    1126               0 :     int samplesperpixel = img->samplesperpixel;
    1127                 : 
    1128                 :     (void) y;
    1129               0 :     while (h-- > 0) {
    1130               0 :   for (x = w; x-- > 0;)
    1131                 :         {
    1132               0 :       *cp++ = PALmap[*pp][0];
    1133               0 :             pp += samplesperpixel;
    1134                 :         }
    1135               0 :   cp += toskew;
    1136               0 :   pp += fromskew;
    1137                 :     }
    1138               0 : }
    1139                 : 
    1140                 : /*
    1141                 :  * 4-bit palette => colormap/RGB
    1142                 :  */
    1143               0 : DECLAREContigPutFunc(put4bitcmaptile)
    1144                 : {
    1145               0 :     uint32** PALmap = img->PALmap;
    1146                 : 
    1147                 :     (void) x; (void) y;
    1148               0 :     fromskew /= 2;
    1149               0 :     while (h-- > 0) {
    1150                 :   uint32* bw;
    1151               0 :   UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);
    1152               0 :   cp += toskew;
    1153               0 :   pp += fromskew;
    1154                 :     }
    1155               0 : }
    1156                 : 
    1157                 : /*
    1158                 :  * 2-bit palette => colormap/RGB
    1159                 :  */
    1160               0 : DECLAREContigPutFunc(put2bitcmaptile)
    1161                 : {
    1162               0 :     uint32** PALmap = img->PALmap;
    1163                 : 
    1164                 :     (void) x; (void) y;
    1165               0 :     fromskew /= 4;
    1166               0 :     while (h-- > 0) {
    1167                 :   uint32* bw;
    1168               0 :   UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);
    1169               0 :   cp += toskew;
    1170               0 :   pp += fromskew;
    1171                 :     }
    1172               0 : }
    1173                 : 
    1174                 : /*
    1175                 :  * 1-bit palette => colormap/RGB
    1176                 :  */
    1177               0 : DECLAREContigPutFunc(put1bitcmaptile)
    1178                 : {
    1179               0 :     uint32** PALmap = img->PALmap;
    1180                 : 
    1181                 :     (void) x; (void) y;
    1182               0 :     fromskew /= 8;
    1183               0 :     while (h-- > 0) {
    1184                 :   uint32* bw;
    1185               0 :   UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);
    1186               0 :   cp += toskew;
    1187               0 :   pp += fromskew;
    1188                 :     }
    1189               0 : }
    1190                 : 
    1191                 : /*
    1192                 :  * 8-bit greyscale => colormap/RGB
    1193                 :  */
    1194               0 : DECLAREContigPutFunc(putgreytile)
    1195                 : {
    1196               0 :     int samplesperpixel = img->samplesperpixel;
    1197               0 :     uint32** BWmap = img->BWmap;
    1198                 : 
    1199                 :     (void) y;
    1200               0 :     while (h-- > 0) {
    1201               0 :   for (x = w; x-- > 0;)
    1202                 :         {
    1203               0 :       *cp++ = BWmap[*pp][0];
    1204               0 :             pp += samplesperpixel;
    1205                 :         }
    1206               0 :   cp += toskew;
    1207               0 :   pp += fromskew;
    1208                 :     }
    1209               0 : }
    1210                 : 
    1211                 : /*
    1212                 :  * 8-bit greyscale with associated alpha => colormap/RGBA
    1213                 :  */
    1214               0 : DECLAREContigPutFunc(putagreytile)
    1215                 : {
    1216               0 :     int samplesperpixel = img->samplesperpixel;
    1217               0 :     uint32** BWmap = img->BWmap;
    1218                 : 
    1219                 :     (void) y;
    1220               0 :     while (h-- > 0) {
    1221               0 :   for (x = w; x-- > 0;)
    1222                 :         {
    1223               0 :             *cp++ = BWmap[*pp][0] & (*(pp+1) << 24 | ~A1);
    1224               0 :             pp += samplesperpixel;
    1225                 :         }
    1226               0 :   cp += toskew;
    1227               0 :   pp += fromskew;
    1228                 :     }
    1229               0 : }
    1230                 : 
    1231                 : /*
    1232                 :  * 16-bit greyscale => colormap/RGB
    1233                 :  */
    1234               0 : DECLAREContigPutFunc(put16bitbwtile)
    1235                 : {
    1236               0 :     int samplesperpixel = img->samplesperpixel;
    1237               0 :     uint32** BWmap = img->BWmap;
    1238                 : 
    1239                 :     (void) y;
    1240               0 :     while (h-- > 0) {
    1241               0 :         uint16 *wp = (uint16 *) pp;
    1242                 : 
    1243               0 :   for (x = w; x-- > 0;)
    1244                 :         {
    1245                 :             /* use high order byte of 16bit value */
    1246                 : 
    1247               0 :       *cp++ = BWmap[*wp >> 8][0];
    1248               0 :             pp += 2 * samplesperpixel;
    1249               0 :             wp += samplesperpixel;
    1250                 :         }
    1251               0 :   cp += toskew;
    1252               0 :   pp += fromskew;
    1253                 :     }
    1254               0 : }
    1255                 : 
    1256                 : /*
    1257                 :  * 1-bit bilevel => colormap/RGB
    1258                 :  */
    1259               0 : DECLAREContigPutFunc(put1bitbwtile)
    1260                 : {
    1261               0 :     uint32** BWmap = img->BWmap;
    1262                 : 
    1263                 :     (void) x; (void) y;
    1264               0 :     fromskew /= 8;
    1265               0 :     while (h-- > 0) {
    1266                 :   uint32* bw;
    1267               0 :   UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1268               0 :   cp += toskew;
    1269               0 :   pp += fromskew;
    1270                 :     }
    1271               0 : }
    1272                 : 
    1273                 : /*
    1274                 :  * 2-bit greyscale => colormap/RGB
    1275                 :  */
    1276               0 : DECLAREContigPutFunc(put2bitbwtile)
    1277                 : {
    1278               0 :     uint32** BWmap = img->BWmap;
    1279                 : 
    1280                 :     (void) x; (void) y;
    1281               0 :     fromskew /= 4;
    1282               0 :     while (h-- > 0) {
    1283                 :   uint32* bw;
    1284               0 :   UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1285               0 :   cp += toskew;
    1286               0 :   pp += fromskew;
    1287                 :     }
    1288               0 : }
    1289                 : 
    1290                 : /*
    1291                 :  * 4-bit greyscale => colormap/RGB
    1292                 :  */
    1293               0 : DECLAREContigPutFunc(put4bitbwtile)
    1294                 : {
    1295               0 :     uint32** BWmap = img->BWmap;
    1296                 : 
    1297                 :     (void) x; (void) y;
    1298               0 :     fromskew /= 2;
    1299               0 :     while (h-- > 0) {
    1300                 :   uint32* bw;
    1301               0 :   UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1302               0 :   cp += toskew;
    1303               0 :   pp += fromskew;
    1304                 :     }
    1305               0 : }
    1306                 : 
    1307                 : /*
    1308                 :  * 8-bit packed samples, no Map => RGB
    1309                 :  */
    1310               0 : DECLAREContigPutFunc(putRGBcontig8bittile)
    1311                 : {
    1312               0 :     int samplesperpixel = img->samplesperpixel;
    1313                 : 
    1314                 :     (void) x; (void) y;
    1315               0 :     fromskew *= samplesperpixel;
    1316               0 :     while (h-- > 0) {
    1317               0 :   UNROLL8(w, NOP,
    1318                 :       *cp++ = PACK(pp[0], pp[1], pp[2]);
    1319                 :       pp += samplesperpixel);
    1320               0 :   cp += toskew;
    1321               0 :   pp += fromskew;
    1322                 :     }
    1323               0 : }
    1324                 : 
    1325                 : /*
    1326                 :  * 8-bit packed samples => RGBA w/ associated alpha
    1327                 :  * (known to have Map == NULL)
    1328                 :  */
    1329               0 : DECLAREContigPutFunc(putRGBAAcontig8bittile)
    1330                 : {
    1331               0 :     int samplesperpixel = img->samplesperpixel;
    1332                 : 
    1333                 :     (void) x; (void) y;
    1334               0 :     fromskew *= samplesperpixel;
    1335               0 :     while (h-- > 0) {
    1336               0 :   UNROLL8(w, NOP,
    1337                 :       *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
    1338                 :       pp += samplesperpixel);
    1339               0 :   cp += toskew;
    1340               0 :   pp += fromskew;
    1341                 :     }
    1342               0 : }
    1343                 : 
    1344                 : /*
    1345                 :  * 8-bit packed samples => RGBA w/ unassociated alpha
    1346                 :  * (known to have Map == NULL)
    1347                 :  */
    1348               0 : DECLAREContigPutFunc(putRGBUAcontig8bittile)
    1349                 : {
    1350               0 :   int samplesperpixel = img->samplesperpixel;
    1351                 :   (void) y;
    1352               0 :   fromskew *= samplesperpixel;
    1353               0 :   while (h-- > 0) {
    1354                 :     uint32 r, g, b, a;
    1355                 :     uint8* m;
    1356               0 :     for (x = w; x-- > 0;) {
    1357               0 :       a = pp[3];
    1358               0 :       m = img->UaToAa+(a<<8);
    1359               0 :       r = m[pp[0]];
    1360               0 :       g = m[pp[1]];
    1361               0 :       b = m[pp[2]];
    1362               0 :       *cp++ = PACK4(r,g,b,a);
    1363               0 :       pp += samplesperpixel;
    1364                 :     }
    1365               0 :     cp += toskew;
    1366               0 :     pp += fromskew;
    1367                 :   }
    1368               0 : }
    1369                 : 
    1370                 : /*
    1371                 :  * 16-bit packed samples => RGB
    1372                 :  */
    1373               0 : DECLAREContigPutFunc(putRGBcontig16bittile)
    1374                 : {
    1375               0 :   int samplesperpixel = img->samplesperpixel;
    1376               0 :   uint16 *wp = (uint16 *)pp;
    1377                 :   (void) y;
    1378               0 :   fromskew *= samplesperpixel;
    1379               0 :   while (h-- > 0) {
    1380               0 :     for (x = w; x-- > 0;) {
    1381               0 :       *cp++ = PACK(img->Bitdepth16To8[wp[0]],
    1382                 :           img->Bitdepth16To8[wp[1]],
    1383                 :           img->Bitdepth16To8[wp[2]]);
    1384               0 :       wp += samplesperpixel;
    1385                 :     }
    1386               0 :     cp += toskew;
    1387               0 :     wp += fromskew;
    1388                 :   }
    1389               0 : }
    1390                 : 
    1391                 : /*
    1392                 :  * 16-bit packed samples => RGBA w/ associated alpha
    1393                 :  * (known to have Map == NULL)
    1394                 :  */
    1395               0 : DECLAREContigPutFunc(putRGBAAcontig16bittile)
    1396                 : {
    1397               0 :   int samplesperpixel = img->samplesperpixel;
    1398               0 :   uint16 *wp = (uint16 *)pp;
    1399                 :   (void) y;
    1400               0 :   fromskew *= samplesperpixel;
    1401               0 :   while (h-- > 0) {
    1402               0 :     for (x = w; x-- > 0;) {
    1403               0 :       *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
    1404                 :           img->Bitdepth16To8[wp[1]],
    1405                 :           img->Bitdepth16To8[wp[2]],
    1406                 :           img->Bitdepth16To8[wp[3]]);
    1407               0 :       wp += samplesperpixel;
    1408                 :     }
    1409               0 :     cp += toskew;
    1410               0 :     wp += fromskew;
    1411                 :   }
    1412               0 : }
    1413                 : 
    1414                 : /*
    1415                 :  * 16-bit packed samples => RGBA w/ unassociated alpha
    1416                 :  * (known to have Map == NULL)
    1417                 :  */
    1418               0 : DECLAREContigPutFunc(putRGBUAcontig16bittile)
    1419                 : {
    1420               0 :   int samplesperpixel = img->samplesperpixel;
    1421               0 :   uint16 *wp = (uint16 *)pp;
    1422                 :   (void) y;
    1423               0 :   fromskew *= samplesperpixel;
    1424               0 :   while (h-- > 0) {
    1425                 :     uint32 r,g,b,a;
    1426                 :     uint8* m;
    1427               0 :     for (x = w; x-- > 0;) {
    1428               0 :       a = img->Bitdepth16To8[wp[3]];
    1429               0 :       m = img->UaToAa+(a<<8);
    1430               0 :       r = m[img->Bitdepth16To8[wp[0]]];
    1431               0 :       g = m[img->Bitdepth16To8[wp[1]]];
    1432               0 :       b = m[img->Bitdepth16To8[wp[2]]];
    1433               0 :       *cp++ = PACK4(r,g,b,a);
    1434               0 :       wp += samplesperpixel;
    1435                 :     }
    1436               0 :     cp += toskew;
    1437               0 :     wp += fromskew;
    1438                 :   }
    1439               0 : }
    1440                 : 
    1441                 : /*
    1442                 :  * 8-bit packed CMYK samples w/o Map => RGB
    1443                 :  *
    1444                 :  * NB: The conversion of CMYK->RGB is *very* crude.
    1445                 :  */
    1446               2 : DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
    1447                 : {
    1448               2 :     int samplesperpixel = img->samplesperpixel;
    1449                 :     uint16 r, g, b, k;
    1450                 : 
    1451                 :     (void) x; (void) y;
    1452               2 :     fromskew *= samplesperpixel;
    1453              54 :     while (h-- > 0) {
    1454              50 :   UNROLL8(w, NOP,
    1455                 :       k = 255 - pp[3];
    1456                 :       r = (k*(255-pp[0]))/255;
    1457                 :       g = (k*(255-pp[1]))/255;
    1458                 :       b = (k*(255-pp[2]))/255;
    1459                 :       *cp++ = PACK(r, g, b);
    1460                 :       pp += samplesperpixel);
    1461              50 :   cp += toskew;
    1462              50 :   pp += fromskew;
    1463                 :     }
    1464               2 : }
    1465                 : 
    1466                 : /*
    1467                 :  * 8-bit packed CMYK samples w/Map => RGB
    1468                 :  *
    1469                 :  * NB: The conversion of CMYK->RGB is *very* crude.
    1470                 :  */
    1471               0 : DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
    1472                 : {
    1473               0 :     int samplesperpixel = img->samplesperpixel;
    1474               0 :     TIFFRGBValue* Map = img->Map;
    1475                 :     uint16 r, g, b, k;
    1476                 : 
    1477                 :     (void) y;
    1478               0 :     fromskew *= samplesperpixel;
    1479               0 :     while (h-- > 0) {
    1480               0 :   for (x = w; x-- > 0;) {
    1481               0 :       k = 255 - pp[3];
    1482               0 :       r = (k*(255-pp[0]))/255;
    1483               0 :       g = (k*(255-pp[1]))/255;
    1484               0 :       b = (k*(255-pp[2]))/255;
    1485               0 :       *cp++ = PACK(Map[r], Map[g], Map[b]);
    1486               0 :       pp += samplesperpixel;
    1487                 :   }
    1488               0 :   pp += fromskew;
    1489               0 :   cp += toskew;
    1490                 :     }
    1491               0 : }
    1492                 : 
    1493                 : #define DECLARESepPutFunc(name) \
    1494                 : static void name(\
    1495                 :     TIFFRGBAImage* img,\
    1496                 :     uint32* cp,\
    1497                 :     uint32 x, uint32 y, \
    1498                 :     uint32 w, uint32 h,\
    1499                 :     int32 fromskew, int32 toskew,\
    1500                 :     unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
    1501                 : )
    1502                 : 
    1503                 : /*
    1504                 :  * 8-bit unpacked samples => RGB
    1505                 :  */
    1506               0 : DECLARESepPutFunc(putRGBseparate8bittile)
    1507                 : {
    1508                 :     (void) img; (void) x; (void) y; (void) a;
    1509               0 :     while (h-- > 0) {
    1510               0 :   UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
    1511               0 :   SKEW(r, g, b, fromskew);
    1512               0 :   cp += toskew;
    1513                 :     }
    1514               0 : }
    1515                 : 
    1516                 : /*
    1517                 :  * 8-bit unpacked samples => RGBA w/ associated alpha
    1518                 :  */
    1519               0 : DECLARESepPutFunc(putRGBAAseparate8bittile)
    1520                 : {
    1521                 :   (void) img; (void) x; (void) y; 
    1522               0 :   while (h-- > 0) {
    1523               0 :     UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
    1524               0 :     SKEW4(r, g, b, a, fromskew);
    1525               0 :     cp += toskew;
    1526                 :   }
    1527               0 : }
    1528                 : 
    1529                 : /*
    1530                 :  * 8-bit unpacked CMYK samples => RGBA
    1531                 :  */
    1532               0 : DECLARESepPutFunc(putCMYKseparate8bittile)
    1533                 : {
    1534                 :   (void) img; (void) y;
    1535               0 :   while (h-- > 0) {
    1536                 :     uint32 rv, gv, bv, kv;
    1537               0 :     for (x = w; x-- > 0;) {
    1538               0 :       kv = 255 - *a++;
    1539               0 :       rv = (kv*(255-*r++))/255;
    1540               0 :       gv = (kv*(255-*g++))/255;
    1541               0 :       bv = (kv*(255-*b++))/255;
    1542               0 :       *cp++ = PACK4(rv,gv,bv,255);
    1543                 :     }
    1544               0 :     SKEW4(r, g, b, a, fromskew);
    1545               0 :     cp += toskew;
    1546                 :   }
    1547               0 : }
    1548                 : 
    1549                 : /*
    1550                 :  * 8-bit unpacked samples => RGBA w/ unassociated alpha
    1551                 :  */
    1552               0 : DECLARESepPutFunc(putRGBUAseparate8bittile)
    1553                 : {
    1554                 :   (void) img; (void) y;
    1555               0 :   while (h-- > 0) {
    1556                 :     uint32 rv, gv, bv, av;
    1557                 :     uint8* m;
    1558               0 :     for (x = w; x-- > 0;) {
    1559               0 :       av = *a++;
    1560               0 :       m = img->UaToAa+(av<<8);
    1561               0 :       rv = m[*r++];
    1562               0 :       gv = m[*g++];
    1563               0 :       bv = m[*b++];
    1564               0 :       *cp++ = PACK4(rv,gv,bv,av);
    1565                 :     }
    1566               0 :     SKEW4(r, g, b, a, fromskew);
    1567               0 :     cp += toskew;
    1568                 :   }
    1569               0 : }
    1570                 : 
    1571                 : /*
    1572                 :  * 16-bit unpacked samples => RGB
    1573                 :  */
    1574               0 : DECLARESepPutFunc(putRGBseparate16bittile)
    1575                 : {
    1576               0 :   uint16 *wr = (uint16*) r;
    1577               0 :   uint16 *wg = (uint16*) g;
    1578               0 :   uint16 *wb = (uint16*) b;
    1579                 :   (void) img; (void) y; (void) a;
    1580               0 :   while (h-- > 0) {
    1581               0 :     for (x = 0; x < w; x++)
    1582               0 :       *cp++ = PACK(img->Bitdepth16To8[*wr++],
    1583                 :           img->Bitdepth16To8[*wg++],
    1584                 :           img->Bitdepth16To8[*wb++]);
    1585               0 :     SKEW(wr, wg, wb, fromskew);
    1586               0 :     cp += toskew;
    1587                 :   }
    1588               0 : }
    1589                 : 
    1590                 : /*
    1591                 :  * 16-bit unpacked samples => RGBA w/ associated alpha
    1592                 :  */
    1593               0 : DECLARESepPutFunc(putRGBAAseparate16bittile)
    1594                 : {
    1595               0 :   uint16 *wr = (uint16*) r;
    1596               0 :   uint16 *wg = (uint16*) g;
    1597               0 :   uint16 *wb = (uint16*) b;
    1598               0 :   uint16 *wa = (uint16*) a;
    1599                 :   (void) img; (void) y;
    1600               0 :   while (h-- > 0) {
    1601               0 :     for (x = 0; x < w; x++)
    1602               0 :       *cp++ = PACK4(img->Bitdepth16To8[*wr++],
    1603                 :           img->Bitdepth16To8[*wg++],
    1604                 :           img->Bitdepth16To8[*wb++],
    1605                 :           img->Bitdepth16To8[*wa++]);
    1606               0 :     SKEW4(wr, wg, wb, wa, fromskew);
    1607               0 :     cp += toskew;
    1608                 :   }
    1609               0 : }
    1610                 : 
    1611                 : /*
    1612                 :  * 16-bit unpacked samples => RGBA w/ unassociated alpha
    1613                 :  */
    1614               0 : DECLARESepPutFunc(putRGBUAseparate16bittile)
    1615                 : {
    1616               0 :   uint16 *wr = (uint16*) r;
    1617               0 :   uint16 *wg = (uint16*) g;
    1618               0 :   uint16 *wb = (uint16*) b;
    1619               0 :   uint16 *wa = (uint16*) a;
    1620                 :   (void) img; (void) y;
    1621               0 :   while (h-- > 0) {
    1622                 :     uint32 r,g,b,a;
    1623                 :     uint8* m;
    1624               0 :     for (x = w; x-- > 0;) {
    1625               0 :       a = img->Bitdepth16To8[*wa++];
    1626               0 :       m = img->UaToAa+(a<<8);
    1627               0 :       r = m[img->Bitdepth16To8[*wr++]];
    1628               0 :       g = m[img->Bitdepth16To8[*wg++]];
    1629               0 :       b = m[img->Bitdepth16To8[*wb++]];
    1630               0 :       *cp++ = PACK4(r,g,b,a);
    1631                 :     }
    1632               0 :     SKEW4(wr, wg, wb, wa, fromskew);
    1633               0 :     cp += toskew;
    1634                 :   }
    1635               0 : }
    1636                 : 
    1637                 : /*
    1638                 :  * 8-bit packed CIE L*a*b 1976 samples => RGB
    1639                 :  */
    1640               1 : DECLAREContigPutFunc(putcontig8bitCIELab)
    1641                 : {
    1642                 :   float X, Y, Z;
    1643                 :   uint32 r, g, b;
    1644                 :   (void) y;
    1645               1 :   fromskew *= 3;
    1646               3 :   while (h-- > 0) {
    1647               3 :     for (x = w; x-- > 0;) {
    1648               3 :       TIFFCIELabToXYZ(img->cielab,
    1649               1 :           (unsigned char)pp[0],
    1650               1 :           (signed char)pp[1],
    1651               1 :           (signed char)pp[2],
    1652                 :           &X, &Y, &Z);
    1653               1 :       TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
    1654               1 :       *cp++ = PACK(r, g, b);
    1655               1 :       pp += 3;
    1656                 :     }
    1657               1 :     cp += toskew;
    1658               1 :     pp += fromskew;
    1659                 :   }
    1660               1 : }
    1661                 : 
    1662                 : /*
    1663                 :  * YCbCr -> RGB conversion and packing routines.
    1664                 :  */
    1665                 : 
    1666                 : #define YCbCrtoRGB(dst, Y) {            \
    1667                 :   uint32 r, g, b;             \
    1668                 :   TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b);   \
    1669                 :   dst = PACK(r, g, b);            \
    1670                 : }
    1671                 : 
    1672                 : /*
    1673                 :  * 8-bit packed YCbCr samples => RGB 
    1674                 :  * This function is generic for different sampling sizes, 
    1675                 :  * and can handle blocks sizes that aren't multiples of the
    1676                 :  * sampling size.  However, it is substantially less optimized
    1677                 :  * than the specific sampling cases.  It is used as a fallback
    1678                 :  * for difficult blocks.
    1679                 :  */
    1680                 : #ifdef notdef
    1681                 : static void putcontig8bitYCbCrGenericTile( 
    1682                 :     TIFFRGBAImage* img, 
    1683                 :     uint32* cp, 
    1684                 :     uint32 x, uint32 y, 
    1685                 :     uint32 w, uint32 h, 
    1686                 :     int32 fromskew, int32 toskew, 
    1687                 :     unsigned char* pp,
    1688                 :     int h_group, 
    1689                 :     int v_group )
    1690                 : 
    1691                 : {
    1692                 :     uint32* cp1 = cp+w+toskew;
    1693                 :     uint32* cp2 = cp1+w+toskew;
    1694                 :     uint32* cp3 = cp2+w+toskew;
    1695                 :     int32 incr = 3*w+4*toskew;
    1696                 :     int32   Cb, Cr;
    1697                 :     int     group_size = v_group * h_group + 2;
    1698                 : 
    1699                 :     (void) y;
    1700                 :     fromskew = (fromskew * group_size) / h_group;
    1701                 : 
    1702                 :     for( yy = 0; yy < h; yy++ )
    1703                 :     {
    1704                 :         unsigned char *pp_line;
    1705                 :         int     y_line_group = yy / v_group;
    1706                 :         int     y_remainder = yy - y_line_group * v_group;
    1707                 : 
    1708                 :         pp_line = pp + v_line_group * 
    1709                 : 
    1710                 :         
    1711                 :         for( xx = 0; xx < w; xx++ )
    1712                 :         {
    1713                 :             Cb = pp
    1714                 :         }
    1715                 :     }
    1716                 :     for (; h >= 4; h -= 4) {
    1717                 :   x = w>>2;
    1718                 :   do {
    1719                 :       Cb = pp[16];
    1720                 :       Cr = pp[17];
    1721                 : 
    1722                 :       YCbCrtoRGB(cp [0], pp[ 0]);
    1723                 :       YCbCrtoRGB(cp [1], pp[ 1]);
    1724                 :       YCbCrtoRGB(cp [2], pp[ 2]);
    1725                 :       YCbCrtoRGB(cp [3], pp[ 3]);
    1726                 :       YCbCrtoRGB(cp1[0], pp[ 4]);
    1727                 :       YCbCrtoRGB(cp1[1], pp[ 5]);
    1728                 :       YCbCrtoRGB(cp1[2], pp[ 6]);
    1729                 :       YCbCrtoRGB(cp1[3], pp[ 7]);
    1730                 :       YCbCrtoRGB(cp2[0], pp[ 8]);
    1731                 :       YCbCrtoRGB(cp2[1], pp[ 9]);
    1732                 :       YCbCrtoRGB(cp2[2], pp[10]);
    1733                 :       YCbCrtoRGB(cp2[3], pp[11]);
    1734                 :       YCbCrtoRGB(cp3[0], pp[12]);
    1735                 :       YCbCrtoRGB(cp3[1], pp[13]);
    1736                 :       YCbCrtoRGB(cp3[2], pp[14]);
    1737                 :       YCbCrtoRGB(cp3[3], pp[15]);
    1738                 : 
    1739                 :       cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
    1740                 :       pp += 18;
    1741                 :   } while (--x);
    1742                 :   cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1743                 :   pp += fromskew;
    1744                 :     }
    1745                 : }
    1746                 : #endif
    1747                 : 
    1748                 : /*
    1749                 :  * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
    1750                 :  */
    1751               0 : DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
    1752                 : {
    1753               0 :     uint32* cp1 = cp+w+toskew;
    1754               0 :     uint32* cp2 = cp1+w+toskew;
    1755               0 :     uint32* cp3 = cp2+w+toskew;
    1756               0 :     int32 incr = 3*w+4*toskew;
    1757                 : 
    1758                 :     (void) y;
    1759                 :     /* adjust fromskew */
    1760               0 :     fromskew = (fromskew * 18) / 4;
    1761               0 :     if ((h & 3) == 0 && (w & 3) == 0) {               
    1762               0 :         for (; h >= 4; h -= 4) {
    1763               0 :             x = w>>2;
    1764                 :             do {
    1765               0 :                 int32 Cb = pp[16];
    1766               0 :                 int32 Cr = pp[17];
    1767                 : 
    1768               0 :                 YCbCrtoRGB(cp [0], pp[ 0]);
    1769               0 :                 YCbCrtoRGB(cp [1], pp[ 1]);
    1770               0 :                 YCbCrtoRGB(cp [2], pp[ 2]);
    1771               0 :                 YCbCrtoRGB(cp [3], pp[ 3]);
    1772               0 :                 YCbCrtoRGB(cp1[0], pp[ 4]);
    1773               0 :                 YCbCrtoRGB(cp1[1], pp[ 5]);
    1774               0 :                 YCbCrtoRGB(cp1[2], pp[ 6]);
    1775               0 :                 YCbCrtoRGB(cp1[3], pp[ 7]);
    1776               0 :                 YCbCrtoRGB(cp2[0], pp[ 8]);
    1777               0 :                 YCbCrtoRGB(cp2[1], pp[ 9]);
    1778               0 :                 YCbCrtoRGB(cp2[2], pp[10]);
    1779               0 :                 YCbCrtoRGB(cp2[3], pp[11]);
    1780               0 :                 YCbCrtoRGB(cp3[0], pp[12]);
    1781               0 :                 YCbCrtoRGB(cp3[1], pp[13]);
    1782               0 :                 YCbCrtoRGB(cp3[2], pp[14]);
    1783               0 :                 YCbCrtoRGB(cp3[3], pp[15]);
    1784                 : 
    1785               0 :                 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
    1786               0 :                 pp += 18;
    1787               0 :             } while (--x);
    1788               0 :             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1789               0 :             pp += fromskew;
    1790                 :         }
    1791                 :     } else {
    1792               0 :         while (h > 0) {
    1793               0 :             for (x = w; x > 0;) {
    1794               0 :                 int32 Cb = pp[16];
    1795               0 :                 int32 Cr = pp[17];
    1796               0 :                 switch (x) {
    1797                 :                 default:
    1798               0 :                     switch (h) {
    1799               0 :                     default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
    1800               0 :                     case 3:  YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
    1801               0 :                     case 2:  YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
    1802               0 :                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
    1803                 :                     }                                    /* FALLTHROUGH */
    1804                 :                 case 3:
    1805               0 :                     switch (h) {
    1806               0 :                     default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
    1807               0 :                     case 3:  YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
    1808               0 :                     case 2:  YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
    1809               0 :                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
    1810                 :                     }                                    /* FALLTHROUGH */
    1811                 :                 case 2:
    1812               0 :                     switch (h) {
    1813               0 :                     default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
    1814               0 :                     case 3:  YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
    1815               0 :                     case 2:  YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
    1816               0 :                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
    1817                 :                     }                                    /* FALLTHROUGH */
    1818                 :                 case 1:
    1819               0 :                     switch (h) {
    1820               0 :                     default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
    1821               0 :                     case 3:  YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
    1822               0 :                     case 2:  YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
    1823               0 :                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
    1824                 :                     }                                    /* FALLTHROUGH */
    1825                 :                 }
    1826               0 :                 if (x < 4) {
    1827               0 :                     cp += x; cp1 += x; cp2 += x; cp3 += x;
    1828               0 :                     x = 0;
    1829                 :                 }
    1830                 :                 else {
    1831               0 :                     cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
    1832               0 :                     x -= 4;
    1833                 :                 }
    1834               0 :                 pp += 18;
    1835                 :             }
    1836               0 :             if (h <= 4)
    1837               0 :                 break;
    1838               0 :             h -= 4;
    1839               0 :             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1840               0 :             pp += fromskew;
    1841                 :         }
    1842                 :     }
    1843               0 : }
    1844                 : 
    1845                 : /*
    1846                 :  * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
    1847                 :  */
    1848               0 : DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
    1849                 : {
    1850               0 :     uint32* cp1 = cp+w+toskew;
    1851               0 :     int32 incr = 2*toskew+w;
    1852                 : 
    1853                 :     (void) y;
    1854               0 :     fromskew = (fromskew * 10) / 4;
    1855               0 :     if ((h & 3) == 0 && (w & 1) == 0) {
    1856               0 :         for (; h >= 2; h -= 2) {
    1857               0 :             x = w>>2;
    1858                 :             do {
    1859               0 :                 int32 Cb = pp[8];
    1860               0 :                 int32 Cr = pp[9];
    1861                 :                 
    1862               0 :                 YCbCrtoRGB(cp [0], pp[0]);
    1863               0 :                 YCbCrtoRGB(cp [1], pp[1]);
    1864               0 :                 YCbCrtoRGB(cp [2], pp[2]);
    1865               0 :                 YCbCrtoRGB(cp [3], pp[3]);
    1866               0 :                 YCbCrtoRGB(cp1[0], pp[4]);
    1867               0 :                 YCbCrtoRGB(cp1[1], pp[5]);
    1868               0 :                 YCbCrtoRGB(cp1[2], pp[6]);
    1869               0 :                 YCbCrtoRGB(cp1[3], pp[7]);
    1870                 :                 
    1871               0 :                 cp += 4, cp1 += 4;
    1872               0 :                 pp += 10;
    1873               0 :             } while (--x);
    1874               0 :             cp += incr, cp1 += incr;
    1875               0 :             pp += fromskew;
    1876                 :         }
    1877                 :     } else {
    1878               0 :         while (h > 0) {
    1879               0 :             for (x = w; x > 0;) {
    1880               0 :                 int32 Cb = pp[8];
    1881               0 :                 int32 Cr = pp[9];
    1882               0 :                 switch (x) {
    1883                 :                 default:
    1884               0 :                     switch (h) {
    1885               0 :                     default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
    1886               0 :                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
    1887                 :                     }                                    /* FALLTHROUGH */
    1888                 :                 case 3:
    1889               0 :                     switch (h) {
    1890               0 :                     default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
    1891               0 :                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
    1892                 :                     }                                    /* FALLTHROUGH */
    1893                 :                 case 2:
    1894               0 :                     switch (h) {
    1895               0 :                     default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
    1896               0 :                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
    1897                 :                     }                                    /* FALLTHROUGH */
    1898                 :                 case 1:
    1899               0 :                     switch (h) {
    1900               0 :                     default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
    1901               0 :                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
    1902                 :                     }                                    /* FALLTHROUGH */
    1903                 :                 }
    1904               0 :                 if (x < 4) {
    1905               0 :                     cp += x; cp1 += x;
    1906               0 :                     x = 0;
    1907                 :                 }
    1908                 :                 else {
    1909               0 :                     cp += 4; cp1 += 4;
    1910               0 :                     x -= 4;
    1911                 :                 }
    1912               0 :                 pp += 10;
    1913                 :             }
    1914               0 :             if (h <= 2)
    1915               0 :                 break;
    1916               0 :             h -= 2;
    1917               0 :             cp += incr, cp1 += incr;
    1918               0 :             pp += fromskew;
    1919                 :         }
    1920                 :     }
    1921               0 : }
    1922                 : 
    1923                 : /*
    1924                 :  * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
    1925                 :  */
    1926               0 : DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
    1927                 : {
    1928                 :     (void) y;
    1929                 :     /* XXX adjust fromskew */
    1930                 :     do {
    1931               0 :   x = w>>2;
    1932                 :   do {
    1933               0 :       int32 Cb = pp[4];
    1934               0 :       int32 Cr = pp[5];
    1935                 : 
    1936               0 :       YCbCrtoRGB(cp [0], pp[0]);
    1937               0 :       YCbCrtoRGB(cp [1], pp[1]);
    1938               0 :       YCbCrtoRGB(cp [2], pp[2]);
    1939               0 :       YCbCrtoRGB(cp [3], pp[3]);
    1940                 : 
    1941               0 :       cp += 4;
    1942               0 :       pp += 6;
    1943               0 :   } while (--x);
    1944                 : 
    1945               0 :         if( (w&3) != 0 )
    1946                 :         {
    1947               0 :       int32 Cb = pp[4];
    1948               0 :       int32 Cr = pp[5];
    1949                 : 
    1950               0 :             switch( (w&3) ) {
    1951               0 :               case 3: YCbCrtoRGB(cp [2], pp[2]);
    1952               0 :               case 2: YCbCrtoRGB(cp [1], pp[1]);
    1953               0 :               case 1: YCbCrtoRGB(cp [0], pp[0]);
    1954                 :               case 0: break;
    1955                 :             }
    1956                 : 
    1957               0 :             cp += (w&3);
    1958               0 :             pp += 6;
    1959                 :         }
    1960                 : 
    1961               0 :   cp += toskew;
    1962               0 :   pp += fromskew;
    1963               0 :     } while (--h);
    1964                 : 
    1965               0 : }
    1966                 : 
    1967                 : /*
    1968                 :  * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
    1969                 :  */
    1970               1 : DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
    1971                 : {
    1972                 :   uint32* cp2;
    1973               1 :   int32 incr = 2*toskew+w;
    1974                 :   (void) y;
    1975               1 :   fromskew = (fromskew / 2) * 6;
    1976               1 :   cp2 = cp+w+toskew;
    1977             108 :   while (h>=2) {
    1978             106 :     x = w;
    1979           12614 :     while (x>=2) {
    1980           12402 :       uint32 Cb = pp[4];
    1981           12402 :       uint32 Cr = pp[5];
    1982           12402 :       YCbCrtoRGB(cp[0], pp[0]);
    1983           12402 :       YCbCrtoRGB(cp[1], pp[1]);
    1984           12402 :       YCbCrtoRGB(cp2[0], pp[2]);
    1985           12402 :       YCbCrtoRGB(cp2[1], pp[3]);
    1986           12402 :       cp += 2;
    1987           12402 :       cp2 += 2;
    1988           12402 :       pp += 6;
    1989           12402 :       x -= 2;
    1990                 :     }
    1991             106 :     if (x==1) {
    1992               0 :       uint32 Cb = pp[4];
    1993               0 :       uint32 Cr = pp[5];
    1994               0 :       YCbCrtoRGB(cp[0], pp[0]);
    1995               0 :       YCbCrtoRGB(cp2[0], pp[2]);
    1996               0 :       cp ++ ;
    1997               0 :       cp2 ++ ;
    1998               0 :       pp += 6;
    1999                 :     }
    2000             106 :     cp += incr;
    2001             106 :     cp2 += incr;
    2002             106 :     pp += fromskew;
    2003             106 :     h-=2;
    2004                 :   }
    2005               1 :   if (h==1) {
    2006               1 :     x = w;
    2007             119 :     while (x>=2) {
    2008             117 :       uint32 Cb = pp[4];
    2009             117 :       uint32 Cr = pp[5];
    2010             117 :       YCbCrtoRGB(cp[0], pp[0]);
    2011             117 :       YCbCrtoRGB(cp[1], pp[1]);
    2012             117 :       cp += 2;
    2013             117 :       cp2 += 2;
    2014             117 :       pp += 6;
    2015             117 :       x -= 2;
    2016                 :     }
    2017               1 :     if (x==1) {
    2018               0 :       uint32 Cb = pp[4];
    2019               0 :       uint32 Cr = pp[5];
    2020               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2021                 :     }
    2022                 :   }
    2023               1 : }
    2024                 : 
    2025                 : /*
    2026                 :  * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
    2027                 :  */
    2028               0 : DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
    2029                 : {
    2030                 :   (void) y;
    2031               0 :   fromskew = (fromskew * 4) / 2;
    2032                 :   do {
    2033               0 :     x = w>>1;
    2034                 :     do {
    2035               0 :       int32 Cb = pp[2];
    2036               0 :       int32 Cr = pp[3];
    2037                 : 
    2038               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2039               0 :       YCbCrtoRGB(cp[1], pp[1]);
    2040                 : 
    2041               0 :       cp += 2;
    2042               0 :       pp += 4;
    2043               0 :     } while (--x);
    2044                 : 
    2045               0 :     if( (w&1) != 0 )
    2046                 :     {
    2047               0 :       int32 Cb = pp[2];
    2048               0 :       int32 Cr = pp[3];
    2049                 : 
    2050               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2051                 : 
    2052               0 :       cp += 1;
    2053               0 :       pp += 4;
    2054                 :     }
    2055                 : 
    2056               0 :     cp += toskew;
    2057               0 :     pp += fromskew;
    2058               0 :   } while (--h);
    2059               0 : }
    2060                 : 
    2061                 : /*
    2062                 :  * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
    2063                 :  */
    2064               0 : DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
    2065                 : {
    2066                 :   uint32* cp2;
    2067               0 :   int32 incr = 2*toskew+w;
    2068                 :   (void) y;
    2069               0 :   fromskew = (fromskew / 2) * 4;
    2070               0 :   cp2 = cp+w+toskew;
    2071               0 :   while (h>=2) {
    2072               0 :     x = w;
    2073                 :     do {
    2074               0 :       uint32 Cb = pp[2];
    2075               0 :       uint32 Cr = pp[3];
    2076               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2077               0 :       YCbCrtoRGB(cp2[0], pp[1]);
    2078               0 :       cp ++;
    2079               0 :       cp2 ++;
    2080               0 :       pp += 4;
    2081               0 :     } while (--x);
    2082               0 :     cp += incr;
    2083               0 :     cp2 += incr;
    2084               0 :     pp += fromskew;
    2085               0 :     h-=2;
    2086                 :   }
    2087               0 :   if (h==1) {
    2088               0 :     x = w;
    2089                 :     do {
    2090               0 :       uint32 Cb = pp[2];
    2091               0 :       uint32 Cr = pp[3];
    2092               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2093               0 :       cp ++;
    2094               0 :       pp += 4;
    2095               0 :     } while (--x);
    2096                 :   }
    2097               0 : }
    2098                 : 
    2099                 : /*
    2100                 :  * 8-bit packed YCbCr samples w/ no subsampling => RGB
    2101                 :  */
    2102               0 : DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
    2103                 : {
    2104                 :   (void) y;
    2105               0 :   fromskew *= 3;
    2106                 :   do {
    2107               0 :     x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
    2108                 :     do {
    2109               0 :       int32 Cb = pp[1];
    2110               0 :       int32 Cr = pp[2];
    2111                 : 
    2112               0 :       YCbCrtoRGB(*cp++, pp[0]);
    2113                 : 
    2114               0 :       pp += 3;
    2115               0 :     } while (--x);
    2116               0 :     cp += toskew;
    2117               0 :     pp += fromskew;
    2118               0 :   } while (--h);
    2119               0 : }
    2120                 : 
    2121                 : /*
    2122                 :  * 8-bit packed YCbCr samples w/ no subsampling => RGB
    2123                 :  */
    2124               0 : DECLARESepPutFunc(putseparate8bitYCbCr11tile)
    2125                 : {
    2126                 :   (void) y;
    2127                 :   (void) a;
    2128                 :   /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
    2129               0 :   while (h-- > 0) {
    2130               0 :     x = w;
    2131                 :     do {
    2132                 :       uint32 dr, dg, db;
    2133               0 :       TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
    2134               0 :       *cp++ = PACK(dr,dg,db);
    2135               0 :     } while (--x);
    2136               0 :     SKEW(r, g, b, fromskew);
    2137               0 :     cp += toskew;
    2138                 :   }
    2139               0 : }
    2140                 : #undef YCbCrtoRGB
    2141                 : 
    2142                 : static int
    2143               1 : initYCbCrConversion(TIFFRGBAImage* img)
    2144                 : {
    2145                 :   static const char module[] = "initYCbCrConversion";
    2146                 : 
    2147                 :   float *luma, *refBlackWhite;
    2148                 : 
    2149               1 :   if (img->ycbcr == NULL) {
    2150               1 :     img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
    2151                 :         TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))  
    2152                 :         + 4*256*sizeof (TIFFRGBValue)
    2153                 :         + 2*256*sizeof (int)
    2154                 :         + 3*256*sizeof (int32)
    2155                 :         );
    2156               1 :     if (img->ycbcr == NULL) {
    2157               0 :       TIFFErrorExt(img->tif->tif_clientdata, module,
    2158                 :           "No space for YCbCr->RGB conversion state");
    2159               0 :       return (0);
    2160                 :     }
    2161                 :   }
    2162                 : 
    2163               1 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
    2164               1 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
    2165                 :       &refBlackWhite);
    2166               1 :   if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
    2167               0 :     return(0);
    2168               1 :   return (1);
    2169                 : }
    2170                 : 
    2171                 : static tileContigRoutine
    2172               1 : initCIELabConversion(TIFFRGBAImage* img)
    2173                 : {
    2174                 :   static const char module[] = "initCIELabConversion";
    2175                 : 
    2176                 :   float   *whitePoint;
    2177                 :   float   refWhite[3];
    2178                 : 
    2179               1 :   if (!img->cielab) {
    2180               1 :     img->cielab = (TIFFCIELabToRGB *)
    2181                 :       _TIFFmalloc(sizeof(TIFFCIELabToRGB));
    2182               1 :     if (!img->cielab) {
    2183               0 :       TIFFErrorExt(img->tif->tif_clientdata, module,
    2184                 :           "No space for CIE L*a*b*->RGB conversion state.");
    2185               0 :       return NULL;
    2186                 :     }
    2187                 :   }
    2188                 : 
    2189               1 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
    2190               1 :   refWhite[1] = 100.0F;
    2191               1 :   refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
    2192               2 :   refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
    2193               1 :           / whitePoint[1] * refWhite[1];
    2194               1 :   if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
    2195               0 :     TIFFErrorExt(img->tif->tif_clientdata, module,
    2196                 :         "Failed to initialize CIE L*a*b*->RGB conversion state.");
    2197               0 :     _TIFFfree(img->cielab);
    2198               0 :     return NULL;
    2199                 :   }
    2200                 : 
    2201               1 :   return putcontig8bitCIELab;
    2202                 : }
    2203                 : 
    2204                 : /*
    2205                 :  * Greyscale images with less than 8 bits/sample are handled
    2206                 :  * with a table to avoid lots of shifts and masks.  The table
    2207                 :  * is setup so that put*bwtile (below) can retrieve 8/bitspersample
    2208                 :  * pixel values simply by indexing into the table with one
    2209                 :  * number.
    2210                 :  */
    2211                 : static int
    2212               0 : makebwmap(TIFFRGBAImage* img)
    2213                 : {
    2214               0 :     TIFFRGBValue* Map = img->Map;
    2215               0 :     int bitspersample = img->bitspersample;
    2216               0 :     int nsamples = 8 / bitspersample;
    2217                 :     int i;
    2218                 :     uint32* p;
    2219                 : 
    2220               0 :     if( nsamples == 0 )
    2221               0 :         nsamples = 1;
    2222                 : 
    2223               0 :     img->BWmap = (uint32**) _TIFFmalloc(
    2224               0 :   256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
    2225               0 :     if (img->BWmap == NULL) {
    2226               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
    2227               0 :     return (0);
    2228                 :     }
    2229               0 :     p = (uint32*)(img->BWmap + 256);
    2230               0 :     for (i = 0; i < 256; i++) {
    2231                 :   TIFFRGBValue c;
    2232               0 :   img->BWmap[i] = p;
    2233               0 :   switch (bitspersample) {
    2234                 : #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
    2235                 :   case 1:
    2236               0 :       GREY(i>>7);
    2237               0 :       GREY((i>>6)&1);
    2238               0 :       GREY((i>>5)&1);
    2239               0 :       GREY((i>>4)&1);
    2240               0 :       GREY((i>>3)&1);
    2241               0 :       GREY((i>>2)&1);
    2242               0 :       GREY((i>>1)&1);
    2243               0 :       GREY(i&1);
    2244               0 :       break;
    2245                 :   case 2:
    2246               0 :       GREY(i>>6);
    2247               0 :       GREY((i>>4)&3);
    2248               0 :       GREY((i>>2)&3);
    2249               0 :       GREY(i&3);
    2250               0 :       break;
    2251                 :   case 4:
    2252               0 :       GREY(i>>4);
    2253               0 :       GREY(i&0xf);
    2254               0 :       break;
    2255                 :   case 8:
    2256                 :         case 16:
    2257               0 :       GREY(i);
    2258                 :       break;
    2259                 :   }
    2260                 : #undef  GREY
    2261                 :     }
    2262               0 :     return (1);
    2263                 : }
    2264                 : 
    2265                 : /*
    2266                 :  * Construct a mapping table to convert from the range
    2267                 :  * of the data samples to [0,255] --for display.  This
    2268                 :  * process also handles inverting B&W images when needed.
    2269                 :  */ 
    2270                 : static int
    2271               0 : setupMap(TIFFRGBAImage* img)
    2272                 : {
    2273                 :     int32 x, range;
    2274                 : 
    2275               0 :     range = (int32)((1L<<img->bitspersample)-1);
    2276                 :     
    2277                 :     /* treat 16 bit the same as eight bit */
    2278               0 :     if( img->bitspersample == 16 )
    2279               0 :         range = (int32) 255;
    2280                 : 
    2281               0 :     img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
    2282               0 :     if (img->Map == NULL) {
    2283               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
    2284                 :       "No space for photometric conversion table");
    2285               0 :     return (0);
    2286                 :     }
    2287               0 :     if (img->photometric == PHOTOMETRIC_MINISWHITE) {
    2288               0 :   for (x = 0; x <= range; x++)
    2289               0 :       img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
    2290                 :     } else {
    2291               0 :   for (x = 0; x <= range; x++)
    2292               0 :       img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
    2293                 :     }
    2294               0 :     if (img->bitspersample <= 16 &&
    2295               0 :   (img->photometric == PHOTOMETRIC_MINISBLACK ||
    2296               0 :    img->photometric == PHOTOMETRIC_MINISWHITE)) {
    2297                 :   /*
    2298                 :    * Use photometric mapping table to construct
    2299                 :    * unpacking tables for samples <= 8 bits.
    2300                 :    */
    2301               0 :   if (!makebwmap(img))
    2302               0 :       return (0);
    2303                 :   /* no longer need Map, free it */
    2304               0 :   _TIFFfree(img->Map), img->Map = NULL;
    2305                 :     }
    2306               0 :     return (1);
    2307                 : }
    2308                 : 
    2309                 : static int
    2310               0 : checkcmap(TIFFRGBAImage* img)
    2311                 : {
    2312               0 :     uint16* r = img->redcmap;
    2313               0 :     uint16* g = img->greencmap;
    2314               0 :     uint16* b = img->bluecmap;
    2315               0 :     long n = 1L<<img->bitspersample;
    2316                 : 
    2317               0 :     while (n-- > 0)
    2318               0 :   if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
    2319               0 :       return (16);
    2320               0 :     return (8);
    2321                 : }
    2322                 : 
    2323                 : static void
    2324               0 : cvtcmap(TIFFRGBAImage* img)
    2325                 : {
    2326               0 :     uint16* r = img->redcmap;
    2327               0 :     uint16* g = img->greencmap;
    2328               0 :     uint16* b = img->bluecmap;
    2329                 :     long i;
    2330                 : 
    2331               0 :     for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
    2332                 : #define CVT(x)    ((uint16)((x)>>8))
    2333               0 :   r[i] = CVT(r[i]);
    2334               0 :   g[i] = CVT(g[i]);
    2335               0 :   b[i] = CVT(b[i]);
    2336                 : #undef  CVT
    2337                 :     }
    2338               0 : }
    2339                 : 
    2340                 : /*
    2341                 :  * Palette images with <= 8 bits/sample are handled
    2342                 :  * with a table to avoid lots of shifts and masks.  The table
    2343                 :  * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
    2344                 :  * pixel values simply by indexing into the table with one
    2345                 :  * number.
    2346                 :  */
    2347                 : static int
    2348               0 : makecmap(TIFFRGBAImage* img)
    2349                 : {
    2350               0 :     int bitspersample = img->bitspersample;
    2351               0 :     int nsamples = 8 / bitspersample;
    2352               0 :     uint16* r = img->redcmap;
    2353               0 :     uint16* g = img->greencmap;
    2354               0 :     uint16* b = img->bluecmap;
    2355                 :     uint32 *p;
    2356                 :     int i;
    2357                 : 
    2358               0 :     img->PALmap = (uint32**) _TIFFmalloc(
    2359               0 :   256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
    2360               0 :     if (img->PALmap == NULL) {
    2361               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
    2362               0 :     return (0);
    2363                 :   }
    2364               0 :     p = (uint32*)(img->PALmap + 256);
    2365               0 :     for (i = 0; i < 256; i++) {
    2366                 :   TIFFRGBValue c;
    2367               0 :   img->PALmap[i] = p;
    2368                 : #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
    2369               0 :   switch (bitspersample) {
    2370                 :   case 1:
    2371               0 :       CMAP(i>>7);
    2372               0 :       CMAP((i>>6)&1);
    2373               0 :       CMAP((i>>5)&1);
    2374               0 :       CMAP((i>>4)&1);
    2375               0 :       CMAP((i>>3)&1);
    2376               0 :       CMAP((i>>2)&1);
    2377               0 :       CMAP((i>>1)&1);
    2378               0 :       CMAP(i&1);
    2379               0 :       break;
    2380                 :   case 2:
    2381               0 :       CMAP(i>>6);
    2382               0 :       CMAP((i>>4)&3);
    2383               0 :       CMAP((i>>2)&3);
    2384               0 :       CMAP(i&3);
    2385               0 :       break;
    2386                 :   case 4:
    2387               0 :       CMAP(i>>4);
    2388               0 :       CMAP(i&0xf);
    2389               0 :       break;
    2390                 :   case 8:
    2391               0 :       CMAP(i);
    2392                 :       break;
    2393                 :   }
    2394                 : #undef CMAP
    2395                 :     }
    2396               0 :     return (1);
    2397                 : }
    2398                 : 
    2399                 : /* 
    2400                 :  * Construct any mapping table used
    2401                 :  * by the associated put routine.
    2402                 :  */
    2403                 : static int
    2404               3 : buildMap(TIFFRGBAImage* img)
    2405                 : {
    2406               3 :     switch (img->photometric) {
    2407                 :     case PHOTOMETRIC_RGB:
    2408                 :     case PHOTOMETRIC_YCBCR:
    2409                 :     case PHOTOMETRIC_SEPARATED:
    2410               2 :   if (img->bitspersample == 8)
    2411               2 :       break;
    2412                 :   /* fall thru... */
    2413                 :     case PHOTOMETRIC_MINISBLACK:
    2414                 :     case PHOTOMETRIC_MINISWHITE:
    2415               0 :   if (!setupMap(img))
    2416               0 :       return (0);
    2417               0 :   break;
    2418                 :     case PHOTOMETRIC_PALETTE:
    2419                 :   /*
    2420                 :    * Convert 16-bit colormap to 8-bit (unless it looks
    2421                 :    * like an old-style 8-bit colormap).
    2422                 :    */
    2423               0 :   if (checkcmap(img) == 16)
    2424               0 :       cvtcmap(img);
    2425                 :   else
    2426               0 :       TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
    2427                 :   /*
    2428                 :    * Use mapping table and colormap to construct
    2429                 :    * unpacking tables for samples < 8 bits.
    2430                 :    */
    2431               0 :   if (img->bitspersample <= 8 && !makecmap(img))
    2432               0 :       return (0);
    2433                 :   break;
    2434                 :     }
    2435               3 :     return (1);
    2436                 : }
    2437                 : 
    2438                 : /*
    2439                 :  * Select the appropriate conversion routine for packed data.
    2440                 :  */
    2441                 : static int
    2442               4 : PickContigCase(TIFFRGBAImage* img)
    2443                 : {
    2444               4 :   img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
    2445               4 :   img->put.contig = NULL;
    2446               4 :   switch (img->photometric) {
    2447                 :     case PHOTOMETRIC_RGB:
    2448               0 :       switch (img->bitspersample) {
    2449                 :         case 8:
    2450               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2451               0 :             img->put.contig = putRGBAAcontig8bittile;
    2452               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2453                 :           {
    2454               0 :             if (BuildMapUaToAa(img))
    2455               0 :               img->put.contig = putRGBUAcontig8bittile;
    2456                 :           }
    2457                 :           else
    2458               0 :             img->put.contig = putRGBcontig8bittile;
    2459               0 :           break;
    2460                 :         case 16:
    2461               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2462                 :           {
    2463               0 :             if (BuildMapBitdepth16To8(img))
    2464               0 :               img->put.contig = putRGBAAcontig16bittile;
    2465                 :           }
    2466               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2467                 :           {
    2468               0 :             if (BuildMapBitdepth16To8(img) &&
    2469               0 :                 BuildMapUaToAa(img))
    2470               0 :               img->put.contig = putRGBUAcontig16bittile;
    2471                 :           }
    2472                 :           else
    2473                 :           {
    2474               0 :             if (BuildMapBitdepth16To8(img))
    2475               0 :               img->put.contig = putRGBcontig16bittile;
    2476                 :           }
    2477                 :           break;
    2478                 :       }
    2479               0 :       break;
    2480                 :     case PHOTOMETRIC_SEPARATED:
    2481               2 :       if (buildMap(img)) {
    2482               2 :         if (img->bitspersample == 8) {
    2483               2 :           if (!img->Map)
    2484               2 :             img->put.contig = putRGBcontig8bitCMYKtile;
    2485                 :           else
    2486               0 :             img->put.contig = putRGBcontig8bitCMYKMaptile;
    2487                 :         }
    2488                 :       }
    2489               2 :       break;
    2490                 :     case PHOTOMETRIC_PALETTE:
    2491               0 :       if (buildMap(img)) {
    2492               0 :         switch (img->bitspersample) {
    2493                 :           case 8:
    2494               0 :             img->put.contig = put8bitcmaptile;
    2495               0 :             break;
    2496                 :           case 4:
    2497               0 :             img->put.contig = put4bitcmaptile;
    2498               0 :             break;
    2499                 :           case 2:
    2500               0 :             img->put.contig = put2bitcmaptile;
    2501               0 :             break;
    2502                 :           case 1:
    2503               0 :             img->put.contig = put1bitcmaptile;
    2504                 :             break;
    2505                 :         }
    2506                 :       }
    2507               0 :       break;
    2508                 :     case PHOTOMETRIC_MINISWHITE:
    2509                 :     case PHOTOMETRIC_MINISBLACK:
    2510               0 :       if (buildMap(img)) {
    2511               0 :         switch (img->bitspersample) {
    2512                 :           case 16:
    2513               0 :             img->put.contig = put16bitbwtile;
    2514               0 :             break;
    2515                 :           case 8:
    2516               0 :             if (img->alpha && img->samplesperpixel == 2)
    2517               0 :               img->put.contig = putagreytile;
    2518                 :             else
    2519               0 :               img->put.contig = putgreytile;
    2520               0 :             break;
    2521                 :           case 4:
    2522               0 :             img->put.contig = put4bitbwtile;
    2523               0 :             break;
    2524                 :           case 2:
    2525               0 :             img->put.contig = put2bitbwtile;
    2526               0 :             break;
    2527                 :           case 1:
    2528               0 :             img->put.contig = put1bitbwtile;
    2529                 :             break;
    2530                 :         }
    2531                 :       }
    2532               0 :       break;
    2533                 :     case PHOTOMETRIC_YCBCR:
    2534               1 :       if ((img->bitspersample==8) && (img->samplesperpixel==3))
    2535                 :       {
    2536               1 :         if (initYCbCrConversion(img)!=0)
    2537                 :         {
    2538                 :           /*
    2539                 :            * The 6.0 spec says that subsampling must be
    2540                 :            * one of 1, 2, or 4, and that vertical subsampling
    2541                 :            * must always be <= horizontal subsampling; so
    2542                 :            * there are only a few possibilities and we just
    2543                 :            * enumerate the cases.
    2544                 :            * Joris: added support for the [1,2] case, nonetheless, to accomodate
    2545                 :            * some OJPEG files
    2546                 :            */
    2547                 :           uint16 SubsamplingHor;
    2548                 :           uint16 SubsamplingVer;
    2549               1 :           TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
    2550               1 :           switch ((SubsamplingHor<<4)|SubsamplingVer) {
    2551                 :             case 0x44:
    2552               0 :               img->put.contig = putcontig8bitYCbCr44tile;
    2553               0 :               break;
    2554                 :             case 0x42:
    2555               0 :               img->put.contig = putcontig8bitYCbCr42tile;
    2556               0 :               break;
    2557                 :             case 0x41:
    2558               0 :               img->put.contig = putcontig8bitYCbCr41tile;
    2559               0 :               break;
    2560                 :             case 0x22:
    2561               1 :               img->put.contig = putcontig8bitYCbCr22tile;
    2562               1 :               break;
    2563                 :             case 0x21:
    2564               0 :               img->put.contig = putcontig8bitYCbCr21tile;
    2565               0 :               break;
    2566                 :             case 0x12:
    2567               0 :               img->put.contig = putcontig8bitYCbCr12tile;
    2568               0 :               break;
    2569                 :             case 0x11:
    2570               0 :               img->put.contig = putcontig8bitYCbCr11tile;
    2571                 :               break;
    2572                 :           }
    2573                 :         }
    2574                 :       }
    2575               1 :       break;
    2576                 :     case PHOTOMETRIC_CIELAB:
    2577               1 :       if (buildMap(img)) {
    2578               1 :         if (img->bitspersample == 8)
    2579               1 :           img->put.contig = initCIELabConversion(img);
    2580                 :         break;
    2581                 :       }
    2582                 :   }
    2583               4 :   return ((img->get!=NULL) && (img->put.contig!=NULL));
    2584                 : }
    2585                 : 
    2586                 : /*
    2587                 :  * Select the appropriate conversion routine for unpacked data.
    2588                 :  *
    2589                 :  * NB: we assume that unpacked single channel data is directed
    2590                 :  *   to the "packed routines.
    2591                 :  */
    2592                 : static int
    2593               0 : PickSeparateCase(TIFFRGBAImage* img)
    2594                 : {
    2595               0 :   img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
    2596               0 :   img->put.separate = NULL;
    2597               0 :   switch (img->photometric) {
    2598                 :   case PHOTOMETRIC_MINISWHITE:
    2599                 :   case PHOTOMETRIC_MINISBLACK:
    2600                 :     /* greyscale images processed pretty much as RGB by gtTileSeparate */
    2601                 :   case PHOTOMETRIC_RGB:
    2602               0 :     switch (img->bitspersample) {
    2603                 :     case 8:
    2604               0 :       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2605               0 :         img->put.separate = putRGBAAseparate8bittile;
    2606               0 :       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2607                 :       {
    2608               0 :         if (BuildMapUaToAa(img))
    2609               0 :           img->put.separate = putRGBUAseparate8bittile;
    2610                 :       }
    2611                 :       else
    2612               0 :         img->put.separate = putRGBseparate8bittile;
    2613               0 :       break;
    2614                 :     case 16:
    2615               0 :       if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2616                 :       {
    2617               0 :         if (BuildMapBitdepth16To8(img))
    2618               0 :           img->put.separate = putRGBAAseparate16bittile;
    2619                 :       }
    2620               0 :       else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2621                 :       {
    2622               0 :         if (BuildMapBitdepth16To8(img) &&
    2623               0 :             BuildMapUaToAa(img))
    2624               0 :           img->put.separate = putRGBUAseparate16bittile;
    2625                 :       }
    2626                 :       else
    2627                 :       {
    2628               0 :         if (BuildMapBitdepth16To8(img))
    2629               0 :           img->put.separate = putRGBseparate16bittile;
    2630                 :       }
    2631                 :       break;
    2632                 :     }
    2633               0 :     break;
    2634                 :   case PHOTOMETRIC_SEPARATED:
    2635               0 :     if (img->bitspersample == 8 && img->samplesperpixel == 4)
    2636                 :     {
    2637               0 :       img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
    2638               0 :       img->put.separate = putCMYKseparate8bittile;
    2639                 :     }
    2640               0 :     break;
    2641                 :   case PHOTOMETRIC_YCBCR:
    2642               0 :     if ((img->bitspersample==8) && (img->samplesperpixel==3))
    2643                 :     {
    2644               0 :       if (initYCbCrConversion(img)!=0)
    2645                 :       {
    2646                 :         uint16 hs, vs;
    2647               0 :         TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
    2648               0 :         switch ((hs<<4)|vs) {
    2649                 :         case 0x11:
    2650               0 :           img->put.separate = putseparate8bitYCbCr11tile;
    2651                 :           break;
    2652                 :           /* TODO: add other cases here */
    2653                 :         }
    2654                 :       }
    2655                 :     }
    2656                 :     break;
    2657                 :   }
    2658               0 :   return ((img->get!=NULL) && (img->put.separate!=NULL));
    2659                 : }
    2660                 : 
    2661                 : static int
    2662               0 : BuildMapUaToAa(TIFFRGBAImage* img)
    2663                 : {
    2664                 :   static const char module[]="BuildMapUaToAa";
    2665                 :   uint8* m;
    2666                 :   uint16 na,nv;
    2667               0 :   assert(img->UaToAa==NULL);
    2668               0 :   img->UaToAa=_TIFFmalloc(65536);
    2669               0 :   if (img->UaToAa==NULL)
    2670                 :   {
    2671               0 :     TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
    2672               0 :     return(0);
    2673                 :   }
    2674               0 :   m=img->UaToAa;
    2675               0 :   for (na=0; na<256; na++)
    2676                 :   {
    2677               0 :     for (nv=0; nv<256; nv++)
    2678               0 :       *m++=(nv*na+127)/255;
    2679                 :   }
    2680               0 :   return(1);
    2681                 : }
    2682                 : 
    2683                 : static int
    2684               0 : BuildMapBitdepth16To8(TIFFRGBAImage* img)
    2685                 : {
    2686                 :   static const char module[]="BuildMapBitdepth16To8";
    2687                 :   uint8* m;
    2688                 :   uint32 n;
    2689               0 :   assert(img->Bitdepth16To8==NULL);
    2690               0 :   img->Bitdepth16To8=_TIFFmalloc(65536);
    2691               0 :   if (img->Bitdepth16To8==NULL)
    2692                 :   {
    2693               0 :     TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
    2694               0 :     return(0);
    2695                 :   }
    2696               0 :   m=img->Bitdepth16To8;
    2697               0 :   for (n=0; n<65536; n++)
    2698               0 :     *m++=(n+128)/257;
    2699               0 :   return(1);
    2700                 : }
    2701                 : 
    2702                 : 
    2703                 : /*
    2704                 :  * Read a whole strip off data from the file, and convert to RGBA form.
    2705                 :  * If this is the last strip, then it will only contain the portion of
    2706                 :  * the strip that is actually within the image space.  The result is
    2707                 :  * organized in bottom to top form.
    2708                 :  */
    2709                 : 
    2710                 : 
    2711                 : int
    2712               3 : TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
    2713                 : 
    2714                 : {
    2715               3 :     char  emsg[1024] = "";
    2716                 :     TIFFRGBAImage img;
    2717                 :     int   ok;
    2718                 :     uint32  rowsperstrip, rows_to_read;
    2719                 : 
    2720               3 :     if( TIFFIsTiled( tif ) )
    2721                 :     {
    2722               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2723                 :                   "Can't use TIFFReadRGBAStrip() with tiled file.");
    2724               0 :   return (0);
    2725                 :     }
    2726                 :     
    2727               3 :     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
    2728               3 :     if( (row % rowsperstrip) != 0 )
    2729                 :     {
    2730               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2731                 :         "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
    2732               0 :     return (0);
    2733                 :     }
    2734                 : 
    2735               6 :     if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
    2736                 : 
    2737               3 :         img.row_offset = row;
    2738               3 :         img.col_offset = 0;
    2739                 : 
    2740               3 :         if( row + rowsperstrip > img.height )
    2741               1 :             rows_to_read = img.height - row;
    2742                 :         else
    2743               2 :             rows_to_read = rowsperstrip;
    2744                 :         
    2745               3 :   ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
    2746                 :         
    2747               3 :   TIFFRGBAImageEnd(&img);
    2748                 :     } else {
    2749               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
    2750               0 :     ok = 0;
    2751                 :     }
    2752                 :     
    2753               3 :     return (ok);
    2754                 : }
    2755                 : 
    2756                 : /*
    2757                 :  * Read a whole tile off data from the file, and convert to RGBA form.
    2758                 :  * The returned RGBA data is organized from bottom to top of tile,
    2759                 :  * and may include zeroed areas if the tile extends off the image.
    2760                 :  */
    2761                 : 
    2762                 : int
    2763               1 : TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
    2764                 : 
    2765                 : {
    2766               1 :     char  emsg[1024] = "";
    2767                 :     TIFFRGBAImage img;
    2768                 :     int   ok;
    2769                 :     uint32  tile_xsize, tile_ysize;
    2770                 :     uint32  read_xsize, read_ysize;
    2771                 :     uint32  i_row;
    2772                 : 
    2773                 :     /*
    2774                 :      * Verify that our request is legal - on a tile file, and on a
    2775                 :      * tile boundary.
    2776                 :      */
    2777                 :     
    2778               1 :     if( !TIFFIsTiled( tif ) )
    2779                 :     {
    2780               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2781                 :           "Can't use TIFFReadRGBATile() with stripped file.");
    2782               0 :     return (0);
    2783                 :     }
    2784                 :     
    2785               1 :     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
    2786               1 :     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
    2787               1 :     if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
    2788                 :     {
    2789               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2790                 :                   "Row/col passed to TIFFReadRGBATile() must be top"
    2791                 :                   "left corner of a tile.");
    2792               0 :   return (0);
    2793                 :     }
    2794                 : 
    2795                 :     /*
    2796                 :      * Setup the RGBA reader.
    2797                 :      */
    2798                 :     
    2799               2 :     if (!TIFFRGBAImageOK(tif, emsg) 
    2800               1 :   || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
    2801               0 :       TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
    2802               0 :       return( 0 );
    2803                 :     }
    2804                 : 
    2805                 :     /*
    2806                 :      * The TIFFRGBAImageGet() function doesn't allow us to get off the
    2807                 :      * edge of the image, even to fill an otherwise valid tile.  So we
    2808                 :      * figure out how much we can read, and fix up the tile buffer to
    2809                 :      * a full tile configuration afterwards.
    2810                 :      */
    2811                 : 
    2812               1 :     if( row + tile_ysize > img.height )
    2813               1 :         read_ysize = img.height - row;
    2814                 :     else
    2815               0 :         read_ysize = tile_ysize;
    2816                 :     
    2817               1 :     if( col + tile_xsize > img.width )
    2818               1 :         read_xsize = img.width - col;
    2819                 :     else
    2820               0 :         read_xsize = tile_xsize;
    2821                 : 
    2822                 :     /*
    2823                 :      * Read the chunk of imagery.
    2824                 :      */
    2825                 :     
    2826               1 :     img.row_offset = row;
    2827               1 :     img.col_offset = col;
    2828                 : 
    2829               1 :     ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
    2830                 :         
    2831               1 :     TIFFRGBAImageEnd(&img);
    2832                 : 
    2833                 :     /*
    2834                 :      * If our read was incomplete we will need to fix up the tile by
    2835                 :      * shifting the data around as if a full tile of data is being returned.
    2836                 :      *
    2837                 :      * This is all the more complicated because the image is organized in
    2838                 :      * bottom to top format. 
    2839                 :      */
    2840                 : 
    2841               1 :     if( read_xsize == tile_xsize && read_ysize == tile_ysize )
    2842               0 :         return( ok );
    2843                 : 
    2844             214 :     for( i_row = 0; i_row < read_ysize; i_row++ ) {
    2845             639 :         memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
    2846             426 :                  raster + (read_ysize - i_row - 1) * read_xsize,
    2847                 :                  read_xsize * sizeof(uint32) );
    2848             213 :         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
    2849             213 :                      0, sizeof(uint32) * (tile_xsize - read_xsize) );
    2850                 :     }
    2851                 : 
    2852              12 :     for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
    2853              11 :         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
    2854              11 :                      0, sizeof(uint32) * tile_xsize );
    2855                 :     }
    2856                 : 
    2857               1 :     return (ok);
    2858                 : }
    2859                 : 
    2860                 : /* vim: set ts=8 sts=8 sw=8 noet: */
    2861                 : /*
    2862                 :  * Local Variables:
    2863                 :  * mode: c
    2864                 :  * c-basic-offset: 8
    2865                 :  * fill-column: 78
    2866                 :  * End:
    2867                 :  */

Generated by: LCOV version 1.7