brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.14k stars 255 forks source link

Add an image type getter #619

Closed feinstein closed 3 months ago

feinstein commented 4 months ago

It would be useful if we could get a function that returns an image type. I think this is feasible for most image formats, since they have magic numbers. This function could return an enum with the possible image types, like png or jpg.

feinstein commented 4 months ago

Looking at the lib, I think this can be done by using the findDecoderForData and then checking which decoder was selected.

brendan-duncan commented 4 months ago

Yeah I was going to suggest findDecoderForData. It will try decoding just the header for all the supported formats until it finds one that's most likely the format. It's always better to know what format you're decoding to avoid the library trying to guess.

feinstein commented 3 months ago

Was this completed by a PR? I don't see it linked.

brendan-duncan commented 3 months ago

It was my understanding existing functionality provided what you needed.

feinstein commented 3 months ago

Yes, but we could have an easier and simpler API to abstract this information

brendan-duncan commented 3 months ago

Normally I wouldn't go for a request like that, it could easily be done by the client app without needing to add even more clutter to the already cluttered API. But as I starting to type that argument, whatever I don't really care, it took 5 minutes to add. So here it is, https://github.com/brendan-duncan/image/commit/ab3eecb2ffdff09328a5be7f8b0e711521972cfe.

enum ImageFormat {
  bmp, cur, exr, gif, ico, jpg, png, pnm, psd, pvr, tga, tiff, webp, custom, invalid
}
class Decoder {
  ImageFormat get format => ImageFormat.invalid;
}
ImageFormat findFormatForData(List<int> data);
Decoder? createDecoderForFormat(ImageFormat format);

It will still be a while before any of this is published.

feinstein commented 3 months ago

Looks awesome, thanks!