KOffice – TDE office suite
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*
  2. * This file is part of Chalk
  3. *
  4. * Copyright (c) 2006 Frederic Coiffier <fcoiffie@gmail.com>
  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 <math.h>
  21. #include <tdelocale.h>
  22. #include <tqlayout.h>
  23. #include <tqpixmap.h>
  24. #include <tqpainter.h>
  25. #include <tqlabel.h>
  26. #include <tqspinbox.h>
  27. #include "kis_filter_config_widget.h"
  28. #include "kis_level_filter.h"
  29. #include "wdg_level.h"
  30. #include "kis_colorspace.h"
  31. #include "kis_paint_device.h"
  32. #include "kis_iterators_pixel.h"
  33. #include "kis_iterator.h"
  34. #include "kis_histogram.h"
  35. #include "kis_basic_histogram_producers.h"
  36. #include "kis_painter.h"
  37. #include "kgradientslider.h"
  38. KisLevelFilterConfiguration::KisLevelFilterConfiguration()
  39. : KisFilterConfiguration( "levels", 1 )
  40. {
  41. whitevalue = 255;
  42. blackvalue = 0;
  43. gammavalue = 1.0;
  44. outwhitevalue = 0xFFFF;
  45. outblackvalue = 0;
  46. m_adjustment = 0;
  47. }
  48. KisLevelFilterConfiguration::~KisLevelFilterConfiguration()
  49. {
  50. delete m_adjustment;
  51. }
  52. void KisLevelFilterConfiguration::fromXML( const TQString& s )
  53. {
  54. KisFilterConfiguration::fromXML(s);
  55. blackvalue = getInt( "blackvalue" );
  56. whitevalue = getInt( "whitevalue" );
  57. gammavalue = getDouble( "gammavalue" );
  58. outblackvalue = getInt( "outblackvalue" );
  59. outwhitevalue = getInt( "outwhitevalue" );
  60. }
  61. TQString KisLevelFilterConfiguration::toString()
  62. {
  63. m_properties.clear();
  64. setProperty("blackvalue", blackvalue);
  65. setProperty("whitevalue", whitevalue);
  66. setProperty("gammavalue", gammavalue);
  67. setProperty("outblackvalue", outblackvalue);
  68. setProperty("outwhitevalue", outwhitevalue);
  69. return KisFilterConfiguration::toString();
  70. }
  71. KisLevelFilter::KisLevelFilter()
  72. : KisFilter( id(), "adjust", i18n("&Levels"))
  73. {
  74. }
  75. KisFilterConfigWidget * KisLevelFilter::createConfigurationWidget(TQWidget *parent, KisPaintDeviceSP dev)
  76. {
  77. return new KisLevelConfigWidget(parent, dev);
  78. }
  79. KisFilterConfiguration* KisLevelFilter::configuration(TQWidget *nwidget)
  80. {
  81. KisLevelConfigWidget* widget = (KisLevelConfigWidget*)nwidget;
  82. if ( widget == 0 )
  83. {
  84. return new KisLevelFilterConfiguration();
  85. } else {
  86. return widget->config();
  87. }
  88. }
  89. std::list<KisFilterConfiguration*> KisLevelFilter::listOfExamplesConfiguration(KisPaintDeviceSP /*dev*/)
  90. {
  91. //XXX should really come up with a list of configurations
  92. std::list<KisFilterConfiguration*> list;
  93. list.insert(list.begin(), new KisLevelFilterConfiguration( ));
  94. return list;
  95. }
  96. bool KisLevelFilter::workWith(KisColorSpace* cs)
  97. {
  98. return (cs->getProfile() != 0);
  99. }
  100. void KisLevelFilter::process(KisPaintDeviceSP src, KisPaintDeviceSP dst, KisFilterConfiguration* config, const TQRect& rect)
  101. {
  102. if (!config) {
  103. kdWarning() << "No configuration object for level filter\n";
  104. return;
  105. }
  106. KisLevelFilterConfiguration* configBC = (KisLevelFilterConfiguration*) config;
  107. Q_ASSERT(config);
  108. if (src!=dst) {
  109. KisPainter gc(dst);
  110. gc.bitBlt(rect.x(), rect.y(), COMPOSITE_COPY, src, rect.x(), rect.y(), rect.width(), rect.height());
  111. gc.end();
  112. }
  113. if (configBC->m_adjustment == 0) {
  114. TQ_UINT16 transfer[256];
  115. for (int i = 0; i < 256; i++) {
  116. if (i <= configBC->blackvalue)
  117. transfer[i] = configBC->outblackvalue;
  118. else if (i < configBC->whitevalue)
  119. {
  120. double a = (double)(i - configBC->blackvalue) / (double)(configBC->whitevalue - configBC->blackvalue);
  121. a = (double)(configBC->outwhitevalue - configBC->outblackvalue) * pow (a, (1.0 / configBC->gammavalue));
  122. transfer[i] = int(configBC->outblackvalue + a);
  123. }
  124. else
  125. transfer[i] = configBC->outwhitevalue;
  126. }
  127. configBC->m_adjustment = src->colorSpace()->createBrightnessContrastAdjustment(transfer);
  128. }
  129. KisRectIteratorPixel iter = dst->createRectIterator(rect.x(), rect.y(), rect.width(), rect.height(), true );
  130. setProgressTotalSteps(rect.width() * rect.height());
  131. TQ_INT32 pixelsProcessed = 0;
  132. while( ! iter.isDone() && !cancelRequested())
  133. {
  134. TQ_UINT32 npix=0, maxpix = iter.nConseqPixels();
  135. TQ_UINT8 selectedness = iter.selectedness();
  136. // The idea here is to handle stretches of completely selected and completely unselected pixels.
  137. // Partially selected pixels are handled one pixel at a time.
  138. switch(selectedness)
  139. {
  140. case MIN_SELECTED:
  141. while(iter.selectedness()==MIN_SELECTED && maxpix)
  142. {
  143. --maxpix;
  144. ++iter;
  145. ++npix;
  146. }
  147. pixelsProcessed += npix;
  148. break;
  149. case MAX_SELECTED:
  150. {
  151. TQ_UINT8 *firstPixel = iter.rawData();
  152. while(iter.selectedness()==MAX_SELECTED && maxpix)
  153. {
  154. --maxpix;
  155. if (maxpix != 0)
  156. ++iter;
  157. ++npix;
  158. }
  159. // adjust
  160. src->colorSpace()->applyAdjustment(firstPixel, firstPixel, configBC->m_adjustment, npix);
  161. pixelsProcessed += npix;
  162. ++iter;
  163. break;
  164. }
  165. default:
  166. // adjust, but since it's partially selected we also only partially adjust
  167. src->colorSpace()->applyAdjustment(iter.oldRawData(), iter.rawData(), configBC->m_adjustment, 1);
  168. const TQ_UINT8 *pixels[2] = {iter.oldRawData(), iter.rawData()};
  169. TQ_UINT8 weights[2] = {MAX_SELECTED - selectedness, selectedness};
  170. src->colorSpace()->mixColors(pixels, weights, 2, iter.rawData());
  171. ++iter;
  172. pixelsProcessed++;
  173. break;
  174. }
  175. setProgress(pixelsProcessed);
  176. }
  177. setProgressDone();
  178. }
  179. KisLevelConfigWidget::KisLevelConfigWidget(TQWidget * parent, KisPaintDeviceSP dev, const char * name, WFlags f)
  180. : KisFilterConfigWidget(parent, name, f)
  181. {
  182. m_page = new WdgLevel(this);
  183. histogram = NULL;
  184. m_page->ingradient->enableGamma(true);
  185. m_page->blackspin->setValue(0);
  186. m_page->whitespin->setValue(255);
  187. m_page->gammaspin->setNum(1.0);
  188. m_page->ingradient->modifyGamma(1.0);
  189. m_page->outblackspin->setValue(0);
  190. m_page->outwhitespin->setValue(255);
  191. TQHBoxLayout * l = new TQHBoxLayout(this);
  192. TQ_CHECK_PTR(l);
  193. l->addWidget(m_page, 0, TQt::AlignTop);
  194. connect( m_page->blackspin, TQT_SIGNAL(valueChanged(int)), TQT_SIGNAL(sigPleaseUpdatePreview()));
  195. connect( m_page->whitespin, TQT_SIGNAL(valueChanged(int)), TQT_SIGNAL(sigPleaseUpdatePreview()));
  196. connect( m_page->ingradient, TQT_SIGNAL(modifiedGamma(double)), TQT_SIGNAL(sigPleaseUpdatePreview()));
  197. connect( m_page->blackspin, TQT_SIGNAL(valueChanged(int)), m_page->ingradient, TQT_SLOT(modifyBlack(int)));
  198. connect( m_page->whitespin, TQT_SIGNAL(valueChanged(int)), m_page->ingradient, TQT_SLOT(modifyWhite(int)));
  199. //connect( m_page->whitespin, TQT_SIGNAL(valueChanged(int)), m_page->ingradient, TQT_SLOT(modifyGamma()));
  200. connect( m_page->ingradient, TQT_SIGNAL(modifiedBlack(int)), m_page->blackspin, TQT_SLOT(setValue(int)));
  201. connect( m_page->ingradient, TQT_SIGNAL(modifiedWhite(int)), m_page->whitespin, TQT_SLOT(setValue(int)));
  202. connect( m_page->ingradient, TQT_SIGNAL(modifiedGamma(double)), m_page->gammaspin, TQT_SLOT(setNum(double)));
  203. connect( m_page->outblackspin, TQT_SIGNAL(valueChanged(int)), TQT_SIGNAL(sigPleaseUpdatePreview()));
  204. connect( m_page->outwhitespin, TQT_SIGNAL(valueChanged(int)), TQT_SIGNAL(sigPleaseUpdatePreview()));
  205. connect( m_page->outblackspin, TQT_SIGNAL(valueChanged(int)), m_page->outgradient, TQT_SLOT(modifyBlack(int)));
  206. connect( m_page->outwhitespin, TQT_SIGNAL(valueChanged(int)), m_page->outgradient, TQT_SLOT(modifyWhite(int)));
  207. connect( m_page->outgradient, TQT_SIGNAL(modifiedBlack(int)), m_page->outblackspin, TQT_SLOT(setValue(int)));
  208. connect( m_page->outgradient, TQT_SIGNAL(modifiedWhite(int)), m_page->outwhitespin, TQT_SLOT(setValue(int)));
  209. connect( (TQObject*)(m_page->chkLogarithmic), TQT_SIGNAL(toggled(bool)), this, TQT_SLOT(drawHistogram(bool)));
  210. KisHistogramProducerSP producer = new KisGenericLabHistogramProducer();
  211. histogram = new KisHistogram(dev, producer, LINEAR);
  212. m_histlog = false;
  213. drawHistogram();
  214. }
  215. KisLevelConfigWidget::~KisLevelConfigWidget()
  216. {
  217. delete histogram;
  218. }
  219. void KisLevelConfigWidget::drawHistogram(bool logarithmic)
  220. {
  221. int height = 256;
  222. if (m_histlog != logarithmic) {
  223. // Update the histogram
  224. if (logarithmic)
  225. histogram->setHistogramType(LOGARITHMIC);
  226. else
  227. histogram->setHistogramType(LINEAR);
  228. m_histlog = logarithmic;
  229. }
  230. TQPixmap pix(256, height);
  231. pix.fill();
  232. TQPainter p(&pix);
  233. p.setPen(TQPen(TQt::gray,1, TQt::SolidLine));
  234. double highest = (double)histogram->calculations().getHighest();
  235. TQ_INT32 bins = histogram->producer()->numberOfBins();
  236. if (histogram->getHistogramType() == LINEAR) {
  237. double factor = (double)height / highest;
  238. for( int i=0; i<bins; ++i ) {
  239. p.drawLine(i, height, i, height - int(histogram->getValue(i) * factor));
  240. }
  241. } else {
  242. double factor = (double)height / (double)log(highest);
  243. for( int i = 0; i < bins; ++i ) {
  244. p.drawLine(i, height, i, height - int(log((double)histogram->getValue(i)) * factor));
  245. }
  246. }
  247. m_page->histview->setPixmap(pix);
  248. }
  249. KisLevelFilterConfiguration * KisLevelConfigWidget::config()
  250. {
  251. KisLevelFilterConfiguration * cfg = new KisLevelFilterConfiguration();
  252. cfg->blackvalue = m_page->blackspin->value();
  253. cfg->whitevalue = m_page->whitespin->value();
  254. cfg->gammavalue = m_page->ingradient->getGamma();
  255. cfg->outblackvalue = m_page->outblackspin->value() * 255;
  256. cfg->outwhitevalue = m_page->outwhitespin->value() * 255;
  257. return cfg;
  258. }
  259. void KisLevelConfigWidget::setConfiguration( KisFilterConfiguration * config )
  260. {
  261. KisLevelFilterConfiguration * cfg = dynamic_cast<KisLevelFilterConfiguration *>(config);
  262. m_page->blackspin->setValue(cfg->blackvalue);
  263. m_page->whitespin->setValue(cfg->whitevalue);
  264. m_page->ingradient->modifyGamma(cfg->gammavalue);
  265. m_page->outblackspin->setValue(cfg->outblackvalue / 255);
  266. m_page->outwhitespin->setValue(cfg->outwhitevalue / 255);
  267. }