From 98ed1a8d7d50405f58617878300ba6cd633aeecc Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 17 Oct 2003 10:23:24 +0000 Subject: [PATCH] From Michael Gronenger, updates to JP2 plugin. --- src/osgPlugins/jp2/ReaderWriterJP2.cpp | 177 ++++++++++++++++++++++++- 1 file changed, 173 insertions(+), 4 deletions(-) diff --git a/src/osgPlugins/jp2/ReaderWriterJP2.cpp b/src/osgPlugins/jp2/ReaderWriterJP2.cpp index 05c0099ac..2b7b26f3b 100644 --- a/src/osgPlugins/jp2/ReaderWriterJP2.cpp +++ b/src/osgPlugins/jp2/ReaderWriterJP2.cpp @@ -6,11 +6,14 @@ #include #include +#include #include +#include +#include +#include #include #include -#include extern "C" { @@ -124,7 +127,8 @@ extern "C" { } } - for (y = 0; y < height; ++y) + for (y = height - 1; y >= 0; --y) +// for (y = 0; y < height; ++y) { for (x = 0; x < width; ++x) { @@ -179,6 +183,13 @@ class ReaderWriterJP2 : public osgDB::ReaderWriter std::string ext = osgDB::getFileExtension(fileName); if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED; + if(!osgDB::fileExists(fileName)) + { + osg::Image *img = new osg::Image; + img->setFileName(fileName); + return img; + } + jas_init(); jas_stream_t* in = jas_stream_fopen(fileName.c_str(), "rb"); @@ -223,13 +234,81 @@ class ReaderWriterJP2 : public osgDB::ReaderWriter pixelFormat, dataType, data, - osg::Image::USE_NEW_DELETE); +// osg::Image::USE_NEW_DELETE); + osg::Image::NO_DELETE); notify(INFO) << "image read ok "< vdata; + + while(!fin.eof()) + { + fin.read(&c, 1); + vdata.push_back(c); + } + ssize = vdata.size(); + + sdata = &vdata[0]; + + jas_init(); + + jas_stream_t* in = jas_stream_memopen((char*)sdata, ssize); + + 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 "<tlx = 0; + cmptparm->tly = 0; + cmptparm->hstep = 1; + cmptparm->vstep = 1; + cmptparm->width = img.s(); + cmptparm->height = img.t(); + cmptparm->prec = 8; + cmptparm->sgnd = 0; + } + if (!(jimage = jas_image_create(internalFormat, cmptparms, JAS_CLRSPC_UNKNOWN))) { + return WriteResult::ERROR_IN_WRITING_FILE; + } + + if(internalFormat == 1) + { + jas_image_setclrspc(jimage, JAS_CLRSPC_SGRAY); + jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); + } + else if(internalFormat == 2) + { + jas_image_setclrspc(jimage, JAS_CLRSPC_SGRAY); + jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y)); + jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY)); + } + else if(internalFormat == 3) + { + jas_image_setclrspc(jimage, JAS_CLRSPC_SRGB); + jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); + jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); + jas_image_setcmpttype(jimage, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); + } + else if(internalFormat == 4) + { + jas_image_setclrspc(jimage, JAS_CLRSPC_SRGB); + jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R)); + jas_image_setcmpttype(jimage, 1, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G)); + jas_image_setcmpttype(jimage, 2, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B)); + jas_image_setcmpttype(jimage, 3, JAS_IMAGE_CT_COLOR(JAS_IMAGE_CT_OPACITY)); + } + + getdata(mem, jimage); + + jas_stream_t* out = jas_stream_memopen(0, 0); +// jas_stream_t* out = jas_stream_fopen(fileName.c_str(), "wb"); + if (!out) + return WriteResult::ERROR_IN_WRITING_FILE; + + char* opt = 0; + if(options) + { + opt = new char[options->getOptionString().size() + 1]; + strcpy(opt, options->getOptionString().c_str()); + } + jas_image_encode(jimage, out, jas_image_strtofmt("jp2"), opt); + if(opt) delete[] opt; + + jas_stream_flush(out); + + // now the encoded jp2 image resides in the out->buf_ member with size out->len_ we now need to stream it to a std::ostream + jas_stream_memobj_t* obj = (jas_stream_memobj_t*) out->obj_; + + fout.write((char*)obj->buf_, obj->len_); + + fout << std::flush; + + jas_stream_close(out); + + jas_image_destroy(jimage); + jas_image_clearfmts(); + + return WriteResult::FILE_SAVED; + } + }; // now register with Registry to instantiate the above