/* * LoadBitmap loads and a bitmap image contained in a package. */ func LoadBitmap(pkg Package, URI string) (image.Image, error) { r, err := pkg.GetReader(URI) if err != nil { return nil, err } defer r.Close() return bmp.Decode(r) }
func loadImage(data []byte) (image.Image, error) { switch { case string(data[:2]) == "BM": return bmp.Decode(bytes.NewReader(data)) case string(data[:len(pngHeader)]) == pngHeader: return png.Decode(bytes.NewReader(data)) default: return nil, errors.New("invalid source image format") } }
func Fuzz(data []byte) int { cfg, err := bmp.DecodeConfig(bytes.NewReader(data)) if err != nil { return 0 } if cfg.Width*cfg.Height > 1e6 { return 0 } if cfg.Width*cfg.Height == 0 { // Workaround for a know bug. return 0 } img, err := bmp.Decode(bytes.NewReader(data)) if err != nil { return 0 } var w bytes.Buffer err = bmp.Encode(&w, img) if err != nil { panic(err) } enc := w.Bytes() img1, err := bmp.Decode(&w) if err != nil { panic(err) } var w1 bytes.Buffer err = bmp.Encode(&w1, img1) if err != nil { panic(err) } enc1 := w1.Bytes() if !bytes.Equal(enc, enc1) { fmt.Printf("image1: [%v]%v\n", len(enc), hex.EncodeToString(enc)) fmt.Printf("image2: [%v]%v\n", len(enc1), hex.EncodeToString(enc1)) panic("not equal") } return 1 }
func loadSuzanneRGBA() (*image.RGBA, error) { f, err := os.Open("testdata/suzanne.bmp") if err != nil { return nil, err } defer f.Close() img, err := bmp.Decode(f) if err != nil { return nil, err } return img.(*image.RGBA), nil }
func Walkfunc(path string, info os.FileInfo, err error) error { if filepath.Ext(info.Name()) == ".bmp" { name := strings.TrimSuffix(info.Name(), ".bmp") dir := filepath.Dir(path) fmt.Println(filepath.Join(dir, name+".png")) f1, err := os.Open(path) img, err := bmp.Decode(f1) f2, err := os.Create(filepath.Join(dir, name+".png")) png.Encode(f2, img) return err } return nil }
"io" "regexp" "strings" "golang.org/x/image/bmp" ) type convertFunc func(io.Reader) (image.Image, error) var ( ExtensionExp = regexp.MustCompile(`.(\w+)$`) ) var convertFuncs = map[string]convertFunc{ "gif": func(b io.Reader) (image.Image, error) { return gif.Decode(b) }, "bmp": func(b io.Reader) (image.Image, error) { return bmp.Decode(b) }, } type Converter struct { } func (c *Converter) Convert(fileName string, buffer []byte) ([]byte, error) { matches := ExtensionExp.FindAllStringSubmatch(fileName, -1) if matches != nil { ext := strings.ToLower(matches[0][1]) converter := convertFuncs[ext] if converter != nil { in := bytes.NewReader(buffer) img, err := converter(in) if err != nil {