Skip to content

Commit 706895c

Browse files
Alain Volmatdanieldegrasse
authored andcommitted
samples: video: capture_to_lvgl: add crop/compose support
Demonstrate the crop/compose API by introducing 4 new CONFIG options in order to define the crop area. Moreover, if the selection API is available and if the targetted size is different from the current crop size, then try to apply a compose in order to reach the targetted format size. Signed-off-by: Alain Volmat <[email protected]>
1 parent aa34540 commit 706895c

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

samples/drivers/video/capture_to_lvgl/Kconfig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,32 @@ mainmenu "Video capture to LVGL sample application"
77

88
menu "Video capture configuration"
99

10+
config VIDEO_SOURCE_CROP_LEFT
11+
int "Crop area left value"
12+
default 0
13+
help
14+
Left value of the crop area within the video source.
15+
16+
config VIDEO_SOURCE_CROP_TOP
17+
int "Crop area top value"
18+
default 0
19+
help
20+
Top value of the crop area within the video source.
21+
22+
config VIDEO_SOURCE_CROP_WIDTH
23+
int "Crop area width value"
24+
default 0
25+
help
26+
Width value of the crop area within the video source.
27+
If set to 0, the crop is not applied.
28+
29+
config VIDEO_SOURCE_CROP_HEIGHT
30+
int "Crop area height value"
31+
default 0
32+
help
33+
Height value of the crop area within the video source.
34+
If set to 0, the crop is not applied.
35+
1036
config VIDEO_WIDTH
1137
int "Define the width of the video"
1238
default 320

samples/drivers/video/capture_to_lvgl/src/main.c

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ int main(void)
3131
struct video_format fmt;
3232
struct video_caps caps;
3333
enum video_buf_type type = VIDEO_BUF_TYPE_OUTPUT;
34+
struct video_selection sel = {
35+
.type = VIDEO_BUF_TYPE_OUTPUT,
36+
};
3437
size_t bsize;
3538
int i = 0;
39+
int err;
3640

3741
display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
3842
if (!device_is_ready(display_dev)) {
@@ -74,11 +78,50 @@ int main(void)
7478
return 0;
7579
}
7680

81+
/* Set the crop setting if necessary */
82+
#if CONFIG_VIDEO_SOURCE_CROP_WIDTH && CONFIG_VIDEO_SOURCE_CROP_HEIGHT
83+
sel.target = VIDEO_SEL_TGT_CROP;
84+
sel.rect.left = CONFIG_VIDEO_SOURCE_CROP_LEFT;
85+
sel.rect.top = CONFIG_VIDEO_SOURCE_CROP_TOP;
86+
sel.rect.width = CONFIG_VIDEO_SOURCE_CROP_WIDTH;
87+
sel.rect.height = CONFIG_VIDEO_SOURCE_CROP_HEIGHT;
88+
if (video_set_selection(video_dev, &sel)) {
89+
LOG_ERR("Unable to set selection crop");
90+
return 0;
91+
}
92+
LOG_INF("Selection crop set to (%u,%u)/%ux%u",
93+
sel.rect.left, sel.rect.top, sel.rect.width, sel.rect.height);
94+
#endif
95+
7796
/* Set format */
7897
fmt.width = CONFIG_VIDEO_WIDTH;
7998
fmt.height = CONFIG_VIDEO_HEIGHT;
8099
fmt.pixelformat = VIDEO_PIX_FMT_RGB565;
81100

101+
/*
102+
* Check (if possible) if targeted size is same as crop
103+
* and if compose is necessary
104+
*/
105+
sel.target = VIDEO_SEL_TGT_CROP;
106+
err = video_get_selection(video_dev, &sel);
107+
if (err < 0 && err != -ENOSYS) {
108+
LOG_ERR("Unable to get selection crop");
109+
return 0;
110+
}
111+
112+
if (err == 0 && (sel.rect.width != fmt.width || sel.rect.height != fmt.height)) {
113+
sel.target = VIDEO_SEL_TGT_COMPOSE;
114+
sel.rect.left = 0;
115+
sel.rect.top = 0;
116+
sel.rect.width = fmt.width;
117+
sel.rect.height = fmt.height;
118+
err = video_set_selection(video_dev, &sel);
119+
if (err < 0 && err != -ENOSYS) {
120+
LOG_ERR("Unable to set selection compose");
121+
return 0;
122+
}
123+
}
124+
82125
if (video_set_format(video_dev, &fmt)) {
83126
LOG_ERR("Unable to set up video format");
84127
return 0;
@@ -142,8 +185,6 @@ int main(void)
142185
/* Grab video frames */
143186
vbuf->type = type;
144187
while (1) {
145-
int err;
146-
147188
err = video_dequeue(video_dev, &vbuf, K_FOREVER);
148189
if (err) {
149190
LOG_ERR("Unable to dequeue video buf");

0 commit comments

Comments
 (0)