From Michael Gronager, jp2 plugin for reading and writing JPEG2000 files.
This commit is contained in:
@@ -52,6 +52,8 @@ PLUGIN_DIRS = \
|
||||
3dc\
|
||||
ive\
|
||||
|
||||
# comment in if you have japser (JPEG2000 library) installed.
|
||||
# PLUGIN_DIRS += jp2
|
||||
|
||||
# comment in if you have Performer installed.
|
||||
# PLUGIN_DIRS += pfb
|
||||
@@ -84,6 +86,13 @@ endif
|
||||
endif
|
||||
endif
|
||||
|
||||
# comment in if you want Open DX support, currently not on by default
|
||||
# since we havn't yet checked compilation on all platforms yet.
|
||||
# note, moved to non Darwin build since it currently breaks the Drawin build.
|
||||
|
||||
# commented out while the DX plugin is converted across to use Geometry rather than GeoSet.
|
||||
# PLUGIN_DIRS += dx
|
||||
|
||||
|
||||
#### MacOS X specific definitions
|
||||
ifeq ($(OS),Darwin)
|
||||
@@ -92,14 +101,6 @@ ifeq ($(OS),Darwin)
|
||||
|
||||
else
|
||||
|
||||
# comment in if you want Open DX support, currently not on by default
|
||||
# since we havn't yet checked compilation on all platforms yet.
|
||||
# note, moved to non Darwin build since it currently breaks the Drawin build.
|
||||
|
||||
# commented out while the DX plugin is converted across to use Geometry rather than GeoSet.
|
||||
# PLUGIN_DIRS += dx
|
||||
|
||||
|
||||
# comment in if you have libtiff installed.
|
||||
PLUGIN_DIRS += tiff
|
||||
|
||||
|
||||
109
VisualStudio/osgPlugins/jp2/jp2.dsp
Normal file
109
VisualStudio/osgPlugins/jp2/jp2.dsp
Normal file
@@ -0,0 +1,109 @@
|
||||
# Microsoft Developer Studio Project File - Name="osgPlugin jp2" - Package Owner=<4>
|
||||
# Microsoft Developer Studio Generated Build File, Format Version 6.00
|
||||
# ** DO NOT EDIT **
|
||||
|
||||
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
|
||||
|
||||
CFG=osgPlugin jp2 - Win32 Release
|
||||
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
|
||||
!MESSAGE use the Export Makefile command and run
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "jp2.mak".
|
||||
!MESSAGE
|
||||
!MESSAGE You can specify a configuration when running NMAKE
|
||||
!MESSAGE by defining the macro CFG on the command line. For example:
|
||||
!MESSAGE
|
||||
!MESSAGE NMAKE /f "jp2.mak" CFG="osgPlugin jp2 - Win32 Release"
|
||||
!MESSAGE
|
||||
!MESSAGE Possible choices for configuration are:
|
||||
!MESSAGE
|
||||
!MESSAGE "osgPlugin jp2 - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE "osgPlugin jp2 - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
|
||||
!MESSAGE
|
||||
|
||||
# Begin Project
|
||||
# PROP AllowPerConfigDependencies 0
|
||||
# PROP Scc_ProjName ""
|
||||
# PROP Scc_LocalPath ""
|
||||
CPP=cl.exe
|
||||
MTL=midl.exe
|
||||
RSC=rc.exe
|
||||
|
||||
!IF "$(CFG)" == "osgPlugin jp2 - Win32 Release"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 0
|
||||
# PROP BASE Output_Dir "Release"
|
||||
# PROP BASE Intermediate_Dir "Release"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 0
|
||||
# PROP Output_Dir "../../../lib"
|
||||
# PROP Intermediate_Dir "Release"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /YX /FD /c
|
||||
# ADD CPP /nologo /MD /W3 /GR /GX /O2 /I "../../../include" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "JAS_WIN_MSVC_BUILD" /YX /FD /c
|
||||
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "NDEBUG"
|
||||
# ADD RSC /l 0x809 /d "NDEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
|
||||
# ADD LINK32 osg.lib osgDB.lib libjasper.lib /nologo /dll /pdb:none /machine:I386 /nodefaultlib:"MSVCRT" /out:"../../../bin/osgdb_jp2.dll" /libpath:"../../../lib"
|
||||
# SUBTRACT LINK32 /nodefaultlib
|
||||
|
||||
!ELSEIF "$(CFG)" == "osgPlugin jp2 - Win32 Debug"
|
||||
|
||||
# PROP BASE Use_MFC 0
|
||||
# PROP BASE Use_Debug_Libraries 1
|
||||
# PROP BASE Output_Dir "Debug"
|
||||
# PROP BASE Intermediate_Dir "Debug"
|
||||
# PROP BASE Target_Dir ""
|
||||
# PROP Use_MFC 0
|
||||
# PROP Use_Debug_Libraries 1
|
||||
# PROP Output_Dir "../../../lib"
|
||||
# PROP Intermediate_Dir "Debug"
|
||||
# PROP Ignore_Export_Lib 0
|
||||
# PROP Target_Dir ""
|
||||
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /YX /FD /GZ /c
|
||||
# ADD CPP /nologo /MDd /W3 /Gm /vmg /vd0 /GR /GX /Zi /Od /I "../../../include" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "WIN32" /D "_DEBUG" /D "JAS_WIN_MSVC_BUILD" /FR /YX /FD /GZ /c
|
||||
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
|
||||
# ADD BASE RSC /l 0x809 /d "_DEBUG"
|
||||
# ADD RSC /l 0x809 /d "_DEBUG"
|
||||
BSC32=bscmake.exe
|
||||
# ADD BASE BSC32 /nologo
|
||||
# ADD BSC32 /nologo
|
||||
LINK32=link.exe
|
||||
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
|
||||
# ADD LINK32 osgd.lib osgDBd.lib libjasperd.lib /nologo /dll /debug /machine:I386 /nodefaultlib:"MSVCRTD" /out:"../../../bin/osgdb_jp2d.dll" /pdbtype:sept /libpath:"../../../lib"
|
||||
# SUBTRACT LINK32 /nodefaultlib
|
||||
|
||||
!ENDIF
|
||||
|
||||
# Begin Target
|
||||
|
||||
# Name "osgPlugin jp2 - Win32 Release"
|
||||
# Name "osgPlugin jp2 - Win32 Debug"
|
||||
# Begin Group "Source Files"
|
||||
|
||||
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
# Begin Source File
|
||||
|
||||
SOURCE=..\..\..\src\osgPlugins\jp2\ReaderWriterJP2.cpp
|
||||
# End Source File
|
||||
# End Group
|
||||
# Begin Group "Header Files"
|
||||
|
||||
# PROP Default_Filter "h;hpp;hxx;hm;inl"
|
||||
# End Group
|
||||
# Begin Group "Resource Files"
|
||||
|
||||
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
|
||||
# End Group
|
||||
# End Target
|
||||
# End Project
|
||||
13
src/osgPlugins/jp2/GNUmakefile
Normal file
13
src/osgPlugins/jp2/GNUmakefile
Normal file
@@ -0,0 +1,13 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
ReaderWriterJP2.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS)-ljasper $(OTHER_LIBS)
|
||||
|
||||
TARGET_BASENAME = jp2
|
||||
include $(TOPDIR)/Make/cygwin_plugin_def
|
||||
PLUGIN = $(PLUGIN_PREFIX)$(TARGET_BASENAME).$(PLUGIN_EXT)
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
317
src/osgPlugins/jp2/ReaderWriterJP2.cpp
Normal file
317
src/osgPlugins/jp2/ReaderWriterJP2.cpp
Normal file
@@ -0,0 +1,317 @@
|
||||
#include <osg/Image>
|
||||
#include <osg/Notify>
|
||||
|
||||
#include <osg/Geode>
|
||||
|
||||
#include <osg/GL>
|
||||
|
||||
#include <osgDB/FileNameUtils>
|
||||
#include <osgDB/Registry>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include <jasper/jasper.h>
|
||||
}
|
||||
|
||||
#ifndef SEEK_SET
|
||||
# define SEEK_SET 0
|
||||
#endif
|
||||
|
||||
using 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 = 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:
|
||||
virtual const char* className() { return "RGB Image Reader/Writer"; }
|
||||
|
||||
virtual bool acceptsExtension(const std::string& extension)
|
||||
{
|
||||
return osgDB::equalCaseInsensitive(extension,"jp2") ||
|
||||
osgDB::equalCaseInsensitive(extension,"jpc");
|
||||
}
|
||||
|
||||
virtual ReadResult readImage(const std::string& fileName, const osgDB::ReaderWriter::Options* options)
|
||||
{
|
||||
std::string ext = osgDB::getFileExtension(fileName);
|
||||
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
|
||||
|
||||
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);
|
||||
|
||||
notify(INFO) << "image read ok "<<s<<" "<<t<< std::endl;
|
||||
return image;
|
||||
|
||||
}
|
||||
|
||||
virtual WriteResult writeImage(const osg::Image &img,const std::string& fileName, const osgDB::ReaderWriter::Options* options)
|
||||
{
|
||||
std::string ext = osgDB::getFileExtension(fileName);
|
||||
if (!acceptsExtension(ext)) return WriteResult::FILE_NOT_HANDLED;
|
||||
|
||||
jas_init();
|
||||
|
||||
jas_image_cmptparm_t cmptparms[4];
|
||||
jas_image_cmptparm_t *cmptparm;
|
||||
|
||||
int internalFormat = osg::Image::computeNumComponents(img.getPixelFormat());
|
||||
|
||||
jas_stream_t* mem = jas_stream_memopen((char*)img.data(), internalFormat*img.s()*img.t());
|
||||
|
||||
/* Create an image of the correct size. */
|
||||
jas_image_t* jimage;
|
||||
int i;
|
||||
for (i = 0, cmptparm = cmptparms; i < internalFormat; ++i, ++cmptparm) {
|
||||
cmptparm->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_GENGRAY);
|
||||
jas_image_setcmpttype(jimage, 0, JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
|
||||
}
|
||||
else if(internalFormat == 2)
|
||||
{
|
||||
return WriteResult::ERROR_IN_WRITING_FILE;
|
||||
}
|
||||
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_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);
|
||||
|
||||
jas_stream_close(out);
|
||||
jas_image_destroy(jimage);
|
||||
jas_image_clearfmts();
|
||||
|
||||
return WriteResult::FILE_SAVED;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// now register with Registry to instantiate the above
|
||||
// reader/writer.
|
||||
osgDB::RegisterReaderWriterProxy<ReaderWriterJP2> g_readerWriter_JP2_Proxy;
|
||||
Reference in New Issue
Block a user