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.

noisefilter.cc 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "noisefilter.h"
  21. #include <stdlib.h>
  22. #include <vector>
  23. #include <tqpoint.h>
  24. #include <kdebug.h>
  25. #include <kiconloader.h>
  26. #include <kinstance.h>
  27. #include <kgenericfactory.h>
  28. #include <tdelocale.h>
  29. #include <tdemessagebox.h>
  30. #include <knuminput.h>
  31. #include <kstandarddirs.h>
  32. #include <tdetempfile.h>
  33. #include <kis_image.h>
  34. #include <kis_iterators_pixel.h>
  35. #include <kis_layer.h>
  36. #include <kis_filter_registry.h>
  37. #include <kis_global.h>
  38. #include <kis_types.h>
  39. #include "kis_wdg_noise.h"
  40. #include "wdgnoiseoptions.h"
  41. typedef KGenericFactory<ChalkNoiseFilter> ChalkNoiseFilterFactory;
  42. K_EXPORT_COMPONENT_FACTORY( chalknoisefilter, ChalkNoiseFilterFactory( "chalk" ) )
  43. ChalkNoiseFilter::ChalkNoiseFilter(TQObject *parent, const char *name, const TQStringList &)
  44. : KParts::Plugin(parent, name)
  45. {
  46. setInstance(ChalkNoiseFilterFactory::instance());
  47. if (parent->inherits("KisFilterRegistry")) {
  48. KisFilterRegistry * manager = dynamic_cast<KisFilterRegistry *>(parent);
  49. manager->add(new KisFilterNoise());
  50. }
  51. }
  52. ChalkNoiseFilter::~ChalkNoiseFilter()
  53. {
  54. }
  55. KisFilterNoise::KisFilterNoise() : KisFilter(id(), "other", i18n("&Random Noise..."))
  56. {
  57. }
  58. KisFilterConfiguration* KisFilterNoise::configuration(TQWidget* w)
  59. {
  60. KisWdgNoise* wN = dynamic_cast<KisWdgNoise*>(w);
  61. KisFilterConfiguration* config = new KisFilterConfiguration(id().id(), 1);
  62. if(wN)
  63. {
  64. config->setProperty("level", wN->widget()->intLevel->value() );
  65. config->setProperty("opacity", wN->widget()->intOpacity->value() );
  66. }
  67. return config;
  68. }
  69. KisFilterConfigWidget * KisFilterNoise::createConfigurationWidget(TQWidget* parent, KisPaintDeviceSP dev)
  70. {
  71. return new KisWdgNoise((KisFilter*)this, (TQWidget*)parent, i18n("Configuration of noise filter").ascii());
  72. }
  73. void KisFilterNoise::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* config, const TQRect& rect)
  74. {
  75. Q_ASSERT(src != 0);
  76. Q_ASSERT(dst != 0);
  77. setProgressTotalSteps(rect.width() * rect.height());
  78. KisColorSpace * cs = src->colorSpace();
  79. TQ_INT32 psize = cs->pixelSize();
  80. TQVariant value;
  81. int level = (config && config->getProperty("level", value)) ? value.toInt() : 50;
  82. int opacity = (config && config->getProperty("opacity", value)) ? value.toInt() : 100;
  83. KisRectIteratorPixel dstIt = dst->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), true );
  84. KisRectIteratorPixel srcIt = src->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), false);
  85. TQ_UINT8* interm = new TQ_UINT8[ cs->pixelSize() ];
  86. TQ_UINT32 threshold = (RAND_MAX / 100) * (100 - level);
  87. TQ_UINT8 weights[2];
  88. weights[0] = (255 * opacity) / 100; weights[1] = 255 - weights[0];
  89. const TQ_UINT8* pixels[2];
  90. pixels[0] = interm;
  91. while(!srcIt.isDone())
  92. {
  93. if(rand() > threshold)
  94. {
  95. TQColor c = tqRgb((double)rand()/RAND_MAX * 255,(double)rand()/RAND_MAX * 255,(double)rand()/RAND_MAX * 255);
  96. cs->fromTQColor( c, interm, 0 );
  97. pixels[1] = srcIt.oldRawData();
  98. cs->mixColors( pixels, weights, 2, dstIt.rawData() );
  99. }
  100. ++srcIt;
  101. ++dstIt;
  102. incProgress();
  103. }
  104. delete interm;
  105. setProgressDone(); // Must be called even if you don't really support progression
  106. }