天嵌 ARM开发社区

 找回密码
 注册
查看: 4487|回复: 4

LINUX 2.6.30.4 开发 图像处理

[复制链接]
福大牛二爷 发表于 2012-11-13 16:48:24 | 显示全部楼层 |阅读模式
进来在研究USB摄像头图像的采集与发送,现在要进行将图片转化为YUV格式,但是不知道为什么我的程序编译后,在开发板上运行 ./example13  (我的程序)后,出现segment default 错误,求有经验者帮忙看看啊,感激不尽啊~~
我的源代码如下,我是利用libjpeg 来解读jpg图片,然后提取其中的RGB分量,然后进行YUV存储的,应该就是存储过程出错了····不知道哪里,大家来看看吧~~~


  1. #include <stdio.h>
  2. #include <jpeglib.h>
  3. #include <setjmp.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. typedef unsigned char uint8_t;
  7. /*arm-linux-gcc -o xxx xxx.c -ljpeg*/
  8. /*to realize get R G B from JPG image,then put it to YUV`````test*/
  9. extern JSAMPLE * image_buffer; /* Points to large array of R,G,B-order data */
  10. int image_height= 480 ; /* Number of rows in image */
  11. int image_width = 640; /* Number of columns in image */

  12. typedef struct YUVlzm

  13. {

  14. uint8_t *data[4];

  15. int linesize[4];

  16. }YUVlzm;



  17. struct my_error_mgr {
  18. struct jpeg_error_mgr pub; /* "public" fields */
  19. jmp_buf setjmp_buffer; /* for return to caller */
  20. };

  21. typedef struct my_error_mgr * my_error_ptr;

  22. METHODDEF(void)
  23. my_error_exit (j_common_ptr cinfo)
  24. {
  25. my_error_ptr myerr = (my_error_ptr) cinfo->err;
  26. (*cinfo->err->output_message) (cinfo);
  27. longjmp(myerr->setjmp_buffer, 1);
  28. }

  29. GLOBAL(int)
  30. read_JPEG_file (char * filename,char * output_filename)
  31. {
  32. struct jpeg_decompress_struct cinfo;
  33. struct my_error_mgr jerr;

  34. FILE *input_file;
  35. FILE *output_file;
  36. /* More stuff */
  37. //FILE * infile; /* source file */
  38. JSAMPARRAY buffer; /* Output row buffer */
  39. // int row_stride; /* physical row width in output buffer */
  40. int row_width;

  41. unsigned char *output_buffer;
  42. unsigned char *tmp=NULL;

  43. if ((/*infile*/input_file = fopen(filename, "rb")) == NULL) {
  44. fprintf(stderr, "can't open %s\n", filename);
  45. return 0;
  46. }

  47. if ((/*infile*/output_file = fopen(output_filename, "wb")) == NULL) {
  48. fprintf(stderr, "can't open %s\n", filename);
  49. return 0;
  50. }
  51. cinfo.err = jpeg_std_error(&jerr.pub);
  52. jerr.pub.error_exit = my_error_exit;

  53. jpeg_create_decompress(&cinfo);
  54. jpeg_stdio_src(&cinfo, input_file/*infile*/);
  55. (void) jpeg_read_header(&cinfo, TRUE);
  56. (void) jpeg_start_decompress(&cinfo);
  57. /////////////////////////////////////////////////////////
  58. row_width=cinfo.output_width*cinfo.output_components;
  59. ////////////////////////////////////////////////////////////
  60. // row_stride = cinfo.output_width * cinfo.output_components;
  61. buffer = (*cinfo.mem->alloc_sarray)
  62. ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_width/*row_stride*/, 1);

  63. ////////////////////////////////////////////////////////////////
  64. // write_bmp_header();//my test define delete
  65. ///////////////////////////////////////////////////////////////////
  66. output_buffer = (unsigned char *)malloc(row_width * cinfo.output_height);
  67. memset(output_buffer, 0, row_width * cinfo.output_height);
  68. tmp = output_buffer;

  69. /*process data*/
  70. while (cinfo.output_scanline < cinfo.output_height) {
  71. (void) jpeg_read_scanlines(&cinfo, buffer, 1);

  72. memcpy(tmp,*buffer,row_width);
  73. tmp+=row_width; // above here,there is the "tmp = output_buffer;"

  74. //put_scanline_someplace(buffer[0], row_stride);
  75. }

  76. write_pixel_data(&cinfo, output_buffer, output_file);
  77. free(output_buffer);

  78. (void) jpeg_finish_decompress(&cinfo);
  79. jpeg_destroy_decompress(&cinfo);
  80. //fclose(infile);
  81. fclose(input_file);
  82. fclose(output_file);

  83. return 1;
  84. }

  85. void write_pixel_data(j_decompress_ptr cinfo, unsigned char *output_buffer, FILE *output_file)
  86. {
  87. int rows,cols;
  88. int row_width;
  89. //////////////////////////////////////////////////////////
  90. const uint8_t *p, *p1;

  91. uint8_t *lum, *cr, *cb, *lum1, *cr1, *cb1;

  92. int w;
  93. int jumper;
  94. /////////////////////////////////////////////////

  95. int step;
  96. unsigned char *tmp = NULL;
  97. unsigned char *pdata;

  98. YUVlzm dst;//////////////////////////
  99. memset(&dst,0,sizeof(YUVlzm));/////////

  100. row_width = cinfo->output_width * cinfo->output_components;
  101. step = row_width;
  102. while ((step & 3) != 0) step++;// to alignment with 4bits xx..xx00

  103. /////////////////////////////////////////////////////////
  104. dst.linesize[0] = step*2;

  105. dst.linesize[1] = step>>1;

  106. dst.linesize[2] = step>>1;
  107. dst.data[0] = (uint8_t *)malloc(step * cinfo->output_height);

  108. dst.data[1] = (uint8_t *)malloc(step * cinfo->output_height/4);

  109. dst.data[2] = (uint8_t *)malloc(step * cinfo->output_height/4);
  110. /////////////////////////////////////////////////////////

  111. pdata = (unsigned char *)malloc(step);
  112. memset(pdata, 0, step);

  113. tmp = output_buffer + row_width * (cinfo->output_height - 1);

  114. lum1 = dst.data[0];

  115. cb1 = dst.data[1];

  116. cr1 = dst.data[2];
  117. for (rows = 0; rows < cinfo->output_height; rows++) {
  118. jumper=0;
  119. lum = lum1;

  120. cb = cb1;

  121. cr = cr1;
  122. for (cols = 0; cols < row_width; cols += 3)
  123. {
  124. lum[0]= (299*tmp[cols + 0]+587*tmp[cols + 1]+114*tmp[cols + 2])/1000 ;//Red Y
  125. if(jumper%4==0){
  126. cb[0]=(492*(tmp[cols + 2]-pdata[cols + 2]))/1000;//Green ;0 U
  127. cr[0] = (877*(tmp[cols + 0]-pdata[cols + 2]))/1000;//Blue ;0 V
  128. cb++;

  129. cr++;}

  130. lum++;
  131. jumper++;
  132. }
  133. tmp -= row_width;
  134. lum1 += dst.linesize[0];

  135. cb1 += dst.linesize[1];

  136. cr1 += dst.linesize[2];
  137. }
  138. fwrite(dst.data[0], step * cinfo->output_height,1,output_file);
  139. fwrite(dst.data[1], step * cinfo->output_height/4,1,output_file);
  140. fwrite(dst.data[2], step * cinfo->output_height/4,1,output_file);

  141. free(pdata);
  142. }


  143. int main()
  144. {
  145. read_JPEG_file ("jpgimage.jpg","jpgimage1.yuv");
  146. return 0;
  147. }
复制代码


 楼主| 福大牛二爷 发表于 2012-11-14 08:58:18 | 显示全部楼层
自己顶一下~~:L
TQ-ZQL 发表于 2012-11-14 11:52:59 | 显示全部楼层
你看下有没有有越界访问。(可以在pc上跑一下)
 楼主| 福大牛二爷 发表于 2012-11-15 08:35:13 | 显示全部楼层
小弟不才,还未解决·····
aa5956 发表于 2013-3-29 12:38:06 | 显示全部楼层
同问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2024-6-17 07:06 , Processed in 1.031250 second(s), 20 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表