{"id":23177,"url":"https://patchwork.libcamera.org/api/1.1/patches/23177/?format=json","web_url":"https://patchwork.libcamera.org/patch/23177/","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":"<Z/t0DbvYXAjjRmVB@duo.ucw.cz>","date":"2025-04-13T08:21:33","name":"cam: Convert RGB variations to BGR","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"576e40f43e8557ed686c34f308e7f2f2ef8c9d6f","submitter":{"id":49,"url":"https://patchwork.libcamera.org/api/1.1/people/49/?format=json","name":"Pavel Machek","email":"pavel@ucw.cz"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/23177/mbox/","series":[{"id":5124,"url":"https://patchwork.libcamera.org/api/1.1/series/5124/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=5124","date":"2025-04-13T08:21:33","name":"cam: Convert RGB variations to BGR","version":1,"mbox":"https://patchwork.libcamera.org/series/5124/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/23177/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/23177/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 495F9C3213\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun, 13 Apr 2025 08:21:38 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3CD0968AB3;\n\tSun, 13 Apr 2025 10:21:37 +0200 (CEST)","from jabberwock.ucw.cz (jabberwock.ucw.cz [46.255.230.98])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8F10268A38\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 13 Apr 2025 10:21:34 +0200 (CEST)","by jabberwock.ucw.cz (Postfix, from userid 1017)\n\tid 096D51C013F; Sun, 13 Apr 2025 10:21:34 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ucw.cz header.i=@ucw.cz header.b=\"AOLApKhC\";\n\tdkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=ucw.cz; s=gen1;\n\tt=1744532494;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:mime-version:mime-version:content-type:content-type;\n\tbh=Aea+8zeXxH2ZujqclUdAlf+wUVo6RGAvrWZGrXVOjx4=;\n\tb=AOLApKhC6JAaqMEzteNIh/ilW3hcUTBVYluv9VWhsXbkdleSO5I5JMcqEm4W8romGZTww0\n\t9lYirDGXcdkc5YlVIbGjc9XMXt+WiIQslwKInueIFwtQO4a1vB5MRRbMFKg1YZh5clyfqg\n\tvI2SH0fQiMTgGR2Hkm5iNtXDMshxDZA=","Date":"Sun, 13 Apr 2025 10:21:33 +0200","From":"Pavel Machek <pavel@ucw.cz>","To":"laurent.pinchart@ideasonboard.com, libcamera-devel@lists.libcamera.org","Subject":"[PATCH] cam: Convert RGB variations to BGR","Message-ID":"<Z/t0DbvYXAjjRmVB@duo.ucw.cz>","MIME-Version":"1.0","Content-Type":"multipart/signed; micalg=pgp-sha1;\n\tprotocol=\"application/pgp-signature\"; boundary=\"s0cQbyUgC5Qidkq7\"","Content-Disposition":"inline","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":"swIsp with libcamera produces ARGB data by default, which needs\nconversion during write. Implement it and similar conversions.\n    \nSigned-off-by: Pavel Machek <pavel@ucw.cz>","diff":"diff --git a/src/apps/common/ppm_writer.cpp b/src/apps/common/ppm_writer.cpp\nindex 368de8bf..ddbd3263 100644\n--- a/src/apps/common/ppm_writer.cpp\n+++ b/src/apps/common/ppm_writer.cpp\n@@ -20,8 +20,56 @@ int PPMWriter::write(const char *filename,\n \t\t     const StreamConfiguration &config,\n \t\t     const Span<uint8_t> &data)\n {\n-\tif (config.pixelFormat != formats::BGR888) {\n-\t\tstd::cerr << \"Only BGR888 output pixel format is supported (\"\n+\tint r_pos, g_pos, b_pos, bpp;\n+\tswitch (config.pixelFormat) {\n+\tcase libcamera::formats::R8:\n+\t\tr_pos = 0;\n+\t\tg_pos = 0;\n+\t\tb_pos = 0;\n+\t\tbpp = 1;\n+\t\tbreak;\n+\tcase libcamera::formats::RGB888:\n+\t\tr_pos = 2;\n+\t\tg_pos = 1;\n+\t\tb_pos = 0;\n+\t\tbpp = 3;\n+\t\tbreak;\n+\tcase libcamera::formats::BGR888:\n+\t\tr_pos = 0;\n+\t\tg_pos = 1;\n+\t\tb_pos = 2;\n+\t\tbpp = 3;\n+\t\tbreak;\n+\tcase libcamera::formats::ARGB8888:\n+\tcase libcamera::formats::XRGB8888:\n+\t\tr_pos = 2;\n+\t\tg_pos = 1;\n+\t\tb_pos = 0;\n+\t\tbpp = 4;\n+\t\tbreak;\n+\tcase libcamera::formats::RGBA8888:\n+\tcase libcamera::formats::RGBX8888:\n+\t\tr_pos = 3;\n+\t\tg_pos = 2;\n+\t\tb_pos = 1;\n+\t\tbpp = 4;\n+\t\tbreak;\n+\tcase libcamera::formats::ABGR8888:\n+\tcase libcamera::formats::XBGR8888:\n+\t\tr_pos = 0;\n+\t\tg_pos = 1;\n+\t\tb_pos = 2;\n+\t\tbpp = 4;\n+\t\tbreak;\n+\tcase libcamera::formats::BGRA8888:\n+\tcase libcamera::formats::BGRX8888:\n+\t\tr_pos = 1;\n+\t\tg_pos = 2;\n+\t\tb_pos = 3;\n+\t\tbpp = 4;\n+\t\tbreak;\n+\tdefault:\n+\t\tstd::cerr << \"Only RGB output pixel formats are supported (\"\n \t\t\t  << config.pixelFormat << \" requested)\" << std::endl;\n \t\treturn -EINVAL;\n \t}\n@@ -41,14 +89,35 @@ int PPMWriter::write(const char *filename,\n \t}\n \n \tconst unsigned int rowLength = config.size.width * 3;\n-\tconst char *row = reinterpret_cast<const char *>(data.data());\n-\tfor (unsigned int y = 0; y < config.size.height; y++, row += config.stride) {\n-\t\toutput.write(row, rowLength);\n+\t// Output without any conversion will be slightly faster\n+\tif (config.pixelFormat == formats::BGR888) {\n+\t\tconst char *row = reinterpret_cast<const char *>(data.data());\n+\t\tfor (unsigned int y = 0; y < config.size.height; y++, row += config.stride) {\n+\t\t\toutput.write(row, rowLength);\n+\t\t\tif (!output) {\n+\t\t\t\tstd::cerr << \"Failed to write image data at row \" << y << std::endl;\n+\t\t\t\treturn -EIO;\n+\t\t\t}\n+\t\t}\n+\t\treturn 0;\n+\t}\n+\n+\tconst unsigned int inputRowBytes = config.stride; // should be >= width * 4\n+\t// Create a temporary buffer to hold one output row.\n+\tstd::vector<char> rowBuffer(rowLength);\n+\tconst uint8_t *row = reinterpret_cast<const uint8_t *>(data.data());\n+\tfor (unsigned int y = 0; y < config.size.height; y++, row += inputRowBytes) {\n+\t\tfor (unsigned int x = 0; x < config.size.width; x++) {\n+\t\t\tconst uint8_t *pixel = row + x * bpp;\n+\t\t\trowBuffer[x * 3 + 0] = static_cast<char>(pixel[r_pos]);\n+\t\t\trowBuffer[x * 3 + 1] = static_cast<char>(pixel[g_pos]);\n+\t\t\trowBuffer[x * 3 + 2] = static_cast<char>(pixel[b_pos]);\n+\t\t}\n+\t\toutput.write(rowBuffer.data(), rowLength);\n \t\tif (!output) {\n \t\t\tstd::cerr << \"Failed to write image data at row \" << y << std::endl;\n \t\t\treturn -EIO;\n \t\t}\n \t}\n-\n \treturn 0;\n }\n","prefixes":[]}