MCPcopy Create free account
hub / github.com/RenderKit/embree / loadPPM

Function loadPPM

tutorials/common/image/ppm.cpp:26–92  ·  view source on GitHub ↗

! read PPM file from disk */

Source from the content-addressed store, hash-verified

24
25 /*! read PPM file from disk */
26 Ref<Image> loadPPM(const FileName& fileName)
27 {
28 /* open file for reading */
29 std::fstream file;
30 file.exceptions (std::fstream::failbit | std::fstream::badbit);
31 file.open (fileName.c_str(), std::fstream::in | std::fstream::binary);
32
33 /* read file type */
34 char cty[2]; file.read(cty,2);
35 skipSpacesAndComments(file);
36 std::string type(cty,2);
37
38 /* read width, height, and maximum color value */
39 int width; file >> width;
40 skipSpacesAndComments(file);
41 int height; file >> height;
42 skipSpacesAndComments(file);
43 int maxColor; file >> maxColor;
44 if (maxColor <= 0) THROW_RUNTIME_ERROR("Invalid maxColor value in PPM file");
45 float rcpMaxColor = 1.0f/float(maxColor);
46 file.ignore(); // skip space or return
47
48 /* create image and fill with data */
49 Ref<Image> img = new Image4uc(width,height,fileName);
50
51 /* image in text format */
52 if (type == "P3")
53 {
54 int r, g, b;
55 for (ssize_t y=0; y<height; y++) {
56 for (ssize_t x=0; x<width; x++) {
57 file >> r; file >> g; file >> b;
58 img->set(x,y,Color4(float(r)*rcpMaxColor,float(g)*rcpMaxColor,float(b)*rcpMaxColor,1.0f));
59 }
60 }
61 }
62
63 /* image in binary format 8 bit */
64 else if (type == "P6" && maxColor <= 255)
65 {
66 unsigned char rgb[3];
67 for (ssize_t y=0; y<height; y++) {
68 for (ssize_t x=0; x<width; x++) {
69 file.read((char*)rgb,sizeof(rgb));
70 img->set(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f));
71 }
72 }
73 }
74
75 /* image in binary format 16 bit */
76 else if (type == "P6" && maxColor <= 65535)
77 {
78 unsigned short rgb[3];
79 for (ssize_t y=0; y<height; y++) {
80 for (ssize_t x=0; x<width; x++) {
81 file.read((char*)rgb,sizeof(rgb));
82 img->set(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f));
83 }

Callers 1

loadImageFromDiskFunction · 0.85

Calls 5

skipSpacesAndCommentsFunction · 0.70
Color4Class · 0.50
openMethod · 0.45
c_strMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected