Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience function for loading a picture directly from a file #111

Merged
merged 6 commits into from
Aug 31, 2024

Conversation

dusk125
Copy link
Contributor

@dusk125 dusk125 commented Aug 21, 2024

No description provided.

@dusk125 dusk125 requested a review from a team August 21, 2024 14:22
@dusk125 dusk125 marked this pull request as draft August 21, 2024 14:49
@dusk125 dusk125 marked this pull request as ready for review August 21, 2024 15:21
Copy link
Contributor

@bhperry bhperry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the Atlas file helpers use this?

@dusk125
Copy link
Contributor Author

dusk125 commented Aug 21, 2024

Not directly, but I can update the atlas to mimic this functionality.

The atlas loads the images as image.Image so it can pack them, then creates the pixel.PictureData from the already loaded images.

Copy link
Contributor

@bhperry bhperry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dusk125 dusk125 marked this pull request as draft August 21, 2024 16:07
@dusk125
Copy link
Contributor Author

dusk125 commented Aug 21, 2024

Thanks for the review, converting back to draft as I want to test something else out.

@bhperry
Copy link
Contributor

bhperry commented Aug 22, 2024

Alternative proposal from discord discussions:

type ImageLoader interface {
    Image(path string) (image.Image, error)
    PictureData(path string) (*PictureData, error)
}

func NewFileLoader(decoder DecoderFunc) FileLoader {
    if decoder == nil {
        decoder = DefaultImageDecoder
    }
    return FileLoader{decoder: decoder}
}

type FileLoader struct {
    decoder DecoderFunc
}

func (fl FileLoader) Image(path string) (image.Image, error) {
    f, err := os.Open(path)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    return fl.decoder(f)
}

func (fl FileLoader) PictureData(path string) (*PictureData, error) {
    img, err := fl.Image(path)
    if err != nil {
        return nil, err
    }
    return PictureDataFromImage(img), nil
}

func NewEmbedLoader(fs fs.FS, decoder DecoderFunc) EmbedLoader {
    if decoder == nil {
        decoder = DefaultImageDecoder
    }
    return EmbedLoader{fs: fs, decoder: decoder}
}

type EmbedLoader struct {
    fs      fs.FS
    decoder DecoderFunc
}

func (el EmbedLoader) Image(path string) (image.Image, error) {
    f, err := el.fs.Open(path)
    if err != nil {
        return nil, err
    }
    defer f.Close()

    return el.decoder(f)
}

func (el EmbedLoader) PictureData(path string) (*PictureData, error) {
    img, err := el.Image(path)
    if err != nil {
        return nil, err
    }
    return PictureDataFromImage(img), nil
}

Interface style allows pixel to provide a powerful set of loaders that come with self-contained state, while also allowing users to implement their own custom loaders (e.g. MyHttpServerLoader) that will work with the pixel ecosystem.

@dusk125 dusk125 marked this pull request as ready for review August 30, 2024 23:33
ext/atlas/help.go Outdated Show resolved Hide resolved
ext/atlas/group.go Show resolved Hide resolved
@dusk125 dusk125 merged commit 5ce991f into gopxl:main Aug 31, 2024
1 check passed
@dusk125 dusk125 deleted the pic-file-load branch August 31, 2024 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants