-
-
Notifications
You must be signed in to change notification settings - Fork 93
imaagejson: JSON wrapper around image.Image that auto-encodes images #1597
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
95cbc74
imaagejson: JSON wrapper around image.Image that auto-encodes images,…
rcoreilly fab9f2b
imagejson: JSON wrapper fully working and tested..
rcoreilly 465a24a
imagejson: null check in svg
rcoreilly e1ccf93
Merge branch 'main' into imagejson
kkoreilly 40e7e1e
imagejson: much cleaner impl of base64 encoding functions, and avoid …
rcoreilly 12d0b88
imagejson: remove todo
rcoreilly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Copyright (c) 2025, Cogent Core. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package imagex | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "image" | ||
| "image/color" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| type testObj struct { | ||
| Name string | ||
| Image *JSON | ||
| Another string | ||
| } | ||
|
|
||
| func testImage() *image.RGBA { | ||
| im := image.NewRGBA(image.Rect(0, 0, 16, 16)) | ||
| for y := range 16 { | ||
| for x := range 16 { | ||
| im.Set(x, y, color.RGBA{uint8(x * 16), uint8(y * 16), 128, 255}) | ||
| } | ||
| } | ||
| return im | ||
| } | ||
|
|
||
| func TestSave(t *testing.T) { | ||
| im := testImage() | ||
| // this tests Save and Open etc for all formats | ||
| Assert(t, im, "test.png", 1) // should be exact | ||
| Assert(t, im, "test.jpg", 20) // quite bad | ||
| Assert(t, im, "test.gif", 50) // even worse | ||
| Assert(t, im, "test.tif", 1) | ||
| Assert(t, im, "test.bmp", 1) | ||
| // Assert(t, im, "test.webp") // only for reading, not writing | ||
| } | ||
|
|
||
| func TestBase64(t *testing.T) { | ||
| im := testImage() | ||
| b, mime := ToBase64(im, PNG) | ||
| assert.Equal(t, "image/png", mime) | ||
| bim, f, err := FromBase64(b) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, PNG, f) | ||
| bounds, content, _, _, _, _ := ImagesEqual(im, bim, 1) | ||
| assert.True(t, bounds) | ||
| assert.True(t, content) | ||
|
|
||
| b, mime = ToBase64(im, JPEG) | ||
| assert.Equal(t, "image/jpeg", mime) | ||
| bim, f, err = FromBase64(b) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, JPEG, f) | ||
| bounds, content, _, _, _, _ = ImagesEqual(im, bim, 20) | ||
| assert.True(t, bounds) | ||
| assert.True(t, content) | ||
| } | ||
|
|
||
| func TestJSON(t *testing.T) { | ||
| im := testImage() | ||
| jsi := &JSON{Image: im} | ||
|
|
||
| b, err := json.Marshal(jsi) | ||
| assert.NoError(t, err) | ||
|
|
||
| nsi := &JSON{} | ||
| err = json.Unmarshal(b, nsi) | ||
| assert.NoError(t, err) | ||
|
|
||
| ri := nsi.Image.(*image.RGBA) | ||
|
|
||
| assert.Equal(t, im, ri) | ||
|
|
||
| bounds, content, _, _, _, _ := ImagesEqual(im, ri, 1) | ||
| assert.True(t, bounds) | ||
| assert.True(t, content) | ||
|
|
||
| jo := &testObj{Name: "testy", Another: "guy"} | ||
| jo.Image = NewJSON(im) | ||
|
|
||
| b, err = json.Marshal(jo) | ||
| assert.NoError(t, err) | ||
|
|
||
| no := &testObj{} | ||
| err = json.Unmarshal(b, no) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, jo, no) | ||
| bounds, content, _, _, _, _ = ImagesEqual(jo.Image.Image, no.Image.Image, 1) | ||
| assert.True(t, bounds) | ||
| assert.True(t, content) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // Copyright (c) 2025, Cogent Core. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package imagex | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "image" | ||
| ) | ||
|
|
||
| // JSON is a wrapper around an [image.Image] that defines JSON | ||
| // Marshal and Unmarshal methods, so that the image will automatically | ||
| // be properly saved / loaded when used as a struct field, for example. | ||
| // Must be a pointer type to support custom unmarshal function. | ||
| // The original image is not anonymously embedded so that you have to | ||
| // extract it, otherwise it will be processed inefficiently. | ||
| type JSON struct { | ||
| Image image.Image | ||
| } | ||
|
|
||
| // JSONEncoded is a representation of an image encoded into a byte stream, | ||
| // using the PNG encoder. This can be Marshal and Unmarshal'd directly. | ||
| type JSONEncoded struct { | ||
| Width int | ||
| Height int | ||
|
|
||
| // Image is the encoded byte stream, which will be encoded in JSON | ||
| // using Base64 | ||
| Image []byte | ||
| } | ||
|
|
||
| // NewJSON returns a new JSON wrapper around given image, | ||
| // to support automatic wrapping and unwrapping. | ||
| func NewJSON(im image.Image) *JSON { | ||
| return &JSON{Image: im} | ||
| } | ||
|
|
||
| func (js *JSON) MarshalJSON() ([]byte, error) { | ||
| id := &JSONEncoded{} | ||
| if js.Image != nil { | ||
| sz := js.Image.Bounds().Size() | ||
| id.Width = sz.X | ||
| id.Height = sz.Y | ||
| ibuf := &bytes.Buffer{} | ||
| Write(js.Image, ibuf, PNG) | ||
| id.Image = ibuf.Bytes() | ||
| } | ||
| return json.Marshal(id) | ||
| } | ||
|
|
||
| func (js *JSON) UnmarshalJSON(b []byte) error { | ||
| id := &JSONEncoded{} | ||
| err := json.Unmarshal(b, id) | ||
| if err != nil || (id.Width == 0 && id.Height == 0) { | ||
| js.Image = nil | ||
| return err | ||
| } | ||
| im, _, err := image.Decode(bytes.NewReader(id.Image)) | ||
| if err != nil { | ||
| js.Image = nil | ||
| return err | ||
| } | ||
| js.Image = im | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.