home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / include / k3d / k3dsdk / bitmap_source.h < prev    next >
C/C++ Source or Header  |  2008-08-04  |  4KB  |  119 lines

  1. #ifndef K3DSDK_BITMAP_SOURCE_H
  2. #define K3DSDK_BITMAP_SOURCE_H
  3.  
  4. // K-3D
  5. // Copyright (c) 1995-2008, Timothy M. Shead
  6. //
  7. // Contact: tshead@k-3d.com
  8. //
  9. // This program is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public
  20. // License along with this program; if not, write to the Free Software
  21. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22.  
  23. #include "bitmap.h"
  24. #include "data.h"
  25. #include "hints.h"
  26. #include "ibitmap_source.h"
  27. #include "ipipeline_profiler.h"
  28. #include "k3d-i18n-config.h"
  29. #include "pointer_demand_storage.h"
  30.  
  31. namespace k3d
  32. {
  33.  
  34. /// Boilerplate CRTP class for bitmap source objects that produce a k3d::bitmap* as output.
  35. template<typename derived_t>
  36. class bitmap_source :
  37.     public ibitmap_source
  38. {
  39. public:
  40.     iproperty& bitmap_source_output()
  41.     {
  42.         return m_output_bitmap;
  43.     }
  44.  
  45.     /// Returns a slot that should be connected to input properties to signal that the output bitmap has changed.
  46.     sigc::slot<void, ihint*> make_update_bitmap_slot()
  47.     {
  48.         return m_output_bitmap.make_slot();
  49.     }
  50.  
  51. protected:
  52.     bitmap_source() :
  53.         m_output_bitmap(
  54.             init_owner(owner())
  55.             + init_name("output_bitmap")
  56.             + init_label(_("Output Bitmap"))
  57.             + init_description(_("Output bitmap")))
  58.     {
  59.         m_output_bitmap.set_update_slot(sigc::mem_fun(*this, &bitmap_source<derived_t>::execute));
  60.     }
  61.  
  62.     /// Stores the output bitmap, which is created on-demand.
  63.     k3d_data(bitmap*, immutable_name, change_signal, no_undo, pointer_demand_storage, no_constraint, read_only_property, no_serialization) m_output_bitmap;
  64.  
  65. private:
  66.     inline derived_t& owner()
  67.     {
  68.         return *static_cast<derived_t*>(this);
  69.     }
  70.  
  71.     /// Called whenever the output bitmap has been modified and needs to be updated.
  72.     void execute(const std::vector<ihint*>& Hints, bitmap& Bitmap)
  73.     {
  74.         bool_t resize_bitmap = false;
  75.         bool_t assign_pixels = false;
  76.  
  77.         for(uint_t i = 0; i != Hints.size(); ++i)
  78.         {
  79.             // Input pixels changed, so all we have to do is reassign ours ...
  80.             if(dynamic_cast<hint::bitmap_pixels_changed*>(Hints[i]))
  81.             {
  82.                 assign_pixels = true;
  83.             }
  84.             // In every other case (bitmap_dimensions_changed, unknown hint, or no hint),
  85.             // we must assume the worst and recreate everything from scratch ...
  86.             else
  87.             {
  88.                 resize_bitmap = true;
  89.                 assign_pixels = true;
  90.                 break;
  91.             }
  92.         }
  93.  
  94.         if(resize_bitmap)
  95.         {
  96.             owner().document().pipeline_profiler().start_execution(owner(), "Resize Bitmap");
  97.             on_resize_bitmap(Bitmap);
  98.             owner().document().pipeline_profiler().finish_execution(owner(), "Resize Bitmap");
  99.         }
  100.  
  101.         if(assign_pixels)
  102.         {
  103.             owner().document().pipeline_profiler().start_execution(owner(), "Assign Pixels");
  104.             on_assign_pixels(Bitmap);
  105.             owner().document().pipeline_profiler().finish_execution(owner(), "Assign Pixels");
  106.         }
  107.     }
  108.  
  109.     /// Implement this in derived classes to set the size of the output bitmap.
  110.     virtual void on_resize_bitmap(bitmap& Output) = 0;
  111.     /// Implement this in derived classes to assign values to each output pixel.
  112.     virtual void on_assign_pixels(bitmap& Output) = 0;
  113. };
  114.  
  115. } // namespace k3d
  116.  
  117. #endif // !K3DSDK_BITMAP_SOURCE_H
  118.  
  119.