Skip to content

image: Decode drops interfaces #20851

@carl-mastrangelo

Description

@carl-mastrangelo

What version of Go are you using (go version)?

1.8

Background: I am trying to register an image decoder for Webm (to make thumbnails). Commonly the reader passed to image.Decode() is an *os.File or a multipart.File.

What did you see instead?

The Reader passed to the decoder function (register with image.RegisterFormat) is almost never the same one as was passed to image.Decode. It is wrapped in a reader which also implements Peek (for looking at the first few bytes without accidentally consuming them.

The problem is that wrapping in this type drops all the other interfaces of the reader, which may be useful to the Decoder. For example, in my case, the image decoding will be delegated to an executable. It would be better to pass the file name to the subprocess rather than streaming it over stdin. The interface{Name() string} interface of *os.File, and the io.ReaderAt and io.Seeker shared by *os.File and multipart.File cannot be accessed.

It would be nice if the image package forwarded commonly used methods by optionally implementing them if the source reader did. For example:

type readernamer interface {
  reader
  Name() string
} 

which the image package would pass instead. I realize that would result in 2^n interfaces, but I don't see a way around it without exposing the original reader.

One thing worth noting: The peeker interface is unnecessary if the input reader implements Seeker. Peek can be implemented with Read and Seek

Metadata

Metadata

Assignees

No one assigned

    Labels

    NeedsDecisionFeedback is required from experts, contributors, and/or the community before a change can be made.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions