Skip to content

Commit 277fb20

Browse files
committed
samples: lib: pixel: show usage of the pixel library
The newly introduced lib/pixel features utilities that help composing video pipelines together for the purpose of stream processing, as well as debug utilities. Signed-off-by: Josuah Demangeon <me@josuah.net>
1 parent 715a8b0 commit 277fb20

File tree

17 files changed

+386
-0
lines changed

17 files changed

+386
-0
lines changed

samples/lib/lib.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. zephyr:code-sample-category:: lib
2+
:name: Libraries
3+
:show-listing:
4+
:live-search:
5+
6+
These samples demonstrate how to use the libraries present in Zephyr.

samples/lib/pixel/pixel.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.. zephyr:code-sample-category:: lib_pixel
2+
:name: Pixel Library
3+
:show-listing:
4+
:live-search:
5+
6+
These samples demonstrate how to use the Pixel processing library of Zephyr.
7+
8+
These samples can be used as starting point for test benches that print an input image,
9+
perform some custom processing, and print the color image back along with the logs directly
10+
on the terminal.
11+
12+
The color rendering of the ``truecolor`` printing functions will be accurate RGB output.
13+
14+
This helps debugging individual functions before integrating it into a larger streams.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(pixel)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/lib/pixel/print/README.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.. zephyr:code-sample:: lib_pixel_print
2+
:name: Pixel Printiing Library
3+
4+
Print images on the console.
5+
6+
Overview
7+
********
8+
9+
A sample showcasing how to make use of the pixel library to visualize an image or histogram data
10+
by printing it out on the console using `ANSI escape codes`_.
11+
12+
This allow interleaving debug logs with small previews of the image as a way to debug image data.
13+
14+
.. _ANSI escape codes: https://en.wikipedia.org/wiki/ANSI_escape_code
15+
16+
Building and Running
17+
********************
18+
19+
This application can be built and executed on QEMU as follows:
20+
21+
.. zephyr-app-commands::
22+
:zephyr-app: samples/lib/pixel/print
23+
:host-os: unix
24+
:board: native_sim
25+
:goals: run
26+
:compact:
27+
28+
To build for another board, change "native_sim" above to that board's name.
29+
30+
Sample Output
31+
=============
32+
33+
.. code-block:: console

samples/lib/pixel/print/prj.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_PIXEL=y
2+
CONFIG_ASSERT=y
3+
CONFIG_LOG=y
4+
5+
# Required to make sure the test harnesses catch the log output
6+
CONFIG_LOG_MODE_IMMEDIATE=y

samples/lib/pixel/print/sample.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
sample:
2+
description: Pixel Print sample, print images in the terminal for debug purpose
3+
name: pixel print
4+
common:
5+
min_ram: 32
6+
tags: pixel
7+
integration_platforms:
8+
- native_sim
9+
harness: console
10+
harness_config:
11+
type: one_line
12+
regex:
13+
- "truecolor:"
14+
- "256color:"
15+
- "hexdump:"
16+
- "histogram"
17+
tests:
18+
sample.pixel.print:
19+
tags: pixel
20+
extra_configs:
21+
- CONFIG_PIXEL_PRINT_NONE=y

samples/lib/pixel/print/src/main.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright (c) 2025 tinyVision.ai Inc.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <stdint.h>
8+
9+
#include <zephyr/sys/util.h>
10+
#include <zephyr/pixel/print.h>
11+
#include <zephyr/logging/log.h>
12+
13+
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
14+
15+
static uint8_t rgb24frame[16 * 32 * 3];
16+
17+
void print_image(void)
18+
{
19+
const uint8_t beg[] = {0x00, 0x70, 0xc5};
20+
const uint8_t end[] = {0x79, 0x29, 0xd2};
21+
22+
/* Generate an image with a gradient of the two colors above */
23+
for (size_t i = 0, size = sizeof(rgb24frame); i + 3 <= size; i += 3) {
24+
rgb24frame[i + 0] = (beg[0] * (size - i) + end[0] * i) / size;
25+
rgb24frame[i + 1] = (beg[1] * (size - i) + end[1] * i) / size;
26+
rgb24frame[i + 2] = (beg[2] * (size - i) + end[2] * i) / size;
27+
}
28+
29+
LOG_INF("Printing the gradient #%02x%02x%02x -> #%02x%02x%02x",
30+
beg[0], beg[1], beg[2], end[0], end[1], end[2]);
31+
32+
LOG_INF("hexdump:");
33+
pixel_print_rgb24frame_hex(rgb24frame, sizeof(rgb24frame), 16, 32);
34+
35+
LOG_INF("truecolor:");
36+
pixel_print_rgb24frame_truecolor(rgb24frame, sizeof(rgb24frame), 16, 32);
37+
38+
LOG_INF("256color:");
39+
pixel_print_rgb24frame_256color(rgb24frame, sizeof(rgb24frame), 16, 32);
40+
}
41+
42+
void print_histogram(void)
43+
{
44+
static const uint16_t rgb24hist[] = {
45+
9, 4, 7, 1, 0, 5, 1, 0, 0, 2, 2, 3, 0, 1, 3, 0,
46+
7, 6, 5, 1, 1, 4, 2, 0, 1, 2, 3, 4, 1, 1, 2, 2,
47+
8, 4, 7, 4, 2, 3, 1, 2, 2, 2, 2, 2, 0, 0, 1, 1,
48+
};
49+
50+
static const uint16_t y8hist[] = {
51+
8, 5, 6, 2, 1, 4, 1, 1, 1, 2, 3, 3, 1, 1, 2, 1,
52+
};
53+
54+
LOG_INF("Printing a histogram of %zu RGB buckets", ARRAY_SIZE(rgb24hist));
55+
pixel_print_rgb24hist(rgb24hist, ARRAY_SIZE(rgb24hist), 8);
56+
57+
LOG_INF("Printing a histogram of %zu Y (luma) buckets", ARRAY_SIZE(y8hist));
58+
pixel_print_y8hist(y8hist, ARRAY_SIZE(y8hist), 8);
59+
}
60+
61+
int main(void)
62+
{
63+
print_image();
64+
print_histogram();
65+
66+
return 0;
67+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(pixel)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/lib/pixel/resize/README.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
.. zephyr:code-sample:: lib_pixel_resize
2+
:name: Pixel Resizing Library
3+
4+
Resize an image using subsampling.
5+
6+
Overview
7+
********
8+
9+
A sample showcasing how to make use of the pixel library to resize an input image to a smaller or
10+
bigger output image, using the subsampling method. This helps generating a smaller preview of an
11+
input image.
12+
13+
The input and output are printed as preview images on the console.
14+
15+
Building and Running
16+
********************
17+
18+
This application can be built and executed on the native simulator as follows:
19+
20+
.. zephyr-app-commands::
21+
:zephyr-app: samples/lib/pixel/resize
22+
:host-os: unix
23+
:board: native_sim
24+
:goals: run
25+
:compact:
26+
27+
To build for another board, change "native_sim" above to that board's name.
28+
29+
Sample Output
30+
=============
31+
32+
.. code-block:: console

samples/lib/pixel/resize/prj.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CONFIG_PIXEL=y
2+
CONFIG_ASSERT=y
3+
CONFIG_LOG=y
4+
5+
# Required to make sure the test harnesses catch the log output
6+
CONFIG_LOG_MODE_IMMEDIATE=y

0 commit comments

Comments
 (0)