8000 feat(js-ico): read and write ICO files by hipstersmoothie · Pull Request #1519 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content
< 8000 script type="application/json" data-target="react-partial.embeddedData">{"props":{"contextRegion":{"crumbs":[{"crumb_type":"organization","label":"jimp-dev","is_root":false,"href":"/jimp-dev"},{"crumb_type":"repository","label":"jimp","is_root":false,"href":"/jimp-dev/jimp"}],"localNavigation":[{"id":"code","icon":"code","label":"Code","href":"/jimp-dev/jimp","selectedLinks":["repo_source","repo_downloads","repo_commits","repo_releases","repo_tags","repo_branches","repo_packages","repo_deployments","repo_attestations"],"popoverTarget":false,"commandId":"repositories:go-to-code","reactNav":{"appTarget":"code-view","anchor":"code-view-repo-link"},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"issues","icon":"issue-opened","label":"Issues","href":"/jimp-dev/jimp/issues","selectedLinks":["repo_issues","repo_labels","repo_milestones"],"count":0,"popoverTarget":false,"commandId":"repositories:go-to-issues","reactNav":{"appTarget":"repo","anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"pull-requests","icon":"git-pull-request","label":"Pull requests","href":"/jimp-dev/jimp/pulls","selectedLinks":["repo_pulls","checks"],"count":1,"popoverTarget":false,"commandId":"repositories:go-to-pull-requests","reactNav":{"appTarget":null,"anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"discussions","icon":"comment-discussion","label":"Discussions","href":"/jimp-dev/jimp/discussions","selectedLinks":["repo_discussions"],"popoverTarget":false,"commandId":"repositories:go-to-discussions","reactNav":{"appTarget":null,"anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"actions","icon":"play","label":"Actions","href":"/jimp-dev/jimp/actions","selectedLinks":["repo_actions"],"popoverTarget":false,"commandId":"repositories:go-to-actions","reactNav":{"appTarget":"repo","anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"projects","icon":"table","label":"Projects","href":"/jimp-dev/jimp/projects","selectedLinks":["repo_projects","new_repo_project","repo_project"],"popoverTarget":false,"commandId":"repositories:go-to-projects","reactNav":{"appTarget":"repo","anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"wiki","icon":"book","label":"Wiki","href":"/jimp-dev/jimp/wiki","selectedLinks":["repo_wiki"],"popoverTarget":false,"commandId":"repositories:go-to-wiki","reactNav":{"appTarget":null,"anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"security-and-quality","icon":"shield","label":"Security and quality","href":"/jimp-dev/jimp/security","selectedLinks":["security","overview","alerts","policy","token_scanning","code_scanning"],"count":0,"popoverTarget":false,"commandId":"repositories:go-to-security","reactNav":{"appTarget":null,"anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}},{"id":"insights","icon":"graph","label":"Insights","href":"/jimp-dev/jimp/insights","selectedLinks":["repo_graphs","repo_contributors","dependency_graph","dependabot_updates","pulse","people","community"],"popoverTarget":false,"commandId":"repositories:go-to-insights","reactNav":{"appTarget":null,"anchor":null},"turboNav":{"frame":"repo-content-turbo-frame"}}],"localNavigationUpdateChannel":null,"selectedLink":"repo_pulls"},"owner":null,"headerLogo":{"href":"/","aria-label":"Homepage "},"notifications":{"indicatorMode":"disabled","websocketChannel":null,"fetchIndicatorSrc":"/notifications/indicator","fetchIndicatorEnabled":false},"issues":{"href":"/issues"},"pulls":{"href":"/pulls"},"contributedRepos":{"href":"/repos"},"copilot":{"show":false,"showAgentsButton":false,"showRelaunchAnnouncement":false,"licenseGrantNudge":null,"copilotChatUrl":null,"copilotApiUrl":"https://api.githubcopilot.com"},"search":{"show":true,"showCommandPalette":false,"isSearchPage":false,"isJumpToSearch":false,"searchContext":{"scope":"repo:jimp-dev/jimp","current_repo_name":"jimp","current_repo_nwo":"jimp-dev/jimp","current_repo_org":"jimp-dev","user_id":"jimp-dev"}},"commandPalette":null,"enterpriseBar":{"show":false},"globalTransactionalMessage":[],"ipmNudgeDelivery":null,"payloadsUrl":"/_global-navigation/payloads.json?can_toggle_site_admin_and_employee_status=0\u0026is_admin_mode_on=0\u0026is_ui_opted_out=0\u0026show_ui_opt_out=0\u0026v=9","contextRegionUrl":null}}

feat(js-ico): read and write ICO files - #1519

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-ico-format-plugin-691
Nov 23, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-ico-format-plugin-691

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Fixes #691

What was wrong

ICO files couldn't be read or written:

await Jimp.read("favicon.ico");
// Error: Mime type image/x-icon does not support decoding

await image.write("favicon.ico");
// Error: Cannot write "favicon.ico": the path has no recognisable file extension ...

file-type already detects ICO and CUR buffers as image/x-icon, but no format handled that type. Also, mime/lite has no mapping for .ico: the registered type image/vnd.microsoft.icon is only in mime's full database, and image/x-icon isn't registered.

What changed

New package @jimp/js-ico (pure JS, made with pnpm turbo gen plugin and set up like js-bmp):

  • Decode parses the ICONDIR. Each entry is either an embedded PNG, decoded with @jimp/js-png, or a headerless BMP. BMP entries go through bmp-ts with a 14-byte file header added and the doubled height halved. For 32-bit entries the 4th byte is used as alpha (bmp-ts ignores it without an alpha mask). For 1/4/8/16/24-bit entries, and 32-bit entries whose alpha is all 0, the 1-bit AND mask becomes alpha. A missing mask means opaque. CUR files (type 2) work the same way.
  • Default: decodes the largest entry (width×height, then bit depth).
  • Decode options: { index } picks an entry by position, and { size } picks the smallest entry whose longest side is ≥ size (largest if none is), preferring higher bit depth.
  • readIcoEntries(buffer) lists { index, width, height, bitDepth, format: "png" | "bmp" }. Width, height and bit depth come from the embedded PNG/BMP, not the directory bytes, which can't hold sizes over 256 and often say 0 bpp.
  • Encode writes the image as one 32-bit RGBA PNG entry (older Windows only reads RGBA PNGs in icons). Images over 256 px are scaled down to fit. sizes: [16, 32, 48, 256] writes one entry per size, each scaled with plugin-resize to fit in a size×size square with the aspect ratio kept. Sizes are rounded, clamped to 1–256 and deduplicated. A width or height of 256 is stored as 0. The source image's bitmap isn't modified.
  • MIME image/x-icon (default export) and image/vnd.microsoft.icon (msIcon, like msBmp).
  • Clear errors for bad input: Invalid ICO file: it doesn't start with an ICO header, ... unknown image type N, ... it contains no images, ... the directory of N images is truncated, ... image N is past the end of the file, ... image N is truncated, Unsupported BMP bit depth N, and ICO image index N is out of range, the file has M images.

core: write("x.ico") now maps to image/x-icon, through a small Mime instance used as a fallback for extensions mime/lite doesn't know. On the type level, .ico paths take the ICO encode options.

jimp: ico and msIcon are added to defaultFormats, and JimpMime.ico is added.

Why it's in defaultFormats

ICO is common (favicons), and the plugin reuses decoders the defaults already bundle (pngjs through js-png, bmp-ts, plugin-resize). The only new code is the ICO container itself. The jimp browser bundle grows by about 3.7 KB minified, 1.5 KB gzipped (756,593 → 760,336 bytes measured before the rebase). Custom builds can still add it with createJimp({ formats: [..., ico] }).

Tests

  • Crafted in the test: PNG entry, 32-bit BMP with alpha, 32-bit with all-zero alpha (uses the mask), 24/8/4/1-bit BMPs with AND masks (odd widths so rows are padded), BMP without a mask, CUR (type 2), multi-entry picking (largest by default, ties broken by bit depth, index, size, out-of-range index), and garbage/truncated input.
  • A 2.7 KB ImageMagick-written ICO fixture (24×24 32-bit and 16×16 4-bit+mask) decodes pixel-for-pixel the same as ImageMagick's own decoding of each entry.
  • Encode: single-image round trip (lossless including alpha, directory bytes checked), image/vnd.microsoft.icon output, multi-size (16/32/48/256, 256 stored as 0, source not modified), aspect ratio/clamping/dedup, and images over 256 px scaled down.
  • jimp: write("favicon.ico", { sizes }) reads back as image/x-icon. Type tests cover .ico write options and image/x-icon decode options.
  • Outside the tests, I checked that ImageMagick (magick identify) and macOS sips both read the multi-size ICOs we write.

🤖 Generated with Claude Code

Add @jimp/js-ico, a pure JS format for image/x-icon (and
image/vnd.microsoft.icon):

- Reads ICO and CUR files with PNG or BMP images. BMP images are
  decoded with bmp-ts, using their 32 bit alpha or their 1 bit mask.
  The largest image is decoded by default; the `index` and `size`
  decode options pick another, and `readIcoEntries` lists them.
- Writes the image as a PNG inside the ICO, or one per size with the
  `sizes` option.

Add it to jimp's default formats, and map the .ico extension to
image/x-icon in write(), which mime/lite doesn't know.

Fixes #691

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added enhancement a request for a new feature or change in behavior minor Increment the minor version when merged labels Nov 23, 2026
@hipstersmoothie
hipstersmoothie merged commit b9eb8fc into main Nov 23, 2026
6 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/jimp-ico-format-plugin-691 branch November 23, 2026 20:51
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.0.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement a request for a new feature or change in behavior minor Increment the minor version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

support image/x-icon mime types

1 participant

0