summaryrefslogtreecommitdiff
path: root/vendor/github.com/dsoprea/go-png-image-structure/chunk_decoder.go
blob: 1358c3df29c1a8867ff91ff49bb52626652e304d (plain)
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
package pngstructure

import (
    "fmt"
    "bytes"

    "encoding/binary"

    "github.com/dsoprea/go-logging"
)

type ChunkDecoder struct {

}

func NewChunkDecoder() *ChunkDecoder {
    return new(ChunkDecoder)
}

func (cd *ChunkDecoder) Decode(c *Chunk) (decoded interface{}, err error) {
    defer func() {
        if state := recover(); state != nil {
            err := log.Wrap(state.(error))
            log.Panic(err)
        }
    }()

    switch c.Type {
        case "IHDR":
            ihdr, err := cd.decodeIHDR(c)
            log.PanicIf(err)

            return ihdr, nil
    }

    // We don't decode this particular type.
    return nil, nil
}


type ChunkIHDR struct {
    Width uint32
    Height uint32
    BitDepth uint8
    ColorType uint8
    CompressionMethod uint8
    FilterMethod uint8
    InterlaceMethod uint8
}

func (ihdr *ChunkIHDR) String() string {
    return fmt.Sprintf("IHDR<WIDTH=(%d) HEIGHT=(%d) DEPTH=(%d) COLOR-TYPE=(%d) COMP-METHOD=(%d) FILTER-METHOD=(%d) INTRLC-METHOD=(%d)>", ihdr.Width, ihdr.Height, ihdr.BitDepth, ihdr.ColorType, ihdr.CompressionMethod, ihdr.FilterMethod, ihdr.InterlaceMethod)
}

func (cd *ChunkDecoder) decodeIHDR(c *Chunk) (ihdr *ChunkIHDR, err error) {
    defer func() {
        if state := recover(); state != nil {
            err := log.Wrap(state.(error))
            log.Panic(err)
        }
    }()

    b := bytes.NewBuffer(c.Data)

    ihdr = new(ChunkIHDR)

    err = binary.Read(b, binary.BigEndian, &ihdr.Width)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.Height)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.BitDepth)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.ColorType)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.CompressionMethod)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.FilterMethod)
    log.PanicIf(err)

    err = binary.Read(b, binary.BigEndian, &ihdr.InterlaceMethod)
    log.PanicIf(err)

    return ihdr, nil
}