Patch Detail
Show a patch.
GET /api/patches/13190/?format=api
{ "id": 13190, "url": "https://patchwork.libcamera.org/api/patches/13190/?format=api", "web_url": "https://patchwork.libcamera.org/patch/13190/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/projects/1/?format=api", "name": "libcamera", "link_name": "libcamera", "list_id": "libcamera_core", "list_email": "libcamera-devel@lists.libcamera.org", "web_url": "", "scm_url": "", "webscm_url": "" }, "msgid": "<tencent_FF0DB70FBE0E0D89696050DFCEEEA04DAA08@qq.com>", "date": "2021-08-04T07:33:46", "name": "[libcamera-devel,RFC,v1,2/3] pipeline: isp: The software ISP class", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "709b744f9df1d23faa521860ab35651fbaedda8c", "submitter": { "id": 88, "url": "https://patchwork.libcamera.org/api/people/88/?format=api", "name": "Siyuan Fan", "email": "siyuan.fan@foxmail.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/13190/mbox/", "series": [ { "id": 2299, "url": "https://patchwork.libcamera.org/api/series/2299/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2299", "date": "2021-08-04T07:33:44", "name": "pipeline: isp: The software ISP Module", "version": 1, "mbox": "https://patchwork.libcamera.org/series/2299/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/13190/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/13190/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 1450EC3235\n\tfor <parsemail@patchwork.libcamera.org>;\n\tWed, 4 Aug 2021 07:34:07 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DE6B168888;\n\tWed, 4 Aug 2021 09:34:05 +0200 (CEST)", "from out162-62-57-87.mail.qq.com (out162-62-57-87.mail.qq.com\n\t[162.62.57.87])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id E24AC6880F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 4 Aug 2021 09:33:58 +0200 (CEST)", "from localhost.localdomain ([123.126.82.9])\n\tby newxmesmtplogicsvrsza5.qq.com (NewEsmtp) with SMTP\n\tid 8722689A; Wed, 04 Aug 2021 15:33:50 +0800" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=foxmail.com header.i=@foxmail.com\n\theader.b=\"hlsL2Qzz\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com;\n\ts=s201512; t=1628062436;\n\tbh=cDfBCdx3KpPINQMCVlKW7BBE9zwzOZtJCpQ0x30VTuk=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References;\n\tb=hlsL2QzzK/f8rmndVqFkqUcEuZpskrihaewpQWTO/sJ5X7XQaEz7ohGOEGIQSP41A\n\tRfQR5mwDmd71AxhUrD9+WSUAMh1RN9f9LFfOM1/S9mXPqAbK5Ml8NhBjK29eO+5bGE\n\tZalpwYCFinVkOAKlecG5XLc3oVXrSlpxrDB8av/U=", "X-QQ-mid": "xmsmtpt1628062433t8qqkh1qy", "Message-ID": "<tencent_FF0DB70FBE0E0D89696050DFCEEEA04DAA08@qq.com>", "X-QQ-XMAILINFO": "NOIkHYnr7VzdUfjOSQ3+N4cGBQZfJu/bkiCYSdew0OTWpOt/XF/zQ+2atIL2kM\n\tRGbBxeaoAoaqfoR11xZ60/2ixO8qog6BLpGNOK6+0J78Qd2Ko50EVeoBooEMfJEmZZoukE1exdif\n\tVjOYN/JNyJIbCfJxpXcHBo/Ox2MCyOJmTpfRdv4U5fosXXtx3f8WE1HbKqNDeqSzXMxc3bNINdUo\n\txlahi6k8MgWyB9DTbvV65ktsaEdSobubtpOVKNFzAHSgvATWhrP1LhDm3Tb3euddRtBHPc411yyC\n\t9seUn5IyrbVSZIISmIHMjtDhplOM9bop8pZydw86ldFLpMuyQYyqRYGt7pTxYZNto4qTMHxSL0pX\n\taECtFn7Y0TO5+3VSgde+l3nRGNouWI883ggYBtUZIuY2Hi/mevRNJT4AZ/8oCwpnPfBKREIv/bxv\n\t9AK3UABBcQeXflZDk0nDufPsdxwmWUElXnnN/dZCY/RLZxkvbQ7KEN/BJur76L2TDpXqZW9O2WqW\n\tl9Nxx6SP4e0Yb8A5cazfTvVS3FQ1uk9ojg2qsiJbXv4Di73aBGatX72/9obDG4HirPZ6sAWaBnqQ\n\tozOfUGF3jaJngGcMTkYP6Z5GJJh//yLTUJK99VVXgdreroKjXKAeAodhL84W6HUyx+6yhDGxtZgm\n\trD42cXlUB0aaisB0l24BJhodNPphsH0Aap5ylJu4ACWZHOYBnL47SIMxYgZt980MQ2pcvfMc5Uin\n\twy0PAgcsJWtORCNmWVFVIN3Ldm1ht16ouGS2YLTQbKZH8EaJUMSQzOckpPQGiXgoJbOPMRliarG/\n\twX+FAjW2ke4PViy7r7ErtcvTTZq6KLceXbbOVif0HA0A7lGXnMBx5/IfSVRaa+nz/fl6+F7ujAPf\n\t65LpT1/qVdWke4GP5Q14QzVwPaqdD3Qr15wdsxsARIK6MJRWDSXmGuwI9TUfebufgWdVVsyM6xAT\n\tp/+AGwEhgYSkohI93r0sskoeKhmBicyplbEbxg9qyJamKi6d/aMg==", "From": "Siyuan Fan <siyuan.fan@foxmail.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Wed, 4 Aug 2021 08:33:46 +0100", "X-OQ-MSGID": "<20210804073347.1368-3-siyuan.fan@foxmail.com>", "X-Mailer": "git-send-email 2.20.1", "In-Reply-To": "<20210804073347.1368-1-siyuan.fan@foxmail.com>", "References": "<20210804073347.1368-1-siyuan.fan@foxmail.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [RFC PATCH v1 2/3] pipeline: isp: The software\n\tISP class", "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>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "From: Fan Siyuan <siyuan.fan@foxmail.com>\n\nAdd the software ISP class only CPU-based. So far the basic alogrithm\nincludes black level correct, bilinear demosaic interpolation, auto \nwhite balance(gray world), tone mapping(auto contrast), gamma correct.\nbilateral filter, 10bit to 8bit compression.\n\nSigned-off-by: Fan Siyuan <siyuan.fan@foxmail.com>\n\n---\n src/libcamera/pipeline/isp/isp_processing.cpp | 593 ++++++++++++++++++\n src/libcamera/pipeline/isp/isp_processing.h | 62 ++\n 2 files changed, 655 insertions(+)\n create mode 100644 src/libcamera/pipeline/isp/isp_processing.cpp\n create mode 100644 src/libcamera/pipeline/isp/isp_processing.h", "diff": "diff --git a/src/libcamera/pipeline/isp/isp_processing.cpp b/src/libcamera/pipeline/isp/isp_processing.cpp\nnew file mode 100644\nindex 00000000..94e8081d\n--- /dev/null\n+++ b/src/libcamera/pipeline/isp/isp_processing.cpp\n@@ -0,0 +1,593 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Siyuan Fan <siyuan.fan@foxmail.com>\n+ *\n+ * isp_processing.cpp - The software ISP class\n+ */\n+\n+#include \"isp_processing.h\"\n+\n+#include <math.h>\n+#include <stdlib.h>\n+#include <string.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+\n+#include <libcamera/request.h>\n+\n+#include \"libcamera/base/log.h\"\n+\n+namespace libcamera{\n+\n+LOG_DECLARE_CATEGORY(ISP)\n+\n+void ISP::autoContrast(uint16_t *data, float lowCut, float highCut, int width, int height)\n+{\n+ int blue, gr, gb, red;\n+ int histBlue[1024] = {0}, histGb[1024] = {0}, histGr[1024] = {0}, histRed[1024] = {0};\n+\n+ int index;\n+ for (int i = 0; i < height; i++) {\n+ index = i * width;\n+ for (int j = 0; j < width; j++) {\n+ if (i % 2 == 0 && j % 2 == 0) {\n+ blue = data[index];\n+ histBlue[blue]++;\n+ }\n+ else if ((i % 2 == 0 && j % 2 == 1)) {\n+ gb = data[index];\n+ histGb[gb]++;\n+ }\n+ else if ((i % 2 == 1 && j % 2 == 0)) {\n+ gr = data[index];\n+ histGr[gr]++;\n+ }\n+ else {\n+ red = data[index];\n+ histRed[red]++;\n+ }\n+ index++;\n+ } \n+ }\n+\n+ int pixelAmount = width * height;\n+ int sum = 0;\n+ int minBlue;\n+ for (int i = 0; i < 1024; i++){\n+ sum = sum + histBlue[i];\n+ if (sum >= pixelAmount * lowCut * 0.01) {\n+ minBlue = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int maxBlue;\n+ for (int i = 1023; i >= 0; i--){\n+ sum = sum + histBlue[i];\n+ if (sum >= pixelAmount * highCut * 0.01) {\n+ maxBlue = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int minGb;\n+ for (int i = 0; i < 1024; i++){\n+ sum = sum + histGb[i];\n+ if (sum >= pixelAmount * lowCut * 0.01) {\n+ minGb = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int maxGb;\n+ for (int i = 1023; i >= 0; i--){\n+ sum = sum + histGb[i];\n+ if (sum >= pixelAmount * highCut * 0.01) {\n+ maxGb = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int minGr;\n+ for (int i = 0; i < 1024; i++){\n+ sum = sum + histGr[i];\n+ if (sum >= pixelAmount * lowCut * 0.01) {\n+ minGr = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int maxGr;\n+ for (int i = 1023; i >= 0; i--){\n+ sum = sum + histGr[i];\n+ if (sum >= pixelAmount * highCut * 0.01) {\n+ maxGr = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int minRed;\n+ for (int i = 0; i < 1024; i++){\n+ sum = sum + histRed[i];\n+ if (sum >= pixelAmount * lowCut * 0.01) {\n+ minRed = i;\n+ break;\n+ }\n+ }\n+\n+ sum = 0;\n+ int maxRed;\n+ for (int i = 1023; i >= 0; i--){\n+ sum = sum + histRed[i];\n+ if (sum >= pixelAmount * highCut * 0.01) {\n+ maxRed = i;\n+ break;\n+ }\n+ }\n+\n+ int blueMap[1024];\n+ float norb = 1.0 / (maxBlue - minBlue);\n+ for (int i = 0; i < 1024; i++) {\n+ if (i < minBlue) {\n+ blueMap[i] = 0;\n+ }\n+ else if (i > maxBlue) {\n+ blueMap[i] = 1023;\n+ }\n+ else {\n+ blueMap[i] = (i - minBlue) * norb * 1023;\n+ }\n+ if (blueMap[i] > 1023) blueMap[i] = 1023;\n+ }\n+\n+ int gbMap[1024];\n+ float norgb = 1.0 / (maxGb - minGb);\n+ for (int i = 0; i < 1024; i++) {\n+ if (i < minGb) {\n+ gbMap[i] = 0;\n+ }\n+ else if (i > maxGb) {\n+ gbMap[i] = 1023;\n+ }\n+ else {\n+ gbMap[i] = (i - minGb) * norgb * 1023;\n+ }\n+ if (gbMap[i] > 1023) gbMap[i] = 1023;\n+ }\n+\n+ int grMap[1024];\n+ float norgr = 1.0 / (maxGr - minGr);\n+ for (int i = 0; i < 1024; i++) {\n+ if (i < minGr) {\n+ grMap[i] = 0;\n+ }\n+ else if (i > maxGr) {\n+ grMap[i] = 1023;\n+ }\n+ else {\n+ grMap[i] = (i - minGr) * norgr * 1023;\n+ }\n+ if (grMap[i] > 1023) grMap[i] = 1023;\n+ }\n+\n+ int redMap[1024];\n+ float norr = 1.0 / (maxRed - minRed);\n+ for (int i = 0; i < 1024; i++) {\n+ if (i < minRed) {\n+ redMap[i] = 0;\n+ }\n+ else if (i > maxRed) {\n+ redMap[i] = 1023;\n+ }\n+ else{\n+ redMap[i] = (i - minRed) * norr * 1023;\n+ }\n+ if (redMap[i] > 1023) redMap[i] = 1023;\n+ }\n+\n+ for (int i = 0;i < height; i++) {\n+ for (int j = 0; j < width; j++){\n+ index = i * width;\n+ if (i % 2 == 0 && j % 2 == 0) {\n+ data[index] = blueMap[data[index]];\n+ } \n+ else if (i % 2 == 0 && j % 2 == 1) {\n+ data[index] = gbMap[data[index]];\n+ } \n+ else if (i % 2 == 1 && j % 2 == 0) {\n+ data[index] = grMap[data[index]];\n+ } \n+ else {\n+ data[index] = redMap[data[index]];\n+ }\n+ index++;\n+ }\n+ }\n+}\n+\n+void ISP::blackLevelCorrect(uint16_t *data, uint16_t offset, int width, int height)\n+{\n+ int len = width * height;\n+ for(int i = 0; i < len; i++) {\n+ if (data[i] < offset){\n+ data[i] = 0;\n+ }\n+ else {\n+ data[i] -= offset;\n+ }\n+ }\n+}\n+\n+void ISP::readChannels(uint16_t *data, uint16_t *R, uint16_t *G, uint16_t *B,\n+ int width, int height)\n+{\n+ int index;\n+ for (int i = 0; i < height; i++) {\n+ index = i * width;\n+ for (int j = 0; j < width; j++) {\n+ if (i % 2 == 0 && j % 2 == 0) {\n+ B[index] = data[index];\n+ }\n+ else if ((i % 2 == 0 && j % 2 == 1) || (i % 2 == 1 && j % 2 == 0)){\n+ G[index] = data[index];\n+ }\n+ else {\n+ R[index] = data[index];\n+ }\n+ index++;\n+ }\n+ }\n+}\n+\n+void ISP::firstPixelInsert(uint16_t *src, uint16_t *dst, int width, int height)\n+{\n+ int index;\n+ for (int i = 0; i < height; i++) {\n+ index = i * width;\n+ for (int j = 0; j < width; j++){\n+ if (i % 2 == 0 && j % 2 == 1) {\n+ if (j == (width - 1)) {\n+ dst[index] = src[index - 1];\n+ }\n+ else {\n+ dst[index] = (src[index - 1] +\n+ src[index + 1]) >> 1;\n+ }\n+ }\n+\n+ if (i % 2 == 1 && j % 2 == 0) {\n+ if(i == height - 1) {\n+ dst[index] = src[index - width];\n+ }\n+ else {\n+ dst[index] = (src[index - width]+\n+ src[index + width]) >> 1;\n+ }\n+ }\n+ \n+ if (i % 2 == 1 && j % 2 == 1) {\n+ if (j < width - 1 && i < height - 1) {\n+ dst[index] = (src[index - width - 1] +\n+ src[index - width + 1] +\n+ src[index + width - 1] +\n+ src[index + width + 1]) >> 2;\n+ }\n+ else if (i == height - 1 && j < width - 1) {\n+ dst[index] = (src[index - width - 1] +\n+ src[index - width + 1]) >> 1;\n+ }\n+ else if (i < height - 1 && j == width - 1) {\n+ dst[index] = (src[index - width - 1] +\n+ src[index + width - 1]) >> 1;\n+ }\n+ else {\n+ dst[index] = src[index - width - 1];\n+ } \n+ }\n+ index++;\n+ }\n+ }\n+}\n+\n+void ISP::twoPixelInsert(uint16_t *src, uint16_t *dst, int width, int height)\n+{\n+ int index;\n+ for (int i = 0; i < height; i++) {\n+ index = i * width;\n+ for (int j = 0; j < width; j++) {\n+ if (i == 0 && j == 0) {\n+ dst[index] = (src[index + width] +\n+ src[index + 1]) >> 1;\n+ }\n+ else if (i == 0 && j > 0 && j % 2 == 0) {\n+ dst[index] = (src[index - 1] +\n+ src[index + width] +\n+ src[index + 1]) / 3;\n+ }\n+ else if (i > 0 && j == 0 && i % 2 == 0) {\n+ dst[index] = (src[index - width] +\n+ src[index + 1] +\n+ src[index + width]) / 3;\n+ }\n+ else if (i == (height - 1) && j < (width - 1) && j % 2 == 1) {\n+ dst[index] = (src[index - 1] +\n+ src[index - width] +\n+ src[index + 1]) / 3;\n+ }\n+ else if (i < (height - 1) && j == (width - 1) && i % 2 == 1) {\n+ dst[index] = (src[index - width] +\n+ src[index - 1] +\n+ src[index + width]) / 3;\n+ }\n+ else if (i == (height - 1) && j == (width - 1)) {\n+ dst[index] = (src[index - width] +\n+ src[index - 1]) >> 1;\n+ }\n+ else if ((i % 2 == 0 && j % 2 == 0) || (i % 2 == 1 && j % 2 == 1)) {\n+ dst[index] = (src[index - 1] +\n+ src[index + 1] +\n+ src[index - width] +\n+ src[index + width]) / 4;\n+ }\n+ index++;\n+ }\n+ }\n+}\n+\n+void ISP::lastPixelInsert(uint16_t *src, uint16_t *dst, int width, int height)\n+{\n+ int index;\n+ for (int i = 0; i < height; i++) {\n+ index = i * width;\n+ for (int j = 0; j < width; j++){\n+ if (i % 2 == 1 && j % 2 == 0) {\n+ if (j == 0) {\n+ dst[index] = src[index + 1];\n+ }\n+ else {\n+ dst[index] = (src[index - 1] +\n+ src[index + 1]) >> 1;\n+ }\n+ }\n+\n+ if (i % 2 == 0 && j % 2 == 1) {\n+ if(i == 0) {\n+ dst[index] = src[index + width];\n+ }\n+ else {\n+ dst[index] = (src[index - width]+\n+ src[index + width]) >> 1;\n+ }\n+ }\n+\n+ if (i % 2 == 0 && j % 2 == 0) {\n+ if (i > 0 && j > 0) {\n+ dst[index] = (src[index - width - 1] +\n+ src[index - width + 1] +\n+ src[index + width - 1] +\n+ src[index + width + 1]) >> 2;\n+ }\n+ else if (i == 0 && j > 0) {\n+ dst[index] = (src[index + width - 1] +\n+ src[index + width + 1]) >> 1;\n+ }\n+ else if (i > 0 && j == 0) {\n+ dst[index] = (src[index - width + 1] +\n+ src[index + width + 1]) >> 1;\n+ }\n+ else {\n+ dst[index] = src[index + width + 1];\n+ }\n+ }\n+ index++;\n+ }\n+ }\n+}\n+\n+void ISP::demosaic(uint16_t *data, uint16_t *R, uint16_t *G, uint16_t *B,\n+ int width, int height)\n+{\n+ firstPixelInsert(data, B, width, height);\n+ twoPixelInsert(data, G, width, height);\n+ lastPixelInsert(data, R, width, height);\n+}\n+\n+void ISP::autoWhiteBalance(uint16_t *R, uint16_t *G, uint16_t *B, int width, int height)\n+{\n+ float aveB = 0, aveG = 0, aveR = 0;\n+ float Kb, Kg, Kr;\n+\n+ for (int i = 0; i < width * height; i++) {\n+ aveB += 1.0 * B[i];\n+ aveG += 1.0 * G[i];\n+ aveR += 1.0 * R[i];\n+ }\n+\n+ aveB *= (1.0 / (width * height));\n+ aveG *= (1.0 / (width * height));\n+ aveR *= (1.0 / (width * height));\n+\n+ Kr = (aveB + aveG + aveR) / aveR * (1.0 / 3.0);\n+ Kg = (aveB + aveG + aveR) / aveG * (1.0 / 3.0);\n+ Kb = (aveB + aveG + aveR) / aveB * (1.0 / 3.0);\n+\n+ for (int i = 0; i < width * height; i++) {\n+ B[i] = B[i] * Kb;\n+ G[i] = G[i] * Kg;\n+ R[i] = R[i] * Kr;\n+\n+ if (R[i] > 1023) R[i] = 1023;\n+ if (G[i] > 1023) G[i] = 1023;\n+ if (R[i] > 1023) B[i] = 1023;\n+ }\n+}\n+\n+void ISP::gammaCorrect(uint16_t *R, uint16_t *G, uint16_t *B, float val,\n+ int width, int height)\n+{\n+ float nor = 1.0 / 1023.0;\n+ float gamma = 1.0 / val;\n+ for (int i = 0; i < width * height; i++) {\n+ R[i] = pow(R[i] * nor, gamma) * 1023;\n+ G[i] = pow(G[i] * nor, gamma) * 1023;\n+ B[i] = pow(B[i] * nor, gamma) * 1023;\n+\n+ if (R[i] > 1023) R[i] = 1023;\n+ if (G[i] > 1023) G[i] = 1023;\n+ if (B[i] > 1023) B[i] = 1023;\n+ }\n+}\n+\n+void ISP::compress_10bit_to_8bit (uint16_t *src, uint8_t *dst, int width, int height)\n+{\n+ for (int i = 0; i < width * height; i++) {\n+ dst[i] = src[i] >> 2 & 0xff;\n+ }\n+}\n+\n+float ISP::distance(int x, int y, int i, int j)\n+{\n+ return float(sqrt(pow(x - i, 2) + pow(y - j, 2)));\n+}\n+\n+double ISP::gaussian(float x, double sigma)\n+{\n+ return exp(-(pow(x, 2)) / (2 * pow(sigma, 2))) / (2 * 3.1415926 * pow(sigma, 2));\n+}\n+\n+void ISP::bilateralFilter(uint16_t *R, uint16_t *G, uint16_t *B,\n+ int diameter, double sigmaI,\n+ double sigmaS, int width, int height)\n+{\n+ for (int i = 2; i < height - 2; i++) {\n+ for (int j = 2; j < width - 2; j++) {\n+ double iFiltered = 0;\n+ double wp = 0;\n+ int neighbor_x = 0;\n+ int neighbor_y = 0;\n+ int half = diameter / 2;\n+\n+ for (int k = 0; k < diameter; k++) {\n+ for (int l = 0; l < diameter; l++) {\n+ neighbor_x = i - (half - k);\n+ neighbor_y = j - (half - l);\n+ double gi = gaussian(R[neighbor_x * width + neighbor_y] - R[i * width +j], sigmaI);\n+ double gs = gaussian(distance(i, j, neighbor_x, neighbor_y), sigmaS);\n+ double w = gi * gs;\n+ iFiltered = iFiltered + R[neighbor_x * width + neighbor_y] * w;\n+ wp = wp + w;\n+ } \n+ }\n+\n+ iFiltered = iFiltered / wp;\n+ R[i * width + j] = iFiltered;\n+ }\n+ }\n+\n+ for (int i = 2; i < height - 2; i++) {\n+ for (int j = 2; j < width - 2; j++) {\n+ double iFiltered = 0;\n+ double wp = 0;\n+ int neighbor_x = 0;\n+ int neighbor_y = 0;\n+ int half = diameter / 2;\n+\n+ for (int k = 0; k < diameter; k++) {\n+ for (int l = 0; l < diameter; l++) {\n+ neighbor_x = i - (half - k);\n+ neighbor_y = j - (half - l);\n+ double gi = gaussian(G[neighbor_x * width + neighbor_y] - G[i * width +j], sigmaI);\n+ double gs = gaussian(distance(i, j, neighbor_x, neighbor_y), sigmaS);\n+ double w = gi * gs;\n+ iFiltered = iFiltered + G[neighbor_x * width + neighbor_y] * w;\n+ wp = wp + w;\n+ } \n+ }\n+\n+ iFiltered = iFiltered / wp;\n+ G[i * width + j] = iFiltered;\n+ }\n+ }\n+\n+ for (int i = 2; i < height - 2; i++) {\n+ for (int j = 2; j < width - 2; j++) {\n+ double iFiltered = 0;\n+ double wp = 0;\n+ int neighbor_x = 0;\n+ int neighbor_y = 0;\n+ int half = diameter / 2;\n+\n+ for (int k = 0; k < diameter; k++) {\n+ for (int l = 0; l < diameter; l++) {\n+ neighbor_x = i - (half - k);\n+ neighbor_y = j - (half - l);\n+ double gi = gaussian(B[neighbor_x * width + neighbor_y] - B[i * width +j], sigmaI);\n+ double gs = gaussian(distance(i, j, neighbor_x, neighbor_y), sigmaS);\n+ double w = gi * gs;\n+ iFiltered = iFiltered + B[neighbor_x * width + neighbor_y] * w;\n+ wp = wp + w;\n+ } \n+ }\n+\n+ iFiltered = iFiltered / wp;\n+ B[i * width + j] = iFiltered;\n+ }\n+ }\n+}\n+\n+void ISP::processing(FrameBuffer *srcBuffer, FrameBuffer *dstBuffer, int width, int height)\n+{\n+ uint8_t *rgb_buf;\n+ uint16_t *rawData;\n+ uint16_t *rgbData = new std::uint16_t[width * height * 3];\n+\n+ uint16_t *rData = rgbData;\n+ uint16_t *gData = rData + width * height;\n+ uint16_t *bData = gData + width * height;\n+ memset(rgbData, 0x0, width * height * 3);\n+\n+ const FrameBuffer::Plane &plane = srcBuffer->planes()[0]; \n+ rawData = (uint16_t *)mmap(NULL, plane.length, PROT_READ|PROT_WRITE, MAP_SHARED, plane.fd.fd(), 0);\n+ if (rawData == MAP_FAILED) {\n+ LOG(ISP, Error) << \"Read raw data failed\";\n+ ispCompleted.emit(dstBuffer);\n+ }\n+\n+ blackLevelCorrect(rawData, 16, width, height);\n+ readChannels(rawData, rData, gData, bData, width, height);\n+ demosaic(rawData, rData, gData, bData, width, height);\n+ autoWhiteBalance(rData, gData, bData, width, height);\n+ autoContrast(rData, 0.01, 0.01, width, height);\n+ autoContrast(gData, 0.01, 0.01, width, height);\n+ autoContrast(bData, 0.01, 0.01, width, height);\n+ gammaCorrect(rData, gData, bData, 2.2, width, height);\n+ //bilateralFilter(rData, gData, bData, 5, 24.0, 32.0);\n+ \n+ const FrameBuffer::Plane &rgbPlane = dstBuffer->planes()[0];\n+ rgb_buf = (uint8_t *)mmap(NULL, rgbPlane.length, PROT_READ|PROT_WRITE, MAP_SHARED, rgbPlane.fd.fd(), 0);\n+ if (rgb_buf == MAP_FAILED) {\n+ LOG(ISP, Error) << \"Read rgb data failed\";\n+ ispCompleted.emit(dstBuffer);\n+ }\n+\n+ uint8_t *rData_8 = rgb_buf;\n+ uint8_t *gData_8 = rData_8 + width * height;\n+ uint8_t *bData_8 = gData_8 + width * height;\n+\n+ compress_10bit_to_8bit(rData, rData_8, width, height);\n+ compress_10bit_to_8bit(gData, gData_8, width, height);\n+ compress_10bit_to_8bit(bData, bData_8, width, height);\n+\n+ delete[] rgbData;\n+\n+ ispCompleted.emit(dstBuffer);\n+}\n+\n+} /* namespace libcamera */\n+\ndiff --git a/src/libcamera/pipeline/isp/isp_processing.h b/src/libcamera/pipeline/isp/isp_processing.h\nnew file mode 100644\nindex 00000000..b80b4218\n--- /dev/null\n+++ b/src/libcamera/pipeline/isp/isp_processing.h\n@@ -0,0 +1,62 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Siyuan Fan <siyuan.fan@foxmail.com>\n+ *\n+ * isp_processing.h - The software ISP class\n+ */\n+#ifndef __LIBCAMERA_PIPELINE_ISP_PROCESSING_H__\n+#define __LIBCAMERA_PIPELINE_ISP_PROCESSING_H__\n+\n+#include <libcamera/framebuffer.h>\n+\n+#include \"libcamera/base/signal.h\"\n+#include \"libcamera/base/object.h\"\n+\n+namespace libcamera{\n+\n+using std::uint16_t;\n+using std::uint8_t;\n+\n+class ISP : public Object\n+{\n+public:\n+ void autoContrast(uint16_t *data, float lowCut, float highCut, int width, int height);\n+\n+ void blackLevelCorrect(uint16_t *data, uint16_t offset, int width, int height);\n+\n+ void readChannels(uint16_t *data, uint16_t *R, uint16_t *G, uint16_t *B,\n+ int width, int height);\n+\n+ void firstPixelInsert(uint16_t *src, uint16_t *dst, int width, int height);\n+\n+ void twoPixelInsert(uint16_t *src, uint16_t *dst, int width, int height);\n+\n+ void lastPixelInsert(uint16_t *src, uint16_t *dst, int width, int height);\n+\n+ void demosaic(uint16_t *data, uint16_t *R, uint16_t *G, uint16_t *B,\n+ int width, int height);\n+\n+ void autoWhiteBalance(uint16_t *R, uint16_t *G, uint16_t *B, int width, int height);\n+\n+ void gammaCorrect(uint16_t *R, uint16_t *G, uint16_t *B, float val,\n+ int width, int height);\n+\n+ float distance(int x, int y, int i, int j);\n+\n+ double gaussian(float x, double sigma);\n+\n+ void bilateralFilter(uint16_t *R, uint16_t *G, uint16_t *B,\n+ int diameter, double sigmaI, double sigmaS,\n+ int width, int height);\n+\n+ void compress_10bit_to_8bit(uint16_t *src, uint8_t *dst, int width, int height); \n+\n+ void processing(FrameBuffer *srcBuffer, FrameBuffer *dstBuffer, int width, int height);\n+\n+ Signal<FrameBuffer *> ispCompleted;\n+\n+};\n+\n+}\n+\n+#endif /* __LIBCAMERA_PIPELINE_ISP_PROCESSING_H__ */\n", "prefixes": [ "libcamera-devel", "RFC", "v1", "2/3" ] }