forked from chai2010/tiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
146 lines (129 loc) · 3.57 KB
/
compress.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2015 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tiff
import (
"bufio"
"compress/zlib"
"fmt"
"io"
"io/ioutil"
"github.com/chai2010/tiff/internal/fax"
)
func (p TagValue_CompressionType) Decode(r io.Reader, width, height int) (data []byte, err error) {
switch p {
case TagValue_CompressionType_None, TagValue_CompressionType_Nil:
return p.decode_None(r)
case TagValue_CompressionType_CCITT:
return p.decode_CCITT(r)
case TagValue_CompressionType_G3:
return p.decode_G3(r, width, height)
case TagValue_CompressionType_G4:
return p.decode_G4(r, width, height)
case TagValue_CompressionType_LZW:
return p.decode_LZW(r)
case TagValue_CompressionType_JPEGOld:
return p.decode_JPEGOld(r)
case TagValue_CompressionType_JPEG:
return p.decode_JPEG(r)
case TagValue_CompressionType_Deflate:
return p.decode_Deflate(r)
case TagValue_CompressionType_PackBits:
return p.decode_PackBits(r)
case TagValue_CompressionType_DeflateOld:
return p.decode_DeflateOld(r)
}
err = fmt.Errorf("tiff: unsupport %v compression type", int(p))
return
}
func (p TagValue_CompressionType) decode_None(r io.Reader) (data []byte, err error) {
data, err = ioutil.ReadAll(r)
return
}
func (p TagValue_CompressionType) decode_CCITT(r io.Reader) (data []byte, err error) {
err = fmt.Errorf("tiff: unsupport %v compression type", "CCITT")
return
}
func (p TagValue_CompressionType) decode_G3(r io.Reader, width, height int) (data []byte, err error) {
return p.decode_G4(r, width, height)
}
func (p TagValue_CompressionType) decode_G4(r io.Reader, width, height int) (data []byte, err error) {
br, ok := r.(io.ByteReader)
if !ok {
br = bufio.NewReader(r)
}
return fax.DecodeG4Pixels(br, width, height)
}
func (p TagValue_CompressionType) decode_LZW(r io.Reader) (data []byte, err error) {
lzwReader := newLzwReader(r, lzwMSB, 8)
data, err = ioutil.ReadAll(lzwReader)
lzwReader.Close()
return
}
func (p TagValue_CompressionType) decode_JPEGOld(r io.Reader) (data []byte, err error) {
println("decode_JPEGOld")
err = fmt.Errorf("tiff: unsupport %v compression type", "JPEGOld")
return
}
func (p TagValue_CompressionType) decode_JPEG(r io.Reader) (data []byte, err error) {
err = fmt.Errorf("tiff: unsupport %v compression type", "JPEG")
return
}
func (p TagValue_CompressionType) decode_Deflate(r io.Reader) (data []byte, err error) {
zlibReader, err := zlib.NewReader(r)
if err != nil {
return nil, err
}
data, err = ioutil.ReadAll(zlibReader)
zlibReader.Close()
return
}
func (p TagValue_CompressionType) decode_DeflateOld(r io.Reader) (data []byte, err error) {
zlibReader, err := zlib.NewReader(r)
if err != nil {
return nil, err
}
data, err = ioutil.ReadAll(zlibReader)
zlibReader.Close()
return
}
func (p TagValue_CompressionType) decode_PackBits(r io.Reader) (data []byte, err error) {
type byteReader interface {
io.Reader
io.ByteReader
}
buf := make([]byte, 128)
dst := make([]byte, 0, 1024)
br, ok := r.(byteReader)
if !ok {
br = bufio.NewReader(r)
}
for {
b, err := br.ReadByte()
if err != nil {
if err == io.EOF {
return dst, nil
}
return nil, err
}
code := int(int8(b))
switch {
case code >= 0:
n, err := io.ReadFull(br, buf[:code+1])
if err != nil {
return nil, err
}
dst = append(dst, buf[:n]...)
case code == -128:
// No-op.
default:
if b, err = br.ReadByte(); err != nil {
return nil, err
}
for j := 0; j < 1-code; j++ {
buf[j] = b
}
dst = append(dst, buf[:1-code]...)
}
}
}