📄 readerwritertiff.cpp
字号:
#include <osg/Image>#include <osg/Notify>#include <osg/Geode>#include <osg/GL>#include <osgDB/Registry>#include <osgDB/FileUtils>#include <osgDB/FileNameUtils>#include <stdio.h>#include <tiffio.h>/**************************************************************************** * * Follows is code extracted from the simage library. Original Authors: * * Systems in Motion, * <URL:http://www.sim.no> * * Peder Blekken <pederb@sim.no> * Morten Eriksen <mortene@sim.no> * Marius Bugge Monsen <mariusbu@sim.no> * * The original COPYING notice * * All files in this library are public domain, except simage_rgb.cpp which is * Copyright (c) Mark J Kilgard <mjk@nvidia.com>. I will contact Mark * very soon to hear if this source also can become public domain. * * Please send patches for bugs and new features to: <pederb@sim.no>. * * Peder Blekken * * * Ported into the OSG as a plugin, Robert Osfield Decemeber 2000. * Note, reference above to license of simage_rgb is not relevent to the OSG * as the OSG does not use it. Also for patches, bugs and new features * please send them direct to the OSG dev team rather than address above. * **********************************************************************/#include <string.h>#include <stdarg.h>#include <assert.h>#include <stdlib.h>#define ERR_NO_ERROR 0#define ERR_OPEN 1#define ERR_READ 2#define ERR_MEM 3#define ERR_UNSUPPORTED 4#define ERR_TIFFLIB 5/* Functions to read TIFF image from memory * */tsize_t libtiffStreamReadProc(thandle_t fd, tdata_t buf, tsize_t size){ std::istream *fin = (std::istream*)fd; fin->read((char*)buf,size); if(fin->bad()) return -1; if(fin->gcount() < size) return 0; return size;}tsize_t libtiffStreamWriteProc(thandle_t, tdata_t, tsize_t){ return 0;}toff_t libtiffStreamSeekProc(thandle_t fd, toff_t off, int i){ std::istream *fin = (std::istream*)fd; toff_t ret; switch(i) { case SEEK_SET: fin->seekg(off,std::ios::beg); ret = fin->tellg(); if(fin->bad()) ret = 0; break; case SEEK_CUR: fin->seekg(off,std::ios::cur); ret = fin->tellg(); if(fin->bad()) ret = 0; break; case SEEK_END: fin->seekg(off,std::ios::end); ret = fin->tellg(); if(fin->bad()) ret = 0; break; default: ret = 0; break; } return ret;}int libtiffStreamCloseProc(thandle_t){ return 0;}toff_t libtiffStreamSizeProc(thandle_t fd){ std::istream *fin = (std::istream*)fd; std::streampos curPos = fin->tellg(); fin->seekg(0, std::ios::end); toff_t size = fin->tellg(); fin->seekg(curPos, std::ios::beg); return size;}int libtiffStreamMapProc(thandle_t, tdata_t*, toff_t*){ return 0;}void libtiffStreamUnmapProc(thandle_t, tdata_t, toff_t){}/* Functions to write TIFF image from memory * */tsize_t libtiffOStreamReadProc(thandle_t, tdata_t, tsize_t){ return 0;}tsize_t libtiffOStreamWriteProc(thandle_t fd, tdata_t buf, tsize_t size){ std::ostream *fout = (std::ostream*)fd; fout->write((const char*)buf,size); if(fout->bad()) { return -1; } return size;}toff_t libtiffOStreamSizeProc(thandle_t fd){ std::ostream *fout = (std::ostream*)fd; std::streampos curPos = fout->tellp(); fout->seekp(0, std::ios::end); toff_t size = fout->tellp(); fout->seekp(curPos, std::ios::beg); return size;}toff_t libtiffOStreamSeekProc(thandle_t fd, toff_t off, int i){ std::ostream *fout = (std::ostream*)fd; toff_t ret; switch(i) { case SEEK_SET: fout->seekp(off,std::ios::beg); ret = fout->tellp(); if(fout->bad()) ret = 0; break; case SEEK_CUR: fout->seekp(off,std::ios::cur); ret = fout->tellp(); if(fout->bad()) ret = 0; break; case SEEK_END: fout->seekp(off,std::ios::end); ret = fout->tellp(); if(fout->bad()) ret = 0; break; default: ret = 0; break; } return ret;}static int tifferror = ERR_NO_ERROR;intsimage_tiff_error(char * buffer, int buflen){ switch (tifferror) { case ERR_OPEN: strncpy(buffer, "TIFF loader: Error opening file", buflen); break; case ERR_MEM: strncpy(buffer, "TIFF loader: Out of memory error", buflen); break; case ERR_UNSUPPORTED: strncpy(buffer, "TIFF loader: Unsupported image type", buflen); break; case ERR_TIFFLIB: strncpy(buffer, "TIFF loader: Illegal tiff file", buflen); break; } return tifferror;}static voidtiff_error(const char*, const char*, va_list){ // values are (const char* module, const char* fmt, va_list list) /* FIXME: store error message ? */}static voidtiff_warn(const char *, const char *, va_list){ // values are (const char* module, const char* fmt, va_list list) /* FIXME: notify? */}static intcheckcmap(int n, uint16* r, uint16* g, uint16* b){ while (n-- > 0) if (*r++ >= 256 || *g++ >= 256 || *b++ >= 256) return (16); /* Assuming 8-bit colormap */ return (8);}static voidinvert_row(unsigned char *ptr, unsigned char *data, int n, int invert, uint16 bitspersample){ // osg::notify(osg::NOTICE)<<"invert_row "<<invert<<std::endl; if (bitspersample == 8) { while (n--) { if (invert) *ptr++ = 255 - *data++; else *ptr++ = *data++; } } else if (bitspersample == 16) { unsigned short *ptr1 = (unsigned short *)ptr; unsigned short *data1 = (unsigned short *)data; while (n--) { if (invert) *ptr1++ = 65535 - *data1++; else *ptr1++ = *data1++; } } else if (bitspersample == 32) { float *ptr1 = (float *)ptr; float *data1 = (float *)data; while (n--) { if (invert) *ptr1++ = 1.0 - *data1++; else *ptr1++ = *data1++; } }}static voidremap_row(unsigned char *ptr, unsigned char *data, int n,unsigned short *rmap, unsigned short *gmap, unsigned short *bmap){ // osg::notify(osg::NOTICE)<<"remap row"<<std::endl; unsigned int ix; while (n--) { ix = *data++; *ptr++ = (unsigned char) rmap[ix]; *ptr++ = (unsigned char) gmap[ix]; *ptr++ = (unsigned char) bmap[ix]; }}static void interleave_row(unsigned char *ptr, unsigned char *red, unsigned char *green, unsigned char *blue, int n, int numSamples, uint16 bitspersample){ // osg::notify(osg::NOTICE)<<"Interleave row RGB"<<std::endl; if (bitspersample == 8) { while (n--) { *ptr++ = *red++; *ptr++ = *green++; *ptr++ = *blue++; if (numSamples==4) *ptr++ = 255; } } else if (bitspersample == 16) { unsigned short *ptr1 = (unsigned short *)ptr; unsigned short *red1 = (unsigned short *)red; unsigned short *green1 = (unsigned short *)green; unsigned short *blue1 = (unsigned short *)blue; while (n--) { *ptr1++ = *red1++; *ptr1++ = *green1++; *ptr1++ = *blue1++; if (numSamples==4) *ptr1++ = 65535; } } else if (bitspersample == 32) { float *ptr1 = (float *)ptr; float *red1 = (float *)red; float *green1 = (float *)green; float *blue1 = (float *)blue; while (n--) { *ptr1++ = *red1++; *ptr1++ = *green1++; *ptr1++ = *blue1++; if (numSamples==4) *ptr1++ = 1.0f; } }}static void interleave_row(unsigned char *ptr, unsigned char *red, unsigned char *green, unsigned char *blue, unsigned char *alpha, int n, int numSamples, uint16 bitspersample){ // osg::notify(osg::NOTICE)<<"Interleave row RGBA"<<std::endl; if (bitspersample == 8) { while (n--) { *ptr++ = *red++; *ptr++ = *green++; *ptr++ = *blue++; if (numSamples==4) *ptr++ = *alpha++; } } else if (bitspersample == 16) { unsigned short *ptr1 = (unsigned short *)ptr; unsigned short *red1 = (unsigned short *)red; unsigned short *green1 = (unsigned short *)green; unsigned short *blue1 = (unsigned short *)blue; unsigned short *alpha1 = (unsigned short *)alpha; while (n--) { *ptr1++ = *red1++; *ptr1++ = *green1++; *ptr1++ = *blue1++; if (numSamples==4) *ptr1++ = *alpha1++; } } else if (bitspersample == 32) { float *ptr1 = (float *)ptr; float *red1 = (float *)red; float *green1 = (float *)green; float *blue1 = (float *)blue; float *alpha1 = (float *)alpha; while (n--) { *ptr1++ = *red1++; *ptr1++ = *green1++; *ptr1++ = *blue1++; if (numSamples==4) *ptr1++ = *alpha1++; } }}intsimage_tiff_identify(const char *,const unsigned char *header,int headerlen){ static unsigned char tifcmp[] = {0x4d, 0x4d, 0x0, 0x2a}; static unsigned char tifcmp2[] = {0x49, 0x49, 0x2a, 0}; if (headerlen < 4) return 0; if (memcmp((const void*)header, (const void*)tifcmp, 4) == 0) return 1; if (memcmp((const void*)header, (const void*)tifcmp2, 4) == 0) return 1; return 0;}/* useful defines (undef'ed below) */#define CVT(x) (((x) * 255L) / ((1L<<16)-1))#define pack(a,b) ((a)<<8 | (b))unsigned char *simage_tiff_load(std::istream& fin, int& width_ret, int& height_ret, int& numComponents_ret, uint16& bitspersample){ TIFF *in; uint16 dataType; uint16 samplesperpixel; uint16 photometric; uint32 w, h; uint16 config; uint16* red; uint16* green; uint16* blue; unsigned char *inbuf = NULL; tsize_t rowsize;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -