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_penop.cc 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
  3. * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
  4. * Copyright (c) 2004 Clarence Dang <dang@kde.org>
  5. * Copyright (c) 2004 Adrian Page <adrian@pagenet.plus.com>
  6. * Copyright (c) 2004 Cyrille Berger <cberger@cberger.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. */
  22. #include <tqrect.h>
  23. #include <kdebug.h>
  24. #include "kis_brush.h"
  25. #include "kis_global.h"
  26. #include "kis_paint_device.h"
  27. #include "tdelocale.h"
  28. #include "kis_layer.h"
  29. #include "kis_painter.h"
  30. #include "kis_types.h"
  31. #include "kis_paintop.h"
  32. #include "kis_iterator.h"
  33. #include "kis_selection.h"
  34. #include "kis_iterators_pixel.h"
  35. #include "kis_penop.h"
  36. KisPaintOp * KisPenOpFactory::createOp(const KisPaintOpSettings */*settings*/, KisPainter * painter)
  37. {
  38. KisPaintOp * op = new KisPenOp(painter);
  39. TQ_CHECK_PTR(op);
  40. return op;
  41. }
  42. KisPenOp::KisPenOp(KisPainter * painter)
  43. : super(painter)
  44. {
  45. }
  46. KisPenOp::~KisPenOp()
  47. {
  48. }
  49. void KisPenOp::paintAt(const KisPoint &pos, const KisPaintInformation& info)
  50. {
  51. if (!m_painter) return;
  52. KisPaintDeviceSP device = m_painter->device();
  53. if (!device) return;
  54. KisBrush * brush = m_painter->brush();
  55. if (!brush) return;
  56. if (! brush->canPaintFor(info) )
  57. return;
  58. KisPoint hotSpot = brush->hotSpot(info);
  59. KisPoint pt = pos - hotSpot;
  60. TQ_INT32 x = pt.roundX();
  61. TQ_INT32 y = pt.roundY();
  62. KisPaintDeviceSP dab = 0;
  63. if (brush->brushType() == IMAGE ||
  64. brush->brushType() == PIPE_IMAGE) {
  65. dab = brush->image(device->colorSpace(), info);
  66. }
  67. else {
  68. // Compute mask without sub-pixel positioning
  69. KisAlphaMaskSP mask = brush->mask(info);
  70. dab = computeDab(mask);
  71. }
  72. m_painter->setPressure(info.pressure);
  73. TQRect dabRect = TQRect(0, 0, brush->maskWidth(info), brush->maskHeight(info));
  74. TQRect dstRect = TQRect(x, y, dabRect.width(), dabRect.height());
  75. KisImage * image = device->image();
  76. if (image != 0) {
  77. dstRect &= image->bounds();
  78. }
  79. if (dstRect.isNull() || dstRect.isEmpty() || !dstRect.isValid()) return;
  80. KisColorSpace * cs = dab->colorSpace();
  81. // Set all alpha > opaque/2 to opaque, the rest to transparent.
  82. // XXX: Using 4/10 as the 1x1 circle brush paints nothing with 0.5.
  83. KisRectIteratorPixel pixelIt = dab->createRectIterator(dabRect.x(), dabRect.y(), dabRect.width(), dabRect.height(), true);
  84. while (!pixelIt.isDone()) {
  85. TQ_UINT8 alpha = cs->getAlpha(pixelIt.rawData());
  86. if (alpha < (4 * OPACITY_OPAQUE) / 10) {
  87. cs->setAlpha(pixelIt.rawData(), OPACITY_TRANSPARENT, 1);
  88. } else {
  89. cs->setAlpha(pixelIt.rawData(), OPACITY_OPAQUE, 1);
  90. }
  91. ++pixelIt;
  92. }
  93. TQ_INT32 sx = dstRect.x() - x;
  94. TQ_INT32 sy = dstRect.y() - y;
  95. TQ_INT32 sw = dstRect.width();
  96. TQ_INT32 sh = dstRect.height();
  97. if (m_source->hasSelection()) {
  98. m_painter->bltSelection(dstRect.x(), dstRect.y(), m_painter->compositeOp(), dab.data(),
  99. m_source->selection(), m_painter->opacity(), sx, sy, sw, sh);
  100. }
  101. else {
  102. m_painter->bitBlt(dstRect.x(), dstRect.y(), m_painter->compositeOp(), dab.data(), m_painter->opacity(), sx, sy, sw, sh);
  103. }
  104. m_painter->addDirtyRect(dstRect);
  105. }