Files
OpenSceneGraph/src/osgPlugins/pic/ReaderWriterPIC.cpp
Robert Osfield 6402710910 Merged changes to svn/trunk that address cppcheck reported issues:
[examples/osgphotoalbum/PhotoArchive.cpp:56]: (error) Memory leak: fileIndentifier
[examples/osgphotoalbum/PhotoArchive.cpp:257]: (error) Deallocating a deallocated pointer: newData
[examples/osgphotoalbum/PhotoArchive.cpp:318]: (error) Deallocating a deallocated pointer: newData
[src/osg/ImageUtils.cpp:116]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:307]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:312]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:367]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:399]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:400]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:482]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:483]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:484]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:519]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/ImageUtils.cpp:536]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:71]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:74]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:77]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:82]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:102]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:107]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:599]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:600]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:601]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:602]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:603]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:604]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:605]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osg/OcclusionQueryNode.cpp:606]: (portability) Extra qualification 'osg::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:134]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:135]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:136]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:137]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:139]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:177]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:178]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:195]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:198]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:203]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:205]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/ExternalFileWriter.cpp:253]: (portability) Extra qualification 'osgDB::' unnecessary and considered an error by many compilers.
[src/osgDB/InputStream.cpp:553]: (error) Memory leak: data
[src/osgDB/OutputStream.cpp:393]: (error) Memory leak: data
[src/osgPlugins/Inventor/ConvertToInventor.cpp:656]: (error) Mismatching allocation and deallocation: tmpArray
[src/osgPlugins/Inventor/ReaderWriterIV.cpp:237]: (error) Common realloc mistake: 'buf' nulled but not freed upon failure
[src/osgPlugins/OpenFlight/expGeometryRecords.cpp:167]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers.
[src/osgPlugins/OpenFlight/expGeometryRecords.cpp:373]: (portability) Extra qualification 'flt::' unnecessary and considered an error by many compilers.
[src/osgPlugins/cfg/CameraConfig.cpp:635]: (error) Unusual pointer arithmetic
[src/osgPlugins/freetype/FreeTypeLibrary.cpp:122]: (error) Memory leak: buffer
[src/osgPlugins/geo/ReaderWriterGEO.cpp:210]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 211
[src/osgPlugins/geo/ReaderWriterGEO.cpp:227]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 228
[src/osgPlugins/geo/ReaderWriterGEO.cpp:903]: (error) Possible null pointer dereference: gfd - otherwise it is redundant to check if gfd is null at line 904
[src/osgPlugins/geo/osgGeoNodes.h:180]: (error) Memory leak: geoHeaderGeo::intVars
[src/osgPlugins/geo/osgGeoNodes.h:181]: (error) Memory leak: geoHeaderGeo::useVars
[src/osgPlugins/geo/osgGeoNodes.h:182]: (error) Memory leak: geoHeaderGeo::extVars
[src/osgPlugins/md2/ReaderWriterMD2.cpp:180]: (error) Memory leak: mapbase
[src/osgPlugins/md2/ReaderWriterMD2.cpp:166]: (error) Resource leak: file_fd
[src/osgPlugins/pic/ReaderWriterPIC.cpp:152]: (error) Mismatching allocation and deallocation: tmpbuf
[src/osgPlugins/pic/ReaderWriterPIC.cpp:153]: (error) Mismatching allocation and deallocation: buffer
[src/osgPlugins/ply/plyfile.cpp:843]: (error) Memory leak: plyfile
[src/osgPlugins/pvr/ReaderWriterPVR.cpp:179]: (error) Memory leak: imageData
[src/osgPlugins/shp/ESRIShapeParser.cpp:29]: (error) Resource leak: fd
[src/osgPlugins/shp/XBaseParser.cpp:96]: (error) Resource leak: fd
[src/osgPlugins/zip/unzip.cpp:3158]: (error) Possible null pointer dereference: s - otherwise it is redundant to check if s is null at line 3159
[src/osgPlugins/zip/unzip.cpp:4155]: (error) Dangerous usage of 'rd' (strncpy doesn't always 0-terminate it)
[src/osgShadow/MinimalCullBoundsShadowMap.cpp:334]: (error) Possible null pointer dereference: rl - otherwise it is redundant to check if rl is null at line 331
[src/osgViewer/ScreenCaptureHandler.cpp:617]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 611
[src/osgViewer/ScreenCaptureHandler.cpp:632]: (error) Possible null pointer dereference: camera - otherwise it is redundant to check if camera is null at line 626
[src/osgVolume/Locator.cpp:209]: (error) Dangerous iterator usage after erase()-method.
[src/osgVolume/RayTracedTechnique.cpp:274]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259
[src/osgVolume/RayTracedTechnique.cpp:275]: (error) Possible null pointer dereference: imageLayer - otherwise it is redundant to check if imageLayer is null at line 259
[src/osgWrappers/serializers/osg/ShaderBinary.cpp:28]: (error) Mismatching allocation and deallocation: data
2011-06-20 19:33:47 +00:00

254 lines
6.6 KiB
C++

#include <osg/Image>
#include <osg/Notify>
#include <osg/Geode>
#include <osg/GL>
#include <osgDB/Registry>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
/****************************************************************************
*
* Follows is code extracted from the simage library. Original Authors:
*
* Systems in Motion,
* <URL:http://www.sim.no>
*
* Peder Blekken <pederb@sim.no>
* Morten Eriksen <mortene@sim.no>
* Marius Bugge Monsen <mariusbu@sim.no>
*
* The original COPYING notice
*
* All files in this library are public domain, except simage_rgb.cpp which is
* Copyright (c) Mark J Kilgard <mjk@nvidia.com>. I will contact Mark
* very soon to hear if this source also can become public domain.
*
* Please send patches for bugs and new features to: <pederb@sim.no>.
*
* Peder Blekken
*
*
* Ported into the OSG as a plugin, Robert Osfield Decemeber 2000.
* Note, reference above to license of simage_rgb is not relevent to the OSG
* as the OSG does not use it. Also for patches, bugs and new features
* please send them direct to the OSG dev team rather than address above.
*
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define ERROR_NO_ERROR 0
#define ERROR_READING_HEADER 1
#define ERROR_READING_PALETTE 2
#define ERROR_MEMORY 3
#define ERROR_READ_ERROR 4
static int picerror = ERROR_NO_ERROR;
int
simage_pic_error(char *buffer, int bufferlen)
{
switch (picerror)
{
case ERROR_READING_HEADER:
strncpy(buffer, "PIC loader: Error reading header", bufferlen);
break;
case ERROR_READING_PALETTE:
strncpy(buffer, "PIC loader: Error reading palette", bufferlen);
break;
case ERROR_MEMORY:
strncpy(buffer, "PIC loader: Out of memory error", bufferlen);
break;
case ERROR_READ_ERROR:
strncpy(buffer, "PIC loader: Read error", bufferlen);
break;
}
return picerror;
}
/* byte order workaround *sigh* */
static int
readint16(FILE *fp, int * res)
{
unsigned char tmp = 0;
unsigned int tmp2;
if (fread(&tmp, 1, 1, fp) != 1) return 0;
*res = tmp;
if (fread(&tmp, 1, 1, fp) != 1) return 0;
tmp2 = tmp;
tmp2 <<= 8;
*res |= tmp2;
return 1;
}
int
simage_pic_identify(const char *, const unsigned char *header, int headerlen)
{
static unsigned char piccmp[] = {0x19, 0x91};
if (headerlen < 2) return 0;
if (memcmp((const void*)header,
(const void*)piccmp, 2) == 0) return 1;
return 0;
}
unsigned char *
simage_pic_load(const char *filename,
int *width_ret,
int *height_ret,
int *numComponents_ret)
{
int w, h, width, height, i, j, format;
unsigned char palette[256][3];
unsigned char * tmpbuf, * buffer, * ptr;
FILE *fp = osgDB::fopen(filename, "rb");
if (!fp) return NULL;
picerror = ERROR_NO_ERROR;
fseek(fp, 2, SEEK_SET);
if (!readint16(fp, &w))
{
picerror = ERROR_READING_HEADER;
fclose(fp);
return NULL;
}
fseek(fp, 4, SEEK_SET);
if (!readint16(fp, &h))
{
picerror = ERROR_READING_HEADER;
fclose(fp);
return NULL;
}
width = w;
height = h;
if (width <= 0 || height <= 0)
{
fclose(fp);
return NULL;
}
fseek(fp, 32, SEEK_SET);
if (fread(&palette, 3, 256, fp) != 256)
{
picerror = ERROR_READING_PALETTE;
}
tmpbuf = new unsigned char [width];
buffer = new unsigned char [3*width*height];
if (tmpbuf == NULL || buffer == NULL)
{
picerror = ERROR_MEMORY;
if (tmpbuf) delete [] tmpbuf;
if (buffer) delete [] buffer;
fclose(fp);
return NULL;
}
ptr = buffer;
for (i = 0; i < height; i++)
{
if (fread(tmpbuf, 1, width, fp) != (size_t) width)
{
picerror = ERROR_READ_ERROR;
fclose(fp);
if (tmpbuf) delete [] tmpbuf;
if (buffer) delete [] buffer;
buffer = NULL;
width = height = 0;
return NULL;
}
for (j = 0; j < width; j++)
{
int idx = tmpbuf[j];
*ptr++ = palette[idx][0];
*ptr++ = palette[idx][1];
*ptr++ = palette[idx][2];
}
}
format = 3;
fclose(fp);
*width_ret = width;
*height_ret = height;
*numComponents_ret = format;
if (tmpbuf) delete [] tmpbuf;
return buffer;
}
class ReaderWriterPIC : public osgDB::ReaderWriter
{
public:
ReaderWriterPIC()
{
supportsExtension("pic","PIC Image format");
}
virtual const char* className() const { return "PIC Image Reader"; }
virtual ReadResult readObject(const std::string& file, const osgDB::ReaderWriter::Options* options =NULL) const
{
return readImage(file, options);
}
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;
unsigned char *imageData = NULL;
int width_ret;
int height_ret;
int numComponents_ret;
imageData = simage_pic_load(fileName.c_str(),&width_ret,&height_ret,&numComponents_ret);
if (imageData==NULL) return ReadResult::FILE_NOT_HANDLED;
int s = width_ret;
int t = height_ret;
int r = 1;
int internalFormat = numComponents_ret;
unsigned int pixelFormat =
numComponents_ret == 1 ? GL_LUMINANCE :
numComponents_ret == 2 ? GL_LUMINANCE_ALPHA :
numComponents_ret == 3 ? GL_RGB :
numComponents_ret == 4 ? GL_RGBA : (GLenum)-1;
unsigned int dataType = GL_UNSIGNED_BYTE;
osg::Image* pOsgImage = new osg::Image;
pOsgImage->setFileName(fileName.c_str());
pOsgImage->setImage(s,t,r,
internalFormat,
pixelFormat,
dataType,
imageData,
osg::Image::USE_NEW_DELETE);
return pOsgImage;
}
};
// now register with Registry to instantiate the above
// reader/writer.
REGISTER_OSGPLUGIN(pic, ReaderWriterPIC)