首页 > 应用开发 > > ifstream >> std::string
下面一段代码,来自 http://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-mathematical-foundations
其中有 ifs >> header ,我用vs2017是编译不过的,但是相信作者肯定是编译过的,不知道作者用的什么环境,请有经验的同学指点一下
#include <fstream>
#include <cstdlib>
#include <cstdio>
int main(int argc, char **argv)
{
std::ifstream ifs;
ifs.open("./tex.pbm");
std::string header;
uint32_t w, h, l;
ifs >> header;
ifs >> w >> h >> l;
ifs.ignore();
unsigned char *pixels = new unsigned char[w * h * 3];
ifs.read((char*)pixels, w * h * 3);
// sample
int nsamples = 8;
srand48(13);
float avgr = 0, avgg = 0, avgb = 0;
float sumr = 0, sumg = 0, sumb = 0;
for (int n = 0; n < nsamples; ++ n) {
float x = drand48() * w;
float y = drand48() * h;
int i = ((int)(y) * w + (int)(x)) * 3;
sumr += pixels[i];
sumg += pixels[i + 1];
sumb += pixels[i + 2];
}
sumr /= nsamples;
sumg /= nsamples;
sumb /= nsamples;
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int i = (y * w + x) * 3;
avgr += pixels[i];
avgg += pixels[i + 1];
avgb += pixels[i + 2];
}
}
avgr /= w * h;
avgg /= w * h;
avgb /= w * h;
printf("Average %0.2f %0.2f %0.2f, Approximation %0.2f %0.2f %0.2f\n", avgr, avgg, avgb, sumr, sumg, sumb);
delete [] pixels;
return 0;
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4479/viewspace-3146080/ ,如需转载,请注明出处,否则将追究法律责任。