如何在 Jasper JPEG2000 中指定小端

How to specify little endian in Jasper JPEG2000

我正在使用 jasper 库将原始字节编码为 JPEG2000。生成的图像是大端,而我需要小端输出。如何指定碧玉中的字节顺序?这是代码片段:

EncodeAsJPEG2000(array<Byte> ^inputImage, array<Byte> ^outputImage, uint32 width, uint32 height, uint32 size)
{
    jas_init();
    jas_image_t *pImage;
    pImage = jas_image_create0();
    pin_ptr<Byte> pInput = &inputImage[0];

    int totalCopied = 0;
    if (pImage)
    {
        tsize_t bytesperline = 2;
        int iCmp = 0;

            jas_stream_t *pStream;
            jas_image_cmptparm_t cmptparm;
            cmptparm.tlx = 0;
            cmptparm.tly = 0;
            cmptparm.hstep = 1;
            cmptparm.vstep = 1;
            cmptparm.width = width;
            cmptparm.height = height;
            cmptparm.prec = 16;
            cmptparm.sgnd = false;
            jas_image_addcmpt(pImage, iCmp, &cmptparm);


            //jas_image_setcmpttype(pImage, 0, JAS_IMAGE_CT_GRAY_Y);

            pImage->clrspc_ = JAS_CLRSPC_SGRAY;         /* grayscale Image */
            pImage->cmprof_ = 0;

            jas_stream_seek(pImage->cmpts_[iCmp]->stream_, 0, SEEK_SET);
            jas_stream_write(pImage->cmpts_[iCmp]->stream_, pInput, size);


            pStream = jas_stream_fopen("C:\jaspimage.jp2" , "w+b");
            int copied = 0;
            if (pStream)
            {
                char optionsString[128];
                optionsString[0] = '[=10=]';

                int format = jas_image_strtofmt("jp2");
                jas_image_encode(pImage, pStream, format, "rate=1.0");

                jas_stream_close(pStream);
            }



        jas_image_destroy(pImage);
    }
}

我使用 ImageJ 验证了字节序。它说小端错误。

How to specify the endianness in jasper?

不能

它的 documentation mentions anything on that, nor its src 都不包含任何相关内容。

您可以手动切换字节序,这可能会带来额外的性能开销(即使该库支持该功能,您也必须应对它)。

但是,正如@MatthewPope 提到的,您可以尝试仅翻转 Exif 数据(在 How can I change the endianness of my file with exiftool? 中阅读更多内容),例如:

exiftool -all= -tagsfromfile test.jpg -all:all -unsafe -exifbyteorder=little-endian test.jpg

这种方法比上面提到的要快得多,因为 Exif 数据的大小在大多数情况下至少比整个文件小一个数量级。

Wikipedia 声明 Exif 元数据在 JPEG 图像中的大小限制为 64 kB,如果属实,则比您正在处理的图像大小小约 812 倍。

ExifTool can be used for editing meta information in an image. Read this interesting question too: How does JPEG endianness matter on coding?