16 bit images

Apr 11, 2010 at 10:44 AM

Hi, Im trying to compress 16bit values and there seems to be some issue.


I am supplying the encoder an array of say 10,000 short values. The compressed data consists of 1400 16 bit values.


When I use the compressed data to decompress ... the returned array contains only half the number of values correctly decompressed and the rest is a random value. In above example, there will be first 5000 values correctly decompressed and the rest 5000 is a repeating number(52685).


Any idea why this is happening ?



Apr 13, 2010 at 9:22 PM

It could be that you are supplying the count of pixels as the buffersize to the encode/decode functions, where the library expects counts of bytes.

In case of 10000 16 bit pixels, the buffersize should be 20000. This holds for both encode and decode. 

There are of course lots of other things that could go wrong, but for that I'd need more detail.

Apr 19, 2010 at 7:16 AM

Yeah that was the stupid mistake. Thanks.

Another question: I have a array of short values (16 bits). My data is 12 bit but I use the short array to store this data. After compression, I expected to have values within the 12 bit range since the data is 12 bit. But I noticed some of the values in the compressed data were larger.

Is this normal for JpegLS compression to have values larger than the original data range ?


If I use a 16bit data buffer but specify the bitspersample in JlsParams as 12, i get a runtime error saying " Assertion failed .. 

file: c:/charls/defaulttraits.h

line: 85

Expression: abs(Errval) <=RANGE "


How do I deal with this ?


Apr 19, 2010 at 9:17 PM

If you set the bitspersample correct, that should not happen. (and never in lossless mode, anyway).

The current implementation requires that the unused bits are 0's. Could you make sure they are? I would guess that to be the cause of the assert going off.

Yeah I should fix this, I know.

Apr 19, 2010 at 9:45 PM

I will confirm that and get back to you.

Still, the compression ratio will not change if the bitspersample is 12 or 16 right ? because ultimately the pixel values are not changing. (pls correct me if im wrong)

So is it normal to expect larger compressed values than the largest pixel value ? 


Apr 20, 2010 at 12:01 AM

Heres what I'm doing.


My threshold is 512. so bits per sample will be 9.

I put all the pixels below or equal to 512 in a short array. So even though each value is expressed using 16 bits, im sure it is less than or equal to 512 and hence higher bits must be zero only.


for bitspersample = 9,10,11 ... I get the same assertion failed as above 

" Assertion failed .. 

file: c:/charls/defaulttraits.h

line: 85

Expression: abs(Errval) <=RANGE "


for bitspersample = 12 .. I get a new error

" Assertion failed ..

file: c:/charls/scan.h

line: 289

Expression: traits.IsNear(traits.ComputeReconstructedSample(Px, ApplySign(Errval, sign)), x)  "


for bitspersample = 13 ... everything works fine. Although now the original and the decompressed values dont match. I guess the reason could be that im reading 16bit values after decompression whereas maybe I should be reading 13bits at a time and constructing values. (are the bits accumulated to construct the decompressed data ?)


I'm puzzled. The input data is definitely 9 bit. Thanks for your help on this, really appreciate it!