3

I am encoding raw byte to JPEG2000 using jasper library. The image produced is big endian whereas I need the output in little endian. How to specify the endianness in jasper? Here is the code snippet:

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] = '\0';

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

                jas_stream_close(pStream);
            }



        jas_image_destroy(pImage);
    }
}

I verified the endian using ImageJ. It says little endian false.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
samnaction
  • 1,194
  • 1
  • 17
  • 45
  • The JasPer library doesn’t provide any way to change the endianness. Are you able to use other libraries instead of JasPer? – Matthew Pope Dec 09 '18 at 16:50
  • @MatthewPope i have used Jasper for 8-bit image, and now I am trying for 16-bit image thats why sticking with jasper. – samnaction Dec 10 '18 at 04:07
  • I have only a casual familiarity with image encoding, so forgive me if I’m way off here, but if you have a 16 bits per color channel, can’t you just flip the byte order at some point in the process? Also, my understanding is that most jpeg libraries can read files of either Endianness. Maybe there’s something you can do so that you don’t need to use little endian jpegs. – Matthew Pope Dec 10 '18 at 05:22
  • Currently I am switching the endianness, but i have multiple tile images, and each have an byte array of size > 52000, so thinking about the performance hit. The image viewer doesnt show image properly with big endianness – samnaction Dec 10 '18 at 05:27
  • Do you have to flip the Endianness of the whole image? It seems like others have found that flipping only the Exif data is enough to fix their problem. (For example: http://u88.n24.queensu.ca/exiftool/forum/index.php?topic=3539.0 ). The exif data is usually one or more orders of magnitude smaller than the rest of the file, so your potential performance hit is much smaller if that will work for you. – Matthew Pope Dec 10 '18 at 06:02
  • You meant to say to use exifTool to convert endianness ? – samnaction Dec 10 '18 at 08:59
  • I mean maybe you could, or maybe you could find the exif data in your output file programmatically and switch it. I don’t know enough about your use case to say whether exifTool is a viable option. You could at least use it to experiment and see if the whole image needs to switch endianness or just the exif data. I don’t know how your image viewer is implemented, but this should be a pretty straight forward thing to experiment and find out. – Matthew Pope Dec 10 '18 at 14:20
  • What language are you using? You should add a language tag if you want to get noticed. Not many people follow the tags you used. People tend to filter their question list based on programming language. – Cris Luengo Dec 12 '18 at 16:42
  • C++, added the tag – samnaction Dec 13 '18 at 04:43

1 Answers1

3

How to specify the endianness in jasper?

You cannot.

Neither its documentation mentions anything on that, nor its src contains anything related.

You can switch the endianness manually, which may come with an additional performance overhead (which even if the library supported that feature, you would have to cope with it anyway).

However, as @MatthewPope mentioned, you could try flipping only the Exif data (read more in How can I change the endianness of my file with exiftool?), like this for example:

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

This approach will be significantly faster than the above mentioned, since the size of the Exif data is at least one order of magnitude smaller than the whole file most of the times.

Wikipedia states that Exif metadata are restricted in size to 64 kB in JPEG images, which, if true, is ~812 times less than the image sizes you are handling.

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

gsamaras
  • 71,951
  • 46
  • 188
  • 305