/** use free() to free data. */ int store_picture_to_buffer(guint8 **data, guint *data_len) { struct GLOBAL *global = all_data.global; struct vdIn *videoIn = all_data.videoIn; struct JPEG_ENCODER_STRUCTURE *jpeg_struct = NULL; BYTE *pim = NULL; BYTE *jpeg = NULL; int jpeg_size = 0; /* TODO snapshot by rock. I save raw (no filter, no image processing) frame in snapshot. So here should save picture from snapshot, not framebuffer. */ switch(global->imgFormat) { case 0:/*jpg*/ /* Save directly from MJPG frame */ #if 0 // TODO #1432, some webcam's mjpeg frame could not directly save as jpeg. if((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_MJPEG)) { if(SaveJPG(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } } else #endif if ((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_JPEG)) { *data_len = videoIn->buf.bytesused; *data = malloc(videoIn->buf.bytesused); memmove(*data, videoIn->tmpbuffer, videoIn->buf.bytesused); #if 0 if (SaveBuff(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif } else { /* use built in encoder */ jpeg = g_new0(BYTE, ((global->width)*(global->height))>>1); jpeg_struct = g_new0(struct JPEG_ENCODER_STRUCTURE, 1); /* Initialization of JPEG control structure */ initialization (jpeg_struct,global->width,global->height); /* Initialization of Quantization Tables */ initialize_quantization_tables (jpeg_struct); //jpeg_size = encode_image(videoIn->framebuffer, jpeg, jpeg_size = encode_image(videoIn->snapshot, jpeg, jpeg_struct, 1, global->width, global->height); *data_len = jpeg_size; *data = malloc(jpeg_size); memmove(*data, jpeg, jpeg_size); #if 0 if(SaveBuff(videoIn->ImageFName, jpeg_size, jpeg)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif } break; case 1:/*bmp*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2bgr(videoIn->framebuffer,pim,global->width,global->height); yuyv2bgr(videoIn->snapshot,pim,global->width,global->height); _store_picture_to_buffer_bmp(global->width, global->height, 24, pim, data, data_len); #if 0 if(SaveBPM(videoIn->ImageFName, global->width, global->height, 24, pim)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } #endif break; case 2:/*png*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2rgb(videoIn->framebuffer,pim,global->width,global->height); yuyv2rgb(videoIn->snapshot,pim,global->width,global->height); _store_picture_to_buffer_png(global->width, global->height, pim, data, data_len); //write_png(videoIn->ImageFName, global->width, global->height, pim); break; case 3:/*raw*/ videoIn->cap_raw = 1; return 1; } if(jpeg_struct) g_free(jpeg_struct); jpeg_struct=NULL; if(jpeg) g_free(jpeg); jpeg = NULL; if(pim) g_free(pim); pim=NULL; return 0; }
int main_loop(void *data) { int ret=0; int i,j,k,l,m,n,o; unsigned int YUVMacroPix; unsigned char *pix8 = (unsigned char *)&YUVMacroPix; Pix *pix2; char *pix; if ((pix2= malloc(sizeof(Pix)))==NULL) { printf("couldn't allocate memory for: pix2\n"); ret=1; return(ret); } //fprintf(stderr,"Thread started...\n"); /* ImageSurf=SDL_CreateRGBSurface(SDL_SWSURFACE, overlay->w, overlay->h, 24, 0x00ff0000,0x0000ff00,0x000000ff,0); */ while (videoIn->signalquit) { currtime = SDL_GetTicks(); if (currtime - lasttime > 0) { frmrate = 1000/(currtime - lasttime); } lasttime = currtime; // sprintf(capt,"Frame Rate: %d",frmrate); // SDL_WM_SetCaption(capt, NULL); if (uvcGrab(videoIn) < 0) { printf("Error grabbing=> Frame Rate is %d\n",frmrate); videoIn->signalquit=0; ret = 2; } SDL_LockYUVOverlay(overlay); memcpy(p, videoIn->framebuffer, videoIn->width * (videoIn->height) * 2); SDL_UnlockYUVOverlay(overlay); SDL_DisplayYUVOverlay(overlay, &drect); /*capture Image*/ if (videoIn->capImage){ if((pim= malloc((pscreen->w)*(pscreen->h)*3))==NULL){/*24 bits -> 3bytes 32 bits ->4 bytes*/ printf("Couldn't allocate memory for: pim\n"); videoIn->signalquit=0; ret = 3; } //char *ppmheader = "P6\n# Generated by guvcview\n320 240\n255\n"; //FILE * out = fopen("Yimage.ppm", "wb"); //saving as ppm //fprintf(out, ppmheader); k=overlay->h; //printf("overlay->h is %i\n",overlay->h); //printf("and pitches[0] is %i\n",overlay->pitches[0]); for(j=0;j<(overlay->h);j++){ l=j*overlay->pitches[0];/*must add lines already writen=*/ /*pitches is the overlay number */ /*off bytes in a line (2*width) */ m=(k*3*overlay->pitches[0])>>1;/*must add lines already writen= */ /*for this case (rgb) every pixel */ /*as 3 bytes (3*width=3*pitches/2) */ /* >>1 = (/2) divide by 2 (?faster?)*/ for (i=0;i<((overlay->pitches[0])>>2);i++){ /*>>2 = (/4)*/ /*iterate every 4 bytes (32 bits)*/ /*Y-U-V-Y1 =>2 pixel (4 bytes) */ n=i<<2;/*<<2 = (*4) multiply by 4 (?faster?)*/ pix8[0] = p[n+l]; pix8[1] = p[n+1+l]; pix8[2] = p[n+2+l]; pix8[3] = p[n+3+l]; /*get RGB data*/ pix2=yuv2rgb(YUVMacroPix,0,pix2); /*In BitMaps lines are upside down and*/ /*pixel format is bgr */ o=i*6; /*first pixel*/ pim[o+m]=pix2->b; pim[o+1+m]=pix2->g; pim[o+2+m]=pix2->r; /*second pixel*/ pim[o+3+m]=pix2->b1; pim[o+4+m]=pix2->g1; pim[o+5+m]=pix2->r1; } k--; } /* SDL_LockSurface(ImageSurf); memcpy(pix, pim,(pscreen->w)*(pscreen->h)*3); //24 bits -> 3bytes 32 bits ->4 bytes SDL_UnlockSurface(ImageSurf);*/ if(SaveBPM(videoIn->ImageFName, width, height, 24, pim)) { fprintf (stderr,"Error: Couldn't capture Image to %s \n", videoIn->ImageFName); } else { printf ("Capture Image to %s \n",videoIn->ImageFName); } free(pim); videoIn->capImage=FALSE; } /*capture AVI */ if (videoIn->capAVI && videoIn->signalquit){ long framesize; switch (AVIFormat) { case 1: framesize=(pscreen->w)*(pscreen->h)*2; /*YUY2 -> 2 bytes per pixel */ if (AVI_write_frame (AviOut, p, framesize) < 0) printf ("write error on avi out \n"); break; case 2: framesize=(pscreen->w)*(pscreen->h)*3; /*DIB 24/32 -> 3/4 bytes per pixel*/ if((pim= malloc(framesize))==NULL){ printf("Couldn't allocate memory for: pim\n"); videoIn->signalquit=0; ret = 4; } k=overlay->h; for(j=0;j<(overlay->h);j++){ l=j*overlay->pitches[0];/*must add lines already writen=*/ /*pitches is the overlay number */ /*off bytes in a line (2*width) */ m=(k*3*overlay->pitches[0])>>1;/*must add lines already writen= */ /*for this case (rgb) every pixel */ /*as 3 bytes (3*width=3*pitches/2) */ /* >>1 = (/2) divide by 2 (?faster?)*/ for (i=0;i<((overlay->pitches[0])>>2);i++){ /*>>2 = (/4)*/ /*iterate every 4 bytes (32 bits)*/ /*Y-U-V-Y1 =>2 pixel (4 bytes) */ n=i<<2;/*<<2 = (*4) multiply by 4 (?faster?)*/ pix8[0] = p[n+l]; pix8[1] = p[n+1+l]; pix8[2] = p[n+2+l]; pix8[3] = p[n+3+l]; /*get RGB data*/ pix2=yuv2rgb(YUVMacroPix,0,pix2); /*In BitMaps lines are upside down and*/ /*pixel format is bgr */ o=i*6; /*first pixel*/ pim[o+m]=pix2->b; pim[o+1+m]=pix2->g; pim[o+2+m]=pix2->r; /*second pixel*/ pim[o+3+m]=pix2->b1; pim[o+4+m]=pix2->g1; pim[o+5+m]=pix2->r1; } k--; } if (AVI_write_frame (AviOut, pim, framesize) < 0) printf ("write error on avi out \n"); free(pim); break; } framecount++; } SDL_Delay(SDL_WAIT_TIME); }
int store_picture(void *data) { struct ALL_DATA *all_data = (struct ALL_DATA *) data; struct GLOBAL *global = all_data->global; struct vdIn *videoIn = all_data->videoIn; struct JPEG_ENCODER_STRUCTURE *jpeg_struct = NULL; BYTE *pim = NULL; BYTE *jpeg = NULL; int jpeg_size = 0; int rc = 0; /* TODO snapshot by rock. I save raw (no filter, no image processing) frame in snapshot. So here should save picture from snapshot, not framebuffer. */ switch(global->imgFormat) { case IMG_FORMAT_JPG:/*jpg*/ /* Save directly from MJPG frame */ #if 0 // TODO #1432, some webcam's mjpeg frame could not directly save as jpeg. if((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_MJPEG)) { if(SaveJPG(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); return(-1); } } else #endif if ((global->Frame_Flags==0) && (global->format==V4L2_PIX_FMT_JPEG)) { if (SaveBuff(videoIn->ImageFName,videoIn->buf.bytesused,videoIn->tmpbuffer)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } } else { /* use built in encoder */ jpeg = g_new0(BYTE, ((global->width)*(global->height))>>1); jpeg_struct = g_new0(struct JPEG_ENCODER_STRUCTURE, 1); /* Initialization of JPEG control structure */ initialization (jpeg_struct,global->width,global->height); /* Initialization of Quantization Tables */ initialize_quantization_tables (jpeg_struct); //jpeg_size = encode_image(videoIn->framebuffer, jpeg, jpeg_size = encode_image(videoIn->snapshot, jpeg, jpeg_struct, 1, global->width, global->height); if(SaveBuff(videoIn->ImageFName, jpeg_size, jpeg)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } } break; case IMG_FORMAT_BMP:/*bmp*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2bgr(videoIn->framebuffer,pim,global->width,global->height); yuyv2bgr(videoIn->snapshot,pim,global->width,global->height); if(SaveBPM(videoIn->ImageFName, global->width, global->height, 24, pim)) { g_printerr ("Error: Couldn't capture Image to %s \n", videoIn->ImageFName); //return(-1); rc = -1; goto end_func; } break; case IMG_FORMAT_PNG:/*png*/ /*24 bits -> 3bytes 32 bits ->4 bytes*/ pim = g_new0(BYTE, (global->width)*(global->height)*3); //yuyv2rgb(videoIn->framebuffer,pim,global->width,global->height); yuyv2rgb(videoIn->snapshot,pim,global->width,global->height); write_png(videoIn->ImageFName, global->width, global->height, pim); break; case IMG_FORMAT_RAW:/*raw*/ videoIn->cap_raw = 1; //return 1; rc = 1; } end_func: if(jpeg_struct) g_free(jpeg_struct); jpeg_struct=NULL; if(jpeg) g_free(jpeg); jpeg = NULL; if(pim) g_free(pim); pim=NULL; return rc; }