经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
JPG学习笔记2(附完整代码)
来源:cnblogs  作者:哇哩顾得  时间:2021/2/18 15:44:00  对本文有异议

  我们已经从BMP图中拿到了需要压缩RGB的数据,我们需要对原数据从RGB域转变YCbCr域,之后对YCbCr数据进行下采样(down sampling)。对于不需要看文章的同学,这边直接给出源代码。https://github.com/Cheemion/JPEG_COMPRESS

                图片引用"Compressed Image File Formats JPEG, PNG, GIF, XBM, BMP - John Miano"[1]

1.RGB域和YCbCr域

RGB代表红绿蓝,通过3种颜色的叠加来得到我们看到的颜色。0-到255分别代表颜色从浅到深。

Y   =  0.299   * red + 0.587  *  green + 0.114  *  blue;
Cb = -0.1687 * red - 0.3313 * green + 0.5    *    blue + 128;
Cr  =  0.5       * red - 0.4187 * green -  0.0813 * blue + 128;

Y是RGB的加权平均值,称之为亮度(luminance)

Cb是B分量和亮度的差值, 称为Chrominance(Cb)

Cr是R分量和亮度的差值,称为Chrominance(Cr)

以下代码将RGB转为YCbCr。为什么将RGB转为YCbCr? 因为人眼对亮度(Y)的变化更敏感,所以我可以对Cr和Cb进行下采样(压缩,比如本来1个字节代表一个pixel的数据,压缩后用1个字节代表4个pixels的数据),尽可能保留完整的Y分量。通过这样子我们可以进一步的压缩数据。

  1. void JPG::convertToYCbCr() {
  2. for(uint i = 0; i < height; i++) {
  3. for(uint j = 0; j < width; j++) {
  4. YCbCr temp = BMPData[i * width + j];
  5. BMPData[i * width + j].Y = 0.299 * temp.red + 0.587 * temp.green + 0.114 * temp.blue;
  6. BMPData[i * width + j].Cb = -0.1687 * temp.red - 0.3313 * temp.green + 0.5 * temp.blue + 128;
  7. BMPData[i * width + j].Cr = 0.5 * temp.red - 0.4187 * temp.green - 0.0813 * temp.blue + 128;
  8. }
  9. }
  10. }

2.sampling(采样)

 采样通常是对连续信号进行采样,比如下图蓝色是连续信号x(t),红色是对信号进行采样后得到的信号x[n]=x(T*n), T是采样间隔,1/T是采样频率。

 而在JPEG中,我们是对已经离散的数据进行采样,并且JPEG中的采样数值是相对采样数值。相对于最高采样频率的采样数值。

如下左图

Y(luminance)分量的水平采样频率(H, Horizantal sampling frequency)和垂直采样频率(V, vertical sampling frequency)都是4,是最高的采样频率。最高的采样频率就相当于保留原图的Y分量,不进行下采样。

Cb分量的水平和垂直的采样频率都是2,等于最高采样频率的一半。所以水平每2个点采样一次,垂直每2个点采样一次。

Cr分量的水平和垂直采样频率都是1,等于最高采样频率的1/4。所以水平和垂直每4个点采样一个点。

3个分量的量叠加就得到了我们的像素的值。

  图片引用"Compressed Image File Formats JPEG, PNG, GIF, XBM, BMP - John Miano"[1]

2.YCbCr数据在JPEG中的存储

JPEG规定所有的数据都是以8*8的一个block(data unit)的形式进行离散余弦变化和存储的.可以把这8*8的block看成是最小存储单元。

MCU是Y,Cb,Cr的完整的block组成的能够完整还原一个范围的色彩的最小单元。啥意思?

假设我们的图片是10*10的大小.

若Y,Cb,Cr的水平和垂直的采样频率都为1,则原图由4个mcu(4种颜色分别代表一个MCU)组成(每个mcu包含1个y的block,一个cb的block,一个cr的block, 每个mcu的大小为8*8),边缘空白的地方可用0替代,也可以重复边缘的值。

左上角那块4*4的小block的值分别

pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]

pixel[0,1] = y[0,1] + cb[0,1] + cr[0,1]

pixel[1,0] = y[1,0] + cb[1,0] + cr[1,0]

pixel[1,1] = y[1,1] + cb[1,1] + cr[1,1]

若Y的水平和垂直采样频率为2, cb和cr的采样频率为1, 则原图由1个mcu组成(大小为16*16)。mcu中包含4个y的block(2*2),一个cb,一个cr。总共6个block,大小只占原来block的一半。

左上角那块4*4的小block的值分别

pixel[0,0] = y[0,0] + cb[0,0] + cr[0,0]

pixel[0,1] = y[0,1] + cb[0,0] + cr[0,0]

pixel[1,0] = y[1,0] + cb[0,0] + cr[0,0]

pixel[1,1] = y[1,1] + cb[0,0] + cr[0,0]

 

总结:mcu大小= 垂直最大采样值 * 水平最大采样值, 一个mcu包含y的水平采样值*y的垂直采样值个的y个block(y的水平采样为2,垂直为2,则一个muc有4个yblock)。其他分量同理

1.3定义JPG class代码

  1. //定义Block
    using
    Block = int[64];
    //定义YCbCr,同时这个结构用来展示存放rgb数据
  2. struct YCbCr {
  3. union
  4. {
  5. double Y;
  6. double red;
  7. };
  8. union
  9. {
  10. double Cb;
  11. double green;
  12. };
  13. union {
  14. double Cr;
  15. double blue;
  16. };
  17. };

 



  1. struct
    MCU {
  2. Block* y;
  3. Block* cb;
  4. Block* cr;
  5. };


  6. //定义JPG类,用于压缩图片
  7. class JPG
  8. {
  9. public:
    //rgb转到YCbCr
  10. void convertToYCbCr();
       //下采样
  11. void subsampling();
    //变化
  12. void discreteCosineTransform();
    //量化
  13. void quantization();
    //哈夫曼
  14. void huffmanCoding();
    //输出
  15. void output(std::string path);
  16. public:
    MCU
    * data;
    Block
    * blocks;
    //BMPData存放的是bmp图片的RGB数据
  17. YCbCr* BMPData;
  18. uint blockNum;
  19. //原图的像素
  20. uint width;
  21. uint height;
  22. //mcu 有多少个 长度是多少
  23. uint mcuWidth;
  24. uint mcuHeight;
  25. //一个完整的muc的水平和垂直像素个数
  26. uint mcuVerticalPixelNum;
  27. uint mcuHorizontalPixelNum;
  28. //用于subsampling
  29. // only support 1 or 2
  30. byte YVerticalSamplingFrequency;
  31. byte YHorizontalSamplingFrequency;
  32. byte CbVerticalSamplingFrequency;
  33. byte CbHorizontalSamplingFrequency;
  34. byte CrVerticalSamplingFrequency;
  35. byte CrHorizontalSamplingFrequency;
  36. byte maxVerticalSamplingFrequency;
  37. byte maxHorizontalSamplingFrequency;
  38. public:
  39. JPG(uint width, uint height,const RGB* const rgbs,
  40. byte YVerticalSamplingFrequency, byte YHorizontalSamplingFrequency,
  41. byte CbVerticalSamplingFrequency, byte CbHorizontalSamplingFrequency,
  42. byte CrVerticalSamplingFrequency, byte CrHorizontalSamplingFrequency
  43. )
  44. :width(width), height(height),
  45. YVerticalSamplingFrequency(YVerticalSamplingFrequency), YHorizontalSamplingFrequency(YHorizontalSamplingFrequency),
  46. CbVerticalSamplingFrequency(CbVerticalSamplingFrequency), CbHorizontalSamplingFrequency(CbHorizontalSamplingFrequency),
  47. CrVerticalSamplingFrequency(CrVerticalSamplingFrequency), CrHorizontalSamplingFrequency(CrHorizontalSamplingFrequency)
  48. {
  49. maxHorizontalSamplingFrequency = std::max({YHorizontalSamplingFrequency, CbHorizontalSamplingFrequency, CrHorizontalSamplingFrequency});
  50. maxVerticalSamplingFrequency = std::max({YVerticalSamplingFrequency, CbVerticalSamplingFrequency, CrVerticalSamplingFrequency});
  51. //mcu的个数
  52. mcuWidth = (width + (maxHorizontalSamplingFrequency * 8 - 1)) / (maxHorizontalSamplingFrequency * 8);
  53. mcuHeight = (height + (maxVerticalSamplingFrequency * 8 - 1)) / (maxVerticalSamplingFrequency * 8);
  54. mcuVerticalPixelNum = maxVerticalSamplingFrequency * 8;
  55. mcuHorizontalPixelNum = maxHorizontalSamplingFrequency * 8;
  56. //总共多少个MCU
  57. data = new MCU[mcuWidth * mcuHeight];
  58. //一个MCU有多少个Block
  59. blockNum = (YVerticalSamplingFrequency * YHorizontalSamplingFrequency + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency + CrHorizontalSamplingFrequency * CrVerticalSamplingFrequency);
  60. //分配block内存空间
  61. blocks = new Block[mcuHeight * mcuHeight * blockNum];
  62. //把内存映射到对于的结构中
  63. for (uint i = 0; i < mcuHeight; i++) {
  64. for (uint j = 0; j < mcuWidth; j++) {
  65. data[i * mcuWidth + j].y = &blocks[(i * mcuWidth + j) * blockNum];
  66. data[i * mcuWidth + j].cb = data[i * mcuWidth + j].y + YVerticalSamplingFrequency * YHorizontalSamplingFrequency;
  67. data[i * mcuWidth + j].cr = data[i * mcuWidth + j].cb + CbVerticalSamplingFrequency * CbHorizontalSamplingFrequency;
  68. }
  69. }
  70. //BMP数据用于存放,bmp的原图的数据
  71. BMPData = new YCbCr[width * height];
    //把bmp数据暂时存放在BMPdata中
  72. for(uint i = 0; i < height; i++) {
  73. for(uint j = 0; j < width; j++) {
  74. BMPData[i * width + j].red = static_cast<double>(rgbs[i * width + j].red);
  75. BMPData[i * width + j].blue = static_cast<double>(rgbs[i * width + j].blue);
  76. BMPData[i * width + j].green = static_cast<double>(rgbs[i * width + j].green);
  77. }
  78. }
  79. }
  80. ~JPG() {
  81. delete[] data;
  82. delete[] blocks;
  83. delete[] BMPData;
  84. }
  85. };

 

 

1.6下采样代码

  1. //这里直接把左上的点 当作subsampling的点了
  2. //也可以取平均值
  3. void JPG::subsampling() {
  4. //遍历mcu
  5. for (uint i = 0; i < mcuHeight; i++) {
  6. for (uint j = 0; j < mcuWidth; j++) {
    //拿到mcu
  7. MCU& currentMCU = data[i * mcuWidth + j];
    //每个mcu起始的坐标点
  8. uint heightOffset = i * maxVerticalSamplingFrequency * 8;
  9. uint widthOffset = j * maxHorizontalSamplingFrequency * 8;
  10. //iterate over 每一个component Y, cb cr
  11. for (uint componentID = 1; componentID <= 3; componentID++) {
  12. //遍历block, 从muc中拿block
  13. for(uint ii = 0, yOffSet = heightOffset; ii < getVerticalSamplingFrequency(componentID); ii++, yOffSet = yOffSet + 8) {
  14. for(uint jj = 0, xOffset = widthOffset; jj < getHorizontalSamplingFrequency(componentID); jj++, xOffset = xOffset + 8) {
    //拿到具体的block对象
  15. Block& currentBlock = currentMCU[componentID][ii * getHorizontalSamplingFrequency(componentID) + jj];
  16. //遍历Block every pixels 像素, 并且采样赋值
  17. for(uint y = 0; y < 8; y++) {
  18. for(uint x = 0; x < 8; x++) {
    //得到被采样的那个点的坐标
  19. uint sampledY = yOffSet + y * maxVerticalSamplingFrequency / getVerticalSamplingFrequency(componentID);
  20. uint sampledX = xOffset + x * maxHorizontalSamplingFrequency / getHorizontalSamplingFrequency(componentID);
  21. //cannot find in original pictures;
  22. if(sampledX >= width || sampledY >= height) {
  23. currentBlock[y * 8 + x] = 0;
  24. } else {
  25. currentBlock[y * 8 + x] = BMPData[sampledY * width + sampledX][componentID];
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }
  34. }

完整代码  https://github.com/Cheemion/JPEG_COMPRESS/tree/main/Day2

完结

  Thanks for reading.

  wish you have a good day.

  >>>> JPG学习笔记3(附完整代码)

参考资料

[1]https://github.com/Cheemion/JPEG_COMPRESS/blob/main/resource/Compressed%20Image%20File%20Formats%20JPEG%2C%20PNG%2C%20GIF%2C%20XBM%2C%20BMP%20-%20John%20Miano.pdf

原文链接:http://www.cnblogs.com/robsann/p/14399323.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号