func Encode(w io.Writer, m image.Image, o *Options) error {
引数w
にio.Writer
インターフェースを実装している型の変数を取る。
var e encoder
if ww, ok := w.(writer); ok {
e.w = ww
} else {
e.w = bufio.NewWriter(w)
}
jepg.writer
インターフェースに変換。
キャストできればそのまま。できない場合はbufio.NewWrite()
で行う。
type writer interface {
Flush() error
io.Writer
io.ByteWriter
}
jpeg.writer
インターフェースの定義。
bufio.NewWriter()
はbufio.Writer
を返す。これはjpeg.writer
インターフェースを実装している。
// Write the Start Of Image marker.
e.buf[0] = 0xff
e.buf[1] = 0xd8
e.write(e.buf[:2])
// Write the quantization tables.
e.writeDQT()
// Write the image dimensions.
e.writeSOF0(b.Size(), nComponent)
// Write the Huffman tables.
e.writeDHT(nComponent)
// Write the image data.
e.writeSOS(m)
// Write the End Of Image marker.
e.buf[0] = 0xff
e.buf[1] = 0xd9
e.write(e.buf[:2])
e.flush()
このあたりで変換後の書き込みをしていると思われる。
いずれの処理も内部的にjpeg.writer
インターフェースのメソッドを利用している。