硬汉嵌入式论坛

 找回密码
 立即注册
查看: 4890|回复: 1
收起左侧

[LibJPEG] 【LibJPEG专题系列十五】文件格式的识别与处理

[复制链接]

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
发表于 2015-5-8 17:59:59 | 显示全部楼层 |阅读模式
                               Section 15—文件格式的识别与处理

    如果你有一个软件无法读取所谓JPEG文件,它很有可能是基于JPEG的专有格式。通过检查该文件的前
几个字节可以辨别你所拥有的格式:
     1.  JFIF标准文件FF D8 FF E0四个字节(十六进制)开始,后面是两个可变字节(通常十六进制00
10),后跟'JFIF'
     2.  如果是以FF FF D8开始,而不是“JFIF”标志,这可能是“不完全JFIF”的JPEG文件。大部分的
JFIF软件应该都可以毫无异议地对其进行读取。如果你正在使用的解码器对于JFIF标志的缺乏极其挑剔,
那么可以尝试使用另一种解码器。(非常老以及非常新的JPEG文件都有可能缺少JFIF标志---更何况之前所
提的新的SPIFF标准却不使用JFIF标志。所以,如果你觉得这是麻烦的话,可以向软件供应商提出不满。)
     3.  Mac机的PICT文件,如果是JPEG压缩的,将有后跟JPEG数据的几百字节的头文件(通常是726字节,
但不总是)。查找连续的三个字节(十六进制)FF D8 FF。文本“Photo - JPEG”通常会很快出现在头文件
之前,“AppleMark”或“JFIF”将会在其后立刻出现。去掉FF D8 FF之前的所有东西,然后你通常可以解
码该文件。(如果PICT图像被划分为多个“波段”,这样做将会失败;幸好波状PICT图像不是很常见。波状
PICT包含多个JPEG数据流,其高度加起来相当于整个图像的高度。这些数据流应该固定一起形成一个图像。
Bailey Brown有一些简单的工具用于这一目的,详情可见于网页http://www.isomedia.com/homes/bailey/
photo-jpeg/photo-jpeg.html
     4.  如果图像文件来自Mac机,它也可以是附着MacBinary头文件的标准JFIF文件。在这种情况下,JFIF
文件将会是128个字节。
     5.  此外:它只是一个专有格式,而非JPEG。幸运的话,该文件有可能包含一个头文件以及一个原始的
JPEG数据流。如果你能识别JPEG数据流的开端(查找FF D8),可以尝试去掉其之前的所有东西。


    至少HiJaak Pro的发行可编写JFIF文件,并声称是2.01版本。事实上没有这样的规范,最新的JFIF版本是
1.02。看起来像是HiJaak向后得到了高低字节。不幸的是,遇到这些文件时,大多数的JFIF软件将会选择放弃,
这是因为JFIF规范定义一个主版本号的变化来表示一个不兼容的格式转换。如果真的存在2.01的版本,它会这
样被编号是因为目前的软件不能也不会尝试对其进行读取。(人们不禁要怀疑HiJaak是否听说过与其他软件的
交叉测试。)如果碰到这些不按次序排列的文件,你可以用一个二进制文件编辑器对其进行修复,通过将文件
的第12个字节由2改为1
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

740

主题

1326

回帖

3546

积分

管理员

春暖花开

Rank: 9Rank: 9Rank: 9

积分
3546
QQ
 楼主| 发表于 2015-5-8 18:02:51 | 显示全部楼层
                    Section - [15] How do I recognize which file
                       format I have, and what do I do about it?

If you have an alleged JPEG file that your software won't read, it's likely to be some proprietary JPEG-
based format.  You can tell what you have by inspecting the first few bytes of the file:
     1.  A JFIF-standard file will start with the four bytes (hex) FF D8 FF E0, followed by two variable b
ytes (often hex 00 10), followed by 'JFIF'.
     2.  If you see FF D8 FF at the start, but not the 'JFIF' marker, you probably have a not-quite-JFIF
JPEG file.  Most JFIF software should read it without complaint.  If you are using something that is
picky enough to complain about the lack of a JFIF marker, try another decoder. (Both very old JPEG
files and very new ones may lack JFIF markers --- the new SPIFF standard mentioned above doesn't
use a JFIF marker. So gripe to your software vendor if you find this to be the problem.)
     3.  A Macintosh PICT file, if JPEG-compressed, will have several hundred bytes of header (often 726
bytes, but not always) followed by JPEG data. Look for the 3-byte sequence (hex) FF D8 FF.  The text
&#39hoto - JPEG' will usually appear shortly before this header, and 'AppleMark' or 'JFIF' will usually ap-
pear shortly after it.  Strip off everything before the FF D8 FF and you will usually be able to decode the
file. (This will fail if the PICT image is divided into multiple "bands"; fortunately banded PICTs aren't
very common.  A banded PICT contains multiple JPEG datastreams whose heights add up to the total
image  height.  These need to be stitched back together into one image. Bailey Brown has some simple
tools for this purpose on a Web page at http://www.isomedia.com/homes/bailey/photo-jpeg/photo-jpeg.html.)
          4.  If the file came from a Macintosh, it could also be a standard JFIF file with a MacBinary header
attached.  In this case, the JFIF header will appear 128 bytes into the file.  Get rid of the first 128 bytes
and you're set.
    5.  Anything else: it's a proprietary format, or not JPEG at all.  If you are lucky, the file may consist o
f a header and a raw JPEG data stream.If you can identify the start of the JPEG data stream (look for
FF D8), try stripping off everything before that.


    At least one release of HiJaak Pro writes JFIF files that claim to be revision 2.01.  There is no such spec;
the latest JFIF revision is 1.02. It looks like HiJaak got the high and low bytes backwards.  Unfortunately,
most JFIF readers will give up on encountering these files, because the JFIF spec defines a major version
number change to mean an incompatible format change.  If there ever *were* a version 2.01, it would be so
numbered because current software could not read it and should not try.  (One wonders if HiJaak has ever
heard of cross-testing with other people's software.) If you run into one of these misnumbered files, you
can fix it with a binary-file editor, by changing the twelfth byte of the file from 2 to 1.
努力打造安富莱高质量微信公众号:点击扫描图片关注
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-12 05:25 , Processed in 0.146033 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表