Skip to content

Commit

Permalink
Add progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
chriscasa95 committed Apr 25, 2022
1 parent 84f3a09 commit 24083c8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pn_imager:
.PHONY: windows
windows: pn_imager.exe
pn_imager.exe:
$(MINGW) -o $@ $^ ${INC}
$(MINGW) ${CFLAGS} -o $@ $^ ${INC}

# both
.PHONY: all
Expand Down
32 changes: 19 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
#include <string>
#include "ppm.h"
#include "PerlinNoise.h"
#include "progressbar.h"

using namespace std;

#define VERSION "v0.15"

int main(int argc, char *argv[])
{
string VERSION = "v0.1";
string filename = "perlin_noise";

// Define the size of the image
Expand Down Expand Up @@ -50,47 +52,47 @@ int main(int argc, char *argv[])
cout << "perlin noise imager " << VERSION << endl;
return 0;
}
if (input.compare("-o") == 0 || input.compare("--output") == 0)
else if (input.compare("-o") == 0 || input.compare("--output") == 0)
{
filename = argv[count + 1];
}
if (input.compare("-f") == 0 || input.compare("--factor") == 0)
else if (input.compare("-f") == 0 || input.compare("--factor") == 0)
{
factor = stoi(argv[count + 1]);
}
if (input.compare("-w") == 0 || input.compare("--width") == 0)
else if (input.compare("-w") == 0 || input.compare("--width") == 0)
{
width = stoi(argv[count + 1]);
}
if (input.compare("-h") == 0 || input.compare("--height") == 0)
else if (input.compare("-h") == 0 || input.compare("--height") == 0)
{
height = stoi(argv[count + 1]);
}
if (input.compare("-z") == 0 || input.compare("--zoom") == 0)
else if (input.compare("-z") == 0 || input.compare("--zoom") == 0)
{
zoom = stoi(argv[count + 1]);
}
if (input.compare("-c") == 0 || input.compare("--contour") == 0)
else if (input.compare("-c") == 0 || input.compare("--contour") == 0)
{
contour_lines = stoi(argv[count + 1]);
}
if (input.compare("-s") == 0 || input.compare("--seed") == 0)
else if (input.compare("-s") == 0 || input.compare("--seed") == 0)
{
seed = stof(argv[count + 1]);
}
if (input.compare("-n") == 0 || input.compare("--normalize") == 0)
else if (input.compare("-n") == 0 || input.compare("--normalize") == 0)
{
normalize = !normalize;
}
if (input.compare("-r") == 0 || input.compare("--red") == 0)
else if (input.compare("-r") == 0 || input.compare("--red") == 0)
{
red = stoi(argv[count + 1]);
}
if (input.compare("-g") == 0 || input.compare("--green") == 0)
else if (input.compare("-g") == 0 || input.compare("--green") == 0)
{
green = stoi(argv[count + 1]);
}
if (input.compare("-b") == 0 || input.compare("--blue") == 0)
else if (input.compare("-b") == 0 || input.compare("--blue") == 0)
{
blue = stoi(argv[count + 1]);
}
Expand All @@ -111,6 +113,7 @@ int main(int argc, char *argv[])
PerlinNoise pn;

unsigned int kk = 0;
unsigned long total_pixel_count = width * height;
// Visit every pixel of the image and assign a color generated with Perlin noise
for (unsigned int i = 0; i < height; ++i)
{ // y
Expand All @@ -135,12 +138,15 @@ int main(int argc, char *argv[])
image.b[kk] = floor(blue * n); // 50
kk++;
}

if (kk % 100000 == 0)
printProgress(0.5 * (double)kk / (double)total_pixel_count);
}

// Save the image in a binary PPM file
image.write(filename + ".ppm");

cout << "Crated perlin noise image with:\n"
cout << "\nCrated perlin noise image with:\n"
<< endl
<< "Image size in pixel: \t" << width << " x " << height << endl
<< "Color weighting: \tred[" << red << "/255]\tgreen[" << green << "/255]\tblue[" << blue << "/255]" << endl
Expand Down
5 changes: 5 additions & 0 deletions src/ppm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <exception>

#include "ppm.h"
#include "progressbar.h"

// init with default values

Expand Down Expand Up @@ -139,7 +140,11 @@ void ppm::write(const std::string &fname)
inp.write(&aux, 1);
aux = (char)b[i];
inp.write(&aux, 1);

if (i % 1000000 == 0)
printProgress(0.5 + 0.5 * (double)i / (double)size);
}
printProgress(1);
}
else
{
Expand Down
15 changes: 15 additions & 0 deletions src/progressbar.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <stdio.h>

#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
#define GRN "\x1B[32m"
#define RESET "\x1B[0m"

void printProgress(double percentage)
{
int val = (int)(percentage * 100);
int lpad = (int)(percentage * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf("\r%3d%% [" GRN "%.*s%*s" RESET "]", val, lpad, PBSTR, rpad, "");
fflush(stdout);
}
6 changes: 6 additions & 0 deletions src/progressbar.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef PROGRESSBAR_H
#define PROGRESSBAR_H

void printProgress(double percentage);

#endif

0 comments on commit 24083c8

Please sign in to comment.