static void on_input_frame_ready (CamUnit *super, const CamFrameBuffer *inbuf, const CamUnitFormat *infmt) { dbg(DBG_FILTER, "[%s] iterate\n", cam_unit_get_name(super)); CamConvertJpegCompress * self = (CamConvertJpegCompress*)super; const CamUnitFormat *outfmt = cam_unit_get_output_format(super); int width = infmt->width; int height = infmt->height; int outsize = self->outbuf->length; int quality = cam_unit_control_get_int (self->quality_control); if (infmt->pixelformat == CAM_PIXEL_FORMAT_RGB) _jpeg_compress_8u_rgb (inbuf->data, width, height, infmt->row_stride, self->outbuf->data, &outsize, quality); else if (infmt->pixelformat == CAM_PIXEL_FORMAT_BGRA) _jpeg_compress_8u_bgra (inbuf->data, width, height, infmt->row_stride, self->outbuf->data, &outsize, quality); else if (infmt->pixelformat == CAM_PIXEL_FORMAT_GRAY) _jpeg_compress_8u_gray (inbuf->data, width, height, infmt->row_stride, self->outbuf->data, &outsize, quality); cam_framebuffer_copy_metadata(self->outbuf, inbuf); self->outbuf->bytesused = outsize; cam_unit_produce_frame (super, self->outbuf, outfmt); }
int main (int argc, char **argv) { state_t * self = (state_t*) calloc (1, sizeof (state_t)); self->use_gui = 1; char *optstring = "hc:p:"; int c; struct option long_opts[] = { { "help", no_argument, 0, 'h' }, { "chain", required_argument, 0, 'c' }, { "plugin-path", required_argument, 0, 'p' }, { "no-gui", no_argument, 0, 'u' }, { 0, 0, 0, 0 } }; while ((c = getopt_long (argc, argv, optstring, long_opts, 0)) >= 0) { switch (c) { case 'c': self->xml_fname = strdup (optarg); break; case 'u': self->use_gui = 0; break; case 'p': self->extra_plugin_path = strdup (optarg); break; case 'h': default: usage (); break; }; } if (!self->use_gui && !self->xml_fname) usage(); if (self->use_gui) { gtk_init (&argc, &argv); } else { g_type_init (); } if (!g_thread_supported()) { g_thread_init (NULL); } if (0 != state_setup (self)) { state_cleanup(self); return 1; } if (self->use_gui) { camview_gtk_quit_on_interrupt (); gtk_main (); state_cleanup (self); } else { // did everything start up correctly? CamUnit *faulty = cam_unit_chain_all_units_stream_init(self->chain); if (faulty) { int faulty_index = 0; for(GList *uiter = cam_unit_chain_get_units(self->chain); uiter && uiter->data != faulty; uiter = uiter->next) faulty_index ++; fprintf(stderr, "Unit %d [%s] is not streaming, aborting...\n", faulty_index, cam_unit_get_name (faulty)); return -1; } GMainLoop *mainloop = g_main_loop_new (NULL, FALSE); camview_g_quit_on_interrupt (mainloop); g_main_loop_run (mainloop); g_main_loop_unref (mainloop); state_cleanup (self); } return 0; }
int main(int argc, char **argv) { g_type_init(); CamUnit *resize_unit = NULL; CamUnit *faulty_unit = NULL; CamUnitChain * chain = NULL; const char *input_id = NULL; state_t s; memset (&s, 0, sizeof (s)); sprintf (s.filename, "snapshot-output.ppm"); // create the GLib mainloop s.mainloop = g_main_loop_new (NULL, FALSE); s.lcm = lcm_create ("udpm://?transmit_only=true"); if (!s.lcm) { fprintf (stderr, "Couldn't create LCM\n"); goto failed; } // create the image processing chain chain = cam_unit_chain_new (); // abort if no input unit was specified if (argc < 2) { print_usage_and_inputs (argv[0], chain->manager); goto failed; } input_id = argv[1]; // instantiate the input unit if (! cam_unit_chain_add_unit_by_id (chain, input_id)) { fprintf (stderr, "Oh no! Couldn't create input unit [%s].\n\n", input_id); print_usage_and_inputs (argv[0], chain->manager); goto failed; } // create a unit to convert the input data to 8-bit RGB cam_unit_chain_add_unit_by_id (chain, "convert.to_rgb8"); resize_unit = cam_unit_chain_add_unit_by_id (chain, "filter.ipp.resize"); // cam_unit_set_control_boolean (resize_unit, "aspect-lock", FALSE); cam_unit_set_control_int (resize_unit, "width", 64); // cam_unit_set_control_int (resize_unit, "height", 64); // start the image processing chain faulty_unit = cam_unit_chain_all_units_stream_init (chain); // did everything start up correctly? if (faulty_unit) { fprintf (stderr, "Unit [%s] is not streaming, aborting...\n", cam_unit_get_name (faulty_unit)); goto failed; } // attach the chain to the glib event loop. cam_unit_chain_attach_glib (chain, 1000, NULL); // subscribe to be notified when an image has made its way through the // chain g_signal_connect (G_OBJECT (chain), "frame-ready", G_CALLBACK (on_frame_ready), &s); s.next_publish_utime = _timestamp_now (); // run g_main_loop_run (s.mainloop); // cleanup g_main_loop_unref (s.mainloop); cam_unit_chain_all_units_stream_shutdown (chain); g_object_unref (chain); return 0; failed: g_main_loop_unref (s.mainloop); cam_unit_chain_all_units_stream_shutdown (chain); g_object_unref (chain); return 1; }