Repository navigation
feat(js-ico): read and write ICO files - #1519
Merged
Merged
Conversation
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>
Collaborator
Author
|
🚀 PR was released in |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #691
What was wrong
ICO files couldn't be read or written:
file-type already detects ICO and CUR buffers as
image/x-icon, but no format handled that type. Also,mime/litehas no mapping for.ico: the registered typeimage/vnd.microsoft.iconis only in mime's full database, andimage/x-iconisn't registered.What changed
New package
@jimp/js-ico(pure JS, made withpnpm turbo gen pluginand set up likejs-bmp):@jimp/js-png, or a headerless BMP. BMP entries go throughbmp-tswith 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.{ 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.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.image/x-icon(default export) andimage/vnd.microsoft.icon(msIcon, likemsBmp).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, andICO image index N is out of range, the file has M images.core:
write("x.ico")now maps toimage/x-icon, through a smallMimeinstance used as a fallback for extensionsmime/litedoesn't know. On the type level,.icopaths take the ICO encode options.jimp:
icoandmsIconare added todefaultFormats, andJimpMime.icois added.Why it's in
defaultFormatsICO 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
index,size, out-of-range index), and garbage/truncated input.image/vnd.microsoft.iconoutput, multi-size (16/32/48/256, 256 stored as 0, source not modified), aspect ratio/clamping/dedup, and images over 256 px scaled down.write("favicon.ico", { sizes })reads back asimage/x-icon. Type tests cover.icowrite options andimage/x-icondecode options.magick identify) and macOSsipsboth read the multi-size ICOs we write.🤖 Generated with Claude Code