2.8 branch: PNM plugin improvements from Eric Sokolowsky: backport of r12220 from trunk; and "fixes numerous bugs in reading 8-bit and 16-bit images, including loading the images upside-down". The latter is pending commit to trunk.

This commit is contained in:
Paul MARTZ
2011-05-24 14:48:13 +00:00
parent ef322aba61
commit 3477e813d0

View File

@@ -14,173 +14,129 @@
#include <stdlib.h>
#include <string.h>
#include <sstream>
using namespace osg;
template <class T>
unsigned char* read_bitmap_ascii(FILE* fp, int width, int height)
unsigned char* read_bitmap_ascii(std::istream& fin, int width, int height)
{
T* data = new T[width*height];
T* dst = data;
T* end = data + width*height;
while(dst < end)
int x = 0;
T* dst = end - width;
int value = 0;
while(dst >= data)
{
T value = 0;
// read in characters looking for '0's and '1's, these
// values map to 255 and 0. Any other characters
// are silently ignored.
while(1)
fin >> value;
if (!fin.good())
{
int ch = fgetc(fp);
if (feof(fp) || ferror(fp))
{
delete [] data;
return NULL;
}
if (ch == '0')
{
value = 255;
break;
}
else if (ch == '1')
{
value = 0;
break;
}
delete [] data;
return NULL;
}
// place value in the image
*(dst++) = value;
// place value in the image. 0 is white and anything else is black.
*(dst++) = value ? 0 : 255;
// At end of each row, jump back two rows
++x;
if (x == width)
{
x = 0;
dst -= 2*width;
}
}
return reinterpret_cast<unsigned char*>(data);
}
template <class T>
unsigned char* read_grayscale_ascii(FILE* fp, int width, int height)
unsigned char* read_grayscale_ascii(std::istream& fin, int width, int height, int max_value)
{
T* data = new T[width*height];
T* dst = data;
T* end = data + width*height;
while(dst < end)
int x = 0;
T* dst = end - width;
int value = 0;
float max = (sizeof(T) == 1) ? 255.0 : 65535.0;
while(dst >= data)
{
int ch;
T value = 0;
// read and discard any whitespace
// until a digit is reached
do
fin >> value;
if (!fin.good())
{
ch = fgetc(fp);
if (feof(fp) || ferror(fp))
{
delete [] data;
return NULL;
}
delete [] data;
return NULL;
}
while(!isdigit(ch));
// continue reading digits and incrementally
// construct the integer value
do
{
value = 10*value + (ch - '0');
ch = fgetc(fp);
if (feof(fp) || ferror(fp))
{
delete [] data;
return NULL;
}
}
while(isdigit(ch));
// place value in the image
*(dst++) = value;
*(dst++) = T(float(value)/float(max_value)*max);
// At end of each row, jump back two rows
++x;
if (x == width)
{
x = 0;
dst -= 2*width;
}
}
return reinterpret_cast<unsigned char*>(data);
}
template <class T>
unsigned char* read_color_ascii(FILE* fp, int width, int height)
unsigned char* read_color_ascii(std::istream& fin, int width, int height, int max_value)
{
T* data = new T[3*width*height];
T* dst = data;
T* end = data + 3*width*height;
osg::notify(osg::NOTICE)<<"Width = "<<width<<std::endl;
osg::notify(osg::NOTICE)<<"Height = "<<height<<std::endl;
unsigned int numRead = 0;
int x = 0;
T* dst = end - 3*width;
int value = 0;
float max = (sizeof(T) == 1) ? 255.0 : 65535.0;
while(dst < end)
while(dst >= data)
{
int ch;
T value = 0;
// read and discard any whitespace
// until a digit is reached
do
fin >> value;
if (!fin.good())
{
ch = fgetc(fp);
if (feof(fp) || ferror(fp))
{
osg::notify(osg::NOTICE)<<"Error 1 "<<numRead<<std::endl;
delete [] data;
return NULL;
}
delete [] data;
return NULL;
}
while(!isdigit(ch));
// continue reading digits and incrementally
// construct the integer value
do
{
value = 10*value + (ch - '0');
ch = fgetc(fp);
if (feof(fp) || ferror(fp))
{
osg::notify(osg::NOTICE)<<"Error 2"<<std::endl;
delete [] data;
return NULL;
}
}
while(isdigit(ch));
++numRead;
// place value in the image
*(dst++) = value;
}
*(dst++) = T(float(value)/float(max_value)*max);
osg::notify(osg::NOTICE)<<"Completed"<<std::endl;
// At end of the row, jump back two rows
++x;
if (x == width*3)
{
x = 0;
dst -= 6*width;
}
}
return reinterpret_cast<unsigned char*>(data);
}
template <class T>
unsigned char* read_bitmap_binary(FILE* fp, int width, int height)
unsigned char* read_bitmap_binary(std::istream& fin, int width, int height)
{
T* data = new T[width*height];
for(int y = 0; y < height; y++)
for(int y = height-1; y >= 0; --y)
{
T* dst = data + (y+0)*width;
T* end = data + (y+1)*width;
while(dst < end)
{
unsigned char b = fgetc(fp);
if (feof(fp) || ferror(fp))
unsigned char b = (unsigned char) fin.get();
if (!fin.good())
{
fclose(fp);
delete [] data;
return NULL;
}
@@ -198,48 +154,58 @@ template <class T>
}
template <class T>
unsigned char* read_grayscale_binary(FILE* fp, int width, int height)
unsigned char* read_grayscale_binary(std::istream& fin, int width, int height)
{
T* data = new T[width*height];
if (fread(data, sizeof(T)*width*height, 1, fp) != 1)
for(int y = height-1; y >= 0; --y)
{
fclose(fp);
delete [] data;
return NULL;
T* dst = data + y*width;
fin.read((char*)dst, sizeof(T)*width);
if (!fin.good())
{
delete [] data;
return NULL;
}
}
// if the machine is little endian swap the bytes around
if (sizeof(T) > 1 && getCpuByteOrder() == osg::LittleEndian)
if (sizeof(T) == 2 && getCpuByteOrder() == osg::LittleEndian)
{
for(int i = 0; i < width*height; i++)
unsigned char *bs = reinterpret_cast<unsigned char *>(data);
unsigned char *end = bs + sizeof(T)*width*height;
for (; bs < end; bs += 2)
{
unsigned char* bs = (unsigned char*)(&data[i]);
std::swap(bs[0], bs[1]);
}
}
return reinterpret_cast<unsigned char*>(data);
return reinterpret_cast<unsigned char *>(data);
}
template <class T>
unsigned char* read_color_binary(FILE* fp, int width, int height)
unsigned char* read_color_binary(std::istream& fin, int width, int height)
{
T* data = new T[3*width*height];
if (fread(data, 3*sizeof(T)*width*height, 1, fp) != 1)
for(int y = height-1; y >= 0; --y)
{
fclose(fp);
delete [] data;
return NULL;
T* dst = data + y*3*width;
fin.read((char*)dst, sizeof(T)*3*width);
if (!fin.good())
{
delete [] data;
return NULL;
}
}
// if the machine is little endian swap the bytes around
if (sizeof(T) > 1 && getCpuByteOrder() == osg::LittleEndian)
if (sizeof(T) == 2 && getCpuByteOrder() == osg::LittleEndian)
{
for(int i = 0; i < 3*width*height; i++)
unsigned char *bs = reinterpret_cast<unsigned char *>(data);
unsigned char *end = bs + sizeof(T)*3*width*height;
for (; bs < end; bs+=2)
{
unsigned char* bs = (unsigned char*)(&data[i]);
std::swap(bs[0], bs[1]);
}
}
@@ -260,32 +226,23 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
virtual const char* className() const { return "PNM Image Reader/Writer"; }
virtual ReadResult readImage(const std::string& file, const osgDB::ReaderWriter::Options* options) const
virtual ReadResult readImage(std::istream& fin, const osgDB::ReaderWriter::Options* options=NULL) 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;
FILE *fp = NULL;
char line[300];
int ppmtype = 0; /* P1, P2, etc. */
int width = 0;
int height = 0;
int max_value = 0;
// Open file.
fp = osgDB::fopen(fileName.c_str(), "rb");
// Read header items.
std::string line;
int row;
for (row = 1; row <= 3; row++)
{
if ( fgets(line, 300, fp) == NULL)
break;
getline(fin, line);
if (!fin.good())
return ReadResult::ERROR_IN_READING_FILE;
char *cp = line;
const char *cp = line.c_str();
while (*cp && isspace(*cp))
cp++;
if (! *cp || *cp == '#')
@@ -303,12 +260,10 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
}
else if (row == 2)
{
// Get the image size.
width = atoi(line);
char *cp = line + strspn(line, "\t \n\r");
cp += strspn(cp, "0123456789");
cp += strspn(line, "\t \n\r");
height = atoi(cp);
std::istringstream istr(line);
istr >> width;
istr >> height;
// pbm files don't have row 3
if (ppmtype == 1 || ppmtype == 4)
@@ -320,7 +275,8 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
else if (row == 3)
{
// Get the maximum value
max_value = atoi(line);
std::istringstream istr(line);
istr >> max_value;
}
}
@@ -329,7 +285,6 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
max_value <= 0 || max_value > 65535 ||
ppmtype < 1 || ppmtype > 6)
{
fclose(fp);
return ReadResult::ERROR_IN_READING_FILE;
}
@@ -342,29 +297,24 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
dataType = GL_UNSIGNED_SHORT;
switch(ppmtype)
{
case 1: // bitmap ascii
pixelFormat = GL_LUMINANCE;
data = read_bitmap_ascii<unsigned short>(fp, width, height);
break;
case 2: // grayscale ascii
pixelFormat = GL_LUMINANCE;
data = read_grayscale_ascii<unsigned short>(fp, width, height);
data = read_grayscale_ascii<unsigned short>(fin, width, height, max_value);
break;
case 3: // color ascii
pixelFormat = GL_RGB;
data = read_color_ascii<unsigned short>(fp, width, height);
break;
case 4: // bitmap binary
pixelFormat = GL_LUMINANCE;
data = read_bitmap_binary<unsigned short>(fp, width, height);
data = read_color_ascii<unsigned short>(fin, width, height, max_value);
break;
case 5: // grayscale binary
pixelFormat = GL_LUMINANCE;
data = read_grayscale_binary<unsigned short>(fp, width, height);
data = read_grayscale_binary<unsigned short>(fin, width, height);
break;
case 6: // color binary
pixelFormat = GL_RGB;
data = read_color_binary<unsigned short>(fp, width, height);
data = read_color_binary<unsigned short>(fin, width, height);
break;
default:
return ReadResult::ERROR_IN_READING_FILE;
break;
}
}
@@ -375,44 +325,41 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
{
case 1: // bitmap ascii
pixelFormat = GL_LUMINANCE;
data = read_bitmap_ascii<unsigned char>(fp, width, height);
data = read_bitmap_ascii<unsigned char>(fin, width, height);
break;
case 2: // grayscale ascii
pixelFormat = GL_LUMINANCE;
data = read_grayscale_ascii<unsigned char>(fp, width, height);
data = read_grayscale_ascii<unsigned char>(fin, width, height, max_value);
break;
case 3: // color ascii
pixelFormat = GL_RGB;
data = read_color_ascii<unsigned char>(fp, width, height);
data = read_color_ascii<unsigned char>(fin, width, height, max_value);
break;
case 4: // bitmap binary
pixelFormat = GL_LUMINANCE;
data = read_bitmap_binary<unsigned char>(fp, width, height);
data = read_bitmap_binary<unsigned char>(fin, width, height);
break;
case 5: // grayscale binary
pixelFormat = GL_LUMINANCE;
data = read_grayscale_binary<unsigned char>(fp, width, height);
data = read_grayscale_binary<unsigned char>(fin, width, height);
break;
case 6: // color binary
pixelFormat = GL_RGB;
data = read_color_binary<unsigned char>(fp, width, height);
data = read_color_binary<unsigned char>(fin, width, height);
break;
default:
return ReadResult::ERROR_IN_READING_FILE;
break;
}
}
if (data == NULL)
{
if (fp)
fclose(fp);
return ReadResult::FILE_NOT_HANDLED;
}
if (fp)
fclose(fp);
osg::Image* pOsgImage = new osg::Image();
pOsgImage->setFileName(fileName.c_str());
pOsgImage->setImage(width, height, 1,
pixelFormat,
pixelFormat,
@@ -428,6 +375,24 @@ class ReaderWriterPNM : public osgDB::ReaderWriter
return pOsgImage;
}
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;
std::ifstream fin(fileName.c_str());
if (!fin.good())
return ReadResult::ERROR_IN_READING_FILE;
ReadResult rr = readImage(fin, options);
fin.close();
if (rr.validImage()) rr.getImage()->setFileName(file);
return rr;
}
virtual WriteResult writeImage(const osg::Image& image,std::ostream& fout,const osgDB::ReaderWriter::Options* options) const
{
bool ascii = (options && options->getOptionString().find("ascii")!=std::string::npos);