小张学算法之音视频算法:2.jpeg图像压缩算法

接着上节视频压缩算法,今天来看看静态图像的压缩,相比视频压缩,图片压缩就太简单啦,let‘s go!!

jepg文件二进制格式

jpeg二进制文件分块表

分块标识涵义
SOIff d8文件开始
APP0ff e0定义交换格式和图像识别信息
DQTff db定义量化表
SOF0ff c0帧开始
DHTff c4霍夫曼(Huffman)表
SOSff da扫描行开始
EOIff d9文件结束

jpeg文件是由0xffd8开头的,代表着文件的开头。
在这里插入图片描述
可以看到文件头信息,其实最重要的就是两张表了,量化表和huffman表,存储图像的压缩信息,至于文件的交换格式和图像识别信息就没什么特别的。

详细头部快信息见https://blog.csdn.net/sinat_26472165/article/details/85342766

jpeg的压缩过程,
1.先分块88或1616的块,进行DCT变换到频域;
2. 进行量化操作,去掉高频分量;
3. Z型扫描,
4. 进行熵编码:huffman编码。
量化表有两张:亮度量化表,色度量化表。
huffman表有亮度AC(交流)、亮度DC(直流)、色度AC、色度DC,四张huffman表;
jpeg中存储的是yCrCb色彩空间的数据,也就是yuv数据。使用jpeg_snoop工具查看下jpeg头信息;

JPEGsnoop 1.8.0 by Calvin Hass
  http://www.impulseadventure.com/photo/
  -------------------------------------

  Filename: [C:\Users\IBM\Pictures\Camera Roll\0.jpg]
  Filesize: [23643] Bytes

Start Offset: 0x00000000
*** Marker: SOI (xFFD8) ***
  OFFSET: 0x00000000   
 
*** Marker: APP0 (xFFE0) ***     /*数据交换信息*/
  OFFSET: 0x00000002
  Length     = 16           /*交换数据长度*/
  Identifier = [JFIF]        /*交换格式*/
  version    = [1.1]           /*版本*/
  density    = 1 x 1 (aspect ratio)
  thumbnail  = 0 x 0
 
*** Marker: DQT (xFFDB) ***      /*量化表*/
  Define a Quantization Table.
  OFFSET: 0x00000014
  Table length = 67
  ----
  Precision=8 bits
  Destination ID=0 (Luminance)    /*灰度量化表*/
    DQT, Row #0:   2   1   1   2   2   4   5   6 
    DQT, Row #1:   1   1   1   2   3   6   6   6 
    DQT, Row #2:   1   1   2   2   4   6   7   6 
    DQT, Row #3:   1   2   2   3   5   9   8   6 
    DQT, Row #4:   2   2   4   6   7  11  10   8 
    DQT, Row #5:   2   4   6   6   8  10  11   9 
    DQT, Row #6:   5   6   8   9  10  12  12  10 
    DQT, Row #7:   7   9  10  10  11  10  10  10 
    Approx quality factor = 95.04 (scaling=9.93 variance=1.25)
 
*** Marker: DQT (xFFDB) ***
  Define a Quantization Table.
  OFFSET: 0x00000059
  Table length = 67
  ----
  Precision=8 bits
  Destination ID=1 (Chrominance)              /*色度量化表*/
    DQT, Row #0:   2   2   2   5  10  10  10  10 
    DQT, Row #1:   2   2   3   7  10  10  10  10 
    DQT, Row #2:   2   3   6  10  10  10  10  10 
    DQT, Row #3:   5   7  10  10  10  10  10  10 
    DQT, Row #4:  10  10  10  10  10  10  10  10 
    DQT, Row #5:  10  10  10  10  10  10  10  10 
    DQT, Row #6:  10  10  10  10  10  10  10  10 
    DQT, Row #7:  10  10  10  10  10  10  10  10 
    Approx quality factor = 94.91 (scaling=10.18 variance=0.26)
 
*** Marker: SOF0 (Baseline DCT) (xFFC0) ***
  OFFSET: 0x0000009E
  Frame header length = 17
  Precision = 8
  Number of Lines = 180
  Samples per Line = 320
  Image Size = 320 x 180
  Raw Image Orientation = Landscape
  Number of Img components = 3      /*组件数yCrCb,所以是3*/
    Component[1]: ID=0x01, Samp Fac=0x22 (Subsamp 1 x 1), Quant Tbl Sel=0x00 (Lum: Y)     /*使用灰度量化表进行量化*/
    Component[2]: ID=0x02, Samp Fac=0x11 (Subsamp 2 x 2), Quant Tbl Sel=0x01 (Chrom: Cb)  /*使用色度量化表进行量化*/
    Component[3]: ID=0x03, Samp Fac=0x11 (Subsamp 2 x 2), Quant Tbl Sel=0x01 (Chrom: Cr) /*使用色度量化表进行量化*/
 
*** Marker: DHT (Define Huffman Table) (xFFC4) ***
  OFFSET: 0x000000B1
  Huffman table length = 31
  ----
  Destination ID = 0
  Class = 0 (DC / Lossless Table)       /*霍夫曼表,灰度的直流分量*/
    Codes of length 01 bits (000 total): 
    Codes of length 02 bits (001 total): 00 
    Codes of length 03 bits (005 total): 01 02 03 04 05 
    Codes of length 04 bits (001 total): 06 
    Codes of length 05 bits (001 total): 07 
    Codes of length 06 bits (001 total): 08 
    Codes of length 07 bits (001 total): 09 
    Codes of length 08 bits (001 total): 0A 
    Codes of length 09 bits (001 total): 0B 
    Codes of length 10 bits (000 total): 
    Codes of length 11 bits (000 total): 
    Codes of length 12 bits (000 total): 
    Codes of length 13 bits (000 total): 
    Codes of length 14 bits (000 total): 
    Codes of length 15 bits (000 total): 
    Codes of length 16 bits (000 total): 
    Total number of codes: 012

 
*** Marker: DHT (Define Huffman Table) (xFFC4) ***
  OFFSET: 0x000000D2
  Huffman table length = 181
  ----
  Destination ID = 0
  Class = 1 (AC Table)    /*霍夫曼表,灰度的交流分量*/
    Codes of length 01 bits (000 total): 
    Codes of length 02 bits (002 total): 01 02 
    Codes of length 03 bits (001 total): 03 
    Codes of length 04 bits (003 total): 00 04 11 
    Codes of length 05 bits (003 total): 05 12 21 
    Codes of length 06 bits (002 total): 31 41 
    Codes of length 07 bits (004 total): 06 13 51 61 
    Codes of length 08 bits (003 total): 07 22 71 
    Codes of length 09 bits (005 total): 14 32 81 91 A1 
    Codes of length 10 bits (005 total): 08 23 42 B1 C1 
    Codes of length 11 bits (004 total): 15 52 D1 F0 
    Codes of length 12 bits (004 total): 24 33 62 72 
    Codes of length 13 bits (000 total): 
    Codes of length 14 bits (000 total): 
    Codes of length 15 bits (001 total): 82 
    Codes of length 16 bits (125 total): 09 0A 16 17 18 19 1A 25 26 27 28 29 2A 34 35 36 
                                         37 38 39 3A 43 44 45 46 47 48 49 4A 53 54 55 56 
                                         57 58 59 5A 63 64 65 66 67 68 69 6A 73 74 75 76 
                                         77 78 79 7A 83 84 85 86 87 88 89 8A 92 93 94 95 
                                         96 97 98 99 9A A2 A3 A4 A5 A6 A7 A8 A9 AA B2 B3 
                                         B4 B5 B6 B7 B8 B9 BA C2 C3 C4 C5 C6 C7 C8 C9 CA 
                                         D2 D3 D4 D5 D6 D7 D8 D9 DA E1 E2 E3 E4 E5 E6 E7 
                                         E8 E9 EA F1 F2 F3 F4 F5 F6 F7 F8 F9 FA 
    Total number of codes: 162

 
*** Marker: DHT (Define Huffman Table) (xFFC4) ***
  OFFSET: 0x00000189
  Huffman table length = 31
  ----
  Destination ID = 1
  Class = 0 (DC / Lossless Table)  /*霍夫曼表,色度的直流分量*/
    Codes of length 01 bits (000 total): 
    Codes of length 02 bits (003 total): 00 01 02 
    Codes of length 03 bits (001 total): 03 
    Codes of length 04 bits (001 total): 04 
    Codes of length 05 bits (001 total): 05 
    Codes of length 06 bits (001 total): 06 
    Codes of length 07 bits (001 total): 07 
    Codes of length 08 bits (001 total): 08 
    Codes of length 09 bits (001 total): 09 
    Codes of length 10 bits (001 total): 0A 
    Codes of length 11 bits (001 total): 0B 
    Codes of length 12 bits (000 total): 
    Codes of length 13 bits (000 total): 
    Codes of length 14 bits (000 total): 
    Codes of length 15 bits (000 total): 
    Codes of length 16 bits (000 total): 
    Total number of codes: 012

 
*** Marker: DHT (Define Huffman Table) (xFFC4) ***
  OFFSET: 0x000001AA
  Huffman table length = 181
  ----
  Destination ID = 1
  Class = 1 (AC Table)    /*霍夫曼表,色度的交流分量*/
    Codes of length 01 bits (000 total): 
    Codes of length 02 bits (002 total): 00 01 
    Codes of length 03 bits (001 total): 02 
    Codes of length 04 bits (002 total): 03 11 
    Codes of length 05 bits (004 total): 04 05 21 31 
    Codes of length 06 bits (004 total): 06 12 41 51 
    Codes of length 07 bits (003 total): 07 61 71 
    Codes of length 08 bits (004 total): 13 22 32 81 
    Codes of length 09 bits (007 total): 08 14 42 91 A1 B1 C1 
    Codes of length 10 bits (005 total): 09 23 33 52 F0 
    Codes of length 11 bits (004 total): 15 62 72 D1 
    Codes of length 12 bits (004 total): 0A 16 24 34 
    Codes of length 13 bits (000 total): 
    Codes of length 14 bits (001 total): E1 
    Codes of length 15 bits (002 total): 25 F1 
    Codes of length 16 bits (119 total): 17 18 19 1A 26 27 28 29 2A 35 36 37 38 39 3A 43 
                                         44 45 46 47 48 49 4A 53 54 55 56 57 58 59 5A 63 
                                         64 65 66 67 68 69 6A 73 74 75 76 77 78 79 7A 82 
                                         83 84 85 86 87 88 89 8A 92 93 94 95 96 97 98 99 
                                         9A A2 A3 A4 A5 A6 A7 A8 A9 AA B2 B3 B4 B5 B6 B7 
                                         B8 B9 BA C2 C3 C4 C5 C6 C7 C8 C9 CA D2 D3 D4 D5 
                                         D6 D7 D8 D9 DA E2 E3 E4 E5 E6 E7 E8 E9 EA F2 F3 
                                         F4 F5 F6 F7 F8 F9 FA 
    Total number of codes: 162

 
*** Marker: SOS (Start of Scan) (xFFDA) ***
  OFFSET: 0x00000261
  Scan header length = 12
  Number of img components = 3
    Component[1]: selector=0x01, table=0(DC),0(AC)   /*组件1,即y分量,选择hufman表0*/
    Component[2]: selector=0x02, table=1(DC),1(AC) /*组件2,即Cb分量,选择hufman表1*/
    Component[3]: selector=0x03, table=1(DC),1(AC)  /*组件3,即Cr分量,选择hufman表1*/
  Spectral selection = 0 .. 63
  Successive approximation = 0x00

/帧数据信息/
*** Decoding SCAN Data ***
  OFFSET: 0x0000026F
  Scan Decode Mode: No IDCT (DC only)
    NOTE: Low-resolution DC component shown. Can decode full-res with [Options->Scan Segment->Full IDCT]

  Scan Data encountered marker   0xFFD9 @ 0x00005C59.0

  Compression stats:
    Compression Ratio:  7.51:1      /*压缩比*/
    Bits per pixel:     3.20:1        /*每个像素占比3.20位*/

  Huffman code histogram stats:      /*huffman编码的直方图统计,因为霍夫曼编码得实现知道没有每个字节数字出现的频次*/
    Huffman Table: (Dest ID: 0, Class: DC)
      # codes of length 01 bits:        0 (  0%)
      # codes of length 02 bits:       50 (  5%)
      # codes of length 03 bits:      426 ( 44%)
      # codes of length 04 bits:      170 ( 18%)
      # codes of length 05 bits:      187 ( 19%)
      # codes of length 06 bits:      111 ( 12%)
      # codes of length 07 bits:       16 (  2%)
      # codes of length 08 bits:        0 (  0%)
      # codes of length 09 bits:        0 (  0%)
      # codes of length 10 bits:        0 (  0%)
      # codes of length 11 bits:        0 (  0%)
      # codes of length 12 bits:        0 (  0%)
      # codes of length 13 bits:        0 (  0%)
      # codes of length 14 bits:        0 (  0%)
      # codes of length 15 bits:        0 (  0%)
      # codes of length 16 bits:        0 (  0%)

    Huffman Table: (Dest ID: 1, Class: DC)
      # codes of length 01 bits:        0 (  0%)
      # codes of length 02 bits:       49 ( 10%)
      # codes of length 03 bits:       49 ( 10%)
      # codes of length 04 bits:       91 ( 19%)
      # codes of length 05 bits:      101 ( 21%)
      # codes of length 06 bits:       96 ( 20%)
      # codes of length 07 bits:       70 ( 15%)
      # codes of length 08 bits:       20 (  4%)
      # codes of length 09 bits:        4 (  1%)
      # codes of length 10 bits:        0 (  0%)
      # codes of length 11 bits:        0 (  0%)
      # codes of length 12 bits:        0 (  0%)
      # codes of length 13 bits:        0 (  0%)
      # codes of length 14 bits:        0 (  0%)
      # codes of length 15 bits:        0 (  0%)
      # codes of length 16 bits:        0 (  0%)

    Huffman Table: (Dest ID: 0, Class: AC)
      # codes of length 01 bits:        0 (  0%)
      # codes of length 02 bits:    12107 ( 46%)
      # codes of length 03 bits:     3396 ( 13%)
      # codes of length 04 bits:     4828 ( 18%)
      # codes of length 05 bits:     2883 ( 11%)
      # codes of length 06 bits:      675 (  3%)
      # codes of length 07 bits:     1210 (  5%)
      # codes of length 08 bits:      598 (  2%)
      # codes of length 09 bits:      276 (  1%)
      # codes of length 10 bits:      207 (  1%)
      # codes of length 11 bits:       46 (  0%)
      # codes of length 12 bits:       26 (  0%)
      # codes of length 13 bits:        0 (  0%)
      # codes of length 14 bits:        0 (  0%)
      # codes of length 15 bits:        9 (  0%)
      # codes of length 16 bits:       65 (  0%)

    Huffman Table: (Dest ID: 1, Class: AC)
      # codes of length 01 bits:        0 (  0%)
      # codes of length 02 bits:     1502 ( 38%)
      # codes of length 03 bits:      687 ( 17%)
      # codes of length 04 bits:      777 ( 20%)
      # codes of length 05 bits:      616 ( 16%)
      # codes of length 06 bits:      218 (  5%)
      # codes of length 07 bits:       35 (  1%)
      # codes of length 08 bits:       78 (  2%)
      # codes of length 09 bits:       28 (  1%)
      # codes of length 10 bits:        8 (  0%)
      # codes of length 11 bits:        8 (  0%)
      # codes of length 12 bits:        4 (  0%)
      # codes of length 13 bits:        0 (  0%)
      # codes of length 14 bits:        1 (  0%)
      # codes of length 15 bits:        1 (  0%)
      # codes of length 16 bits:        2 (  0%)

  YCC clipping in DC:
    Y  component: [<0=    0] [>255=    0]
    Cb component: [<0=    0] [>255=    0]
    Cr component: [<0=    0] [>255=    0]

  RGB clipping in DC:
    R  component: [<0=    0] [>255=    0]
    G  component: [<0=    0] [>255=    0]
    B  component: [<0=    0] [>255=    0]

  Average Pixel Luminance (Y):
    Y=[ 57] (range: 0..255)

  Brightest Pixel Search:
    YCC=[  316,    8,  104] RGB=[185,157,168] @ MCU[ 13,  4]

  Finished Decoding SCAN Data
    Number of RESTART markers decoded: 0
    Next position in scan buffer: Offset 0x00005C58.4

 
*** Marker: EOI (End of Image) (xFFD9) ***
  OFFSET: 0x00005C59


*** Searching Compression Signatures ***

  Signature:           01E764F3ECB6C14A51FF83F1FF6D546B
  Signature (Rotated): 01E6610D026E8E6FE4BECEA9B3328A63
  File Offset:         0 bytes
  Chroma subsampling:  2x2
  EXIF Make/Model:     NONE
  EXIF Makernotes:     NONE
  EXIF Software:       NONE

  Searching Compression Signatures: (3347 built-in, 0 user(*) )

          EXIF.Make / Software        EXIF.Model                            Quality           Subsamp Match?
          -------------------------   -----------------------------------   ----------------  --------------
     CAM:[Minolta Co., Ltd.        ] [DiMAGE F100                        ] [                ] No               
     CAM:[NIKON                    ] [E2500                              ] [FINE            ] No               
     CAM:[NIKON                    ] [E4500                              ] [FINE            ] No               
     CAM:[NIKON                    ] [E5400                              ] [FINE            ] No               
     CAM:[NIKON                    ] [E5700                              ] [FINE            ] No               
     CAM:[NIKON                    ] [E775                               ] [FINE            ] No               
     CAM:[NIKON                    ] [E8700                              ] [FINE            ] No               
     CAM:[OLYMPUS CORPORATION      ] [C8080WZ                            ] [                ] No               
     CAM:[PENTAX                   ] [PENTAX K10D                        ] [                ] No               
     CAM:[SAMSUNG TECHWIN          ] [Pro 815                            ] [                ] No               
     CAM:[SAMSUNG TECHWIN          ] [VLUU NV 7, NV 7                    ] [                ] No               
     CAM:[SAMSUNG TECHWIN          ] [VLUU NV10, NV10                    ] [                ] No               
     CAM:[SEIKO EPSON CORP.        ] [PhotoPC 3000Z                      ] [                ] No               
     CAM:[SONY                     ] [DSC-F828                           ] [                ] No               
     CAM:[SONY                     ] [DSC-H1                             ] [                ] No               
     CAM:[SONY                     ] [DSC-H2                             ] [                ] No               
     CAM:[SONY                     ] [DSC-H5                             ] [                ] No               
     CAM:[SONY                     ] [DSC-H7                             ] [                ] No               
     CAM:[SONY                     ] [DSC-H9                             ] [                ] No               
     CAM:[SONY                     ] [DSC-L1                             ] [                ] No               
     CAM:[SONY                     ] [DSC-N1                             ] [                ] No               
     CAM:[SONY                     ] [DSC-P150                           ] [                ] No               
     CAM:[SONY                     ] [DSC-P200                           ] [                ] No               
     CAM:[SONY                     ] [DSC-S90                            ] [                ] No               
     CAM:[SONY                     ] [DSC-V3                             ] [                ] No               
     CAM:[SONY                     ] [DSC-W55                            ] [                ] No               
     CAM:[SONY                     ] [DSC-W7                             ] [                ] No               
     SW :[Apple ImageIO.framework  ]                                       [084             ]                  
     SW :[IJG Library              ]                                       [095             ]                  

    The following IJG-based editors also match this signature:
     SW :[GIMP                     ]                                       [095             ]                  
     SW :[IrfanView                ]                                       [095             ]                  
     SW :[idImager                 ]                                       [095             ]                  
     SW :[FastStone Image Viewer   ]                                       [095             ]                  
     SW :[NeatImage                ]                                       [095             ]                  
     SW :[Paint.NET                ]                                       [095             ]                  
     SW :[Photomatix               ]                                       [095             ]                  
     SW :[XnView                   ]                                       [095             ]                  

  Based on the analysis of compression characteristics and EXIF metadata:

  ASSESSMENT: Class 1 - Image is processed/edited

  This may be a new software editor for the database.
  If this file is processed, and editor doesn't appear in list above,
  PLEASE ADD TO DATABASE with [Tools->Add Camera to DB]

huffman表的生成可以看

https://www.xuebuyuan.com/1276291.html

libjpeg代码分析

一起来读一读libjpeg中编码部分的实现。源码来自libjpeg-9。
未完待续。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值