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

📄 readerwriterrgb.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
                }                if(raw->bpc != 1)                    ConvertShort(&pixel, 1);                while (count--)                {                    if(raw->bpc == 1)                        *oPtr++ = pixel;                    else                    {                        tempShort = reinterpret_cast<unsigned short*>(oPtr);                        *tempShort = pixel;                        tempShort++;                        oPtr = reinterpret_cast<unsigned char *>(tempShort);                    }                }            }        }    }    else    {        raw->file->seekg(512+(y*raw->sizeX*raw->bpc)+(z*raw->sizeX*raw->sizeY*raw->bpc),std::ios::beg);        raw->file->read((char*)buf, raw->sizeX*raw->bpc);        if(raw->swapFlag && raw->bpc != 1){            ConvertShort(reinterpret_cast<unsigned short*>(buf), raw->sizeX);        }    }}static void RawImageGetData(rawImageRec *raw, unsigned char **data ){    unsigned char *ptr;    int i, j;    unsigned short *tempShort;    //     // round the width to a factor 4    //     int width = (int)(floorf((float)raw->sizeX/4.0f)*4.0f);    //     if (width!=raw->sizeX) width += 4;    // byte aligned.        osg::notify(osg::INFO)<<"raw->sizeX = "<<raw->sizeX<<std::endl;    osg::notify(osg::INFO)<<"raw->sizeY = "<<raw->sizeY<<std::endl;    osg::notify(osg::INFO)<<"raw->sizeZ = "<<raw->sizeZ<<std::endl;    osg::notify(osg::INFO)<<"raw->bpc = "<<raw->bpc<<std::endl;        *data = new unsigned char [(raw->sizeX)*(raw->sizeY)*(raw->sizeZ)*(raw->bpc)];    ptr = *data;    for (i = 0; i < (int)(raw->sizeY); i++)    {        if( raw->sizeZ >= 1 )            RawImageGetRow(raw, raw->tmpR, i, 0);        if( raw->sizeZ >= 2 )            RawImageGetRow(raw, raw->tmpG, i, 1);        if( raw->sizeZ >= 3 )            RawImageGetRow(raw, raw->tmpB, i, 2);        if( raw->sizeZ >= 4 )            RawImageGetRow(raw, raw->tmpA, i, 3);        for (j = 0; j < (int)(raw->sizeX); j++)        {          if(raw->bpc == 1){            if( raw->sizeZ >= 1 )                *ptr++ = *(raw->tmpR + j);            if( raw->sizeZ >= 2 )                *ptr++ = *(raw->tmpG + j);            if( raw->sizeZ >= 3 )                *ptr++ = *(raw->tmpB + j);            if( raw->sizeZ >= 4 )                *ptr++ = *(raw->tmpA + j);          }else{            if( raw->sizeZ >= 1 )            {                tempShort = reinterpret_cast<unsigned short*>(ptr);                *tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpR) + j);                tempShort++;                ptr = reinterpret_cast<unsigned char *>(tempShort);            }            if( raw->sizeZ >= 2 )            {                tempShort = reinterpret_cast<unsigned short*>(ptr);                *tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpG) + j);                tempShort++;                ptr = reinterpret_cast<unsigned char *>(tempShort);            }            if( raw->sizeZ >= 3 )            {                tempShort = reinterpret_cast<unsigned short*>(ptr);                *tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpB) + j);                tempShort++;                ptr = reinterpret_cast<unsigned char *>(tempShort);            }            if( raw->sizeZ >= 4 )            {                tempShort = reinterpret_cast<unsigned short*>(ptr);                *tempShort = *(reinterpret_cast<unsigned short*>(raw->tmpA) + j);                tempShort++;                ptr = reinterpret_cast<unsigned char *>(tempShort);            }          }        }        //         // pad the image width with blanks to bring it up to the rounded width.        //         for(;j<width;++j) *ptr++ = 0;    }}class ReaderWriterRGB : public osgDB::ReaderWriter{    public:            ReaderWriterRGB()        {            supportsExtension("rgb","rgb image format");            supportsExtension("rgba","rgba image format");            supportsExtension("sgi","sgi image format");            supportsExtension("int","int image format");            supportsExtension("inta","inta image format");            supportsExtension("bw","bw image format");        }            virtual const char* className() const { return "RGB Image Reader/Writer"; }                ReadResult readRGBStream(std::istream& fin) const        {            rawImageRec *raw;            if( (raw = RawImageOpen(fin)) == NULL )            {                return ReadResult::ERROR_IN_READING_FILE;            }            int s = raw->sizeX;            int t = raw->sizeY;            int r = 1;        #if 0            int internalFormat = raw->sizeZ == 3 ? GL_RGB5 :            raw->sizeZ == 4 ? GL_RGB5_A1 : GL_RGB;        #else            int internalFormat = raw->sizeZ;        #endif            unsigned int pixelFormat =                raw->sizeZ == 1 ? GL_LUMINANCE :                raw->sizeZ == 2 ? GL_LUMINANCE_ALPHA :                raw->sizeZ == 3 ? GL_RGB :                raw->sizeZ == 4 ? GL_RGBA : (GLenum)-1;            unsigned int dataType = raw->bpc == 1 ? GL_UNSIGNED_BYTE :              GL_UNSIGNED_SHORT;            unsigned char *data;            RawImageGetData(raw, &data);            RawImageClose(raw);            Image* image = new Image();            image->setImage(s,t,r,                internalFormat,                pixelFormat,                dataType,                data,                osg::Image::USE_NEW_DELETE);            notify(INFO) << "image read ok "<<s<<"  "<<t<< std::endl;            return image;        }        virtual ReadResult readObject(std::istream& fin,const osgDB::ReaderWriter::Options* options =NULL) const        {            return readImage(fin, options);        }        virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const        {            return readImage(file, options);        }        virtual ReadResult readImage(std::istream& fin,const osgDB::ReaderWriter::Options* =NULL) const        {            return readRGBStream(fin);        }        virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const        {            std::string ext = osgDB::getLowerCaseFileExtension(file);            if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;            std::string fileName = osgDB::findDataFile( file, options );            if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;            osgDB::ifstream istream(fileName.c_str(), std::ios::in | std::ios::binary);            if(!istream) return ReadResult::FILE_NOT_HANDLED;            ReadResult rr = readRGBStream(istream);            if(rr.validImage()) rr.getImage()->setFileName(file);            return rr;        }        WriteResult writeRGBStream(const osg::Image& img, std::ostream &fout, const std::string& name) const        {            rawImageRec raw;            raw.imagic = 0732;            GLenum dataType = img.getDataType();            raw.type  = dataType == GL_UNSIGNED_BYTE ? 1 :                dataType == GL_BYTE ? 1 :                dataType == GL_BITMAP ? 1 :                dataType == GL_UNSIGNED_SHORT ? 2 :                dataType == GL_SHORT ? 2 :                dataType == GL_UNSIGNED_INT ? 4 :                dataType == GL_INT ? 4 :                dataType == GL_FLOAT ? 4 :                dataType == GL_UNSIGNED_BYTE_3_3_2 ? 1 :                dataType == GL_UNSIGNED_BYTE_2_3_3_REV ? 1 :                dataType == GL_UNSIGNED_SHORT_5_6_5 ? 2 :                dataType == GL_UNSIGNED_SHORT_5_6_5_REV ? 2 :                dataType == GL_UNSIGNED_SHORT_4_4_4_4 ? 2 :                dataType == GL_UNSIGNED_SHORT_4_4_4_4_REV ? 2 :                dataType == GL_UNSIGNED_SHORT_5_5_5_1 ? 2 :                dataType == GL_UNSIGNED_SHORT_1_5_5_5_REV ? 2 :                dataType == GL_UNSIGNED_INT_8_8_8_8 ? 4 :                dataType == GL_UNSIGNED_INT_8_8_8_8_REV ? 4 :                dataType == GL_UNSIGNED_INT_10_10_10_2 ? 4 :                dataType == GL_UNSIGNED_INT_2_10_10_10_REV ? 4 : 4;            GLenum pixelFormat = img.getPixelFormat();            raw.dim    = 3;            raw.sizeX = img.s();            raw.sizeY = img.t();            raw.sizeZ =                pixelFormat == GL_COLOR_INDEX? 1 :                pixelFormat == GL_RED? 1 :                pixelFormat == GL_GREEN? 1 :                pixelFormat == GL_BLUE? 1 :                pixelFormat == GL_ALPHA? 1 :                pixelFormat == GL_RGB? 3 :                pixelFormat == GL_BGR ? 3 :                pixelFormat == GL_RGBA? 4 :                pixelFormat == GL_BGRA? 4 :                pixelFormat == GL_LUMINANCE? 1 :                pixelFormat == GL_LUMINANCE_ALPHA ? 2 : 1;            raw.min = 0;            raw.max = 0xFF;            raw.wasteBytes = 0;            strncpy( raw.name, name.c_str(), 80);            raw.colorMap = 0;            raw.bpc = (img.getPixelSizeInBits()/raw.sizeZ)/8;            int isize = img.getImageSizeInBytes();            unsigned char *buffer = new unsigned char[isize];            if(raw.bpc == 1)            {                unsigned char *dptr = buffer;                int i, j;                for( i = 0; i < raw.sizeZ; ++i )                {                    const unsigned char *ptr = img.data();                    ptr += i;                    for( j = 0; j < isize/raw.sizeZ; ++j )                    {                        *(dptr++) = *ptr;                        ptr += raw.sizeZ;                    }                }            }            else            { // bpc == 2                unsigned short *dptr = reinterpret_cast<unsigned short*>(buffer);                int i, j;                for( i = 0; i < raw.sizeZ; ++i )                {                    const unsigned short *ptr = reinterpret_cast<const unsigned short*>(img.data());                    ptr += i;                    for( j = 0; j < isize/(raw.sizeZ*2); ++j )                    {                        *dptr = *ptr;                        ConvertShort(dptr++, 1);                        ptr += raw.sizeZ;                    }                }            }                    if( raw.needsBytesSwapped() )                raw.swapBytes();            /*            swapBytes( raw.imagic );            swapBytes( raw.type );            swapBytes( raw.dim );            swapBytes( raw.sizeX );            swapBytes( raw.sizeY );            swapBytes( raw.sizeZ );            swapBytes( raw.min );            swapBytes( raw.max );            swapBytes( raw.colorMap );            */            char pad[512 - sizeof(rawImageRec)];            memset( pad, 0, sizeof(pad));            fout.write((const char*)&raw,sizeof(rawImageRec));            fout.write((const char*)pad,sizeof(pad));            fout.write((const char*)buffer,isize);            delete [] buffer;            return WriteResult::FILE_SAVED;        }        virtual WriteResult writeImage(const osg::Image& img,std::ostream& fout,const osgDB::ReaderWriter::Options*) const        {            return writeRGBStream(img,fout,"");        }        virtual WriteResult writeImage(const osg::Image &img,const std::string& fileName, const osgDB::ReaderWriter::Options*) const        {            std::string ext = osgDB::getFileExtension(fileName);            if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;            osgDB::ofstream fout(fileName.c_str(), std::ios::out | std::ios::binary);            if(!fout) return WriteResult::ERROR_IN_WRITING_FILE;            return writeRGBStream(img,fout,fileName);        }};// now register with Registry to instantiate the above// reader/writer.REGISTER_OSGPLUGIN(rgb, ReaderWriterRGB)

⌨️ 快捷键说明

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