From patchwork Thu Aug 6 16:36:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 9264 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id D164ABD86F for ; Thu, 6 Aug 2020 16:36:52 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9EF5260552; Thu, 6 Aug 2020 18:36:52 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="HER4ZhkA"; dkim-atps=neutral Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 2E77860392 for ; Thu, 6 Aug 2020 18:36:50 +0200 (CEST) Received: by mail-wr1-x42c.google.com with SMTP id z18so41057293wrm.12 for ; Thu, 06 Aug 2020 09:36:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=+ls6Qseo3A4UXMXVxoS7O1JGhZWS6PpmWFM/xwwZuck=; b=HER4ZhkAT119zwLLMeGX368pTs9Hd5WGps66w6WFdr73exQq9oZJrsKGR1Z0RK3c7p OtQyHvQIkXQtvspi60ADO6k8bqy+vcUBctGu8M3f3/RlOnZQmSCW4bOXlOl5DKGDvdDd /am3hNTX6SpD/kZEk/qkH4tLhgeDBcLRPFSN9kRaowK/GHxducaZfDzWjbFBu+y8Kn0G BKDSTlbAty4yQwHYBFjcaZNHVwAflE1aeEly7XnmVEl16GYnM6RfpgcuSjvq8Cvls7Xt u6bY8CpfPhw+F6gbp2ACqL1enDWzsZupGoqzsY00mJeyNyO1XGDNauLNgqtznQXdK4o6 Va1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=+ls6Qseo3A4UXMXVxoS7O1JGhZWS6PpmWFM/xwwZuck=; b=msu+s7eTytiL4P9XfhoyRfKA7ixAu2n0v08cLWxSjRnQofT2rMfe/xYqpUJGyPIoGG 2SdErbs1xTqfY4clVE8Z8oS0YsimyH5BnA2+zFSb1ToXuSKrr5yqiqoOppMl/Y0SZeAq rtd27ijsKVYIvyFJ3fM2IfzVkEsy3X+/yEIJYCe/U5SiJxxw+8Y3xSGLlVvS9CRrzx+8 Bl1HMv+XIdrNX1JOm18B1hJT/xTSd78dQua56799KnKLAzcVBAvUCQchLjVYTfz2SJ1w IgEFOy0R8WLmpxRFEOs4p+uJx+2K/TED7xZxNOEjiWIy7Yr1ijbzxpavqbDH91WSaRzo q1dQ== X-Gm-Message-State: AOAM530oDWom2vBn/Qdi0NReL7Ykf5bBOyoPVY/mJ5IiE0pZ75H/7n8K 6iP8fFYilNhJ+BFMlZT+Ot6YLghfg/+SDg== X-Google-Smtp-Source: ABdhPJwoaOnG5bMjS50sAkUhjIw3B7Ol22iAlieQzEBsyKUC3V5Vyg2xQaCiIN8vUvZkRrTq05yvAQ== X-Received: by 2002:adf:82f6:: with SMTP id 109mr8976993wrc.25.1596731809059; Thu, 06 Aug 2020 09:36:49 -0700 (PDT) Received: from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72]) by smtp.gmail.com with ESMTPSA id e5sm7405076wrc.37.2020.08.06.09.36.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Aug 2020 09:36:48 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Thu, 6 Aug 2020 17:36:35 +0100 Message-Id: <20200806163639.12971-2-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200806163639.12971-1-david.plowman@raspberrypi.com> References: <20200806163639.12971-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 1/5] libcamera: Add Transform enum to represet 2d plane transforms. X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" We implement 2d transforms as an enum class with 8 elements, consisting of the usual 2d plane transformations (flips, rotations etc.). The transform is made up of 3 bits, indicating whether the transform includes: a transpose, a horizontal flip (mirror) and a vertical flip. --- include/libcamera/meson.build | 1 + include/libcamera/transform.h | 58 ++++++++++++++++++++++++ src/libcamera/meson.build | 1 + src/libcamera/transform.cpp | 83 +++++++++++++++++++++++++++++++++++ 4 files changed, 143 insertions(+) create mode 100644 include/libcamera/transform.h create mode 100644 src/libcamera/transform.cpp diff --git a/include/libcamera/meson.build b/include/libcamera/meson.build index cdb8e03..7fae5e5 100644 --- a/include/libcamera/meson.build +++ b/include/libcamera/meson.build @@ -19,6 +19,7 @@ libcamera_public_headers = files([ 'span.h', 'stream.h', 'timer.h', + 'transform.h', ]) include_dir = join_paths(libcamera_include_dir, 'libcamera') diff --git a/include/libcamera/transform.h b/include/libcamera/transform.h new file mode 100644 index 0000000..658beb9 --- /dev/null +++ b/include/libcamera/transform.h @@ -0,0 +1,58 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Limited + * + * transform.h - Implementation of 2d plane transforms + */ + +#ifndef __LIBCAMERA_TRANSFORM_H__ +#define __LIBCAMERA_TRANSFORM_H__ + +#include + +namespace libcamera { + +enum class Transform : int { + Identity = 0, + Rot0 = Identity, + HFlip = 1, + VFlip = 2, + HVFlip = HFlip | VFlip, + Rot180 = HVFlip, + Transpose = 4, + Rot270 = HFlip | Transpose, + Rot90 = VFlip | Transpose, + Rot180Transpose = HFlip | VFlip | Transpose +}; + +constexpr Transform operator&(Transform t0, Transform t1) +{ + return static_cast(static_cast(t0) & static_cast(t1)); +} + +constexpr Transform operator|(Transform t0, Transform t1) +{ + return static_cast(static_cast(t0) | static_cast(t1)); +} + +constexpr Transform operator^(Transform t0, Transform t1) +{ + return static_cast(static_cast(t0) ^ static_cast(t1)); +} + +Transform operator*(Transform t0, Transform t1); + +Transform operator-(Transform t); + +constexpr bool operator!(Transform t) +{ + return t == Transform::Identity; +} + +Transform transformFromRotation(int angle, bool *success = nullptr); + +std::string transformToString(Transform t); + +} /* namespace libcamera */ + +#endif /* __LIBCAMERA_TRANSFORM_H__ */ diff --git a/src/libcamera/meson.build b/src/libcamera/meson.build index bada45b..b46247d 100644 --- a/src/libcamera/meson.build +++ b/src/libcamera/meson.build @@ -44,6 +44,7 @@ libcamera_sources = files([ 'sysfs.cpp', 'thread.cpp', 'timer.cpp', + 'transform.cpp', 'utils.cpp', 'v4l2_controls.cpp', 'v4l2_device.cpp', diff --git a/src/libcamera/transform.cpp b/src/libcamera/transform.cpp new file mode 100644 index 0000000..5f00a5c --- /dev/null +++ b/src/libcamera/transform.cpp @@ -0,0 +1,83 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2020, Raspberry Pi (Trading) Limited + * + * transform.cpp - implementation of 2d plane transforms. + */ + +#include + +/** + * \file transform.h + * \brief Enum to represent a 2d plane transforms. + */ + +namespace libcamera { + +Transform operator*(Transform t0, Transform t1) +{ + /* + * Reorder the operations so that we imagine doing t1's transpose + * (if any) after t0's flips. The effect is to swap t0's hflips for + * vflips and vice versa, after which we can just xor all the bits. + */ + Transform reordered = t0; + if (!!(t1 & Transform::Transpose)) + reordered = (t0 & Transform::Transpose) | + (!!(t0 & Transform::HFlip) ? Transform::VFlip : Transform::Identity) | + (!!(t0 & Transform::VFlip) ? Transform::HFlip : Transform::Identity); + + return reordered ^ t1; +} + +Transform operator-(Transform t) +{ + /* All are self-inverses, except for Rot270 and Rot90. */ + static const Transform inverses[] = { + Transform::Identity, Transform::HFlip, Transform::VFlip, Transform::HVFlip, + Transform::Transpose, Transform::Rot90, Transform::Rot270, Transform::Rot180Transpose + }; + + return inverses[static_cast(t)]; +} + +Transform transformFromRotation(int angle, bool *success) +{ + angle = angle % 360; + if (angle < 0) + angle += 360; + + if (success != nullptr) + *success = true; + + if (angle == 0) + return Transform::Identity; + else if (angle == 90) + return Transform::Rot90; + else if (angle == 180) + return Transform::Rot180; + else if (angle == 270) + return Transform::Rot270; + else if (success != nullptr) + *success = false; + + return Transform::Identity; +} + +std::string transformToString(Transform t) +{ + static const char *strings[] = { + "identity", + "hflip", + "vflip", + "hvflip", + "transpose", + "rot270", + "rot90", + "rot180transpose" + }; + + return strings[static_cast(t)]; +} + +} /* namespace libcamera */