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.

randompickfilter.cc 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 "randompickfilter.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_random_accessor.h>
  36. #include <kis_layer.h>
  37. #include <kis_filter_registry.h>
  38. #include <kis_global.h>
  39. #include <kis_types.h>
  40. #include "kis_wdg_random_pick.h"
  41. #include "wdgrandompickoptions.h"
  42. typedef KGenericFactory<ChalkRandomPickFilter> ChalkRandomPickFilterFactory;
  43. K_EXPORT_COMPONENT_FACTORY( chalkrandompickfilter, ChalkRandomPickFilterFactory( "chalk" ) )
  44. ChalkRandomPickFilter::ChalkRandomPickFilter(TQObject *parent, const char *name, const TQStringList &)
  45. : KParts::Plugin(parent, name)
  46. {
  47. setInstance(ChalkRandomPickFilterFactory::instance());
  48. if (parent->inherits("KisFilterRegistry")) {
  49. KisFilterRegistry * manager = dynamic_cast<KisFilterRegistry *>(parent);
  50. manager->add(new KisFilterRandomPick());
  51. }
  52. }
  53. ChalkRandomPickFilter::~ChalkRandomPickFilter()
  54. {
  55. }
  56. KisFilterRandomPick::KisFilterRandomPick() : KisFilter(id(), "other", i18n("&Random Pick..."))
  57. {
  58. }
  59. KisFilterConfiguration* KisFilterRandomPick::configuration(TQWidget* w)
  60. {
  61. KisWdgRandomPick* wN = dynamic_cast<KisWdgRandomPick*>(w);
  62. KisFilterConfiguration* config = new KisFilterConfiguration(id().id(), 1);
  63. if(wN)
  64. {
  65. config->setProperty("level", wN->widget()->intLevel->value() );
  66. config->setProperty("windowsize", wN->widget()->intWindowSize->value() );
  67. config->setProperty("opacity", wN->widget()->intOpacity->value() );
  68. }
  69. return config;
  70. }
  71. KisFilterConfigWidget * KisFilterRandomPick::createConfigurationWidget(TQWidget* parent, KisPaintDeviceSP dev)
  72. {
  73. return new KisWdgRandomPick((KisFilter*)this, (TQWidget*)parent, i18n("Configuration of random pick filter").ascii());
  74. }
  75. void KisFilterRandomPick::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* config, const TQRect& rect)
  76. {
  77. Q_ASSERT(src != 0);
  78. Q_ASSERT(dst != 0);
  79. setProgressTotalSteps(rect.height() * rect.width());
  80. KisColorSpace * cs = src->colorSpace();
  81. TQ_INT32 psize = cs->pixelSize();
  82. TQVariant value;
  83. int level = (config && config->getProperty("level", value)) ? value.toInt() : 50;
  84. double windowsize = (config && config->getProperty("windowsize", value)) ? value.toInt() / 2. : 2.5;
  85. int opacity = (config && config->getProperty("opacity", value)) ? value.toInt() : 100;
  86. KisRectIteratorPixel dstIt = dst->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), true );
  87. KisRectIteratorPixel srcIt = src->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), false);
  88. KisRandomAccessorPixel srcRA = src->createRandomAccessor(0, 0, false);
  89. TQ_UINT32 threshold = (RAND_MAX / 100) * (100 - level);
  90. TQ_UINT8 weights[2];
  91. weights[0] = (255 * opacity) / 100; weights[1] = 255 - weights[0];
  92. const TQ_UINT8* pixels[2];
  93. while(!srcIt.isDone())
  94. {
  95. if(rand() > threshold)
  96. {
  97. int x = srcIt.x() + 2.5 * rand() / RAND_MAX;
  98. int y = srcIt.y() + 2.5 * rand() / RAND_MAX;
  99. srcRA.moveTo( x, y);
  100. pixels[0] = srcRA.oldRawData();
  101. pixels[1] = srcIt.oldRawData();
  102. cs->mixColors( pixels, weights, 2, dstIt.rawData() );
  103. }
  104. ++srcIt;
  105. ++dstIt;
  106. incProgress();
  107. }
  108. setProgressDone(); // Must be called even if you don't really support progression
  109. }