forked from lucasb-eyer/libheatmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplest_libpng.cpp
134 lines (111 loc) · 4.95 KB
/
simplest_libpng.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* heatmap - High performance heatmap creation in C.
*
* The MIT License (MIT)
*
* Copyright (c) 2013 Lucas Beyer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <random>
#include <vector>
#include <iostream>
#include <string.h>
#include <errno.h>
#include <png.h>
#include "heatmap.h"
static unsigned int writepng(const std::string& filename, const unsigned char* data, size_t w, size_t h);
int main()
{
static const size_t w = 256, h = 512, npoints = 1000;
// Create the heatmap object with the given dimensions (in pixel).
heatmap_t* hm = heatmap_new(w, h);
// This creates two normal random distributions which will give us random points.
std::random_device rd;
std::mt19937 prng(rd());
std::normal_distribution<float> x_distr(0.5f*w, 0.5f/3.0f*w), y_distr(0.5f*h, 0.25f*h);
// Add a bunch of random points to the heatmap now.
for(unsigned i = 0 ; i < npoints ; ++i) {
heatmap_add_point(hm, x_distr(prng), y_distr(prng));
}
// This creates an image out of the heatmap.
// `image` now contains the image data in 32-bit RGBA.
std::vector<unsigned char> image(w*h*4);
heatmap_render_default_to(hm, &image[0]);
// Now that we've got a finished heatmap picture, we don't need the map anymore.
heatmap_free(hm);
// Finally, we use the libpng reference library to save it as an image.
return writepng("heatmap.png", &image[0], w, h);
}
static unsigned int writepng(const std::string& filename, const unsigned char* data, size_t w, size_t h)
{
// NULLS are user error/warning functions.
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if(!png_ptr) {
std::cerr << "Error initializing libpng write struct." << std::endl;
return 2;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
std::cerr << "Error initializing libpng info struct." << std::endl;
png_destroy_write_struct(&png_ptr, (png_infopp)nullptr);
return 3;
}
if(setjmp(png_jmpbuf(png_ptr))) {
std::cerr << "Error in setjmp!?" << std::endl;
png_destroy_write_struct(&png_ptr, &info_ptr);
return 4;
}
FILE *fp = fopen(filename.c_str(), "wb");
if(!fp) {
std::cerr << "Error writing " << filename << ": " << strerror(errno) << std::endl;
png_destroy_write_struct(&png_ptr, &info_ptr);
return 5;
}
png_init_io(png_ptr, fp);
// Could be used for progress report of some sort.
//png_set_write_status_fn(png_ptr, write_row_callback);
// turn on or off filtering, and/or choose specific filters.
// You can use either a single PNG_FILTER_VALUE_NAME or the logical OR
// of one or more PNG_FILTER_NAME masks.
png_set_filter(png_ptr, 0,
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE
// | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB
// | PNG_FILTER_UP | PNG_FILTER_VALUE_UP
// | PNG_FILTER_AVE | PNG_FILTER_VALUE_AVE
// | PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH
// | PNG_ALL_FILTERS
);
// set the zlib compression level.
// 1 = fast but not much compression, 9 = slow but much compression.
png_set_compression_level(png_ptr, 1);
static const int bit_depth = 8;
static const int color_type = PNG_COLOR_TYPE_RGB_ALPHA;
static const int interlace_type = PNG_INTERLACE_ADAM7; // or PNG_INTERLACE_NONE
png_set_IHDR(png_ptr, info_ptr, w, h, bit_depth, color_type, interlace_type, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_bytep* row_pointers = (png_byte**)png_malloc(png_ptr, h*sizeof(png_bytep));
for(size_t y = 0 ; y < h ; ++y) {
row_pointers[y] = const_cast<png_bytep>(data + y*w*4);
}
png_set_rows(png_ptr, info_ptr, row_pointers);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, nullptr);
// Cleanup
png_free(png_ptr, row_pointers);
fclose(fp);
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
}