From patchwork Tue Aug 27 09:50:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jacopo Mondi X-Patchwork-Id: 1863 X-Patchwork-Delegate: jacopo@jmondi.org Return-Path: Received: from relay11.mail.gandi.net (relay11.mail.gandi.net [217.70.178.231]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id BB6A160C1E for ; Tue, 27 Aug 2019 11:48:49 +0200 (CEST) Received: from uno.homenet.telecomitalia.it (unknown [87.18.63.98]) (Authenticated sender: jacopo@jmondi.org) by relay11.mail.gandi.net (Postfix) with ESMTPSA id 0EB59100008; Tue, 27 Aug 2019 09:48:48 +0000 (UTC) From: Jacopo Mondi To: libcamera-devel@lists.libcamera.org Date: Tue, 27 Aug 2019 11:50:07 +0200 Message-Id: <20190827095008.11405-8-jacopo@jmondi.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827095008.11405-1-jacopo@jmondi.org> References: <20190827095008.11405-1-jacopo@jmondi.org> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 7/7] libcamera: camera_sensor: Retrieve sensor sizes X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Aug 2019 09:48:49 -0000 Retrieve the camera sensor pixel array sizes and the active pixel area using the V4L2Subdevice selection API. Signed-off-by: Jacopo Mondi --- include/libcamera/geometry.h | 1 + src/libcamera/camera_sensor.cpp | 51 +++++++++++++++++++++++++++ src/libcamera/geometry.cpp | 24 +++++++++++++ src/libcamera/include/camera_sensor.h | 4 +++ 4 files changed, 80 insertions(+) diff --git a/include/libcamera/geometry.h b/include/libcamera/geometry.h index 52f4d010de76..b91fcfa1dd17 100644 --- a/include/libcamera/geometry.h +++ b/include/libcamera/geometry.h @@ -42,6 +42,7 @@ struct Size { unsigned int height; const std::string toString() const; + bool contains(const Rectangle &rectangle) const; }; bool operator==(const Size &lhs, const Size &rhs); diff --git a/src/libcamera/camera_sensor.cpp b/src/libcamera/camera_sensor.cpp index fc7fdcdcaf5b..b6ccaae0930b 100644 --- a/src/libcamera/camera_sensor.cpp +++ b/src/libcamera/camera_sensor.cpp @@ -128,6 +128,28 @@ int CameraSensor::init() } rotation_ = value; + /* + * Retrieve and store the sensor pixel array size and active area + * rectangle. + */ + Rectangle rect; + ret = subdev_->getSelection(0, V4L2_SEL_TGT_NATIVE_SIZE, &rect); + if (ret) + return ret; + pixelArray_.width = rect.w; + pixelArray_.height = rect.h; + + ret = subdev_->getSelection(0, V4L2_SEL_TGT_CROP_BOUNDS, &rect); + if (ret) + return ret; + + if (!pixelArray_.contains(rect)) { + LOG(CameraSensor, Error) + << "Invalid camera sensor pixel array dimension"; + return -EINVAL; + } + activeArea_ = rect; + /* Enumerate and cache media bus codes and sizes. */ const ImageFormats formats = subdev_->formats(0); if (formats.isEmpty()) { @@ -192,6 +214,35 @@ const Size &CameraSensor::resolution() const return sizes_.back(); } +/** + * \fn CameraSensor::pixelArray() + * \brief Retrieve the camera sensor pixel array dimension + * \return The number of horizontal and vertical pixel units installed on the + * camera sensor + * + * The returned pixel array size is the number of pixels units physically + * installed on the sensor, including non-active ones, such as pixels used + * for calibration or testing. + */ + +/** + * \fn CameraSensor::activeArea() + * \brief Retrieve the active pixels area + * \return A rectangle describing the active pixel area + * + * The returned rectangle is contained in the larger pixel array area, + * returned by the CameraSensor::pixelArray() operation. + * + * \todo This operation collides with CameraSensor::resolution() as they + * both reports the same information (bugs in the driver code apart). + * Also, for some sensors, the maximum available resolution might depend on + * the image size ratio (ie the maximumx resolution for images in 4:3 format + * is different from the maximum available resolution for 16:9 format). This + * has to be sorted out as well. + * + * \sa Size::contains() + */ + /** * \brief Retrieve the best sensor format for a desired output * \param[in] mbusCodes The list of acceptable media bus codes diff --git a/src/libcamera/geometry.cpp b/src/libcamera/geometry.cpp index 32b0faeadc63..9b60768a57a7 100644 --- a/src/libcamera/geometry.cpp +++ b/src/libcamera/geometry.cpp @@ -116,6 +116,30 @@ const std::string Size::toString() const return std::to_string(width) + "x" + std::to_string(height); } +/** + * \fn Size::contains() + * \param rectangle The rectangle to verify + * \return True if \a rectangle is contained in the area represented by this + * Size, false otherwise + * + * Return true if \a rectangle is completely contained in the area represented + * by the width and height of this Size, with its top left corner assumed to be + * at coordinates (0, 0). The rectangle 'x' and 'y' fields represent the + * horizontal and vertical positive displacement from the Size top left corner, + * from where the rectangle's horizontal and vertical sizes are calculated. + */ +bool Size::contains(const Rectangle &rectangle) const +{ + if (rectangle.x < 0 || rectangle.y < 0) + return false; + + if (rectangle.x + rectangle.w > width || + rectangle.y + rectangle.h > height) + return false; + + return true; +} + /** * \brief Compare sizes for equality * \return True if the two sizes are equal, false otherwise diff --git a/src/libcamera/include/camera_sensor.h b/src/libcamera/include/camera_sensor.h index 32d39127b275..abf5344cf9d8 100644 --- a/src/libcamera/include/camera_sensor.h +++ b/src/libcamera/include/camera_sensor.h @@ -38,6 +38,8 @@ public: const std::vector &mbusCodes() const { return mbusCodes_; } const std::vector &sizes() const { return sizes_; } const Size &resolution() const; + const Size pixelArray() const { return pixelArray_; } + const Rectangle activeArea() const { return activeArea_; } V4L2SubdeviceFormat getFormat(const std::vector &mbusCodes, const Size &size) const; @@ -59,6 +61,8 @@ private: CameraLocation location_; unsigned int rotation_; + Size pixelArray_; + Rectangle activeArea_; }; } /* namespace libcamera */