diff --git a/AwesomeAttractorND.xcodeproj/project.pbxproj b/AwesomeAttractorND.xcodeproj/project.pbxproj index d01081c..2d5b847 100644 --- a/AwesomeAttractorND.xcodeproj/project.pbxproj +++ b/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", ); }; diff --git a/AwesomeAttractorND/Canvas.hpp b/AwesomeAttractorND/Canvas.hpp index a003089..8fa16e8 100644 --- a/AwesomeAttractorND/Canvas.hpp +++ b/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 size() const { if ( N == 0 ) return storage.size(); @@ -87,15 +91,15 @@ public: struct iterator : public std::iterator { 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 { 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: diff --git a/AwesomeAttractorND/Image.hpp b/AwesomeAttractorND/Image.hpp index df1f545..248369d 100644 --- a/AwesomeAttractorND/Image.hpp +++ b/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(this), header_sz); } - unsigned int bytes(){ + uint32_t bytes(){ return width*height*bitspp/8; } }; template 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 + template 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; diff --git a/AwesomeAttractorND/Logging.hpp b/AwesomeAttractorND/Logging.hpp index 8383594..9a5f382 100644 --- a/AwesomeAttractorND/Logging.hpp +++ b/AwesomeAttractorND/Logging.hpp @@ -11,17 +11,6 @@ #pragma once -#include - -#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 #include #include @@ -31,6 +20,13 @@ #include +#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"); diff --git a/AwesomeAttractorND/Tonemapper.hpp b/AwesomeAttractorND/Tonemapper.hpp index b213ccb..5dcde94 100644 --- a/AwesomeAttractorND/Tonemapper.hpp +++ b/AwesomeAttractorND/Tonemapper.hpp @@ -10,33 +10,59 @@ #define AwesomeAttractorND_Tonemapper_hpp #include +#include #include namespace Tonemappers { - struct Normalizer2D { - Normalizer2D() + struct Normalizer { + Normalizer() : max(0) {} template void analyse(C const & canvas){ - max = *std::max_element(canvas.cbegin(), canvas.cend()); + max = *std::max_element(canvas.begin(), canvas.end()); } template 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 + 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 + 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; + }; } diff --git a/AwesomeAttractorND/main.cpp b/AwesomeAttractorND/main.cpp index 777bef2..3bdf43d 100644 --- a/AwesomeAttractorND/main.cpp +++ b/AwesomeAttractorND/main.cpp @@ -8,30 +8,32 @@ #include -#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); } @@ -48,4 +50,4 @@ int main (int, const char * []){ l.stop(); return 0; -} \ No newline at end of file +} diff --git a/AwesomeAttractorND/nd_array.hpp b/AwesomeAttractorND/nd_array.hpp index 202439c..54a3b09 100644 --- a/AwesomeAttractorND/nd_array.hpp +++ b/AwesomeAttractorND/nd_array.hpp @@ -21,7 +21,7 @@ #include #include #include -#include +#include "array.hpp" template class nd_array{ @@ -146,7 +146,7 @@ public: private: T * data; - std::tr1::array sizes; + std::array sizes; }; #endif