LCOV - code coverage report
Current view: directory - frmts/gtiff/libtiff - tif_getimage.c (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 1351 285 21.1 %
Date: 2012-04-28 Functions: 59 16 27.1 %

       1                 : /* $Id: tif_getimage.c,v 1.79 2012-04-06 16:46:46 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              18 : TIFFRGBAImageOK(TIFF* tif, char emsg[1024])
      77                 : {
      78              18 :   TIFFDirectory* td = &tif->tif_dir;
      79                 :   uint16 photometric;
      80                 :   int colorchannels;
      81                 : 
      82              18 :   if (!tif->tif_decodestatus) {
      83               0 :     sprintf(emsg, "Sorry, requested compression method is not configured");
      84               0 :     return (0);
      85                 :   }
      86              18 :   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              18 :   colorchannels = td->td_samplesperpixel - td->td_extrasamples;
      99              18 :   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              18 :   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               4 :       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              10 :         TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
     152              10 :         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              10 :         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              10 :         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               4 :       break;
     188                 :     default:
     189               0 :       sprintf(emsg, "Sorry, can not handle image with %s=%d",
     190                 :           photoTag, photometric);
     191               0 :       return (0);
     192                 :   }
     193              18 :   return (1);
     194                 : }
     195                 : 
     196                 : void
     197               8 : TIFFRGBAImageEnd(TIFFRGBAImage* img)
     198                 : {
     199               8 :   if (img->Map)
     200               0 :     _TIFFfree(img->Map), img->Map = NULL;
     201               8 :   if (img->BWmap)
     202               0 :     _TIFFfree(img->BWmap), img->BWmap = NULL;
     203               8 :   if (img->PALmap)
     204               0 :     _TIFFfree(img->PALmap), img->PALmap = NULL;
     205               8 :   if (img->ycbcr)
     206               2 :     _TIFFfree(img->ycbcr), img->ycbcr = NULL;
     207               8 :   if (img->cielab)
     208               2 :     _TIFFfree(img->cielab), img->cielab = NULL;
     209               8 :   if (img->UaToAa)
     210               0 :     _TIFFfree(img->UaToAa), img->UaToAa = NULL;
     211               8 :   if (img->Bitdepth16To8)
     212               0 :     _TIFFfree(img->Bitdepth16To8), img->Bitdepth16To8 = NULL;
     213                 : 
     214               8 :   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               8 : }
     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               8 : 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               8 :   img->row_offset = 0;
     246               8 :   img->col_offset = 0;
     247               8 :   img->redcmap = NULL;
     248               8 :   img->greencmap = NULL;
     249               8 :   img->bluecmap = NULL;
     250               8 :   img->req_orientation = ORIENTATION_BOTLEFT;     /* It is the default */
     251                 : 
     252               8 :   img->tif = tif;
     253               8 :   img->stoponerr = stop;
     254               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_BITSPERSAMPLE, &img->bitspersample);
     255               8 :   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               8 :   img->alpha = 0;
     268               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_SAMPLESPERPIXEL, &img->samplesperpixel);
     269               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_EXTRASAMPLES,
     270                 :       &extrasamples, &sampleinfo);
     271               8 :   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               8 :   if( !TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &img->photometric))
     287               0 :     img->photometric = PHOTOMETRIC_MINISWHITE;
     288                 : 
     289              20 :   if( extrasamples == 0
     290               8 :       && img->samplesperpixel == 4
     291              12 :       && img->photometric == PHOTOMETRIC_RGB )
     292                 :   {
     293               0 :     img->alpha = EXTRASAMPLE_ASSOCALPHA;
     294               0 :     extrasamples = 1;
     295                 :   }
     296                 : #endif
     297                 : 
     298               8 :   colorchannels = img->samplesperpixel - extrasamples;
     299               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress);
     300               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_PLANARCONFIG, &planarconfig);
     301               8 :   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               8 :   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               2 :       if (planarconfig == PLANARCONFIG_CONTIG)
     357                 :         /* can rely on libjpeg to convert to RGB */
     358                 :         /* XXX should restore current state on exit */
     359               2 :         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               2 :       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               4 :         TIFFGetFieldDefaulted(tif, TIFFTAG_INKSET, &inkset);
     391               4 :         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               4 :         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               4 :       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               2 :       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               8 :   img->Map = NULL;
     436               8 :   img->BWmap = NULL;
     437               8 :   img->PALmap = NULL;
     438               8 :   img->ycbcr = NULL;
     439               8 :   img->cielab = NULL;
     440               8 :   img->UaToAa = NULL;
     441               8 :   img->Bitdepth16To8 = NULL;
     442               8 :   TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &img->width);
     443               8 :   TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &img->height);
     444               8 :   TIFFGetFieldDefaulted(tif, TIFFTAG_ORIENTATION, &img->orientation);
     445               8 :   img->isContig =
     446               8 :       !(planarconfig == PLANARCONFIG_SEPARATE && img->samplesperpixel > 1);
     447               8 :   if (img->isContig) {
     448               8 :     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               8 :   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               8 : TIFFRGBAImageGet(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     470                 : {
     471               8 :     if (img->get == NULL) {
     472               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No \"get\" routine setup");
     473               0 :     return (0);
     474                 :   }
     475               8 :   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               8 :     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               8 : setorientation(TIFFRGBAImage* img)
     523                 : {
     524               8 :   switch (img->orientation) {
     525                 :     case ORIENTATION_TOPLEFT:
     526                 :     case ORIENTATION_LEFTTOP:
     527              16 :       if (img->req_orientation == ORIENTATION_TOPRIGHT ||
     528               8 :           img->req_orientation == ORIENTATION_RIGHTTOP)
     529               0 :         return FLIP_HORIZONTALLY;
     530              16 :       else if (img->req_orientation == ORIENTATION_BOTRIGHT ||
     531               8 :           img->req_orientation == ORIENTATION_RIGHTBOT)
     532               0 :         return FLIP_HORIZONTALLY | FLIP_VERTICALLY;
     533               8 :       else if (img->req_orientation == ORIENTATION_BOTLEFT ||
     534               0 :           img->req_orientation == ORIENTATION_LEFTBOT)
     535               8 :         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               2 : gtTileContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     590                 : {
     591               2 :     TIFF* tif = img->tif;
     592               2 :     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               2 :     int ret = 1, flip;
     600                 : 
     601               2 :     buf = (unsigned char*) _TIFFmalloc(TIFFTileSize(tif));
     602               2 :     if (buf == 0) {
     603               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", "No space for tile buffer");
     604               0 :     return (0);
     605                 :     }
     606               2 :     _TIFFmemset(buf, 0, TIFFTileSize(tif));
     607               2 :     TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);
     608               2 :     TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);
     609                 : 
     610               2 :     flip = setorientation(img);
     611               2 :     if (flip & FLIP_VERTICALLY) {
     612               2 :       y = h - 1;
     613               2 :       toskew = -(int32)(tw + w);
     614                 :     }
     615                 :     else {
     616               0 :       y = 0;
     617               0 :       toskew = -(int32)(tw - w);
     618                 :     }
     619                 :      
     620               4 :     for (row = 0; row < h; row += nrow)
     621                 :     {
     622               2 :         rowstoread = th - (row + img->row_offset) % th;
     623               2 :       nrow = (row + rowstoread > h ? h - row : rowstoread);
     624               4 :   for (col = 0; col < w; col += tw) 
     625                 :         {
     626               2 :       if (TIFFReadTile(tif, buf, col+img->col_offset,  
     627               2 :            row+img->row_offset, 0, 0)==(tmsize_t)(-1) && img->stoponerr)
     628                 :             {
     629               0 :                 ret = 0;
     630               0 :                 break;
     631                 :             }
     632                 :       
     633               2 :       pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);  
     634                 : 
     635               2 :           if (col + tw > w) 
     636                 :             {
     637                 :                 /*
     638                 :                  * Tile is clipped horizontally.  Calculate
     639                 :                  * visible portion and skewing factors.
     640                 :                  */
     641               2 :                 uint32 npix = w - col;
     642               2 :                 fromskew = tw - npix;
     643               2 :                 (*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               2 :         y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
     653                 :     }
     654               2 :     _TIFFfree(buf);
     655                 : 
     656               2 :     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               2 :     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               6 : gtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h)
     832                 : {
     833               6 :   TIFF* tif = img->tif;
     834               6 :   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               6 :   uint32 imagewidth = img->width;
     841                 :   tmsize_t scanline;
     842                 :   int32 fromskew, toskew;
     843               6 :   int ret = 1, flip;
     844                 : 
     845               6 :   buf = (unsigned char*) _TIFFmalloc(TIFFStripSize(tif));
     846               6 :   if (buf == 0) {
     847               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "No space for strip buffer");
     848               0 :     return (0);
     849                 :   }
     850               6 :   _TIFFmemset(buf, 0, TIFFStripSize(tif));
     851                 : 
     852               6 :   flip = setorientation(img);
     853               6 :   if (flip & FLIP_VERTICALLY) {
     854               6 :     y = h - 1;
     855               6 :     toskew = -(int32)(w + w);
     856                 :   } else {
     857               0 :     y = 0;
     858               0 :     toskew = -(int32)(w - w);
     859                 :   }
     860                 : 
     861               6 :   TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
     862               6 :   TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, &subsamplinghor, &subsamplingver);
     863               6 :   scanline = TIFFScanlineSize(tif);
     864               6 :   fromskew = (w < imagewidth ? imagewidth - w : 0);
     865              12 :   for (row = 0; row < h; row += nrow)
     866                 :   {
     867               6 :     rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;
     868               6 :     nrow = (row + rowstoread > h ? h - row : rowstoread);
     869               6 :     nrowsub = nrow;
     870               6 :     if ((nrowsub%subsamplingver)!=0)
     871               2 :       nrowsub+=subsamplingver-nrowsub%subsamplingver;
     872               6 :     if (TIFFReadEncodedStrip(tif,
     873                 :         TIFFComputeStrip(tif,row+img->row_offset, 0),
     874                 :         buf,
     875               6 :         ((row + img->row_offset)%rowsperstrip + nrowsub) * scanline)==(tmsize_t)(-1)
     876               6 :         && img->stoponerr)
     877                 :     {
     878               0 :       ret = 0;
     879               0 :       break;
     880                 :     }
     881                 : 
     882               6 :     pos = ((row + img->row_offset) % rowsperstrip) * scanline;
     883               6 :     (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);
     884               6 :     y += (flip & FLIP_VERTICALLY ? -(int32) nrow : (int32) nrow);
     885                 :   }
     886                 : 
     887               6 :   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               6 :   _TIFFfree(buf);
     904               6 :   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                 :  * 16-bit greyscale => colormap/RGB
    1213                 :  */
    1214               0 : DECLAREContigPutFunc(put16bitbwtile)
    1215                 : {
    1216               0 :     int samplesperpixel = img->samplesperpixel;
    1217               0 :     uint32** BWmap = img->BWmap;
    1218                 : 
    1219                 :     (void) y;
    1220               0 :     while (h-- > 0) {
    1221               0 :         uint16 *wp = (uint16 *) pp;
    1222                 : 
    1223               0 :   for (x = w; x-- > 0;)
    1224                 :         {
    1225                 :             /* use high order byte of 16bit value */
    1226                 : 
    1227               0 :       *cp++ = BWmap[*wp >> 8][0];
    1228               0 :             pp += 2 * samplesperpixel;
    1229               0 :             wp += samplesperpixel;
    1230                 :         }
    1231               0 :   cp += toskew;
    1232               0 :   pp += fromskew;
    1233                 :     }
    1234               0 : }
    1235                 : 
    1236                 : /*
    1237                 :  * 1-bit bilevel => colormap/RGB
    1238                 :  */
    1239               0 : DECLAREContigPutFunc(put1bitbwtile)
    1240                 : {
    1241               0 :     uint32** BWmap = img->BWmap;
    1242                 : 
    1243                 :     (void) x; (void) y;
    1244               0 :     fromskew /= 8;
    1245               0 :     while (h-- > 0) {
    1246                 :   uint32* bw;
    1247               0 :   UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1248               0 :   cp += toskew;
    1249               0 :   pp += fromskew;
    1250                 :     }
    1251               0 : }
    1252                 : 
    1253                 : /*
    1254                 :  * 2-bit greyscale => colormap/RGB
    1255                 :  */
    1256               0 : DECLAREContigPutFunc(put2bitbwtile)
    1257                 : {
    1258               0 :     uint32** BWmap = img->BWmap;
    1259                 : 
    1260                 :     (void) x; (void) y;
    1261               0 :     fromskew /= 4;
    1262               0 :     while (h-- > 0) {
    1263                 :   uint32* bw;
    1264               0 :   UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1265               0 :   cp += toskew;
    1266               0 :   pp += fromskew;
    1267                 :     }
    1268               0 : }
    1269                 : 
    1270                 : /*
    1271                 :  * 4-bit greyscale => colormap/RGB
    1272                 :  */
    1273               0 : DECLAREContigPutFunc(put4bitbwtile)
    1274                 : {
    1275               0 :     uint32** BWmap = img->BWmap;
    1276                 : 
    1277                 :     (void) x; (void) y;
    1278               0 :     fromskew /= 2;
    1279               0 :     while (h-- > 0) {
    1280                 :   uint32* bw;
    1281               0 :   UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);
    1282               0 :   cp += toskew;
    1283               0 :   pp += fromskew;
    1284                 :     }
    1285               0 : }
    1286                 : 
    1287                 : /*
    1288                 :  * 8-bit packed samples, no Map => RGB
    1289                 :  */
    1290               0 : DECLAREContigPutFunc(putRGBcontig8bittile)
    1291                 : {
    1292               0 :     int samplesperpixel = img->samplesperpixel;
    1293                 : 
    1294                 :     (void) x; (void) y;
    1295               0 :     fromskew *= samplesperpixel;
    1296               0 :     while (h-- > 0) {
    1297               0 :   UNROLL8(w, NOP,
    1298                 :       *cp++ = PACK(pp[0], pp[1], pp[2]);
    1299                 :       pp += samplesperpixel);
    1300               0 :   cp += toskew;
    1301               0 :   pp += fromskew;
    1302                 :     }
    1303               0 : }
    1304                 : 
    1305                 : /*
    1306                 :  * 8-bit packed samples => RGBA w/ associated alpha
    1307                 :  * (known to have Map == NULL)
    1308                 :  */
    1309               0 : DECLAREContigPutFunc(putRGBAAcontig8bittile)
    1310                 : {
    1311               0 :     int samplesperpixel = img->samplesperpixel;
    1312                 : 
    1313                 :     (void) x; (void) y;
    1314               0 :     fromskew *= samplesperpixel;
    1315               0 :     while (h-- > 0) {
    1316               0 :   UNROLL8(w, NOP,
    1317                 :       *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);
    1318                 :       pp += samplesperpixel);
    1319               0 :   cp += toskew;
    1320               0 :   pp += fromskew;
    1321                 :     }
    1322               0 : }
    1323                 : 
    1324                 : /*
    1325                 :  * 8-bit packed samples => RGBA w/ unassociated alpha
    1326                 :  * (known to have Map == NULL)
    1327                 :  */
    1328               0 : DECLAREContigPutFunc(putRGBUAcontig8bittile)
    1329                 : {
    1330               0 :   int samplesperpixel = img->samplesperpixel;
    1331                 :   (void) y;
    1332               0 :   fromskew *= samplesperpixel;
    1333               0 :   while (h-- > 0) {
    1334                 :     uint32 r, g, b, a;
    1335                 :     uint8* m;
    1336               0 :     for (x = w; x-- > 0;) {
    1337               0 :       a = pp[3];
    1338               0 :       m = img->UaToAa+(a<<8);
    1339               0 :       r = m[pp[0]];
    1340               0 :       g = m[pp[1]];
    1341               0 :       b = m[pp[2]];
    1342               0 :       *cp++ = PACK4(r,g,b,a);
    1343               0 :       pp += samplesperpixel;
    1344                 :     }
    1345               0 :     cp += toskew;
    1346               0 :     pp += fromskew;
    1347                 :   }
    1348               0 : }
    1349                 : 
    1350                 : /*
    1351                 :  * 16-bit packed samples => RGB
    1352                 :  */
    1353               0 : DECLAREContigPutFunc(putRGBcontig16bittile)
    1354                 : {
    1355               0 :   int samplesperpixel = img->samplesperpixel;
    1356               0 :   uint16 *wp = (uint16 *)pp;
    1357                 :   (void) y;
    1358               0 :   fromskew *= samplesperpixel;
    1359               0 :   while (h-- > 0) {
    1360               0 :     for (x = w; x-- > 0;) {
    1361               0 :       *cp++ = PACK(img->Bitdepth16To8[wp[0]],
    1362                 :           img->Bitdepth16To8[wp[1]],
    1363                 :           img->Bitdepth16To8[wp[2]]);
    1364               0 :       wp += samplesperpixel;
    1365                 :     }
    1366               0 :     cp += toskew;
    1367               0 :     wp += fromskew;
    1368                 :   }
    1369               0 : }
    1370                 : 
    1371                 : /*
    1372                 :  * 16-bit packed samples => RGBA w/ associated alpha
    1373                 :  * (known to have Map == NULL)
    1374                 :  */
    1375               0 : DECLAREContigPutFunc(putRGBAAcontig16bittile)
    1376                 : {
    1377               0 :   int samplesperpixel = img->samplesperpixel;
    1378               0 :   uint16 *wp = (uint16 *)pp;
    1379                 :   (void) y;
    1380               0 :   fromskew *= samplesperpixel;
    1381               0 :   while (h-- > 0) {
    1382               0 :     for (x = w; x-- > 0;) {
    1383               0 :       *cp++ = PACK4(img->Bitdepth16To8[wp[0]],
    1384                 :           img->Bitdepth16To8[wp[1]],
    1385                 :           img->Bitdepth16To8[wp[2]],
    1386                 :           img->Bitdepth16To8[wp[3]]);
    1387               0 :       wp += samplesperpixel;
    1388                 :     }
    1389               0 :     cp += toskew;
    1390               0 :     wp += fromskew;
    1391                 :   }
    1392               0 : }
    1393                 : 
    1394                 : /*
    1395                 :  * 16-bit packed samples => RGBA w/ unassociated alpha
    1396                 :  * (known to have Map == NULL)
    1397                 :  */
    1398               0 : DECLAREContigPutFunc(putRGBUAcontig16bittile)
    1399                 : {
    1400               0 :   int samplesperpixel = img->samplesperpixel;
    1401               0 :   uint16 *wp = (uint16 *)pp;
    1402                 :   (void) y;
    1403               0 :   fromskew *= samplesperpixel;
    1404               0 :   while (h-- > 0) {
    1405                 :     uint32 r,g,b,a;
    1406                 :     uint8* m;
    1407               0 :     for (x = w; x-- > 0;) {
    1408               0 :       a = img->Bitdepth16To8[wp[3]];
    1409               0 :       m = img->UaToAa+(a<<8);
    1410               0 :       r = m[img->Bitdepth16To8[wp[0]]];
    1411               0 :       g = m[img->Bitdepth16To8[wp[1]]];
    1412               0 :       b = m[img->Bitdepth16To8[wp[2]]];
    1413               0 :       *cp++ = PACK4(r,g,b,a);
    1414               0 :       wp += samplesperpixel;
    1415                 :     }
    1416               0 :     cp += toskew;
    1417               0 :     wp += fromskew;
    1418                 :   }
    1419               0 : }
    1420                 : 
    1421                 : /*
    1422                 :  * 8-bit packed CMYK samples w/o Map => RGB
    1423                 :  *
    1424                 :  * NB: The conversion of CMYK->RGB is *very* crude.
    1425                 :  */
    1426               4 : DECLAREContigPutFunc(putRGBcontig8bitCMYKtile)
    1427                 : {
    1428               4 :     int samplesperpixel = img->samplesperpixel;
    1429                 :     uint16 r, g, b, k;
    1430                 : 
    1431                 :     (void) x; (void) y;
    1432               4 :     fromskew *= samplesperpixel;
    1433             108 :     while (h-- > 0) {
    1434             100 :   UNROLL8(w, NOP,
    1435                 :       k = 255 - pp[3];
    1436                 :       r = (k*(255-pp[0]))/255;
    1437                 :       g = (k*(255-pp[1]))/255;
    1438                 :       b = (k*(255-pp[2]))/255;
    1439                 :       *cp++ = PACK(r, g, b);
    1440                 :       pp += samplesperpixel);
    1441             100 :   cp += toskew;
    1442             100 :   pp += fromskew;
    1443                 :     }
    1444               4 : }
    1445                 : 
    1446                 : /*
    1447                 :  * 8-bit packed CMYK samples w/Map => RGB
    1448                 :  *
    1449                 :  * NB: The conversion of CMYK->RGB is *very* crude.
    1450                 :  */
    1451               0 : DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile)
    1452                 : {
    1453               0 :     int samplesperpixel = img->samplesperpixel;
    1454               0 :     TIFFRGBValue* Map = img->Map;
    1455                 :     uint16 r, g, b, k;
    1456                 : 
    1457                 :     (void) y;
    1458               0 :     fromskew *= samplesperpixel;
    1459               0 :     while (h-- > 0) {
    1460               0 :   for (x = w; x-- > 0;) {
    1461               0 :       k = 255 - pp[3];
    1462               0 :       r = (k*(255-pp[0]))/255;
    1463               0 :       g = (k*(255-pp[1]))/255;
    1464               0 :       b = (k*(255-pp[2]))/255;
    1465               0 :       *cp++ = PACK(Map[r], Map[g], Map[b]);
    1466               0 :       pp += samplesperpixel;
    1467                 :   }
    1468               0 :   pp += fromskew;
    1469               0 :   cp += toskew;
    1470                 :     }
    1471               0 : }
    1472                 : 
    1473                 : #define DECLARESepPutFunc(name) \
    1474                 : static void name(\
    1475                 :     TIFFRGBAImage* img,\
    1476                 :     uint32* cp,\
    1477                 :     uint32 x, uint32 y, \
    1478                 :     uint32 w, uint32 h,\
    1479                 :     int32 fromskew, int32 toskew,\
    1480                 :     unsigned char* r, unsigned char* g, unsigned char* b, unsigned char* a\
    1481                 : )
    1482                 : 
    1483                 : /*
    1484                 :  * 8-bit unpacked samples => RGB
    1485                 :  */
    1486               0 : DECLARESepPutFunc(putRGBseparate8bittile)
    1487                 : {
    1488                 :     (void) img; (void) x; (void) y; (void) a;
    1489               0 :     while (h-- > 0) {
    1490               0 :   UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));
    1491               0 :   SKEW(r, g, b, fromskew);
    1492               0 :   cp += toskew;
    1493                 :     }
    1494               0 : }
    1495                 : 
    1496                 : /*
    1497                 :  * 8-bit unpacked samples => RGBA w/ associated alpha
    1498                 :  */
    1499               0 : DECLARESepPutFunc(putRGBAAseparate8bittile)
    1500                 : {
    1501                 :   (void) img; (void) x; (void) y; 
    1502               0 :   while (h-- > 0) {
    1503               0 :     UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));
    1504               0 :     SKEW4(r, g, b, a, fromskew);
    1505               0 :     cp += toskew;
    1506                 :   }
    1507               0 : }
    1508                 : 
    1509                 : /*
    1510                 :  * 8-bit unpacked CMYK samples => RGBA
    1511                 :  */
    1512               0 : DECLARESepPutFunc(putCMYKseparate8bittile)
    1513                 : {
    1514                 :   (void) img; (void) y;
    1515               0 :   while (h-- > 0) {
    1516                 :     uint32 rv, gv, bv, kv;
    1517                 :     uint8* m;
    1518               0 :     for (x = w; x-- > 0;) {
    1519               0 :       kv = 255 - *a++;
    1520               0 :       rv = (kv*(255-*r++))/255;
    1521               0 :       gv = (kv*(255-*g++))/255;
    1522               0 :       bv = (kv*(255-*b++))/255;
    1523               0 :       *cp++ = PACK4(rv,gv,bv,255);
    1524                 :     }
    1525               0 :     SKEW4(r, g, b, a, fromskew);
    1526               0 :     cp += toskew;
    1527                 :   }
    1528               0 : }
    1529                 : 
    1530                 : /*
    1531                 :  * 8-bit unpacked samples => RGBA w/ unassociated alpha
    1532                 :  */
    1533               0 : DECLARESepPutFunc(putRGBUAseparate8bittile)
    1534                 : {
    1535                 :   (void) img; (void) y;
    1536               0 :   while (h-- > 0) {
    1537                 :     uint32 rv, gv, bv, av;
    1538                 :     uint8* m;
    1539               0 :     for (x = w; x-- > 0;) {
    1540               0 :       av = *a++;
    1541               0 :       m = img->UaToAa+(av<<8);
    1542               0 :       rv = m[*r++];
    1543               0 :       gv = m[*g++];
    1544               0 :       bv = m[*b++];
    1545               0 :       *cp++ = PACK4(rv,gv,bv,av);
    1546                 :     }
    1547               0 :     SKEW4(r, g, b, a, fromskew);
    1548               0 :     cp += toskew;
    1549                 :   }
    1550               0 : }
    1551                 : 
    1552                 : /*
    1553                 :  * 16-bit unpacked samples => RGB
    1554                 :  */
    1555               0 : DECLARESepPutFunc(putRGBseparate16bittile)
    1556                 : {
    1557               0 :   uint16 *wr = (uint16*) r;
    1558               0 :   uint16 *wg = (uint16*) g;
    1559               0 :   uint16 *wb = (uint16*) b;
    1560                 :   (void) img; (void) y; (void) a;
    1561               0 :   while (h-- > 0) {
    1562               0 :     for (x = 0; x < w; x++)
    1563               0 :       *cp++ = PACK(img->Bitdepth16To8[*wr++],
    1564                 :           img->Bitdepth16To8[*wg++],
    1565                 :           img->Bitdepth16To8[*wb++]);
    1566               0 :     SKEW(wr, wg, wb, fromskew);
    1567               0 :     cp += toskew;
    1568                 :   }
    1569               0 : }
    1570                 : 
    1571                 : /*
    1572                 :  * 16-bit unpacked samples => RGBA w/ associated alpha
    1573                 :  */
    1574               0 : DECLARESepPutFunc(putRGBAAseparate16bittile)
    1575                 : {
    1576               0 :   uint16 *wr = (uint16*) r;
    1577               0 :   uint16 *wg = (uint16*) g;
    1578               0 :   uint16 *wb = (uint16*) b;
    1579               0 :   uint16 *wa = (uint16*) a;
    1580                 :   (void) img; (void) y;
    1581               0 :   while (h-- > 0) {
    1582               0 :     for (x = 0; x < w; x++)
    1583               0 :       *cp++ = PACK4(img->Bitdepth16To8[*wr++],
    1584                 :           img->Bitdepth16To8[*wg++],
    1585                 :           img->Bitdepth16To8[*wb++],
    1586                 :           img->Bitdepth16To8[*wa++]);
    1587               0 :     SKEW4(wr, wg, wb, wa, fromskew);
    1588               0 :     cp += toskew;
    1589                 :   }
    1590               0 : }
    1591                 : 
    1592                 : /*
    1593                 :  * 16-bit unpacked samples => RGBA w/ unassociated alpha
    1594                 :  */
    1595               0 : DECLARESepPutFunc(putRGBUAseparate16bittile)
    1596                 : {
    1597               0 :   uint16 *wr = (uint16*) r;
    1598               0 :   uint16 *wg = (uint16*) g;
    1599               0 :   uint16 *wb = (uint16*) b;
    1600               0 :   uint16 *wa = (uint16*) a;
    1601                 :   (void) img; (void) y;
    1602               0 :   while (h-- > 0) {
    1603                 :     uint32 r,g,b,a;
    1604                 :     uint8* m;
    1605               0 :     for (x = w; x-- > 0;) {
    1606               0 :       a = img->Bitdepth16To8[*wa++];
    1607               0 :       m = img->UaToAa+(a<<8);
    1608               0 :       r = m[img->Bitdepth16To8[*wr++]];
    1609               0 :       g = m[img->Bitdepth16To8[*wg++]];
    1610               0 :       b = m[img->Bitdepth16To8[*wb++]];
    1611               0 :       *cp++ = PACK4(r,g,b,a);
    1612                 :     }
    1613               0 :     SKEW4(wr, wg, wb, wa, fromskew);
    1614               0 :     cp += toskew;
    1615                 :   }
    1616               0 : }
    1617                 : 
    1618                 : /*
    1619                 :  * 8-bit packed CIE L*a*b 1976 samples => RGB
    1620                 :  */
    1621               2 : DECLAREContigPutFunc(putcontig8bitCIELab)
    1622                 : {
    1623                 :   float X, Y, Z;
    1624                 :   uint32 r, g, b;
    1625                 :   (void) y;
    1626               2 :   fromskew *= 3;
    1627               6 :   while (h-- > 0) {
    1628               6 :     for (x = w; x-- > 0;) {
    1629               6 :       TIFFCIELabToXYZ(img->cielab,
    1630               2 :           (unsigned char)pp[0],
    1631               2 :           (signed char)pp[1],
    1632               2 :           (signed char)pp[2],
    1633                 :           &X, &Y, &Z);
    1634               2 :       TIFFXYZToRGB(img->cielab, X, Y, Z, &r, &g, &b);
    1635               2 :       *cp++ = PACK(r, g, b);
    1636               2 :       pp += 3;
    1637                 :     }
    1638               2 :     cp += toskew;
    1639               2 :     pp += fromskew;
    1640                 :   }
    1641               2 : }
    1642                 : 
    1643                 : /*
    1644                 :  * YCbCr -> RGB conversion and packing routines.
    1645                 :  */
    1646                 : 
    1647                 : #define YCbCrtoRGB(dst, Y) {            \
    1648                 :   uint32 r, g, b;             \
    1649                 :   TIFFYCbCrtoRGB(img->ycbcr, (Y), Cb, Cr, &r, &g, &b);   \
    1650                 :   dst = PACK(r, g, b);            \
    1651                 : }
    1652                 : 
    1653                 : /*
    1654                 :  * 8-bit packed YCbCr samples => RGB 
    1655                 :  * This function is generic for different sampling sizes, 
    1656                 :  * and can handle blocks sizes that aren't multiples of the
    1657                 :  * sampling size.  However, it is substantially less optimized
    1658                 :  * than the specific sampling cases.  It is used as a fallback
    1659                 :  * for difficult blocks.
    1660                 :  */
    1661                 : #ifdef notdef
    1662                 : static void putcontig8bitYCbCrGenericTile( 
    1663                 :     TIFFRGBAImage* img, 
    1664                 :     uint32* cp, 
    1665                 :     uint32 x, uint32 y, 
    1666                 :     uint32 w, uint32 h, 
    1667                 :     int32 fromskew, int32 toskew, 
    1668                 :     unsigned char* pp,
    1669                 :     int h_group, 
    1670                 :     int v_group )
    1671                 : 
    1672                 : {
    1673                 :     uint32* cp1 = cp+w+toskew;
    1674                 :     uint32* cp2 = cp1+w+toskew;
    1675                 :     uint32* cp3 = cp2+w+toskew;
    1676                 :     int32 incr = 3*w+4*toskew;
    1677                 :     int32   Cb, Cr;
    1678                 :     int     group_size = v_group * h_group + 2;
    1679                 : 
    1680                 :     (void) y;
    1681                 :     fromskew = (fromskew * group_size) / h_group;
    1682                 : 
    1683                 :     for( yy = 0; yy < h; yy++ )
    1684                 :     {
    1685                 :         unsigned char *pp_line;
    1686                 :         int     y_line_group = yy / v_group;
    1687                 :         int     y_remainder = yy - y_line_group * v_group;
    1688                 : 
    1689                 :         pp_line = pp + v_line_group * 
    1690                 : 
    1691                 :         
    1692                 :         for( xx = 0; xx < w; xx++ )
    1693                 :         {
    1694                 :             Cb = pp
    1695                 :         }
    1696                 :     }
    1697                 :     for (; h >= 4; h -= 4) {
    1698                 :   x = w>>2;
    1699                 :   do {
    1700                 :       Cb = pp[16];
    1701                 :       Cr = pp[17];
    1702                 : 
    1703                 :       YCbCrtoRGB(cp [0], pp[ 0]);
    1704                 :       YCbCrtoRGB(cp [1], pp[ 1]);
    1705                 :       YCbCrtoRGB(cp [2], pp[ 2]);
    1706                 :       YCbCrtoRGB(cp [3], pp[ 3]);
    1707                 :       YCbCrtoRGB(cp1[0], pp[ 4]);
    1708                 :       YCbCrtoRGB(cp1[1], pp[ 5]);
    1709                 :       YCbCrtoRGB(cp1[2], pp[ 6]);
    1710                 :       YCbCrtoRGB(cp1[3], pp[ 7]);
    1711                 :       YCbCrtoRGB(cp2[0], pp[ 8]);
    1712                 :       YCbCrtoRGB(cp2[1], pp[ 9]);
    1713                 :       YCbCrtoRGB(cp2[2], pp[10]);
    1714                 :       YCbCrtoRGB(cp2[3], pp[11]);
    1715                 :       YCbCrtoRGB(cp3[0], pp[12]);
    1716                 :       YCbCrtoRGB(cp3[1], pp[13]);
    1717                 :       YCbCrtoRGB(cp3[2], pp[14]);
    1718                 :       YCbCrtoRGB(cp3[3], pp[15]);
    1719                 : 
    1720                 :       cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
    1721                 :       pp += 18;
    1722                 :   } while (--x);
    1723                 :   cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1724                 :   pp += fromskew;
    1725                 :     }
    1726                 : }
    1727                 : #endif
    1728                 : 
    1729                 : /*
    1730                 :  * 8-bit packed YCbCr samples w/ 4,4 subsampling => RGB
    1731                 :  */
    1732               0 : DECLAREContigPutFunc(putcontig8bitYCbCr44tile)
    1733                 : {
    1734               0 :     uint32* cp1 = cp+w+toskew;
    1735               0 :     uint32* cp2 = cp1+w+toskew;
    1736               0 :     uint32* cp3 = cp2+w+toskew;
    1737               0 :     int32 incr = 3*w+4*toskew;
    1738                 : 
    1739                 :     (void) y;
    1740                 :     /* adjust fromskew */
    1741               0 :     fromskew = (fromskew * 18) / 4;
    1742               0 :     if ((h & 3) == 0 && (w & 3) == 0) {               
    1743               0 :         for (; h >= 4; h -= 4) {
    1744               0 :             x = w>>2;
    1745                 :             do {
    1746               0 :                 int32 Cb = pp[16];
    1747               0 :                 int32 Cr = pp[17];
    1748                 : 
    1749               0 :                 YCbCrtoRGB(cp [0], pp[ 0]);
    1750               0 :                 YCbCrtoRGB(cp [1], pp[ 1]);
    1751               0 :                 YCbCrtoRGB(cp [2], pp[ 2]);
    1752               0 :                 YCbCrtoRGB(cp [3], pp[ 3]);
    1753               0 :                 YCbCrtoRGB(cp1[0], pp[ 4]);
    1754               0 :                 YCbCrtoRGB(cp1[1], pp[ 5]);
    1755               0 :                 YCbCrtoRGB(cp1[2], pp[ 6]);
    1756               0 :                 YCbCrtoRGB(cp1[3], pp[ 7]);
    1757               0 :                 YCbCrtoRGB(cp2[0], pp[ 8]);
    1758               0 :                 YCbCrtoRGB(cp2[1], pp[ 9]);
    1759               0 :                 YCbCrtoRGB(cp2[2], pp[10]);
    1760               0 :                 YCbCrtoRGB(cp2[3], pp[11]);
    1761               0 :                 YCbCrtoRGB(cp3[0], pp[12]);
    1762               0 :                 YCbCrtoRGB(cp3[1], pp[13]);
    1763               0 :                 YCbCrtoRGB(cp3[2], pp[14]);
    1764               0 :                 YCbCrtoRGB(cp3[3], pp[15]);
    1765                 : 
    1766               0 :                 cp += 4, cp1 += 4, cp2 += 4, cp3 += 4;
    1767               0 :                 pp += 18;
    1768               0 :             } while (--x);
    1769               0 :             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1770               0 :             pp += fromskew;
    1771                 :         }
    1772                 :     } else {
    1773               0 :         while (h > 0) {
    1774               0 :             for (x = w; x > 0;) {
    1775               0 :                 int32 Cb = pp[16];
    1776               0 :                 int32 Cr = pp[17];
    1777               0 :                 switch (x) {
    1778                 :                 default:
    1779               0 :                     switch (h) {
    1780               0 :                     default: YCbCrtoRGB(cp3[3], pp[15]); /* FALLTHROUGH */
    1781               0 :                     case 3:  YCbCrtoRGB(cp2[3], pp[11]); /* FALLTHROUGH */
    1782               0 :                     case 2:  YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
    1783               0 :                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
    1784                 :                     }                                    /* FALLTHROUGH */
    1785                 :                 case 3:
    1786               0 :                     switch (h) {
    1787               0 :                     default: YCbCrtoRGB(cp3[2], pp[14]); /* FALLTHROUGH */
    1788               0 :                     case 3:  YCbCrtoRGB(cp2[2], pp[10]); /* FALLTHROUGH */
    1789               0 :                     case 2:  YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
    1790               0 :                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
    1791                 :                     }                                    /* FALLTHROUGH */
    1792                 :                 case 2:
    1793               0 :                     switch (h) {
    1794               0 :                     default: YCbCrtoRGB(cp3[1], pp[13]); /* FALLTHROUGH */
    1795               0 :                     case 3:  YCbCrtoRGB(cp2[1], pp[ 9]); /* FALLTHROUGH */
    1796               0 :                     case 2:  YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
    1797               0 :                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
    1798                 :                     }                                    /* FALLTHROUGH */
    1799                 :                 case 1:
    1800               0 :                     switch (h) {
    1801               0 :                     default: YCbCrtoRGB(cp3[0], pp[12]); /* FALLTHROUGH */
    1802               0 :                     case 3:  YCbCrtoRGB(cp2[0], pp[ 8]); /* FALLTHROUGH */
    1803               0 :                     case 2:  YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
    1804               0 :                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
    1805                 :                     }                                    /* FALLTHROUGH */
    1806                 :                 }
    1807               0 :                 if (x < 4) {
    1808               0 :                     cp += x; cp1 += x; cp2 += x; cp3 += x;
    1809               0 :                     x = 0;
    1810                 :                 }
    1811                 :                 else {
    1812               0 :                     cp += 4; cp1 += 4; cp2 += 4; cp3 += 4;
    1813               0 :                     x -= 4;
    1814                 :                 }
    1815               0 :                 pp += 18;
    1816                 :             }
    1817               0 :             if (h <= 4)
    1818               0 :                 break;
    1819               0 :             h -= 4;
    1820               0 :             cp += incr, cp1 += incr, cp2 += incr, cp3 += incr;
    1821               0 :             pp += fromskew;
    1822                 :         }
    1823                 :     }
    1824               0 : }
    1825                 : 
    1826                 : /*
    1827                 :  * 8-bit packed YCbCr samples w/ 4,2 subsampling => RGB
    1828                 :  */
    1829               0 : DECLAREContigPutFunc(putcontig8bitYCbCr42tile)
    1830                 : {
    1831               0 :     uint32* cp1 = cp+w+toskew;
    1832               0 :     int32 incr = 2*toskew+w;
    1833                 : 
    1834                 :     (void) y;
    1835               0 :     fromskew = (fromskew * 10) / 4;
    1836               0 :     if ((h & 3) == 0 && (w & 1) == 0) {
    1837               0 :         for (; h >= 2; h -= 2) {
    1838               0 :             x = w>>2;
    1839                 :             do {
    1840               0 :                 int32 Cb = pp[8];
    1841               0 :                 int32 Cr = pp[9];
    1842                 :                 
    1843               0 :                 YCbCrtoRGB(cp [0], pp[0]);
    1844               0 :                 YCbCrtoRGB(cp [1], pp[1]);
    1845               0 :                 YCbCrtoRGB(cp [2], pp[2]);
    1846               0 :                 YCbCrtoRGB(cp [3], pp[3]);
    1847               0 :                 YCbCrtoRGB(cp1[0], pp[4]);
    1848               0 :                 YCbCrtoRGB(cp1[1], pp[5]);
    1849               0 :                 YCbCrtoRGB(cp1[2], pp[6]);
    1850               0 :                 YCbCrtoRGB(cp1[3], pp[7]);
    1851                 :                 
    1852               0 :                 cp += 4, cp1 += 4;
    1853               0 :                 pp += 10;
    1854               0 :             } while (--x);
    1855               0 :             cp += incr, cp1 += incr;
    1856               0 :             pp += fromskew;
    1857                 :         }
    1858                 :     } else {
    1859               0 :         while (h > 0) {
    1860               0 :             for (x = w; x > 0;) {
    1861               0 :                 int32 Cb = pp[8];
    1862               0 :                 int32 Cr = pp[9];
    1863               0 :                 switch (x) {
    1864                 :                 default:
    1865               0 :                     switch (h) {
    1866               0 :                     default: YCbCrtoRGB(cp1[3], pp[ 7]); /* FALLTHROUGH */
    1867               0 :                     case 1:  YCbCrtoRGB(cp [3], pp[ 3]); /* FALLTHROUGH */
    1868                 :                     }                                    /* FALLTHROUGH */
    1869                 :                 case 3:
    1870               0 :                     switch (h) {
    1871               0 :                     default: YCbCrtoRGB(cp1[2], pp[ 6]); /* FALLTHROUGH */
    1872               0 :                     case 1:  YCbCrtoRGB(cp [2], pp[ 2]); /* FALLTHROUGH */
    1873                 :                     }                                    /* FALLTHROUGH */
    1874                 :                 case 2:
    1875               0 :                     switch (h) {
    1876               0 :                     default: YCbCrtoRGB(cp1[1], pp[ 5]); /* FALLTHROUGH */
    1877               0 :                     case 1:  YCbCrtoRGB(cp [1], pp[ 1]); /* FALLTHROUGH */
    1878                 :                     }                                    /* FALLTHROUGH */
    1879                 :                 case 1:
    1880               0 :                     switch (h) {
    1881               0 :                     default: YCbCrtoRGB(cp1[0], pp[ 4]); /* FALLTHROUGH */
    1882               0 :                     case 1:  YCbCrtoRGB(cp [0], pp[ 0]); /* FALLTHROUGH */
    1883                 :                     }                                    /* FALLTHROUGH */
    1884                 :                 }
    1885               0 :                 if (x < 4) {
    1886               0 :                     cp += x; cp1 += x;
    1887               0 :                     x = 0;
    1888                 :                 }
    1889                 :                 else {
    1890               0 :                     cp += 4; cp1 += 4;
    1891               0 :                     x -= 4;
    1892                 :                 }
    1893               0 :                 pp += 10;
    1894                 :             }
    1895               0 :             if (h <= 2)
    1896               0 :                 break;
    1897               0 :             h -= 2;
    1898               0 :             cp += incr, cp1 += incr;
    1899               0 :             pp += fromskew;
    1900                 :         }
    1901                 :     }
    1902               0 : }
    1903                 : 
    1904                 : /*
    1905                 :  * 8-bit packed YCbCr samples w/ 4,1 subsampling => RGB
    1906                 :  */
    1907               0 : DECLAREContigPutFunc(putcontig8bitYCbCr41tile)
    1908                 : {
    1909                 :     (void) y;
    1910                 :     /* XXX adjust fromskew */
    1911                 :     do {
    1912               0 :   x = w>>2;
    1913                 :   do {
    1914               0 :       int32 Cb = pp[4];
    1915               0 :       int32 Cr = pp[5];
    1916                 : 
    1917               0 :       YCbCrtoRGB(cp [0], pp[0]);
    1918               0 :       YCbCrtoRGB(cp [1], pp[1]);
    1919               0 :       YCbCrtoRGB(cp [2], pp[2]);
    1920               0 :       YCbCrtoRGB(cp [3], pp[3]);
    1921                 : 
    1922               0 :       cp += 4;
    1923               0 :       pp += 6;
    1924               0 :   } while (--x);
    1925                 : 
    1926               0 :         if( (w&3) != 0 )
    1927                 :         {
    1928               0 :       int32 Cb = pp[4];
    1929               0 :       int32 Cr = pp[5];
    1930                 : 
    1931               0 :             switch( (w&3) ) {
    1932               0 :               case 3: YCbCrtoRGB(cp [2], pp[2]);
    1933               0 :               case 2: YCbCrtoRGB(cp [1], pp[1]);
    1934               0 :               case 1: YCbCrtoRGB(cp [0], pp[0]);
    1935                 :               case 0: break;
    1936                 :             }
    1937                 : 
    1938               0 :             cp += (w&3);
    1939               0 :             pp += 6;
    1940                 :         }
    1941                 : 
    1942               0 :   cp += toskew;
    1943               0 :   pp += fromskew;
    1944               0 :     } while (--h);
    1945                 : 
    1946               0 : }
    1947                 : 
    1948                 : /*
    1949                 :  * 8-bit packed YCbCr samples w/ 2,2 subsampling => RGB
    1950                 :  */
    1951               2 : DECLAREContigPutFunc(putcontig8bitYCbCr22tile)
    1952                 : {
    1953                 :   uint32* cp2;
    1954               2 :   int32 incr = 2*toskew+w;
    1955                 :   (void) y;
    1956               2 :   fromskew = (fromskew / 2) * 6;
    1957               2 :   cp2 = cp+w+toskew;
    1958             216 :   while (h>=2) {
    1959             212 :     x = w;
    1960           25228 :     while (x>=2) {
    1961           24804 :       uint32 Cb = pp[4];
    1962           24804 :       uint32 Cr = pp[5];
    1963           24804 :       YCbCrtoRGB(cp[0], pp[0]);
    1964           24804 :       YCbCrtoRGB(cp[1], pp[1]);
    1965           24804 :       YCbCrtoRGB(cp2[0], pp[2]);
    1966           24804 :       YCbCrtoRGB(cp2[1], pp[3]);
    1967           24804 :       cp += 2;
    1968           24804 :       cp2 += 2;
    1969           24804 :       pp += 6;
    1970           24804 :       x -= 2;
    1971                 :     }
    1972             212 :     if (x==1) {
    1973               0 :       uint32 Cb = pp[4];
    1974               0 :       uint32 Cr = pp[5];
    1975               0 :       YCbCrtoRGB(cp[0], pp[0]);
    1976               0 :       YCbCrtoRGB(cp2[0], pp[2]);
    1977               0 :       cp ++ ;
    1978               0 :       cp2 ++ ;
    1979               0 :       pp += 6;
    1980                 :     }
    1981             212 :     cp += incr;
    1982             212 :     cp2 += incr;
    1983             212 :     pp += fromskew;
    1984             212 :     h-=2;
    1985                 :   }
    1986               2 :   if (h==1) {
    1987               2 :     x = w;
    1988             238 :     while (x>=2) {
    1989             234 :       uint32 Cb = pp[4];
    1990             234 :       uint32 Cr = pp[5];
    1991             234 :       YCbCrtoRGB(cp[0], pp[0]);
    1992             234 :       YCbCrtoRGB(cp[1], pp[1]);
    1993             234 :       cp += 2;
    1994             234 :       cp2 += 2;
    1995             234 :       pp += 6;
    1996             234 :       x -= 2;
    1997                 :     }
    1998               2 :     if (x==1) {
    1999               0 :       uint32 Cb = pp[4];
    2000               0 :       uint32 Cr = pp[5];
    2001               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2002                 :     }
    2003                 :   }
    2004               2 : }
    2005                 : 
    2006                 : /*
    2007                 :  * 8-bit packed YCbCr samples w/ 2,1 subsampling => RGB
    2008                 :  */
    2009               0 : DECLAREContigPutFunc(putcontig8bitYCbCr21tile)
    2010                 : {
    2011                 :   (void) y;
    2012               0 :   fromskew = (fromskew * 4) / 2;
    2013                 :   do {
    2014               0 :     x = w>>1;
    2015                 :     do {
    2016               0 :       int32 Cb = pp[2];
    2017               0 :       int32 Cr = pp[3];
    2018                 : 
    2019               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2020               0 :       YCbCrtoRGB(cp[1], pp[1]);
    2021                 : 
    2022               0 :       cp += 2;
    2023               0 :       pp += 4;
    2024               0 :     } while (--x);
    2025                 : 
    2026               0 :     if( (w&1) != 0 )
    2027                 :     {
    2028               0 :       int32 Cb = pp[2];
    2029               0 :       int32 Cr = pp[3];
    2030                 : 
    2031               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2032                 : 
    2033               0 :       cp += 1;
    2034               0 :       pp += 4;
    2035                 :     }
    2036                 : 
    2037               0 :     cp += toskew;
    2038               0 :     pp += fromskew;
    2039               0 :   } while (--h);
    2040               0 : }
    2041                 : 
    2042                 : /*
    2043                 :  * 8-bit packed YCbCr samples w/ 1,2 subsampling => RGB
    2044                 :  */
    2045               0 : DECLAREContigPutFunc(putcontig8bitYCbCr12tile)
    2046                 : {
    2047                 :   uint32* cp2;
    2048               0 :   int32 incr = 2*toskew+w;
    2049                 :   (void) y;
    2050               0 :   fromskew = (fromskew / 2) * 4;
    2051               0 :   cp2 = cp+w+toskew;
    2052               0 :   while (h>=2) {
    2053               0 :     x = w;
    2054                 :     do {
    2055               0 :       uint32 Cb = pp[2];
    2056               0 :       uint32 Cr = pp[3];
    2057               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2058               0 :       YCbCrtoRGB(cp2[0], pp[1]);
    2059               0 :       cp ++;
    2060               0 :       cp2 ++;
    2061               0 :       pp += 4;
    2062               0 :     } while (--x);
    2063               0 :     cp += incr;
    2064               0 :     cp2 += incr;
    2065               0 :     pp += fromskew;
    2066               0 :     h-=2;
    2067                 :   }
    2068               0 :   if (h==1) {
    2069               0 :     x = w;
    2070                 :     do {
    2071               0 :       uint32 Cb = pp[2];
    2072               0 :       uint32 Cr = pp[3];
    2073               0 :       YCbCrtoRGB(cp[0], pp[0]);
    2074               0 :       cp ++;
    2075               0 :       pp += 4;
    2076               0 :     } while (--x);
    2077                 :   }
    2078               0 : }
    2079                 : 
    2080                 : /*
    2081                 :  * 8-bit packed YCbCr samples w/ no subsampling => RGB
    2082                 :  */
    2083               0 : DECLAREContigPutFunc(putcontig8bitYCbCr11tile)
    2084                 : {
    2085                 :   (void) y;
    2086               0 :   fromskew *= 3;
    2087                 :   do {
    2088               0 :     x = w; /* was x = w>>1; patched 2000/09/25 warmerda@home.com */
    2089                 :     do {
    2090               0 :       int32 Cb = pp[1];
    2091               0 :       int32 Cr = pp[2];
    2092                 : 
    2093               0 :       YCbCrtoRGB(*cp++, pp[0]);
    2094                 : 
    2095               0 :       pp += 3;
    2096               0 :     } while (--x);
    2097               0 :     cp += toskew;
    2098               0 :     pp += fromskew;
    2099               0 :   } while (--h);
    2100               0 : }
    2101                 : 
    2102                 : /*
    2103                 :  * 8-bit packed YCbCr samples w/ no subsampling => RGB
    2104                 :  */
    2105               0 : DECLARESepPutFunc(putseparate8bitYCbCr11tile)
    2106                 : {
    2107                 :   (void) y;
    2108                 :   (void) a;
    2109                 :   /* TODO: naming of input vars is still off, change obfuscating declaration inside define, or resolve obfuscation */
    2110               0 :   while (h-- > 0) {
    2111               0 :     x = w;
    2112                 :     do {
    2113                 :       uint32 dr, dg, db;
    2114               0 :       TIFFYCbCrtoRGB(img->ycbcr,*r++,*g++,*b++,&dr,&dg,&db);
    2115               0 :       *cp++ = PACK(dr,dg,db);
    2116               0 :     } while (--x);
    2117               0 :     SKEW(r, g, b, fromskew);
    2118               0 :     cp += toskew;
    2119                 :   }
    2120               0 : }
    2121                 : #undef YCbCrtoRGB
    2122                 : 
    2123                 : static int
    2124               2 : initYCbCrConversion(TIFFRGBAImage* img)
    2125                 : {
    2126                 :   static const char module[] = "initYCbCrConversion";
    2127                 : 
    2128                 :   float *luma, *refBlackWhite;
    2129                 : 
    2130               2 :   if (img->ycbcr == NULL) {
    2131               2 :     img->ycbcr = (TIFFYCbCrToRGB*) _TIFFmalloc(
    2132                 :         TIFFroundup_32(sizeof (TIFFYCbCrToRGB), sizeof (long))  
    2133                 :         + 4*256*sizeof (TIFFRGBValue)
    2134                 :         + 2*256*sizeof (int)
    2135                 :         + 3*256*sizeof (int32)
    2136                 :         );
    2137               2 :     if (img->ycbcr == NULL) {
    2138               0 :       TIFFErrorExt(img->tif->tif_clientdata, module,
    2139                 :           "No space for YCbCr->RGB conversion state");
    2140               0 :       return (0);
    2141                 :     }
    2142                 :   }
    2143                 : 
    2144               2 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRCOEFFICIENTS, &luma);
    2145               2 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_REFERENCEBLACKWHITE,
    2146                 :       &refBlackWhite);
    2147               2 :   if (TIFFYCbCrToRGBInit(img->ycbcr, luma, refBlackWhite) < 0)
    2148               0 :     return(0);
    2149               2 :   return (1);
    2150                 : }
    2151                 : 
    2152                 : static tileContigRoutine
    2153               2 : initCIELabConversion(TIFFRGBAImage* img)
    2154                 : {
    2155                 :   static const char module[] = "initCIELabConversion";
    2156                 : 
    2157                 :   float   *whitePoint;
    2158                 :   float   refWhite[3];
    2159                 : 
    2160               2 :   if (!img->cielab) {
    2161               2 :     img->cielab = (TIFFCIELabToRGB *)
    2162                 :       _TIFFmalloc(sizeof(TIFFCIELabToRGB));
    2163               2 :     if (!img->cielab) {
    2164               0 :       TIFFErrorExt(img->tif->tif_clientdata, module,
    2165                 :           "No space for CIE L*a*b*->RGB conversion state.");
    2166               0 :       return NULL;
    2167                 :     }
    2168                 :   }
    2169                 : 
    2170               2 :   TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);
    2171               2 :   refWhite[1] = 100.0F;
    2172               2 :   refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];
    2173               4 :   refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])
    2174               2 :           / whitePoint[1] * refWhite[1];
    2175               2 :   if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {
    2176               0 :     TIFFErrorExt(img->tif->tif_clientdata, module,
    2177                 :         "Failed to initialize CIE L*a*b*->RGB conversion state.");
    2178               0 :     _TIFFfree(img->cielab);
    2179               0 :     return NULL;
    2180                 :   }
    2181                 : 
    2182               2 :   return putcontig8bitCIELab;
    2183                 : }
    2184                 : 
    2185                 : /*
    2186                 :  * Greyscale images with less than 8 bits/sample are handled
    2187                 :  * with a table to avoid lots of shifts and masks.  The table
    2188                 :  * is setup so that put*bwtile (below) can retrieve 8/bitspersample
    2189                 :  * pixel values simply by indexing into the table with one
    2190                 :  * number.
    2191                 :  */
    2192                 : static int
    2193               0 : makebwmap(TIFFRGBAImage* img)
    2194                 : {
    2195               0 :     TIFFRGBValue* Map = img->Map;
    2196               0 :     int bitspersample = img->bitspersample;
    2197               0 :     int nsamples = 8 / bitspersample;
    2198                 :     int i;
    2199                 :     uint32* p;
    2200                 : 
    2201               0 :     if( nsamples == 0 )
    2202               0 :         nsamples = 1;
    2203                 : 
    2204               0 :     img->BWmap = (uint32**) _TIFFmalloc(
    2205               0 :   256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
    2206               0 :     if (img->BWmap == NULL) {
    2207               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");
    2208               0 :     return (0);
    2209                 :     }
    2210               0 :     p = (uint32*)(img->BWmap + 256);
    2211               0 :     for (i = 0; i < 256; i++) {
    2212                 :   TIFFRGBValue c;
    2213               0 :   img->BWmap[i] = p;
    2214               0 :   switch (bitspersample) {
    2215                 : #define GREY(x) c = Map[x]; *p++ = PACK(c,c,c);
    2216                 :   case 1:
    2217               0 :       GREY(i>>7);
    2218               0 :       GREY((i>>6)&1);
    2219               0 :       GREY((i>>5)&1);
    2220               0 :       GREY((i>>4)&1);
    2221               0 :       GREY((i>>3)&1);
    2222               0 :       GREY((i>>2)&1);
    2223               0 :       GREY((i>>1)&1);
    2224               0 :       GREY(i&1);
    2225               0 :       break;
    2226                 :   case 2:
    2227               0 :       GREY(i>>6);
    2228               0 :       GREY((i>>4)&3);
    2229               0 :       GREY((i>>2)&3);
    2230               0 :       GREY(i&3);
    2231               0 :       break;
    2232                 :   case 4:
    2233               0 :       GREY(i>>4);
    2234               0 :       GREY(i&0xf);
    2235               0 :       break;
    2236                 :   case 8:
    2237                 :         case 16:
    2238               0 :       GREY(i);
    2239                 :       break;
    2240                 :   }
    2241                 : #undef  GREY
    2242                 :     }
    2243               0 :     return (1);
    2244                 : }
    2245                 : 
    2246                 : /*
    2247                 :  * Construct a mapping table to convert from the range
    2248                 :  * of the data samples to [0,255] --for display.  This
    2249                 :  * process also handles inverting B&W images when needed.
    2250                 :  */ 
    2251                 : static int
    2252               0 : setupMap(TIFFRGBAImage* img)
    2253                 : {
    2254                 :     int32 x, range;
    2255                 : 
    2256               0 :     range = (int32)((1L<<img->bitspersample)-1);
    2257                 :     
    2258                 :     /* treat 16 bit the same as eight bit */
    2259               0 :     if( img->bitspersample == 16 )
    2260               0 :         range = (int32) 255;
    2261                 : 
    2262               0 :     img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));
    2263               0 :     if (img->Map == NULL) {
    2264               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),
    2265                 :       "No space for photometric conversion table");
    2266               0 :     return (0);
    2267                 :     }
    2268               0 :     if (img->photometric == PHOTOMETRIC_MINISWHITE) {
    2269               0 :   for (x = 0; x <= range; x++)
    2270               0 :       img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);
    2271                 :     } else {
    2272               0 :   for (x = 0; x <= range; x++)
    2273               0 :       img->Map[x] = (TIFFRGBValue) ((x * 255) / range);
    2274                 :     }
    2275               0 :     if (img->bitspersample <= 16 &&
    2276               0 :   (img->photometric == PHOTOMETRIC_MINISBLACK ||
    2277               0 :    img->photometric == PHOTOMETRIC_MINISWHITE)) {
    2278                 :   /*
    2279                 :    * Use photometric mapping table to construct
    2280                 :    * unpacking tables for samples <= 8 bits.
    2281                 :    */
    2282               0 :   if (!makebwmap(img))
    2283               0 :       return (0);
    2284                 :   /* no longer need Map, free it */
    2285               0 :   _TIFFfree(img->Map), img->Map = NULL;
    2286                 :     }
    2287               0 :     return (1);
    2288                 : }
    2289                 : 
    2290                 : static int
    2291               0 : checkcmap(TIFFRGBAImage* img)
    2292                 : {
    2293               0 :     uint16* r = img->redcmap;
    2294               0 :     uint16* g = img->greencmap;
    2295               0 :     uint16* b = img->bluecmap;
    2296               0 :     long n = 1L<<img->bitspersample;
    2297                 : 
    2298               0 :     while (n-- > 0)
    2299               0 :   if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)
    2300               0 :       return (16);
    2301               0 :     return (8);
    2302                 : }
    2303                 : 
    2304                 : static void
    2305               0 : cvtcmap(TIFFRGBAImage* img)
    2306                 : {
    2307               0 :     uint16* r = img->redcmap;
    2308               0 :     uint16* g = img->greencmap;
    2309               0 :     uint16* b = img->bluecmap;
    2310                 :     long i;
    2311                 : 
    2312               0 :     for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {
    2313                 : #define CVT(x)    ((uint16)((x)>>8))
    2314               0 :   r[i] = CVT(r[i]);
    2315               0 :   g[i] = CVT(g[i]);
    2316               0 :   b[i] = CVT(b[i]);
    2317                 : #undef  CVT
    2318                 :     }
    2319               0 : }
    2320                 : 
    2321                 : /*
    2322                 :  * Palette images with <= 8 bits/sample are handled
    2323                 :  * with a table to avoid lots of shifts and masks.  The table
    2324                 :  * is setup so that put*cmaptile (below) can retrieve 8/bitspersample
    2325                 :  * pixel values simply by indexing into the table with one
    2326                 :  * number.
    2327                 :  */
    2328                 : static int
    2329               0 : makecmap(TIFFRGBAImage* img)
    2330                 : {
    2331               0 :     int bitspersample = img->bitspersample;
    2332               0 :     int nsamples = 8 / bitspersample;
    2333               0 :     uint16* r = img->redcmap;
    2334               0 :     uint16* g = img->greencmap;
    2335               0 :     uint16* b = img->bluecmap;
    2336                 :     uint32 *p;
    2337                 :     int i;
    2338                 : 
    2339               0 :     img->PALmap = (uint32**) _TIFFmalloc(
    2340               0 :   256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));
    2341               0 :     if (img->PALmap == NULL) {
    2342               0 :     TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");
    2343               0 :     return (0);
    2344                 :   }
    2345               0 :     p = (uint32*)(img->PALmap + 256);
    2346               0 :     for (i = 0; i < 256; i++) {
    2347                 :   TIFFRGBValue c;
    2348               0 :   img->PALmap[i] = p;
    2349                 : #define CMAP(x) c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);
    2350               0 :   switch (bitspersample) {
    2351                 :   case 1:
    2352               0 :       CMAP(i>>7);
    2353               0 :       CMAP((i>>6)&1);
    2354               0 :       CMAP((i>>5)&1);
    2355               0 :       CMAP((i>>4)&1);
    2356               0 :       CMAP((i>>3)&1);
    2357               0 :       CMAP((i>>2)&1);
    2358               0 :       CMAP((i>>1)&1);
    2359               0 :       CMAP(i&1);
    2360               0 :       break;
    2361                 :   case 2:
    2362               0 :       CMAP(i>>6);
    2363               0 :       CMAP((i>>4)&3);
    2364               0 :       CMAP((i>>2)&3);
    2365               0 :       CMAP(i&3);
    2366               0 :       break;
    2367                 :   case 4:
    2368               0 :       CMAP(i>>4);
    2369               0 :       CMAP(i&0xf);
    2370               0 :       break;
    2371                 :   case 8:
    2372               0 :       CMAP(i);
    2373                 :       break;
    2374                 :   }
    2375                 : #undef CMAP
    2376                 :     }
    2377               0 :     return (1);
    2378                 : }
    2379                 : 
    2380                 : /* 
    2381                 :  * Construct any mapping table used
    2382                 :  * by the associated put routine.
    2383                 :  */
    2384                 : static int
    2385               6 : buildMap(TIFFRGBAImage* img)
    2386                 : {
    2387               6 :     switch (img->photometric) {
    2388                 :     case PHOTOMETRIC_RGB:
    2389                 :     case PHOTOMETRIC_YCBCR:
    2390                 :     case PHOTOMETRIC_SEPARATED:
    2391               4 :   if (img->bitspersample == 8)
    2392               4 :       break;
    2393                 :   /* fall thru... */
    2394                 :     case PHOTOMETRIC_MINISBLACK:
    2395                 :     case PHOTOMETRIC_MINISWHITE:
    2396               0 :   if (!setupMap(img))
    2397               0 :       return (0);
    2398               0 :   break;
    2399                 :     case PHOTOMETRIC_PALETTE:
    2400                 :   /*
    2401                 :    * Convert 16-bit colormap to 8-bit (unless it looks
    2402                 :    * like an old-style 8-bit colormap).
    2403                 :    */
    2404               0 :   if (checkcmap(img) == 16)
    2405               0 :       cvtcmap(img);
    2406                 :   else
    2407               0 :       TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");
    2408                 :   /*
    2409                 :    * Use mapping table and colormap to construct
    2410                 :    * unpacking tables for samples < 8 bits.
    2411                 :    */
    2412               0 :   if (img->bitspersample <= 8 && !makecmap(img))
    2413               0 :       return (0);
    2414                 :   break;
    2415                 :     }
    2416               6 :     return (1);
    2417                 : }
    2418                 : 
    2419                 : /*
    2420                 :  * Select the appropriate conversion routine for packed data.
    2421                 :  */
    2422                 : static int
    2423               8 : PickContigCase(TIFFRGBAImage* img)
    2424                 : {
    2425               8 :   img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;
    2426               8 :   img->put.contig = NULL;
    2427               8 :   switch (img->photometric) {
    2428                 :     case PHOTOMETRIC_RGB:
    2429               0 :       switch (img->bitspersample) {
    2430                 :         case 8:
    2431               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2432               0 :             img->put.contig = putRGBAAcontig8bittile;
    2433               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2434                 :           {
    2435               0 :             if (BuildMapUaToAa(img))
    2436               0 :               img->put.contig = putRGBUAcontig8bittile;
    2437                 :           }
    2438                 :           else
    2439               0 :             img->put.contig = putRGBcontig8bittile;
    2440               0 :           break;
    2441                 :         case 16:
    2442               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2443                 :           {
    2444               0 :             if (BuildMapBitdepth16To8(img))
    2445               0 :               img->put.contig = putRGBAAcontig16bittile;
    2446                 :           }
    2447               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2448                 :           {
    2449               0 :             if (BuildMapBitdepth16To8(img) &&
    2450               0 :                 BuildMapUaToAa(img))
    2451               0 :               img->put.contig = putRGBUAcontig16bittile;
    2452                 :           }
    2453                 :           else
    2454                 :           {
    2455               0 :             if (BuildMapBitdepth16To8(img))
    2456               0 :               img->put.contig = putRGBcontig16bittile;
    2457                 :           }
    2458                 :           break;
    2459                 :       }
    2460               0 :       break;
    2461                 :     case PHOTOMETRIC_SEPARATED:
    2462               4 :       if (buildMap(img)) {
    2463               4 :         if (img->bitspersample == 8) {
    2464               4 :           if (!img->Map)
    2465               4 :             img->put.contig = putRGBcontig8bitCMYKtile;
    2466                 :           else
    2467               0 :             img->put.contig = putRGBcontig8bitCMYKMaptile;
    2468                 :         }
    2469                 :       }
    2470               4 :       break;
    2471                 :     case PHOTOMETRIC_PALETTE:
    2472               0 :       if (buildMap(img)) {
    2473               0 :         switch (img->bitspersample) {
    2474                 :           case 8:
    2475               0 :             img->put.contig = put8bitcmaptile;
    2476               0 :             break;
    2477                 :           case 4:
    2478               0 :             img->put.contig = put4bitcmaptile;
    2479               0 :             break;
    2480                 :           case 2:
    2481               0 :             img->put.contig = put2bitcmaptile;
    2482               0 :             break;
    2483                 :           case 1:
    2484               0 :             img->put.contig = put1bitcmaptile;
    2485                 :             break;
    2486                 :         }
    2487                 :       }
    2488               0 :       break;
    2489                 :     case PHOTOMETRIC_MINISWHITE:
    2490                 :     case PHOTOMETRIC_MINISBLACK:
    2491               0 :       if (buildMap(img)) {
    2492               0 :         switch (img->bitspersample) {
    2493                 :           case 16:
    2494               0 :             img->put.contig = put16bitbwtile;
    2495               0 :             break;
    2496                 :           case 8:
    2497               0 :             img->put.contig = putgreytile;
    2498               0 :             break;
    2499                 :           case 4:
    2500               0 :             img->put.contig = put4bitbwtile;
    2501               0 :             break;
    2502                 :           case 2:
    2503               0 :             img->put.contig = put2bitbwtile;
    2504               0 :             break;
    2505                 :           case 1:
    2506               0 :             img->put.contig = put1bitbwtile;
    2507                 :             break;
    2508                 :         }
    2509                 :       }
    2510               0 :       break;
    2511                 :     case PHOTOMETRIC_YCBCR:
    2512               2 :       if ((img->bitspersample==8) && (img->samplesperpixel==3))
    2513                 :       {
    2514               2 :         if (initYCbCrConversion(img)!=0)
    2515                 :         {
    2516                 :           /*
    2517                 :            * The 6.0 spec says that subsampling must be
    2518                 :            * one of 1, 2, or 4, and that vertical subsampling
    2519                 :            * must always be <= horizontal subsampling; so
    2520                 :            * there are only a few possibilities and we just
    2521                 :            * enumerate the cases.
    2522                 :            * Joris: added support for the [1,2] case, nonetheless, to accomodate
    2523                 :            * some OJPEG files
    2524                 :            */
    2525                 :           uint16 SubsamplingHor;
    2526                 :           uint16 SubsamplingVer;
    2527               2 :           TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &SubsamplingHor, &SubsamplingVer);
    2528               2 :           switch ((SubsamplingHor<<4)|SubsamplingVer) {
    2529                 :             case 0x44:
    2530               0 :               img->put.contig = putcontig8bitYCbCr44tile;
    2531               0 :               break;
    2532                 :             case 0x42:
    2533               0 :               img->put.contig = putcontig8bitYCbCr42tile;
    2534               0 :               break;
    2535                 :             case 0x41:
    2536               0 :               img->put.contig = putcontig8bitYCbCr41tile;
    2537               0 :               break;
    2538                 :             case 0x22:
    2539               2 :               img->put.contig = putcontig8bitYCbCr22tile;
    2540               2 :               break;
    2541                 :             case 0x21:
    2542               0 :               img->put.contig = putcontig8bitYCbCr21tile;
    2543               0 :               break;
    2544                 :             case 0x12:
    2545               0 :               img->put.contig = putcontig8bitYCbCr12tile;
    2546               0 :               break;
    2547                 :             case 0x11:
    2548               0 :               img->put.contig = putcontig8bitYCbCr11tile;
    2549                 :               break;
    2550                 :           }
    2551                 :         }
    2552                 :       }
    2553               2 :       break;
    2554                 :     case PHOTOMETRIC_CIELAB:
    2555               2 :       if (buildMap(img)) {
    2556               2 :         if (img->bitspersample == 8)
    2557               2 :           img->put.contig = initCIELabConversion(img);
    2558                 :         break;
    2559                 :       }
    2560                 :   }
    2561               8 :   return ((img->get!=NULL) && (img->put.contig!=NULL));
    2562                 : }
    2563                 : 
    2564                 : /*
    2565                 :  * Select the appropriate conversion routine for unpacked data.
    2566                 :  *
    2567                 :  * NB: we assume that unpacked single channel data is directed
    2568                 :  *   to the "packed routines.
    2569                 :  */
    2570                 : static int
    2571               0 : PickSeparateCase(TIFFRGBAImage* img)
    2572                 : {
    2573               0 :   img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;
    2574               0 :   img->put.separate = NULL;
    2575               0 :   switch (img->photometric) {
    2576                 :     case PHOTOMETRIC_MINISWHITE:
    2577                 :     case PHOTOMETRIC_MINISBLACK:
    2578                 :                   /* greyscale images processed pretty much as RGB by gtTileSeparate */
    2579                 :     case PHOTOMETRIC_RGB:
    2580               0 :       switch (img->bitspersample) {
    2581                 :         case 8:
    2582               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2583               0 :             img->put.separate = putRGBAAseparate8bittile;
    2584               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2585                 :           {
    2586               0 :             if (BuildMapUaToAa(img))
    2587               0 :               img->put.separate = putRGBUAseparate8bittile;
    2588                 :           }
    2589                 :           else
    2590               0 :             img->put.separate = putRGBseparate8bittile;
    2591               0 :           break;
    2592                 :         case 16:
    2593               0 :           if (img->alpha == EXTRASAMPLE_ASSOCALPHA)
    2594                 :           {
    2595               0 :             if (BuildMapBitdepth16To8(img))
    2596               0 :               img->put.separate = putRGBAAseparate16bittile;
    2597                 :           }
    2598               0 :           else if (img->alpha == EXTRASAMPLE_UNASSALPHA)
    2599                 :           {
    2600               0 :             if (BuildMapBitdepth16To8(img) &&
    2601               0 :                 BuildMapUaToAa(img))
    2602               0 :               img->put.separate = putRGBUAseparate16bittile;
    2603                 :           }
    2604                 :           else
    2605                 :           {
    2606               0 :             if (BuildMapBitdepth16To8(img))
    2607               0 :               img->put.separate = putRGBseparate16bittile;
    2608                 :           }
    2609                 :           break;
    2610                 :       }
    2611               0 :       break;
    2612                 :     case PHOTOMETRIC_SEPARATED:
    2613               0 :       if (img->bitspersample == 8 && img->samplesperpixel == 4)
    2614                 :       {
    2615               0 :         img->alpha = 1; // Not alpha, but seems like the only way to get 4th band
    2616               0 :         img->put.separate = putCMYKseparate8bittile;
    2617                 :       }
    2618               0 :       break;
    2619                 :     case PHOTOMETRIC_YCBCR:
    2620               0 :       if ((img->bitspersample==8) && (img->samplesperpixel==3))
    2621                 :       {
    2622               0 :         if (initYCbCrConversion(img)!=0)
    2623                 :         {
    2624                 :           uint16 hs, vs;
    2625               0 :           TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);
    2626               0 :           switch ((hs<<4)|vs) {
    2627                 :             case 0x11:
    2628               0 :               img->put.separate = putseparate8bitYCbCr11tile;
    2629                 :               break;
    2630                 :             /* TODO: add other cases here */
    2631                 :           }
    2632                 :         }
    2633                 :       }
    2634                 :       break;
    2635                 :   }
    2636               0 :   return ((img->get!=NULL) && (img->put.separate!=NULL));
    2637                 : }
    2638                 : 
    2639                 : static int
    2640               0 : BuildMapUaToAa(TIFFRGBAImage* img)
    2641                 : {
    2642                 :   static const char module[]="BuildMapUaToAa";
    2643                 :   uint8* m;
    2644                 :   uint16 na,nv;
    2645               0 :   assert(img->UaToAa==NULL);
    2646               0 :   img->UaToAa=_TIFFmalloc(65536);
    2647               0 :   if (img->UaToAa==NULL)
    2648                 :   {
    2649               0 :     TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
    2650               0 :     return(0);
    2651                 :   }
    2652               0 :   m=img->UaToAa;
    2653               0 :   for (na=0; na<256; na++)
    2654                 :   {
    2655               0 :     for (nv=0; nv<256; nv++)
    2656               0 :       *m++=(nv*na+127)/255;
    2657                 :   }
    2658               0 :   return(1);
    2659                 : }
    2660                 : 
    2661                 : static int
    2662               0 : BuildMapBitdepth16To8(TIFFRGBAImage* img)
    2663                 : {
    2664                 :   static const char module[]="BuildMapBitdepth16To8";
    2665                 :   uint8* m;
    2666                 :   uint32 n;
    2667               0 :   assert(img->Bitdepth16To8==NULL);
    2668               0 :   img->Bitdepth16To8=_TIFFmalloc(65536);
    2669               0 :   if (img->Bitdepth16To8==NULL)
    2670                 :   {
    2671               0 :     TIFFErrorExt(img->tif->tif_clientdata,module,"Out of memory");
    2672               0 :     return(0);
    2673                 :   }
    2674               0 :   m=img->Bitdepth16To8;
    2675               0 :   for (n=0; n<65536; n++)
    2676               0 :     *m++=(n+128)/257;
    2677               0 :   return(1);
    2678                 : }
    2679                 : 
    2680                 : 
    2681                 : /*
    2682                 :  * Read a whole strip off data from the file, and convert to RGBA form.
    2683                 :  * If this is the last strip, then it will only contain the portion of
    2684                 :  * the strip that is actually within the image space.  The result is
    2685                 :  * organized in bottom to top form.
    2686                 :  */
    2687                 : 
    2688                 : 
    2689                 : int
    2690               6 : TIFFReadRGBAStrip(TIFF* tif, uint32 row, uint32 * raster )
    2691                 : 
    2692                 : {
    2693               6 :     char  emsg[1024] = "";
    2694                 :     TIFFRGBAImage img;
    2695                 :     int   ok;
    2696                 :     uint32  rowsperstrip, rows_to_read;
    2697                 : 
    2698               6 :     if( TIFFIsTiled( tif ) )
    2699                 :     {
    2700               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2701                 :                   "Can't use TIFFReadRGBAStrip() with tiled file.");
    2702               0 :   return (0);
    2703                 :     }
    2704                 :     
    2705               6 :     TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);
    2706               6 :     if( (row % rowsperstrip) != 0 )
    2707                 :     {
    2708               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2709                 :         "Row passed to TIFFReadRGBAStrip() must be first in a strip.");
    2710               0 :     return (0);
    2711                 :     }
    2712                 : 
    2713              12 :     if (TIFFRGBAImageOK(tif, emsg) && TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
    2714                 : 
    2715               6 :         img.row_offset = row;
    2716               6 :         img.col_offset = 0;
    2717                 : 
    2718               6 :         if( row + rowsperstrip > img.height )
    2719               2 :             rows_to_read = img.height - row;
    2720                 :         else
    2721               4 :             rows_to_read = rowsperstrip;
    2722                 :         
    2723               6 :   ok = TIFFRGBAImageGet(&img, raster, img.width, rows_to_read );
    2724                 :         
    2725               6 :   TIFFRGBAImageEnd(&img);
    2726                 :     } else {
    2727               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
    2728               0 :     ok = 0;
    2729                 :     }
    2730                 :     
    2731               6 :     return (ok);
    2732                 : }
    2733                 : 
    2734                 : /*
    2735                 :  * Read a whole tile off data from the file, and convert to RGBA form.
    2736                 :  * The returned RGBA data is organized from bottom to top of tile,
    2737                 :  * and may include zeroed areas if the tile extends off the image.
    2738                 :  */
    2739                 : 
    2740                 : int
    2741               2 : TIFFReadRGBATile(TIFF* tif, uint32 col, uint32 row, uint32 * raster)
    2742                 : 
    2743                 : {
    2744               2 :     char  emsg[1024] = "";
    2745                 :     TIFFRGBAImage img;
    2746                 :     int   ok;
    2747                 :     uint32  tile_xsize, tile_ysize;
    2748                 :     uint32  read_xsize, read_ysize;
    2749                 :     uint32  i_row;
    2750                 : 
    2751                 :     /*
    2752                 :      * Verify that our request is legal - on a tile file, and on a
    2753                 :      * tile boundary.
    2754                 :      */
    2755                 :     
    2756               2 :     if( !TIFFIsTiled( tif ) )
    2757                 :     {
    2758               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2759                 :           "Can't use TIFFReadRGBATile() with stripped file.");
    2760               0 :     return (0);
    2761                 :     }
    2762                 :     
    2763               2 :     TIFFGetFieldDefaulted(tif, TIFFTAG_TILEWIDTH, &tile_xsize);
    2764               2 :     TIFFGetFieldDefaulted(tif, TIFFTAG_TILELENGTH, &tile_ysize);
    2765               2 :     if( (col % tile_xsize) != 0 || (row % tile_ysize) != 0 )
    2766                 :     {
    2767               0 :     TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif),
    2768                 :                   "Row/col passed to TIFFReadRGBATile() must be top"
    2769                 :                   "left corner of a tile.");
    2770               0 :   return (0);
    2771                 :     }
    2772                 : 
    2773                 :     /*
    2774                 :      * Setup the RGBA reader.
    2775                 :      */
    2776                 :     
    2777               4 :     if (!TIFFRGBAImageOK(tif, emsg) 
    2778               2 :   || !TIFFRGBAImageBegin(&img, tif, 0, emsg)) {
    2779               0 :       TIFFErrorExt(tif->tif_clientdata, TIFFFileName(tif), "%s", emsg);
    2780               0 :       return( 0 );
    2781                 :     }
    2782                 : 
    2783                 :     /*
    2784                 :      * The TIFFRGBAImageGet() function doesn't allow us to get off the
    2785                 :      * edge of the image, even to fill an otherwise valid tile.  So we
    2786                 :      * figure out how much we can read, and fix up the tile buffer to
    2787                 :      * a full tile configuration afterwards.
    2788                 :      */
    2789                 : 
    2790               2 :     if( row + tile_ysize > img.height )
    2791               2 :         read_ysize = img.height - row;
    2792                 :     else
    2793               0 :         read_ysize = tile_ysize;
    2794                 :     
    2795               2 :     if( col + tile_xsize > img.width )
    2796               2 :         read_xsize = img.width - col;
    2797                 :     else
    2798               0 :         read_xsize = tile_xsize;
    2799                 : 
    2800                 :     /*
    2801                 :      * Read the chunk of imagery.
    2802                 :      */
    2803                 :     
    2804               2 :     img.row_offset = row;
    2805               2 :     img.col_offset = col;
    2806                 : 
    2807               2 :     ok = TIFFRGBAImageGet(&img, raster, read_xsize, read_ysize );
    2808                 :         
    2809               2 :     TIFFRGBAImageEnd(&img);
    2810                 : 
    2811                 :     /*
    2812                 :      * If our read was incomplete we will need to fix up the tile by
    2813                 :      * shifting the data around as if a full tile of data is being returned.
    2814                 :      *
    2815                 :      * This is all the more complicated because the image is organized in
    2816                 :      * bottom to top format. 
    2817                 :      */
    2818                 : 
    2819               2 :     if( read_xsize == tile_xsize && read_ysize == tile_ysize )
    2820               0 :         return( ok );
    2821                 : 
    2822             428 :     for( i_row = 0; i_row < read_ysize; i_row++ ) {
    2823            1278 :         memmove( raster + (tile_ysize - i_row - 1) * tile_xsize,
    2824             852 :                  raster + (read_ysize - i_row - 1) * read_xsize,
    2825                 :                  read_xsize * sizeof(uint32) );
    2826             426 :         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize+read_xsize,
    2827             426 :                      0, sizeof(uint32) * (tile_xsize - read_xsize) );
    2828                 :     }
    2829                 : 
    2830              24 :     for( i_row = read_ysize; i_row < tile_ysize; i_row++ ) {
    2831              22 :         _TIFFmemset( raster + (tile_ysize - i_row - 1) * tile_xsize,
    2832              22 :                      0, sizeof(uint32) * tile_xsize );
    2833                 :     }
    2834                 : 
    2835               2 :     return (ok);
    2836                 : }
    2837                 : 
    2838                 : /* vim: set ts=8 sts=8 sw=8 noet: */
    2839                 : /*
    2840                 :  * Local Variables:
    2841                 :  * mode: c
    2842                 :  * c-basic-offset: 8
    2843                 :  * fill-column: 78
    2844                 :  * End:
    2845                 :  */

Generated by: LCOV version 1.7