Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify text drawing #23

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,6 @@ static void on_request_complete(Request *request) {

camp->frame_cb(
camp->mapped_buffers.at(buffer),
camp->video_stream->configuration().stride,
camp->video_stream->configuration().size.height,
buffer->planes()[0].fd.get(),
buffer_size(buffer->planes()),
ts);
Expand All @@ -310,12 +308,12 @@ static void on_request_complete(Request *request) {
camp->camera->queueRequest(request);
}

int camera_get_mode_stride(camera_t *cam) {
int camera_get_stride(camera_t *cam) {
CameraPriv *camp = (CameraPriv *)cam;
return camp->video_stream->configuration().stride;
}

int camera_get_mode_colorspace(camera_t *cam) {
int camera_get_colorspace(camera_t *cam) {
CameraPriv *camp = (CameraPriv *)cam;
return get_v4l2_colorspace(camp->video_stream->configuration().colorSpace);
}
Expand Down
6 changes: 2 additions & 4 deletions camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ typedef void camera_t;

typedef void (*camera_frame_cb)(
uint8_t *mapped_buffer,
int stride,
int height,
int buffer_fd,
uint64_t size,
uint64_t timestamp);
Expand All @@ -19,8 +17,8 @@ extern "C" {

const char *camera_get_error();
bool camera_create(const parameters_t *params, camera_frame_cb frame_cb, camera_t **cam);
int camera_get_mode_stride(camera_t *cam);
int camera_get_mode_colorspace(camera_t *cam);
int camera_get_stride(camera_t *cam);
int camera_get_colorspace(camera_t *cam);
bool camera_start(camera_t *cam);
void camera_reload_params(camera_t *cam, const parameters_t *params);

Expand Down
5 changes: 4 additions & 1 deletion encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

typedef void encoder_t;

typedef void (*encoder_output_cb)(uint64_t ts, const uint8_t *buf, uint64_t size);
typedef void (*encoder_output_cb)(
uint64_t ts,
const uint8_t *buf,
uint64_t size);

const char *encoder_get_error();
bool encoder_create(const parameters_t *params, int stride, int colorspace, encoder_output_cb output_cb, encoder_t **enc);
Expand Down
13 changes: 7 additions & 6 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ static encoder_t *enc;

static void on_frame(
uint8_t *mapped_buffer,
int stride,
int height,
int buffer_fd,
uint64_t size,
uint64_t timestamp) {
text_draw(text, mapped_buffer, stride, height);
text_draw(text, mapped_buffer);
encoder_encode(enc, mapped_buffer, buffer_fd, size, timestamp);
}

Expand Down Expand Up @@ -67,16 +65,19 @@ int main() {
return -1;
}

ok = text_create(&params, &text);
ok = text_create(
&params,
camera_get_stride(cam),
&text);
if (!ok) {
pipe_write_error(pipe_video_fd, "text_create(): %s", text_get_error());
return -1;
}

ok = encoder_create(
&params,
camera_get_mode_stride(cam),
camera_get_mode_colorspace(cam),
camera_get_stride(cam),
camera_get_colorspace(cam),
on_encoder_output,
&enc);
if (!ok) {
Expand Down
16 changes: 10 additions & 6 deletions text.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@ const char *text_get_error() {
typedef struct {
bool enabled;
char *text_overlay;
int stride;
int height;
FT_Library library;
FT_Face face;
} text_priv_t;

bool text_create(const parameters_t *params, text_t **text) {
bool text_create(const parameters_t *params, int stride, text_t **text) {
*text = malloc(sizeof(text_priv_t));
text_priv_t *textp = (text_priv_t *)(*text);
memset(textp, 0, sizeof(text_priv_t));

textp->enabled = params->text_overlay_enable;
textp->text_overlay = strdup(params->text_overlay);
textp->stride = stride;
textp->height = params->height;

if (textp->enabled) {
int error = FT_Init_FreeType(&textp->library);
Expand Down Expand Up @@ -130,7 +134,7 @@ static int get_text_width(FT_Face face, const char *text) {
return ret;
}

void text_draw(text_t *text, uint8_t *buf, int stride, int height) {
void text_draw(text_t *text, uint8_t *buf) {
text_priv_t *textp = (text_priv_t *)text;

if (textp->enabled) {
Expand All @@ -142,8 +146,8 @@ void text_draw(text_t *text, uint8_t *buf, int stride, int height) {

draw_rect(
buf,
stride,
height,
textp->stride,
textp->height,
7,
7,
get_text_width(textp->face, buffer) + 10,
Expand All @@ -160,8 +164,8 @@ void text_draw(text_t *text, uint8_t *buf, int stride, int height) {

draw_bitmap(
buf,
stride,
height,
textp->stride,
textp->height,
&textp->face->glyph->bitmap,
x + textp->face->glyph->bitmap_left,
y - textp->face->glyph->bitmap_top);
Expand Down
4 changes: 2 additions & 2 deletions text.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
typedef void text_t;

const char *text_get_error();
bool text_create(const parameters_t *params, text_t **text);
void text_draw(text_t *text, uint8_t *buf, int stride, int height);
bool text_create(const parameters_t *params, int stride, text_t **text);
void text_draw(text_t *text, uint8_t *buf);

#endif