The open imaging DSP library
Loading...
Searching...
No Matches
image.h
1/* SPDX-License-Identifier: Apache-2.0 */
7#ifndef MPIX_IMAGE_H
8#define MPIX_IMAGE_H
9
10#include <mpix/custom_api.h>
11#include <mpix/formats.h>
12#include <mpix/operation.h>
13#include <mpix/pipeline.h>
14#include <mpix/sample.h>
15#include <mpix/stats.h>
16#include <mpix/types.h>
17
30static inline void mpix_image_from_buf(struct mpix_image *img, const uint8_t *buffer, size_t size,
31 const struct mpix_format *fmt)
32{
33 img->buffer = buffer;
34 img->size = size;
35 img->fmt = *fmt;
36}
37
46static inline int mpix_image_to_buf(struct mpix_image *img, uint8_t *buffer, size_t size)
47{
48 if (mpix_op_append(img, MPIX_OP_END, sizeof(*img->last_op), size) == NULL) return -ENOMEM;
49 mpix_ring_set_buffer(&img->last_op->ring, buffer, size);
50 return mpix_pipeline_process(img->first_op, img->buffer, img->size);
51}
52
66static inline int mpix_image_convert(struct mpix_image *img, uint32_t new_format)
67{
68 int32_t p[] = { (int32_t)new_format };
69 return mpix_pipeline_add(img, MPIX_OP_CONVERT, p, ARRAY_SIZE(p));
70}
71
83static inline int mpix_image_palette_encode(struct mpix_image *img, uint32_t fourcc)
84{
85 int32_t p[] = { (int32_t)fourcc };
86 return mpix_pipeline_add(img, MPIX_OP_PALETTE_ENCODE, p, ARRAY_SIZE(p));
87}
88
98static inline int mpix_image_palette_decode(struct mpix_image *img)
99{
100 return mpix_pipeline_add(img, MPIX_OP_PALETTE_DECODE, NULL, 0);
101}
102
116static inline int mpix_image_set_palette(struct mpix_image *img, struct mpix_palette *palette)
117{
118 return mpix_pipeline_set_palette(img->first_op, palette);
119}
120
134static inline int mpix_image_debayer(struct mpix_image *img, uint32_t window_size)
135{
136 enum mpix_op_type type = (window_size == 1) ? MPIX_OP_DEBAYER_1X1 :
137 (window_size == 2) ? MPIX_OP_DEBAYER_2X2 :
138 (window_size == 3) ? MPIX_OP_DEBAYER_3X3 : MPIX_NB_OP;
139 return mpix_pipeline_add(img, type, NULL, 0);
140}
141
151static inline int mpix_image_correct_color_matrix(struct mpix_image *img)
152{
153 return mpix_pipeline_add(img, MPIX_OP_CORRECT_COLOR_MATRIX, NULL, 0);
154}
155
165static inline int mpix_image_correct_white_balance(struct mpix_image *img)
166{
167 return mpix_pipeline_add(img, MPIX_OP_CORRECT_WHITE_BALANCE, NULL, 0);
168}
169
179static inline int mpix_image_correct_black_level(struct mpix_image *img)
180{
181 return mpix_pipeline_add(img, MPIX_OP_CORRECT_BLACK_LEVEL, NULL, 0);
182}
183
193static inline int mpix_image_correct_gamma(struct mpix_image *img)
194{
195 return mpix_pipeline_add(img, MPIX_OP_CORRECT_GAMMA, NULL, 0);
196}
197
204static inline int mpix_image_qoi_encode(struct mpix_image *img)
205{
206 return mpix_pipeline_add(img, MPIX_OP_QOI_ENCODE, NULL, 0);
207}
208
215static inline int mpix_image_jpeg_encode(struct mpix_image *img)
216{
217 return mpix_pipeline_add(img, MPIX_OP_JPEG_ENCODE, NULL, 0);
218}
219
232static inline int mpix_image_resize_subsample(struct mpix_image *img,
233 uint16_t width, uint16_t height)
234{
235 int32_t p[] = { width, height };
236 return mpix_pipeline_add(img, MPIX_OP_RESIZE_SUBSAMPLE, p, ARRAY_SIZE(p));
237}
238
252static inline int mpix_image_crop(struct mpix_image *img, uint16_t x_offset, uint16_t y_offset,
253 uint16_t crop_width, uint16_t crop_height)
254{
255 int32_t p[] = { x_offset, y_offset, crop_width, crop_height };
256 return mpix_pipeline_add(img, MPIX_OP_CROP, p, ARRAY_SIZE(p));
257}
258
266static inline int mpix_image_sharpen(struct mpix_image *img, uint8_t level)
267{
268 int32_t p[] = { MPIX_KERNEL_SHARPEN };
269 enum mpix_op_type type = (level == 3) ? MPIX_OP_KERNEL_CONVOLVE_3X3 :
270 (level == 5) ? MPIX_OP_KERNEL_CONVOLVE_5X5 : MPIX_NB_OP;
271 return mpix_pipeline_add(img, type, p, ARRAY_SIZE(p));
272}
273
280static inline int mpix_image_denoise(struct mpix_image *img, uint8_t level)
281{
282 enum mpix_op_type type = (level == 3) ? MPIX_OP_KERNEL_DENOISE_3X3 :
283 (level == 5) ? MPIX_OP_KERNEL_DENOISE_5X5 : MPIX_NB_OP;
284 return mpix_pipeline_add(img, type, NULL, 0);
285}
286
294static inline int mpix_image_edge_detect(struct mpix_image *img, uint8_t level)
295{
296 int32_t p[] = { MPIX_KERNEL_EDGE_DETECT };
297 enum mpix_op_type type = (level == 3) ? MPIX_OP_KERNEL_CONVOLVE_3X3 :
298 (level == 5) ? MPIX_OP_KERNEL_CONVOLVE_5X5 : MPIX_NB_OP;
299 return mpix_pipeline_add(img, type, p, ARRAY_SIZE(p));
300}
301
309static inline int mpix_image_gaussian_blur(struct mpix_image *img, uint8_t level)
310{
311 int32_t p[] = { MPIX_KERNEL_GAUSSIAN_BLUR };
312 enum mpix_op_type type = (level == 3) ? MPIX_OP_KERNEL_CONVOLVE_3X3 :
313 (level == 5) ? MPIX_OP_KERNEL_CONVOLVE_5X5 : MPIX_NB_OP;
314 return mpix_pipeline_add(img, type, p, ARRAY_SIZE(p));
315}
316
326static inline void mpix_image_from_palette(struct mpix_image *img, const struct mpix_palette *palette)
327{
328 uint16_t n = 1 << mpix_palette_bit_depth(palette->fourcc);
329 struct mpix_format fmt = { .width = n, .height = 1, .fourcc = MPIX_FMT_RGB24 };
330 mpix_image_from_buf(img, palette->colors_rgb24, n * 3, &fmt);
331}
332
342static inline int mpix_image_to_palette(struct mpix_image *img, struct mpix_palette *palette)
343{
344 uint16_t n = 1 << mpix_palette_bit_depth(palette->fourcc);
345 return (n == 0) ? -EINVAL : mpix_image_to_buf(img, palette->colors_rgb24, n * 3);
346}
347
360int mpix_image_optimize_palette(struct mpix_image *img, struct mpix_palette *palette,
361 uint16_t num_samples);
362
373static inline void mpix_image_free(struct mpix_image *img)
374{
376 img->first_op = img->last_op = NULL;
377 memset(img->ctrls, 0x00, sizeof(img->ctrls));
378}
379
386static inline struct mpix_format *mpix_image_format(struct mpix_image *img)
387{
388 return img->first_op == NULL ? &img->fmt : &img->first_op->fmt;
389}
390
408static inline int mpix_image_sample_random_rgb(struct mpix_image *img, uint8_t rgb[3])
409{
410 return mpix_sample_random_rgb(img->buffer, mpix_image_format(img), rgb);
411}
412
424static inline void mpix_image_stats(struct mpix_image *img, struct mpix_stats *stats)
425{
427}
428
440static inline int mpix_image_ctrl_value(struct mpix_image *img, enum mpix_control_id cid,
441 int32_t value)
442{
443 if (cid > MPIX_NB_CID) return -ERANGE;
444 if (img->ctrls[cid] == NULL) return -ENOENT;
445 img->ctrls[cid][0] = value;
446 return 0;
447}
448
460static inline int mpix_image_ctrl_array(struct mpix_image *img, enum mpix_control_id cid,
461 int32_t *array, size_t size)
462{
463 if (cid + size > MPIX_NB_CID) return -ERANGE;
464 for (size_t i = 0; i < size; i++) {
465 if (img->ctrls[cid + i] == NULL) return -ENOENT;
466 *img->ctrls[cid + i] = array[i];
467 }
468 return 0;
469}
470
481static inline int mpix_image_read_output(struct mpix_image *img, const uint8_t **buf, size_t *size)
482{
483 if (img->last_op == NULL) return -EINVAL;
484 mpix_op_input_all(img->last_op, buf, size);
485 return (*size == 0) ? -ENODATA : 0;
486}
487
488#endif
#define MPIX_FMT_RGB24
Definition formats.h:125
static uint8_t mpix_palette_bit_depth(uint32_t fourcc)
Convert an image and store it into a color palette.
Definition formats.h:52
static void mpix_image_from_buf(struct mpix_image *img, const uint8_t *buffer, size_t size, const struct mpix_format *fmt)
Initialize an image from a memory buffer.
Definition image.h:30
static int mpix_image_edge_detect(struct mpix_image *img, uint8_t level)
Apply an edge detection operation to an image.
Definition image.h:294
int mpix_image_optimize_palette(struct mpix_image *img, struct mpix_palette *palette, uint16_t num_samples)
Optimize a color palette after the values from the image.
static int mpix_image_resize_subsample(struct mpix_image *img, uint16_t width, uint16_t height)
Resize an image.
Definition image.h:232
static int mpix_image_palette_encode(struct mpix_image *img, uint32_t fourcc)
Convert an image to an indexed color format.
Definition image.h:83
static int mpix_image_debayer(struct mpix_image *img, uint32_t window_size)
Convert an image from a bayer array format to RGB24.
Definition image.h:134
static int mpix_image_ctrl_array(struct mpix_image *img, enum mpix_control_id cid, int32_t *array, size_t size)
Set a control to a pipeline.
Definition image.h:460
static struct mpix_format * mpix_image_format(struct mpix_image *img)
Get the input format of an image, matching the inage input buffer.
Definition image.h:386
static int mpix_image_gaussian_blur(struct mpix_image *img, uint8_t level)
Apply a gaussian blur operation to an image.
Definition image.h:309
static int mpix_image_crop(struct mpix_image *img, uint16_t x_offset, uint16_t y_offset, uint16_t crop_width, uint16_t crop_height)
Crop an image to a smaller region.
Definition image.h:252
static int mpix_image_qoi_encode(struct mpix_image *img)
Encode an image to the QOI compressed image format.
Definition image.h:204
static void mpix_image_free(struct mpix_image *img)
Free the resources of an image.
Definition image.h:373
static int mpix_image_read_output(struct mpix_image *img, const uint8_t **buf, size_t *size)
Return the output buffer and its size, resetting it to zero.
Definition image.h:481
static int mpix_image_convert(struct mpix_image *img, uint32_t new_format)
Convert an image to a new pixel format.
Definition image.h:66
static int mpix_image_correct_black_level(struct mpix_image *img)
Apply Black Level Correction (BLC) to the image.
Definition image.h:179
static int mpix_image_ctrl_value(struct mpix_image *img, enum mpix_control_id cid, int32_t value)
Set a control to a pipeline.
Definition image.h:440
static int mpix_image_palette_decode(struct mpix_image *img)
Convert an image from an indexed color format.
Definition image.h:98
static int mpix_image_set_palette(struct mpix_image *img, struct mpix_palette *palette)
Set the palette of every operation currently in the pipeline.
Definition image.h:116
static int mpix_image_jpeg_encode(struct mpix_image *img)
Compressed an image to the JPEG format.
Definition image.h:215
static void mpix_image_from_palette(struct mpix_image *img, const struct mpix_palette *palette)
Initialize an image from a palette.
Definition image.h:326
static int mpix_image_to_buf(struct mpix_image *img, uint8_t *buffer, size_t size)
Convert an image and store it into the output buffer.
Definition image.h:46
static int mpix_image_sample_random_rgb(struct mpix_image *img, uint8_t rgb[3])
Collect a random RGB pixel from an image.
Definition image.h:408
static int mpix_image_correct_color_matrix(struct mpix_image *img)
Apply Color Correction Matrix (CCM) to the image.
Definition image.h:151
static int mpix_image_correct_white_balance(struct mpix_image *img)
Apply White Balance Correction (AWB) to the image.
Definition image.h:165
static int mpix_image_correct_gamma(struct mpix_image *img)
Apply Gamma Correction (GC) to the image.
Definition image.h:193
static void mpix_image_stats(struct mpix_image *img, struct mpix_stats *stats)
Collect statistics from an image.
Definition image.h:424
static int mpix_image_sharpen(struct mpix_image *img, uint8_t level)
Apply a sharpen operation to an image.
Definition image.h:266
static int mpix_image_to_palette(struct mpix_image *img, struct mpix_palette *palette)
Convert an image and store it into a color palette.
Definition image.h:342
static int mpix_image_denoise(struct mpix_image *img, uint8_t level)
Apply a denoise operation to an image.
Definition image.h:280
static void * mpix_op_append(struct mpix_image *img, enum mpix_op_type op_type, size_t op_sz, size_t buf_sz)
Allocate a new operation and add it to an image pipeline.
Definition operation.h:188
int mpix_pipeline_process(struct mpix_base_op *op, const uint8_t *buffer, size_t size)
Process a buffer into a pipeline.
void mpix_pipeline_free(struct mpix_base_op *first_op)
Free the intermediate memory as well as operations of a pipeline.
int mpix_pipeline_add(struct mpix_image *img, enum mpix_op_type type, const int32_t *params, size_t params_nb)
Add an operation to an image.
int mpix_pipeline_set_palette(struct mpix_base_op *first_op, struct mpix_palette *palette)
Set an image palette for every palette-related elements of the pipeline.
int mpix_sample_random_rgb(const uint8_t *buf, const struct mpix_format *fmt, uint8_t *dst)
Collect a pixel at a random location from the input buffer in RGB24 format.
void mpix_stats_from_buf(struct mpix_stats *stats, const uint8_t *buf, struct mpix_format *fmt)
Collect red, green, blue channel averages of all pixels in an RGB24 frame.
mpix_op_type
MPIX operation type identifying an operation family.
Definition types.h:21
mpix_control_id
Definition types.h:52
@ MPIX_KERNEL_EDGE_DETECT
Definition types.h:40
@ MPIX_KERNEL_GAUSSIAN_BLUR
Definition types.h:42
@ MPIX_KERNEL_SHARPEN
Definition types.h:46
@ MPIX_NB_CID
Definition types.h:82
struct mpix_ring ring
Definition types.h:149
struct mpix_format fmt
Definition types.h:145
Image format description.
Definition types.h:100
uint16_t width
Definition types.h:104
Represent the image currently being processed.
Definition types.h:164
struct mpix_base_op * first_op
Definition types.h:166
struct mpix_base_op * last_op
Definition types.h:168
int32_t * ctrls[MPIX_NB_CID]
Definition types.h:176
const uint8_t * buffer
Definition types.h:170
size_t size
Definition types.h:172
struct mpix_format fmt
Definition types.h:174
Definition types.h:190
uint32_t fourcc
Definition types.h:194
uint8_t colors_rgb24[3<< 8]
Definition types.h:192
Definition types.h:198