1 // Copyright 2009-2021 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 
4 #include "image.h"
5 
6 #include <iostream>
7 #include <cstring>
8 #include <cstdio>
9 
10 namespace embree
11 {
skipSpacesAndComments(std::fstream & file)12   static void skipSpacesAndComments(std::fstream& file)
13   {
14     while (true)
15     {
16       if  (isspace(file.peek())) {
17         file.ignore();
18       } else  if (file.peek() == '#') {
19         std::string line; std::getline(file,line);
20       } else break;
21     }
22   }
23 
24   /*! read PFM file from disk */
loadPFM(const FileName & fileName)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 */
storePFM(const Ref<Image> & img,const FileName & fileName)70   void storePFM(const Ref<Image>& img, const FileName& fileName)
71   {
72     /* open file for writing */
73     std::fstream file;
74     file.exceptions (std::fstream::failbit | std::fstream::badbit);
75     file.open (fileName.c_str(), std::fstream::out | std::fstream::binary);
76 
77     /* write file header */
78     file << "PF" << std::endl;
79     file << img->width << " " << img->height << std::endl;
80     file << -1.0f << std::endl;
81 
82     /* write image */
83     for (ssize_t y=img->height-1; y>=0; y--) {
84       for (ssize_t x=0; x<(ssize_t)img->width; x++) {
85         const Color4 c = img->get(x,y);
86         file.write((char*)&c,3*sizeof(float));
87       }
88     }
89   }
90 }
91