Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
Created February 25, 2020 01:34
Show Gist options
  • Save cosinekitty/260f5de6d098f3270c1e2adb01c2d6ce to your computer and use it in GitHub Desktop.
Save cosinekitty/260f5de6d098f3270c1e2adb01c2d6ce to your computer and use it in GitHub Desktop.
A reusable C++ class for generating an image and saving it as a PNG file via lodepng.
class VideoFrame
{
private:
int width;
int height;
std::vector<unsigned char> buffer;
public:
VideoFrame(int _width, int _height)
: width(_width)
, height(_height)
, buffer(4 * _width * _height, 255)
{}
void SetPixel(int x, int y, PixelColor color)
{
int index = 4 * (y*width + x);
buffer[index] = color.red;
buffer[index+1] = color.green;
buffer[index+2] = color.blue;
buffer[index+3] = color.alpha;
}
int SavePng(const char *outFileName)
{
unsigned error = lodepng::encode(outFileName, buffer, width, height);
if (error)
{
fprintf(stderr, "ERROR: lodepng::encode returned %u\n", error);
return 1;
}
return 0;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment