libjpeg-turbo的简单使用之一

libjpeg-turbo支持直接从JPEG解压成YUV格式,或者反之。这也是我当初想研究它的一个动力。

看了头文件注释,它是支持YUV444(即宏TJSAMP_444),YUV422(即宏TJSAMP_422),YUV420(即宏TJSAMP_420),YUV400(即宏TJSAMP_440),YUV411(即宏TJSAMP_411)。可惜的是,只支持平面格式(plane),对于交织的如UYVY或特别的如NV12(即YUV420SP)或NV16(即YUV422SP),都没看到有支持。在sourceforge上看邮件列表,发现有些描述,但还没研究过,看发布的源码,也未见有说明,估计是不支持的。

本文简单介绍如何从JPEG解压成YUV格式,以及如何将YUV压缩成JPEG。

libjpeg-turbo使用tjBufSizeYUV2函数计算YUV大小,开始时没注意第二个参数pad,默认传递0,发现没效果,而传1或4,却是可以的。解压后的YUV的格式,是由JPEG图片的采样格式决定的,如果JPEG本身是YUV420,则解压得到的YUV,就是YUV420格式。

示例代码如下:


  
  
  1. int tjpeg2yuv(unsigned char* jpeg_buffer, int jpeg_size, unsigned char** yuv_buffer, int* yuv_size, int* yuv_type)
  2. {
  3. tjhandle handle = NULL;
  4. int width, height, subsample, colorspace;
  5. int flags = 0;
  6. int padding = 1; // 1或4均可,但不能是0
  7. int ret = 0;
  8. handle = tjInitDecompress();
  9. tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, &width, &height, &subsample, &colorspace);
  10. printf( "w: %d h: %d subsample: %d color: %d\n", width, height, subsample, colorspace);
  11. flags |= 0;
  12. *yuv_type = subsample;
  13. // 注:经测试,指定的yuv采样格式只对YUV缓冲区大小有影响,实际上还是按JPEG本身的YUV格式来转换的
  14. *yuv_size = tjBufSizeYUV2(width, padding, height, subsample);
  15. *yuv_buffer =( unsigned char *) malloc(*yuv_size);
  16. if (*yuv_buffer == NULL)
  17. {
  18. printf( "malloc buffer for rgb failed.\n");
  19. return -1;
  20. }
  21. ret = tjDecompressToYUV2(handle, jpeg_buffer, jpeg_size, *yuv_buffer, width,
  22. padding, height, flags);
  23. if (ret < 0)
  24. {
  25. printf( "compress to jpeg failed: %s\n", tjGetErrorStr());
  26. }
  27. tjDestroy(handle);
  28. return ret;
  29. }
  30. int tyuv2jpeg(unsigned char* yuv_buffer, int yuv_size, int width, int height, int subsample, unsigned char** jpeg_buffer, unsigned long* jpeg_size, int quality)
  31. {
  32. tjhandle handle = NULL;
  33. int flags = 0;
  34. int padding = 1; // 1或4均可,但不能是0
  35. int need_size = 0;
  36. int ret = 0;
  37. handle = tjInitCompress();
  38. flags |= 0;
  39. need_size = tjBufSizeYUV2(width, padding, height, subsample);
  40. if (need_size != yuv_size)
  41. {
  42. printf( "we detect yuv size: %d, but you give: %d, check again.\n", need_size, yuv_size);
  43. return 0;
  44. }
  45. ret = tjCompressFromYUV(handle, yuv_buffer, width, padding, height, subsample, jpeg_buffer, jpeg_size, quality, flags);
  46. if (ret < 0)
  47. {
  48. printf( "compress to jpeg failed: %s\n", tjGetErrorStr());
  49. }
  50. tjDestroy(handle);
  51. return ret;
  52. }


另外,该库也支持从RGB转换成YUV,或反之。调用相应的函数即可,不再详述。示例代码如下:


  
  
  1. int trgb2yuv(unsigned char* rgb_buffer, int width, int height, unsigned char** yuv_buffer, int* yuv_size, int subsample)
  2. {
  3. tjhandle handle = NULL;
  4. int flags = 0;
  5. int padding = 1; // 1或4均可,但不能是0
  6. int pixelfmt = TJPF_RGB;
  7. int ret = 0;
  8. handle = tjInitCompress();
  9. flags |= 0;
  10. *yuv_size = tjBufSizeYUV2(width, padding, height, subsample);
  11. *yuv_buffer =( unsigned char *) malloc(*yuv_size);
  12. if (*yuv_buffer == NULL)
  13. {
  14. printf( "malloc buffer for rgb failed.\n");
  15. return -1;
  16. }
  17. ret = tjEncodeYUV3(handle, rgb_buffer, width, 0, height, pixelfmt, *yuv_buffer, padding, subsample, flags);
  18. if (ret < 0)
  19. {
  20. printf( "encode to yuv failed: %s\n", tjGetErrorStr());
  21. }
  22. tjDestroy(handle);
  23. return ret;
  24. }
  25. int tyuv2rgb(unsigned char* yuv_buffer, int yuv_size, int width, int height, int subsample, unsigned char** rgb_buffer, int* rgb_size)
  26. {
  27. tjhandle handle = NULL;
  28. int flags = 0;
  29. int padding = 1; // 1或4均可,但不能是0
  30. int pixelfmt = TJPF_RGB;
  31. int need_size = 0;
  32. int ret = 0;
  33. handle = tjInitDecompress();
  34. flags |= 0;
  35. need_size = tjBufSizeYUV2(width, padding, height, subsample);
  36. if (need_size != yuv_size)
  37. {
  38. printf( "we detect yuv size: %d, but you give: %d, check again.\n", need_size, yuv_size);
  39. return -1;
  40. }
  41. *rgb_size = width*height*tjPixelSize[pixelfmt];
  42. *rgb_buffer =( unsigned char *) malloc(*rgb_size);
  43. if (*rgb_buffer == NULL)
  44. {
  45. printf( "malloc buffer for rgb failed.\n");
  46. return -1;
  47. }
  48. ret = tjDecodeYUV(handle, yuv_buffer, padding, subsample, *rgb_buffer, width, 0, height, pixelfmt, flags);
  49. if (ret < 0)
  50. {
  51. printf( "decode to rgb failed: %s\n", tjGetErrorStr());
  52. }
  53. tjDestroy(handle);
  54. return ret;
  55. }


以上代码示例,二级指针均在函数内分配内存,需要调用者自行释放,否则会有内存泄漏。



扫描二维码关注公众号,回复: 8632986 查看本文章


发布了13 篇原创文章 · 获赞 6 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sdsszk/article/details/82348278