|
| 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 | +} |
0 commit comments