krita

kis_boundary.cc

00001 /*
00002  *  Copyright (c) 2005 Bart Coppens <kde@bartcoppens.be>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 #include <qpixmap.h>
00019 #include <qpainter.h>
00020 
00021 #include "kis_colorspace.h"
00022 #include "kis_iterators_pixel.h"
00023 #include "kis_paint_device.h"
00024 #include "kis_boundary.h"
00025 
00026 KisBoundary::KisBoundary(KisPaintDevice* dev) {
00027     m_device = dev;
00028     m_fuzzyness = 255 / 2;
00029 }
00030 
00031 bool KisBoundary::isDark(Q_UINT8 val) {
00032     return val < m_fuzzyness;
00033 }
00034 
00035 void KisBoundary::generateBoundary(int w, int h) {
00036     if (!m_device)
00037         return;
00038 
00039     KisColorSpace* cs = m_device->colorSpace();
00040 
00041     // Horizontal
00042     for (int currentY = - 1; currentY < h; currentY++) {
00043         KisHLineIteratorPixel topIt = m_device->createHLineIterator(0, currentY, w, false);
00044         KisHLineIteratorPixel botIt = m_device->createHLineIterator(0, currentY + 1, w, false);
00045         bool darkTop;
00046         bool darkBot;
00047 
00048         m_horSegments.append(QValueList<PointPair>());
00049 
00050         while (!topIt.isDone()) {
00051             darkTop = cs->getAlpha(topIt.rawData());
00052             darkBot = cs->getAlpha(botIt.rawData());
00053             if (darkTop != darkBot) {
00054                 // detected a change
00055                 m_horSegments.back().append(qMakePair(KisPoint(botIt.x(), botIt.y()), 1));
00056             }
00057             ++topIt;
00058             ++botIt;
00059         }
00060     }
00061 
00062     // Vertical
00063     for (int currentX = - 1; currentX < w; currentX++) {
00064         KisVLineIteratorPixel leftIt = m_device->createVLineIterator(currentX, 0, h, false);
00065         KisVLineIteratorPixel rightIt = m_device->createVLineIterator(currentX + 1, 0, h, false);
00066         bool darkLeft;
00067         bool darkRight;
00068 
00069         m_vertSegments.append(QValueList<PointPair>());
00070 
00071         while (!leftIt.isDone()) {
00072             darkLeft = cs->getAlpha(leftIt.rawData());
00073             darkRight = cs->getAlpha(rightIt.rawData());
00074             if (darkLeft != darkRight) {
00075                 // detected a change
00076                 m_vertSegments.back().append(qMakePair(KisPoint(rightIt.x(), rightIt.y()), 1));
00077             }
00078             ++leftIt;
00079             ++rightIt;
00080         }
00081     }
00082 }
00083 
KDE Home | KDE Accessibility Home | Description of Access Keys