Kitty 图形协议仅在 kitty 中不起作用

发布于 2025-01-21 00:24:59 字数 1374 浏览 1 评论 0原文

我有AC程序,应该使用 nofollow noreferrer“> kitty graphics protable 并利用 ImageMagick 7

我从我的代码中创建了这个最小示例。请注意,错误检查和释放内存的错误是为了简单性:

#include <stdint.h>
#include <stdio.h>

#include <MagickCore/MagickCore.h>

int main()
{
    ExceptionInfo* exceptionInfo = AcquireExceptionInfo();
    
    ImageInfo* imageInfoIn = AcquireImageInfo();
    CopyMagickString(imageInfoIn->filename, "./image.jpg", 12);

    Image* image = ReadImage(imageInfoIn, exceptionInfo);
    
    ImageInfo* imageInfoOut = AcquireImageInfo();
    CopyMagickString(imageInfoOut->magick, "RGBA", 5);

    size_t length;
    void* blob = ImageToBlob(imageInfoOut, image, &length, exceptionInfo);
    blob = Base64Encode(blob, length, &length);

    printf("\033_Ga=T,f=32,s=%u,v=%u;", image->columns, image->rows);
    fwrite(blob, sizeof(char), length, stdout);
    puts("\033\\");

    return 0;
}

此代码在Konsole,Wezterm和Wayst(显示图像)中的工作正常,但在Kitty本身中,它只是将RGBA数据的Base64编码作为文本打印。

Kitty +小猫ICAT路径/to/lose/image.png正常工作,因此终端支持正在工作。

我想让它在凯蒂(Kitty)也有什么作用?

I have a c program, that should print an image to a terminal, using the Kitty graphics protocol and utilizing ImageMagick 7.

I created this minimal example from the code i have. Note that error checks and memory freeing is missing for simplicity:

#include <stdint.h>
#include <stdio.h>

#include <MagickCore/MagickCore.h>

int main()
{
    ExceptionInfo* exceptionInfo = AcquireExceptionInfo();
    
    ImageInfo* imageInfoIn = AcquireImageInfo();
    CopyMagickString(imageInfoIn->filename, "./image.jpg", 12);

    Image* image = ReadImage(imageInfoIn, exceptionInfo);
    
    ImageInfo* imageInfoOut = AcquireImageInfo();
    CopyMagickString(imageInfoOut->magick, "RGBA", 5);

    size_t length;
    void* blob = ImageToBlob(imageInfoOut, image, &length, exceptionInfo);
    blob = Base64Encode(blob, length, &length);

    printf("\033_Ga=T,f=32,s=%u,v=%u;", image->columns, image->rows);
    fwrite(blob, sizeof(char), length, stdout);
    puts("\033\\");

    return 0;
}

This code works perfectly fine in Konsole, WezTerm and Wayst (displays the image), but in kitty itself it just prints the base64 encoding of the RGBA data as text.

kitty +kitten icat path/to/some/image.png works fine, so terminal support is working.

What am i missing to make it work in kitty too?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请别遗忘我 2025-01-28 00:24:59

问题是,kitty 一次只允许发送 4096 字节的图像数据。所以你需要将它分成多个转义码才能使用它。

上面的程序修改为在 kitty 中工作也如下所示:

#include <stdint.h>
#include <stdio.h>

#include <MagickCore/MagickCore.h>

static void printChunc(void** blob, size_t* length, uint32_t chunkSize)
{
    size_t toWrite = chunkSize < *length ? chunkSize : *length;
    printf("\033_Gm=%i;", toWrite == chunkSize ? 1 : 0); 
    fwrite(*blob, sizeof(char), toWrite, stdout);
    fputs("\033\\", stdout);
    *blob = ((char*) *blob) + toWrite;
    *length -= toWrite;
}

int main()
{
    ExceptionInfo* exceptionInfo = AcquireExceptionInfo();
    
    ImageInfo* imageInfoIn = AcquireImageInfo();
    CopyMagickString(imageInfoIn->filename, "./image.jpg", 12);

    Image* image = ReadImage(imageInfoIn, exceptionInfo);
    
    ImageInfo* imageInfoOut = AcquireImageInfo();
    CopyMagickString(imageInfoOut->magick, "RGBA", 5);

    size_t length;
    void* blob = ImageToBlob(imageInfoOut, image, &length, exceptionInfo);
    blob = Base64Encode(blob, length, &length);

    printf("\033_Ga=T,f=32,s=%u,v=%u,m=1;\033\\", image->columns, image->rows);
    while(length > 0)
        printChunc(&blob, &length, 4096);
    putchar('\n');

    return 0;
}

注意 m= 键,告诉 kitty 是否有更多数据到来。

感谢 Reddit 上的 u/aumerlex 帮助解决这个问题。

The problem was, that kitty allows only 4096 bytes of image data to be sent at once. So you need to split it up in multiple escape codes to use it.

The program above modified to work in kitty too looks like this:

#include <stdint.h>
#include <stdio.h>

#include <MagickCore/MagickCore.h>

static void printChunc(void** blob, size_t* length, uint32_t chunkSize)
{
    size_t toWrite = chunkSize < *length ? chunkSize : *length;
    printf("\033_Gm=%i;", toWrite == chunkSize ? 1 : 0); 
    fwrite(*blob, sizeof(char), toWrite, stdout);
    fputs("\033\\", stdout);
    *blob = ((char*) *blob) + toWrite;
    *length -= toWrite;
}

int main()
{
    ExceptionInfo* exceptionInfo = AcquireExceptionInfo();
    
    ImageInfo* imageInfoIn = AcquireImageInfo();
    CopyMagickString(imageInfoIn->filename, "./image.jpg", 12);

    Image* image = ReadImage(imageInfoIn, exceptionInfo);
    
    ImageInfo* imageInfoOut = AcquireImageInfo();
    CopyMagickString(imageInfoOut->magick, "RGBA", 5);

    size_t length;
    void* blob = ImageToBlob(imageInfoOut, image, &length, exceptionInfo);
    blob = Base64Encode(blob, length, &length);

    printf("\033_Ga=T,f=32,s=%u,v=%u,m=1;\033\\", image->columns, image->rows);
    while(length > 0)
        printChunc(&blob, &length, 4096);
    putchar('\n');

    return 0;
}

Note the m= key, telling kitty if there is more data to come or not.

Thanks to u/aumerlex on Reddit for helping figure that out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文