saveFrame()
Output / ImageDescription
Saves a numbered image file. Use #### as a placeholder for the frame number.
Syntax
void saveFrame(std::string filename)
Parameters
| Name | Type | Description |
|---|---|---|
| filename | std::string | filename template, e.g. frame-####.png |
Returns
voidRelated
Under the Hood
From Processing.h:
void saveFrame(const std::string& filename="frame-####.png");
Under the Hood
From Processing.cpp:
void PApplet::saveFrame(const std::string& filename) {
std::string fn = filename;
size_t pos = fn.find("####");
if (pos != std::string::npos) {
char buf[16];
snprintf(buf, sizeof(buf), "%04d", frameCount);
fn.replace(pos, 4, buf);
}
int w = pixelWidth > 0 ? pixelWidth : logicalW;
int h = pixelHeight > 0 ? pixelHeight : logicalH;
std::vector<unsigned char> px(w * h * 3);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, px.data());
for (int y = 0; y < h / 2; y++)
for (int x = 0; x < w * 3; x++)
std::swap(px[y*w*3+x], px[(h-1-y)*w*3+x]);
std::string ext = fn.size() > 4 ? fn.substr(fn.size()-4) : "";
for (auto& c : ext) c = tolower(c);
int ok = 0;
if (ext == ".png") ok = stbi_write_png(fn.c_str(), w, h, 3, px.data(), w*3);
else if (ext == ".jpg" || ext == "jpeg") ok = stbi_write_jpg(fn.c_str(), w, h, 3, px.data(), 95);
else if (ext == ".bmp") ok = stbi_write_bmp(fn.c_str(), w, h, 3, px.data());
else if (ext == ".tga" || ext == ".tif" || ext == ".tiff")
ok = stbi_write_tga(fn.c_str(), w, h, 3, px.data());
else { fn += ".png"; ok = stbi_write_png(fn.c_str(), w, h, 3, px.data(), w*3); }
if (ok) std::cout << "Saved: " << fn << "\n";
else std::cout << "save: failed to write " << fn << "\n";
}
void PApplet::save(const std::string& fn){saveFrame(fn);