github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/webp/reader.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 webp
     6  
     7  import (
     8  	"image"
     9  	"image/color"
    10  	"io"
    11  	"io/ioutil"
    12  
    13  	image_ext "github.com/chai2010/gopkg/image"
    14  	color_ext "github.com/chai2010/gopkg/image/color"
    15  	"github.com/chai2010/gopkg/image/convert"
    16  )
    17  
    18  const DefaulQuality = 90
    19  
    20  // Options are the encoding parameters.
    21  type Options struct {
    22  	ColorModel color.Model
    23  	Lossless   bool
    24  	Quality    float32 // 0 ~ 100
    25  }
    26  
    27  // DecodeConfig returns the color model and dimensions of a WEBP image without
    28  // decoding the entire image.
    29  func DecodeConfig(r io.Reader) (config image.Config, err error) {
    30  	data, err := ioutil.ReadAll(r)
    31  	if err != nil {
    32  		return
    33  	}
    34  	width, height, hasAlpha, err := GetInfo(data)
    35  	if err != nil {
    36  		return
    37  	}
    38  	config.Width = width
    39  	config.Height = height
    40  	if hasAlpha {
    41  		config.ColorModel = color.RGBAModel
    42  	} else {
    43  		config.ColorModel = color_ext.RGBModel
    44  	}
    45  	return
    46  }
    47  
    48  // Decode reads a WEBP image from r and returns it as an image.Image.
    49  func Decode(r io.Reader, opt *Options) (m image.Image, err error) {
    50  	data, err := ioutil.ReadAll(r)
    51  	if err != nil {
    52  		return
    53  	}
    54  	_, _, hasAlpha, err := GetInfo(data)
    55  	if err != nil {
    56  		return
    57  	}
    58  
    59  	if opt != nil && opt.ColorModel == color.GrayModel {
    60  		m, err = DecodeGray(data)
    61  	} else if opt != nil && opt.ColorModel == color_ext.RGBModel {
    62  		m, err = DecodeRGB(data)
    63  	} else if opt != nil && opt.ColorModel == color.RGBAModel {
    64  		m, err = DecodeRGBA(data)
    65  	} else if hasAlpha {
    66  		m, err = DecodeRGBA(data)
    67  	} else {
    68  		m, err = DecodeRGB(data)
    69  	}
    70  	if err != nil {
    71  		return
    72  	}
    73  	if opt != nil && opt.ColorModel != nil {
    74  		m = convert.ColorModel(m, opt.ColorModel)
    75  	}
    76  	return
    77  }
    78  
    79  func imageDecode(r io.Reader) (image.Image, error) {
    80  	return Decode(r, nil)
    81  }
    82  
    83  func imageExtDecode(r io.Reader, opt interface{}) (image.Image, error) {
    84  	if opt, ok := opt.(*Options); ok {
    85  		return Decode(r, opt)
    86  	} else {
    87  		return Decode(r, nil)
    88  	}
    89  }
    90  
    91  func imageExtEncode(w io.Writer, m image.Image, opt interface{}) error {
    92  	if opt, ok := opt.(*Options); ok {
    93  		return Encode(w, m, opt)
    94  	} else {
    95  		return Encode(w, m, nil)
    96  	}
    97  }
    98  
    99  func init() {
   100  	image.RegisterFormat("webp", "RIFF????WEBPVP8 ", imageDecode, DecodeConfig)
   101  
   102  	image_ext.RegisterFormat(image_ext.Format{
   103  		Name:         "webp",
   104  		Extensions:   []string{".webp"},
   105  		Magics:       []string{"RIFF????WEBPVP8 "},
   106  		DecodeConfig: DecodeConfig,
   107  		Decode:       imageExtDecode,
   108  		Encode:       imageExtEncode,
   109  	})
   110  }