Created
February 25, 2020 01:34
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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