Skip to content

refactor: replace sharp with jimp#17

Open
LeoMiguelB wants to merge 7 commits intoendernoke:mainfrom
LeoMiguelB:201-integrate-jimp
Open

refactor: replace sharp with jimp#17
LeoMiguelB wants to merge 7 commits intoendernoke:mainfrom
LeoMiguelB:201-integrate-jimp

Conversation

@LeoMiguelB
Copy link

No description provided.

removal of sharp to use jimp is done

Completely removed usage of sharp to rely on jimp with uses no native
modules.

rebase changes
import { Jimp } from "jimp";

// NOTE: jimp docs say the read return type JIMP but ts says otherwise. They haven't fully ironed out their types, once they do we can remove this
export type JimpImg = Awaited<ReturnType<typeof Jimp.read>>;
Copy link
Author

Choose a reason for hiding this comment

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

Curious to hear what folks this on this. Might be anti pattern, but i'm not sure.

Copy link
Owner

Choose a reason for hiding this comment

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

Lmao I love monkey patching. If it works it works I guess...

Copy link
Author

Choose a reason for hiding this comment

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

Lmao I love monkey patching. If it works it works I guess...

I removed this in favor of a hopefully okay hard cast inside utils/image.ts

Copy link
Author

Choose a reason for hiding this comment

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

I saw a couple places that used the same types, hence I thought putting it into it's own type file was better. Let me know if any objections.

const img = await Jimp.read(src);
return img;
} catch (error) {
// TODO: LEO: wondering if it's possible to intgrate logger from instagram-cli into here
Copy link
Author

Choose a reason for hiding this comment

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

Form what i've seen there's no logger implementation in this codebase. let me know if i'm missed somewhting. Otherise I think this could be something good to add just like in instagram-cli

Copy link
Owner

Choose a reason for hiding this comment

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

Nope there's no logging. I think it'd be nice to have a onError prop you can pass to <Image> so the logging can be easily integrated with projects downstream. Lmk what you think

(currentProtocol: ImageProtocolName): ImageProtocolName => {
switch (currentProtocol) {
case "kitty":
return "iterm2";
Copy link
Author

Choose a reason for hiding this comment

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

not sure why iterm2 was hardcoded here, but i changed it to kitty since we already implemntation for it. Let me know if I'm stepping on anyones toes here.

Copy link
Owner

Choose a reason for hiding this comment

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

This function is supposed to get the next best protocol when the current protocol isn't supported, so kitty --> iterm

@LeoMiguelB LeoMiguelB mentioned this pull request Dec 29, 2025
Copy link
Owner

@endernoke endernoke left a comment

Choose a reason for hiding this comment

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

Thanks for the work 😄

When I test it the colors are garbled, I think it's an issue in the to<ProtocolName> functions:

Image

You could reference a working implementation of reading individual pixel colors from jimp here:
https://github.com/sindresorhus/terminal-image/blob/a2d012f48748d2ef01e4e927125eb4dfc18071c9/index.js#L73

"\x1b]1337;File=" +
`size=${info.size};` +
// optional, only used for showing progress
// `size=${size};` +
Copy link
Owner

Choose a reason for hiding this comment

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

size is actually needed here despite the iTerm docs because xterm.js (and perhaps some other terminal emulators) need it, see also xtermjs/xterm.js#5326

src/index.ts Outdated
// export { default as BrailleImage } from "./components/image/Braille.js";
// export { default as HalfBlockImage } from "./components/image/HalfBlock.js";
// export { default as SixelImage } from "./components/image/Sixel.js";
//
Copy link
Owner

Choose a reason for hiding this comment

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

I've realized the old exports are unnecessary 😂 Could you just delete these lines thanks

.raw()
.toBuffer({ resolveWithObject: true });
// resized in place
image.scaleToFit({ w: width, h: height });
Copy link
Owner

Choose a reason for hiding this comment

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

Why did you use scaleToFit here but resize elsewhere?

Copy link
Author

Choose a reason for hiding this comment

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

Why did you use scaleToFit here but resize elsewhere?

Had to sanity check. I was trying match one to one from prev implementation, but it makes more sense to use scaleToFit for all to avoid distortion. It's changed now.

@LeoMiguelB LeoMiguelB closed this Dec 29, 2025
@LeoMiguelB LeoMiguelB reopened this Dec 29, 2025
@LeoMiguelB
Copy link
Author

Thanks for the work 😄

When I test it the colors are garbled, I think it's an issue in the to<ProtocolName> functions:

Image You could reference a working implementation of reading individual pixel colors from jimp here: https://github.com/sindresorhus/terminal-image/blob/a2d012f48748d2ef01e4e927125eb4dfc18071c9/index.js#L73

Thanks for the work 😄

When I test it the colors are garbled, I think it's an issue in the to<ProtocolName> functions:

Image You could reference a working implementation of reading individual pixel colors from jimp here: https://github.com/sindresorhus/terminal-image/blob/a2d012f48748d2ef01e4e927125eb4dfc18071c9/index.js#L73

@endernoke Half block is fixed with my recent commit. I still need to figure out out why ascii and braille is acting out. Thanks again for the callout, will update ya'll.
screenshot_2025-12-29_15-18-04

in rgba the maximum instenstiy across all channels is 255, hence we had
to normalize against that by dividing by 255
Fixed resizing and fitting of images.
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