diff --git a/src/osgPlugins/pnm/ReaderWriterPNM.cpp b/src/osgPlugins/pnm/ReaderWriterPNM.cpp index 6ac775689..5da6bb9f6 100644 --- a/src/osgPlugins/pnm/ReaderWriterPNM.cpp +++ b/src/osgPlugins/pnm/ReaderWriterPNM.cpp @@ -14,173 +14,129 @@ #include #include +#include + using namespace osg; + template - 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(data); } template - 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(data); } template - 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 = "<(data); } template - 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 } template - 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(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(data); + return reinterpret_cast(data); } template - 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(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(fp, width, height); - break; case 2: // grayscale ascii pixelFormat = GL_LUMINANCE; - data = read_grayscale_ascii(fp, width, height); + data = read_grayscale_ascii(fin, width, height, max_value); break; case 3: // color ascii pixelFormat = GL_RGB; - data = read_color_ascii(fp, width, height); - break; - case 4: // bitmap binary - pixelFormat = GL_LUMINANCE; - data = read_bitmap_binary(fp, width, height); + data = read_color_ascii(fin, width, height, max_value); break; case 5: // grayscale binary pixelFormat = GL_LUMINANCE; - data = read_grayscale_binary(fp, width, height); + data = read_grayscale_binary(fin, width, height); break; case 6: // color binary pixelFormat = GL_RGB; - data = read_color_binary(fp, width, height); + data = read_color_binary(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(fp, width, height); + data = read_bitmap_ascii(fin, width, height); break; case 2: // grayscale ascii pixelFormat = GL_LUMINANCE; - data = read_grayscale_ascii(fp, width, height); + data = read_grayscale_ascii(fin, width, height, max_value); break; case 3: // color ascii pixelFormat = GL_RGB; - data = read_color_ascii(fp, width, height); + data = read_color_ascii(fin, width, height, max_value); break; case 4: // bitmap binary pixelFormat = GL_LUMINANCE; - data = read_bitmap_binary(fp, width, height); + data = read_bitmap_binary(fin, width, height); break; case 5: // grayscale binary pixelFormat = GL_LUMINANCE; - data = read_grayscale_binary(fp, width, height); + data = read_grayscale_binary(fin, width, height); break; case 6: // color binary pixelFormat = GL_RGB; - data = read_color_binary(fp, width, height); + data = read_color_binary(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);