If the first pass hasn't found any opaque pixel, there is no reason to check that 3 more times. They will not appear in the meantime. Just return an empty bounding rect.
1324{
1325 if (startRect == endRect) return startRect;
1326
1327
1328
1329 if (!startRect.isValid()) return QRect();
1330
1331
1332 int x, y, w, h;
1333 int boundLeft, boundTop, boundRight, boundBottom;
1334 int endDirN, endDirE, endDirS, endDirW;
1335
1336 startRect.getRect(&x, &y, &w, &h);
1337
1338 if (endRect.isEmpty()) {
1339 endDirS = startRect.bottom();
1340 endDirN = startRect.top();
1341 endDirE = startRect.right();
1342 endDirW = startRect.left();
1343 startRect.getCoords(&boundLeft, &boundTop, &boundRight, &boundBottom);
1344 } else {
1345 endDirS = endRect.top() - 1;
1346 endDirN = endRect.bottom() + 1;
1347 endDirE = endRect.left() - 1;
1348 endDirW = endRect.right() + 1;
1349 endRect.getCoords(&boundLeft, &boundTop, &boundRight, &boundBottom);
1350 }
1351
1352
1353
1354
1356
1357 bool found = false;
1358 {
1359 for (qint32 y2 = y; y2 <= endDirS; ++y2) {
1360 for (qint32 x2 = x; x2 <
x +
w || found; ++ x2) {
1361 accessor->
moveTo(x2, y2);
1362 if (!compareOp.isPixelEmpty(accessor->
rawDataConst())) {
1363 boundTop = y2;
1364 found = true;
1365 break;
1366 }
1367 }
1368 if (found) break;
1369 }
1370 }
1371
1377 if (!found && endRect.isEmpty()) {
1378 return QRect();
1379 }
1380
1381 found = false;
1382
1383 for (qint32 y2 = y + h - 1; y2 >= endDirN ; --y2) {
1384 for (qint32 x2 = x + w - 1; x2 >=
x || found; --x2) {
1385 accessor->
moveTo(x2, y2);
1386 if (!compareOp.isPixelEmpty(accessor->
rawDataConst())) {
1387 boundBottom = y2;
1388 found = true;
1389 break;
1390 }
1391 }
1392 if (found) break;
1393 }
1394 found = false;
1395
1396 {
1397 for (qint32 x2 = x; x2 <= endDirE ; ++x2) {
1398 for (qint32 y2 = y; y2 <
y +
h || found; ++y2) {
1399 accessor->
moveTo(x2, y2);
1400 if (!compareOp.isPixelEmpty(accessor->
rawDataConst())) {
1401 boundLeft = x2;
1402 found = true;
1403 break;
1404 }
1405 }
1406 if (found) break;
1407 }
1408 }
1409
1410 found = false;
1411
1412
1413 {
1414
1415 for (qint32 x2 = x + w - 1; x2 >= endDirW; --x2) {
1416 for (qint32 y2 = y + h - 1; y2 >=
y || found; --y2) {
1417 accessor->
moveTo(x2, y2);
1418 if (!compareOp.isPixelEmpty(accessor->
rawDataConst())) {
1419 boundRight = x2;
1420 found = true;
1421 break;
1422 }
1423 }
1424 if (found) break;
1425 }
1426 }
1427
1428 return QRect(boundLeft, boundTop,
1429 boundRight - boundLeft + 1,
1430 boundBottom - boundTop + 1);
1431}
virtual const quint8 * rawDataConst() const =0
KisRandomConstAccessorSP createRandomConstAccessorNG() const
virtual void moveTo(qint32 x, qint32 y)=0