Skip to content

Commit

Permalink
Add jpeg support (mat#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgeous authored Feb 23, 2021
1 parent 3ebe7a3 commit 5dc6418
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 69 deletions.
121 changes: 58 additions & 63 deletions Readme.markdown

Large diffs are not rendered by default.

13 changes: 10 additions & 3 deletions besticon/besticon.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

// Load supported image formats.
_ "image/gif"
_ "image/jpeg"
_ "image/png"

_ "github.com/mat/besticon/ico"
Expand All @@ -36,6 +37,7 @@ import (
var defaultFormats []string

const MinIconSize = 0

// TODO: Turn into env var: https://github.com/rendomnet/besticon/commit/c85867cc80c00c898053ce8daf40d51a93b9d39f#diff-37b57e3fdbe4246771791e86deb4d69dL41
const MaxIconSize = 500

Expand Down Expand Up @@ -222,9 +224,9 @@ func MainColorForIcons(icons []Icon) *color.RGBA {
}

var icon *Icon
// Prefer .png and .gif
// Prefer gif, jpg, png
for _, ico := range icons {
if ico.Format == "png" || ico.Format == "gif" {
if ico.Format == "gif" || ico.Format == "jpg" || ico.Format == "png" {
icon = &ico
break
}
Expand Down Expand Up @@ -293,6 +295,11 @@ func fetchIconDetails(url string) Icon {
return i
}

// jpeg => jpg
if format == "jpeg" {
format = "jpg"
}

i.Width = cfg.Width
i.Height = cfg.Height
i.Format = format
Expand Down Expand Up @@ -441,7 +448,7 @@ func init() {
setHTTPClient(&http.Client{Timeout: duration})

// Needs to be kept in sync with those image/... imports
defaultFormats = []string{"png", "gif", "ico"}
defaultFormats = []string{"gif", "ico", "jpg", "png"}
}

func setHTTPClient(c *http.Client) {
Expand Down
16 changes: 15 additions & 1 deletion besticon/besticon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ func TestRandomOrg(t *testing.T) {
assertEquals(t, expectedImage, actualImage)
}

func TestArchiveOrgWithJpg(t *testing.T) {
actualImages, _, err := fetchIconsWithVCR("archive.org.vcr", "https://archive.org")
assertEquals(t, nil, err)

expectedImages := []Icon{
{URL: "https://archive.org/apple-touch-icon-precomposed.png", Width: 180, Height: 180, Format: "png", Bytes: 5495, Sha1sum: "7b583a9eee7c4f705f2a93ddc50e6927bde4b634"},
{URL: "https://archive.org/apple-touch-icon.png", Width: 180, Height: 180, Format: "png", Bytes: 7494, Sha1sum: "093d651bf04e480e5c25167e780455c879c02447"},
{URL: "https://archive.org/images/glogo.jpg", Width: 40, Height: 40, Format: "jpg", Bytes: 3213, Sha1sum: "279fe766b791ae83a10765a8790a0928448a4e35"},
{URL: "https://archive.org/favicon.ico", Width: 32, Height: 32, Format: "ico", Bytes: 4286, Sha1sum: "b18786d77997511ab0f6e5c9d3c5b9e1bff164be"},
}
assertEquals(t, expectedImages, actualImages)
}

func TestParsingInexistentSite(t *testing.T) {
actualImages, _, err := fetchIconsWithVCR("not_existent.vcr", "http://wikipedia.org/does-not-exist")

Expand All @@ -210,8 +223,9 @@ func TestFindBestIconNoIcons(t *testing.T) {
}

func TestImageSizeDetection(t *testing.T) {
assertEquals(t, 1, getImageWidthForFile("testdata/pixel.png"))
assertEquals(t, 1, getImageWidthForFile("testdata/pixel.gif"))
assertEquals(t, 1, getImageWidthForFile("testdata/pixel.jpg"))
assertEquals(t, 1, getImageWidthForFile("testdata/pixel.png"))
assertEquals(t, 48, getImageWidthForFile("testdata/favicon.ico"))
}

Expand Down
Binary file added besticon/testdata/archive.org.vcr
Binary file not shown.
Binary file added besticon/testdata/pixel.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion colorfinder/colorfinder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"image/color"

// Load supported image formats
_ "github.com/mat/besticon/ico"
_ "image/gif"
_ "image/jpeg"
_ "image/png"

_ "github.com/mat/besticon/ico"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion colorfinder/colorfinder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ func TestSimplePixel(t *testing.T) {
}

func TestImageFormats(t *testing.T) {
assertFindsRightColor(t, "white1x1.png", "ffffff")
assertFindsRightColor(t, "white1x1.gif", "ffffff")
assertFindsRightColor(t, "white1x1.jpg", "ffffff")
assertFindsRightColor(t, "white1x1.png", "ffffff")
}

func TestFindColors01(t *testing.T) {
Expand Down
Binary file modified colorfinder/testdata/white1x1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5dc6418

Please sign in to comment.