如何创建文件格式? [英] How do you create a file format?

查看:63
本文介绍了如何创建文件格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读一些文件格式方面的内容,并且对它们非常感兴趣.我想知道创建格式的过程是什么.例如,.jpeg、.gif 或音频格式.你会使用什么编程语言(如果你使用编程语言的话)?

I've been doing some reading on file formats and I'm very interested in them. I'm wondering what the process is to create a format. For example, a .jpeg, or .gif, or an audio format. What programming language would you use (if you use a programming language at all)?

该网站警告我,这个问题可能会被关闭,但这只是我在追求知识时会冒的风险.:)

The site warned me that this question might be closed, but that's just a risk I'll take in the pursuit of knowledge. :)

推荐答案

您不需要编程语言来编写文件格式的规范,尽管文字处理器可能被证明是一种方便的工具.

You do not need a programming language to write the specification for a file format, although a word processor might prove to be a handy tool.

基本上,您需要决定如何将文件的信息存储为位序列.这可能是微不足道的,也可能是极其困难的.举个简单的例子,一种非常原始的位图图像格式可以从一个表示位图宽度的无符号 32 位整数开始,然后再用一个表示位图高度的整数.然后你可以决定简单地按顺序写出像素的颜色,从左到右和从上到下(第 1 行像素,第 2 行像素,...),使用每像素 24 位,在形式为 8 位红色 + 8 位绿色 + 8 位蓝色.例如,由交替的蓝色和红色像素组成的 8×8 位图将存储为

Basically, you need to decide how the information of the file is to be stored as a sequence of bits. This might be trivial, or it might be exceedingly difficult. As a trivial example, a very primitive bitmap image format could start with one unsigned 32-bit integer representing the width of the bitmap, and then one more such integer representing the height of the bitmap. Then you could decide to simply write out the colour of the pixels sequentially, left-to-right and top-to-bottom (row 1 of pixels, row 2 of pixels, ...), using 24-bits per pixel, on the form 8 bits for red + 8 bits for green + 8 bits for blue. For instance, a 8×8 bitmap consisting of alternating blue and red pixels would be stored as

00000008000000080000FFFF00000000FFFF0000...

在一个不太简单的例子中,它实际上取决于您希望保存的数据.通常你会定义很多记录/结构,例如 BITMAPINFOHEADER,并指定它们应该以什么顺序出现,它们应该如何嵌套,并且您可能需要编写很多索引和查找表.我自己编写了很多文件格式,最近是用于保存 AlgoSim 结构的 ASD(AlgoSim 数据)文件格式.此类文件由许多记录(可能嵌套)、查找表、魔术词(指示结构开始、结构结束等)和自定义格式的字符串组成.通常简化文件格式的一个典型事情是记录包含有关其大小的数据,以及记录之后的自定义数据部分的大小(如果记录是某种标题,则以自定义格式在前面的数据,例如像素颜色或声音样本).

In a less trivial example, it really depends on the data you wish to save. Typically you would define a lot of records/structures, such as BITMAPINFOHEADER, and specify in what order they should come, how they should be nestled, and you might need to write a lot of indicies and look-up tables. Myself I have written quite a few file formats, most recently the ASD (AlgoSim Data) file format used to save AlgoSim structures. Such files consists of a number of records (maybe nestled), look-up tables, magic words (indicating structure begin, structures end, etc.) and strings in a custom-defined format. One typical thing that often simplifies the file format is that the records contain data about their size, and the sizes of the custom data parts following the record (in case the record is some sort of a header, preceeding data in a custom format, e.g. pixel colours or sound samples).

如果您以前没有使用过文件格式,我建议您学习一种非常简单的格式,例如 Windows 3 位图格式,并编写自己的 BMP 编码器/解码器,即创建和读取 BMP 的程序文件(从头开始),并显示读取的 BMP 文件.那么你现在的基本想法.

If you havn't been working with file formats before, I would suggest that you learn a very simple format, such as the Windows 3 Bitmap format, and write your own BMP encoder/decoder, i.e. programs that creates and reads BMP files (from scratch), and displays the read BMP files. Then you now the basic ideas.

这篇关于如何创建文件格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆