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.
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?