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

📄 readerwriterjp2.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
#include <osg/Image>#include <osg/Notify>#include <osg/Geode>#include <osg/GL>#include <osgDB/FileNameUtils>#include <osgDB/FileUtils>#include <osgDB/Registry>#include <string>#include <sstream>#include <vector>#include <stdio.h>#include <stdlib.h>extern "C"{    #include <jasper/jasper.h>}#ifndef SEEK_SET#  define SEEK_SET 0#endifusing namespace osg;extern "C" {    static int putdata(jas_stream_t *out, jas_image_t *image, int numcmpts)    {        int ret;        int cmptno;        int x;        int y;        jas_matrix_t *data[4];        jas_seqent_t *d[4];        jas_seqent_t v;        int linelen;        int width, height;        width = jas_image_cmptwidth(image, 0);        height = jas_image_cmptheight(image, 0);        ret = -1;        data[0] = 0;        data[1] = 0;        data[2] = 0;        data[3] = 0;        for (cmptno = 0; cmptno < numcmpts; ++cmptno) {            if (!(data[cmptno] = jas_matrix_create(1, width))) {                goto done;            }        }        for (y = height - 1; y >= 0; --y) {            for (cmptno = 0; cmptno < numcmpts; ++cmptno) {                if (jas_image_readcmpt(image, cmptno, 0, y, width, 1,                  data[cmptno])) {                    goto done;                }                d[cmptno] = jas_matrix_getref(data[cmptno], 0, 0);            }            linelen = 0;            for (x = 0; x < width; ++x) {                for (cmptno = 0; cmptno < numcmpts; ++cmptno) {                    v = *d[cmptno];                    if (v < 0) {                        v = 0;                    }                    if (v > 255) {                        v = 255;                    }                    unsigned char c;                    c = v;                    if (jas_stream_putc(out, c) == EOF) {                        goto done;                    }                    ++d[cmptno];                }            }            if (jas_stream_error(out)) {                goto done;            }        }        jas_stream_flush(out);        ret = 0;    done:        for (cmptno = 0; cmptno < numcmpts; ++cmptno) {            if (data[cmptno]) {                jas_matrix_destroy(data[cmptno]);            }        }        return ret;    }    static int getdata(jas_stream_t *in, jas_image_t *image)    {        int ret;        int numcmpts;        int cmptno;        jas_matrix_t *data[4];        int x;        int y;        int width, height;        width = jas_image_cmptwidth(image, 0);        height = jas_image_cmptheight(image, 0);        numcmpts = jas_image_numcmpts(image);        ret = -1;        data[0] = 0;        data[1] = 0;        data[2] = 0;        data[3] = 0;        for (cmptno = 0; cmptno < numcmpts; ++cmptno) {            if (!(data[cmptno] = jas_matrix_create(1, width))) {                goto done;            }        }        for (y = height - 1; y >= 0; --y)//        for (y = 0; y < height; ++y)        {            for (x = 0; x < width; ++x)            {                for (cmptno = 0; cmptno < numcmpts; ++cmptno)                {                    /* The sample data is unsigned. */                    int c;                    if ((c = jas_stream_getc(in)) == EOF) {                        return -1;                    }                    jas_matrix_set(data[cmptno], 0, x, c);                }            }            for (cmptno = 0; cmptno < numcmpts; ++cmptno) {                if (jas_image_writecmpt(image, cmptno, 0, y, width, 1,                  data[cmptno])) {                    goto done;                }            }        }        jas_stream_flush(in);        ret = 0;    done:        for (cmptno = 0; cmptno < numcmpts; ++cmptno) {            if (data[cmptno]) {                jas_matrix_destroy(data[cmptno]);            }        }        return ret;    }}class ReaderWriterJP2 : public osgDB::ReaderWriter{    public:        ReaderWriterJP2()        {            supportsExtension("jp2","Jpeg2000 image format");            supportsExtension("jpc","Jpeg2000 image format");                    // little dance here to get around warnings created by jas_image_strtofmt use of char* rather than const char*            // as a parameted and modern compilers deprecating "jp2" string being treated as char*.            char* jp2 = strdup("jp2");            _fmt_jp2 = jas_image_strtofmt(jp2);            free(jp2);        }        virtual const char* className() const { return "RGB Image Reader/Writer"; }                virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options) const        {            return readImage(file,options);        }        virtual ReadResult readObject(std::istream& fin, const Options* options) const        {            return readImage(fin,options);        }        virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const        {            std::string ext = osgDB::getFileExtension(file);            if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;            std::string fileName = osgDB::findDataFile( file, options );            if(fileName.empty())            {                // note from Robert, Dec03, I find returning a valid image when no                // file exists a bit odd...                osg::Image *img = new osg::Image;                img->setFileName(fileName);                return img;            }            jas_init();            jas_stream_t* in = jas_stream_fopen(fileName.c_str(), "rb");            char* opt = 0;            if(options)            {                opt = new char[options->getOptionString().size() + 1];                strcpy(opt, options->getOptionString().c_str());            }            jas_image_t* jimage = jas_image_decode(in, -1, opt); // last is the option string whatto put there?            if(opt) delete[] opt;            int internalFormat = jimage->numcmpts_;            int s = jas_image_width(jimage);            int t = jas_image_height(jimage);            int r = 1;            unsigned char* data = new unsigned char[internalFormat*s*t];            jas_stream_t* mem = jas_stream_memopen((char*)data, internalFormat*s*t);            putdata(mem, jimage, internalFormat);            jas_image_destroy(jimage);            jas_stream_close(in);            jas_image_clearfmts();            unsigned int pixelFormat =                internalFormat == 1 ? GL_LUMINANCE :                internalFormat == 2 ? GL_LUMINANCE_ALPHA :                internalFormat == 3 ? GL_RGB :                internalFormat == 4 ? GL_RGBA : (GLenum)-1;            unsigned int dataType = GL_UNSIGNED_BYTE;            Image* image = new Image();            image->setFileName(fileName.c_str());            image->setImage(s,t,r,                internalFormat,                pixelFormat,                dataType,                data,//                osg::Image::USE_NEW_DELETE);                osg::Image::NO_DELETE);            notify(INFO) << "image read ok "<<s<<"  "<<t<< std::endl;            return image;        }

⌨️ 快捷键说明

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