Changeset View
Changeset View
Standalone View
Standalone View
imagelib/transforms/kpTransformAutoCrop.cpp
Show First 20 Lines • Show All 168 Lines • ▼ Show 20 Line(s) | |||||
169 | // public | 169 | // public | ||
170 | kpColor kpTransformAutoCropBorder::averageColor () const | 170 | kpColor kpTransformAutoCropBorder::averageColor () const | ||
171 | { | 171 | { | ||
172 | if (!m_rect.isValid ()) | 172 | if (!m_rect.isValid ()) | ||
173 | return kpColor::Invalid; | 173 | return kpColor::Invalid; | ||
174 | 174 | | |||
175 | if (m_referenceColor.isTransparent ()) | 175 | if (m_referenceColor.isTransparent ()) | ||
176 | return kpColor::Transparent; | 176 | return kpColor::Transparent; | ||
177 | else if (m_processedColorSimilarity == 0) | 177 | | ||
178 | if (m_processedColorSimilarity == 0) | ||||
178 | return m_referenceColor; | 179 | return m_referenceColor; | ||
179 | else | 180 | | ||
180 | { | | |||
181 | int numPixels = (m_rect.width () * m_rect.height ()); | 181 | int numPixels = (m_rect.width () * m_rect.height ()); | ||
182 | Q_ASSERT (numPixels > 0); | 182 | Q_ASSERT (numPixels > 0); | ||
183 | 183 | | |||
184 | return kpColor (m_redSum / numPixels, | 184 | return kpColor (m_redSum / numPixels, m_greenSum / numPixels, m_blueSum / numPixels); | ||
185 | m_greenSum / numPixels, | 185 | | ||
186 | m_blueSum / numPixels); | | |||
187 | } | | |||
188 | } | 186 | } | ||
189 | 187 | | |||
190 | //--------------------------------------------------------------------- | 188 | //--------------------------------------------------------------------- | ||
191 | 189 | | |||
192 | bool kpTransformAutoCropBorder::isSingleColor () const | 190 | bool kpTransformAutoCropBorder::isSingleColor () const | ||
193 | { | 191 | { | ||
194 | return m_isSingleColor; | 192 | return m_isSingleColor; | ||
195 | } | 193 | } | ||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Line(s) | 313 | { | |||
317 | m_referenceColor = kpColor::Invalid; | 315 | m_referenceColor = kpColor::Invalid; | ||
318 | m_redSum = m_greenSum = m_blueSum = 0; | 316 | m_redSum = m_greenSum = m_blueSum = 0; | ||
319 | m_isSingleColor = false; | 317 | m_isSingleColor = false; | ||
320 | } | 318 | } | ||
321 | 319 | | |||
322 | 320 | | |||
323 | struct kpTransformAutoCropCommandPrivate | 321 | struct kpTransformAutoCropCommandPrivate | ||
324 | { | 322 | { | ||
325 | bool actOnSelection; | 323 | bool actOnSelection{}; | ||
326 | kpTransformAutoCropBorder leftBorder, rightBorder, topBorder, botBorder; | 324 | kpTransformAutoCropBorder leftBorder, rightBorder, topBorder, botBorder; | ||
327 | kpImage *leftImage, *rightImage, *topImage, *botImage; | 325 | kpImage *leftImage{}, *rightImage{}, *topImage{}, *botImage{}; | ||
328 | 326 | | |||
329 | QRect contentsRect; | 327 | QRect contentsRect; | ||
330 | int oldWidth, oldHeight; | 328 | int oldWidth{}, oldHeight{}; | ||
331 | kpAbstractImageSelection *oldSelectionPtr; | 329 | kpAbstractImageSelection *oldSelectionPtr{}; | ||
332 | }; | 330 | }; | ||
333 | 331 | | |||
334 | // REFACTOR: Move to /commands/ | 332 | // REFACTOR: Move to /commands/ | ||
335 | kpTransformAutoCropCommand::kpTransformAutoCropCommand (bool actOnSelection, | 333 | kpTransformAutoCropCommand::kpTransformAutoCropCommand (bool actOnSelection, | ||
336 | const kpTransformAutoCropBorder &leftBorder, | 334 | const kpTransformAutoCropBorder &leftBorder, | ||
337 | const kpTransformAutoCropBorder &rightBorder, | 335 | const kpTransformAutoCropBorder &rightBorder, | ||
338 | const kpTransformAutoCropBorder &topBorder, | 336 | const kpTransformAutoCropBorder &topBorder, | ||
339 | const kpTransformAutoCropBorder &botBorder, | 337 | const kpTransformAutoCropBorder &botBorder, | ||
Show All 32 Lines | |||||
372 | 370 | | |||
373 | //--------------------------------------------------------------------- | 371 | //--------------------------------------------------------------------- | ||
374 | // public static | 372 | // public static | ||
375 | 373 | | |||
376 | QString kpTransformAutoCropCommand::text(bool actOnSelection, int options) | 374 | QString kpTransformAutoCropCommand::text(bool actOnSelection, int options) | ||
377 | { | 375 | { | ||
378 | if (actOnSelection) | 376 | if (actOnSelection) | ||
379 | { | 377 | { | ||
380 | if (options & kpTransformAutoCropCommand::ShowAccel) | 378 | if (options & kpTransformAutoCropCommand::ShowAccel) { | ||
381 | return i18n ("Remove Internal B&order"); | 379 | return i18n ("Remove Internal B&order"); | ||
382 | else | 380 | } | ||
381 | | ||||
383 | return i18n ("Remove Internal Border"); | 382 | return i18n ("Remove Internal Border"); | ||
384 | } | 383 | } | ||
385 | else | 384 | | ||
386 | { | | |||
387 | if (options & kpTransformAutoCropCommand::ShowAccel) | 385 | if (options & kpTransformAutoCropCommand::ShowAccel) | ||
388 | return i18n ("Autocr&op"); | 386 | return i18n ("Autocr&op"); | ||
389 | else | 387 | | ||
390 | return i18n ("Autocrop"); | 388 | return i18n ("Autocrop"); | ||
391 | } | 389 | } | ||
392 | } | | |||
393 | 390 | | |||
394 | //--------------------------------------------------------------------- | 391 | //--------------------------------------------------------------------- | ||
395 | // public virtual [base kpCommand] | 392 | // public virtual [base kpCommand] | ||
396 | 393 | | |||
397 | kpCommandSize::SizeType kpTransformAutoCropCommand::size () const | 394 | kpCommandSize::SizeType kpTransformAutoCropCommand::size () const | ||
398 | { | 395 | { | ||
399 | return d->leftBorder.size () + | 396 | return d->leftBorder.size () + | ||
400 | d->rightBorder.size () + | 397 | d->rightBorder.size () + | ||
Show All 12 Lines | |||||
413 | void kpTransformAutoCropCommand::getUndoImage (const kpTransformAutoCropBorder &border, kpImage **image) | 410 | void kpTransformAutoCropCommand::getUndoImage (const kpTransformAutoCropBorder &border, kpImage **image) | ||
414 | { | 411 | { | ||
415 | kpDocument *doc = document (); | 412 | kpDocument *doc = document (); | ||
416 | Q_ASSERT (doc); | 413 | Q_ASSERT (doc); | ||
417 | 414 | | |||
418 | qCDebug(kpLogImagelib) << "kpTransformAutoCropCommand::getUndoImage()"; | 415 | qCDebug(kpLogImagelib) << "kpTransformAutoCropCommand::getUndoImage()"; | ||
419 | qCDebug(kpLogImagelib) << "\timage=" << image | 416 | qCDebug(kpLogImagelib) << "\timage=" << image | ||
420 | << " border: rect=" << border.rect () | 417 | << " border: rect=" << border.rect () | ||
421 | << " isSingleColor=" << border.isSingleColor () | 418 | << " isSingleColor=" << border.isSingleColor (); | ||
422 | << endl; | | |||
423 | 419 | | |||
424 | if (image && border.exists () && !border.isSingleColor ()) | 420 | if (image && border.exists () && !border.isSingleColor ()) | ||
425 | { | 421 | { | ||
426 | if (*image) | 422 | if (*image) | ||
427 | { | 423 | { | ||
428 | qCDebug(kpLogImagelib) << "\talready have *image - delete it"; | 424 | qCDebug(kpLogImagelib) << "\talready have *image - delete it"; | ||
429 | delete *image; | 425 | delete *image; | ||
430 | } | 426 | } | ||
Show All 24 Lines | 446 | { | |||
455 | delete d->topImage; d->topImage = nullptr; | 451 | delete d->topImage; d->topImage = nullptr; | ||
456 | delete d->botImage; d->botImage = nullptr; | 452 | delete d->botImage; d->botImage = nullptr; | ||
457 | } | 453 | } | ||
458 | 454 | | |||
459 | 455 | | |||
460 | // public virtual [base kpCommand] | 456 | // public virtual [base kpCommand] | ||
461 | void kpTransformAutoCropCommand::execute () | 457 | void kpTransformAutoCropCommand::execute () | ||
462 | { | 458 | { | ||
463 | if (!d->contentsRect.isValid ()) | 459 | if (!d->contentsRect.isValid ()) { | ||
464 | d->contentsRect = contentsRect (); | 460 | d->contentsRect = contentsRect (); | ||
461 | } | ||||
465 | 462 | | |||
466 | 463 | | |||
467 | getUndoImages (); | 464 | getUndoImages (); | ||
468 | 465 | | |||
469 | 466 | | |||
470 | kpDocument *doc = document (); | 467 | kpDocument *doc = document (); | ||
471 | Q_ASSERT (doc); | 468 | Q_ASSERT (doc); | ||
472 | 469 | | |||
473 | 470 | | |||
474 | kpImage imageWithoutBorder = | 471 | kpImage imageWithoutBorder = | ||
475 | kpTool::neededPixmap (doc->image (d->actOnSelection), | 472 | kpTool::neededPixmap (doc->image (d->actOnSelection), | ||
476 | d->contentsRect); | 473 | d->contentsRect); | ||
477 | 474 | | |||
478 | 475 | | |||
479 | if (!d->actOnSelection) | 476 | if (!d->actOnSelection) { | ||
480 | doc->setImage (imageWithoutBorder); | 477 | doc->setImage (imageWithoutBorder); | ||
481 | else | 478 | } | ||
482 | { | 479 | else { | ||
483 | d->oldSelectionPtr = doc->imageSelection ()->clone (); | 480 | d->oldSelectionPtr = doc->imageSelection ()->clone (); | ||
484 | d->oldSelectionPtr->setBaseImage (kpImage ()); | 481 | d->oldSelectionPtr->setBaseImage (kpImage ()); | ||
485 | 482 | | |||
486 | // d->contentsRect is relative to the top of the sel | 483 | // d->contentsRect is relative to the top of the sel | ||
487 | // while sel is relative to the top of the doc | 484 | // while sel is relative to the top of the doc | ||
488 | QRect rect = d->contentsRect; | 485 | QRect rect = d->contentsRect; | ||
489 | rect.translate (d->oldSelectionPtr->x (), d->oldSelectionPtr->y ()); | 486 | rect.translate (d->oldSelectionPtr->x (), d->oldSelectionPtr->y ()); | ||
490 | 487 | | |||
Show All 36 Lines | 523 | { | |||
527 | d->leftImage, d->rightImage, | 524 | d->leftImage, d->rightImage, | ||
528 | d->topImage, d->botImage, | 525 | d->topImage, d->botImage, | ||
529 | nullptr | 526 | nullptr | ||
530 | }; | 527 | }; | ||
531 | 528 | | |||
532 | const kpImage **p = images; | 529 | const kpImage **p = images; | ||
533 | for (const kpTransformAutoCropBorder **b = borders; *b; b++, p++) | 530 | for (const kpTransformAutoCropBorder **b = borders; *b; b++, p++) | ||
534 | { | 531 | { | ||
535 | if (!(*b)->exists ()) | 532 | if (!(*b)->exists ()) { | ||
536 | continue; | 533 | continue; | ||
534 | } | ||||
537 | 535 | | |||
538 | if ((*b)->isSingleColor ()) | 536 | if ((*b)->isSingleColor ()) | ||
539 | { | 537 | { | ||
540 | kpColor col = (*b)->referenceColor (); | 538 | kpColor col = (*b)->referenceColor (); | ||
541 | #if DEBUG_KP_TOOL_AUTO_CROP && 1 | 539 | #if DEBUG_KP_TOOL_AUTO_CROP && 1 | ||
542 | qCDebug(kpLogImagelib) << "\tdrawing border " << (*b)->rect () | 540 | qCDebug(kpLogImagelib) << "\tdrawing border " << (*b)->rect () | ||
543 | << " rgb=" << (int *) col.toQRgb () /* %X hack */ << endl; | 541 | << " rgb=" << (int *) col.toQRgb () /* %X hack */; | ||
544 | #endif | 542 | #endif | ||
545 | 543 | | |||
546 | const QRect r = (*b)->rect (); | 544 | const QRect r = (*b)->rect (); | ||
547 | kpPainter::fillRect (&image, | 545 | kpPainter::fillRect (&image, | ||
548 | r.x (), r.y (), r.width (), r.height (), | 546 | r.x (), r.y (), r.width (), r.height (), | ||
549 | col); | 547 | col); | ||
550 | } | 548 | } | ||
551 | else | 549 | else | ||
552 | { | 550 | { | ||
553 | #if DEBUG_KP_TOOL_AUTO_CROP && 1 | 551 | #if DEBUG_KP_TOOL_AUTO_CROP && 1 | ||
554 | qCDebug(kpLogImagelib) << "\trestoring border image " << (*b)->rect (); | 552 | qCDebug(kpLogImagelib) << "\trestoring border image " << (*b)->rect (); | ||
555 | #endif | 553 | #endif | ||
556 | if (*p) | 554 | if (*p) | ||
557 | { | 555 | { | ||
558 | // REFACTOR: Add equivalent method to kpPainter and use. | 556 | // REFACTOR: Add equivalent method to kpPainter and use. | ||
559 | kpPixmapFX::setPixmapAt (&image, (*b)->rect (), **p); | 557 | kpPixmapFX::setPixmapAt (&image, (*b)->rect (), **p); | ||
560 | } | 558 | } | ||
561 | } | 559 | } | ||
562 | } | 560 | } | ||
563 | 561 | | |||
564 | 562 | | |||
565 | if (!d->actOnSelection) | 563 | if (!d->actOnSelection) { | ||
566 | doc->setImage (image); | 564 | doc->setImage (image); | ||
565 | } | ||||
567 | else | 566 | else | ||
568 | { | 567 | { | ||
569 | d->oldSelectionPtr->setBaseImage (image); | 568 | d->oldSelectionPtr->setBaseImage (image); | ||
570 | 569 | | |||
571 | doc->setSelection (*d->oldSelectionPtr); | 570 | doc->setSelection (*d->oldSelectionPtr); | ||
572 | delete d->oldSelectionPtr; d->oldSelectionPtr = nullptr; | 571 | delete d->oldSelectionPtr; d->oldSelectionPtr = nullptr; | ||
573 | 572 | | |||
574 | environ ()->somethingBelowTheCursorChanged (); | 573 | environ ()->somethingBelowTheCursorChanged (); | ||
Show All 17 Lines | 589 | d->topBorder.exists () ? | |||
592 | 0); | 591 | 0); | ||
593 | QPoint botRight (d->rightBorder.exists () ? | 592 | QPoint botRight (d->rightBorder.exists () ? | ||
594 | d->rightBorder.rect ().left () - 1 : | 593 | d->rightBorder.rect ().left () - 1 : | ||
595 | image.width () - 1, | 594 | image.width () - 1, | ||
596 | d->botBorder.exists () ? | 595 | d->botBorder.exists () ? | ||
597 | d->botBorder.rect ().top () - 1 : | 596 | d->botBorder.rect ().top () - 1 : | ||
598 | image.height () - 1); | 597 | image.height () - 1); | ||
599 | 598 | | |||
600 | return QRect (topLeft, botRight); | 599 | return {topLeft, botRight}; | ||
601 | } | 600 | } | ||
602 | 601 | | |||
603 | 602 | | |||
604 | static void ShowNothingToAutocropMessage (kpMainWindow *mainWindow, bool actOnSelection) | 603 | static void ShowNothingToAutocropMessage (kpMainWindow *mainWindow, bool actOnSelection) | ||
605 | { | 604 | { | ||
606 | kpSetOverrideCursorSaver cursorSaver (Qt::ArrowCursor); | 605 | kpSetOverrideCursorSaver cursorSaver (Qt::ArrowCursor); | ||
607 | 606 | | |||
608 | if (actOnSelection) | 607 | if (actOnSelection) | ||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Line(s) | 666 | ((numRegions = leftBorder.exists () + | |||
668 | rightBorder.exists () + | 667 | rightBorder.exists () + | ||
669 | topBorder.exists () + | 668 | topBorder.exists () + | ||
670 | botBorder.exists ()) == 0)) | 669 | botBorder.exists ()) == 0)) | ||
671 | { | 670 | { | ||
672 | #if DEBUG_KP_TOOL_AUTO_CROP | 671 | #if DEBUG_KP_TOOL_AUTO_CROP | ||
673 | qCDebug(kpLogImagelib) << "\tcan't find border; leftBorder.rect=" << leftBorder.rect () | 672 | qCDebug(kpLogImagelib) << "\tcan't find border; leftBorder.rect=" << leftBorder.rect () | ||
674 | << " rightBorder.rect=" << rightBorder.rect () | 673 | << " rightBorder.rect=" << rightBorder.rect () | ||
675 | << " topBorder.rect=" << topBorder.rect () | 674 | << " topBorder.rect=" << topBorder.rect () | ||
676 | << " botBorder.rect=" << botBorder.rect () | 675 | << " botBorder.rect=" << botBorder.rect (); | ||
677 | << endl; | | |||
678 | #endif | 676 | #endif | ||
679 | ::ShowNothingToAutocropMessage (mainWindow, static_cast<bool> (doc->selection ())); | 677 | ::ShowNothingToAutocropMessage (mainWindow, static_cast<bool> (doc->selection ())); | ||
680 | return false; | 678 | return false; | ||
681 | } | 679 | } | ||
682 | 680 | | |||
683 | qCDebug(kpLogImagelib) << "\tnumRegions=" << numRegions; | 681 | qCDebug(kpLogImagelib) << "\tnumRegions=" << numRegions; | ||
684 | qCDebug(kpLogImagelib) << "\t\tleft=" << leftBorder.rect () | 682 | qCDebug(kpLogImagelib) << "\t\tleft=" << leftBorder.rect () | ||
685 | << " refCol=" << (leftBorder.exists () ? (int *) leftBorder.referenceColor ().toQRgb () : 0) | 683 | << " refCol=" << (leftBorder.exists () ? (int *) leftBorder.referenceColor ().toQRgb () : nullptr) | ||
686 | << " avgCol=" << (leftBorder.exists () ? (int *) leftBorder.averageColor ().toQRgb () : 0) | 684 | << " avgCol=" << (leftBorder.exists () ? (int *) leftBorder.averageColor ().toQRgb () : nullptr); | ||
687 | << endl; | | |||
688 | qCDebug(kpLogImagelib) << "\t\tright=" << rightBorder.rect () | 685 | qCDebug(kpLogImagelib) << "\t\tright=" << rightBorder.rect () | ||
689 | << " refCol=" << (rightBorder.exists () ? (int *) rightBorder.referenceColor ().toQRgb () : 0) | 686 | << " refCol=" << (rightBorder.exists () ? (int *) rightBorder.referenceColor ().toQRgb () : nullptr) | ||
690 | << " avgCol=" << (rightBorder.exists () ? (int *) rightBorder.averageColor ().toQRgb () : 0) | 687 | << " avgCol=" << (rightBorder.exists () ? (int *) rightBorder.averageColor ().toQRgb () : nullptr); | ||
691 | << endl; | | |||
692 | qCDebug(kpLogImagelib) << "\t\ttop=" << topBorder.rect () | 688 | qCDebug(kpLogImagelib) << "\t\ttop=" << topBorder.rect () | ||
693 | << " refCol=" << (topBorder.exists () ? (int *) topBorder.referenceColor ().toQRgb () : 0) | 689 | << " refCol=" << (topBorder.exists () ? (int *) topBorder.referenceColor ().toQRgb () : nullptr) | ||
694 | << " avgCol=" << (topBorder.exists () ? (int *) topBorder.averageColor ().toQRgb () : 0) | 690 | << " avgCol=" << (topBorder.exists () ? (int *) topBorder.averageColor ().toQRgb () : nullptr); | ||
695 | << endl; | | |||
696 | qCDebug(kpLogImagelib) << "\t\tbot=" << botBorder.rect () | 691 | qCDebug(kpLogImagelib) << "\t\tbot=" << botBorder.rect () | ||
697 | << " refCol=" << (botBorder.exists () ? (int *) botBorder.referenceColor ().toQRgb () : 0) | 692 | << " refCol=" << (botBorder.exists () ? (int *) botBorder.referenceColor ().toQRgb () : nullptr) | ||
698 | << " avgCol=" << (botBorder.exists () ? (int *) botBorder.averageColor ().toQRgb () : 0) | 693 | << " avgCol=" << (botBorder.exists () ? (int *) botBorder.averageColor ().toQRgb () : nullptr); | ||
699 | << endl; | | |||
700 | 694 | | |||
701 | 695 | | |||
702 | // In case e.g. the user pastes a solid, coloured-in rectangle, | 696 | // In case e.g. the user pastes a solid, coloured-in rectangle, | ||
703 | // we favor killing the bottom and right regions | 697 | // we favor killing the bottom and right regions | ||
704 | // (these regions probably contain the unwanted whitespace due | 698 | // (these regions probably contain the unwanted whitespace due | ||
705 | // to the doc being bigger than the pasted selection to start with). | 699 | // to the doc being bigger than the pasted selection to start with). | ||
706 | // | 700 | // | ||
707 | // We also kill if they kiss or even overlap. | 701 | // We also kill if they kiss or even overlap. | ||
Show All 35 Lines |