Jpg文件格式[参考]

微处理机中的存放顺序有正序(big endian)和逆序(little endian)之分。正序存放就是高字节存放在前低字节在后,而逆序存放就是低字节在前高字节在后。例如,十六进制数为A02B,正序存放就是A02B, 逆序存放就是2BA0。摩托罗拉(Motorola)公司的微处理器使用正序存放,而英特尔(Intel)公司的微处理器使用逆序。JPEG文件中的字节 是按照正序排列的。


JPEG委员会在制定JPEG标准时,定义了许多标记(marker)用来区分和识别图像数据及其相关信息,但笔者没有找到JPEG委员会对JPEG文件交换格式的明确定义。直到1998年12月从分析网上具体的JPG图像来看,使用比较广泛的还是JPEG文件交换格式(JPEG File Interchange Format,JFIF)版本号为1.02。这是1992年9月由在C-Cube Microsystems公司工作的Eric Hamilton提出的。此外还有TIFF JPEG等格式,但由于这种格式比较复杂,因此大多数应用程序都支持JFIF文件交换格式。

JPEG文件使用的颜色空间是CCIR 601推荐标准进行的彩色空间(参看第7章)。在这个彩色空间中,每个分量、每个像素的电平规定为255级,用8位代码表示。从RGB转换成YCbCr空间时,使用下面的精确的转换关系:

       Y = 256 * E'y

      Cb = 256 * [E'Cb] + 128

      Cr = 256 * [E'Cr] + 128
其中亮度电平E'y和色差电平E'Cb和E'Cb分别是CCIR 601定义的参数。由于E'y的范围是0~1,E'Cb和E'Cb的范围是-0.5~+0.5,因此Y, Cb和Cr的最大值必须要箝到255。于是RGB和YCbCr之间的转换关系需要按照下面的方法计算。

(1) 从RGB转换成YCbCr

YCbCr(256级)分量可直接从用8位表示的RGB分量计算得到:

       Y =   0.299 R + 0.587 G  + 0.114 B

     Cb = - 0.1687R - 0.3313G  + 0.5   B + 128

    Cr = 0.5 R - 0.4187G - 0.0813 B + 128

需要注意的是不是所有图像文件格式都按照R0,G0,B0,…… Rn,Gn,Bn的次序存储样本数据,因此在RGB文件转换成JFIF文件时需要首先验证RGB的次序。

(2) 从YCbCr转换成RGB

RGB分量可直接从YCbCr(256级)分量计算得到:

     R = Y                 + 1.402 (Cr-128)

      G = Y - 0.34414 (Cb-128) - 0.71414 (Cr-128)

      B = Y + 1.772 (Cb-128)

在JFIF文件格式中,图像样本的存放顺序是从左到右和从上到下。这就是说JFIF文件中的第一个图像样本是图像左上角的样本。

2 文件结构

JFIF文件格式直接使用JPEG标准为应用程序定义的许多标记,因此JFIF格式成了事实上JPEG文件交换格式标准。JPEG的每个标记都是由
2个字节组成,其前一个字节是固定值0xFF。每个标记之前还可以添加数目不限的0xFF填充字节(fill byte)。下面是其中的8个标记:

  1. SOI  0xD8            图像开始
  2. APP0 0xE0            JFIF应用数据块
  3. APPn 0xE1 - 0xEF    其他的应用数据块(n, 1~15)
  4. DQT  0xDB           量化表
  5. SOF0 0xC0            帧开始
  6. DHT  0xC4           霍夫曼(Huffman)表
  7. SOS  0xDA           扫描线开始
  8. EOI  0xD9            图像结束

为使读者对JPEG定义的标记一目了然,现将JPEG的标记码列于表6-05,并保留英文解释。

表6-05 JPEG定义的标记

Symbol

(符号)

Code Assignment

(标记代码)

Description

(说明)

Start Of Frame markers, non-hierarchical Huffman coding

SOF0

0xFFC0

Baseline DCT

SOF1

0xFFC1

Extended sequential DCT

SOF2

0xFFC2

Progressive DCT

SOF3

0xFFC3

Spatial (sequential) lossless

Start Of Frame markers, hierarchical Huffman coding

SOF5

0xFFC5

Differential sequential DCT

SOF6

0xFFC6

Differential progressive DCT

SOF7

0xFFC7

Differential spatial lossless

Start Of Frame markers, non-hierarchical arithmetic coding

JPG

0xFFC8

Reserved for JPEG extensions

SOF9

0xFFC9

Extended sequential DCT

SOF10

0xFFCA

Progressive DCT

SOF11

0xFFCB

Spatial (sequential) Lossless

Start Of Frame markers, hierarchical arithmetic coding

SOF13

0xFFCD

Differential sequential DCT

SOF14

0xFFCE

Differential progressive DCT

SOF15

0xFFCF

Differential spatial Lossless

Huffman table specification

DHT

0xFFC4

Define Huffman table(s)

arithmetic coding conditioning specification

DAC

0xFFCC

Define arithmetic conditioning table

Restart interval termination

RSTm

0xFFD0~0xFFD7

Restart with modulo 8 counter m

Other marker

SOI

0xFFD8

Start of image

EOI

0xFFD9

End of image

SOS

0xFFDA

Start of scan

DQT

0xFFDB

Define quantization table(s)

DNL

0xFFDC

Define number of lines

DRI

0xFFDD

Define restart interval

DHP

0xFFDE

Define hierarchical progression

EXP

0xFFDF

Expand reference image(s)

APPn

0xFFE0~0xFFEF

Reserved for application use

JPGn

0xFFF0~0xFFFD

Reserved for JPEG extension

COM

0xFFFE

Comment

Reserved markers

TEM

0xFF01

For temporary use in arithmetic coding

RES

0xFF02~0xFFBF

Reserved

JPEG文件由下面的8个部分组成:

(1) 图像开始SOI(Start of Image)标记

(2) APP0标记(Marker)

① APP0长度(length)

② 标识符(identifier)

③ 版本号(version)

④ X和Y的密度单位(units=0:无单位;units=1:点数/英寸;units=2:点数/厘米)

⑤ X方向像素密度(X density)

⑥ Y方向像素密度(Y density)

⑦ 缩略图水平像素数目(thumbnail horizontal pixels)

⑧ 缩略图垂直像素数目(thumbnail vertical pixels)

⑨ 缩略图RGB位图(thumbnail RGB bitmap)

(3) APPn标记(Markers),其中n=1~15(任选)

① APPn长度(length)

② 由于详细信息(application specific information)

(4) 一个或者多个量化表DQT(difine quantization table)

① 量化表长度(quantization table length)

② 量化表数目(quantization table number)

③ 量化表(quantization table)

(5) 帧图像开始SOF0(Start of Frame)

① 帧开始长度(start of frame length)

② 精度(precision),每个颜色分量每个像素的位数(bits per pixel per color component)

③ 图像高度(image height)

④ 图像宽度(image width)

⑤ 颜色分量数(number of color components)

⑥ 对每个颜色分量(for each component)

    • ID
    • 垂直方向的样本因子(vertical sample factor)
    • 水平方向的样本因子(horizontal sample factor)
    • 量化表号(quantization table#)

(6) 一个或者多个霍夫曼表DHT(Difine Huffman Table)

① 霍夫曼表的长度(Huffman table length)

② 类型、AC或者DC(Type, AC or DC)

③ 索引(Index)

④ 位表(bits table)

⑤ 值表(value table)

(7) 扫描开始SOS(Start of Scan)

① 扫描开始长度(start of scan length)

② 颜色分量数(number of color components)

③ 每个颜色分量

    • ID
    • 交流系数表号(AC table #)
    • 直流系数表号(DC table #)

④ 压缩图像数据(compressed image data)

(8) 图像结束EOI(End of Image)

表6-06表示了APP0域的详细结构。有兴趣的读者可通过UltraEdit或者PC TOOLS等工具软件打开一个JPG图像文件,对APP0的结构进行分析和验证。

表6-06 JFIF格式中APP0域的详细结构

偏移

长度

内容

块的名称

说明

0

2 byte

0xFFD8

(Start of Image,SOI)

图像开始

2

2 byte

0xFFE0

APP0(JFIF application segment)

JFIF应用数据块

4

2 bytes

 

length of APP0 block

APP0块的长度

6

5 bytes

 

"JFIF"+"0"

识别APP0标记

11

1 byte

 

<Major version>

主要版本号(如版本1.02中的1)

12

1 byte

 

<Minor version>

次要版本号(如版本1.02中的02)

13

1 byte

 

<Units for the X
and Y densities>

X和Y的密度单位

units=0:无单位

units=1:点数/英寸

units=2:点数/厘米

14

2 bytes

 

<Xdensity>

水平方向像素密度

16

2 bytes

 

<Ydensity>

垂直方向像素密度

18

1 byte

 

<Xthumbnail>

缩略图水平像素数目

19

1 byte

 

<Ythumbnail>

缩略图垂直像素数目

 

3n

 

< Thumbnail RGB bitmap>

缩略RGB位图(n为缩略图的像素数)

     

Optional JFIF extension APP0 marker segment(s)

任选的JFIF扩展APP0标记段

 

……

 

……

 
 

2 byte

0xFFD9

(EOI) end-of-file

图像文件结束标记

===================

JPEG Header Format

Strictly speaking, JPEG files do not have formal headers, but fg_jpeghead() and fgi_jpeghead() return relevant information from the file's start of frame segment. We call it a header for consistency with other image file formats.

offset

size

description

0

2

JPEG SOI marker (FFD8 hex)

2

2

image width in pixels

4

2

image height in pixels

6

1

number of components (1 = grayscale, 3 = RGB)

7

1

horizontal/vertical sampling factors for component 1

8

1

sampling factors for component 2 (if RGB)

9

1

sampling factors for component 3 (if RGB)

===================

wiki:

Syntax and structure

A JPEG image consists of a sequence of segments, each beginning with a marker, each of which begins with a 0xFF byte followed by a byte indicating what kind of marker it is. Some markers consist of just those two bytes; others are followed by two bytes (high then low) indicating the length of marker-specific payload data that follows. (The length includes the two bytes for the length, but not the two bytes for the marker.) Some markers are followed by entropy-coded data; the length of such a marker does not include the entropy-coded data. Note that consecutive 0xFF bytes are used as fill bytes for padding purposes, although this fill byte padding should only ever take place for markers immediately following entropy-coded scan data (see JPEG specification section B.1.1.2 and E.1.2 for details; specifically "In all cases where markers are appended after the compressed data, optional 0xFF fill bytes may precede the marker").

Within the entropy-coded data, after any 0xFF byte, a 0x00 byte is inserted by the encoder before the next byte, so that there does not appear to be a marker where none is intended, preventing framing errors. Decoders must skip this 0x00 byte. This technique, called byte stuffing (see JPEG specification section F.1.2.3), is only applied to the entropy-coded data, not to marker payload data. Note however that entropy-coded data has a few markers of its own; specifically the Reset markers (0xD0 through 0xD7), which are used to isolate independent chunks of entropy-coded data to allow parallel decoding, and encoders are free to insert these Reset markers at regular intervals (although not all encoders do this).

Common JPEG markers[19]
Short name Bytes Payload Name Comments
SOI 0xFF, 0xD8 none Start Of Image  
SOF0 0xFF, 0xC0 variable size Start Of Frame (baseline DCT) Indicates that this is a baseline DCT-based JPEG, and specifies the width, height, number of components, and component subsampling (e.g., 4:2:0).
SOF2 0xFF, 0xC2 variable size Start Of Frame (progressive DCT) Indicates that this is a progressive DCT-based JPEG, and specifies the width, height, number of components, and component subsampling (e.g., 4:2:0).
DHT 0xFF, 0xC4 variable size Define Huffman Table(s) Specifies one or more Huffman tables.
DQT 0xFF, 0xDB variable size Define Quantization Table(s) Specifies one or more quantization tables.
DRI 0xFF, 0xDD 4 bytes Define Restart Interval Specifies the interval between RSTn markers, in macroblocks. This marker is followed by two bytes indicating the fixed size so it can be treated like any other variable size segment.
SOS 0xFF, 0xDA variable size Start Of Scan Begins a top-to-bottom scan of the image. In baseline DCT JPEG images, there is generally a single scan. Progressive DCT JPEG images usually contain multiple scans. This marker specifies which slice of data it will contain, and is immediately followed by entropy-coded data.
RSTn 0xFF, 0xDn (n=0..7) none Restart Inserted every r macroblocks, where r is the restart interval set by a DRI marker. Not used if there was no DRI marker. The low three bits of the marker code cycle in value from 0 to 7.
APPn 0xFF, 0xEn variable size Application-specific For example, an Exif JPEG file uses an APP1 marker to store metadata, laid out in a structure based closely on TIFF.
COM 0xFF, 0xFE variable size Comment Contains a text comment.
EOI 0xFF, 0xD9 none End Of Image  

There are other Start Of Frame markers that introduce other kinds of JPEG encodings.

Since several vendors might use the same APPn marker type, application-specific markers often begin with a standard or vendor name (e.g., "Exif" or "Adobe") or some other identifying string.

At a restart marker, block-to-block predictor variables are reset, and the bitstream is synchronized to a byte boundary. Restart markers provide means for recovery after bitstream error, such as transmission over an unreliable network or file corruption. Since the runs of macroblocks between restart markers may be independently decoded, these runs may be decoded in parallel.

 
  • The 2008-04-06 00:43

JPEG格式的更多相关文章

  1. 图像JPEG格式介绍

    1 JPG格式介绍 JPEG (Joint PhotographicExperts GROUP)是由国际标准组织和国际电话电报咨询委员会为静态图像所建立的第一个国际数字图像压缩标准,也是至今一直在使用 ...

  2. 让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式

    我们在Centos安装ImageMagick教程中讲述了如何安装ImageMagick,安装完毕之后发现程序并不支持png和jpeg格式的图片,但是这两种图片又是我们平时所常见的,所以我们还要进一步地 ...

  3. ecshop图片上传JPEG格式失败问题

    在根目录下找到includes文件目录,在其目录中找到cls_image.php打开并找到: $allow_file_types = '|GIF|JPG|JEPG|PNG|BMP|SWF|'; 此处J ...

  4. 嵌入式Linux基于framebuffer的jpeg格式本地LCD屏显示

    在基于Linux的视频监控采集系统中,摄像头采集到的一帧视频图像数据一般都是经过硬件自动压缩成jpeg格式的,然后再保存到摄像头设备的缓冲区.如果要把采集到的jpeg格式显示在本地LCD屏上,由于我们 ...

  5. 超图不支持JPEG格式的WMTS服务

    就目前面而言,超图不支持JPEG格式的WMTS服务,只支持PNG格式的. <本篇完>

  6. 一般源码安装添加的GD库 是不支持 jpeg 格式的图片的

    一般源码安装添加的GD库 是不支持 jpeg 格式的图片的,只支持如下格式 GD Support enabled GD Version bundled (2.0.34 compatible) GIF ...

  7. linux下将jpg,jpeg格式转为PDF

    1.安装imagemagick(用其中的convert)和gthumb     sudo apt-get install imagemagick gthumb 2.将tiff图片转换为png或jpeg ...

  8. Call to undefined function imagecreatefromjpeg() 让GD支持JPEG格式的图片扩展

    安装扩展支持jpeg格式: 第一步:首先下载文件: 版本v8: wget http://www.ijg.org/files/jpegsrc.v8b.tar.gz 版本v9: wget http://w ...

  9. JPEG格式 介绍

    JPEG格式 jpeg是有损压缩格式, 将像素信息用jpeg保存成文件再读取出来,其中某些像素值会有少许变化.在保存时有个质量参数可在[0,100]之间选择,参数越大图片就越保真,但图片的体积也就越大 ...

随机推荐

  1. 设计模式C#实现(十)——桥接模式

    意图 0 适用性 1 结构 2 实现 3 效果 4 意图 将抽象部分与它的实现部分分离,使它们都可以独立的变化. 适用性 1.不希望抽象和实现间有固定的绑定关系2.类的抽象以及它的实现都可以通过生成子 ...

  2. MMORPG大型游戏设计与开发(客户端架构 part12 of vegine)

    在游戏中的交互过程中输入是一个必不可少的过程,比如登陆的时候需要用户输入用户名与密码,就算是单机游戏很多时候也要求用户输入一个用户名作为存档的依据.网络游戏中没有了输入,只用鼠标来交互是不切实际的,因 ...

  3. 模版(template)

    模版(template) 在c++Template中很多地方都用到了typename与class这两个关键字,而且好像可以替换,是不是这两个关键字完全一样呢? 相信学习C++的人对class这个关键字 ...

  4. AC日记——单词替换 1.7 21

    21:单词替换 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个字符串,以回车结束(字符串长度<=100).该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区 ...

  5. 使用mx:Repeater在删除和添加item时列表闪烁

    使用mx:Repeater在删除和添加item时列表闪烁 不可能在用户界面上闪闪的吧,recycleChildren属性可帮助我们 recycleChildren属性==缓存,设为true就可以了 本 ...

  6. java 22 - 21 多线程之多线程的代码实现方式3

    JDK5新增了一个Executors工厂类来产生线程池,有如下几个方法 A.public static ExecutorService newCachedThreadPool() B.public s ...

  7. 一个简单的socket程序运行与抓包查看

    为了熟悉socket编程,在ubuntu下运行了一个现有例子(Networking and Socket programming tutorial in C - CodeProject),并通过抓包查 ...

  8. 《ASP.NET MVC 5 高级编程(第5版)》

    第1章.入门 本章主要内容: ASP.NET MVC 5概述 其应用程序的创建方法 其应用程序的及结构 概述:将MVC设计模式应用于ASP.NET框架 ASP.NET 1.0支持两层抽象: Syste ...

  9. 为什么mysql设置了密码之后,本地还可以直接访问,不需要输入密码就可以登录数据库了?

    应为数据库里面有空用户 select * from mysql.user where user=''; 查询如果有,把他删了然后重启mysql服务. 他有空用户你删除了 然后重启mysql生效,这个是 ...

  10. javascript中的双向队列

    1.概念 我们知道队列是一种先进先出的结构,只能在队伍的开头添加元素,队伍的结尾删除元素.双向队列的概念就是同时允许在队伍的开头和结尾添加和删除元素.在javascript中有一个处理数组的方法Arr ...