本文整理匯總了Golang中image/gif.Decode函數的典型用法代碼示例。如果您正苦於以下問題:Golang Decode函數的具體用法?Golang Decode怎麽用?Golang Decode使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Decode函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Fuzz
func Fuzz(data []byte) int {
cfg, err := gif.DecodeConfig(bytes.NewReader(data))
if err != nil {
return 0
}
if cfg.Width*cfg.Height > 1e6 {
return 0
}
img, err := gif.Decode(bytes.NewReader(data))
if err != nil {
return 0
}
for c := 1; c <= 256; c += 21 {
var w bytes.Buffer
err = gif.Encode(&w, img, &gif.Options{NumColors: c})
if err != nil {
panic(err)
}
img1, err := gif.Decode(&w)
if err != nil {
panic(err)
}
b0 := img.Bounds()
b1 := img1.Bounds()
if b0.Max.X-b0.Min.X != b1.Max.X-b1.Min.X || b0.Max.Y-b0.Min.Y != b1.Max.Y-b1.Min.Y {
fmt.Printf("img0: %#v\n", img.Bounds())
fmt.Printf("img1: %#v\n", img1.Bounds())
panic("bounds changed")
}
}
return 1
}
示例2: writeToJP
func writeToJP() {
imgRect := image.Rect(0, 0, gridSize*10, gridSize*10)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for x := 0; x < gridSize; x++ {
for y := 0; y < gridSize; y++ {
fill := &image.Uniform{color.White}
if Env[x][y] == -1 {
fill = &image.Uniform{color.Black}
} else if Env[x][y] > 1 {
c := color.Gray{uint8(Env[x][y] * 20)}
fill = &image.Uniform{c}
}
draw.Draw(img, image.Rect((x-1)*10, (y-1)*10, x*10, y*10), fill, image.ZP, draw.Src)
}
}
buf := bytes.Buffer{}
// ok, write out the data into the new JPEG file
err := gif.Encode(&buf, img, nil)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
tmpimg, err := gif.Decode(&buf)
if err != nil {
log.Printf("Skipping frame due to weird error reading the temporary gif :%s", err)
}
frames = append(frames, tmpimg.(*image.Paletted))
}
示例3: init
func init() {
var err error
Gordon, err = gif.Decode(bytes.NewReader(GordonGif))
if err != nil {
panic(err)
}
}
示例4: TestNewRevisitImageWithGIF
func TestNewRevisitImageWithGIF(t *testing.T) {
gifMsg, err := NewRevisitMsgFromFiles("./fixtures/bob.gif")
if err != nil {
t.Fatal(err)
}
ri, err := NewRevisitImageFromMsg(gifMsg)
if err != nil {
t.Fatal(err)
}
if len(ri.Rgbas) != 4 {
t.Errorf("ri.Rgbas length should be 4, is %d", len(ri.Rgbas))
}
if len(ri.Delay) != 4 {
t.Errorf("ri.Delay length should be 4, is %d", len(ri.Delay))
}
if ri.LoopCount != 0 {
t.Errorf("LoopCount should be 0, is %d", ri.LoopCount)
}
ri.Transform(mockTransform)
m, err := ri.RevisitMsg()
if err != nil {
t.Error(err)
}
_, err = gif.Decode(m.ImageByteReader())
if err != nil {
t.Error(err)
}
}
示例5: Test_fetchGifCompareWithPng_Fail
func Test_fetchGifCompareWithPng_Fail(t *testing.T) {
ts := testutils.TestImgServer("gif")
defer ts.Close()
data, err := fetch(ts.URL)
if err != nil {
t.FailNow()
}
imgServer, err := gif.Decode(bytes.NewReader(data))
if err != nil {
t.FailNow()
}
file, _ := os.Open("testdata/test.png")
defer file.Close()
imgDisk, _, _ := image.Decode(file)
if reflect.DeepEqual(imgDisk, imgServer) {
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d:\n\ncall fetch(%#v)\n\texp: %#v\n\n\tgot: %#v\n\n",
filepath.Base(file), line, ts.URL, imgDisk, imgServer)
t.FailNow()
}
}
示例6: decode_img
func decode_img(data []byte, content_type string) (*image.Image, error) {
var (
img image.Image
err error
)
buffer := bytes.NewBuffer(data)
switch content_type {
case "image/jpeg":
img, err = jpeg.Decode(buffer)
case "image/gif":
img, err = gif.Decode(buffer)
case "image/png":
img, err = png.Decode(buffer)
default:
err = errors.New("Unknown content type " + content_type)
}
if err != nil {
log.Println(err)
return nil, err
}
return &img, nil
}
示例7: GetImage
// GetImage downloads a Media object and returns an image.Image. The
// documentation isn't great on what happens - as of October 2016, we make a
// request to the Twilio API, then to media.twiliocdn.com, then to a S3 URL. We
// then download that image and decode it based on the provided content-type.
func (m *MediaService) GetImage(ctx context.Context, messageSid string, sid string) (image.Image, error) {
u, err := m.GetURL(ctx, messageSid, sid)
if err != nil {
return nil, err
}
if u.Scheme == "http" {
return nil, fmt.Errorf("Attempted to download image over insecure URL: %s", u.String())
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
req = withContext(req, ctx)
req.Header.Set("User-Agent", userAgent)
resp, err := MediaClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
// https://www.twilio.com/docs/api/rest/accepted-mime-types#supported
ctype := resp.Header.Get("Content-Type")
switch ctype {
case "image/jpeg":
return jpeg.Decode(resp.Body)
case "image/gif":
return gif.Decode(resp.Body)
case "image/png":
return png.Decode(resp.Body)
default:
io.Copy(ioutil.Discard, resp.Body)
return nil, fmt.Errorf("twilio: Unknown content-type %s", ctype)
}
}
示例8: NewMazeMatrix
// NewMazeMatrix creates a image matrix base on fetched body
func NewMazeMatrix(r io.Reader, m *MazeImage) (*MazeMatrix, error) {
mazeMatrix := &MazeMatrix{i: m}
img, err := gif.Decode(r)
if err != nil {
return nil, err
}
rect := img.Bounds()
rgba := image.NewRGBA(rect)
draw.Draw(rgba, rect, img, rect.Min, draw.Src)
reader := bytes.NewReader(rgba.Pix)
matrix := make([][]MatrixToken, rect.Max.Y)
for y := 0; y < rect.Max.Y; y++ {
for x := 0; x < rect.Max.X; x++ {
if len(matrix[y]) == 0 {
matrix[y] = make([]MatrixToken, rect.Max.X)
}
part := make([]byte, 4)
reader.Read(part)
if y == 0 || x == 0 {
matrix[y][x] = BORDER
} else {
if mazeMatrix.isWall(part[0], part[1], part[2]) {
matrix[y][x] = WALL
} else {
matrix[y][x] = PATH
}
}
}
}
mazeMatrix.m = matrix
return mazeMatrix, nil
}
示例9: getImage
func getImage(rc io.Reader) (img image.Image, ft string, err error) {
// Tries to identify an Image as type JPEG, PNG or Gif.
// I have seen this failing so I don't know how long this is going to last.
img, ft, err = image.Decode(rc)
if err == nil {
return
}
img, err = png.Decode(rc)
if err == nil {
ft = "png"
return
}
img, err = jpeg.Decode(rc)
if err == nil {
ft = "jpeg"
return
}
img, err = gif.Decode(rc)
if err == nil {
ft = "gif"
}
return
}
示例10: decodeGif
func decodeGif(buf bytes.Buffer) (*Img, error) {
image := new(Img)
image.Original = buf
var newBuf bytes.Buffer
io.Copy(&newBuf, &buf)
g, err := gif.Decode(&newBuf)
if err != nil {
log.Println(err.Error())
return nil, err
}
var pngImage bytes.Buffer
err = png.Encode(&pngImage, g)
if err != nil {
log.Println(err.Error())
return nil, err
}
image.Snapshot = pngImage
return image, nil
}
示例11: TestWriter
func TestWriter(t *testing.T) {
for _, tc := range testCase {
m0, err := readImg(tc.filename)
if err != nil {
t.Error(tc.filename, err)
}
var buf bytes.Buffer
err = Encode(&buf, m0, nil)
if err != nil {
t.Error(tc.filename, err)
}
m1, err := gif.Decode(&buf)
if err != nil {
t.Error(tc.filename, err)
}
if m0.Bounds() != m1.Bounds() {
t.Errorf("%s, bounds differ: %v and %v", tc.filename, m0.Bounds(), m1.Bounds())
}
// Compare the average delta to the tolerance level.
avgDelta := averageDelta(m0, m1)
if avgDelta > tc.tolerance {
t.Errorf("%s: average delta is too high. expected: %d, got %d", tc.filename, tc.tolerance, avgDelta)
continue
}
}
}
示例12: init
func init() {
gordonGifData := gordon_gif()
var err error
Gordon, err = gif.Decode(bytes.NewReader(gordonGifData))
if err != nil {
panic(err)
}
}
示例13: ResizeGif
func ResizeGif(wb *bytes.Buffer, r io.Reader, g *gift.GIFT, width int, height int) error {
src, err := gif.Decode(r)
bounds := src.Bounds()
dst := src
if width < bounds.Dx() || height < bounds.Dy() {
dst = ApplyFilter(src, g)
}
err = gif.Encode(wb, dst, nil)
return err
}
示例14: Decode
func (ih *ImageHelper) Decode(file multipart.File, fileType string) (image.Image, error) {
if fileType == "png" {
return png.Decode(file)
} else if fileType == "jpeg" || fileType == "jpg" {
return jpeg.Decode(file)
} else if fileType == "gif" {
return gif.Decode(file)
}
log.Println("Unsupported media for decoding ", fileType)
return nil, errors.New("Unsupported media type")
}
示例15: imageDecode
func imageDecode(fileext string, file io.Reader) (image.Image, error) {
switch fileext {
case "jpg", "jpeg":
return jpeg.Decode(file)
case "png":
return png.Decode(file)
case "gif":
return gif.Decode(file)
default:
return nil, nil
}
}