Ver Fonte

TIFF and BMP image format support added. Now Mercurial is needed to 'go get' the package

disintegration há 11 anos atrás
pai
commit
9e6af48e9e
2 ficheiros alterados com 17 adições e 53 exclusões
  1. 4 51
      README.md
  2. 13 2
      helpers.go

+ 4 - 51
README.md

@@ -6,6 +6,8 @@ This package is based on the standard Go image package and works best along with
 ## Installation
 
     go get -u github.com/disintegration/imaging
+
+*[Git](http://git-scm.com/) and [Mercurial](http://mercurial.selenic.com/) are needed.*
     
 ## Documentation
 
@@ -170,6 +172,8 @@ dstImage = imaging.Invert(srcImage)
 #### Open, Save, New, Clone
 
 Imaging package provides useful shortcuts for image loading, saving, creation and copying.
+Open and Save functions support JPEG, PNG, TIFF and BMP images. 
+External libraries can be used to load other image formats.
 
 ```go
 // load an image from file
@@ -191,57 +195,6 @@ newImg := imaging.New(800, 600, color.NRGBA{255, 0, 0, 255})
 copiedImg := imaging.Clone(img)
 ```
 
-Open and Save functions support JPEG and PNG images. 
-External libraries can be used to work with other image formats.
-The example of TIFF image resizing:
-
-```go
-package main
-
-import (
-    "image"
-    "os"
-    "runtime"
-
-    // package to load and save tiff images
-    "code.google.com/p/go.image/tiff"
-    
-    "github.com/disintegration/imaging"
-)
-
-func main() {
-    // use all CPU cores for maximum performance
-    runtime.GOMAXPROCS(runtime.NumCPU())
-
-    // Load TIFF image
-    file, err := os.Open("src.tif")
-    if err != nil {
-        panic(err)
-    }
-    defer file.Close()
-
-    srcImage, _, err := image.Decode(file)
-    if err != nil {
-        panic(err)
-    }
-
-    // Resize the image
-    resizedImg := imaging.Resize(srcImage, 800, 0, imaging.Lanczos)
-
-    // Save to file
-    outfile, err := os.Create("dst.tif")
-    if err != nil {
-        panic(err)
-    }
-    defer outfile.Close()
-
-    err = tiff.Encode(outfile, resizedImg, nil)
-    if err != nil {
-        panic(err)
-    }
-}
-```
-
 
 ## Code example
 Here is the complete example that loades several images, makes thumbnails of them

+ 13 - 2
helpers.go

@@ -10,7 +10,11 @@ import (
 	"math"
 	"os"
 	"path/filepath"
+	"regexp"
 	"strings"
+
+	"code.google.com/p/go.image/bmp"
+	"code.google.com/p/go.image/tiff"
 )
 
 // Open loads an image from file
@@ -31,10 +35,11 @@ func Open(filename string) (img image.Image, err error) {
 }
 
 // Save saves the image to file with the specified filename.
-// The format is determined from the filename extension, "jpg" (or "jpeg") and "png" are supported.
+// The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "tif" (or "tiff") and "bmp" are supported.
 func Save(img image.Image, filename string) (err error) {
 	format := strings.ToLower(filepath.Ext(filename))
-	if format != ".jpg" && format != ".jpeg" && format != ".png" {
+	m, err := regexp.MatchString(`^\.(jpg|jpeg|png|tif|tiff|bmp)$`, format)
+	if err != nil || !m {
 		err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format)
 		return
 	}
@@ -65,6 +70,12 @@ func Save(img image.Image, filename string) (err error) {
 
 	case ".png":
 		err = png.Encode(file, img)
+	case ".tif", ".tiff":
+		err = tiff.Encode(file, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
+	case ".bmp":
+		err = bmp.Encode(file, img)
+	default:
+		err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format)
 	}
 	return
 }