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.

wavefilter.cc 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * This file is part of the KDE project
  3. *
  4. * Copyright (c) 2005 Cyrille Berger <cberger@cberger.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include "wavefilter.h"
  21. #include <stdlib.h>
  22. #include <vector>
  23. #include <tqpoint.h>
  24. #include <kcombobox.h>
  25. #include <kdebug.h>
  26. #include <kgenericfactory.h>
  27. #include <kiconloader.h>
  28. #include <kinstance.h>
  29. #include <tdelocale.h>
  30. #include <tdemessagebox.h>
  31. #include <knuminput.h>
  32. #include <kstandarddirs.h>
  33. #include <tdetempfile.h>
  34. #include <kis_image.h>
  35. #include <kis_iterators_pixel.h>
  36. #include <kis_filter_registry.h>
  37. #include <kis_global.h>
  38. #include <kis_layer.h>
  39. #include <kis_random_sub_accessor.h>
  40. #include <kis_types.h>
  41. #include "kis_wdg_wave.h"
  42. #include "wdgwaveoptions.h"
  43. typedef KGenericFactory<ChalkWaveFilter> ChalkWaveFilterFactory;
  44. K_EXPORT_COMPONENT_FACTORY( chalkwavefilter, ChalkWaveFilterFactory( "chalk" ) )
  45. class KisWaveCurve {
  46. public:
  47. virtual double valueAt(int x, int y) =0;
  48. };
  49. class KisSinusoidalWaveCurve : public KisWaveCurve {
  50. public:
  51. KisSinusoidalWaveCurve(int amplitude, int wavelength, int shift) : m_amplitude(amplitude), m_wavelength(wavelength), m_shift(shift)
  52. {
  53. }
  54. virtual double valueAt(int x, int y)
  55. {
  56. return y + m_amplitude * cos( (double) ( m_shift + x) / m_wavelength );
  57. }
  58. private:
  59. int m_amplitude, m_wavelength, m_shift;
  60. };
  61. class KisTriangleWaveCurve : public KisWaveCurve {
  62. public:
  63. KisTriangleWaveCurve(int amplitude, int wavelength, int shift) : m_amplitude(amplitude), m_wavelength(wavelength), m_shift(shift)
  64. {
  65. }
  66. virtual double valueAt(int x, int y)
  67. {
  68. return y + m_amplitude * pow( -1, (m_shift + x) / m_wavelength ) * (0.5 - (double)( (m_shift + x) % m_wavelength ) / m_wavelength );
  69. }
  70. private:
  71. int m_amplitude, m_wavelength, m_shift;
  72. };
  73. ChalkWaveFilter::ChalkWaveFilter(TQObject *parent, const char *name, const TQStringList &)
  74. : KParts::Plugin(parent, name)
  75. {
  76. setInstance(ChalkWaveFilterFactory::instance());
  77. if (parent->inherits("KisFilterRegistry")) {
  78. KisFilterRegistry * manager = dynamic_cast<KisFilterRegistry *>(parent);
  79. manager->add(new KisFilterWave());
  80. }
  81. }
  82. ChalkWaveFilter::~ChalkWaveFilter()
  83. {
  84. }
  85. KisFilterWave::KisFilterWave() : KisFilter(id(), "other", i18n("&Wave..."))
  86. {
  87. }
  88. KisFilterConfiguration* KisFilterWave::configuration(TQWidget* w)
  89. {
  90. KisWdgWave* wN = dynamic_cast<KisWdgWave*>(w);
  91. KisFilterConfiguration* config = new KisFilterConfiguration(id().id(), 1);
  92. if(wN)
  93. {
  94. config->setProperty("horizontalwavelength", wN->widget()->intHWavelength->value() );
  95. config->setProperty("horizontalshift", wN->widget()->intHShift->value() );
  96. config->setProperty("horizontalamplitude", wN->widget()->intHAmplitude->value() );
  97. config->setProperty("horizontalshape", wN->widget()->cbHShape->currentItem() );
  98. config->setProperty("verticalwavelength", wN->widget()->intVWavelength->value() );
  99. config->setProperty("verticalshift", wN->widget()->intVShift->value() );
  100. config->setProperty("verticalamplitude", wN->widget()->intVAmplitude->value() );
  101. config->setProperty("verticalshape", wN->widget()->cbVShape->currentItem() );
  102. }
  103. return config;
  104. }
  105. KisFilterConfigWidget * KisFilterWave::createConfigurationWidget(TQWidget* parent, KisPaintDeviceSP /*dev*/)
  106. {
  107. return new KisWdgWave((KisFilter*)this, (TQWidget*)parent, i18n("Configuration of wave filter").ascii());
  108. }
  109. void KisFilterWave::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* config, const TQRect& rect)
  110. {
  111. Q_ASSERT(src != 0);
  112. Q_ASSERT(dst != 0);
  113. setProgressTotalSteps(rect.width() * rect.height());
  114. TQVariant value;
  115. int horizontalwavelength = (config && config->getProperty("horizontalwavelength", value)) ? value.toInt() : 50;
  116. int horizontalshift = (config && config->getProperty("horizontalshift", value)) ? value.toInt() : 50;
  117. int horizontalamplitude = (config && config->getProperty("horizontalamplitude", value)) ? value.toInt() : 4;
  118. int horizontalshape = (config && config->getProperty("horizontalshape", value)) ? value.toInt() : 0;
  119. int verticalwavelength = (config && config->getProperty("verticalwavelength", value)) ? value.toInt() : 50;
  120. int verticalshift = (config && config->getProperty("verticalshift", value)) ? value.toInt() : 50;
  121. int verticalamplitude = (config && config->getProperty("verticalamplitude", value)) ? value.toInt() : 4;
  122. int verticalshape = (config && config->getProperty("verticalshape", value)) ? value.toInt() : 0;
  123. KisRectIteratorPixel dstIt = dst->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), true );
  124. KisWaveCurve* verticalcurve;
  125. if(verticalshape == 1)
  126. verticalcurve = new KisTriangleWaveCurve(verticalamplitude, verticalwavelength, verticalshift);
  127. else
  128. verticalcurve = new KisSinusoidalWaveCurve(verticalamplitude, verticalwavelength, verticalshift);
  129. KisWaveCurve* horizontalcurve;
  130. if(horizontalshape == 1)
  131. horizontalcurve = new KisTriangleWaveCurve(horizontalamplitude, horizontalwavelength, horizontalshift);
  132. else
  133. horizontalcurve = new KisSinusoidalWaveCurve(horizontalamplitude, horizontalwavelength, horizontalshift);
  134. KisRandomSubAccessorPixel srcRSA = src->createRandomSubAccessor();
  135. while(!dstIt.isDone())
  136. {
  137. double xv = horizontalcurve->valueAt( dstIt.y(), dstIt.x() );
  138. double yv = verticalcurve->valueAt( dstIt.x(), dstIt.y() );
  139. srcRSA.moveTo( KisPoint( xv, yv ) );
  140. srcRSA.sampledOldRawData(dstIt.rawData());
  141. ++dstIt;
  142. incProgress();
  143. }
  144. delete horizontalcurve;
  145. delete verticalcurve;
  146. setProgressDone(); // Must be called even if you don't really support progression
  147. }