-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdecoder.go
More file actions
139 lines (115 loc) · 4.16 KB
/
decoder.go
File metadata and controls
139 lines (115 loc) · 4.16 KB
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
// The MIT License (MIT)
//
// Copyright (c) 2019 Amangeldy Kadyl
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package decoder
/*
#cgo linux LDFLAGS: -lwebp
#cgo darwin pkg-config: libwebp
#include <stdlib.h>
#include <webp/decode.h>
*/
import "C"
import (
"bytes"
"errors"
"fmt"
"image"
"io"
"unsafe"
"github.com/kolesa-team/go-webp/utils"
)
// Decoder stores information to decode picture
type Decoder struct {
data []byte
options *Options
config *C.WebPDecoderConfig
dPtr *C.uint8_t
sPtr C.size_t
}
func NewDecoder(r io.Reader, options *Options) (d *Decoder, err error) {
if options == nil {
options = &Options{}
}
if options.ImageFactory == nil {
options.ImageFactory = &DefaultImageFactory{}
}
buf := bytes.NewBuffer(options.Buffer)
if _, err = io.Copy(buf, r); err != nil {
return nil, err
}
if len(buf.Bytes()) == 0 {
return nil, errors.New("data is empty")
}
d = &Decoder{data: buf.Bytes(), options: options}
if d.config, err = d.options.GetConfig(); err != nil {
return nil, err
}
d.dPtr = (*C.uint8_t)(&d.data[0])
d.sPtr = (C.size_t)(len(d.data))
// получаем WebPBitstreamFeatures
if status := d.parseFeatures(d.dPtr, d.sPtr); status != utils.Vp8StatusOk {
return nil, errors.New(fmt.Sprintf("cannot fetch features: %s", status.String()))
}
return
}
// Decode picture from reader
func (d *Decoder) Decode() (image.Image, error) {
// вписываем размеры итоговой картинки
d.config.output.width, d.config.output.height = d.getOutputDimensions()
// указываем что декодируем в RGBA
d.config.output.colorspace = C.MODE_RGBA
d.config.output.is_external_memory = 1
img := d.options.ImageFactory.Get(int(d.config.output.width), int(d.config.output.height))
buff := (*C.WebPRGBABuffer)(unsafe.Pointer(&d.config.output.u[0]))
buff.stride = C.int(img.Stride)
buff.rgba = (*C.uint8_t)(&img.Pix[0])
buff.size = (C.size_t)(len(img.Pix))
if status := utils.VP8StatusCode(C.WebPDecode(d.dPtr, d.sPtr, d.config)); status != utils.Vp8StatusOk {
return nil, errors.New(fmt.Sprintf("cannot decode picture: %s", status.String()))
}
return img, nil
}
// GetFeatures return information about picture: width, height ...
func (d *Decoder) GetFeatures() utils.BitstreamFeatures {
return utils.BitstreamFeatures{
Width: int(d.config.input.width),
Height: int(d.config.input.height),
HasAlpha: int(d.config.input.has_alpha) == 1,
HasAnimation: int(d.config.input.has_animation) == 1,
Format: utils.FormatType(d.config.input.format),
}
}
// parse features from picture
func (d *Decoder) parseFeatures(dataPtr *C.uint8_t, sizePtr C.size_t) utils.VP8StatusCode {
return utils.VP8StatusCode(C.WebPGetFeatures(dataPtr, sizePtr, &d.config.input))
}
// return dimensions of result image
func (d *Decoder) getOutputDimensions() (width, height C.int) {
width = d.config.input.width
height = d.config.input.height
if d.config.options.use_scaling > 0 {
width = d.config.options.scaled_width
height = d.config.options.scaled_height
} else if d.config.options.use_cropping > 0 {
width = d.config.options.crop_width
height = d.config.options.crop_height
}
return
}