// // Load a new image from an image file (PPM, JPEG // and PNG formats are supported). // Returns NULL on failure. // STImage::STImage(const std::string& filename) : mWidth(-1) , mHeight(-1) , mPixels(NULL) { // Determine the right routine based on the file's extension. // The format-specific subroutines are each implemented in // a different file. std::string ext = STGetExtension( filename ); if (ext.compare("PPM") == 0) { LoadPPM(filename); } else if (ext.compare("PNG") == 0) { LoadPNG(filename); } else if (ext.compare("JPG") == 0 || ext.compare("JPEG") == 0) { LoadJPG(filename); } else { fprintf(stderr, "STImage::STImage() - Unknown image file type \"%s\".\n", filename.c_str()); throw new std::runtime_error("Error creating STImage"); } }
/** * Loads an image off disk. This method does not do any real work, it * just calls a format specific method based on the image format (format * determined by file extension) */ STHDRImage::STHDRImage(const std::string& filename) { // Determine how to load the file from the file's extension // The ST library currently only supports hdr images of type PFM std::string ext = STGetExtension( filename ); if (ext.compare("PFM") == 0) LoadPFM(filename.c_str()); else { throw new std::runtime_error("HDR image file extension must be PFM."); } }
/** * Saves an image to disk. This method does not do any real work, it * just calls a format specific method based on the image format * (format determined by file extension) */ STStatus STHDRImage::Save(const std::string& filename) const { if (mPixels == NULL) return ST_ERROR; std::string ext = STGetExtension( filename ); if (ext.compare("PFM") == 0) return SavePFM(filename.c_str()); else { fprintf(stderr, "Unknown image file type.\n"); return ST_ERROR; } }
// // Read the triangle mesh from files. // bool STTriangleMesh::Read(const std::string& filename) { // Determine the right routine based on the file's extension. // The format-specific subroutines are each implemented in // a different file. std::string ext = STGetExtension( filename ); if (ext.compare("OBJ") == 0){ std::ifstream in( filename.c_str(), std::ios::in ); if( !in ){ std::cout << "cannot open file" << filename << std::endl; return false; } for(unsigned int i=0;i<mVertices.size();i++)delete mVertices[i]; mVertices.clear(); for(unsigned int i=0;i<mFaces.size();i++)delete mFaces[i]; mFaces.clear(); //std::string comments; //std::string token; char comments[256]; char token[128]; float x,y,z; float nx,ny,nz; float u,v; int v1,v2,v3; int t1,t2,t3; int n1,n2,n3; std::vector<STVector3> vertices; std::vector<STVector3> normals; std::vector<STPoint2> tex_coords; while(in>>token){ if(strcmp(token,"#")==0){ in.getline(comments,256); } else if(strcmp(token,"v")==0){ in>>x>>y>>z; mVertices.push_back(new STVertex(x,y,z)); } else if(strcmp(token,"vt")==0){
// // Save the image to a file (PPM, JPEG and PNG // formats are supported). // Returns a non-zero value on error. // STStatus STImage::Save(const std::string& filename) const { // Determine the right routine based on the file's extension. // The format-specific subroutines are each implemented in // a different file. std::string ext = STGetExtension( filename ); if (ext.compare("PPM") == 0 ) { return SavePPM(filename); } else if (ext.compare("PNG") == 0) { return SavePNG(filename); } else if (ext.compare("JPG") == 0) { return SaveJPG(filename); } else { fprintf(stderr, "STImage::Save() - Unknown image file type \"%s\".\n", filename.c_str()); assert(false); return ST_ERROR; } }