Skip to content

Commit

Permalink
Added 'save frame to ppm' button to Linux/GTK3 test program
Browse files Browse the repository at this point in the history
  • Loading branch information
trcwm committed Jul 24, 2017
1 parent 9091337 commit 1e9e627
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions linux/tests/gtkmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct
CapContext ctx;
int32_t streamID;
bool takeSnapshot;
uint32_t snapshotCounter;
} TimerCallbackInfo;


Expand All @@ -43,6 +44,14 @@ static void activate(GtkApplication* app,
gtk_widget_show_all (window);
}

static void triggerSnapshot(GtkWidget *widget,
gpointer data)
{
TimerCallbackInfo *id = (TimerCallbackInfo*)data;
id->takeSnapshot = true;
printf("Snapshot triggered!\n");
}

bool writeBufferAsPPM(uint32_t frameNum, uint32_t width, uint32_t height, const uint8_t *bufferPtr, size_t bytes)
{
char fname[100];
Expand Down Expand Up @@ -82,7 +91,7 @@ int updatePicture(gpointer data)
if (id->takeSnapshot)
{
id->takeSnapshot = false;
writeBufferAsPPM(0, id->cols, id->rows, g, id->cols * id->rows * 3);
writeBufferAsPPM(id->snapshotCounter++, id->cols, id->rows, g, id->cols * id->rows * 3);
}
}
else
Expand Down Expand Up @@ -111,6 +120,9 @@ int main (int argc, char *argv[])
// https://cboard.cprogramming.com/c-programming/172801-how-display-2d-array-image-using-gtk3plus.html

GtkApplication *app;
GtkWidget *button;
GtkWidget *button_box;

int status;

uint32_t deviceFormatID = 0;
Expand Down Expand Up @@ -190,7 +202,8 @@ int main (int argc, char *argv[])
id.stride += (4 - id.stride % 4) % 4; // ensure multiple of 4
id.ctx = ctx;
id.streamID = streamID;
id.takeSnapshot = true;
id.takeSnapshot = false;
id.snapshotCounter = 0;

guchar *pixels = (guchar *)calloc(finfo.height * id.stride, 1);

Expand Down Expand Up @@ -222,7 +235,15 @@ int main (int argc, char *argv[])
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(id.image));


button_box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 10);
gtk_container_add(GTK_CONTAINER (window), button_box);

button = gtk_button_new_with_label ("Save Frame to .ppm");
g_signal_connect (button, "clicked", G_CALLBACK (triggerSnapshot), &id);
gtk_container_add(GTK_CONTAINER(button_box), GTK_WIDGET(id.image));
gtk_container_add(GTK_CONTAINER(button_box), button);

g_timeout_add(250, // milliseconds
updatePicture, // handler function
Expand Down

0 comments on commit 1e9e627

Please sign in to comment.