CCS+C6678LE开发记录05:编译并使用开源JPEG图像(解)压缩库libjpeg

憋了这么久终于要爆发了。

上次解决了BMP图片读取的问题,这一次想解决读取JPEG图片的问题,本来打算自己新造一个轮子的,

但是既然已经有了libjpeg为何不尝试移植呢?话说这次真的移植成功了!

废话不多说,就列出具体步骤吧。

首先是准备libjpeg的源码(删除所有不必要的文件),我这里有一份整理好的源码压缩包,下载链接

http://download.csdn.net/detail/von_ryan_hack/8317245

然后打开CCS新建项目





设置目标平台为TMS320C6678

项目名称libjpeg

点击[ Next > ] 弹出对话框,展开[ Advanced Settings ]

选择Output type为Static Library


项目模板为Empty Project



在项目上右键菜单选择添加文件



浏览libjpeg源码文件夹,[Add Files...]添加文件(全选)



提示,选择【复制文件】即可



编译类型默认为Debug,如需调整,可在项目右键设置

或者打开设置对话框,选择 [Manage Configurations...]



选择Release并【Set Active】



接下来执行[Project]-->[Build All]即可

编译完成后请将输出文件夹(Debug或Release文件夹)下的libjpeg.lib

和jconfig.h   jmorecfg.h   jpeglib.h这写文件拷贝出来以供其他项目使用。


下面给出一个应用示例

新建项目(可参考这篇文章http://blog.csdn.net/fengyhack/article/details/41945029)

设置的时候注意添加libjpeg.lib以及jconfig.h; jmorecfg.h; jpeglib.h这几个文件

添加文件后,打开项目属性设置对话框



浏览Workspace选择刚才添加的libjpeg.lib文件


然后确定



接下来贴上示例的源代码main.c

(参考http://blog.csdn.net/fengyhack/article/details/42239807)


  
  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "jpeglib.h"
  4. typedef unsigned char BYTE;
  5. int main(void)
  6. {
  7. char* szFileName = "F:\\Images\\Snapshot\\000.jpg";
  8. struct jpeg_decompress_struct cinfo;
  9. struct jpeg_error_mgr jerr;
  10. // STEP 1: StdError
  11. printf( "\n-----------------------------------\n");
  12. cinfo.err = jpeg_std_error(&jerr);
  13. // STEP 2: Create
  14. printf( "Create decompress information.\n");
  15. jpeg_create_decompress(&cinfo);
  16. FILE* pf = fopen(szFileName, "rb");
  17. if (pf != NULL)
  18. {
  19. // STEP 3: IO
  20. printf( "Attach input file.\n");
  21. jpeg_stdio_src(&cinfo, pf);
  22. // STEP 4: Header
  23. printf( "Read header information.\n");
  24. jpeg_read_header(&cinfo, TRUE);
  25. long width=cinfo.image_width;
  26. long height=cinfo.image_height;
  27. long channels=cinfo.num_components;
  28. printf( "Image size information:\n%d*%d*%d(width*height*channel)\n",width,height,channels);
  29. long bytes = width*height*channels;
  30. printf( "Allocate %d bytes memory:",bytes);
  31. BYTE* data = (BYTE*) malloc(bytes);
  32. int line= 0;
  33. if (data != NULL)
  34. {
  35. printf( "OK.\nPrepare to decompress the image...\n");
  36. // STEP 5: Start
  37. jpeg_start_decompress(&cinfo);
  38. JSAMPROW row_pointer[ 1];
  39. // STEP 6: ReadScan
  40. printf( "Scan lines...\n");
  41. while (cinfo.output_scanline < cinfo.output_height)
  42. {
  43. row_pointer[ 0] = &data[(cinfo.output_height - cinfo.output_scanline - 1)*cinfo.image_width*cinfo.num_components];
  44. jpeg_read_scanlines(&cinfo, row_pointer, 1);
  45. ++line;
  46. if(line% 100== 0)
  47. {
  48. printf( "Current line: %03d\n",line);
  49. }
  50. }
  51. // STEP 7: Finish
  52. jpeg_finish_decompress(&cinfo);
  53. printf( "Decompression finished.\n");
  54. // Do something with
  55. // BYTE data[] here
  56. // and then release it
  57. free(data);
  58. }
  59. else
  60. {
  61. printf( "FAILED.\n");
  62. }
  63. // STEP 8: Destroy
  64. jpeg_destroy_decompress(&cinfo);
  65. fclose(pf);
  66. }
  67. else
  68. {
  69. printf( "Failed to open \'%s\'\n", szFileName);
  70. }
  71. printf( "Test PASSED.\n");
  72. return 0;
  73. }

运行时输出截图如下



最后附上适用于C6678的libjpeg库及相应头文件的下载链接

http://download.csdn.net/detail/von_ryan_hack/8317241


发布了0 篇原创文章 · 获赞 123 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/kunkliu/article/details/104412760
今日推荐