Commit cb37f682 authored by Mathis Rosenhauer's avatar Mathis Rosenhauer

szip compatibility mode allows unpacking with szip

parent 647a83fc
...@@ -522,7 +522,6 @@ int ae_decode(ae_streamp strm, int flush) ...@@ -522,7 +522,6 @@ int ae_decode(ae_streamp strm, int flush)
zero_blocks--; zero_blocks--;
} }
if (state->ref) if (state->ref)
state->i = zero_blocks * strm->block_size - 1; state->i = zero_blocks * strm->block_size - 1;
else else
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#define ROS -1 #define ROS -1
#define MIN(a, b) (((a) < (b))? (a): (b)) #define MIN(a, b) (((a) < (b))? (a): (b))
#define MAX(a, b) (((a) > (b))? (a): (b))
static int m_get_block(ae_streamp strm); static int m_get_block(ae_streamp strm);
static int m_get_block_cautious(ae_streamp strm); static int m_get_block_cautious(ae_streamp strm);
...@@ -94,8 +93,8 @@ static inline void preprocess(ae_streamp strm) ...@@ -94,8 +93,8 @@ static inline void preprocess(ae_streamp strm)
int64_t theta, d, Delta; int64_t theta, d, Delta;
encode_state *state = strm->state; encode_state *state = strm->state;
/* If this is the first block between reference /* Insert reference samples into first block of Reference Sample
samples then we need to insert one. * Interval.
*/ */
if(state->in_total_blocks % strm->rsi == 0) if(state->in_total_blocks % strm->rsi == 0)
{ {
...@@ -187,6 +186,7 @@ static int m_get_block(ae_streamp strm) ...@@ -187,6 +186,7 @@ static int m_get_block(ae_streamp strm)
static int m_get_block_cautious(ae_streamp strm) static int m_get_block_cautious(ae_streamp strm)
{ {
int pad;
encode_state *state = strm->state; encode_state *state = strm->state;
do do
...@@ -197,8 +197,9 @@ static int m_get_block_cautious(ae_streamp strm) ...@@ -197,8 +197,9 @@ static int m_get_block_cautious(ae_streamp strm)
{ {
if (state->i > 0) if (state->i > 0)
{ {
/* pad block with last sample if we have /* Pad block with last sample if we have a partial
a partial block */ * block.
*/
state->in_block[state->i] = state->in_block[state->i - 1]; state->in_block[state->i] = state->in_block[state->i - 1];
} }
else else
...@@ -209,15 +210,30 @@ static int m_get_block_cautious(ae_streamp strm) ...@@ -209,15 +210,30 @@ static int m_get_block_cautious(ae_streamp strm)
state->mode = m_encode_zero; state->mode = m_encode_zero;
return M_CONTINUE; return M_CONTINUE;
} }
/* Pad last output byte with 0 bits
if user wants to flush, i.e. we got if ((strm->flags & AE_DATA_SZ_COMPAT)
all input there is. && (state->in_total_blocks % strm->rsi != 0))
{
/* If user wants szip copatibility then we
* have to pad until but not including the
* next reference sample.
*/
pad = 64 - (state->in_total_blocks % strm->rsi % 64);
state->in_total_blocks += pad;
state->zero_blocks = (pad > 4)? ROS: pad;
state->mode = m_encode_zero;
return M_CONTINUE;
}
/* Pad last output byte with 0 bits if user wants
* to flush, i.e. we got all input there is.
*/ */
emit(state, 0, state->bitp); emit(state, 0, state->bitp);
if (state->out_direct == 0) if (state->out_direct == 0)
*strm->next_out++ = *state->out_bp; *strm->next_out++ = *state->out_bp;
strm->avail_out--; strm->avail_out--;
strm->total_out++; strm->total_out++;
return M_EXIT; return M_EXIT;
} }
} }
...@@ -272,9 +288,9 @@ static inline int m_check_zero_block(ae_streamp strm) ...@@ -272,9 +288,9 @@ static inline int m_check_zero_block(ae_streamp strm)
} }
else if (state->zero_blocks) else if (state->zero_blocks)
{ {
/* The current block isn't zero but we have to /* The current block isn't zero but we have to emit a previous
emit a previous zero block first. The * zero block first. The current block will be handled
current block will be handled later. * later.
*/ */
state->block_deferred = 1; state->block_deferred = 1;
state->mode = m_encode_zero; state->mode = m_encode_zero;
...@@ -299,8 +315,9 @@ static inline int m_select_code_option(ae_streamp strm) ...@@ -299,8 +315,9 @@ static inline int m_select_code_option(ae_streamp strm)
direction = 1; direction = 1;
looked_bothways = 0; looked_bothways = 0;
/* Starting with splitting position of last block look left /* Starting with splitting position of last block look left and
and possibly right to find new minimum.*/ * possibly right to find new minimum.
*/
for (;;) for (;;)
{ {
fs_len = (state->in_block[1] >> i) fs_len = (state->in_block[1] >> i)
...@@ -331,7 +348,8 @@ static inline int m_select_code_option(ae_streamp strm) ...@@ -331,7 +348,8 @@ static inline int m_select_code_option(ae_streamp strm)
if (split_len_min < INT64_MAX) if (split_len_min < INT64_MAX)
{ {
/* We are moving towards the minimum so it cant be in /* We are moving towards the minimum so it cant be in
the other direction.*/ * the other direction.
*/
looked_bothways = 1; looked_bothways = 1;
} }
split_len_min = split_len; split_len_min = split_len;
...@@ -342,9 +360,10 @@ static inline int m_select_code_option(ae_streamp strm) ...@@ -342,9 +360,10 @@ static inline int m_select_code_option(ae_streamp strm)
if (fs_len < this_bs) if (fs_len < this_bs)
{ {
/* Next can't get better because what we lose by /* Next can't get better because what we lose by
additional uncompressed bits isn't compensated by a * additional uncompressed bits isn't compensated
smaller FS part. Vice versa if we are coming from * by a smaller FS part. Vice versa if we are
the other direction.*/ * coming from the other direction.
*/
if (looked_bothways) if (looked_bothways)
{ {
break; break;
...@@ -368,14 +387,16 @@ static inline int m_select_code_option(ae_streamp strm) ...@@ -368,14 +387,16 @@ static inline int m_select_code_option(ae_streamp strm)
else if (fs_len > this_bs) else if (fs_len > this_bs)
{ {
/* Since we started looking the other way there is no /* Since we started looking the other way there is no
need to turn back.*/ * need to turn back.
*/
break; break;
} }
} }
else else
{ {
/* Stop looking for better option if we /* Stop looking for better option if we don't see any
don't see any improvement. */ * improvement.
*/
if (looked_bothways) if (looked_bothways)
{ {
break; break;
...@@ -644,7 +665,6 @@ int ae_encode_init(ae_streamp strm) ...@@ -644,7 +665,6 @@ int ae_encode_init(ae_streamp strm)
/* Largest possible block according to specs */ /* Largest possible block according to specs */
state->out_blklen = (5 + 16 * 32) / 8 + 3; state->out_blklen = (5 + 16 * 32) / 8 + 3;
/* Output buffer */
state->out_block = (uint8_t *)malloc(state->out_blklen); state->out_block = (uint8_t *)malloc(state->out_blklen);
if (state->out_block == NULL) if (state->out_block == NULL)
{ {
......
...@@ -35,10 +35,7 @@ typedef struct internal_state { ...@@ -35,10 +35,7 @@ typedef struct internal_state {
int64_t zero_ref_sample;/* reference sample of zero block */ int64_t zero_ref_sample;/* reference sample of zero block */
int zero_blocks; /* number of contiguous zero blocks */ int zero_blocks; /* number of contiguous zero blocks */
int k; /* splitting position */ int k; /* splitting position */
int flush; int flush; /* flush option copied from argument */
#ifdef PROFILE
int *prof;
#endif
} encode_state; } encode_state;
#endif #endif
...@@ -35,7 +35,10 @@ typedef ae_stream *ae_streamp; ...@@ -35,7 +35,10 @@ typedef ae_stream *ae_streamp;
#define AE_DATA_LSB 8 #define AE_DATA_LSB 8
#define AE_DATA_MSB 16 #define AE_DATA_MSB 16
#define AE_DATA_PREPROCESS 32 /* Set if preprocessor should be used */ #define AE_DATA_PREPROCESS 32 /* Set if preprocessor should be used */
#define AE_DATA_SZ_COMPAT 256 /* Set this if you want szip to decode
* our output. Increases output
* slightly.
*/
/* Return codes of library functions */ /* Return codes of library functions */
#define AE_OK 0 #define AE_OK 0
...@@ -49,7 +52,8 @@ typedef ae_stream *ae_streamp; ...@@ -49,7 +52,8 @@ typedef ae_stream *ae_streamp;
#define AE_NO_FLUSH 0 /* Do not enforce output flushing. More #define AE_NO_FLUSH 0 /* Do not enforce output flushing. More
* input may be provided with later * input may be provided with later
* calls. So far only relevant for * calls. So far only relevant for
* encoding. */ * encoding.
*/
#define AE_FLUSH 1 /* Flush output and end encoding. The last #define AE_FLUSH 1 /* Flush output and end encoding. The last
* call to ae_encode() must set AE_FLUSH to * call to ae_encode() must set AE_FLUSH to
* drain all output. * drain all output.
...@@ -57,7 +61,8 @@ typedef ae_stream *ae_streamp; ...@@ -57,7 +61,8 @@ typedef ae_stream *ae_streamp;
* It is not possible to continue encoding * It is not possible to continue encoding
* of the same stream after it has been * of the same stream after it has been
* flushed because the last byte may be * flushed because the last byte may be
* padded with fill bits. */ * padded with fill bits.
*/
int ae_decode_init(ae_streamp strm); int ae_decode_init(ae_streamp strm);
int ae_decode(ae_streamp strm, int flush); int ae_decode(ae_streamp strm, int flush);
......
#include <stdio.h>
#include <stddef.h> #include <stddef.h>
#include "szlib.h" #include "szlib.h"
...@@ -9,7 +10,7 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, const void *source, size_ ...@@ -9,7 +10,7 @@ int SZ_BufftoBuffCompress(void *dest, size_t *destLen, const void *source, size_
strm.bit_per_sample = param->bits_per_pixel; strm.bit_per_sample = param->bits_per_pixel;
strm.block_size = param->pixels_per_block; strm.block_size = param->pixels_per_block;
strm.rsi = param->pixels_per_scanline / param->pixels_per_block; strm.rsi = param->pixels_per_scanline / param->pixels_per_block;
strm.flags = param->options_mask; strm.flags = param->options_mask | AE_DATA_SZ_COMPAT;
strm.avail_in = sourceLen; strm.avail_in = sourceLen;
strm.avail_out = *destLen; strm.avail_out = *destLen;
strm.next_out = dest; strm.next_out = dest;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment