// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #include "image.h" #include #include #include #include namespace embree { static void skipSpacesAndComments(std::fstream& file) { while (true) { if (isspace(file.peek())) { file.ignore(); } else if (file.peek() == '#') { std::string line; std::getline(file,line); } else break; } } /*! read PPM file from disk */ Ref loadPPM(const FileName& fileName) { /* open file for reading */ std::fstream file; file.exceptions (std::fstream::failbit | std::fstream::badbit); file.open (fileName.c_str(), std::fstream::in | std::fstream::binary); /* read file type */ char cty[2]; file.read(cty,2); skipSpacesAndComments(file); std::string type(cty,2); /* read width, height, and maximum color value */ int width; file >> width; skipSpacesAndComments(file); int height; file >> height; skipSpacesAndComments(file); int maxColor; file >> maxColor; if (maxColor <= 0) THROW_RUNTIME_ERROR("Invalid maxColor value in PPM file"); float rcpMaxColor = 1.0f/float(maxColor); file.ignore(); // skip space or return /* create image and fill with data */ Ref img = new Image4uc(width,height,fileName); /* image in text format */ if (type == "P3") { int r, g, b; for (ssize_t y=0; y> r; file >> g; file >> b; img->set(x,y,Color4(float(r)*rcpMaxColor,float(g)*rcpMaxColor,float(b)*rcpMaxColor,1.0f)); } } } /* image in binary format 8 bit */ else if (type == "P6" && maxColor <= 255) { unsigned char rgb[3]; for (ssize_t y=0; yset(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f)); } } } /* image in binary format 16 bit */ else if (type == "P6" && maxColor <= 65535) { unsigned short rgb[3]; for (ssize_t y=0; yset(x,y,Color4(float(rgb[0])*rcpMaxColor,float(rgb[1])*rcpMaxColor,float(rgb[2])*rcpMaxColor,1.0f)); } } } /* invalid magic value */ else { THROW_RUNTIME_ERROR("Invalid magic value in PPM file"); } return img; } /*! store PPM file to disk */ void storePPM(const Ref& img, const FileName& fileName) { /* open file for writing */ std::fstream file; file.exceptions (std::fstream::failbit | std::fstream::badbit); file.open (fileName.c_str(), std::fstream::out | std::fstream::binary); /* write file header */ file << "P6" << std::endl; file << img->width << " " << img->height << std::endl; file << 255 << std::endl; /* write image */ for (size_t y=0; yheight; y++) { for (size_t x=0; xwidth; x++) { const Color4 c = img->get(x,y); file << (unsigned char)(clamp(c.r)*255.0f); file << (unsigned char)(clamp(c.g)*255.0f); file << (unsigned char)(clamp(c.b)*255.0f); } } } }