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

📄 tif_getimage.c

📁 一个国人自己实现图像库的程序(有参考价值)
💻 C
📖 第 1 页 / 共 5 页
字号:
            {                ret = 0;                break;            }            pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);    	    if (col + tw > w)             {                /*                 * Tile is clipped horizontally.  Calculate                 * visible portion and skewing factors.                 */                uint32 npix = w - col;                fromskew = tw - npix;                (*put)(img, raster+y*w+col, col, y,                       npix, nrow, fromskew, toskew + fromskew, buf + pos);            }            else             {                (*put)(img, raster+y*w+col, col, y, tw, nrow, 0, toskew, buf + pos);            }        }        y += (orientation == ORIENTATION_TOPLEFT ? -(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (ret);}/* * Get an tile-organized image that has *	 SamplesPerPixel > 1 *	 PlanarConfiguration separated * We assume that all such images are RGB. */	static intgtTileSeparate(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h){    TIFF* tif = img->tif;    tileSeparateRoutine put = img->put.separate;    uint16 orientation;    uint32 col, row, y, rowstoread;
    uint32 pos;    uint32 tw, th;    u_char* buf;    u_char* r;    u_char* g;    u_char* b;    u_char* a;    tsize_t tilesize;    int32 fromskew, toskew;    int alpha = img->alpha;    uint32 nrow;    int ret = 1;    tilesize = TIFFTileSize(tif);    buf = (u_char*) _TIFFmalloc(4*tilesize);    if (buf == 0) {	TIFFError(TIFFFileName(tif), "No space for tile buffer");	return (0);    }    r = buf;    g = r + tilesize;    b = g + tilesize;    a = b + tilesize;    if (!alpha)	memset(a, 0xff, tilesize);    TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tw);    TIFFGetField(tif, TIFFTAG_TILELENGTH, &th);    y = setorientation(img, h);    orientation = img->orientation;    toskew = -(int32) (orientation == ORIENTATION_TOPLEFT ? tw+w : tw-w);    for (row = 0; row < h; row += nrow)     {        rowstoread = th - (row + img->row_offset) % th;    	nrow = (row + rowstoread > h ? h - row : rowstoread);        for (col = 0; col < w; col += tw)         {            if (TIFFReadTile(tif, r, col+img->col_offset,                             row+img->row_offset,0,0) < 0 && img->stoponerr)            {                ret = 0;                break;            }            if (TIFFReadTile(tif, g, col+img->col_offset,                             row+img->row_offset,0,1) < 0 && img->stoponerr)            {                ret = 0;                break;            }            if (TIFFReadTile(tif, b, col+img->col_offset,                             row+img->row_offset,0,2) < 0 && img->stoponerr)            {                ret = 0;                break;            }            if (alpha && TIFFReadTile(tif,a,col+img->col_offset,                                      row+img->row_offset,0,3) < 0 && img->stoponerr)            {                ret = 0;                break;            }            pos = ((row+img->row_offset) % th) * TIFFTileRowSize(tif);            if (col + tw > w)             {                /*                 * Tile is clipped horizontally.  Calculate                 * visible portion and skewing factors.                 */                uint32 npix = w - col;                fromskew = tw - npix;                (*put)(img, raster+y*w+col, col, y,                       npix, nrow, fromskew, toskew + fromskew,                        r + pos, g + pos, b + pos, a + pos);            }             else             {                (*put)(img, raster+y*w+col, col, y,                       tw, nrow, 0, toskew, r + pos, g + pos, b + pos, a + pos);            }        }        y += (orientation == ORIENTATION_TOPLEFT ?-(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (ret);}/* * Get a strip-organized image that has *	PlanarConfiguration contiguous if SamplesPerPixel > 1 * or *	SamplesPerPixel == 1 */	static intgtStripContig(TIFFRGBAImage* img, uint32* raster, uint32 w, uint32 h){    TIFF* tif = img->tif;    tileContigRoutine put = img->put.contig;    uint16 orientation;    uint32 row, y, nrow, rowstoread;    uint32 pos;    u_char* buf;    uint32 rowsperstrip;    uint32 imagewidth = img->width;    tsize_t scanline;    int32 fromskew, toskew;    int ret = 1;    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 += nrow)     {        rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;        nrow = (row + rowstoread > h ? h - row : rowstoread);        if (TIFFReadEncodedStrip(tif,                                 TIFFComputeStrip(tif,row+img->row_offset, 0),                                 buf,                                  ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0            && img->stoponerr)        {            ret = 0;            break;        }        pos = ((row + img->row_offset) % rowsperstrip) * scanline;        (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, buf + pos);        y += (orientation == ORIENTATION_TOPLEFT ?-(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (ret);}/* * 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, rowstoread;    uint32 pos;    tsize_t scanline;    uint32 rowsperstrip, offset_row;    uint32 imagewidth = img->width;    tsize_t stripsize;    int32 fromskew, toskew;    int alpha = img->alpha;    int	ret = 1;    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 += nrow)     {        rowstoread = rowsperstrip - (row + img->row_offset) % rowsperstrip;    	        nrow = (row + rowstoread > h ? h - row : rowstoread);        offset_row = row + img->row_offset;    	if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 0),                                 r, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0             && img->stoponerr)        {            ret = 0;            break;        }        if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 1),                                 g, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0             && img->stoponerr)        {            ret = 0;            break;        }        if (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 2),                                 b, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0             && img->stoponerr)        {            ret = 0;            break;        }        if (alpha &&            (TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, offset_row, 3),                                  a, ((row + img->row_offset)%rowsperstrip + nrow) * scanline) < 0              && img->stoponerr))        {            ret = 0;            break;        }        pos = ((row + img->row_offset) % rowsperstrip) * scanline;        (*put)(img, raster+y*w, 0, y, w, nrow, fromskew, toskew, r + pos, g + pos,                b + pos, a + pos);        y += (orientation == ORIENTATION_TOPLEFT ? -(int32) nrow : (int32) nrow);    }    _TIFFfree(buf);    return (ret);}/* * 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;    int samplesperpixel = img->samplesperpixel;    (void) y;    while (h-- > 0) {	for (x = w; x-- > 0;)        {	    *cp++ = PALmap[*pp][0];            pp += samplesperpixel;        }	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){    int samplesperpixel = img->samplesperpixel;    uint32** BWmap = img->BWmap;    (void) y;    while (h-- > 0) {	for (x = w; x-- > 0;)        {	    *cp++ = BWmap[*pp][0];            pp += samplesperpixel;        }	cp += toskew;	pp += fromskew;    }}/* * 16-bit greyscale => colormap/RGB */DECLAREContigPutFunc(put16bitbwtile){    int samplesperpixel = img->samplesperpixel;    uint32** BWmap = img->BWmap;    (void) y;    while (h-- > 0) {        uint16 *wp = (uint16 *) pp;	for (x = w; x-- > 0;)        {            /* use high order byte of 16bit value */	    *cp++ = BWmap[*wp >> 8][0];            pp += 2 * samplesperpixel;            wp += samplesperpixel;        }	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

⌨️ 快捷键说明

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