{"id":16633,"url":"https://patchwork.libcamera.org/api/1.1/patches/16633/?format=json","web_url":"https://patchwork.libcamera.org/patch/16633/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20220714152409.9780-7-naush@raspberrypi.com>","date":"2022-07-14T15:24:07","name":"[libcamera-devel,v5,6/8] utils: raspberrypi: Add tuning file conversion script","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"737591023e5eb2354e991785ce2f4a15fa5f309e","submitter":{"id":34,"url":"https://patchwork.libcamera.org/api/1.1/people/34/?format=json","name":"Naushir Patuck","email":"naush@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/16633/mbox/","series":[{"id":3285,"url":"https://patchwork.libcamera.org/api/1.1/series/3285/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3285","date":"2022-07-14T15:24:01","name":"Replace boost JSON parser with libyaml in Raspberry Pi IPA","version":5,"mbox":"https://patchwork.libcamera.org/series/3285/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/16633/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/16633/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id A9669BD1F1\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 14 Jul 2022 15:24:27 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3CD5C63327;\n\tThu, 14 Jul 2022 17:24:27 +0200 (CEST)","from mail-wm1-x332.google.com (mail-wm1-x332.google.com\n\t[IPv6:2a00:1450:4864:20::332])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 769DB63318\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 14 Jul 2022 17:24:22 +0200 (CEST)","by mail-wm1-x332.google.com with SMTP id ay25so1265482wmb.1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 14 Jul 2022 08:24:22 -0700 (PDT)","from naush-laptop.localdomain ([93.93.133.154])\n\tby smtp.gmail.com with ESMTPSA id\n\th14-20020a1ccc0e000000b003a2e7c13a3asm2322476wmb.42.2022.07.14.08.24.20\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tThu, 14 Jul 2022 08:24:20 -0700 (PDT)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1657812267;\n\tbh=aRQLgXOOl5tP0UfobEXAX64m0ppM/5b594xVTJdupLk=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=RNlE5ZBaIGtb1wHEReKbFGb0uJa0ahjG6thECPRNQH2K3iTHQb8O4s3JgND95JAkY\n\twSW/oIFPAFGSzVOIp6L7SsuJmdfKyW+OHFfNlTwHGqDWqfzQYb/UtPJvJckXZHI+UU\n\tu6PtobAn3+/oFuZqCfDJeHCAqmWJgTpxAW+NzTGcmQhQKrN3RcKKMN1id6S8I/sHuy\n\t47EjEguFC4sBPlNGuewdtfXjqbbwa3N6LE3sYU2qbAS8iwoBX2MVxudHJ+rPUGev59\n\tmR/JIWOxF5iqC3CLHlKb96Qeu6rd5u9By9/KtsgP/qhqCHF3kxGgwHA0NEeWcjq/KY\n\tDpPxDbzBLFJXg==","v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=r/sF2eU2UliSzf+2+JVSBBZmVC5drTcI+/bKTQNrWhc=;\n\tb=VxzSyoRC8Z09/uI2oUn0dynpTKFxSoR0cm2q/9ztvq+5VOxcBSz36dYe2af3KivHs3\n\tuzKZ0sprjYwJVPsKKZIN+/BLbM3/PbNKKpoIoni0W7y/cw9ZxnVet9oMZc2XGuCbq7m9\n\tVR4+5zzh8lZZgvUd9CtvM6/hjbWKvxuFmnsQFlQW1HgmwDw4/8+MfTLxzVjLJzFBGFAX\n\tmNhFjRKKnEzM5vRzNRJTddLYGSrJGweDwDwEYGTzg1UqqEsgvZ1r13SH8dWdWcFIXEZV\n\t6CLD727ekTLAWbleaj92GMZkWu8IKQ8Ia6VdSM/QBK4M25deDpu8486khOepSH2U77BS\n\tvPZQ=="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (2048-bit key; \n\tunprotected) header.d=raspberrypi.com\n\theader.i=@raspberrypi.com\n\theader.b=\"VxzSyoRC\"; dkim-atps=neutral","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=r/sF2eU2UliSzf+2+JVSBBZmVC5drTcI+/bKTQNrWhc=;\n\tb=htM+qIvIFafV8GoJWZN+fBzy8gt2I0h0R0vFlzP5zI6CeONcqdN6/h/xFlKaz8w4+J\n\tDbRWVlAPXDMpudLh0GOa/pdRV90YKDKv0xsrdJ8KgWjqzAqffENq/H0xbrd2WB++IH4W\n\tTkOc/zKSCiR8fTm4eV+LK6gaDvT8kvuQksQAYcnaAGXvJduA0akQlRoWh/842B/1qPGw\n\t3q7STKyMbrnma07BnHSxs9D7l2t49IhSVCM8K8fjyojv/87kW3U0EeeI72o0PmfFJgQf\n\toxydXwBDBglsp5PPkvERLFd+jG6VnZ9x34B4U2/lkamQ6ajJtXi7OfOI+vsJ7Czi76A/\n\tujwQ==","X-Gm-Message-State":"AJIora9Mr7pDc+XH7ZBMEf8CdALf69PVQdec0mv4rz25hfP3qVxHIu3z\n\t9rvBrZQigvYl5JKwl8Tj1xkHXZcLF14jHw==","X-Google-Smtp-Source":"AGRyM1tDHPHHkqxzFSnvKAfZIHpe9YSO3RxMaODa/jX7YB+PC0oEsVE8KHmm6kBHy2gDvWTvtlEIlw==","X-Received":"by 2002:a05:600c:154a:b0:3a1:70dd:9a12 with SMTP id\n\tf10-20020a05600c154a00b003a170dd9a12mr16253617wmg.70.1657812261715; \n\tThu, 14 Jul 2022 08:24:21 -0700 (PDT)","To":"libcamera-devel@lists.libcamera.org","Date":"Thu, 14 Jul 2022 16:24:07 +0100","Message-Id":"<20220714152409.9780-7-naush@raspberrypi.com>","X-Mailer":"git-send-email 2.25.1","In-Reply-To":"<20220714152409.9780-1-naush@raspberrypi.com>","References":"<20220714152409.9780-1-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v5 6/8] utils: raspberrypi: Add tuning\n\tfile conversion script","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","From":"Naushir Patuck via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Naushir Patuck <naush@raspberrypi.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Add a script to convert the Raspberry Pi camera tuning file format from version\n1.0 to 2.0.\n\nThe version 1.0 format was originally used with the boost JSON parser that\nhappen to provided algorithm ordering based on the ordering in the file. The new\nformat provides implicit ordering by having the algorithms listed in an array.\n\nThis script also adds a root level version key set to 2.0 to the config file,\nallowing the controller to distinguish between the two formats.\n\nSigned-off-by: Naushir Patuck <naush@raspberrypi.com>\n---\n utils/raspberrypi/ctt/convert_tuning.py | 117 ++++++++++++++++++++++++\n 1 file changed, 117 insertions(+)\n create mode 100755 utils/raspberrypi/ctt/convert_tuning.py","diff":"diff --git a/utils/raspberrypi/ctt/convert_tuning.py b/utils/raspberrypi/ctt/convert_tuning.py\nnew file mode 100755\nindex 000000000000..c915bcb46f64\n--- /dev/null\n+++ b/utils/raspberrypi/ctt/convert_tuning.py\n@@ -0,0 +1,117 @@\n+#!/bin/python3\n+# Script to convert version 1.0 Raspberry Pi camera tuning files to version 2.0\n+# and later.\n+#\n+# Copyright 2022 Raspberry Pi Ltd.\n+\n+import argparse\n+import json\n+import textwrap\n+\n+\n+class Encoder(json.JSONEncoder):\n+\n+    def __init__(self, *args, **kwargs):\n+        super().__init__(*args, **kwargs)\n+        self.indentation_level = 0\n+        self.hard_break = 120\n+        self.custom_elems = {\n+            'table': 16,\n+            'luminance_lut': 16,\n+            'ct_curve': 3,\n+            'ccm': 3\n+        }\n+\n+    def encode(self, o, node_key=None):\n+        if isinstance(o, (list, tuple)):\n+            # Check if we are a flat list of numbers.\n+            if not any(isinstance(el, (list, tuple, dict)) for el in o):\n+                s = ', '.join(json.dumps(el) for el in o)\n+                if node_key in self.custom_elems.keys():\n+                    # Special case handling to specify number of elements in a row for tables, ccm, etc.\n+                    self.indentation_level += 1\n+                    sl = s.split(', ')\n+                    num = self.custom_elems[node_key]\n+                    chunk = [self.indent_str + ', '.join(sl[x:x + num]) for x in range(0, len(sl), num)]\n+                    t = ',\\n'.join(chunk)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                elif len(s) > self.hard_break - len(self.indent_str):\n+                    # Break a long list with wraps.\n+                    self.indentation_level += 1\n+                    t = textwrap.fill(s, self.hard_break, break_long_words=False,\n+                                      initial_indent=self.indent_str, subsequent_indent=self.indent_str)\n+                    self.indentation_level -= 1\n+                    output = f'\\n{self.indent_str}[\\n{t}\\n{self.indent_str}]'\n+                else:\n+                    # Smaller lists can remain on a single line.\n+                    output = f' [ {s} ]'\n+                return output\n+            else:\n+                # Sub-structures in the list case.\n+                self.indentation_level += 1\n+                output = [self.indent_str + self.encode(el) for el in o]\n+                self.indentation_level -= 1\n+                output = ',\\n'.join(output)\n+                return f' [\\n{output}\\n{self.indent_str}]'\n+\n+        elif isinstance(o, dict):\n+            self.indentation_level += 1\n+            output = []\n+            for k, v in o.items():\n+                if isinstance(v, dict) and len(v) == 0:\n+                    # Empty config block special case.\n+                    output.append(self.indent_str + f'{json.dumps(k)}: {{ }}')\n+                else:\n+                    # Only linebreak if the next node is a config block.\n+                    sep = f'\\n{self.indent_str}' if isinstance(v, dict) else ''\n+                    output.append(self.indent_str + f'{json.dumps(k)}:{sep}{self.encode(v, k)}')\n+            output = ',\\n'.join(output)\n+            self.indentation_level -= 1\n+            return f'{{\\n{output}\\n{self.indent_str}}}'\n+\n+        else:\n+            return ' ' + json.dumps(o)\n+\n+    @property\n+    def indent_str(self) -> str:\n+        return ' ' * self.indentation_level * self.indent\n+\n+    def iterencode(self, o, **kwargs):\n+        return self.encode(o)\n+\n+\n+def convert_v2(in_json):\n+\n+    ver = 1.0 if 'version' not in in_json.keys() else in_json['version']\n+\n+    if ver == 1.0:\n+        converted = {}\n+        converted['version'] = 2.0\n+        converted['algorithms'] = []\n+\n+        for k, v in in_json.items():\n+            if k == 'version':\n+                continue\n+            converted['algorithms'].append(dict(zip([k], [v])))\n+    else:\n+        converted = in_json\n+\n+    return json.dumps(converted, cls=Encoder, indent=4, sort_keys=False)\n+\n+\n+if __name__ == \"__main__\":\n+    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=\n+                    'Convert the format of the Raspberry Pi camera tuning file from v1.0 to v2.0.\\n'\n+                    'If a v2.0 format file is provided, the tool prettifies its contents.')\n+    parser.add_argument('input', type=str, nargs=1, help='Input tuning file')\n+    parser.add_argument('output', type=str, nargs='?', help='Output converted tuning file', default=None)\n+    args = parser.parse_args()\n+\n+    with open(args.input[0], 'r') as f:\n+        in_json = json.load(f)\n+\n+    out_json = convert_v2(in_json)\n+\n+    with open(args.output if args.output is not None else args.input[0], 'w') as f:\n+        f.write(out_json)\n","prefixes":["libcamera-devel","v5","6/8"]}