进来在研究USB摄像头图像的采集与发送,现在要进行将图片转化为YUV格式,但是不知道为什么我的程序编译后,在 开发板上运行 ./example13 (我的程序)后,出现segment default 错误,求有经验者帮忙看看啊,感激不尽啊~~
我的源代码如下,我是利用libjpeg 来解读jpg图片,然后提取其中的RGB分量,然后进行YUV存储的,应该就是存储过程出错了····不知道哪里,大家来看看吧~~~
- #include
- #include
- #include
- #include
- #include
- typedef unsigned char uint8_t;
- /*ARM-linux-gcc -o xxx xxx.c -ljpeg*/
- /*to realize get R G B from JPG image,then put it to YUV`````test*/
- extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
- int image_height= 480 ; /* Number of rows in image */
- int image_width = 640; /* Number of columns in image */
- typedef struct YUVlzm
- {
- uint8_t *data[4];
- int linesize[4];
- }YUVlzm;
- struct my_error_mgr {
- struct jpeg_error_mgr pub; /* "public" fields */
- jmp_buf setjmp_buffer; /* for return to caller */
- };
- typedef struct my_error_mgr * my_error_ptr;
- METHODDEF(void)
- my_error_exit (j_common_ptr cinfo)
- {
- my_error_ptr myerr = (my_error_ptr) cinfo->err;
- (*cinfo->err->output_message) (cinfo);
- longjmp(myerr->setjmp_buffer, 1);
- }
- GLOBAL(int)
- read_JPEG_file (char * filename,char * output_filename)
- {
- struct jpeg_decompress_struct cinfo;
- struct my_error_mgr jerr;
- FILE *input_file;
- FILE *output_file;
- /* More stuff */
- //FILE * infile; /* source file */
- JSAMPARRAY buffer; /* Output row buffer */
- // int row_stride; /* physical row width in output buffer */
- int row_width;
- unsigned char *output_buffer;
- unsigned char *tmp=NULL;
- if ((/*infile*/input_file = fopen(filename, "rb")) == NULL) {
- fprintf(stderr, "can't open %sn", filename);
- return 0;
- }
- if ((/*infile*/output_file = fopen(output_filename, "wb")) == NULL) {
- fprintf(stderr, "can't open %sn", filename);
- return 0;
- }
- cinfo.err = jpeg_std_error(&jerr.pub);
- jerr.pub.error_exit = my_error_exit;
- jpeg_create_decompress(&cinfo);
- jpeg_stdio_src(&cinfo, input_file/*infile*/);
- (void) jpeg_read_header(&cinfo, TRUE);
- (void) jpeg_start_decompress(&cinfo);
- /////////////////////////////////////////////////////////
- row_width=cinfo.output_width*cinfo.output_components;
- ////////////////////////////////////////////////////////////
- // row_stride = cinfo.output_width * cinfo.output_components;
- buffer = (*cinfo.mem->alloc_sarray)
- ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width/*row_stride*/, 1);
- ////////////////////////////////////////////////////////////////
- // write_bmp_header();//my test define delete
- ///////////////////////////////////////////////////////////////////
- output_buffer = (unsigned char *)malloc(row_width * cinfo.output_height);
- memset(output_buffer, 0, row_width * cinfo.output_height);
- tmp = output_buffer;
- /*process data*/
- while (cinfo.output_scanline < cinfo.output_height) {
- (void) jpeg_read_scanlines(&cinfo, buffer, 1);
- memcpy(tmp,*buffer,row_width);
- tmp+=row_width; // above here,there is the "tmp = output_buffer;"
- //put_scanline_someplace(buffer[0], row_stride);
- }
- write_pixel_data(&cinfo, output_buffer, output_file);
- free(output_buffer);
- (void) jpeg_finish_decompress(&cinfo);
- jpeg_destroy_decompress(&cinfo);
- //fclose(infile);
- fclose(input_file);
- fclose(output_file);
- return 1;
- }
- void write_pixel_data(j_decompress_ptr cinfo, unsigned char *output_buffer, FILE *output_file)
- {
- int rows,cols;
- int row_width;
- //////////////////////////////////////////////////////////
- const uint8_t *p, *p1;
- uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;
- int w;
- int jumper;
- /////////////////////////////////////////////////
- int step;
- unsigned char *tmp = NULL;
- unsigned char *pdata;
- YUVlzm dst;//////////////////////////
- memset(&dst,0,sizeof(YUVlzm));/////////
- row_width = cinfo->output_width * cinfo->output_components;
- step = row_width;
- while ((step & 3) != 0) step++;// to alignment with 4bits xx..xx00
- /////////////////////////////////////////////////////////
- dst.linesize[0] = step*2;
- dst.linesize[1] = step>>1;
- dst.linesize[2] = step>>1;
- dst.data[0] = (uint8_t *)malloc(step * cinfo->output_height);
- dst.data[1] = (uint8_t *)malloc(step * cinfo->output_height/4);
- dst.data[2] = (uint8_t *)malloc(step * cinfo->output_height/4);
- /////////////////////////////////////////////////////////
- pdata = (unsigned char *)malloc(step);
- memset(pdata, 0, step);
- tmp = output_buffer + row_width * (cinfo->output_height - 1);
- lum1 = dst.data[0];
- cb1 = dst.data[1];
- cr1 = dst.data[2];
- for (rows = 0; rows < cinfo->output_height; rows++) {
- jumper=0;
- lum = lum1;
- cb = cb1;
- cr = cr1;
- for (cols = 0; cols < row_width; cols += 3)
- {
- lum[0]= (299*tmp[cols + 0]+587*tmp[cols + 1]+114*tmp[cols + 2])/1000 ;//Red Y
- if(jumper%4==0){
- cb[0]=(492*(tmp[cols + 2]-pdata[cols + 2]))/1000;//Green ;0 U
- cr[0] = (877*(tmp[cols + 0]-pdata[cols + 2]))/1000;//Blue ;0 V
- cb++;
- cr++;}
- lum++;
- jumper++;
- }
- tmp -= row_width;
- lum1 += dst.linesize[0];
- cb1 += dst.linesize[1];
- cr1 += dst.linesize[2];
- }
- fwrite(dst.data[0], step * cinfo->output_height,1,output_file);
- fwrite(dst.data[1], step * cinfo->output_height/4,1,output_file);
- fwrite(dst.data[2], step * cinfo->output_height/4,1,output_file);
- free(pdata);
- }
- int main()
- {
- read_JPEG_file ("jpgimage.jpg","jpgimage1.yuv");
- return 0;
- }
1
|
6个回答
|
|
|