Compression · 压缩
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| compression/kəmˈpreʃn/ | 压缩 | yā suō |
| lossless/ˈlɒsləs/ | 无损 | wú sǔn |
| lossy/ˈlɒsi/ | 有损 | yǒu sǔn |
| bandwidth/ˈbændwɪdθ/ | 带宽 | dài kuān |
| run-length encoding/rʌn leŋθ enˈkəʊdɪŋ/ | 行程编码 | xíng chéng biān mǎ |
| dictionary coding/ˈdɪkʃənəri ˈkəʊdɪŋ/ | 字典编码 | zì diǎn biān mǎ |
| Huffman coding/ˈhʌfmən ˈkəʊdɪŋ/ | 霍夫曼编码 | huò fū màn biān mǎ |
| spatial/ˈspeɪʃl/ | 空间 | kōng jiān |
| temporal/ˈtempərəl/ | 时间 | shí jiān |
The three letters that made the web possible
- In 1987 a programmer named Phil Katz wrote a compression program and put the file format in the public domain. He called it ZIP. He was 24.
- Nothing about the web works without that idea. A single uncompressed second of HD video is about 187 MB; a two-hour film would be 1.3 terabytes. Streaming it over a home connection is arithmetically impossible.
- The films you watch are perhaps a thousandth of that, and they still look right, because the compression throws away things your eye was never going to notice.
- This lesson is compression 压缩: the two families, the three lossless methods, and how to justify one for a given file.
让万维网成为可能的那三个字母
- 1987 年,一位叫 Phil Katz 的程序员写了一个压缩程序,并把文件格式放入公有领域。他把它叫做 ZIP。当时他 24 岁。
- 没有这个想法,今天的网络没有一样能运转。未压缩的高清视频一秒约 187 MB;一部两小时的电影会是 1.3 TB。用家庭宽带播放它在算术上就不可能。
- 你看的电影大概只有那个的千分之一,而且看起来仍然对,因为压缩扔掉的是你的眼睛本来就不会注意到的东西。
- 这一课讲压缩(compression):两个家族、三种无损方法,以及怎样为给定文件论证选择。
Lossless and lossy
- Compression reduces a file's size, saving storage space and transmission bandwidth 带宽, and making downloads and streams faster.
- Lossless 无损 compression is compression from which the original data can be recovered exactly. ZIP and PNG are lossless.
- Lossy 有损 compression permanently removes some data, so the original cannot be recovered. JPEG, MP3 and streamed video are lossy.
Recover exactly, or shrink much further
无损与有损
- 压缩减小文件大小,节省存储空间和传输带宽(bandwidth),让下载和流媒体更快。
- 无损(lossless)压缩是能精确恢复原始数据的压缩。ZIP 和 PNG 是无损的。
- 有损(lossy)压缩永久移除部分数据,所以原始数据无法恢复。JPEG、MP3 和流媒体视频是有损的。

要么精确恢复,要么缩得更狠
Lossless compression means: · 无损压缩意味着:
Lossless compression lets you rebuild the original data exactly — essential for text, programs and ZIP/PNG. · 无损压缩让你精确地重建原始数据——对文本、程序和 ZIP/PNG 必不可少。
Match each compression idea to what it means. · 把每个压缩概念与它的含义配对。
Lossless keeps every bit (needed for text/code); lossy trades quality for size (photos, audio). · 无损保留每一位(文本/代码需要);有损以质量换大小(照片、音频)。
Lossless compression rebuilds the original data exactly (needed for text and programs), while lossy compression permanently removes some data to shrink the file (used for photos and audio). · 无损压缩完全重建原始数据(文本和程序需要),而有损压缩永久移除一些数据以缩小文件(用于照片和音频)。
That is why a program or a ZIP must be lossless, but a photo or a song can use lossy compression. · 这就是为什么程序或 ZIP 必须无损,而照片或歌曲可以使用有损压缩。
Worked example: justify the choice
- A company archives its accounting spreadsheets. Which kind of compression, and why? Lossless, because the spreadsheet must be restored exactly; a single changed value would make the accounts wrong.
- A photographer uploads holiday photographs to a phone gallery. Lossy, because the photographs are viewed on a small screen where the dropped detail is not visible, and the smaller files upload faster and use less storage.
- Name the kind, then give the reason from the situation. "Lossy is smaller" on its own is not a justification.
例题:论证选择
- 一家公司归档它的会计电子表格。用哪种压缩,为什么? 无损,因为电子表格必须被精确还原;一个值变了账目就错了。
- 一位摄影师把度假照片上传到手机相册。 有损,因为照片在小屏幕上观看,丢掉的细节看不出来,而更小的文件上传更快、占用存储更少。
- 说出种类,再给出来自情境的理由。单说"有损更小"不是论证。
Which file should be compressed losslessly? · 哪个文件应该被无损压缩?
Source code must be recovered exactly — a single changed character could break it — so it needs lossless compression. · 源代码必须被精确地恢复——一个改变的字符就可能破坏它——所以它需要无损压缩。
Run-length encoding
- Run-length encoding 行程编码 (RLE) replaces a run of repeated values with one value and a count: instead of eight identical white pixels, store "8, white".
- It is excellent on data with long runs, such as icons, diagrams, black-and-white scans and areas of flat colour.
- It is useless, and can make a file larger, on noisy data such as a photograph, where almost no two neighbouring values are equal.
Each row becomes counts and colours
行程编码
- 行程编码(run-length encoding,RLE)把一串重复的值替换成一个值加一个计数:不存八个相同的白像素,而存"8,白"。
- 它在有长串重复的数据上表现极佳,比如图标、示意图、黑白扫描件和大片纯色区域。
- 它在照片这类噪声数据上毫无用处,甚至会让文件更大,因为几乎没有相邻的两个值相同。

每一行变成计数和颜色
Run-length encoding works best on data that has: · 行程编码在有以下的数据上工作得最好:
RLE replaces a run of identical values with a count + value, so it shines on flat areas and is useless on noisy data. · RLE 用一个计数 + 值替换一段相同的值,所以它在平坦区域出色,在噪声数据上无用。
Worked example: encode a row with RLE
- A row of an 8-pixel black-and-white image reads: white white white black black white white white. Encode it with RLE.
- Three white, two black, three white, so
3W 2B 3W, or as pairs,(3, 0) (2, 1) (3, 0)if white is 0. - Eight values became three pairs. Now encode
W B W B W B W B. Eight runs of one:1W 1B 1W 1B 1W 1B 1W 1B, which stores more than the original. That is exactly why RLE is not used on photographs.
例题:用 RLE 编码一行
- 一幅 8 像素黑白图像的一行是:白白白黑黑白白白。用 RLE 编码它。
- 三白、两黑、三白,所以是
3W 2B 3W;若白是 0,写成数对就是(3, 0) (2, 1) (3, 0)。 - 八个值变成了三个数对。*现在编码
W B W B W B W B。*八段长度为一的行程:1W 1B 1W 1B 1W 1B 1W 1B,存的比原来还多。这正是 RLE 不用于照片的原因。
Run-length encoding (a lossless method) · 行程编码(一个无损方法)
Run-length encoding replaces a run of repeated values with one value plus a count. It is lossless — the original rebuilds exactly — but only shrinks data that has long runs. · 行程编码用一个值加一个计数替换一段重复的值。它是无损的——原始数据精确地重建——但只缩小有长行程的数据。
Encode the pixel row W W W B B W W W with run-length encoding, using the form 3W 2B 3W. · 用行程编码对像素行 W W W B B W W W 编码,采用 3W 2B 3W 的形式。
Three white, two black, three white. Eight values become three pairs, but an alternating row would become eight pairs and grow. · 三白、两黑、三白。八个值变成三个数对,但交替的一行会变成八个数对而变大。
Run-length encoding always makes a file smaller. · 行程编码总是让文件变小。
On data with no runs, such as a photograph or an alternating pattern, every run has length one and the encoding stores more than the original. · 在没有重复串的数据上——照片或交替图案——每段行程长度都是一,编码存的比原始数据还多。
Dictionary coding and Huffman coding
- Dictionary coding 字典编码, used by ZIP and PNG, builds a dictionary of repeated byte sequences and replaces each occurrence with a short index. It suits text and program code, where words and patterns recur.
- Huffman coding 霍夫曼编码 gives short codes to common symbols and long codes to rare ones, so the average code length falls. In "BANANA" the A is commonest and gets the shortest code.
- Both are lossless: the decoder rebuilds the original byte for byte.
字典编码与霍夫曼编码
- 字典编码(dictionary coding),ZIP 和 PNG 使用它,建立一个重复字节序列的字典,把每次出现替换成一个短索引。它适合文本和程序代码,那里词语和模式反复出现。
- 霍夫曼编码(Huffman coding)给常见符号短代码、给罕见符号长代码,于是平均码长下降。在 "BANANA" 里 A 最常见,得到最短的代码。
- 两者都是无损的:解码器逐字节重建原始数据。
Huffman coding reduces size by: · 霍夫曼编码这样减小大小:
Huffman assigns the shortest codes to the most frequent symbols, lowering the average code length. · 霍夫曼把最短的码分配给最频繁的符号,降低平均码长。
Match each lossless method to how it works. · 把每种无损方法与它的工作方式配对。
All three are lossless: the decoder rebuilds the original byte for byte. · 三者都是无损的:解码器逐字节重建原始数据。
Lossy methods
- Images (JPEG): fine detail and colour differences the eye barely notices are dropped.
- Sound (MP3, AAC): pitches we hear poorly are removed, along with quiet sounds masked by louder ones at the same moment.
- Video: spatial 空间 compression works within each frame, like JPEG, and temporal 时间 compression stores most frames as only the differences from the previous frame, since consecutive frames are nearly identical.
有损方法
- 图像(JPEG):丢掉眼睛几乎注意不到的细节和颜色差异。
- 声音(MP3、AAC):去掉我们听得不好的音高,以及同一时刻被更响的声音掩盖的轻声。
- 视频:空间(spatial)压缩在每一帧内部工作,像 JPEG;时间(temporal)压缩把大多数帧只存成与上一帧的差异,因为连续的帧几乎一模一样。
Temporal compression of video works by: · 视频的时间压缩这样工作:
Temporal compression stores how each frame differs from the one before, since most of the picture stays the same between frames. (Spatial compression handles within-frame detail.) · 时间压缩存储每一帧与前一帧有什么不同,因为大部分画面在帧之间保持相同。(空间压缩处理帧内的细节。)
Worked example: how each kind of file is compressed
- Text file: dictionary and Huffman coding turn repeated words and common characters into short codes. It must stay lossless, because one changed character changes the meaning.
- Bitmap image: RLE for runs of identical pixels in icons and diagrams; lossy JPEG for photographs; or reduce the colour depth or resolution, which is also lossy.
- Vector graphic: the drawing list is already small. Remove drawing objects that are not needed, store coordinates to fewer decimal places, or apply a lossless method such as ZIP to the file.
- Sound file: lossy MP3 or AAC removes what the ear cannot hear; lowering the sampling rate or resolution is also lossy; lossless formats keep every sample and shrink much less.
例题:各类文件怎样被压缩
- 文本文件:字典编码和霍夫曼编码把重复的词和常见字符变成短代码。它必须保持无损,因为一个字符变了含义就变了。
- 位图图像:图标和示意图中相同像素的行程用 RLE;照片用有损的 JPEG;或者降低颜色深度或分辨率,那也是有损的。
- 矢量图形:绘图列表本来就小。删掉不需要的绘图对象、坐标存更少的小数位,或对文件应用 ZIP 这样的无损方法。
- 声音文件:有损的 MP3 或 AAC 去掉耳朵听不到的;降低采样率或分辨率也是有损的;无损格式保留每个采样,缩得少得多。
How can a vector graphic file be made smaller? Select all · 所有 that apply. · 怎样让矢量图形文件更小?选出所有适用的。
A vector file is a drawing list, not pixels, so it has no colour depth. Colour depth belongs to bitmaps. · 矢量文件是绘图列表而不是像素,所以它没有颜色深度。颜色深度属于位图。
Why streaming must be lossy
- Raw HD video is gigabytes per minute, and a home connection carries a few megabits per second.
- Lossless compression on video achieves perhaps a factor of two, nowhere near enough, so the picture would keep stopping to buffer.
- Lossy compression achieves a factor of a hundred or more by discarding detail the viewer does not notice at normal speed. The reason names the bandwidth and the data rate, not just "it is smaller".
流媒体为什么必须有损
- 原始高清视频每分钟数 GB,而家庭宽带每秒只有几 Mb。
- 对视频做无损压缩大概只能达到两倍,远远不够,画面会不停停下来缓冲。
- 有损压缩通过丢弃观众在正常播放速度下注意不到的细节,达到一百倍甚至更高。理由要点出带宽和数据率,而不只是"它更小"。
Why does real-time video streaming use lossy compression? · 为什么实时视频流用有损压缩?
Raw HD video is gigabytes per minute; only lossy compression shrinks it enough to stream in real time without freezing. · 原始高清视频每分钟数 GB;只有有损压缩把它缩得足够小,能实时流式传输而不冻结。
Marks that slip away
- Lossless means the original is recovered exactly; lossy means it cannot be. "Lossy loses quality" misses the point that the data is gone for good.
- RLE can make a noisy file larger. Say where it works and where it does not.
- Video compression is spatial and temporal; temporal stores the differences between frames.
- A "justify" answer ties the method to the file's use: exactness for accounts and medical images, size for streaming and phone galleries.
容易丢掉的分
- 无损意味着原始数据被精确恢复;有损意味着无法恢复。"有损会损失质量"没说到关键:数据是永久没了。
- RLE 会让噪声文件更大。要说清它在哪里有效、在哪里无效。
- 视频压缩是空间和时间两种;时间压缩存的是帧之间的差异。
- "论证"的答案要把方法与文件的用途绑定:账目和医学影像要精确,流媒体和手机相册要小。
You've got it
- compression saves storage and bandwidth; lossless recovers the original exactly, lossy removes data permanently
- lossless methods: RLE (a value and a count, good on runs, bad on noise), dictionary coding (repeated sequences to short indices), Huffman coding (short codes for common symbols)
- lossy methods drop what the eye or ear misses; video adds spatial within a frame and temporal between frames
- justify from the use: exact data means lossless, limited bandwidth for streaming means lossy
你掌握了
- 压缩节省存储和带宽;无损精确恢复原始数据,有损永久移除数据
- 无损方法:RLE(一个值加一个计数,擅长长串、不擅噪声)、字典编码(重复序列变短索引)、霍夫曼编码(常见符号用短代码)
- 有损方法丢掉眼睛或耳朵漏掉的东西;视频再加帧内的空间压缩和帧间的时间压缩
- 从用途论证:需要精确数据就无损,流媒体带宽有限就有损