github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/tiff/tiff.go (about) 1 // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package tiff implements a TIFF image decoder and encoder. 6 // 7 // The TIFF specification is at http://partners.adobe.com/public/developer/en/tiff/TIFF6.pdf 8 package tiff 9 10 // BUG(chai2010): support Gray32f/RGB/RGB48/RGB96f/RGBA128f. 11 12 import ( 13 "image" 14 "image/color" 15 "io" 16 17 "code.google.com/p/go.image/tiff" 18 image_ext "github.com/chai2010/gopkg/image" 19 "github.com/chai2010/gopkg/image/convert" 20 ) 21 22 const ( 23 leHeader = "II\x2A\x00" // Header for little-endian files. 24 beHeader = "MM\x00\x2A" // Header for big-endian files. 25 ) 26 27 // Options are the encoding and decoding parameters. 28 type Options struct { 29 *tiff.Options 30 ColorModel color.Model 31 } 32 33 // DecodeConfig returns the color model and dimensions of a TIFF image without 34 // decoding the entire image. 35 func DecodeConfig(r io.Reader) (config image.Config, err error) { 36 return tiff.DecodeConfig(r) 37 } 38 39 // Decode reads a TIFF image from r and returns it as an image.Image. 40 // The type of Image returned depends on the contents of the TIFF. 41 func Decode(r io.Reader, opt *Options) (m image.Image, err error) { 42 if m, err = tiff.Decode(r); err != nil { 43 return 44 } 45 if opt != nil && opt.ColorModel != nil { 46 m = convert.ColorModel(m, opt.ColorModel) 47 } 48 return 49 } 50 51 // Encode writes the image m to w. opt determines the options used for 52 // encoding, such as the compression type. If opt is nil, an uncompressed 53 // image is written. 54 func Encode(w io.Writer, m image.Image, opt *Options) error { 55 if opt != nil && opt.ColorModel != nil { 56 m = convert.ColorModel(m, opt.ColorModel) 57 } 58 if opt != nil && opt.Options != nil { 59 return tiff.Encode(w, m, opt.Options) 60 } else { 61 return tiff.Encode(w, m, nil) 62 } 63 } 64 65 func imageExtDecode(r io.Reader, opt interface{}) (image.Image, error) { 66 if opt, ok := opt.(*Options); ok { 67 return Decode(r, opt) 68 } else { 69 return Decode(r, nil) 70 } 71 } 72 73 func imageExtEncode(w io.Writer, m image.Image, opt interface{}) error { 74 if opt, ok := opt.(*Options); ok { 75 return Encode(w, m, opt) 76 } else { 77 return Encode(w, m, nil) 78 } 79 } 80 81 func init() { 82 image_ext.RegisterFormat(image_ext.Format{ 83 Name: "tiff", 84 Extensions: []string{".tiff", ".tif"}, 85 Magics: []string{leHeader, beHeader}, 86 DecodeConfig: DecodeConfig, 87 Decode: imageExtDecode, 88 Encode: imageExtEncode, 89 }) 90 }