static void sample1 (FILE * file) { #define BUFFER_SIZE 4096 uint8_t buffer[BUFFER_SIZE]; mpeg2dec_t * mpeg2dec; const mpeg2_info_t * info; int state; int size; int framenum = 0; mpeg2dec = mpeg2_init (); if (mpeg2dec == NULL) exit (1); info = mpeg2_info (mpeg2dec); size = BUFFER_SIZE; do { state = mpeg2_parse (mpeg2dec); switch (state) { case -1: size = fread (buffer, 1, BUFFER_SIZE, file); mpeg2_buffer (mpeg2dec, buffer, buffer + size); break; case STATE_SLICE: case STATE_END: if (info->display_fbuf) save_pgm (info->sequence->width, info->sequence->height, info->display_fbuf->buf, framenum++); break; } } while (size); mpeg2_close (mpeg2dec); }
void save_img(std::string filename, const Matrix<Color>& img) { std::size_t n = filename.size(); std::string extension3 = filename.substr(n-3, n); std::string extension4 = filename.substr(n-4, n); if(!extension3.compare("bmp")) { save_bmp(filename, img); } else if(!extension3.compare("gif")) { save_gif(filename, img); } else if(!extension3.compare("ico")) { save_ico(filename, img); } /*else if(!extension3.compare("jpg")) { save_jpeg(filename, img); }*/ else if(!extension3.compare("pcx")) { save_pcx(filename, img); } else if(!extension3.compare("png")) { save_png(filename, img); } else if(!extension3.compare("pbm")) { save_pbm(filename, color2bwimage(img)); } else if(!extension3.compare("pgm")) { save_pgm(filename, color2grayimage(img)); } else if(!extension3.compare("ppm")) { save_ppm(filename, img); } else if(!extension3.compare("tga")) { save_tga(filename, img); } else if(!extension4.compare("tiff")) { save_tiff(filename, img); } }
static void sample1 (FILE * mpgfile) { #define BUFFER_SIZE 4096 uint8_t buffer[BUFFER_SIZE]; mpeg2dec_t * decoder; const mpeg2_info_t * info; const mpeg2_sequence_t * sequence; mpeg2_state_t state; size_t size; int framenum = 0; decoder = mpeg2_init (); if (decoder == NULL) { fprintf (stderr, "Could not allocate a decoder object.\n"); exit (1); } info = mpeg2_info (decoder); size = (size_t)-1; do { state = mpeg2_parse (decoder); sequence = info->sequence; switch (state) { case STATE_BUFFER: size = fread (buffer, 1, BUFFER_SIZE, mpgfile); mpeg2_buffer (decoder, buffer, buffer + size); break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: if (info->display_fbuf) save_pgm (sequence->width, sequence->height, sequence->chroma_width, sequence->chroma_height, info->display_fbuf->buf, framenum++); break; default: break; } } while (size); mpeg2_close (decoder); }
int main(int argc, char** argv) { CamContext **cc; unsigned char **pixels; int num_cameras; int num_buffers; double last_fps_print; int n_frames; int have_frame; int buffer_size; int num_modes, num_props; char mode_string[255]; int i,mode_number,camno; CameraPropertyInfo cam_props; long prop_value; int prop_auto; int errnum; int left, top, width, height; int do_num_frames; CameraPixelCoding coding; cam_iface_constructor_func_t new_CamContext; char save_fname[100]; cam_iface_startup_with_version_check(); _check_error(); if (argc>1) { if (strcmp(argv[1],"forever")==0) { do_num_frames = -1; } else if (sscanf(argv[1],"%d",&do_num_frames)==0) { show_usage(argv[0]); } } else { do_num_frames = 50; } for (i=0;i<argc;i++) { printf("%d: %s\n",i,argv[i]); } printf("using driver %s\n",cam_iface_get_driver_name()); num_cameras = cam_iface_get_num_cameras(); printf("%d cameras found\n",cam_iface_get_num_cameras()); cc = (CamContext**)malloc( num_cameras*sizeof(CamContext**)); if (cc==NULL) { printf("error allocating memory, will now exit\n"); cam_iface_shutdown(); _check_error(); exit(1); } pixels = (unsigned char **)malloc( num_cameras*sizeof(unsigned char**)); if (pixels==NULL) { printf("error allocating memory, will now exit\n"); cam_iface_shutdown(); _check_error(); exit(1); } for (camno=0; camno<num_cameras; camno++) { printf("initializing camera number %d\n",camno); cam_iface_get_num_modes(camno, &num_modes); _check_error(); printf("%d mode(s) available:\n",num_modes); mode_number = 0; for (i=0; i<num_modes; i++) { cam_iface_get_mode_string(camno,i,mode_string,255); if (strstr(mode_string,"FORMAT7_0")!=NULL) { if (strstr(mode_string,"MONO8")!=NULL) { // pick this mode mode_number = i; } } printf(" %d: %s\n",i,mode_string); } num_buffers = 5; new_CamContext = cam_iface_get_constructor_func(camno); cc[camno] = new_CamContext(camno,num_buffers,mode_number,NULL); _check_error(); CamContext_get_frame_roi(cc[camno], &left, &top, &width, &height); _check_error(); CamContext_get_num_framebuffers(cc[camno],&num_buffers); printf("allocated %d buffers\n",num_buffers); CamContext_get_num_camera_properties(cc[camno],&num_props); _check_error(); for (i=0; i<num_props; i++) { CamContext_get_camera_property_info(cc[camno],i,&cam_props); _check_error(); if (strcmp(cam_props.name,"white balance")==0) { fprintf(stderr,"WARNING: ignoring white balance property\n"); continue; } if (cam_props.is_present) { CamContext_get_camera_property(cc[camno],i,&prop_value,&prop_auto); _check_error(); printf(" %s: %ld (%s)\n",cam_props.name,prop_value, prop_auto ? "AUTO" : "MANUAL"); } else { printf(" %s: not present\n",cam_props.name); } if (cam_props.has_auto_mode) { prop_auto = 1; CamContext_set_camera_property(cc[camno],i,prop_value,prop_auto); _check_error(); printf(" %s: set to AUTO\n",cam_props.name); } } CamContext_get_buffer_size(cc[camno],&buffer_size); _check_error(); if (buffer_size == 0) { fprintf(stderr,"buffer size was 0 in %s, line %d\n",__FILE__,__LINE__); exit(1); } pixels[camno] = (unsigned char *)malloc( buffer_size ); if (pixels[camno]==NULL) { fprintf(stderr,"couldn't allocate memory in %s, line %d\n",__FILE__,__LINE__); exit(1); } CamContext_start_camera(cc[camno]); _check_error(); } last_fps_print = my_floattime(); n_frames = 0; if (do_num_frames < 0) { printf("will now run forever. press Ctrl-C to interrupt\n"); } else { printf("will now grab %d frames.\n",do_num_frames); } while (1) { if (do_num_frames<0) break; have_frame = 0; for (camno=0; camno<num_cameras; camno++) { CamContext_grab_next_frame_blocking(cc[camno],pixels[camno],0.001f); // timeout after 1 msec errnum = cam_iface_have_error(); if (errnum == CAM_IFACE_FRAME_TIMEOUT) { cam_iface_clear_error(); continue; // wait again on next camera } else if (errnum == CAM_IFACE_FRAME_DATA_MISSING_ERROR) { cam_iface_clear_error(); fprintf(stdout,"M"); fflush(stdout); continue; // wait again on next camera } else if (errnum == CAM_IFACE_FRAME_INTERRUPTED_SYSCALL) { cam_iface_clear_error(); fprintf(stdout,"I"); fflush(stdout); continue; // wait again on next camera } _check_error(); have_frame = 1; fprintf(stdout,"%d",camno); fflush(stdout); } if (!have_frame) { continue; } do_num_frames--; } for (camno=0; camno<num_cameras; camno++) { coding = cc[camno]->coding; printf("\n"); delete_CamContext(cc[camno]); _check_error(); if (coding==CAM_IFACE_MONO8) { snprintf(save_fname, 100, "image_camera%d.pgm", camno); save_pgm(save_fname, pixels[camno], width, height); printf("saved last image as %s\n",save_fname); } else { printf("do not know how to save sample image for this format\n"); } free(pixels[camno]); } free(pixels); cam_iface_shutdown(); _check_error(); return 0; }
static void sample5 (FILE * mpgfile) { #define BUFFER_SIZE 4096 #define ALIGN_16(p) ((void *)(((uintptr_t)(p) + 15) & ~((uintptr_t)15))) uint8_t buffer[BUFFER_SIZE]; mpeg2dec_t * decoder; const mpeg2_info_t * info; const mpeg2_sequence_t * sequence; mpeg2_state_t state; size_t size; int framenum = 0; int i, j; struct fbuf_s * current_fbuf; decoder = mpeg2_init (); if (decoder == NULL) { fprintf (stderr, "Could not allocate a decoder object.\n"); exit (1); } info = mpeg2_info (decoder); size = (size_t)-1; do { state = mpeg2_parse (decoder); sequence = info->sequence; switch (state) { case STATE_BUFFER: size = fread (buffer, 1, BUFFER_SIZE, mpgfile); mpeg2_buffer (decoder, buffer, buffer + size); break; case STATE_SEQUENCE: mpeg2_custom_fbuf (decoder, 1); for (i = 0; i < 3; i++) { fbuf[i].mbuf[0] = (uint8_t *) malloc (sequence->width * sequence->height + 15); fbuf[i].mbuf[1] = (uint8_t *) malloc (sequence->chroma_width * sequence->chroma_height + 15); fbuf[i].mbuf[2] = (uint8_t *) malloc (sequence->chroma_width * sequence->chroma_height + 15); if (!fbuf[i].mbuf[0] || !fbuf[i].mbuf[1] || !fbuf[i].mbuf[2]) { fprintf (stderr, "Could not allocate an output buffer.\n"); exit (1); } for (j = 0; j < 3; j++) fbuf[i].yuv[j] = ALIGN_16 (fbuf[i].mbuf[j]); fbuf[i].used = 0; } for (i = 0; i < 2; i++) { current_fbuf = get_fbuf (); mpeg2_set_buf (decoder, current_fbuf->yuv, current_fbuf); } break; case STATE_PICTURE: current_fbuf = get_fbuf (); mpeg2_set_buf (decoder, current_fbuf->yuv, current_fbuf); break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: if (info->display_fbuf) save_pgm (sequence->width, sequence->height, sequence->chroma_width, sequence->chroma_height, info->display_fbuf->buf, framenum++); if (info->discard_fbuf) ((struct fbuf_s *)info->discard_fbuf->id)->used = 0; if (state != STATE_SLICE) for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) free (fbuf[i].mbuf[j]); break; default: break; } } while (size); mpeg2_close (decoder); }
static void sample3 (FILE * mpgfile) { #define BUFFER_SIZE 4096 uint8_t buffer[BUFFER_SIZE]; mpeg2dec_t * decoder; const mpeg2_info_t * info; const mpeg2_sequence_t * sequence; mpeg2_state_t state; size_t size; int framenum = 0; uint8_t * fbuf[3][3]; int i, j; decoder = mpeg2_init (); if (decoder == NULL) { fprintf (stderr, "Could not allocate a decoder object.\n"); exit (1); } info = mpeg2_info (decoder); size = (size_t)-1; do { state = mpeg2_parse (decoder); sequence = info->sequence; switch (state) { case STATE_BUFFER: size = fread (buffer, 1, BUFFER_SIZE, mpgfile); mpeg2_buffer (decoder, buffer, buffer + size); break; case STATE_SEQUENCE: for (i = 0; i < 3; i++) { fbuf[i][0] = (uint8_t *) malloc (sequence->width * sequence->height); fbuf[i][1] = (uint8_t *) malloc (sequence->chroma_width * sequence->chroma_height); fbuf[i][2] = (uint8_t *) malloc (sequence->chroma_width * sequence->chroma_height); if (!fbuf[i][0] || !fbuf[i][1] || !fbuf[i][2]) { fprintf (stderr, "Could not allocate an output buffer.\n"); exit (1); } mpeg2_set_buf (decoder, fbuf[i], NULL); } break; case STATE_SLICE: case STATE_END: case STATE_INVALID_END: if (info->display_fbuf) save_pgm (sequence->width, sequence->height, sequence->chroma_width, sequence->chroma_height, info->display_fbuf->buf, framenum++); if (state != STATE_SLICE) for (i = 0; i < 3; i++) for (j = 0; j < 3; j++) free (fbuf[i][j]); break; default: break; } } while (size); mpeg2_close (decoder); }