Example #1
0
File: bmpic.go Project: ishawge/bmp
func showAll() {

	X, err := xgbutil.NewConn()
	if err != nil {
		log.Fatal(err)
	}
	for i := 0; i < len(testImages); i++ {
		fname := testImages[i]
		fmt.Printf("%d Working on %s\n", i, fname)
		file, err := os.Open(testDir + fname)
		if err != nil {
			continue
		}
		defer file.Close()
		var img image.Image
		// Decode the image.
		// using true forces bmp decoder, otherwise whatever is registered for ext is used
		// result slightly different if non-bmps fed to it
		if true {
			img, err = bmp.Decode(file)
		} else {
			img, _, err = image.Decode(file)
		}
		if err != nil {
			continue
		}
		ximg := xgraphics.NewConvert(X, img)
		ximg.XShowExtra(fname, true)
		time.Sleep(1 * time.Second)
	}
	xevent.Main(X)
	time.Sleep(4 * time.Second)
	xevent.Quit(X)
}
Example #2
0
File: bmpwv.go Project: ishawge/bmp
func bmpWriteOut(imageName string, w http.ResponseWriter) {
	fmt.Printf("bmpWriteOut: imageName = %s\n", imageName)
	bf, err := os.Open(imageName)
	if err != nil {
		fmt.Printf("bmpWriteOut: cant open bmp %s\n", imageName)
		return
	}
	img, err := bmp.Decode(bf)
	if err != nil {
		fmt.Printf("bmpWriteOut: bmp decode failed for %s\n", imageName)
		w.Write([]byte(fmt.Sprintf("Decode failed for %s\n", imageName)))
		return
	}
	b := make([]byte, 0, 10000)
	wo := bytes.NewBuffer(b)
	err = png.Encode(wo, img)
	if err != nil {
		fmt.Printf("bmpWriteOut: png encode failed for %s\n", imageName)
		return
	}
	w.Write(wo.Bytes())
}
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
}