⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tif_getimage.c

📁 支持各种栅格图像和矢量图像读取的库
💻 C
📖 第 1 页 / 共 5 页
字号:
	static char module[] = "initCIELabConversion";	float   *whitePoint;	float   refWhite[3];	if (!img->cielab) {		img->cielab = (TIFFCIELabToRGB *)			_TIFFmalloc(sizeof(TIFFCIELabToRGB));		if (!img->cielab) {			TIFFErrorExt(img->tif->tif_clientdata, module,			    "No space for CIE L*a*b*->RGB conversion state.");			return NULL;		}	}	TIFFGetFieldDefaulted(img->tif, TIFFTAG_WHITEPOINT, &whitePoint);	refWhite[1] = 100.0F;	refWhite[0] = whitePoint[0] / whitePoint[1] * refWhite[1];	refWhite[2] = (1.0F - whitePoint[0] - whitePoint[1])		      / whitePoint[1] * refWhite[1];	if (TIFFCIELabToRGBInit(img->cielab, &display_sRGB, refWhite) < 0) {		TIFFErrorExt(img->tif->tif_clientdata, module,		    "Failed to initialize CIE L*a*b*->RGB conversion state.");		_TIFFfree(img->cielab);		return NULL;	}	return putcontig8bitCIELab;}/* * Greyscale images with less than 8 bits/sample are handled * with a table to avoid lots of shifts and masks.  The table * is setup so that put*bwtile (below) can retrieve 8/bitspersample * pixel values simply by indexing into the table with one * number. */static intmakebwmap(TIFFRGBAImage* img){    TIFFRGBValue* Map = img->Map;    int bitspersample = img->bitspersample;    int nsamples = 8 / bitspersample;    int i;    uint32* p;    if( nsamples == 0 )        nsamples = 1;    img->BWmap = (uint32**) _TIFFmalloc(	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));    if (img->BWmap == NULL) {		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for B&W mapping table");		return (0);    }    p = (uint32*)(img->BWmap + 256);    for (i = 0; i < 256; i++) {	TIFFRGBValue c;	img->BWmap[i] = p;	switch (bitspersample) {#define	GREY(x)	c = Map[x]; *p++ = PACK(c,c,c);	case 1:	    GREY(i>>7);	    GREY((i>>6)&1);	    GREY((i>>5)&1);	    GREY((i>>4)&1);	    GREY((i>>3)&1);	    GREY((i>>2)&1);	    GREY((i>>1)&1);	    GREY(i&1);	    break;	case 2:	    GREY(i>>6);	    GREY((i>>4)&3);	    GREY((i>>2)&3);	    GREY(i&3);	    break;	case 4:	    GREY(i>>4);	    GREY(i&0xf);	    break;	case 8:        case 16:	    GREY(i);	    break;	}#undef	GREY    }    return (1);}/* * Construct a mapping table to convert from the range * of the data samples to [0,255] --for display.  This * process also handles inverting B&W images when needed. */ static intsetupMap(TIFFRGBAImage* img){    int32 x, range;    range = (int32)((1L<<img->bitspersample)-1);        /* treat 16 bit the same as eight bit */    if( img->bitspersample == 16 )        range = (int32) 255;    img->Map = (TIFFRGBValue*) _TIFFmalloc((range+1) * sizeof (TIFFRGBValue));    if (img->Map == NULL) {		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif),			"No space for photometric conversion table");		return (0);    }    if (img->photometric == PHOTOMETRIC_MINISWHITE) {	for (x = 0; x <= range; x++)	    img->Map[x] = (TIFFRGBValue) (((range - x) * 255) / range);    } else {	for (x = 0; x <= range; x++)	    img->Map[x] = (TIFFRGBValue) ((x * 255) / range);    }    if (img->bitspersample <= 16 &&	(img->photometric == PHOTOMETRIC_MINISBLACK ||	 img->photometric == PHOTOMETRIC_MINISWHITE)) {	/*	 * Use photometric mapping table to construct	 * unpacking tables for samples <= 8 bits.	 */	if (!makebwmap(img))	    return (0);	/* no longer need Map, free it */	_TIFFfree(img->Map), img->Map = NULL;    }    return (1);}static intcheckcmap(TIFFRGBAImage* img){    uint16* r = img->redcmap;    uint16* g = img->greencmap;    uint16* b = img->bluecmap;    long n = 1L<<img->bitspersample;    while (n-- > 0)	if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256)	    return (16);    return (8);}static voidcvtcmap(TIFFRGBAImage* img){    uint16* r = img->redcmap;    uint16* g = img->greencmap;    uint16* b = img->bluecmap;    long i;    for (i = (1L<<img->bitspersample)-1; i >= 0; i--) {#define	CVT(x)		((uint16)((x)>>8))	r[i] = CVT(r[i]);	g[i] = CVT(g[i]);	b[i] = CVT(b[i]);#undef	CVT    }}/* * Palette images with <= 8 bits/sample are handled * with a table to avoid lots of shifts and masks.  The table * is setup so that put*cmaptile (below) can retrieve 8/bitspersample * pixel values simply by indexing into the table with one * number. */static intmakecmap(TIFFRGBAImage* img){    int bitspersample = img->bitspersample;    int nsamples = 8 / bitspersample;    uint16* r = img->redcmap;    uint16* g = img->greencmap;    uint16* b = img->bluecmap;    uint32 *p;    int i;    img->PALmap = (uint32**) _TIFFmalloc(	256*sizeof (uint32 *)+(256*nsamples*sizeof(uint32)));    if (img->PALmap == NULL) {		TIFFErrorExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "No space for Palette mapping table");		return (0);	}    p = (uint32*)(img->PALmap + 256);    for (i = 0; i < 256; i++) {	TIFFRGBValue c;	img->PALmap[i] = p;#define	CMAP(x)	c = (TIFFRGBValue) x; *p++ = PACK(r[c]&0xff, g[c]&0xff, b[c]&0xff);	switch (bitspersample) {	case 1:	    CMAP(i>>7);	    CMAP((i>>6)&1);	    CMAP((i>>5)&1);	    CMAP((i>>4)&1);	    CMAP((i>>3)&1);	    CMAP((i>>2)&1);	    CMAP((i>>1)&1);	    CMAP(i&1);	    break;	case 2:	    CMAP(i>>6);	    CMAP((i>>4)&3);	    CMAP((i>>2)&3);	    CMAP(i&3);	    break;	case 4:	    CMAP(i>>4);	    CMAP(i&0xf);	    break;	case 8:	    CMAP(i);	    break;	}#undef CMAP    }    return (1);}/*  * Construct any mapping table used * by the associated put routine. */static intbuildMap(TIFFRGBAImage* img){    switch (img->photometric) {    case PHOTOMETRIC_RGB:    case PHOTOMETRIC_YCBCR:    case PHOTOMETRIC_SEPARATED:	if (img->bitspersample == 8)	    break;	/* fall thru... */    case PHOTOMETRIC_MINISBLACK:    case PHOTOMETRIC_MINISWHITE:	if (!setupMap(img))	    return (0);	break;    case PHOTOMETRIC_PALETTE:	/*	 * Convert 16-bit colormap to 8-bit (unless it looks	 * like an old-style 8-bit colormap).	 */	if (checkcmap(img) == 16)	    cvtcmap(img);	else	    TIFFWarningExt(img->tif->tif_clientdata, TIFFFileName(img->tif), "Assuming 8-bit colormap");	/*	 * Use mapping table and colormap to construct	 * unpacking tables for samples < 8 bits.	 */	if (img->bitspersample <= 8 && !makecmap(img))	    return (0);	break;    }    return (1);}/* * Select the appropriate conversion routine for packed data. */static intPickContigCase(TIFFRGBAImage* img){	img->get = TIFFIsTiled(img->tif) ? gtTileContig : gtStripContig;	img->put.contig = NULL;	switch (img->photometric) {		case PHOTOMETRIC_RGB:			switch (img->bitspersample) {				case 8:					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)						img->put.contig = putRGBAAcontig8bittile;					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)					{						if (BuildMapUaToAa(img))							img->put.contig = putRGBUAcontig8bittile;					}					else						img->put.contig = putRGBcontig8bittile;					break;				case 16:					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)					{						if (BuildMapBitdepth16To8(img))							img->put.contig = putRGBAAcontig16bittile;					}					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)					{						if (BuildMapBitdepth16To8(img) &&						    BuildMapUaToAa(img))							img->put.contig = putRGBUAcontig16bittile;					}					else					{						if (BuildMapBitdepth16To8(img))							img->put.contig = putRGBcontig16bittile;					}					break;			}			break;		case PHOTOMETRIC_SEPARATED:			if (buildMap(img)) {				if (img->bitspersample == 8) {					if (!img->Map)						img->put.contig = putRGBcontig8bitCMYKtile;					else						img->put.contig = putRGBcontig8bitCMYKMaptile;				}			}			break;		case PHOTOMETRIC_PALETTE:			if (buildMap(img)) {				switch (img->bitspersample) {					case 8:						img->put.contig = put8bitcmaptile;						break;					case 4:						img->put.contig = put4bitcmaptile;						break;					case 2:						img->put.contig = put2bitcmaptile;						break;					case 1:						img->put.contig = put1bitcmaptile;						break;				}			}			break;		case PHOTOMETRIC_MINISWHITE:		case PHOTOMETRIC_MINISBLACK:			if (buildMap(img)) {				switch (img->bitspersample) {					case 16:						img->put.contig = put16bitbwtile;						break;					case 8:						img->put.contig = putgreytile;						break;					case 4:						img->put.contig = put4bitbwtile;						break;					case 2:						img->put.contig = put2bitbwtile;						break;					case 1:						img->put.contig = put1bitbwtile;						break;				}			}			break;		case PHOTOMETRIC_YCBCR:			if (img->bitspersample == 8)			{				if (initYCbCrConversion(img)!=0)				{					uint16 hs, vs;					/*					 * The 6.0 spec says that subsampling must be					 * one of 1, 2, or 4, and that vertical subsampling					 * must always be <= horizontal subsampling; so					 * there are only a few possibilities and we just					 * enumerate the cases.					 * Joris: added support for the [1,2] case, nonetheless, to accomodate					 * some OJPEG files					 */					TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);					switch ((hs<<4)|vs) {						case 0x44:							img->put.contig = putcontig8bitYCbCr44tile;							break;						case 0x42:							img->put.contig = putcontig8bitYCbCr42tile;							break;						case 0x41:							img->put.contig = putcontig8bitYCbCr41tile;							break;						case 0x22:							img->put.contig = putcontig8bitYCbCr22tile;							break;						case 0x21:							img->put.contig = putcontig8bitYCbCr21tile;							break;						case 0x12:							img->put.contig = putcontig8bitYCbCr12tile;							break;						case 0x11:							img->put.contig = putcontig8bitYCbCr11tile;							break;					}				}			}			break;		case PHOTOMETRIC_CIELAB:			if (buildMap(img)) {				if (img->bitspersample == 8)					img->put.contig = initCIELabConversion(img);				break;			}	}	return ((img->get!=NULL) && (img->put.contig!=NULL));}/* * Select the appropriate conversion routine for unpacked data. * * NB: we assume that unpacked single channel data is directed *	 to the "packed routines. */static intPickSeparateCase(TIFFRGBAImage* img){	img->get = TIFFIsTiled(img->tif) ? gtTileSeparate : gtStripSeparate;	img->put.separate = NULL;	switch (img->photometric) {		case PHOTOMETRIC_RGB:			switch (img->bitspersample) {				case 8:					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)						img->put.separate = putRGBAAseparate8bittile;					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)					{						if (BuildMapUaToAa(img))							img->put.separate = putRGBUAseparate8bittile;					}					else						img->put.separate = putRGBseparate8bittile;					break;				case 16:					if (img->alpha == EXTRASAMPLE_ASSOCALPHA)					{						if (BuildMapBitdepth16To8(img))							img->put.separate = putRGBAAseparate16bittile;					}					else if (img->alpha == EXTRASAMPLE_UNASSALPHA)					{						if (BuildMapBitdepth16To8(img) &&						    BuildMapUaToAa(img))							img->put.separate = putRGBUAseparate16bittile;					}					else					{						if (BuildMapBitdepth16To8(img))							img->put.separate = putRGBseparate16bittile;					}					break;			}			break;		case PHOTOMETRIC_YCBCR:			if ((img->bitspersample==8) && (img->samplesperpixel==3))			{				if (initYCbCrConversion(img)!=0)				{					uint16 hs, vs;					TIFFGetFieldDefaulted(img->tif, TIFFTAG_YCBCRSUBSAMPLING, &hs, &vs);					switch ((hs<<4)|vs) {						case 0x11:							img->put.separate = putseparate8bitYCbCr11tile;							break;						/* TODO: add other cases here */					}				}			}			break;	}	return ((img->get!=NULL) && (img->put.separate!=NULL));}static intBuildMapUaToAa(TIFFRGBAImage* img){	static const char module[]="BuildMapUaToAa";	uint8* m;	uint16 na,nv;	assert(img->UaToAa==NULL);	img->UaToAa=_TIFFmalloc(65536);	if (img->UaToAa==NULL)	{		TIFFErrorExt(img->tif,module,"Out of memory");		return(0);	}	m=img->UaToAa;	for (na=0; na<256; na++)	{		for (nv=0; nv<256; nv++)			*m++=(nv*na+127)/255;	}	return(1);}static intBuildMapBitdepth16To8(TIFFRGBAImage* img){	static const char module[]="BuildMapBitdepth16To8";	uint8* m;	uint32 n;	assert(img->Bitdepth16To8==NULL);	img->Bitdepth16To8=_TIFFmalloc(65536);	if (img->Bitdepth16To8==NULL)	{		TIFFErrorExt(img->tif,module,"Out of memory");		return(0);	}	m=img->Bitdepth16To8;	for (n=0; n<65536; n++)		*m++=(n+128)/257;	return(1);}/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -