JPEG格式
微处理机中的存放顺序有正序(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个标记:
- SOI 0xD8 图像开始
- APP0 0xE0 JFIF应用数据块
- APPn 0xE1 - 0xEF 其他的应用数据块(n, 1~15)
- DQT 0xDB 量化表
- SOF0 0xC0 帧开始
- DHT 0xC4 霍夫曼(Huffman)表
- SOS 0xDA 扫描线开始
- 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 |
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).
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格式的更多相关文章
- 图像JPEG格式介绍
1 JPG格式介绍 JPEG (Joint PhotographicExperts GROUP)是由国际标准组织和国际电话电报咨询委员会为静态图像所建立的第一个国际数字图像压缩标准,也是至今一直在使用 ...
- 让阿里云的Centos,PHP组件 ImageMagick支持png和jpeg格式
我们在Centos安装ImageMagick教程中讲述了如何安装ImageMagick,安装完毕之后发现程序并不支持png和jpeg格式的图片,但是这两种图片又是我们平时所常见的,所以我们还要进一步地 ...
- ecshop图片上传JPEG格式失败问题
在根目录下找到includes文件目录,在其目录中找到cls_image.php打开并找到: $allow_file_types = '|GIF|JPG|JEPG|PNG|BMP|SWF|'; 此处J ...
- 嵌入式Linux基于framebuffer的jpeg格式本地LCD屏显示
在基于Linux的视频监控采集系统中,摄像头采集到的一帧视频图像数据一般都是经过硬件自动压缩成jpeg格式的,然后再保存到摄像头设备的缓冲区.如果要把采集到的jpeg格式显示在本地LCD屏上,由于我们 ...
- 超图不支持JPEG格式的WMTS服务
就目前面而言,超图不支持JPEG格式的WMTS服务,只支持PNG格式的. <本篇完>
- 一般源码安装添加的GD库 是不支持 jpeg 格式的图片的
一般源码安装添加的GD库 是不支持 jpeg 格式的图片的,只支持如下格式 GD Support enabled GD Version bundled (2.0.34 compatible) GIF ...
- linux下将jpg,jpeg格式转为PDF
1.安装imagemagick(用其中的convert)和gthumb sudo apt-get install imagemagick gthumb 2.将tiff图片转换为png或jpeg ...
- Call to undefined function imagecreatefromjpeg() 让GD支持JPEG格式的图片扩展
安装扩展支持jpeg格式: 第一步:首先下载文件: 版本v8: wget http://www.ijg.org/files/jpegsrc.v8b.tar.gz 版本v9: wget http://w ...
- JPEG格式 介绍
JPEG格式 jpeg是有损压缩格式, 将像素信息用jpeg保存成文件再读取出来,其中某些像素值会有少许变化.在保存时有个质量参数可在[0,100]之间选择,参数越大图片就越保真,但图片的体积也就越大 ...
随机推荐
- iOS 判断内容是否是中文,两种实现
用category实现 新建类别文件,代码 .h文件 #import <Foundation/Foundation.h> @interface NSString (Valid) - (BO ...
- Linux rpm 查询
[root@wang /]# rpm -qa // 查看安装所有包 [root@wang /]# rpm -qa |grep vim // 查询所安装的包 +包名 [root@wang /]# rpm ...
- 天朝使用GAE入门指南
0. 引言 Across the Great Wall, we can reach every corner in the world. 洒家最近玩了几下 Google App Engine.由于众所 ...
- Eclipse 各种小图标的含义
Eclipse的Package Explorer和Outline中用图标表示了很多内容,其实能看懂这些图标在编程中也是很有用的. 绿色的小圆表示公有类型:如果是实心的就是函数(),如果是圈就表示是变量 ...
- KSFramework配置表:扩展表格解析类型
解析和扩展表格 配置表示例 配置表模块在编译时,把Excel转化成TSV,并根据Excel的头部信息,生成对应的代码: 比如源码库中的Test.xlsx Excel文件,两个列头,Id和Value,其 ...
- Xbox Live会员身份:银会员和金会员
推出Xbox360之后,微软重新设计了Xbox Live平台.新设计建立了两个等级的Xbox Live会员身份:银会员和金会员.
- hdu 2586 How far away
How far away ? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- android java.io.IOException: open failed: EBUSY (Device or resource busy)
今天遇到一个奇怪的问题, 测试在程序的下载界面,下载一个文件第一次下载成功,删除后再下载结果下载报错, 程序:file.createNewFile(); 报错:java.io.IOException: ...
- OC中的指针
NSError *err = nil; NSError __strong **error = &err; //因为在oc中,通过* *err 创建的指针是用__strong修改的,所以要一致, ...
- js模拟高级语言的重载
js以递归的方式模拟高级语言的重载,我以添加元素节点为例子: //现有的子元素之前插入一个新的子元素 var before = function(elem,newElement,targetEleme ...