summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2024-08-07utils: checkstyle.py: Validate SoB trailer against authorLaurent Pinchart
2024-08-07utils: checkstyle.py: Add author property to Commit classLaurent Pinchart
2024-08-07libcamera: Don't add libcamera_public_headers to libcamera_public_sourcesLaurent Pinchart
2024-08-07libcamera: Consolidate tracepoint header in libcamera_internal_headersLaurent Pinchart
2024-08-07libcamera: Add missing headers to libcamera_internal_headersLaurent Pinchart
2024-08-07libcamera: Consolidate all IPA headers in libcamera_ipa_headersLaurent Pinchart
2024-08-07libcamera: Move IPA headers to the libcamera_private dependencyLaurent Pinchart
2024-08-07libcamera: Drop libcamera_generated_ipa_headers from sourcesLaurent Pinchart
2024-08-07libcamera: Add version.h to public headersLaurent Pinchart
2024-08-07libcamera: Split public and internal source arraysDaniel Scally
2024-08-07libcamera: Hide *::Private classes with __DOXYGEN_PUBLIC__Laurent Pinchart
2024-08-07libcamera: Mark internal parts of public classes with \internalLaurent Pinchart
2024-08-07libcamera: Unify Doxygen file directive prefix for formats.hLaurent Pinchart
2024-08-07libcamera: Make all internal headers visible to DoxygenLaurent Pinchart
2024-08-07libcamera: Drop path prefix from Doxygen file directiveLaurent Pinchart
2024-08-05utils: checkstyle.py: Warn when no valid Signed-off-by line is foundLaurent Pinchart
2024-08-05libcamera: software_isp: Replace malloc() with std::vector<>Laurent Pinchart
2024-08-05ipa: libipa: camera_sensor_helper: Reference blackLevel() in documentationLaurent Pinchart
2024-08-05meson: Drop gcc 8 supportLaurent Pinchart
2024-08-05libcamera: Remove spaces at end of linesLaurent Pinchart
2024-08-01Document additional environmental variablesPablo Pita
2024-08-01libcamera: ipa_proxy: Report a missing configuration as a warningMilan Zamazal
2024-08-01libcamera: ipa_proxy: Unify configurationFile argument nameMilan Zamazal
2024-08-01libcamera: simple: Log a missing sensor in a better wayMilan Zamazal
2024-08-01libcamera: software_isp: Remove file seal TODO itemLaurent Pinchart
2024-08-01libcamera: shared_mem_object: Prevent memfd from shrinking or growingLaurent Pinchart
2024-08-01libcamera: base: memfd: Handle uClibc compatibility with function wrapperLaurent Pinchart
2024-08-01libcamera: base: Add MemFd helper classLaurent Pinchart
2024-07-31meson: Enable simple pipeline handler on all platforms by defaultLaurent Pinchart
2024-07-31libcamera: Avoid variable-length arraysLaurent Pinchart
2024-07-30apps: qcam: Disable -Wextra-semiLaurent Pinchart
2024-07-26gstreamer: Fix width and height range handlingHou Qi
2024-07-25libcamera v0.3.1v0.3.1Kieran Bingham
2024-07-25gst: Document libcamerasrc multi stream usageNicolas Dufresne
2024-07-25gst: Add child proxy support to libcamerasrcNicolas Dufresne
2024-07-25libcamera: libipa: camera_sensor: Add onsemi AR0144 sensor propertiesLaurent Pinchart
2024-07-25v4l2: v4l2_compat: Fix ioctl() prototype with musl C libraryLaurent Pinchart
2024-07-25gstreamer: allocator: Ensure camera manager stay aliveNicolas Dufresne
2024-07-25test: gstreamer: Test memory lifetimeNicolas Dufresne
2024-07-25utils: gen-version: Use posix compliant dateZach DeCook
2024-07-25gstreamer: pool: Replace GstAtomicQueue with deque and mutexNicolas Dufresne
2024-07-24ipa: rkisp1: ccm: Ensure metadata contains valid ccmStefan Klug
2024-07-24ipa: rkisp1: ccm: Fix ccm metadata outputStefan Klug
2024-07-24ipa: rkisp1: awb: Unconditionally fill metadataStefan Klug
2024-07-24ipa rkisp1: Remove temperatureK from FrameContextStefan Klug
2024-07-24ipa: rkisp1: awb: Clamp gains to machine limitsStefan Klug
2024-07-24libipa: camera_sensor_helper: Add OV5675 black levelDaniel Semkowicz
2024-07-24apps: qcam: Port to Qt 6Neal Gompa
2024-07-22libcamera: pipeline: simple: Enable Soft ISP for TI CSI-RXJai Luthra
2024-07-21libcamera: v4l2_videodevice: Use bufferType_ in [get|try|set]Format()Hans de Goede
381'>381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
 * Copyright (C) 2019, Google Inc.
 *
 * geometry.cpp - Geometry-related structures
 */

#include <libcamera/geometry.h>

#include <sstream>
#include <stdint.h>

#include "libcamera/internal/log.h"

/**
 * \file geometry.h
 * \brief Data structures related to geometric objects
 */

namespace libcamera {

/**
 * \class Point
 * \brief Describe a point in two-dimensional space
 *
 * The Point structure defines a point in two-dimensional space with integer
 * precision. The coordinates of a Point may be negative as well as positive.
 */

/**
 * \fn Point::Point()
 * \brief Construct a Point with x and y set to 0
 */

/**
 * \fn Point::Point(int xpos, int ypos)
 * \brief Construct a Point at given \a xpos and \a ypos values
 * \param[in] xpos The x-coordinate
 * \param[in] ypos The y-coordinate
 */

/**
 * \var Point::x
 * \brief The x-coordinate of the Point
 */

/**
 * \var Point::y
 * \brief The y-coordinate of the Point
 */

/**
 * \brief Assemble and return a string describing the point
 * \return A string describing the point
 */
const std::string Point::toString() const
{
	std::stringstream ss;

	ss << "(" << x << "," << y << ")";

	return ss.str();
}

/**
 * \fn Point Point::operator-() const
 * \brief Negate a Point by negating both its x and y coordinates
 * \return The negated point
 */

/**
 * \brief Compare points for equality
 * \return True if the two points are equal, false otherwise
 */
bool operator==(const Point &lhs, const Point &rhs)
{
	return lhs.x == rhs.x && lhs.y == rhs.y;
}

/**
 * \fn bool operator!=(const Point &lhs, const Point &rhs)
 * \brief Compare points for inequality
 * \return True if the two points are not equal, false otherwise
 */

/**
 * \struct Size
 * \brief Describe a two-dimensional size
 *
 * The Size structure defines a two-dimensional size with integer precision.
 */

/**
 * \fn Size::Size()
 * \brief Construct a Size with width and height set to 0
 */

/**
 * \fn Size::Size(unsigned int width, unsigned int height)
 * \brief Construct a Size with given \a width and \a height
 * \param[in] width The Size width
 * \param[in] height The Size height
 */

/**
 * \var Size::width
 * \brief The Size width
 */

/**
 * \var Size::height
 * \brief The Size height
 */

/**
 * \fn bool Size::isNull() const
 * \brief Check if the size is null
 * \return True if both the width and height are 0, or false otherwise
 */

/**
 * \brief Assemble and return a string describing the size
 * \return A string describing the size
 */
const std::string Size::toString() const
{
	return std::to_string(width) + "x" + std::to_string(height);
}

/**
 * \fn Size::alignDownTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size down horizontally and vertically in place
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 *
 * This functions rounds the width and height down to the nearest multiple of
 * \a hAlignment and \a vAlignment respectively.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::alignUpTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size up horizontally and vertically in place
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 *
 * This functions rounds the width and height up to the nearest multiple of
 * \a hAlignment and \a vAlignment respectively.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::boundTo(const Size &bound)
 * \brief Bound the size to \a bound in place
 * \param[in] bound The maximum size
 *
 * This function sets the width and height to the minimum of this size and the
 * \a bound size.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::expandTo(const Size &expand)
 * \brief Expand the size to \a expand
 * \param[in] expand The minimum size
 *
 * This function sets the width and height to the maximum of this size and the
 * \a expand size.
 *
 * \return A reference to this object
 */

/**
 * \fn Size::alignedDownTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size down horizontally and vertically
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 * \return A Size whose width and height are equal to the width and height of
 * this size rounded down to the nearest multiple of \a hAlignment and
 * \a vAlignment respectively
 */

/**
 * \fn Size::alignedUpTo(unsigned int hAlignment, unsigned int vAlignment)
 * \brief Align the size up horizontally and vertically
 * \param[in] hAlignment Horizontal alignment
 * \param[in] vAlignment Vertical alignment
 * \return A Size whose width and height are equal to the width and height of
 * this size rounded up to the nearest multiple of \a hAlignment and
 * \a vAlignment respectively
 */

/**
 * \fn Size::boundedTo(const Size &bound)
 * \brief Bound the size to \a bound
 * \param[in] bound The maximum size
 * \return A Size whose width and height are the minimum of the width and
 * height of this size and the \a bound size
 */

/**
 * \fn Size::expandedTo(const Size &expand)
 * \brief Expand the size to \a expand
 * \param[in] expand The minimum size
 * \return A Size whose width and height are the maximum of the width and
 * height of this size and the \a expand size
 */

/**
 * \brief Bound the size down to match the aspect ratio given by \a ratio
 * \param[in] ratio The size whose aspect ratio must be matched
 *
 * The behaviour of this function is undefined if either the width or the
 * height of the \a ratio is zero.
 *
 * \return A Size whose width and height are equal to the width and height
 * of this Size aligned down to the aspect ratio of \a ratio
 */
Size Size::boundedToAspectRatio(const Size &ratio) const
{
	ASSERT(ratio.width && ratio.height);

	uint64_t ratio1 = static_cast<uint64_t>(width) *
			  static_cast<uint64_t>(ratio.height);
	uint64_t ratio2 = static_cast<uint64_t>(ratio.width) *
			  static_cast<uint64_t>(height);

	if (ratio1 > ratio2)
		return { static_cast<unsigned int>(ratio2 / ratio.height), height };
	else
		return { width, static_cast<unsigned int>(ratio1 / ratio.width) };
}

/**
 * \brief Expand the size to match the aspect ratio given by \a ratio
 * \param[in] ratio The size whose aspect ratio must be matched
 *
 * The behaviour of this function is undefined if either the width or the
 * height of the \a ratio is zero.
 *
 * \return A Size whose width and height are equal to the width and height
 * of this Size expanded up to the aspect ratio of \a ratio
 */
Size Size::expandedToAspectRatio(const Size &ratio) const
{
	ASSERT(ratio.width && ratio.height);

	uint64_t ratio1 = static_cast<uint64_t>(width) *
			  static_cast<uint64_t>(ratio.height);
	uint64_t ratio2 = static_cast<uint64_t>(ratio.width) *
			  static_cast<uint64_t>(height);

	if (ratio1 < ratio2)
		return { static_cast<unsigned int>(ratio2 / ratio.height), height };
	else
		return { width, static_cast<unsigned int>(ratio1 / ratio.width) };
}

/**
 * \brief Center a rectangle of this size at a given Point
 * \param[in] center The center point the Rectangle is to have
 *
 * A Rectangle of this object's size is positioned so that its center
 * is at the given Point.
 *
 * \return A Rectangle of this size, centered at the given Point.
 */
Rectangle Size::centeredTo(const Point &center) const
{
	int x = center.x - width / 2;
	int y = center.y - height / 2;

	return { x, y, width, height };
}

/**
 * \brief Scale size up by the given factor
 * \param[in] factor The factor
 * \return The scaled Size
 */
Size Size::operator*(float factor) const
{
	return Size(width * factor, height * factor);
}

/**
 * \brief Scale size down by the given factor
 * \param[in] factor The factor
 * \return The scaled Size
 */
Size Size::operator/(float factor) const
{
	return Size(width / factor, height / factor);
}

/**
 * \brief Scale this size up by the given factor in place
 * \param[in] factor The factor
 * \return A reference to this object
 */
Size &Size::operator*=(float factor)
{
	width *= factor;
	height *= factor;
	return *this;
}

/**
 * \brief Scale this size down by the given factor in place
 * \param[in] factor The factor
 * \return A reference to this object
 */
Size &Size::operator/=(float factor)
{
	width /= factor;
	height /= factor;
	return *this;
}

/**
 * \brief Compare sizes for equality
 * \return True if the two sizes are equal, false otherwise
 */
bool operator==(const Size &lhs, const Size &rhs)
{
	return lhs.width == rhs.width && lhs.height == rhs.height;
}

/**
 * \brief Compare sizes for smaller than order
 *
 * Sizes are compared on three criteria, in the following order.
 *
 * - A size with smaller width and smaller height is smaller.
 * - A size with smaller area is smaller.
 * - A size with smaller width is smaller.
 *
 * \return True if \a lhs is smaller than \a rhs, false otherwise
 */
bool operator<(const Size &lhs, const Size &rhs)
{
	if (lhs.width < rhs.width && lhs.height < rhs.height)
		return true;
	else if (lhs.width >= rhs.width && lhs.height >= rhs.height)
		return false;

	uint64_t larea = static_cast<uint64_t>(lhs.width) *
			 static_cast<uint64_t>(lhs.height);
	uint64_t rarea = static_cast<uint64_t>(rhs.width) *
			 static_cast<uint64_t>(rhs.height);
	if (larea < rarea)
		return true;
	else if (larea > rarea)
		return false;

	return lhs.width < rhs.width;
}

/**
 * \fn bool operator!=(const Size &lhs, const Size &rhs)
 * \brief Compare sizes for inequality
 * \return True if the two sizes are not equal, false otherwise
 */

/**
 * \fn bool operator<=(const Size &lhs, const Size &rhs)
 * \brief Compare sizes for smaller than or equal to order
 * \return True if \a lhs is smaller than or equal to \a rhs, false otherwise
 * \sa bool operator<(const Size &lhs, const Size &rhs)
 */

/**
 * \fn bool operator>(const Size &lhs, const Size &rhs)
 * \brief Compare sizes for greater than order
 * \return True if \a lhs is greater than \a rhs, false otherwise
 * \sa bool operator<(const Size &lhs, const Size &rhs)
 */

/**
 * \fn bool operator>=(const Size &lhs, const Size &rhs)
 * \brief Compare sizes for greater than or equal to order
 * \return True if \a lhs is greater than or equal to \a rhs, false otherwise
 * \sa bool operator<(const Size &lhs, const Size &rhs)
 */

/**
 * \struct SizeRange
 * \brief Describe a range of sizes
 *
 * A SizeRange describes a range of sizes included in the [min, max] interval
 * for both the width and the height. If the minimum and maximum sizes are
 * identical it represents a single size.
 *
 * Size ranges may further limit the valid sizes through steps in the horizontal
 * and vertical direction. The step values represent the increase in pixels
 * between two valid width or height values, starting from the minimum. Valid
 * sizes within the range are thus expressed as
 *