chalk

kis_boundary.cpp
1/*
2 * Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18#include <tqpixmap.h>
19#include <tqpainter.h>
20
21#include "kis_colorspace.h"
22#include "kis_iterators_pixel.h"
23#include "kis_paint_device.h"
24#include "kis_boundary.h"
25
26KisBoundary::KisBoundary(KisPaintDevice* dev) {
27 m_device = dev;
28 m_fuzzyness = 255 / 2;
29}
30
31bool KisBoundary::isDark(TQ_UINT8 val) {
32 return val < m_fuzzyness;
33}
34
35void KisBoundary::generateBoundary(int w, int h) {
36 if (!m_device)
37 return;
38
39 KisColorSpace* cs = m_device->colorSpace();
40
41 // Horizontal
42 for (int currentY = - 1; currentY < h; currentY++) {
43 KisHLineIteratorPixel topIt = m_device->createHLineIterator(0, currentY, w, false);
44 KisHLineIteratorPixel botIt = m_device->createHLineIterator(0, currentY + 1, w, false);
45 bool darkTop;
46 bool darkBot;
47
48 m_horSegments.append(TQValueList<PointPair>());
49
50 while (!topIt.isDone()) {
51 darkTop = cs->getAlpha(topIt.rawData());
52 darkBot = cs->getAlpha(botIt.rawData());
53 if (darkTop != darkBot) {
54 // detected a change
55 m_horSegments.back().append(qMakePair(KisPoint(botIt.x(), botIt.y()), 1));
56 }
57 ++topIt;
58 ++botIt;
59 }
60 }
61
62 // Vertical
63 for (int currentX = - 1; currentX < w; currentX++) {
64 KisVLineIteratorPixel leftIt = m_device->createVLineIterator(currentX, 0, h, false);
65 KisVLineIteratorPixel rightIt = m_device->createVLineIterator(currentX + 1, 0, h, false);
66 bool darkLeft;
67 bool darkRight;
68
69 m_vertSegments.append(TQValueList<PointPair>());
70
71 while (!leftIt.isDone()) {
72 darkLeft = cs->getAlpha(leftIt.rawData());
73 darkRight = cs->getAlpha(rightIt.rawData());
74 if (darkLeft != darkRight) {
75 // detected a change
76 m_vertSegments.back().append(qMakePair(KisPoint(rightIt.x(), rightIt.y()), 1));
77 }
78 ++leftIt;
79 ++rightIt;
80 }
81 }
82}
83
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.
A paint device contains the actual pixel data and offers methods to read and write pixels.
KisVLineIteratorPixel createVLineIterator(TQ_INT32 x, TQ_INT32 y, TQ_INT32 h, bool writable)
This function return an iterator which points to the first pixel of a vertical line.
KisHLineIteratorPixel createHLineIterator(TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, bool writable)
This function return an iterator which points to the first pixel of a horizontal line.
A double-based point class that can return it's coordinates approximated to integers.
Definition kis_point.h:28