From 4abf99a644accdd7d95532622e606aacfdf6d3eb Mon Sep 17 00:00:00 2001 From: Milan Nikolic Date: Tue, 3 Nov 2015 19:47:24 +0100 Subject: [PATCH] add gif support --- README.md | 9 +++++---- cbconvert.go | 27 ++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index c0452d5..78e4f33 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ Features - reads RAR, ZIP, 7Z, GZ, BZ2, CBR, CBZ, CB7, CBT, PDF, EPUB, XPS and plain directory - always saves processed comic in CBZ (ZIP) archive format - - images can be converted to JPEG, PNG or 4-Bit BMP (16 colors) file format + - images can be converted to JPEG, PNG, GIF or 4-Bit BMP (16 colors) file format - reads JPEG, PNG, BMP, GIF, TIFF and WEBP file formats - choose resize algorithm (NearestNeighbor, Bilinear, Bicubic, MitchellNetravali, Lanczos2/3) - export covers from comics @@ -20,10 +20,10 @@ Features Download -------- - - [Windows static build](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0.zip) + - [Windows binary](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0.zip) - - [Linux 64bit build](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0.tar.gz) - - [Linux 64bit static build](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0-static.tar.gz) + - [Linux 64bit binary](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0.tar.gz) + - [Linux 64bit static binary](https://github.com/gen2brain/cbconvert/releases/download/0.3.0/cbconvert-0.3.0-static.tar.gz) Using ----- @@ -37,6 +37,7 @@ Using --version Show application version. -p, --png encode images to PNG instead of JPEG -b, --bmp encode images to 4-Bit BMP instead of JPEG + -g, --gif encode images to GIF instead of JPEG -w, --width=0 image width -h, --height=0 image height -q, --quality=75 JPEG image quality diff --git a/cbconvert.go b/cbconvert.go index 9c71180..c53d959 100644 --- a/cbconvert.go +++ b/cbconvert.go @@ -62,7 +62,8 @@ var ( // Command line options type options struct { ToPNG bool // encode images to PNG instead of JPEG - ToBMP bool // encode images to 4-Bit BMP instead of JPEG + ToBMP bool // encode images to 4-Bit BMP (16 colors) instead of JPEG + ToGIF bool // encode images to GIF instead of JPEG Quality int // JPEG image quality NoRGB bool // do not convert images with RGB colorspace Width uint // image width @@ -92,6 +93,8 @@ func convertImage(img image.Image, index int, pathName string) { ext = "png" } else if opts.ToBMP { ext = "bmp" + } else if opts.ToGIF { + ext = "gif" } var filename string @@ -118,7 +121,7 @@ func convertImage(img image.Image, index int, pathName string) { defer f.Close() png.Encode(f, i) } else if opts.ToBMP { - // convert image to 4-Bit - 16 colors BMP + // convert image to 4-Bit BMP (16 colors) imagick.Initialize() mw := imagick.NewMagickWand() @@ -143,6 +146,22 @@ func convertImage(img image.Image, index int, pathName string) { mw.SetImageCompression(imagick.COMPRESSION_NO) mw.QuantizeImage(16, imagick.COLORSPACE_SRGB, 8, true, true) mw.WriteImage(fmt.Sprintf("BMP3:%s", filename)) + } else if opts.ToGIF { + // convert image to GIF + imagick.Initialize() + + mw := imagick.NewMagickWand() + defer mw.Destroy() + + b := new(bytes.Buffer) + jpeg.Encode(b, i, &jpeg.Options{jpeg.DefaultQuality}) + + err := mw.ReadImageBlob(b.Bytes()) + if err != nil { + fmt.Fprintf(os.Stderr, "Error ReadImageBlob: %v\n", err.Error()) + } + mw.SetImageFormat("gif") + mw.WriteImage(filename) } else { // convert image to JPEG (default) f, err := os.Create(filename) @@ -772,7 +791,8 @@ func parseFlags() { kingpin.Version("CBconvert 0.3.0") kingpin.CommandLine.Help = "Comic Book convert tool." kingpin.Flag("png", "encode images to PNG instead of JPEG").Short('p').BoolVar(&opts.ToPNG) - kingpin.Flag("bmp", "encode images to 4-Bit BMP instead of JPEG").Short('b').BoolVar(&opts.ToBMP) + kingpin.Flag("bmp", "encode images to 4-Bit BMP (16 colors) instead of JPEG").Short('b').BoolVar(&opts.ToBMP) + kingpin.Flag("gif", "encode images to GIF instead of JPEG").Short('g').BoolVar(&opts.ToGIF) kingpin.Flag("width", "image width").Default(strconv.Itoa(0)).Short('w').UintVar(&opts.Width) kingpin.Flag("height", "image height").Default(strconv.Itoa(0)).Short('h').UintVar(&opts.Height) kingpin.Flag("quality", "JPEG image quality").Short('q').Default(strconv.Itoa(jpeg.DefaultQuality)).IntVar(&opts.Quality) @@ -797,6 +817,7 @@ func main() { signal.Notify(c, os.Interrupt, syscall.SIGHUP, syscall.SIGTERM) go func() { for _ = range c { + fmt.Fprintf(os.Stderr, "Aborting\n") os.RemoveAll(workdir) os.Exit(1) }