noise-tool: extract map writing function

This commit is contained in:
Danny Robson 2015-06-04 14:40:47 +10:00
parent eda46a9e7c
commit 53917404b4

View File

@ -14,12 +14,68 @@
#include "extent.hpp"
#include "colour.hpp"
#include "netpbm.hpp"
#include "types.hpp"
template struct util::noise::fractal::fbm<float, util::noise::basis::perlin<float,util::lerp::cubic>>;
template struct util::noise::fractal::hmf<float, util::noise::basis::value<float,util::lerp::cubic>>;
template struct util::noise::fractal::rmf<float, util::noise::basis::constant<float>>;
template struct util::noise::fractal::hetero<float, util::noise::basis::worley<float,2>>;
// create a coloured map with this gradient (from libnoise tut3)
static const struct {
float scale;
util::colour3u value;
} GRADPOINT[] = {
{-1000000.f, { 0, 0, 128 } },
{ 0 / 32.f, { 0, 0, 128 } }, // deeps
{ 12 / 32.f, { 0, 0, 255 } }, // shallow
{ 16 / 32.f, { 0, 128, 255 } }, // shore
{ 17 / 32.f, { 240, 240, 64 } }, // sand
{ 18 / 32.f, { 32, 160, 0 } }, // grass
{ 22 / 32.f, { 224, 224, 0 } }, // dirt
{ 28 / 32.f, { 128, 128, 128 } }, // rock
{ 32 / 32.f, { 255, 255, 255 } }, // snow
{ 1000000.f, { 255, 255, 255 } },
};
void
write_map (const util::image::buffer<float> &map, boost::filesystem::path name)
{
std::unique_ptr<uint8_t[]> coloured (new uint8_t[map.w * map.h * 3]);
for (size_t i = 0; i < map.w * map.h; ++i) {
auto v = map[i] + 0/32.f;
auto c1 = std::upper_bound (std::begin (GRADPOINT),
std::end (GRADPOINT),
v,
[] (auto a, auto b) { return a < b.scale; });
auto c0 = c1-1;
CHECK_GE (v, c0->scale);
CHECK_LT (v, c1->scale);
float t = (v - c0->scale) / (c1->scale - c0->scale);
CHECK_LIMIT (t, 0, 1);
auto c = (
(1 - t) * c0->value.template cast<float> () +
( t) * c1->value.template cast<float> ()
).template cast<uint8_t> ();
coloured[i*3+0] = c[0];
coloured[i*3+1] = c[1];
coloured[i*3+2] = c[2];
}
name.replace_extension (".ppm");
util::ppm::write (coloured.get (), map.w, map.h, map.w*3, name);
// write the image to disk
auto grey = map.clone<uint8_t> ();
name.replace_extension (".pgm");
util::pgm::write (grey, name);
}
int
main (void)
{
@ -81,48 +137,8 @@ main (void)
std::transform (img.begin (), img.end (), img.begin (), [offset,div] (auto i) { return (i - offset) / div; });
// create a coloured map with this gradient
static const struct {
float scale;
util::colour3u value;
} GRADPOINT[] = {
{ 0 / 32.f, { 0, 0, 128 } },
{ 12 / 32.f, { 0, 0, 255 } },
{ 16 / 32.f, { 0, 128, 255 } },
{ 17 / 32.f, { 240, 240, 64 } },
{ 18 / 32.f, { 32, 160, 0 } },
{ 22 / 32.f, { 224, 224, 0 } },
{ 28 / 32.f, { 128, 128, 128 } },
{ 32 / 32.f, { 255, 255, 255 } },
{ 1000000.f, { 255, 255, 255 } },
};
std::unique_ptr<uint8_t[]> coloured (new uint8_t[size.area () * 3]);
for (size_t i = 0; i < size.area (); ++i) {
auto v = img.data ()[i] + 0/32.f;
auto c1 = std::upper_bound (std::begin (GRADPOINT),
std::end (GRADPOINT),
v,
[] (auto a, auto b) { return a < b.scale; });
auto c0 = c1-1;
// write the images to disk
write_map (img, "noise");
CHECK_GE (v, c0->scale);
CHECK_LT (v, c1->scale);
float t = (v - c0->scale) / (c1->scale - c0->scale);
CHECK_LIMIT (t, 0, 1);
auto c = (
(1 - t) * c0->value.template cast<float> () +
( t) * c1->value.template cast<float> ()
).template cast<uint8_t> ();
coloured[i*3+0] = c[0];
coloured[i*3+1] = c[1];
coloured[i*3+2] = c[2];
}
util::ppm::write (coloured.get (), size.w, size.h, size.w*3, "noise.ppm");
// write the image to disk
auto grey = img.clone<uint8_t> ();
util::pgm::write (grey, "noise.pgm");
}