Browse Source

pretty nice now

master
Joshua Moerman 13 years ago
parent
commit
d62df77b06
  1. 18
      AwesomeAttractorND.xcodeproj/project.pbxproj
  2. 72
      AwesomeAttractorND/Canvas.hpp
  3. 20
      AwesomeAttractorND/Image.hpp
  4. 18
      AwesomeAttractorND/Logging.hpp
  5. 44
      AwesomeAttractorND/Tonemapper.hpp
  6. 16
      AwesomeAttractorND/main.cpp
  7. 4
      AwesomeAttractorND/nd_array.hpp

18
AwesomeAttractorND.xcodeproj/project.pbxproj

@ -151,9 +151,15 @@
);
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES;
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
GCC_WARN_PEDANTIC = YES;
GCC_WARN_SIGN_COMPARE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/local/include/;
MACOSX_DEPLOYMENT_TARGET = 10.7;
@ -161,7 +167,6 @@
SDKROOT = macosx;
WARNING_CFLAGS = (
"-Weffc++",
"-Wextra",
"-Wall",
);
};
@ -177,16 +182,21 @@
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = NO;
GCC_WARN_ABOUT_MISSING_FIELD_INITIALIZERS = YES;
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_HIDDEN_VIRTUAL_FUNCTIONS = YES;
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES;
GCC_WARN_PEDANTIC = YES;
GCC_WARN_SIGN_COMPARE = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/local/include/;
MACOSX_DEPLOYMENT_TARGET = 10.7;
SDKROOT = macosx;
WARNING_CFLAGS = (
"-Weffc++",
"-Wextra",
"-Wall",
);
};

72
AwesomeAttractorND/Canvas.hpp

@ -44,14 +44,18 @@ public:
const size_t width = size<0>();
const size_t height = size<1>();
const size_t x = 0.5*position[0]*width + width*.5;
const size_t y = 0.5*position[1]*width + height*.5;
const size_t c = 0.5*position[0]*width + width*.5;
const size_t r = 0.5*position[1]*width + height*.5;
if(x < width && y < height) {
storage[x][y]++;
if(c < width && r < height) {
storage[r][c]++;
}
}
size_t size() const {
return size<0>() * size<1>();
}
template <size_t N>
size_t size() const {
if ( N == 0 ) return storage.size();
@ -87,15 +91,15 @@ public:
struct iterator : public std::iterator<std::forward_iterator_tag, value_type> {
iterator(iterator const & rh)
: canvas(rh.canvas)
, x(rh.x)
, y(rh.y)
, c(rh.c)
, r(rh.r)
{}
iterator & operator++(){
++x;
if (x >= canvas->size<0>()) {
x = 0;
++y;
++c;
if (c >= canvas->size<0>()) {
c = 0;
++r;
}
return *this;
}
@ -107,48 +111,48 @@ public:
}
bool operator==(iterator const & rh) const {
return canvas == rh.canvas && x == rh.x && y == rh.y;
return canvas == rh.canvas && c == rh.c && r == rh.r;
}
bool operator!=(iterator const & rh) const {
return canvas != rh.canvas || x != rh.x || y != rh.y;
return canvas != rh.canvas || c != rh.c || r != rh.r;
}
value_type & operator*(){
return (*canvas)[x][y];
return (*canvas)[r][c];
}
value_type & operator->(){
return (*canvas)[x][y];
return (*canvas)[r][c];
}
private:
friend class Canvas2D;
iterator(Canvas2D * canvas, size_t x = 0, size_t y = 0)
iterator(Canvas2D * canvas, size_t c = 0, size_t r = 0)
: canvas(canvas)
, x(x)
, y(y)
, c(c)
, r(r)
{}
iterator();
Canvas2D * canvas;
size_t x;
size_t y;
size_t c;
size_t r;
};
struct const_iterator : public std::iterator<std::forward_iterator_tag, value_type const> {
const_iterator(const_iterator const & rh)
: canvas(rh.canvas)
, x(rh.x)
, y(rh.y)
, c(rh.c)
, r(rh.r)
{}
const_iterator & operator++(){
++x;
if (x >= canvas->size<0>()) {
x = 0;
++y;
++c;
if (c >= canvas->size<0>()) {
c = 0;
++r;
}
return *this;
}
@ -160,34 +164,34 @@ public:
}
bool operator==(const_iterator const & rh) const {
return canvas == rh.canvas && x == rh.x && y == rh.y;
return canvas == rh.canvas && c == rh.c && r == rh.r;
}
bool operator!=(const_iterator const & rh) const {
return canvas != rh.canvas || x != rh.x || y != rh.y;
return canvas != rh.canvas || c != rh.c || r != rh.r;
}
value_type const & operator*() const {
return (*canvas)[x][y];
return (*canvas)[r][c];
}
value_type const & operator->() const {
return (*canvas)[x][y];
return (*canvas)[r][c];
}
private:
friend class Canvas2D;
const_iterator(Canvas2D const * canvas, size_t x = 0, size_t y = 0)
const_iterator(Canvas2D const * canvas, size_t c = 0, size_t r = 0)
: canvas(canvas)
, x(x)
, y(y)
, c(c)
, r(r)
{}
const_iterator();
Canvas2D const * canvas;
size_t x;
size_t y;
size_t c;
size_t r;
};
private:

20
AwesomeAttractorND/Image.hpp

@ -69,7 +69,7 @@ namespace ImageFormats {
uint16_t nplanes;
uint16_t bitspp;
bitmapcoreheader(int width, int height, int bitspp = 24):
bitmapcoreheader(uint16_t width, uint16_t height, uint16_t bitspp = 24):
header_sz(sizeof(bitmapcoreheader)),
width(width),
height(height),
@ -80,15 +80,17 @@ namespace ImageFormats {
out.write(reinterpret_cast<const char*>(this), header_sz);
}
unsigned int bytes(){
uint32_t bytes(){
return width*height*bitspp/8;
}
};
template <typename P = pixel, typename DIBT = bitmapcoreheader>
struct bitmap {
bitmap(int width, int height)
: dib_header(width, height, sizeof(P)*8)
typedef P pixel;
bitmap(uint16_t width, uint16_t height)
: dib_header(width, height, sizeof(pixel)*8)
, header(dib_header)
, data(0) {}
@ -110,12 +112,12 @@ namespace ImageFormats {
P const * data;
};
template <typename DIBT = bitmapcoreheader>
template <typename P = pixel, typename DIBT = bitmapcoreheader>
struct bitmap_stream {
typedef pixel pixel;
typedef P pixel;
bitmap_stream(int width, int height, std::string filename)
: dib_header(width, height)
bitmap_stream(uint16_t width, uint16_t height, std::string filename)
: dib_header(width, height, sizeof(pixel)*8)
, header(dib_header)
, file(filename.c_str())
, x(0)
@ -134,7 +136,7 @@ namespace ImageFormats {
pixel p2(p);
p2.swapRB();
file.write((char const *)&p2, 3);
file.write((char const *)&p2, sizeof(pixel));
++x;
if (x >= dib_header.width){
x = 0;

18
AwesomeAttractorND/Logging.hpp

@ -11,17 +11,6 @@
#pragma once
#include <iostream>
#define INFORMATION __FILE__ << ":" << __LINE__ << "(" << __FUNCTION__ << ")"
#define COUT std::cout << INFORMATION << "\n"
#define CERR std::cerr << INFORMATION << "\n"
#define VCOUT if(verbose) COUT
#define VCERR if(verbose) CERR
#pragma once
#include <iostream>
#include <string>
#include <stack>
@ -31,6 +20,13 @@
#include <boost/date_time/posix_time/posix_time.hpp>
#define INFORMATION __FILE__ << ":" << __LINE__ << "(" << __FUNCTION__ << ")"
#define COUT std::cout << INFORMATION << "\n"
#define CERR std::cerr << INFORMATION << "\n"
#define VCOUT if(verbose) COUT
#define VCERR if(verbose) CERR
/* Usage:
When timing anything:
Before starting the task: start("with a string");

44
AwesomeAttractorND/Tonemapper.hpp

@ -10,34 +10,60 @@
#define AwesomeAttractorND_Tonemapper_hpp
#include <algorithm>
#include <numeric>
#include <cmath>
namespace Tonemappers {
struct Normalizer2D {
Normalizer2D()
struct Normalizer {
Normalizer()
: max(0)
{}
template <typename C>
void analyse(C const & canvas){
max = *std::max_element(canvas.cbegin(), canvas.cend());
max = *std::max_element(canvas.begin(), canvas.end());
}
template <typename C, typename I>
void process(C const & canvas, I & image){
for (size_t x = 0; x < canvas.template size<0>(); ++x) {
for (size_t y = 0; y < canvas.template size<1>(); ++y) {
const double grayscale = (double) canvas[x][y] / (double) max;
image << typename I::pixel(std::pow(grayscale, 0.2), std::pow(grayscale, 0.6), grayscale*1.5);
}
for (typename C::const_iterator it = canvas.begin(); it != canvas.end(); ++it) {
const double grayscale = get_value(*it);
image << typename I::pixel(grayscale, grayscale, grayscale);
}
}
private:
protected:
double get_value(unsigned int n){
return (double) n / (double) max;
}
unsigned int max;
};
struct GammaCorrector : public Normalizer {
template <typename C>
void analyse(C const & canvas){
Normalizer::analyse(canvas);
const unsigned int sum = std::accumulate(canvas.begin(), canvas.end(), 0);
const double average = sum / (double) canvas.size();
power = -2.5/std::log(average/max);
}
template <typename C, typename I>
void process(C const & canvas, I & image){
for (typename C::const_iterator it = canvas.begin(); it != canvas.end(); ++it) {
const double grayscale = get_value(*it);
image << typename I::pixel(grayscale, grayscale, grayscale);
}
}
protected:
double get_value(unsigned int n){
return std::pow(Normalizer::get_value(n), power);
}
double power;
};
}
#endif

16
AwesomeAttractorND/main.cpp

@ -8,30 +8,32 @@
#include <iostream>
#include "nd_array.hpp"
#include "Logging.hpp"
#include "Canvas.hpp"
#include "Tonemapper.hpp"
#include "Image.hpp"
#include "Logging.hpp"
typedef Canvas2D Canvas;
void fill(Canvas & canvas);
void fill(Canvas & canvas){
const unsigned int it = 100000000/2;
const unsigned int it = 100000000;
for (unsigned int i = 0; i < it; ++i){
double x = 2.0 * (rand() / (double) RAND_MAX - 0.5);
double y = 2.0 * (rand() / (double) RAND_MAX - 0.5);
double position[2] = {sin(x / (y*y - 1)), sin(y / (x*x - 1))};
double position[2] = {x*x*2.0 - 1.0, y};
canvas.plot(position);
}
}
void output(Canvas const & canvas);
void output(Canvas const & canvas){
ImageFormats::bmp::bitmap_stream<> image(canvas.size<0>(), canvas.size<1>(), "test.bmp");
Tonemappers::Normalizer2D tonemapper;
Tonemappers::GammaCorrector tonemapper;
tonemapper.analyse(canvas);
ImageFormats::bmp::bitmap_stream<> image(canvas.size<0>(), canvas.size<1>(), "test.bmp");
tonemapper.process(canvas, image);
}

4
AwesomeAttractorND/nd_array.hpp

@ -21,7 +21,7 @@
#include <stdexcept>
#include <numeric>
#include <iterator>
#include <tr1/array>
#include "array.hpp"
template <typename T, size_t dimension>
class nd_array{
@ -146,7 +146,7 @@ public:
private:
T * data;
std::tr1::array<size_type, dimension> sizes;
std::array<size_type, dimension> sizes;
};
#endif