-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsave_image.cpp
67 lines (53 loc) · 1.63 KB
/
save_image.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <tuple>
#include <iostream>
#include <FreeImage.h>
#include "window.hpp"
#include "save_image.hpp"
std::tuple<int, int, int> get_rgb_piecewise_linear(int n, int iter_max) {
int N = 256;
int N3 = N * N * N;
double t = (double)n / (double)iter_max;
n = (int)(t * (double)N3);
int b = n / (N * N);
int nn = n - b * N * N;
int r = nn / N;
int g = nn - r * N;
return std::tuple<int, int, int>(r, g, b);
}
std::tuple<int, int, int> get_rgb_smooth(int n, int iter_max) {
double t = (double)n / (double)iter_max;
int r = (int)(9 * (1 - t) * t * t * t * 255);
int g = (int)(15 * (1 - t) * (1 - t) * t * t * 255);
int b = (int)(8.5 * (1 - t) * (1 - t) * (1 - t) * t * 255);
return std::tuple<int, int, int>(r, g, b);
}
void plot(window<int> & scr, std::vector<int> & colors, int iter_max, const char * fname, bool smooth_color) {
#ifdef FREEIMAGE_LIB
FreeImage_Initialise();
#endif
unsigned int width = scr.width(), height = scr.height();
FIBITMAP * bitmap = FreeImage_Allocate(width, height, 32);
int k = 0;
std::tuple<int, int, int> rgb;
for (int i = scr.y_min(); i < scr.y_max(); ++i) {
for (int j = scr.x_min(); j < scr.x_max(); ++j) {
int n = colors[k];
if (!smooth_color)
rgb = get_rgb_piecewise_linear(n, iter_max);
else
rgb = get_rgb_smooth(n, iter_max);
RGBQUAD col;
col.rgbRed = std::get<0>(rgb);
col.rgbGreen = std::get<1>(rgb);
col.rgbBlue = std::get<2>(rgb);
col.rgbReserved = 255;
FreeImage_SetPixelColor(bitmap, j, i, &col);
++k;
}
}
FreeImage_Save(FIF_PNG, bitmap, fname, PNG_DEFAULT);
FreeImage_Unload(bitmap);
#ifdef FREEIMAGE_LIB
FreeImage_DeInitialise();
#endif
}