+ uint16 planarConfig = PLANARCONFIG_CONTIG;
+ (void) TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &planarConfig);
+
+ bool ok = true;
+ char msg[1024] = "";
+ if
+ (
+ (planarConfig == PLANARCONFIG_CONTIG && samplesPerPixel == 2
+ && extraSamples == 1)
+ &&
+ (
+ ( !TIFFRGBAImageOK(tif, msg) )
+ || (bitsPerSample == 8)
+ )
+ )
+ {
+ const bool isGreyScale = (bitsPerSample == 8);
+ unsigned char *buf = (unsigned char *)_TIFFmalloc(TIFFScanlineSize(tif));
+ uint32 pos = 0;
+ const bool minIsWhite = (photometric == PHOTOMETRIC_MINISWHITE);
+ const int minValue = minIsWhite ? 255 : 0;
+ const int maxValue = 255 - minValue;
+
+ /*
+ Decode to ABGR format as that is what the code, that converts to
+ wxImage, later on expects (normally TIFFReadRGBAImageOriented is
+ used to decode which uses an ABGR layout).
+ */
+ for (uint32 y = 0; y < h; ++y)
+ {
+ if (TIFFReadScanline(tif, buf, y, 0) != 1)
+ {
+ ok = false;
+ break;
+ }
+
+ if (isGreyScale)
+ {
+ for (uint32 x = 0; x < w; ++x)
+ {
+ uint8 val = minIsWhite ? 255 - buf[x*2] : buf[x*2];
+ uint8 alpha = minIsWhite ? 255 - buf[x*2+1] : buf[x*2+1];
+ raster[pos] = val + (val << 8) + (val << 16)
+ + (alpha << 24);
+ pos++;
+ }
+ }
+ else
+ {
+ for (uint32 x = 0; x < w; ++x)
+ {
+ int mask = buf[x*2/8] << ((x*2)%8);
+
+ uint8 val = mask & 128 ? maxValue : minValue;
+ raster[pos] = val + (val << 8) + (val << 16)
+ + ((mask & 64 ? maxValue : minValue) << 24);
+ pos++;
+ }
+ }
+ }
+
+ _TIFFfree(buf);
+ }
+ else
+ {
+ ok = TIFFReadRGBAImageOriented( tif, w, h, raster,
+ ORIENTATION_TOPLEFT, 0 ) != 0;
+ }
+
+
+ if (!ok)