-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbitmap_noise.h
87 lines (82 loc) · 3.11 KB
/
bitmap_noise.h
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
// Copyright (C) 2020-2021 Sami Väisänen
// Copyright (C) 2020-2021 Ensisoft http://www.ensisoft.com
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#pragma once
#include "config.h"
#include "graphics/bitmap_generator.h"
namespace gfx
{
// Implement bitmap generation using a noise function.
// Each layer of noise is defined as a function with prime
// number seeds and frequency and amplitude. The bitmap
// is filled by sampling all the noise layers / functions
// and summing their signals.
class NoiseBitmapGenerator : public IBitmapGenerator
{
public:
struct Layer {
std::uint32_t prime0 = 7;
std::uint32_t prime1 = 743;
std::uint32_t prime2 = 7873;
float frequency = 1.0f;
float amplitude = 1.0f;
};
NoiseBitmapGenerator() = default;
NoiseBitmapGenerator(unsigned width, unsigned height)
: mWidth(width)
, mHeight(height)
{}
size_t GetNumLayers() const
{ return mLayers.size(); }
void AddLayer(const Layer& layer)
{ mLayers.push_back(layer); }
const Layer& GetLayer(size_t index) const
{ return mLayers[index]; }
Layer& GetLayer(size_t index)
{ return mLayers[index]; }
void DelLayer(size_t index)
{
auto it = mLayers.begin();
std::advance(it, index);
mLayers.erase(it);
}
bool HasLayers() const
{ return !mLayers.empty(); }
// Create random generator settings.
void Randomize(unsigned min_prime_index,
unsigned max_prime_index,
unsigned layers=1);
Function GetFunction() const override
{ return Function::Noise; }
std::unique_ptr<IBitmapGenerator> Clone() const override
{ return std::make_unique<NoiseBitmapGenerator>(*this); }
unsigned GetWidth() const override
{ return mWidth; }
unsigned GetHeight() const override
{ return mHeight; }
void SetHeight(unsigned height) override
{ mHeight = height; }
void SetWidth(unsigned width) override
{ mWidth = width; }
void IntoJson(data::Writer& data) const override;
bool FromJson(const data::Reader& data) override;
std::unique_ptr<IBitmap> Generate() const override;
std::size_t GetHash() const override;
private:
unsigned mWidth = 0;
unsigned mHeight = 0;
std::vector<Layer> mLayers;
};
} // namespace gfx