forked from Chlumsky/msdfgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save-png.cpp
30 lines (24 loc) · 1.11 KB
/
save-png.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "save-png.h"
#include "../core/arithmetics.hpp"
#include <lodepng.h>
namespace msdfgen {
bool savePng(const Bitmap<float> &bitmap, const char *filename) {
std::vector<unsigned char> pixels(bitmap.width()*bitmap.height());
std::vector<unsigned char>::iterator it = pixels.begin();
for (int y = bitmap.height()-1; y >= 0; --y)
for (int x = 0; x < bitmap.width(); ++x)
*it++ = clamp(int(bitmap(x, y)*0x100), 0xff);
return !lodepng::encode(filename, pixels, bitmap.width(), bitmap.height(), LCT_GREY);
}
bool savePng(const Bitmap<FloatRGB> &bitmap, const char *filename) {
std::vector<unsigned char> pixels(3*bitmap.width()*bitmap.height());
std::vector<unsigned char>::iterator it = pixels.begin();
for (int y = bitmap.height()-1; y >= 0; --y)
for (int x = 0; x < bitmap.width(); ++x) {
*it++ = clamp(int(bitmap(x, y).r*0x100), 0xff);
*it++ = clamp(int(bitmap(x, y).g*0x100), 0xff);
*it++ = clamp(int(bitmap(x, y).b*0x100), 0xff);
}
return !lodepng::encode(filename, pixels, bitmap.width(), bitmap.height(), LCT_RGB);
}
}