Example #1
0
func BenchmarkLoad_video_001_tiff_x(b *testing.B) {
	for i := 0; i < b.N; i++ {
		f, err := os.Open("../testdata/video-001.tiff")
		if err != nil {
			b.Fatal(err)
		}
		_, err = xtiff.Decode(f)
		if err != nil {
			b.Fatal(err)
		}
		f.Close()
	}
}
Example #2
0
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
}
Example #3
0
// 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
}