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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*******************************************************
* Copyright (c) 2015-2019, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <forge.h>
#define USE_FORGE_CPU_COPY_HELPERS
#include <ComputeCopy.h>
#include <complex>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <iostream>
const unsigned IMGW = 256;
const unsigned IMGH = 256;
const unsigned DIMX = 1000;
const unsigned DIMY = 800;
const unsigned NBINS = 256;
using namespace std;
struct Bitmap {
unsigned char *ptr;
unsigned width;
unsigned height;
};
class PerlinNoise
{
private:
float base[IMGW][IMGH];
float perlin[IMGW][IMGH];
public:
PerlinNoise();
float noise(float u, float v);
};
Bitmap createBitmap(unsigned w, unsigned h);
void destroyBitmap(Bitmap& bmp);
void kernel(Bitmap& bmp);
void populateBins(Bitmap& bmp, int *hist_array, const unsigned nbins, float *hist_cols);
int main(void)
{
Bitmap bmp = createBitmap(IMGW, IMGH);
/*
* First Forge call should be a window creation call
* so that necessary OpenGL context is created for any
* other forge::* object to be created successfully
*/
forge::Window wnd(DIMX, DIMY, "Histogram Demo");
wnd.makeCurrent();
forge::Image img(IMGW, IMGH, FG_RGBA, forge::u8);
forge::Chart chart(FG_CHART_2D);
/* set x axis limits to maximum and minimum values of data
* and y axis limits to range [0, number of pixels ideally]
* but practically total number of pixels as y range will skew
* the histogram graph vertically. Therefore setting it to
* 25% of total number of pixels */
chart.setAxesLimits(0, 1, 0, IMGW*IMGH/(float)(NBINS/4.0));
/*
* Create histogram object specifying number of bins
*/
forge::Histogram hist = chart.histogram(NBINS, forge::s32);
/*
* Set histogram colors
*/
hist.setColor(FG_YELLOW);
GfxHandle* handles[3];
createGLBuffer(&handles[0], img.pixels(), FORGE_IMAGE_BUFFER);
createGLBuffer(&handles[1], hist.vertices(), FORGE_VERTEX_BUFFER);
createGLBuffer(&handles[2], hist.colors(), FORGE_VERTEX_BUFFER);
do {
/*
* generate image, and prepare data to pass into
* Histogram's underlying vertex buffer object
*/
kernel(bmp);
copyToGLBuffer(handles[0], (ComputeResourceHandle)bmp.ptr, img.size());
//forge::copy(img, (const void*)bmp.ptr);
/* copy your data into the vertex buffer object exposed by
* forge::Histogram class and then proceed to rendering.
* To help the users with copying the data from compute
* memory to display memory, Forge provides copy headers
* along with the library to help with this task
*/
std::vector<int> histArray(NBINS, 0);
std::vector<float> colArray(3*NBINS, 0.0f);
populateBins(bmp, histArray.data(), NBINS, colArray.data());
copyToGLBuffer(handles[1], (ComputeResourceHandle)histArray.data(), hist.verticesSize());
copyToGLBuffer(handles[2], (ComputeResourceHandle)colArray.data(), hist.colorsSize());
/*
* Split the window into grid regions
*/
//wnd.draw(2, 2, 0, img, "Dynamic Perlin Noise" );
//wnd.draw(2, 2, 1, img, "Dynamic Perlin Noise" );
//wnd.draw(2, 1, 1, chart, "Histogram of Noisy Image");
wnd.draw(2, 3, 0, img, "Dynamic Perlin Noise" );
wnd.draw(2, 3, 1, img, "Dynamic Perlin Noise" );
wnd.draw(2, 3, 2, img, "Dynamic Perlin Noise" );
wnd.draw(2, 2, 2, chart, "Histogram of Noisy Image");
wnd.draw(2, 2, 3, chart, "Histogram of Noisy Image");
wnd.swapBuffers();
} while(!wnd.close());
releaseGLBuffer(handles[0]);
releaseGLBuffer(handles[1]);
releaseGLBuffer(handles[2]);
return 0;
}
float interp(float x0, float x1, float alpha)
{
return x0 * (1 - alpha) + alpha * x1;
}
PerlinNoise::PerlinNoise()
{
std::srand(std::time(0));
for(unsigned i=0; i < IMGW; i++)
{
for(unsigned j=0; j < IMGH; j++)
{
base[i][j] = std::rand()/(float)(RAND_MAX);
perlin[i][j] = 0;
}
}
float persistence = 0.5f;
float amp = 1.0f;
float tamp = 0.0f;
for (int octave=6; octave>=0; --octave)
{
int period = 1 << octave;
float freq = 1.0f / period;
for(unsigned i=0; i < IMGW; i++)
{
int si0 = (i/period) * period;
int si1 = (si0 + period) % IMGW;
float hblend = (i - si0) * freq;
for(unsigned j=0; j < IMGH; j++)
{
int sj0 = (j/period) * period;
int sj1 = (sj0 + period) % IMGH;
float vblend = (j - sj0) * freq;
float top = interp(base[si0][sj0], base[si1][sj0], hblend);
float bot = interp(base[si0][sj1], base[si1][sj1], hblend);
perlin[i][j] += (amp * interp(top, bot, vblend));
}
}
tamp += amp;
amp *= persistence;
}
for(unsigned i=0; i < IMGW; i++)
for(unsigned j=0; j < IMGH; j++)
perlin[i][j] /= tamp;
}
float PerlinNoise::noise(float u, float v)
{
return perlin[(unsigned)(IMGW*u)][(unsigned)(IMGH*v)];
}
Bitmap createBitmap(unsigned w, unsigned h)
{
Bitmap retVal;
retVal.width = w;
retVal.height= h;
retVal.ptr = new unsigned char[4*w*h];
return retVal;
}
void destroyBitmap(Bitmap& bmp)
{
delete[] bmp.ptr;
}
void kernel(Bitmap& bmp)
{
PerlinNoise perlin;
for (unsigned y=0; y<bmp.height; ++y) {
for (unsigned x=0; x<bmp.width; ++x) {
int offset = x + y * bmp.width;
float u = x/(float)(bmp.width);
float v = y/(float)(bmp.height);
unsigned char noiseVal = 255 * perlin.noise(u, v);
bmp.ptr[offset*4 + 0] = noiseVal;
bmp.ptr[offset*4 + 1] = noiseVal;
bmp.ptr[offset*4 + 2] = noiseVal;
bmp.ptr[offset*4 + 3] = 255;
}
}
}
void populateBins(Bitmap& bmp, int *hist_array, const unsigned nbins, float *hist_cols)
{
for (unsigned y=0; y<bmp.height; ++y) {
for (unsigned x=0; x<bmp.width; ++x) {
int offset = x + y * bmp.width;
unsigned char noiseVal = bmp.ptr[offset*4];
unsigned idx = (int)((float)noiseVal/255.f * nbins);
hist_array[idx]++;
}
}
for (unsigned b=0; b<nbins; ++b) {
hist_cols[3*b+0] = std::rand()/(float)RAND_MAX;
hist_cols[3*b+1] = std::rand()/(float)RAND_MAX;
hist_cols[3*b+2] = std::rand()/(float)RAND_MAX;
}
}
|