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. 18
      AwesomeAttractorND/main.cpp
  7. 4
      AwesomeAttractorND/nd_array.hpp

18
AwesomeAttractorND.xcodeproj/project.pbxproj

@ -151,9 +151,15 @@
); );
GCC_SYMBOLS_PRIVATE_EXTERN = NO; GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 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_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = 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; GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/local/include/; HEADER_SEARCH_PATHS = /usr/local/include/;
MACOSX_DEPLOYMENT_TARGET = 10.7; MACOSX_DEPLOYMENT_TARGET = 10.7;
@ -161,7 +167,6 @@
SDKROOT = macosx; SDKROOT = macosx;
WARNING_CFLAGS = ( WARNING_CFLAGS = (
"-Weffc++", "-Weffc++",
"-Wextra",
"-Wall", "-Wall",
); );
}; };
@ -177,16 +182,21 @@
GCC_C_LANGUAGE_STANDARD = gnu99; GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_ENABLE_OBJC_EXCEPTIONS = YES; GCC_ENABLE_OBJC_EXCEPTIONS = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0; 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_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = 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; GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = /usr/local/include/; HEADER_SEARCH_PATHS = /usr/local/include/;
MACOSX_DEPLOYMENT_TARGET = 10.7; MACOSX_DEPLOYMENT_TARGET = 10.7;
SDKROOT = macosx; SDKROOT = macosx;
WARNING_CFLAGS = ( WARNING_CFLAGS = (
"-Weffc++", "-Weffc++",
"-Wextra",
"-Wall", "-Wall",
); );
}; };

72
AwesomeAttractorND/Canvas.hpp

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

20
AwesomeAttractorND/Image.hpp

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

18
AwesomeAttractorND/Logging.hpp

@ -11,17 +11,6 @@
#pragma once #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 <iostream>
#include <string> #include <string>
#include <stack> #include <stack>
@ -31,6 +20,13 @@
#include <boost/date_time/posix_time/posix_time.hpp> #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: /* Usage:
When timing anything: When timing anything:
Before starting the task: start("with a string"); Before starting the task: start("with a string");

44
AwesomeAttractorND/Tonemapper.hpp

@ -10,33 +10,59 @@
#define AwesomeAttractorND_Tonemapper_hpp #define AwesomeAttractorND_Tonemapper_hpp
#include <algorithm> #include <algorithm>
#include <numeric>
#include <cmath> #include <cmath>
namespace Tonemappers { namespace Tonemappers {
struct Normalizer2D { struct Normalizer {
Normalizer2D() Normalizer()
: max(0) : max(0)
{} {}
template <typename C> template <typename C>
void analyse(C const & canvas){ 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> template <typename C, typename I>
void process(C const & canvas, I & image){ void process(C const & canvas, I & image){
for (size_t x = 0; x < canvas.template size<0>(); ++x) { for (typename C::const_iterator it = canvas.begin(); it != canvas.end(); ++it) {
for (size_t y = 0; y < canvas.template size<1>(); ++y) { const double grayscale = get_value(*it);
const double grayscale = (double) canvas[x][y] / (double) max; image << typename I::pixel(grayscale, grayscale, grayscale);
image << typename I::pixel(std::pow(grayscale, 0.2), std::pow(grayscale, 0.6), grayscale*1.5);
}
} }
} }
private: protected:
double get_value(unsigned int n){
return (double) n / (double) max;
}
unsigned int 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;
};
} }

18
AwesomeAttractorND/main.cpp

@ -8,30 +8,32 @@
#include <iostream> #include <iostream>
#include "nd_array.hpp" #include "Logging.hpp"
#include "Canvas.hpp" #include "Canvas.hpp"
#include "Tonemapper.hpp" #include "Tonemapper.hpp"
#include "Image.hpp" #include "Image.hpp"
#include "Logging.hpp"
typedef Canvas2D Canvas; typedef Canvas2D Canvas;
void fill(Canvas & 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){ for (unsigned int i = 0; i < it; ++i){
double x = 2.0 * (rand() / (double) RAND_MAX - 0.5); double x = 2.0 * (rand() / (double) RAND_MAX - 0.5);
double y = 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); canvas.plot(position);
} }
} }
void output(Canvas const & canvas);
void output(Canvas const & canvas){ void output(Canvas const & canvas){
ImageFormats::bmp::bitmap_stream<> image(canvas.size<0>(), canvas.size<1>(), "test.bmp"); Tonemappers::GammaCorrector tonemapper;
Tonemappers::Normalizer2D tonemapper;
tonemapper.analyse(canvas); tonemapper.analyse(canvas);
ImageFormats::bmp::bitmap_stream<> image(canvas.size<0>(), canvas.size<1>(), "test.bmp");
tonemapper.process(canvas, image); tonemapper.process(canvas, image);
} }
@ -48,4 +50,4 @@ int main (int, const char * []){
l.stop(); l.stop();
return 0; return 0;
} }

4
AwesomeAttractorND/nd_array.hpp

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