From c56a1b8c31c9471565b950e3fd9afbdca80575e6 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 5 Oct 2001 19:55:58 +0000 Subject: [PATCH] Added bmp plugin files, bmp plguin written by Geoff Michel --- VisualStudio/osgPlugins/bmp/bmp.dsp | 112 +++++++++++ src/osgPlugins/bmp/Makefile | 18 ++ src/osgPlugins/bmp/ReaderWriterBMP.cpp | 261 +++++++++++++++++++++++++ 3 files changed, 391 insertions(+) create mode 100644 VisualStudio/osgPlugins/bmp/bmp.dsp create mode 100644 src/osgPlugins/bmp/Makefile create mode 100644 src/osgPlugins/bmp/ReaderWriterBMP.cpp diff --git a/VisualStudio/osgPlugins/bmp/bmp.dsp b/VisualStudio/osgPlugins/bmp/bmp.dsp new file mode 100644 index 000000000..7ba20762e --- /dev/null +++ b/VisualStudio/osgPlugins/bmp/bmp.dsp @@ -0,0 +1,112 @@ +# Microsoft Developer Studio Project File - Name="bmp" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102 + +CFG=bmp - Win32 Debug +!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 "bmp.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 "bmp.mak" CFG="bmp - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "bmp - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE "bmp - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +F90=df.exe +MTL=midl.exe +RSC=rc.exe + +!IF "$(CFG)" == "bmp - 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 "Release" +# PROP Intermediate_Dir "Release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE F90 /compile_only /dll /nologo /warn:nofileopt +# ADD F90 /compile_only /dll /nologo /warn:nofileopt +# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BMP_EXPORTS" /YX /FD /c +# ADD CPP /nologo /MT /W3 /GR /GX /O2 /I "../../../include" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BMP_EXPORTS" /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 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 /out:"../../../bin/osgdb_bmp.dll" + +!ELSEIF "$(CFG)" == "bmp - 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 "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE F90 /check:bounds /compile_only /debug:full /dll /nologo /traceback /warn:argument_checking /warn:nofileopt +# ADD F90 /check:bounds /compile_only /debug:full /dll /nologo /traceback /warn:argument_checking /warn:nofileopt +# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BMP_EXPORTS" /YX /FD /GZ /c +# ADD CPP /nologo /MTd /W3 /Gm /GR /GX /ZI /Od /I "../../../include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "BMP_EXPORTS" /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 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 /nodefaultlib:"LIBC" /out:"../../../bin/osgdb_bmpd.dll" /pdbtype:sept /libpath:"../../../lib" + +!ENDIF + +# Begin Target + +# Name "bmp - Win32 Release" +# Name "bmp - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat;f90;for;f;fpp" +# Begin Source File + +SOURCE=..\..\..\src\osgPlugins\bmp\ReaderWriterBMP.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl;fi;fd" +# 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 diff --git a/src/osgPlugins/bmp/Makefile b/src/osgPlugins/bmp/Makefile new file mode 100644 index 000000000..7a28e79c7 --- /dev/null +++ b/src/osgPlugins/bmp/Makefile @@ -0,0 +1,18 @@ +#!smake +include ../../../Make/makedefs + +C++FILES = \ + ReaderWriterBMP.cpp\ + +LIB = ../../../lib/osgPlugins/osgdb_bmp.so + +TARGET_LOADER_FILES = osgPlugins/osgdb_bmp.so + +LIBS = +C++FLAGS += -I. -I../../../include +LDFLAGS += -L../../../lib + +include ../../../Make/makerules + + + diff --git a/src/osgPlugins/bmp/ReaderWriterBMP.cpp b/src/osgPlugins/bmp/ReaderWriterBMP.cpp new file mode 100644 index 000000000..9cd5f6550 --- /dev/null +++ b/src/osgPlugins/bmp/ReaderWriterBMP.cpp @@ -0,0 +1,261 @@ +#include +#include +#include +#include + +#include + +/**************************************************************************** + * + * Follows is code written by GWM and translated to fit with the OSG Ethos. + * + * + * Ported into the OSG as a plugin, Geoff Michel October 2001. + * For patches, bugs and new features + * please send them direct to the OSG dev team. + * + **********************************************************************/ + +#include +#include +#include + +#define ERROR_NO_ERROR 0 +#define ERROR_READING_HEADER 1 +#define ERROR_READING_PALETTE 2 +#define ERROR_MEMORY 3 +#define ERROR_READ_ERROR 4 +#define ERROR_NO_FILE 5 + +static int bmperror = ERROR_NO_ERROR; + +// BMP format bits - at start of file is 512 bytes of pure garbage +enum ftype {MB=19778}; // magic number identifies a bmp file +enum ncol { BW=1, IA, RGB, RGBA}; + +struct bmpheader { + short FileType; //always MB + short siz[2]; + short Reserved1, Reserved2; //reserved for future purposes + short offset[2]; //offset to image in bytes +}; +struct BMPInfo { + long size; //size of BMPinfo in bytes + long width; //width of the image in pixels + long height; // height of the image in pixels + short planes; //:word: number of planes (always 1) + short Colorbits; //word: number of bits used to describe color in each pixel + long compression; //compression used + long ImageSize; //image size in bytes + long XpixPerMeter; //pixels per meter in X + long YpixPerMeter; //pixels per meter in Y + long ColorUsed; //number of the color used żżż??? + long Important; //number of "important" colors +}; + +int +bmp_error(char *buffer, int bufferlen) +{ + switch (bmperror) + { + case ERROR_READING_HEADER: + strncpy(buffer, "BMP loader: Error reading header", bufferlen); + break; + case ERROR_READING_PALETTE: + strncpy(buffer, "BMP loader: Error reading palette", bufferlen); + break; + case ERROR_MEMORY: + strncpy(buffer, "BMP loader: Out of memory error", bufferlen); + break; + case ERROR_READ_ERROR: + strncpy(buffer, "BMP loader: Read error", bufferlen); + break; + } + return bmperror; +} + +/* byte order workarounds *sigh* */ +void swapbyte(long *i) +{ + char *vv=(char *)i; + char tmp=vv[0]; + vv[0]=vv[3]; + vv[3]=tmp; + tmp=vv[1]; + vv[1]=vv[2]; + vv[2]=tmp; +} +void swapbyte(unsigned long *i) +{ + char *vv=(char *)i; + char tmp=vv[0]; + vv[0]=vv[3]; + vv[3]=tmp; + tmp=vv[1]; + vv[1]=vv[2]; + vv[2]=tmp; +} +void swapbyte(float *i) +{ + char *vv=(char *)i; + char tmp=vv[0]; + vv[0]=vv[3]; + vv[3]=tmp; + tmp=vv[1]; + vv[1]=vv[2]; + vv[2]=tmp; +} +void swapbyte(unsigned short *i) +{ + char *vv=(char *)i; + char tmp=vv[0]; + vv[0]=vv[1]; + vv[1]=tmp; +} +void swapbyte(short *i) +{ + char *vv=(char *)i; + char tmp=vv[0]; + vv[0]=vv[1]; + vv[1]=tmp; +} + +unsigned char * +bmp_load(const char *filename, +int *width_ret, +int *height_ret, +int *numComponents_ret) +{ // the main area of changes from the pic format loader. + // reads filename, and returns the buffer + // bmp is very very simple format + // even Master Gates could have invented it. + // It is extremely expensive on disk space - every RGB pixel uses 3 bytes plus a header! + // BMP - sponsored by Seagate. + // unsigned char palette[256][3]; + unsigned char * buffer; + + bmperror = ERROR_NO_FILE; + + FILE *fp = fopen(filename, "rb"); + if (!fp) return NULL; + + int ncolours; + int ncomp=0; + bool swap=false; // dont need to swap bytes + + struct bmpheader hd; + struct BMPInfo inf; + bmperror = ERROR_NO_ERROR; + fread((char *)&hd, sizeof(bmpheader), 1, fp); + fread((char *)&inf, sizeof(BMPInfo), 1, fp); + if (hd.FileType != MB) { + swapbyte(&(hd.FileType)); + swap=true; + if (hd.FileType != MB) { + bmperror=ERROR_READING_HEADER; + return NULL; + } + } + if (hd.FileType == MB) { + if (swap) { // inverse the field of the header which need swapping + swapbyte(&hd.siz[0]); + swapbyte(&hd.siz[1]); + swapbyte(&inf.Colorbits); + swapbyte(&inf.width); + swapbyte(&inf.height); + } + long size = hd.siz[1]*65536+hd.siz[0]; + size -= sizeof(bmpheader)+sizeof(BMPInfo); + ncolours=inf.Colorbits/8; + switch (ncolours) { + case 1: + ncomp = BW; + break; + case 2: + ncomp = IA; + break; + case 3: + ncomp = RGB; + break; + case 4: + ncomp = RGBA; + break; + } + buffer = (unsigned char *)malloc( ncomp*inf.width*inf.height); + osg::notify(osg::NOTICE)<<"BMP file: "<2) { // yes bill, colours are usually BGR aren't they + for(int j=0; jsetFileName(fileName.c_str()); + pOsgImage->setImage(s,t,r, + internalFormat, + pixelFormat, + dataType, + imageData); + + return pOsgImage; + + } +}; + +// now register with Registry to instantiate the above +// reader/writer. +osgDB::RegisterReaderWriterProxy g_readerWriter_BMP_Proxy;