/**
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;
}
Example #2
0
	void yuyv_to_bgr(unsigned char* bgr, unsigned char* yuyv) {
		yuyv2bgr(yuyv, bgr, w, h);
	}
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;
}