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 }
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)) }
func init() { var err error Gordon, err = gif.Decode(bytes.NewReader(GordonGif)) if err != nil { panic(err) } }
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) } }
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() } }
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 }
// 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) } }
// 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 }
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 }
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 }
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 } } }
func init() { gordonGifData := gordon_gif() var err error Gordon, err = gif.Decode(bytes.NewReader(gordonGifData)) if err != nil { panic(err) } }
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 }
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") }
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 } }
func loadGIFromFolder(name string) image.Image { f, e := os.Open(name) if e != nil { log.Println("loadGIFromFolder Failed to Open", name, e) return nil } img, _ := gif.Decode(f) f.Close() return img }
// TODO Could switch to image.Decode here... func decodeImage(reader io.Reader, filetype string) (img image.Image, err error) { switch filetype { case JPEG, JPG: img, err = jpeg.Decode(reader) case PNG: img, err = png.Decode(reader) case GIF: img, err = gif.Decode(reader) default: img, err = nil, errors.New("Cannot decode unknown image file type: "+filetype) } return }
func getDecodedImage(file io.Reader, format string) (img image.Image, err error) { switch format { case JPG: img, err = jpeg.Decode(file) case GIF: img, err = gif.Decode(file) case PNG: img, err = png.Decode(file) default: img = nil err = &otherImageError{"other image format"} } return }
func GetImageFromUrl(url string) image.Image { log.Println(url) response, err := http.Get(url) if err != nil { log.Println(err) } defer response.Body.Close() var img image.Image switch response.Header.Get("Content-Type") { case "image/gif": img, _ = gif.Decode(response.Body) } return img }
func decodeGIF(f multipart.File) (*Image, error) { var image Image ic, err := gif.DecodeConfig(f) _, err = f.Seek(0, 0) i, err := gif.Decode(f) if err != nil { return nil, fmt.Errorf("Could not decode GIF file.") } image.Config = ic image.Image = i return &image, nil }
//parse and concenate all gif file into an animated movie func (r *Run) CreateMovieFromGif() { // open the current working directory cwd, error := os.Open(".") if error != nil { panic("not able to open current working directory") } // get files with their names names, err := cwd.Readdirnames(0) if err != nil { panic("cannot read names in current working directory") } // parse the list of names and pick the ones that match the var files []string for _, dirname := range names { if strings.Contains(dirname, r.country) && strings.Contains(dirname, "gif") { fmt.Printf("dirname %s\n", dirname) files = append(files, dirname) } } // load static image and construct outGif outGif := &gif.GIF{} for _, name := range files { f, errOpen := os.Open(name) if errOpen != nil { panic(fmt.Sprintf("cannot read file %s", name)) } inGif, _ := gif.Decode(f) f.Close() outGif.Image = append(outGif.Image, inGif.(*image.Paletted)) outGif.Delay = append(outGif.Delay, 100) } // save to out.gif f, _ := os.OpenFile("hti.gif", os.O_WRONLY|os.O_CREATE, 0600) defer f.Close() gif.EncodeAll(f, outGif) }
func DecodeImage(fileName string) (img image.Image, err error) { file, err := os.Open(fileName) if err != nil { fmt.Println(err) return } suffixName := strings.ToLower(fileName[strings.LastIndex(fileName, ".")+1:]) switch suffixName { case "jpg": img, err = jpeg.Decode(file) case "png": img, err = png.Decode(file) case "gif": img, err = gif.Decode(file) default: err = errors.New("Not correct image format :" + suffixName) } return }
func ResizeGif(pic string) { // open the Picture file, err := os.Open(pic) utility.ShitAppend(err) img, err := gif.Decode(file) utility.ShitAppend(err) file.Close() // Resize the Picture Data m := resize.Resize(565, 363, img, resize.Lanczos3) out, err := os.Create(pic) utility.ShitAppend(err) defer out.Close() // OverWrite the None Resizing Picture gif.Encode(out, m, nil) }
func GetImg(url string) (img image.Image, err error) { resp, err := http.Get(url) if err != nil { fmt.Println("GetImgError:", err) return img, err } else { //length, _ := strconv.ParseInt(resp.Header.Get("Content-Length"), 10, 0) content_type := resp.Header.Get("Content-Type") switch strings.TrimPrefix(content_type, "image/") { case "png": img, _ = png.Decode(resp.Body) case "jpeg": img, _ = jpeg.Decode(resp.Body) case "gif": img, _ = gif.Decode(resp.Body) } } return img, nil }
func flipImageRes(res *http.Response) *http.Response { switch res.Header["Content-Type"][0] { case "image/jpeg": m, err := jpeg.Decode(res.Body) if err != nil { log.Println("image error:", err) return res } buf := imgBuffer{bytes.NewBuffer([]byte{})} n := flipImage(m) jpeg.Encode(buf, n, nil) res.Header.Del("Content-Length") res.Body = buf case "image/gif": m, err := gif.Decode(res.Body) if err != nil { log.Println("image error:", err) return res } buf := imgBuffer{bytes.NewBuffer([]byte{})} n := flipImage(m) png.Encode(buf, n) res.Header.Del("Content-Length") res.Header.Del("Content-Type") res.Header.Add("Content-Type", "image/png") res.Body = buf case "image/png": m, err := png.Decode(res.Body) if err != nil { log.Println("image error:", err) return res } buf := imgBuffer{bytes.NewBuffer([]byte{})} n := flipImage(m) png.Encode(buf, n) res.Header.Del("Content-Length") res.Header.Del("Content-Type") res.Header.Add("Content-Type", "image/png") res.Body = buf } return res }
func (r *Resize) decodeData(reader bytes.Buffer, ext string) *image.Image { var err error var img image.Image // decode reader into image.Image switch { case ext == "jpg": img, err = jpeg.Decode(&reader) case ext == "png": img, err = png.Decode(&reader) case ext == "gif": img, err = gif.Decode(&reader) } if err != nil { log.Fatal(err) return nil } return &img }
func LoadImage(filename string) image.Image { f, err := os.Open("images/" + filename) tools.ErrorCheck(err) var img image.Image = nil lowerFilename := strings.ToLower(filename) switch { case strings.HasSuffix(lowerFilename, ".jpg"): img, err = jpeg.Decode(f) case strings.HasSuffix(lowerFilename, ".png"): img, err = png.Decode(f) case strings.HasSuffix(lowerFilename, ".gif"): img, err = gif.Decode(f) } tools.ErrorCheck(err) return img }
func decodeImage(filePath string) (img image.Image, err error) { reader, err := os.Open(filePath) if err != nil { return nil, err } defer reader.Close() last3Strings := strings.ToLower(filePath[len(filePath)-3:]) last4Strings := strings.ToLower(filePath[len(filePath)-4:]) if last3Strings == "jpg" || last4Strings == "jpeg" { img, err = jpeg.Decode(reader) } else if last3Strings == "gif" { img, err = gif.Decode(reader) } else if last3Strings == "png" { img, err = png.Decode(reader) } else { img = nil err = errors.New("unknown format") } return }
// Gets an image from a GIF, JPEG, or PNG file. func readImage(filename string) image.Image { file, err := os.Open(filename) if err != nil { die(err) } defer file.Close() // Attempt to decode the file in different formats if image, err := png.Decode(file); err == nil { return image } file.Seek(0, 0) if image, err := gif.Decode(file); err == nil { return image } file.Seek(0, 0) if image, err := jpeg.Decode(file); err == nil { return image } die(errors.New("unsupported file type: " + filename)) return nil // unreachable }
func loadImgSpecFormat(file string, ft string) (image.Image, error) { file_origin, err := os.Open(file) if err != nil { return nil, Newcuserr(10000) } defer file_origin.Close() var img0 image.Image switch ft { case "gif": img0, err = gif.Decode(file_origin) case "png": img0, err = png.Decode(file_origin) case "webp": img0, err = webp.Decode(file_origin) default: img0, err = jpeg.Decode(file_origin) } if err != nil { return nil, Newcuserr(10001) } return img0, nil }