-
Notifications
You must be signed in to change notification settings - Fork 1
/
thumbnail.go
50 lines (41 loc) · 1.14 KB
/
thumbnail.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
package main
import (
"image"
"image/png"
"log"
"os"
)
type Thumbnail struct {
width int
height int
bitspercomponent int
bitsperpixel int
bytesperrow int
bitmapdata_location int64
bitmapdata_length int64
data []byte
}
func NewThumbnail(width, height, bitspercomponent, bitsperpixel, bytesperrow int, bitmapdata_location, bitmapdata_length int64, f *os.File) *Thumbnail {
thumbnail := &Thumbnail{width, height, bitspercomponent, bitsperpixel, bytesperrow, bitmapdata_location, bitmapdata_length, nil}
thumbnail.fetchData(f)
return thumbnail
}
func (thumb *Thumbnail) fetchData(f *os.File) {
buf := make([]byte, thumb.bitmapdata_length)
_, err := f.ReadAt(buf, thumb.bitmapdata_location)
if err != nil {
log.Print(err)
return
}
thumb.data = buf
}
func (thumb *Thumbnail) CreateImage(output string) (err error) {
img := &image.RGBA{Pix: thumb.data, Stride: thumb.bytesperrow, Rect: image.Rect(0, 0, thumb.width, thumb.height)}
out, err := os.OpenFile(output, os.O_WRONLY|os.O_CREATE, 0600)
defer out.Close()
if err != nil {
return
}
png.Encode(out, img)
return
}