📄 testcamera.c
字号:
/* * Camera test on S3C2440A * * $Revision: 1.0 $ * * april 2008 PHT * * 飞 凌 嵌 入 式 * www.witech.com.cn */#include <stdio.h>#include <stdlib.h>#include <string.h>#include <fcntl.h>#include <errno.h>#include <unistd.h>#include <termios.h>#include <linux/fb.h>#include <sys/mman.h>#include <sys/ioctl.h>#include <jpeglib.h>#define __user#include <linux/videodev.h>/****************************************************/#define S3C2440A//#define YCbCr_TO_RGB_TEST#define JPEG_SAVE#define FIXED_SOURCE_WIDTH 640#define FIXED_SOURCE_HEIGHT 480#define STILL_IMAGE 1#define VIDEO_START 0#define VIDEO_STOP -1/****************************************************/static struct { int palette; char *name;} optional_image_format[] = { { VIDEO_PALETTE_YUV422P, "YCbCr422 planar", }, { VIDEO_PALETTE_RGB565, "RGB565", }, { VIDEO_PALETTE_RGB24, "RGB24", },};#define MAX_IMAGE_FORMAT (sizeof(optional_image_format)/sizeof(optional_image_format[0]))#ifndef GLOBAL#define GLOBAL //defined in jmorecfg.h#endifstatic unsigned short image_width;static unsigned short image_height;static unsigned char *image_buffer;static unsigned short optimization = 75;static unsigned short image_format;static char *image_file = "jpgfile";static unsigned short file_index;static unsigned short mmap_camera;__u32 Conv_YCbCr_Rgb(__u8 y0, __u8 y1, __u8 cb0, __u8 cr0);/*************************************************************/#ifdef JPEG_SAVEGLOBAL(void)write_JPEG_file (char * filename, int quality){ struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; /* More stuff */ FILE * outfile; /* target file */ JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */ int row_stride; /* physical row width in image buffer */ cinfo.err = jpeg_std_error(&jerr); /* Now we can initialize the JPEG compression object. */ jpeg_create_compress(&cinfo); if ((outfile = fopen(filename, "wb")) == NULL) { fprintf(stderr, "can't open %s\n", filename); exit(1); } jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = image_width; /* image width and height, in pixels */ cinfo.image_height = image_height; cinfo.input_components = 3; /* # of color components per pixel */ cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */); jpeg_start_compress(&cinfo, TRUE); row_stride = image_width * 3; /* JSAMPLEs per row in image_buffer */ while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); } /* Step 6: Finish compression */ jpeg_finish_compress(&cinfo); /* After finish_compress, we can close the output file. */ fclose(outfile); jpeg_destroy_compress(&cinfo); /* And we're done! */}// support RGB16 or YCbCr422static void save_picture(unsigned char *src){ int y, x; char name[64]; unsigned long rgb_data; unsigned short w = image_width; unsigned short h = image_height; unsigned char *src_y = src; unsigned char *src_cb = src_y+image_width*image_height; unsigned char *src_cr = src_cb+image_width*image_height/2; int palette = optional_image_format[image_format].palette; image_buffer = malloc(w*h*3); //RGB24 if(image_buffer==NULL) { printf("allocate memory fail in saving picture!\n"); return; } if(palette==VIDEO_PALETTE_RGB565) for(y=0; y<h; y++) for(x=0; x<w; x++) { image_buffer[(y*w+x)*3] = (((__u16 *)src)[y*w+x]&0xf800)>>8; image_buffer[(y*w+x)*3+1] = (((__u16 *)src)[y*w+x]&0x07e0)>>3; image_buffer[(y*w+x)*3+2] = (((__u16 *)src)[y*w+x]&0x001f)<<3; } else if(palette==VIDEO_PALETTE_YUV422P) for(y=0; y<h; y++) for(x=0; x<w; x++) { rgb_data = Conv_YCbCr_Rgb(src_y[y*w+x], src_y[y*w+x+1], src_cb[(y*w+x)>>1], src_cr[(y*w+x)>>1]); image_buffer[(y*w+x)*3] = (rgb_data&0xf800)>>8; image_buffer[(y*w+x)*3+1] = (rgb_data&0x07e0)>>3; image_buffer[(y*w+x)*3+2] = (rgb_data&0x001f)<<3; x++; image_buffer[(y*w+x)*3] = (rgb_data&0xf8000000)>>24; image_buffer[(y*w+x)*3+1] = (rgb_data&0x07e00000)>>19; image_buffer[(y*w+x)*3+2] = (rgb_data&0x001f0000)>>13; } else { printf("unsupported input image format for saving jpeg file\n"); free(image_buffer); return; } memset(name, 0, sizeof(name)); sprintf(name, "%s%04d.jpg", image_file, file_index++); write_JPEG_file(name, optimization); free(image_buffer);}#endif/*************************************************************/struct reg_set_s { int val1; int val2;};static int fb_xres;static int fb_yres;static int fb_bpp;#define YCbCrtoR(Y,Cb,Cr) (1000*Y + 1371*(Cr-128))/1000#define YCbCrtoG(Y,Cb,Cr) (1000*Y - 336*(Cb-128) - 698*(Cr-128))/1000#define YCbCrtoB(Y,Cb,Cr) (1000*Y + 1732*(Cb-128))/1000#define min(x1, x2) (((x1)<(x2))?(x1):(x2))__u32 Conv_YCbCr_Rgb(__u8 y0, __u8 y1, __u8 cb0, __u8 cr0){ // bit order is // YCbCr = [Cr0 Y1 Cb0 Y0], RGB=[R1,G1,B1,R0,G0,B0]. int r0, g0, b0, r1, g1, b1; __u16 rgb0, rgb1; __u32 rgb; #if 1 // 4 frames/s @192MHz, 12MHz ; 6 frames/s @450MHz, 12MHz r0 = YCbCrtoR(y0, cb0, cr0); g0 = YCbCrtoG(y0, cb0, cr0); b0 = YCbCrtoB(y0, cb0, cr0); r1 = YCbCrtoR(y1, cb0, cr0); g1 = YCbCrtoG(y1, cb0, cr0); b1 = YCbCrtoB(y1, cb0, cr0); #endif if (r0>255 ) r0 = 255; if (r0<0) r0 = 0; if (g0>255 ) g0 = 255; if (g0<0) g0 = 0; if (b0>255 ) b0 = 255; if (b0<0) b0 = 0; if (r1>255 ) r1 = 255; if (r1<0) r1 = 0; if (g1>255 ) g1 = 255; if (g1<0) g1 = 0; if (b1>255 ) b1 = 255; if (b1<0) b1 = 0; // 5:6:5 16bit format rgb0 = (((__u16)r0>>3)<<11) | (((__u16)g0>>2)<<5) | (((__u16)b0>>3)<<0); //RGB565. rgb1 = (((__u16)r1>>3)<<11) | (((__u16)g1>>2)<<5) | (((__u16)b1>>3)<<0); //RGB565. rgb = (rgb1<<16) | rgb0; return(rgb);}static char yuv_interval[] = {0, 2, 4, 8, 16};static void show_cam_img(void *scr, __u8 *y_buf, __u8 *cb_buf, __u8 *cr_buf){ __u16 x, y, w, h, i, f; __u16 *fb_buf = (__u16 *)scr; __u32 rgb_data; int palette = optional_image_format[image_format].palette; for(i=0; i<4; i++) { //0,1,2,3 if((image_width>>i)<=fb_xres) { f = 0; w = min(image_width>>i, fb_xres); h = min(image_height>>i, fb_yres); break; } if((image_height>>i)<=fb_yres) { f = 1; w = min(image_width>>i, fb_yres); h = min(image_height>>i, fb_xres); break; } } if(i>=4) return; //printf("%d,%d %d,%d\n", i, f, w, h); if(palette==VIDEO_PALETTE_RGB565) { if(!f) { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { fb_buf[x] = ((__u16 *)y_buf)[x<<i]; fb_buf[x+1] = ((__u16 *)y_buf)[(x+1)<<i]; } fb_buf += fb_xres; (__u16 *)y_buf += image_width<<i; } } else { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { fb_buf[(fb_yres-x-1)*fb_xres+y] = ((__u16 *)y_buf)[x<<i]; fb_buf[(fb_yres-x-2)*fb_xres+y] = ((__u16 *)y_buf)[(x+1)<<i]; } (__u16 *)y_buf += image_width<<i; } } } else if(palette==VIDEO_PALETTE_YUV422P) { if(!f) { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { //calculate 2 times if(i) { fb_buf[x] = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[x+1] = Conv_YCbCr_Rgb(y_buf[(x<<i)+yuv_interval[i]], y_buf[(x<<i)+1+yuv_interval[i]], cb_buf[((x<<i)+yuv_interval[i])>>1], cr_buf[((x<<i)+yuv_interval[i])>>1]); } else { rgb_data = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[x] = rgb_data; fb_buf[x+1] = rgb_data>>16; } } fb_buf += fb_xres; y_buf += image_width<<i; cb_buf += (image_width<<i)>>1; cr_buf += (image_width<<i)>>1; } } else { for(y=0; y<h; y++) { for(x=0; x<w; x+=2) { if(i) { fb_buf[(fb_yres-x-1)*fb_xres+y] = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[(fb_yres-x-2)*fb_xres+y] = Conv_YCbCr_Rgb(y_buf[(x<<i)+yuv_interval[i]], y_buf[(x<<i)+1+yuv_interval[i]], cb_buf[((x<<i)+yuv_interval[i])>>1], cr_buf[((x<<i)+yuv_interval[i])>>1]); } else { rgb_data = Conv_YCbCr_Rgb(y_buf[x<<i], y_buf[(x<<i)+1], cb_buf[(x<<i)>>1], cr_buf[(x<<i)>>1]); fb_buf[(fb_yres-x-1)*fb_xres+y] = rgb_data; fb_buf[(fb_yres-x-2)*fb_xres+y] = fb_buf[x+1] = rgb_data>>16; } } y_buf += image_width<<i; cb_buf += (image_width<<i)>>1; cr_buf += (image_width<<i)>>1; } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -