Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * @author Tat
- * c++ rewrite vivienne (WIP)
- */
- #include <iostream>
- #include <vector>
- #include <stdio.h>
- #include <stdlib.h>
- #include <png++/png.hpp>
- using namespace std;
- void SaveTerrFile(const int * image, int size, char * filename);
- class OlsenNoise2D
- {
- public:
- int * olsennoise(int x, int y, int width, int height);
- private:
- int hashrandom(std::vector<int> elements);
- long long hash(long long v);
- };
- int * OlsenNoise2D::olsennoise(int x, int y, int width, int height)
- {
- int maxiterations = 7;
- int cx, cy;
- int cxh, cyh;
- int cwidth, cheight;
- int xoff, yoff;
- int nwidth, nheight;
- int nx, ny;
- int nxh, nyh;
- int m=0;
- int n=0;
- int * field = NULL;
- for (int iteration = 0; iteration < maxiterations; iteration++)
- {
- nx = x;
- ny = y;
- nxh = x + width;
- nyh = y + width;
- n = maxiterations - iteration;
- for (int i = 1; i < n; i++)
- {
- nx = (nx / 2) - 1;
- ny = (ny / 2) - 1;
- nxh = 1 -(-nxh/2);
- nyh = 1 -(-nyh/2);
- }
- xoff = -2*((nx/2)) + nx + 1;
- yoff = -2*((ny/2)) + ny + 1;
- cx = (nx / 2) - 1;
- cy = (ny / 2) - 1;
- cxh = 1 -(-nxh/2);
- cyh = 1 -(-nyh/2);
- nwidth = nxh - nx;
- nheight = nyh - ny;
- cwidth = cxh - cx;
- cheight = cyh - cy;
- /// Field
- m=cwidth;
- n=cheight;
- if (field == NULL) field = new int[m*(n*m)];
- for (int j = 0; j < m; j++)
- {
- for (int k = 0; k < n; k++)
- {
- field[j+(k*m)] += (hashrandom({cx + j, cy + k, iteration}) & (1 << (7 - iteration)));
- }
- }
- /// Up sampled
- //m=field.length * 2;
- //n=field[0].length * 2;
- m=cwidth*2;
- n=cheight*2;
- SaveTerrFile(field, cheight, "rgbOlsena.png");
- int * upsampled = new int[m*(n*m)];
- for (int j = 0; j < m; j++)
- {
- for (int k = 0; k < n; k++)
- {
- upsampled[j+(k*m)] = field[(j / 2)+((k / 2)*cwidth)];
- }
- }
- field = upsampled;
- /// Blur field
- //int m=field.length - 2;
- //int n=field[0].length - 2;
- m=cwidth-2;
- n=cheight-2;
- int * blurfield = new int[m*(n*m)];
- for (int j = 0; j < m; j++)
- {
- for (int k = 0; k < n; k++)
- {
- for (int h = 0; h < 9; h++)
- {
- blurfield[j+(k*m)] += field[(j + (h % 3))+((k + (h / 3))*(cheight*2))];
- }
- blurfield[j+(k*m)] /= 9;
- }
- }
- field = blurfield;
- /// Trim field
- m=nwidth;
- n=nheight;
- int * trimfield = new int[m*(n*m)];
- for (int j = 0;j < m; j++)
- {
- for (int k = 0; k < n; k++)
- {
- trimfield[j+(k*m)] = field[(j + xoff)+((k + yoff)*(nheight-2))];
- }
- }
- field = trimfield;
- }
- return field;
- }
- int OlsenNoise2D::hashrandom(std::vector<int> elements)
- {
- long long hashcalc = 0;
- for (int i = 0; i < elements.size(); i++)
- {
- hashcalc ^= elements[i];
- hashcalc = hash(hashcalc);
- }
- return (int) hashcalc;
- };
- long long OlsenNoise2D::hash(long long v)
- {
- long hash = v;
- long h = hash;
- switch ((int) hash & 3)
- {
- case 3:
- hash += h;
- hash ^= hash << 32;
- hash ^= h << 36;
- hash += hash >> 22;
- break;
- case 2:
- hash += h;
- hash ^= hash << 22;
- hash += hash >> 34;
- break;
- case 1:
- hash += h;
- hash ^= hash << 20;
- hash += hash >> 2;
- }
- hash ^= hash << 6;
- hash += hash >> 10;
- hash ^= hash << 8;
- hash += hash >> 34;
- hash ^= hash << 50;
- hash += hash >> 12;
- return hash;
- };
- int main()
- {
- /// Test
- int ImageSize=2048;
- int * imageInput = new int[ImageSize*ImageSize];
- /// Image
- OlsenNoise2D testingolsen;
- imageInput=testingolsen.olsennoise(0,0,ImageSize,ImageSize);
- SaveTerrFile(imageInput, ImageSize, "rgbOlsen.png");
- return 1;
- }
- void SaveTerrFile(const int * image, int size, char * filename)
- {
- png::image< png::rgb_pixel > newimage(size, size);
- for (unsigned int y = 0; y < newimage.get_width(); ++y)
- {
- for (unsigned int x = 0; x < newimage.get_height(); ++x)
- {
- int col = int(image[x+(y*newimage.get_width())]);
- newimage[y][x] = png::rgb_pixel(col,col,col);
- // non-checking equivalent of image.set_pixel(x, y, ...);
- }
- }
- newimage.write(filename);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement