Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++
7.7 KB
Raw Blame
/*
 *  SPDX-FileCopyrightText: 2014 Dmitry Kazakov <[email protected]>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#ifndef __KIS_SCANLINE_FILL_H
#define __KIS_SCANLINE_FILL_H

#include <QScopedPointer>

#include <kritaimage_export.h>
#include <kis_types.h>
#include <kis_paint_device.h>

class KisFillInterval;
class KisFillIntervalMap;

class KRITAIMAGE_EXPORT KisScanlineFill
{
public:
    KisScanlineFill(KisPaintDeviceSP device, const QPoint &startPoint, const QRect &boundingRect);
    ~KisScanlineFill();

    /**
     * Fill the source device with \p fillColor
     */
    void fill(const KoColor &fillColor);

    /**
     * Fill the source device with \p fillColor until \p boundaryColor is reached
     */
    void fillUntilColor(const KoColor &fillColor, const KoColor &boundaryColor);

    /**
     * Fill \p externalDevice with \p fillColor basing on the contents
     * of the source device.
     */
    void fill(const KoColor &fillColor, KisPaintDeviceSP externalDevice);

    /**
     * Fill \p externalDevice with \p fillColor basing on the contents
     * of the source device. Fills until \p boundaryColor is reached
     */
    void fillUntilColor(const KoColor &fillColor, const KoColor &boundaryColor, KisPaintDeviceSP externalDevice);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area.
     * This method uses an existing selection as boundary for the flood fill.
     */
    void fillSelection(KisPixelSelectionSP pixelSelection, KisPaintDeviceSP boundarySelection);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area
     */
    void fillSelection(KisPixelSelectionSP pixelSelection);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area, which
     * encompass all the connected pixels as long as the color in the
     * pixels of the source device is not similar to \p boundaryColor.
     * This method uses an existing selection as boundary for the flood fill.
     */
    void fillSelectionUntilColor(KisPixelSelectionSP pixelSelection, const KoColor &boundaryColor, KisPaintDeviceSP boundarySelection);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area, which
     * encompass all the connected pixels as long as the color in the
     * pixels of the source device is not similar to \p boundaryColor.
     */
    void fillSelectionUntilColor(KisPixelSelectionSP pixelSelection, const KoColor &boundaryColor);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area, which
     * encompass all the connected pixels as long as the color in the
     * pixels of the source device is not similar to \p boundaryColor or transparent.
     * This method uses an existing selection as boundary for the flood fill.
     */
    void fillSelectionUntilColorOrTransparent(KisPixelSelectionSP pixelSelection, const KoColor &boundaryColor, KisPaintDeviceSP boundarySelection);

    /**
     * Fill \p pixelSelection with the opacity of the contiguous area, which
     * encompass all the connected pixels as long as the color in the
     * pixels of the source device is not similar to \p boundaryColor or transparent.
     */
    void fillSelectionUntilColorOrTransparent(KisPixelSelectionSP pixelSelection, const KoColor &boundaryColor);

    /**
     * Clear the contiguous non-zero area of the device
     *
     * WARNING: the threshold parameter is not counted!
     */
    void clearNonZeroComponent();

    /**
     * A special filler algorithm for the Watershed initialization routine:
     *
     * 1) Clear the contiguous area in the destination device
     * 2) At the same time, fill the corresponding area of \p groupMapDevice with
     *    value \p groupIndex
     * 3) \p groupMapDevice **must** store 4 bytes per pixel
     */
    void fillContiguousGroup(KisPaintDeviceSP groupMapDevice, qint32 groupIndex);

    /**
     * Set the threshold of the filling operation
     *
     * Used in all functions except clearNonZeroComponent()
     */
    void setThreshold(int threshold);

    /**
     * Set the opacity spread for floodfill. The range is 0-100: 0% means that
     * the fully opaque area only encompasses the pixels exactly equal to the
     * seed point with the other pixels of the selected region being
     * semi-transparent (depending on how similar they are to the seed pixel)
     * up to the region boundary (given by the threshold value). 100 means that
     * the fully opaque area will encompass all the pixels of the selected
     * region up to the contour. Any value in between will make the fully opaque
     * portion of the region vary in size, with semi-transparent pixels
     * in between it and  the region boundary
     */
    void setOpacitySpread(int opacitySpread);

    /**
     * Set the close gap size in pixels for the flood fill.
     * The value 0 behaves like a regular fill. Values 1 and larger will cause
     * a gap map to be computed for the area and then used for the fill.
     * The gaps are small areas of non-opaque pixels that are roughly not
     * larger than the selected gap size. If such a gap is detected, the fill
     * will stop spreading beyond that area.
     */
    void setCloseGap(int closeGap);

    /**
     * Returns the extent of the last filled region
     */
    QRect fillExtent() const;

private:
    friend class KisScanlineFillTest;
    Q_DISABLE_COPY(KisScanlineFill)

    template <typename DifferencePolicy, typename SelectionPolicy, typename PixelAccessPolicy>
    void processLine(KisFillInterval interval, const int rowIncrement,
                     DifferencePolicy &differencePolicy,
                     SelectionPolicy &selectionPolicy,
                     PixelAccessPolicy &pixelAccessPolicy);

    template <typename DifferencePolicy, typename SelectionPolicy, typename PixelAccessPolicy>
    void extendedPass(KisFillInterval *currentInterval, int srcRow, bool extendRight,
                      DifferencePolicy &differencePolicy,
                      SelectionPolicy &selectionPolicy,
                      PixelAccessPolicy &pixelAccessPolicy);

    template <typename DifferencePolicy, typename SelectionPolicy, typename PixelAccessPolicy>
    void runImpl(DifferencePolicy &differencePolicy,
                 SelectionPolicy &selectionPolicy,
                 PixelAccessPolicy &pixelAccessPolicy);

    template <template <typename SrcPixelType> typename OptimizedDifferencePolicy,
              typename SlowDifferencePolicy,
              typename SelectionPolicy, typename PixelAccessPolicy>
    void selectDifferencePolicyAndRun(const KoColor &srcColor,
                                      SelectionPolicy &selectionPolicy,
                                      PixelAccessPolicy &pixelAccessPolicy);

    template <typename DifferencePolicy, typename SelectionPolicy, typename PixelAccessPolicy>
    KisFillInterval closeGapPass(DifferencePolicy &differencePolicy,
                                 SelectionPolicy &selectionPolicy,
                                 PixelAccessPolicy &pixelAccessPolicy);

    template <typename DifferencePolicy, typename SelectionPolicy, typename PixelAccessPolicy>
    bool fillOpacity(DifferencePolicy &differencePolicy,
                     SelectionPolicy &selectionPolicy,
                     PixelAccessPolicy &pixelAccessPolicy,
                     KisPaintDevice* const devicePtr,
                     const QRect& rect) const;

    inline bool tryPushingCloseGapSeed(int x, int y, bool allowExpand);

private:
    void testingProcessLine(const KisFillInterval &processInterval);
    QVector<KisFillInterval> testingGetForwardIntervals() const;
    KisFillIntervalMap* testingGetBackwardIntervals() const;
private:
    struct Private;
    const QScopedPointer<Private> m_d;
};

#endif /* __KIS_SCANLINE_FILL_H */