mandelbrot.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #include <boost/gil/image.hpp>
  9. #include <boost/gil/typedefs.hpp>
  10. #include <boost/gil/extension/io/jpeg.hpp>
  11. // Example for convolve_rows() and convolve_cols() in the numeric extension
  12. using namespace boost::gil;
  13. // Models a Unary Function
  14. template <typename P> // Models PixelValueConcept
  15. struct mandelbrot_fn
  16. {
  17. using point_t = boost::gil::point_t;
  18. using const_t = mandelbrot_fn;
  19. using value_type = P;
  20. using reference = value_type;
  21. using const_reference = value_type;
  22. using argument_type = point_t;
  23. using result_type = reference;
  24. static constexpr bool is_mutable =false;
  25. value_type _in_color,_out_color;
  26. point_t _img_size;
  27. static const int MAX_ITER=100; // max number of iterations
  28. mandelbrot_fn() {}
  29. mandelbrot_fn(const point_t& sz, const value_type& in_color, const value_type& out_color) : _in_color(in_color), _out_color(out_color), _img_size(sz) {}
  30. result_type operator()(const point_t& p) const {
  31. // normalize the coords to (-2..1, -1.5..1.5)
  32. // (actually make y -1.0..2 so it is asymmetric, so we can verify some view factory methods)
  33. double t=get_num_iter(point<double>(p.x/(double)_img_size.x*3-2, p.y/(double)_img_size.y*3-1.0f));//1.5f));
  34. t=pow(t,0.2);
  35. value_type ret;
  36. for (int k=0; k<num_channels<P>::value; ++k)
  37. ret[k]=(typename channel_type<P>::type)(_in_color[k]*t + _out_color[k]*(1-t));
  38. return ret;
  39. }
  40. private:
  41. double get_num_iter(const point<double>& p) const {
  42. point<double> Z(0,0);
  43. for (int i=0; i<MAX_ITER; ++i) {
  44. Z = point<double>(Z.x*Z.x - Z.y*Z.y + p.x, 2*Z.x*Z.y + p.y);
  45. if (Z.x*Z.x + Z.y*Z.y > 4)
  46. return i/(double)MAX_ITER;
  47. }
  48. return 0;
  49. }
  50. };
  51. int main()
  52. {
  53. using deref_t = mandelbrot_fn<rgb8_pixel_t>;
  54. using point_t = deref_t::point_t;
  55. using locator_t = virtual_2d_locator<deref_t,false>;
  56. using my_virt_view_t = image_view<locator_t>;
  57. boost::function_requires<PixelLocatorConcept<locator_t>>();
  58. gil_function_requires<StepIteratorConcept<locator_t::x_iterator>>();
  59. point_t dims(200,200);
  60. my_virt_view_t mandel(dims, locator_t(point_t(0,0), point_t(1,1), deref_t(dims, rgb8_pixel_t(255,0,255), rgb8_pixel_t(0,255,0))));
  61. write_view("out-mandelbrot.jpg",mandel, jpeg_tag{});
  62. return 0;
  63. }