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

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

问题描述

我一直在做一些关于文件格式的阅读,我对它们很感兴趣。我想知道什么过程是创建一个格式。例如,.jpeg或.gif或音频格式。你会用什么编程语言(如果你使用的是编程语言)?

该网站警告说,这个问题可能会被封闭,但这只是我追求知识的一个风险。 :)

解决方案

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

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

  00000008000000080000FFFF00000000FFFF0000 ... 

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

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

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.

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...

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).

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天全站免登陆