Skip to content

Commit 96cc37f

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 <[email protected]>
1 parent af9cb06 commit 96cc37f

File tree

20 files changed

+476
-0
lines changed

20 files changed

+476
-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.
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_image)
7+
8+
target_sources(app PRIVATE src/main.c)

samples/lib/pixel/image/README.rst

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
.. zephyr:code-sample:: lib_pixel_image
2+
:name: Pixel Imaging Library
3+
4+
Image an image using subsampling.
5+
6+
Overview
7+
********
8+
9+
A sample showcasing how to perform arbitrary image manipulation using few memory with a
10+
developer-friendly API.
11+
12+
The input and output are printed as preview images on the console.
13+
14+
Building and Running
15+
********************
16+
17+
This application can be built and executed on the native simulator as follows:
18+
19+
.. zephyr-app-commands::
20+
:zephyr-app: samples/lib/pixel/image
21+
:host-os: unix
22+
:board: native_sim
23+
:goals: run
24+
:compact:
25+
26+
To build for another board, change "native_sim" above to that board's name.
27+
28+
Sample Output
29+
=============
30+
31+
.. code-block:: console
32+
33+
*** Booting Zephyr OS build v4.1.0-3103-g25dca1b19885 ***
34+
[00:00:00.000,000] <inf> app: input image, 32x8, 768 bytes:
35+
▄▄▄▄▄▄▄▄▄▄▄ shows-up ▄▄▄▄▄▄▄▄▄▄▄|
36+
▄▄▄▄▄▄▄▄▄▄▄ as color ▄▄▄▄▄▄▄▄▄▄▄|
37+
▄▄▄▄▄▄▄▄▄▄▄ on the ▄▄▄▄▄▄▄▄▄▄▄|
38+
▄▄▄▄▄▄▄▄▄▄▄ terminal ▄▄▄▄▄▄▄▄▄▄▄|
39+
[00:00:00.000,000] <inf> app: output image, 120x40, 14400 bytes:
40+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
41+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
42+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
43+
▄▄▄▄▄▄▄▄▄▄▄ shows-up ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
44+
▄▄▄▄▄▄▄▄▄▄▄ as color ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
45+
▄▄▄▄▄▄▄▄▄▄▄ on the ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
46+
▄▄▄▄▄▄▄▄▄▄▄ terminal ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
47+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
48+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
49+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄[...]▄▄|
50+
51+
.. image:: preview.png

samples/lib/pixel/image/preview.png

8.55 KB
Loading

samples/lib/pixel/image/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/image/sample.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
sample:
2+
description: Pixel image sample, image manipulation library
3+
name: pixel image
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+
- "input image, 32x8, 768 bytes:"
14+
- "output image, 120x40, 14400 bytes:"
15+
tests:
16+
sample.pixel.image:
17+
tags: pixel
18+
extra_configs:
19+
- CONFIG_PIXEL_PRINT_NONE=y

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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/image.h>
11+
#include <zephyr/logging/log.h>
12+
13+
LOG_MODULE_REGISTER(app, LOG_LEVEL_INF);
14+
15+
static void gradient(uint8_t *rgb24buf, size_t size, const uint8_t beg[3], const uint8_t end[3])
16+
{
17+
for (int i = 0; i + 3 <= size; i += 3) {
18+
rgb24buf[i + 0] = (beg[0] * (size - i) + end[0] * i) / size;
19+
rgb24buf[i + 1] = (beg[1] * (size - i) + end[1] * i) / size;
20+
rgb24buf[i + 2] = (beg[2] * (size - i) + end[2] * i) / size;
21+
}
22+
}
23+
24+
static uint8_t rgb24frame_in[32 * 8 * 3];
25+
static uint8_t rgb24frame_out[120 * 40 * 3];
26+
27+
int main(void)
28+
{
29+
const uint8_t beg[] = {0x00, 0x70, 0xc5};
30+
const uint8_t end[] = {0x79, 0x29, 0xd2};
31+
struct pixel_image img;
32+
33+
/* Generate a smooth gradient for a small image */
34+
gradient(rgb24frame_in, sizeof(rgb24frame_in), beg, end);
35+
36+
/* Open that buffer as an image type */
37+
pixel_image_from_buffer(&img, rgb24frame_in, sizeof(rgb24frame_in), 32, 8,
38+
VIDEO_PIX_FMT_RGB24);
39+
LOG_INF("input image, %ux%u, %zu bytes:", img.width, img.height, img.size);
40+
pixel_image_print_truecolor(&img);
41+
42+
/* Turn it into a tall vertical image, now displeasant "banding" artifacts appear */
43+
pixel_image_resize(&img, 5, 40);
44+
45+
/* Try to attenuate it with a blur effect (comment this line to see the difference) */
46+
pixel_image_kernel(&img, PIXEL_KERNEL_GAUSSIAN_BLUR, 5);
47+
48+
/* Stretch the gradient horizontally over the entire width of the output buffer */
49+
pixel_image_resize(&img, 120, 40);
50+
51+
/* Save the image into the output buffer and check for errors */
52+
pixel_image_to_buffer(&img, rgb24frame_out, sizeof(rgb24frame_out));
53+
if (img.err != 0) {
54+
LOG_ERR("Image has error %u: %s", img.err, strerror(-img.err));
55+
return img.err;
56+
}
57+
58+
/* Now that the imagme is exported, we can print it */
59+
LOG_INF("output image, %ux%u, %zu bytes:", img.width, img.height, img.size);
60+
pixel_image_print_truecolor(&img);
61+
62+
return 0;
63+
}

samples/lib/pixel/pixel.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
This helps debugging when other methods are not available.
13+
14+
The ``truecolor`` printing functions give accurate 24-bit RGB colors but slower than the
15+
``256color`` variants.
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 way debug logs can be interleaved with small preview images for debug purposes.
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
34+
35+
*** Booting Zephyr OS build v4.1.0-2611-gfaa7b74cfda7 ***
36+
[00:00:00.000,000] <inf> app: Printing the gradient #0070c5 -> #7929d2
37+
[00:00:00.000,000] <inf> app: hexdump:
38+
col0 col1 col2 col3 col4 [...] col14 col15
39+
R G B R G B R G B R G B R G B [...] R G B R G B
40+
00 70 c5 00 6f c5 00 6f c5 00 6f c5 00 6f c5 [...] 00 6f c5 03 6d c5 row0
41+
03 6d c5 04 6d c5 04 6d c5 04 6d c5 04 6d c5 [...] 04 6d c5 07 6b c5 row1
42+
07 6b c5 07 6b c5 08 6b c5 08 6b c5 08 6b c5 [...] 07 6b c5 0b 69 c6 row2
43+
0b 69 c6 0b 69 c6 0b 69 c6 0c 68 c6 0c 68 c6 [...] 0b 69 c6 0e 67 c6 row3
44+
0f 67 c6 0f 66 c6 0f 66 c6 0f 66 c6 10 66 c6 [...] 0f 66 c6 12 65 c7 row4
45+
12 64 c7 13 64 c7 13 64 c7 13 64 c7 13 64 c7 [...] 13 64 c7 16 62 c7 row5
46+
16 62 c7 16 62 c7 17 62 c7 17 62 c7 17 62 c7 [...] 16 62 c7 1a 60 c7 row6
47+
[...]
48+
[00:00:00.000,000] <inf> app: truecolor:
49+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄|
50+
▄▄▄ shows-up ▄▄▄|
51+
▄▄▄ as color ▄▄▄|
52+
▄▄▄ on the ▄▄▄|
53+
▄▄▄ terminal ▄▄▄|
54+
▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄|
55+
[...]
56+
57+
.. image:: preview.png

0 commit comments

Comments
 (0)