Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
pengwei committed Aug 9, 2017
1 parent efda16b commit bc8a404
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 17 deletions.
4 changes: 1 addition & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ language: go

matrix:
include:
- go: 1.7.x
- go: 1.8.x
- master

Expand All @@ -12,8 +11,7 @@ before_install:
- go get github.com/mattn/goveralls

script:
- go vet $(go list ./... | grep -v /vendor/)
- go test -v -race ./...
- go test -v ./...
- go test -coverprofile=image-type.coverprofile
- gover
- goveralls -coverprofile=gover.coverprofile -service=travis-ci
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

func main() {
file, _ := os.Open("../testdata/test1.jpg")
file, _ := os.Open("../testdata/test.jpg")

bytes := make([]byte, 256)
file.Read(bytes)
Expand Down
2 changes: 1 addition & 1 deletion examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func main() {
file, _ := os.Open("../testdata/test1.jpg")
file, _ := os.Open("../testdata/test.jpg")

bytes := make([]byte, 256)
file.Read(bytes)
Expand Down
38 changes: 28 additions & 10 deletions image.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package imageType

import "errors"
import (
"bufio"
"errors"
"io"
)

// ImageInfo ...
type ImageInfo struct {
Expand All @@ -10,6 +14,16 @@ type ImageInfo struct {
Height int
}

// ParseReader ...
func ParseReader(rd io.Reader) (img *ImageInfo, err error) {
br := bufio.NewReader(rd)
bytes, err := br.Peek(256)
if err != nil {
return
}
return Parse(bytes)
}

// Parse ...
func Parse(bytes []byte) (img *ImageInfo, err error) {
img = &ImageInfo{}
Expand Down Expand Up @@ -61,30 +75,34 @@ func parsePng(bytes []byte, img *ImageInfo) {
byteLen := len(bytes)
img.MimeType = "image/png"
img.Type = "png"
if byteLen > 15 {
img.Width = int(bytes[0])<<24 | int(bytes[1])<<16 | int(bytes[2])<<8 | int(bytes[3])
img.Height = int(bytes[4])<<24 | int(bytes[5])<<16 | int(bytes[6])<<8 | int(bytes[7])
if byteLen > 23 {
r := bytes[16:]
img.Width = int(r[0])<<24 | int(r[1])<<16 | int(r[2])<<8 | int(r[3])
img.Height = int(r[4])<<24 | int(r[5])<<16 | int(r[6])<<8 | int(r[7])
}
}
func parseGif(bytes []byte, img *ImageInfo) {
byteLen := len(bytes)
img.MimeType = "image/gif"
img.Type = "gif"
if byteLen > 5 {
img.Width = int(bytes[0]) + int(bytes[1])*256
img.Height = int(bytes[2]) + int(bytes[3])*256
r := bytes[6:]
img.Width = int(r[0]) + int(r[1])*256
img.Height = int(r[2]) + int(r[3])*256
}
}

func parseBmp(bytes []byte, img *ImageInfo) {
byteLen := len(bytes)
img.MimeType = "image/bmp"
img.Type = "bmp"
if byteLen > 17 {
img.Width = int(bytes[3])<<24 | int(bytes[2])<<16 | int(bytes[1])<<8 | int(bytes[0])
img.Height = int(bytes[7])<<24 | int(bytes[6])<<16 | int(bytes[5])<<8 | int(bytes[4])
if byteLen > 21 {
r := bytes[18:]
img.Width = int(r[3])<<24 | int(r[2])<<16 | int(r[1])<<8 | int(r[0])
img.Height = int(r[7])<<24 | int(r[6])<<16 | int(r[5])<<8 | int(r[4])
}
}
func parseWebp(bytes []byte, img *ImageInfo) {
func parseWebp(data []byte, img *ImageInfo) {
img.MimeType = "image/webp"
img.Type = "webp"
}
Expand Down
44 changes: 42 additions & 2 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (

func TestJPEG(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("testdata/test1.jpg")

file, _ := os.Open("testdata/test.jpg")
bytes := make([]byte, 256)
file.Read(bytes)
res, err := Parse(bytes)
Expand All @@ -21,3 +20,44 @@ func TestJPEG(t *testing.T) {
assert.Equal(600, res.Height)
}
}
func TestPng(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("testdata/test.png")
bytes := make([]byte, 256)
file.Read(bytes)
res, err := Parse(bytes)
if assert.Nil(err) {
assert.Equal("png", res.Type)
assert.Equal("image/png", res.MimeType)
assert.Equal(612, res.Width)
assert.Equal(357, res.Height)
}
}

func TestGif(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("testdata/test.gif")
bytes := make([]byte, 256)
file.Read(bytes)
res, err := Parse(bytes)
if assert.Nil(err) {
assert.Equal("gif", res.Type)
assert.Equal("image/gif", res.MimeType)
assert.Equal(500, res.Width)
assert.Equal(500, res.Height)
}
}

func TestBmp(t *testing.T) {
assert := assert.New(t)
file, _ := os.Open("testdata/test.bmp")
bytes := make([]byte, 256)
file.Read(bytes)
res, err := Parse(bytes)
if assert.Nil(err) {
assert.Equal("bmp", res.Type)
assert.Equal("image/bmp", res.MimeType)
assert.Equal(622, res.Width)
assert.Equal(630, res.Height)
}
}
Binary file added testdata/test.bmp
Binary file not shown.
Binary file added testdata/test.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file added testdata/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bc8a404

Please sign in to comment.