KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kis_scale_visitor.h 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * copyright (c) 2004, 2005 Michael Thaler <michael.thaler@physik.tu-muenchen.de>
  3. *
  4. * this program is free software; you can redistribute it and/or modify
  5. * it under the terms of the gnu general public license as published by
  6. * the free software foundation; either version 2 of the license, or
  7. * (at your option) any later version.
  8. *
  9. * this program is distributed in the hope that it will be useful,
  10. * but without any warranty; without even the implied warranty of
  11. * merchantability or fitness for a particular purpose. see the
  12. * gnu general public license for more details.
  13. *
  14. * you should have received a copy of the gnu general public license
  15. * along with this program; if not, write to the free software
  16. * foundation, inc., 675 mass ave, cambridge, ma 02139, usa.
  17. */
  18. #ifndef KIS_SCALE_VISITOR_H_
  19. #define KIS_SCALE_VISITOR_H_
  20. #include "tdelocale.h"
  21. #include "kis_progress_subject.h"
  22. #include "kis_progress_display_interface.h"
  23. #include "kis_thread.h"
  24. #include "kis_layer_visitor.h"
  25. #include "kis_types.h"
  26. #include "kis_layer.h"
  27. #include "kis_group_layer.h"
  28. #include "kis_paint_layer.h"
  29. #include "kis_adjustment_layer.h"
  30. #include "kis_transaction.h"
  31. #include "kis_undo_adapter.h"
  32. #include "kis_selection.h"
  33. class KisProgressDisplayInterface;
  34. class KisFilterStrategy;
  35. class KisScaleWorker : public KisThread {
  36. /* Structs for the image rescaling routine */
  37. class Contrib {
  38. public:
  39. TQ_INT32 m_pixel;
  40. double m_weight;
  41. };
  42. class ContribList {
  43. public:
  44. TQ_INT32 n; //number of contributors
  45. Contrib *p; //pointer to list of contributions
  46. };
  47. public:
  48. KisScaleWorker(KisPaintDevice * dev, double sx, double sy,
  49. KisFilterStrategy *filterStrategy)
  50. : KisThread()
  51. , m_dev(dev)
  52. , m_sx(sx)
  53. , m_sy(sy)
  54. , m_filterStrategy(filterStrategy) {};
  55. virtual ~KisScaleWorker() {};
  56. void run();
  57. private:
  58. TQ_INT32 m_pixelSize;
  59. KisPaintDevice * m_dev;
  60. double m_sx, m_sy;
  61. KisFilterStrategy * m_filterStrategy;
  62. /**
  63. * calc_x_contrib()
  64. *
  65. * Calculates the filter weights for a single target column.
  66. * contribX->p must be freed afterwards.
  67. *
  68. * Returns -1 if error, 0 otherwise.
  69. */
  70. int calcContrib(ContribList *contribX, double cale, double fwidth, int srcwidth, KisFilterStrategy *filterStrategy, TQ_INT32 i);
  71. ContribList * contrib; //array of contribution lists
  72. };
  73. class KisScaleVisitor : public KisLayerVisitor, KisProgressSubject {
  74. public:
  75. KisScaleVisitor(KisImageSP img,
  76. double sx,
  77. double sy,
  78. KisProgressDisplayInterface *progress,
  79. KisFilterStrategy *filterStrategy)
  80. : KisLayerVisitor()
  81. , m_img(img)
  82. , m_sx(sx)
  83. , m_sy(sy)
  84. , m_progress(progress)
  85. , m_filterStrategy(filterStrategy)
  86. {
  87. if ( progress )
  88. progress -> setSubject(this, true, true);
  89. emit notifyProgressStage(i18n("Scaling..."),0);
  90. }
  91. virtual ~KisScaleVisitor()
  92. {
  93. // Wait for all threads to finish
  94. KisThread * t;
  95. int threadcount = m_scalethreads.count();
  96. int i = 0;
  97. for ( t = m_scalethreads.first(); t; t = m_scalethreads.next()) {
  98. //progress info
  99. if (t) t->wait();
  100. emit notifyProgress((100 / threadcount) * i);
  101. ++i;
  102. }
  103. emit notifyProgressDone();
  104. // Delete all threads
  105. m_scalethreads.setAutoDelete(true);
  106. m_scalethreads.clear();
  107. }
  108. bool visit(KisPaintLayer *layer)
  109. {
  110. // XXX: If all is well, then the image's undoadapter will have started a macro for us
  111. // This will break in a more multi-threaded environment
  112. if (m_img->undoAdapter() && m_img->undoAdapter()->undo()) {
  113. KisTransaction * cmd = new KisTransaction("", layer->paintDevice());
  114. m_img->undoAdapter()->addCommand(cmd);
  115. }
  116. KisScaleWorker * scaleThread = new KisScaleWorker(layer->paintDevice(),
  117. m_sx, m_sy, m_filterStrategy);
  118. m_scalethreads.append(scaleThread);
  119. scaleThread->start();
  120. //scaleThread->run();
  121. layer->setDirty();
  122. return true;
  123. }
  124. bool visit(KisGroupLayer *layer)
  125. {
  126. //KisScaleVisitor visitor (m_img, m_sx, m_sy, m_progress, m_filterStrategy);
  127. // XXX: Maybe faster to scale the projection and do something clever to avoid
  128. // recompositing everything?
  129. layer->resetProjection();
  130. KisLayerSP child = layer->firstChild();
  131. while (child) {
  132. child->accept(*this);
  133. child = child->nextSibling();
  134. }
  135. return true;
  136. }
  137. bool visit(KisPartLayer */*layer*/)
  138. {
  139. return true;
  140. }
  141. virtual bool visit(KisAdjustmentLayer* layer)
  142. {
  143. KisThread * scaleThread = new KisScaleWorker(layer->selection().data(), m_sx, m_sy, m_filterStrategy);
  144. m_scalethreads.append(scaleThread);
  145. scaleThread->start();
  146. layer->resetCache();
  147. layer->setDirty();
  148. return true;
  149. }
  150. // Implement KisProgressSubject
  151. virtual void cancel()
  152. {
  153. KisThread * t;
  154. for ( t = m_scalethreads.first(); t; t = m_scalethreads.next()) {
  155. t->cancel();
  156. }
  157. }
  158. private:
  159. TQPtrList<KisThread> m_scalethreads;
  160. KisImageSP m_img;
  161. double m_sx;
  162. double m_sy;
  163. KisProgressDisplayInterface * m_progress;
  164. KisFilterStrategy * m_filterStrategy;
  165. };
  166. #endif // KIS_SCALE_VISITOR_H_