Spin out a std/lzw package

This commit is contained in:
Nigel Tao committed 2018-06-02 14:15:09 +10:00
1 parent 3e1cdd1a83
commit 6d510bcfcb
13 files changed
+3973 -565

No files matched your search

+3 -1
View File
@@ -14,6 +14,8 @@
packageid "gif "
use "std/lzw"
pub error "bad block"
pub error "bad extension label"
pub error "bad graphic control"
@@ -78,7 +80,7 @@ pub struct decoder?(
// palettes[0] and palettes[1] are the Global and Local Color Table.
palettes array[2] array[4 * 256] base.u8,
lzw lzw_decoder,
lzw lzw.decoder,
)
// TODO: should dst be an nptr instead of a ptr?
+16
View File
@@ -0,0 +1,16 @@
# LZW (Lempel Ziv Welch) Compression
LZW is a general purpose compression algorithm, not specific to GIF, PDF, TIFF
or even to graphics. In practice, there are two incompatible implementations,
LSB (Least Significant Bits) and MSB (Most Significant Bits) first.
The GIF format uses LSB first. The literal width can vary between 2 and 8 bits,
inclusive. There is no EarlyChange option.
The PDF and TIFF formats use MSB first. The literal width is fixed at 8 bits.
There is an EarlyChange option that PDF sometimes uses (see Table 3.7 in the
[PDF 1.4
spec](https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/pdf_reference_archives/PDFReference.pdf))
and TIFF always uses.
TODO: refactor this README.md file and std/gif's one.
@@ -12,23 +12,28 @@
// See the License for the specific language governing permissions and
// limitations under the License.
packageid "lzw "
// TODO: some mechanism for fixing the error code's value, such as a trailing
// "= 100", to maintain API stability.
pub error "LZW code is out of range"
pub error "LZW prefix chain is cyclical"
pub error "code is out of range"
pub error "prefix chain is cyclical"
pri struct lzw_decoder?(
pub struct decoder?(
literal_width base.u32[..8],
stack array[4096] base.u8,
suffixes array[4096] base.u8,
prefixes array[4096] base.u16[..4095],
)
pri func lzw_decoder.set_literal_width!(lw base.u32[2..8])() {
this.literal_width = in.lw
// TODO: u32 should be u32[2..8].
pub func decoder.set_literal_width!(lw base.u32)() {
if in.lw <= 8 {
this.literal_width = in.lw
}
}
pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
pub func decoder.decode?(dst base.io_writer, src base.io_reader)() {
// These variables don't change over the lifetime of this func.
var literal_width base.u32[2..8] = 8
if this.literal_width >= 2 {
@@ -98,7 +103,7 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
{
this.stack[s] = this.suffixes[c]
if s == 0 {
return error "LZW prefix chain is cyclical"
return error "prefix chain is cyclical"
}
s -= 1
c = this.prefixes[c] as base.u32
@@ -129,7 +134,7 @@ pri func lzw_decoder.decode?(dst base.io_writer, src base.io_reader)() {
}
} else {
return error "LZW code is out of range"
return error "code is out of range"
}
if save_code <= 4095 {