func thumbnailHandler() http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { filename, err := url.QueryUnescape(r.URL.String()) checkErr(err) fmt.Println("handling request for", filename) file, err := os.Open(filename) checkErr(err) defer file.Close() img, filetype, err := image.Decode(file) checkErr(err) var newImage image.Image if thumbnailSize < img.Bounds().Dx() || thumbnailSize < img.Bounds().Dy() { if img.Bounds().Dx() > img.Bounds().Dy() { newImage = resize.Resize(uint(thumbnailSize), 0, img, resize.Lanczos3) } else { newImage = resize.Resize(0, uint(thumbnailSize), img, resize.Lanczos3) } } else { newImage = img } switch filetype { case "png": err = png.Encode(w, newImage) case "gif": err = gif.Encode(w, newImage, nil) case "jpeg", "jpg": err = jpeg.Encode(w, newImage, nil) case "bmp": err = bmp.Encode(w, newImage) case "tiff": err = tiff.Encode(w, newImage, nil) default: // not sure how you got here but what are we going to do with you? fmt.Println("Unknown image type: ", filetype) io.Copy(w, file) } checkErr(err) fmt.Println("done handling request") }) }
func writeImage(dst string, mf string, m image.Image) { var err error w := os.Stdout if dst != "-" { w, err = os.Create(dst) if err != nil { log.Fatal(err) } } if ext := filepath.Ext(dst); mf == "" && ext != "" { mf = ext[1:] } switch mf { default: err = png.Encode(w, m) case "jpg", "jpeg": err = jpeg.Encode(w, m, nil) case "gif": err = gif.Encode(w, m, nil) case "tif", "tiff": err = tiff.Encode(w, m, nil) case "bmp": err = bmp.Encode(w, m) } if err != nil { log.Fatal(err) } }
func encode(w io.Writer, img image.Image, format imaging.Format, quality int) error { var err error switch format { case imaging.JPEG: var rgba *image.RGBA if nrgba, ok := img.(*image.NRGBA); ok { if nrgba.Opaque() { rgba = &image.RGBA{ Pix: nrgba.Pix, Stride: nrgba.Stride, Rect: nrgba.Rect, } } } if rgba != nil { err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: quality}) } else { err = jpeg.Encode(w, img, &jpeg.Options{Quality: quality}) } case imaging.PNG: err = png.Encode(w, img) case imaging.GIF: err = gif.Encode(w, img, &gif.Options{NumColors: 256}) case imaging.TIFF: err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true}) case imaging.BMP: err = bmp.Encode(w, img) default: err = imaging.ErrUnsupportedFormat } return err }
// WriteTo implements the io.WriterTo interface, writing a tiff image. func (c TiffCanvas) WriteTo(w io.Writer) (int64, error) { wc := writerCounter{Writer: w} b := bufio.NewWriter(&wc) if err := tiff.Encode(b, c.img, nil); err != nil { return wc.n, err } err := b.Flush() return wc.n, err }
// WriteStdout writes an Image to standard output as a PNG file. func WriteStdout(img image.Image, data *exif.Exif) { switch Output { case JPEG: // Create a temporary file for exiftool to use tmp, _ := ioutil.TempFile("", "img-utils-exif-") path := tmp.Name() // Encode the jpeg to this temp file err := jpeg.Encode(tmp, img, nil) if err != nil { log.Fatal(err) return } // Write the exif data to the temp file err = data.Write(path) if err != nil { // // This will generally return an error, and it still works, so can // // probably be ignored. // log.Println(err) } // Reopen the temp file. Yes, really. This is due to the fact that, even if // I seek to 0, problems still occur. So this works, I'm leaving it. f, err := os.Open(path) if err != nil { log.Fatal(err) return } io.Copy(os.Stdout, f) // Make sure the temp file is deleted after defer func() { os.Remove(path) }() case PNG: err := png.Encode(os.Stdout, img) if err != nil { log.Fatal(err) return } case TIFF: err := tiff.Encode(os.Stdout, img, nil) if err != nil { log.Fatal(err) return } } }
func Fuzz(data []byte) int { cfg, err := tiff.DecodeConfig(bytes.NewReader(data)) if err != nil { return 0 } if cfg.Width*cfg.Height > 1e6 { return 0 } img, err := tiff.Decode(bytes.NewReader(data)) if err != nil { return 0 } var w bytes.Buffer err = tiff.Encode(&w, img, nil) if err != nil { panic(err) } return 1 }
// Encode image to file func (c *Convertor) encodeImage(i image.Image, filename string) (err error) { f, err := os.Create(filename) if err != nil { return } switch filepath.Ext(filename) { case ".png": err = png.Encode(f, i) case ".tif": case ".tiff": err = tiff.Encode(f, i, &tiff.Options{tiff.Uncompressed, false}) case ".gif": err = gif.Encode(f, i, nil) default: err = jpeg.Encode(f, i, &jpeg.Options{c.Opts.Quality}) } f.Close() return }
// Encode implements Encoder. func (enc *Encoder) Encode(w io.Writer, nim image.Image, params imageserver.Params) error { return tiff.Encode(w, nim, opts) }
// Provides image://cover/ func (c *Comics) CoverProvider(file string, width int, height int) image.Image { c.Conv.Opts = c.GetOptions() stat, err := os.Stat(file) if err != nil { fmt.Fprintf(os.Stderr, "Error Stat CoverProvider: %v\n", err.Error()) return image.NewRGBA(image.Rect(0, 0, width, height)) } cover, err := c.Conv.GetCoverImage(file, stat) if err != nil { fmt.Fprintf(os.Stderr, "Error GetCoverImage: %v\n", err.Error()) return image.NewRGBA(image.Rect(0, 0, width, height)) } cover = c.Conv.TransformImage(cover) if c.Conv.Opts.LevelsInMin != 0 || c.Conv.Opts.LevelsInMax != 255 || c.Conv.Opts.LevelsGamma != 1.00 || c.Conv.Opts.LevelsOutMin != 0 || c.Conv.Opts.LevelsOutMax != 255 { cover = c.Conv.LevelImage(cover) } // imaging is used for preview only if c.Conv.Opts.Grayscale { cover = imaging.Grayscale(cover) } // size preview s := 0 b := new(bytes.Buffer) w := 0 h := 0 switch c.Conv.Opts.Format { case "jpeg": jpeg.Encode(b, cover, &jpeg.Options{c.Conv.Opts.Quality}) s = len(b.Bytes()) cover, _ = jpeg.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "png": png.Encode(b, cover) s = len(b.Bytes()) cover, _ = png.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "gif": mw := imagick.NewMagickWand() defer mw.Destroy() mw.ReadImageBlob(c.Conv.GetImageBytes(cover)) mw.SetImageFormat("GIF") blob := mw.GetImageBlob() s = len(blob) cover, _ = gif.Decode(bytes.NewReader(blob)) config, _, _ := image.DecodeConfig(bytes.NewReader(blob)) w = config.Width h = config.Height case "tiff": tiff.Encode(b, cover, &tiff.Options{tiff.Uncompressed, false}) var buf bytes.Buffer gz := gzip.NewWriter(&buf) gz.Write(b.Bytes()) gz.Close() s = buf.Len() cover, _ = tiff.Decode(bytes.NewReader(b.Bytes())) config, _, _ := image.DecodeConfig(bytes.NewReader(b.Bytes())) w = config.Width h = config.Height case "bmp": mw := imagick.NewMagickWand() defer mw.Destroy() bb := c.Conv.GetImageBytes(cover) mw.ReadImageBlob(bb) wand := imagick.NewPixelWand() wand.SetColor("black") defer wand.Destroy() mw.SetImageFormat("BMP3") mw.SetImageBackgroundColor(wand) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_REMOVE) mw.SetImageAlphaChannel(imagick.ALPHA_CHANNEL_DEACTIVATE) mw.SetImageMatte(false) mw.SetImageCompression(imagick.COMPRESSION_NO) mw.QuantizeImage(16, mw.GetImageColorspace(), 8, true, true) var buf bytes.Buffer blob := mw.GetImageBlob() gz := gzip.NewWriter(&buf) gz.Write(blob) gz.Close() s = buf.Len() cover, _ = bmp.Decode(bytes.NewReader(blob)) config, _, _ := image.DecodeConfig(bytes.NewReader(bb)) w = config.Width h = config.Height } if cover == nil { return image.NewRGBA(image.Rect(0, 0, width, height)) } human := humanize.IBytes(uint64(s)) c.Root.ObjectByName("sizePreview").Set("text", fmt.Sprintf("%s (%dx%d)", human, w, h)) return cover }