! read PFM file from disk */
| 23 | |
| 24 | /*! read PFM file from disk */ |
| 25 | Ref<Image> loadPFM(const FileName& fileName) |
| 26 | { |
| 27 | /* open file for reading */ |
| 28 | std::fstream file; |
| 29 | file.exceptions (std::fstream::failbit | std::fstream::badbit); |
| 30 | file.open (fileName.c_str(), std::fstream::in | std::fstream::binary); |
| 31 | |
| 32 | /* read file type */ |
| 33 | char cty[2]; file.read(cty,2); |
| 34 | skipSpacesAndComments(file); |
| 35 | std::string type(cty,2); |
| 36 | |
| 37 | /* read width, height, and maximum color value */ |
| 38 | int width; file >> width; |
| 39 | skipSpacesAndComments(file); |
| 40 | int height; file >> height; |
| 41 | skipSpacesAndComments(file); |
| 42 | float maxColor; file >> maxColor; |
| 43 | if (maxColor > 0) THROW_RUNTIME_ERROR("Big endian PFM files not supported"); |
| 44 | float rcpMaxColor = -1.0f/float(maxColor); |
| 45 | file.ignore(); // skip space or return |
| 46 | |
| 47 | /* create image and fill with data */ |
| 48 | Ref<Image> img = new Image4f(width,height,fileName); |
| 49 | |
| 50 | /* image in binary format 16 bit */ |
| 51 | if (type == "PF") |
| 52 | { |
| 53 | float rgb[3]; |
| 54 | for (ssize_t y=height-1; y>=0; y--) { |
| 55 | for (ssize_t x=0; x<width; x++) { |
| 56 | file.read((char*)rgb,sizeof(rgb)); |
| 57 | img->set(x,y,Color4(rgb[0]*rcpMaxColor,rgb[1]*rcpMaxColor,rgb[2]*rcpMaxColor,1.0f)); |
| 58 | } |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | /* invalid magic value */ |
| 63 | else { |
| 64 | THROW_RUNTIME_ERROR("Invalid magic value in PFM file"); |
| 65 | } |
| 66 | return img; |
| 67 | } |
| 68 | |
| 69 | /*! store PFM file to disk */ |
| 70 | void storePFM(const Ref<Image>& img, const FileName& fileName) |
no test coverage detected