23#include "kis_debug_areas.h"
25#include "kis_paint_layer.h"
26#include "kis_selection.h"
27#include "kis_painter.h"
28#include "kis_undo_adapter.h"
29#include "kis_iterators_pixel.h"
30#include "kis_paint_device.h"
31#include "kis_meta_registry.h"
32#include "kis_colorspace_factory_registry.h"
33#include "kis_datamanager.h"
34#include "kis_undo_adapter.h"
36KisPaintLayer::KisPaintLayer(KisImage *img,
const TQString& name, TQ_UINT8 opacity, KisPaintDeviceSP dev)
37 : super(img, name, opacity)
43 m_maskAsSelection = 0;
44 m_paintdev->setParentLayer(
this);
50KisPaintLayer::KisPaintLayer(KisImage *img,
const TQString& name, TQ_UINT8 opacity)
51 : super(img, name, opacity)
54 m_paintdev =
new KisPaintDevice(
this, img->colorSpace(), name.latin1());
56 m_maskAsSelection = 0;
61KisPaintLayer::KisPaintLayer(KisImage *img,
const TQString& name, TQ_UINT8 opacity,
KisColorSpace * colorSpace)
62 : super(img, name, opacity)
68 m_maskAsSelection = 0;
74 KisLayer(rhs), KisLayerSupportsIndirectPainting(rhs)
77 m_paintdev->setParentLayer(
this);
80 m_mask->setParentLayer(
this);
82 m_renderMask = rhs.m_renderMask;
83 m_editMask = rhs.m_editMask;
88 return new KisPaintLayer(*
this);
91KisPaintLayer::~KisPaintLayer()
93 if (m_paintdev != 0) {
94 m_paintdev->setParentLayer(0);
97 m_mask->setParentLayer(0);
103 if (m_paintdev && m_paintdev->hasSelection()) {
104 m_paintdev->selection()->paintSelection(img, x, y, w, h);
105 }
else if (m_mask && m_editMask && m_mask->hasSelection()) {
106 m_mask->selection()->paintSelection(img, x, y, w, h);
112 if (m_paintdev && m_paintdev->hasSelection()) {
113 m_paintdev->selection()->paintSelection(img, scaledImageRect, scaledImageSize, imageSize);
114 }
else if (m_mask && m_editMask && m_mask->hasSelection()) {
115 m_mask->selection()->paintSelection(img, scaledImageRect, scaledImageSize, imageSize);
121 uchar *j = img.bits();
125 for (TQ_INT32 y2 = y; y2 < h + y; ++y2) {
127 while ( ! it.isDone()) {
128 TQ_UINT8 s = cs->
getAlpha(it.rawData());
131 TQ_UINT8 g = (*(j + 0) + *(j + 1 ) + *(j + 2 )) / 9;
146 return m_paintdev->createThumbnail(w, h);
152TQ_INT32 KisPaintLayer::x()
const {
154 return m_paintdev->getX();
158void KisPaintLayer::setX(TQ_INT32 x)
164TQ_INT32 KisPaintLayer::y()
const {
166 return m_paintdev->getY();
171void KisPaintLayer::setY(TQ_INT32 y) {
178 return m_paintdev->extent();
185 return m_paintdev->exactBounds();
194 m_mask->setParentLayer(0);
196 m_maskAsSelection = 0;
208 m_paintdev->extent(x, y, w, h);
211 KisPaintDeviceSP temp =
new KisPaintDevice(m_paintdev->colorSpace());
213 gc.
bltSelection(x, y, COMPOSITE_OVER, m_paintdev, m_maskAsSelection, OPACITY_OPAQUE, x, y, w, h);
215 gc.
begin(m_paintdev);
216 gc.
bitBlt(x, y, COMPOSITE_COPY, temp, OPACITY_OPAQUE, x, y, w, h);
226 kdDebug() << k_funcinfo << endl;
230 m_mask =
new KisPaintDevice(KisMetaRegistry::instance()->csRegistry()
231 ->getColorSpace(
KisID(
"GRAYA"), 0));
233 genericMaskCreationHelper();
243 kdDebug() << k_funcinfo << endl;
246 genericMaskCreationHelper();
250 kdDebug() << k_funcinfo << endl;
251 m_mask =
new KisPaintDevice(KisMetaRegistry::instance()->csRegistry()
252 ->getColorSpace(
KisID(
"GRAYA"), 0));
253 m_mask->setParentLayer(
this);
258 TQ_UINT8
const defPixel[] = { 255, 255 };
259 m_mask->dataManager()->setDefaultPixel(defPixel);
266 for (
int y = r.y(); y < h; y++) {
270 while(!dstIt.isDone()) {
272 *dstIt.rawData() = *srcIt.rawData();
279 convertMaskToSelection(
extent());
280 m_paintdev->deselect();
288 kdDebug() << k_funcinfo << endl;
294 kdDebug() << k_funcinfo << endl;
295 return m_maskAsSelection;
312void KisPaintLayer::convertMaskToSelection(
const TQRect& r) {
313 KisRectIteratorPixel srcIt = m_mask->createRectIterator(r.x(), r.y(),
314 r.width(), r.height(),
false);
315 KisRectIteratorPixel dstIt = m_maskAsSelection->createRectIterator(r.x(), r.y(),
316 r.width(), r.height(),
true);
328void KisPaintLayer::genericMaskCreationHelper() {
329 m_mask->setParentLayer(
this);
331 m_maskAsSelection =
new KisSelection();
334 TQ_UINT8
const defPixel[] = { 255, 255 };
335 m_mask->dataManager()->setDefaultPixel(defPixel);
343 convertMaskToSelection(
extent());
349 convertMaskToSelection(rect);
355 class KisCreateMaskCommand :
public KNamedCommand {
356 typedef KNamedCommand super;
357 KisPaintLayerSP m_layer;
358 KisPaintDeviceSP m_mask;
360 KisCreateMaskCommand(
const TQString& name,
KisPaintLayer* layer)
361 : super(name), m_layer(layer) {}
362 virtual void execute() {
363 kdDebug() << k_funcinfo << endl;
365 m_mask = m_layer->createMask();
367 m_layer->createMaskFromPaintDevice(m_mask);
369 virtual void unexecute() {
370 m_layer->removeMask();
374 class KisMaskFromSelectionCommand :
public KNamedCommand {
375 typedef KNamedCommand super;
376 KisPaintLayerSP m_layer;
377 KisPaintDeviceSP m_maskBefore;
378 KisPaintDeviceSP m_maskAfter;
379 KisSelectionSP m_selection;
381 KisMaskFromSelectionCommand(
const TQString& name, KisPaintLayer* layer)
382 : super(name), m_layer(layer) {
383 if (m_layer->hasMask())
384 m_maskBefore = m_layer->getMask();
388 if (m_layer->paintDevice()->hasSelection())
389 m_selection = m_layer->paintDevice()->selection();
393 virtual void execute() {
395 m_layer->createMaskFromSelection(m_selection);
396 m_maskAfter = m_layer->getMask();
398 m_layer->paintDevice()->deselect();
399 m_layer->createMaskFromPaintDevice(m_maskAfter);
402 virtual void unexecute() {
403 m_layer->paintDevice()->setSelection(m_selection);
405 m_layer->createMaskFromPaintDevice(m_maskBefore);
407 m_layer->removeMask();
411 class KisMaskToSelectionCommand :
public KNamedCommand {
412 typedef KNamedCommand super;
413 KisPaintLayerSP m_layer;
414 KisPaintDeviceSP m_mask;
415 KisSelectionSP m_selection;
417 KisMaskToSelectionCommand(
const TQString& name, KisPaintLayer* layer)
418 : super(name), m_layer(layer) {
419 m_mask = m_layer->getMask();
420 if (m_layer->paintDevice()->hasSelection())
421 m_selection = m_layer->paintDevice()->selection();
425 virtual void execute() {
426 m_layer->paintDevice()->setSelection(m_layer->getMaskAsSelection());
427 m_layer->removeMask();
429 virtual void unexecute() {
431 m_layer->paintDevice()->setSelection(m_selection);
433 m_layer->paintDevice()->deselect();
434 m_layer->createMaskFromPaintDevice(m_mask);
438 class KisRemoveMaskCommand :
public KNamedCommand {
439 typedef KNamedCommand super;
440 KisPaintLayerSP m_layer;
441 KisPaintDeviceSP m_mask;
443 KisRemoveMaskCommand(
const TQString& name, KisPaintLayer* layer)
444 : super(name), m_layer(layer) {
445 m_mask = m_layer->getMask();
447 virtual void execute() {
448 kdDebug() << k_funcinfo << endl;
449 m_layer->removeMask();
451 virtual void unexecute() {
454 m_layer->createMaskFromPaintDevice(m_mask);
458 class KisApplyMaskCommand :
public KNamedCommand {
459 typedef KNamedCommand super;
460 KisPaintLayerSP m_layer;
461 KisPaintDeviceSP m_mask;
462 KisPaintDeviceSP m_original;
464 KisApplyMaskCommand(
const TQString& name, KisPaintLayer* layer)
465 : super(name), m_layer(layer) {
466 m_mask = m_layer->getMask();
467 m_original =
new KisPaintDevice(*m_layer->paintDevice());
469 virtual void execute() {
470 m_layer->applyMask();
472 virtual void unexecute() {
475 KisPainter gc(m_layer->paintDevice());
477 m_layer->paintDevice()->extent(x, y, w, h);
479 gc.bitBlt(x, y, COMPOSITE_COPY, m_original, OPACITY_OPAQUE, x, y, w, h);
482 m_layer->createMaskFromPaintDevice(m_mask);
488 return new KisCreateMaskCommand(i18n(
"Create Layer Mask"),
this);
492 return new KisMaskFromSelectionCommand(i18n(
"Mask From Selection"),
this);
496 return new KisMaskToSelectionCommand(i18n(
"Mask to Selection"),
this);
501 return new KisRemoveMaskCommand(i18n(
"Remove Layer Mask"),
this);
505 return new KisApplyMaskCommand(i18n(
"Apply Layer Mask"),
this);
509#include "kis_paint_layer.moc"
A colorspace is the definition of a certain color model in Chalk.
virtual TQ_UINT8 getAlpha(const TQ_UINT8 *pixel) const =0
Get the alpha value of the given pixel, downscaled to an 8-bit value.
The pixel iterators are high level iterarators.
Chalk has a large number of extensible resources.
Abstract class that represents the concept of a Layer in Chalk.
virtual void setDirty(bool propagate=true)
Set the entire layer extent dirty; this percolates up to parent layers all the way to the root layer.
A paint device contains the actual pixel data and offers methods to read and write pixels.
This layer is of a type that can be painted on.
virtual TQRect extent() const
Returns an approximation of where the bounds on actual data are in this layer.
KisPaintDeviceSP createMask()
Create a mask if it does not yet exist, and return it.
virtual KisLayerSP clone() const
Return a copy of this layer.
KisPaintDeviceSP getMask()
Returns the layer mask's device. Creates one if there is currently none.
void setEditMask(bool b)
Sets the editMask property.
virtual void setDirty(bool propagate=true)
Overridden to call the private convertMaskToSelection.
KNamedCommand * createMaskCommand()
Undoable version of createMask.
void createMaskFromPaintDevice(KisPaintDeviceSP from)
Convert the from argument to the mask.
void setRenderMask(bool b)
Set the renderMask property.
void sigMaskInfoChanged()
When the mask is created/destroyed or the editmask or rendermask is changed.
virtual void paintMaskInactiveLayers(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h)
paints where no data is on this layer. Useful when it is a transparent layer stacked on top of anothe...
virtual TQImage createThumbnail(TQ_INT32 w, TQ_INT32 h)
Returns a thumbnail in requested size.
KNamedCommand * applyMaskCommand()
Undoable version of applyMask.
KNamedCommand * maskFromSelectionCommand()
Undoable version of createMaskFromSelection.
KisSelectionSP getMaskAsSelection()
Returns the layer mask's device, converted to a selection. Creates one if there is currently none.
void removeMask()
Remove the layer mask.
KNamedCommand * removeMaskCommand()
Undoable version of removeMask.
virtual void paintSelection(TQImage &img, TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h)
paints a mask where the selection on this layer resides
void applyMask()
Apply the layer mask to the paint device, this removes the mask afterwards.
void createMaskFromSelection(KisSelectionSP from)
Convert the from selection to a paint device (should convert the getMaskAsSelection result back to th...
virtual TQRect exactBounds() const
Returns the exact bounds of where the actual data resides in this layer.
bool hasMask() const
Does this layer have a layer mask?
KNamedCommand * maskToSelectionCommand()
Undoable, removes the current mask, but converts it to the current selection.
KisPainter contains the graphics primitives necessary to draw on a KisPaintDevice.
KCommand * end()
Finish painting on the current device.
void bitBlt(TQ_INT32 dx, TQ_INT32 dy, const KisCompositeOp &op, KisPaintDeviceSP src, TQ_INT32 sx, TQ_INT32 sy, TQ_INT32 sw, TQ_INT32 sh)
Blast the specified region from src onto the current paint device.
void begin(KisPaintDeviceSP device)
Start painting on the specified device.
void bltSelection(TQ_INT32 dx, TQ_INT32 dy, const KisCompositeOp &op, KisPaintDeviceSP src, KisSelectionSP selMask, TQ_UINT8 opacity, TQ_INT32 sx, TQ_INT32 sy, TQ_INT32 sw, TQ_INT32 sh)
A version of bitBlt that renders using an external selection mask, ignoring the src device's own sele...
TQ_UINT8 * rawData() const
returns a pointer to the pixel data. Do NOT interpret the data - leave that to a colorstrategy
bool isDone() const
returns true when iterators has reached the end
KisSelection contains a byte-map representation of a layer, where the value of a byte signifies wheth...