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

📄 tif_getimage.c

📁 这是VCF框架的代码
💻 C
📖 第 1 页 / 共 4 页
字号:
    tsize_t scanline;    int32 fromskew, toskew;    buf = (u_char*) _TIFFmalloc(TIFFStripSize(tif));    if (buf == 0) {	TIFFError(TIFFFileName(tif), "No space for strip buffer");	return (0);    }    y = setorientation(img, h);    orientation = img->orientation;    toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);    scanline = TIFFScanlineSize(tif);    fromskew = (w < imagewidth ? imagewidth - w : 0);    for (row = 0; row < h; row += rowsperstrip) {	nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);	if (TIFFReadEncodedStrip(tif,                                 TIFFComputeStrip(tif,row+img->row_offset, 0),                                 buf, nrow*scanline) < 0            && img->stoponerr)		break;	(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf);	y += (orientation == ORIENTATION_TOPLEFT ?	    -(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (1);}/* * Get a strip-organized image with *	 SamplesPerPixel > 1 *	 PlanarConfiguration separated * We assume that all such images are RGB. */static intgtStripSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h){    TIFF* tif = img->tif;    tileSeparateRoutine put = img->put.separate;    uint16 orientation;    u_char *buf;    u_char *r, *g, *b, *a;    uint32 row, y, nrow;    tsize_t scanline;    uint32 rowsperstrip, offset_row;    uint32 imagewidth = img->width;    tsize_t stripsize;    int32 fromskew, toskew;    int alpha = img->alpha;    stripsize = TIFFStripSize(tif);    r = buf = (u_char *)_TIFFmalloc(4*stripsize);    if (buf == 0) {	TIFFError(TIFFFileName(tif), "No space for tile buffer");	return (0);    }    g = r + stripsize;    b = g + stripsize;    a = b + stripsize;    if (!alpha)	memset(a, 0xff, stripsize);    y = setorientation(img, h);    orientation = img->orientation;    toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? w+w : w-w);    TIFFGetFieldDefaulted(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip);    scanline = TIFFScanlineSize(tif);    fromskew = (w < imagewidth ? imagewidth - w : 0);    for (row = 0; row < h; row += rowsperstrip) {	nrow = (row + rowsperstrip > h ? h - row : rowsperstrip);        offset_row = row + img->row_offset;	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),	    r, nrow*scanline) < 0 && img->stoponerr)	    break;	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),	    g, nrow*scanline) < 0 && img->stoponerr)	    break;	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),	    b, nrow*scanline) < 0 && img->stoponerr)	    break;	if (alpha &&	    (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),	    a, nrow*scanline) < 0 && img->stoponerr))	    break;	(*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r, g, b, a);	y += (orientation == ORIENTATION_TOPLEFT ?	    -(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (1);}/* * The following routines move decoded data returned * from the TIFF library into rasters filled with packed * ABGR pixels (i.e. suitable for passing to lrecwrite.) * * The routines have been created according to the most * important cases and optimized.  pickTileContigCase and * pickTileSeparateCase analyze the parameters and select * the appropriate "put" routine to use. */#define	REPEAT8(op)	REPEAT4(op); REPEAT4(op)#define	REPEAT4(op)	REPEAT2(op); REPEAT2(op)#define	REPEAT2(op)	op; op#define	CASE8(x,op)			\    switch (x) {			\    case 7: op; case 6: op; case 5: op;	\    case 4: op; case 3: op; case 2: op;	\    case 1: op;				\    }#define	CASE4(x,op)	switch (x) { case 3: op; case 2: op; case 1: op; }#define	NOP#define	UNROLL8(w, op1, op2) {		\    uint32 _x;				\    for (_x = w; _x >= 8; _x -= 8) {	\	op1;				\	REPEAT8(op2);			\    }					\    if (_x > 0) {			\	op1;				\	CASE8(_x,op2);			\    }					\}#define	UNROLL4(w, op1, op2) {		\    uint32 _x;				\    for (_x = w; _x >= 4; _x -= 4) {	\	op1;				\	REPEAT4(op2);			\    }					\    if (_x > 0) {			\	op1;				\	CASE4(_x,op2);			\    }					\}#define	UNROLL2(w, op1, op2) {		\    uint32 _x;				\    for (_x = w; _x >= 2; _x -= 2) {	\	op1;				\	REPEAT2(op2);			\    }					\    if (_x) {				\	op1;				\	op2;				\    }					\}    #define	SKEW(r,g,b,skew)	{ r += skew; g += skew; b += skew; }#define	SKEW4(r,g,b,a,skew)	{ r += skew; g += skew; b += skew; a+= skew; }#define A1 ((uint32)(0xffL<<24))#define	PACK(r,g,b)	\	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|A1)#define	PACK4(r,g,b,a)	\	((uint32)(r)|((uint32)(g)<<8)|((uint32)(b)<<16)|((uint32)(a)<<24))#define W2B(v) (((v)>>8)&0xff)#define	PACKW(r,g,b)	\	((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|A1)#define	PACKW4(r,g,b,a)	\	((uint32)W2B(r)|((uint32)W2B(g)<<8)|((uint32)W2B(b)<<16)|((uint32)W2B(a)<<24))#define	DECLAREContigPutFunc(name) \static void name(\    TIFFRGBAImage* img, \    uint32* cp, \    uint32 x, uint32 y, \    uint32 w, uint32 h, \    int32 fromskew, int32 toskew, \    u_char* pp \)/* * 8-bit palette => colormap/RGB */DECLAREContigPutFunc(put8bitcmaptile){    uint32** PALmap = img->PALmap;    (void) x; (void) y;    while (h-- > 0) {	UNROLL8(w, NOP, *cp++ = PALmap[*pp++][0]);	cp += toskew;	pp += fromskew;    }}/* * 4-bit palette => colormap/RGB */DECLAREContigPutFunc(put4bitcmaptile){    uint32** PALmap = img->PALmap;    (void) x; (void) y;    fromskew /= 2;    while (h-- > 0) {	uint32* bw;	UNROLL2(w, bw = PALmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 2-bit palette => colormap/RGB */DECLAREContigPutFunc(put2bitcmaptile){    uint32** PALmap = img->PALmap;    (void) x; (void) y;    fromskew /= 4;    while (h-- > 0) {	uint32* bw;	UNROLL4(w, bw = PALmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 1-bit palette => colormap/RGB */DECLAREContigPutFunc(put1bitcmaptile){    uint32** PALmap = img->PALmap;    (void) x; (void) y;    fromskew /= 8;    while (h-- > 0) {	uint32* bw;	UNROLL8(w, bw = PALmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 8-bit greyscale => colormap/RGB */DECLAREContigPutFunc(putgreytile){    uint32** BWmap = img->BWmap;    (void) y;    while (h-- > 0) {	for (x = w; x-- > 0;)	    *cp++ = BWmap[*pp++][0];	cp += toskew;	pp += fromskew;    }}/* * 1-bit bilevel => colormap/RGB */DECLAREContigPutFunc(put1bitbwtile){    uint32** BWmap = img->BWmap;    (void) x; (void) y;    fromskew /= 8;    while (h-- > 0) {	uint32* bw;	UNROLL8(w, bw = BWmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 2-bit greyscale => colormap/RGB */DECLAREContigPutFunc(put2bitbwtile){    uint32** BWmap = img->BWmap;    (void) x; (void) y;    fromskew /= 4;    while (h-- > 0) {	uint32* bw;	UNROLL4(w, bw = BWmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 4-bit greyscale => colormap/RGB */DECLAREContigPutFunc(put4bitbwtile){    uint32** BWmap = img->BWmap;    (void) x; (void) y;    fromskew /= 2;    while (h-- > 0) {	uint32* bw;	UNROLL2(w, bw = BWmap[*pp++], *cp++ = *bw++);	cp += toskew;	pp += fromskew;    }}/* * 8-bit packed samples, no Map => RGB */DECLAREContigPutFunc(putRGBcontig8bittile){    int samplesperpixel = img->samplesperpixel;    (void) x; (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	UNROLL8(w, NOP,	    *cp++ = PACK(pp[0], pp[1], pp[2]);	    pp += samplesperpixel);	cp += toskew;	pp += fromskew;    }}/* * 8-bit packed samples, w/ Map => RGB */DECLAREContigPutFunc(putRGBcontig8bitMaptile){    TIFFRGBValue* Map = img->Map;    int samplesperpixel = img->samplesperpixel;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	for (x = w; x-- > 0;) {	    *cp++ = PACK(Map[pp[0]], Map[pp[1]], Map[pp[2]]);	    pp += samplesperpixel;	}	pp += fromskew;	cp += toskew;    }}/* * 8-bit packed samples => RGBA w/ associated alpha * (known to have Map == NULL) */DECLAREContigPutFunc(putRGBAAcontig8bittile){    int samplesperpixel = img->samplesperpixel;    (void) x; (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	UNROLL8(w, NOP,	    *cp++ = PACK4(pp[0], pp[1], pp[2], pp[3]);	    pp += samplesperpixel);	cp += toskew;	pp += fromskew;    }}/* * 8-bit packed samples => RGBA w/ unassociated alpha * (known to have Map == NULL) */DECLAREContigPutFunc(putRGBUAcontig8bittile){    int samplesperpixel = img->samplesperpixel;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	uint32 r, g, b, a;	for (x = w; x-- > 0;) {	    a = pp[3];	    r = (pp[0] * a) / 255;	    g = (pp[1] * a) / 255;	    b = (pp[2] * a) / 255;	    *cp++ = PACK4(r,g,b,a);	    pp += samplesperpixel;	}	cp += toskew;	pp += fromskew;    }}/* * 16-bit packed samples => RGB */DECLAREContigPutFunc(putRGBcontig16bittile){    int samplesperpixel = img->samplesperpixel;    uint16 *wp = (uint16 *)pp;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	for (x = w; x-- > 0;) {	    *cp++ = PACKW(wp[0], wp[1], wp[2]);	    wp += samplesperpixel;	}	cp += toskew;	wp += fromskew;    }}/* * 16-bit packed samples => RGBA w/ associated alpha * (known to have Map == NULL) */DECLAREContigPutFunc(putRGBAAcontig16bittile){    int samplesperpixel = img->samplesperpixel;    uint16 *wp = (uint16 *)pp;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	for (x = w; x-- > 0;) {	    *cp++ = PACKW4(wp[0], wp[1], wp[2], wp[3]);	    wp += samplesperpixel;	}	cp += toskew;	wp += fromskew;    }}/* * 16-bit packed samples => RGBA w/ unassociated alpha * (known to have Map == NULL) */DECLAREContigPutFunc(putRGBUAcontig16bittile){    int samplesperpixel = img->samplesperpixel;    uint16 *wp = (uint16 *)pp;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	uint32 r,g,b,a;	/*	 * We shift alpha down four bits just in case unsigned	 * arithmetic doesn't handle the full range.	 * We still have plenty of accuracy, since the output is 8 bits.	 * So we have (r * 0xffff) * (a * 0xfff)) = r*a * (0xffff*0xfff)	 * Since we want r*a * 0xff for eight bit output,	 * we divide by (0xffff * 0xfff) / 0xff == 0x10eff.	 */	for (x = w; x-- > 0;) {	    a = wp[3] >> 4; 	    r = (wp[0] * a) / 0x10eff;	    g = (wp[1] * a) / 0x10eff;	    b = (wp[2] * a) / 0x10eff;	    *cp++ = PACK4(r,g,b,a);	    wp += samplesperpixel;	}	cp += toskew;	wp += fromskew;    }}/* * 8-bit packed CMYK samples w/o Map => RGB * * NB: The conversion of CMYK->RGB is *very* crude. */DECLAREContigPutFunc(putRGBcontig8bitCMYKtile){    int samplesperpixel = img->samplesperpixel;    uint16 r, g, b, k;    (void) x; (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	UNROLL8(w, NOP,	    k = 255 - pp[3];	    r = (k*(255-pp[0]))/255;	    g = (k*(255-pp[1]))/255;	    b = (k*(255-pp[2]))/255;	    *cp++ = PACK(r, g, b);	    pp += samplesperpixel);	cp += toskew;	pp += fromskew;    }}/* * 8-bit packed CMYK samples w/Map => RGB * * NB: The conversion of CMYK->RGB is *very* crude. */DECLAREContigPutFunc(putRGBcontig8bitCMYKMaptile){    int samplesperpixel = img->samplesperpixel;    TIFFRGBValue* Map = img->Map;    uint16 r, g, b, k;    (void) y;    fromskew *= samplesperpixel;    while (h-- > 0) {	for (x = w; x-- > 0;) {	    k = 255 - pp[3];	    r = (k*(255-pp[0]))/255;	    g = (k*(255-pp[1]))/255;	    b = (k*(255-pp[2]))/255;	    *cp++ = PACK(Map[r], Map[g], Map[b]);	    pp += samplesperpixel;	}	pp += fromskew;	cp += toskew;    }}#define	DECLARESepPutFunc(name) \static void name(\    TIFFRGBAImage* img,\    uint32* cp,\    uint32 x, uint32 y, \    uint32 w, uint32 h,\    int32 fromskew, int32 toskew,\    u_char* r, u_char* g, u_char* b, u_char* a\)/* * 8-bit unpacked samples => RGB */DECLARESepPutFunc(putRGBseparate8bittile){    (void) img; (void) x; (void) y; (void) a;    while (h-- > 0) {	UNROLL8(w, NOP, *cp++ = PACK(*r++, *g++, *b++));	SKEW(r, g, b, fromskew);	cp += toskew;    }}/* * 8-bit unpacked samples => RGB */DECLARESepPutFunc(putRGBseparate8bitMaptile){    TIFFRGBValue* Map = img->Map;    (void) y; (void) a;    while (h-- > 0) {	for (x = w; x > 0; x--)	    *cp++ = PACK(Map[*r++], Map[*g++], Map[*b++]);	SKEW(r, g, b, fromskew);	cp += toskew;    }}/* * 8-bit unpacked samples => RGBA w/ associated alpha */DECLARESepPutFunc(putRGBAAseparate8bittile){    (void) img; (void) x; (void) y;    while (h-- > 0) {	UNROLL8(w, NOP, *cp++ = PACK4(*r++, *g++, *b++, *a++));	SKEW4(r, g, b, a, fromskew);	cp += toskew;    }}/* * 8-bit unpacked samples => RGBA w/ unassociated alpha */DECLARESepPutFunc(putRGBUAseparate8bittile){    (void) img; (void) y;    while (h-- > 0) {	uint32 rv, gv, bv, av;

⌨️ 快捷键说明

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