static void load_image(Job *job) { int pnm_type; if (!job->input_path) { fprintf(stderr, "You must specify a path to input image\n"); usage(); } pnm_type = load_pnm(job->input_path, &job->pixels, &job->width, &job->height); if (pnm_type != PBM) { fprintf(stderr, "The image is not a PBM file\n"); exit(1); } }
int main(int argc, char *argv[]) { GtkWidget *window; GtkWidget *main_vbox; GtkWidget *menubar; GtkWidget *vbox; g_thread_init(NULL); gtk_init(&argc, &argv); gdk_init (&argc, &argv); gdk_rgb_init(); // buf= (unsigned char *)malloc (ROWS * COLS * DEPTH * sizeof (unsigned char)); //if (!buf) { // perror ("malloc buf"); // exit(0); // } open_dev(); /* abre e inicializa video */ window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_signal_connect(GTK_OBJECT(window), "destroy", GTK_SIGNAL_FUNC(gtk_main_quit), "WM destroy"); gtk_window_set_title(GTK_WINDOW(window), "NutoScope"); //gtk_widget_set_usize(GTK_WIDGET(window), COLS, ROWS); main_vbox = gtk_vbox_new(FALSE, 1); gtk_container_border_width(GTK_CONTAINER(main_vbox), 1); gtk_container_add(GTK_CONTAINER(window), main_vbox); gtk_widget_show(main_vbox); get_main_menu(window, &menubar); gtk_box_pack_start(GTK_BOX(main_vbox), menubar, FALSE, TRUE, 0); gtk_widget_show(menubar); darea = gtk_drawing_area_new(); gtk_box_pack_start (GTK_BOX (main_vbox), darea, FALSE, FALSE, 0); gtk_signal_connect (GTK_OBJECT (darea), "expose-event", GTK_SIGNAL_FUNC (on_darea_expose), (gpointer) darea); gtk_drawing_area_size (GTK_DRAWING_AREA (darea), COLS, ROWS); gtk_widget_show(darea); status = gtk_statusbar_new (); gtk_box_pack_start (GTK_BOX (main_vbox), status, FALSE, FALSE, 0); context_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(status), "Ready"); gtk_widget_show (status); gtk_widget_show(window); // load 50x50x3 logo logo_image=malloc(50*50*3); if (!logo_image) { perror("malloc logo "); exit; } load_pnm("logo.pnm", logo_image); // Queue frames for(frame=0; frame<grab_vm.frames; frame++) { grab_buf.frame = frame; if(ioctl(grab_fd, VIDIOCMCAPTURE, &grab_buf)<0) { perror("VIDIOCMCAPTURE"); exit(-1); } } gtk_timeout_add(25, send_expose, NULL); gdk_threads_enter(); gtk_main(); gdk_threads_leave(); return (0); }
int grab_loop(struct vd_video *vd){ #ifdef STAMP time_t t; struct tm *tm; char stringo[128]; #endif #ifdef BENCH time_t ini,fim; int a=0; #endif #ifdef MOTION struct image_motion _ref_image, _cur_image, mask; int avg=0; #endif int frame=0, i; #ifdef MOTION /* initialize _ref_image, our reference image */ if ((_ref_image.image=malloc(vd->x * vd->y * vd->w))==NULL) { perror("grab_loop: malloc _ref_image"); exit(-1); } memset (_ref_image.image, 0, vd->grab_size); _ref_image.x=vd->x; _ref_image.y=vd->y; _ref_image.depth=vd->w; if ((mask.image=malloc(vd->x * vd->y * vd->w))==NULL) { perror("grab_loop: malloc mask"); exit(-1); } load_pnm ("mask.pnm", &mask); #endif /* Query the actual buffers available */ if(ioctl(vd->grab_fd, VIDIOCGMBUF, &(vd->grab_vm)) < 0) { perror("VIDIOCGMBUF"); exit(-1); } #ifdef DEBUG fprintf (stderr,"vm.size=%d ; grab_size= %d\n", vd->grab_vm.size, vd->grab_size); #endif for(frame=0; frame < vd->grab_vm.frames; frame++) { vd->grab_buf.frame = frame; if(ioctl(vd->grab_fd, VIDIOCMCAPTURE, &(vd->grab_buf))<0) { perror("VIDIOCMCAPTURE"); exit(-1); } } frame = 0; #ifdef BENCH time(&ini); #endif while(1) { i = -1; /* waits for the frame sync */ while ((ioctl(vd->grab_fd, VIDIOCSYNC, &frame)) < 0) { #ifdef BENCH fprintf(stderr, "Waiting frame: %d\n", frame); #endif } /* refer the frame */ vd->frame=frame; vd->image=vd->grab_data + vd->grab_vm.offsets[frame]; //vd->image=vd->grab_data; if (vd->w == 3 && vd->grab_data) { /* original or faster swap function */ #ifdef BGR2RGB bgr2rgb(vd->image, vd->grab_buf.width * vd->grab_buf.height); #else swap_rgb24(vd->image, vd->grab_buf.width * vd->grab_buf.height); #endif #ifdef DEBUG fprintf (stderr,"rgb swap for frame (%d)\n", frame); fprintf (stderr,"image addr %p\n", vd->image); fprintf (stderr,"offset %d \n", vd->grab_vm.offsets[frame]); #endif } // change channel. it needs to keep each channel // properties if (vd->cycleinput) cycle_channel(vd); /* HOOK for effects */ /* save it */ #ifdef MOTION /* test for motion */ _cur_image.image=vd->image; _cur_image.x=vd->x; _cur_image.y=vd->y; _cur_image.depth=vd->w; avg=detect_motion_mask(&_ref_image, &_cur_image, &mask, 10); // our tolerance at +-10 pixels //avg=subtract_motion(&_ref_image, &_cur_image, 50); // our tolerance at +-10 pixels //fprintf(stderr,"Average changes: %d ", avg); //if (_ref_image.left > _cur_image.right ) fprintf (stderr, " - left (l: %d r: %d) ", _ref_image.left, _cur_image.right); //if (_ref_image.right > _cur_image.left ) fprintf (stderr, " - right (l: %d r: %d) ", _cur_image.left, _ref_image.right); //fprintf(stderr,"\r"); #ifdef STAMP /* initial tests for the timestamp function */ time (&t); tm = localtime (&t); if (vd->stamp) { // validation /* if (vd->stamp_scale_w <= 0) vd->stamp_scale_w=1; if (vd->stamp_scale_h <= 0) vd->stamp_scale_h=1; */ strftime (stringo, sizeof (stringo), vd->stamp, tm); rgb24_printf(vd->stamp_pos_x, vd->stamp_pos_y, vd->stamp_scale_w, vd->stamp_scale_h, vd, "%s -> %d", stringo, vd->channel); } #endif // threshold //threshold (vd, 127); //median_filter(vd); //mark_areas(vd); // sensibility // if (avg > ((vd->grab_size/vd->w)/10)) loop_img(vd); // saves when there are more than ~5% of elements have changed. #endif loop_img(vd); /* Once buf is no longer in use by the application... */ vd->grab_buf.frame = frame; if(ioctl(vd->grab_fd, VIDIOCMCAPTURE, &(vd->grab_buf))<0) { perror("VIDIOCMCAPTURE"); exit(-1); } /* next frame */ frame++; /* reset to the 1st frame */ if (frame>=vd->grab_vm.frames) { #ifdef DEBUG fprintf(stderr,"Max buffers %d\n", frame -1); #endif frame = 0; } #ifdef BENCH if (a == 30) { time (&fim); fprintf (stderr, "30 frames/%lu sec\n", (unsigned long)fim -ini); a=0; ini=fim; } else a++; #endif } // loop }