Go to the source code of this file.
Functions | |
int | cbf_compress_packed (void *source, size_t elsize, int elsign, size_t nelem, unsigned int compression, cbf_file *file, size_t *compressedsize, int *storedbits) |
int | cbf_decompress_packed (void *destination, size_t elsize, int elsign, size_t nelem, size_t *nelem_read, unsigned int compression, cbf_file *file) |
int cbf_compress_packed | ( | void * | source, |
size_t | elsize, | ||
int | elsign, | ||
size_t | nelem, | ||
unsigned int | compression, | ||
cbf_file * | file, | ||
size_t * | compressedsize, | ||
int * | storedbits | ||
) |
Definition at line 390 of file cbf_packed.c.
References cbf_compress_nodestruct::bitcount, cbf_add_offset(), cbf_alloc(), CBF_ARGUMENT, cbf_failnez, cbf_free(), cbf_onfailnez, cbf_pack_nextchunk(), cbf_put_integer(), CBF_SHIFT63, and cbf_compress_nodestruct::count.
Referenced by cbf_compress().
{ unsigned int minelement, maxelement; unsigned int count, element, lastelement, unsign, sign, limit; unsigned char *unsigned_char_data; unsigned long bitcount, chunkbits; cbf_packed_data *data; /* Is the element size valid? */ if (elsize != sizeof (int) && elsize != sizeof (short) && elsize != sizeof (char)) return CBF_ARGUMENT; /* Allocate memory */ cbf_failnez (cbf_alloc ((void **) &data, NULL, sizeof (cbf_packed_data), 1)) data->start = 0; data->offsets = 0; /* Count the expected number of bits */ minelement = 0; maxelement = 0; /* Write the number of elements (64 bits) */ cbf_onfailnez (cbf_put_integer (file, nelem, 0, 64), cbf_free ((void **) data, NULL)) /* Write the minimum element (64 bits) */ cbf_onfailnez (cbf_put_integer (file, minelement, elsign, 64), cbf_free ((void **) data, NULL)) /* Write the maximum element (64 bits) */ cbf_onfailnez (cbf_put_integer (file, maxelement, elsign, 64), cbf_free ((void **) data, NULL)) /* Write the reserved entry (64 bits) */ cbf_onfailnez (cbf_put_integer (file, 0, 0, 64), cbf_free ((void **) data, NULL)) bitcount = 4 * 64; /* Initialise the pointers */ unsigned_char_data = (unsigned char *) source; /* Maximum limit (unsigned) is 64 bits */ if (elsize * CHAR_BIT > 64) { sign = 1 << CBF_SHIFT63; limit = ~-(sign << 1); if (storedbits) *storedbits = 64; } else { sign = 1 << (elsize * CHAR_BIT - 1); limit = ~0; if (storedbits) *storedbits = elsize * CHAR_BIT; } /* Offset to make the value unsigned */ if (elsign) unsign = sign; else unsign = 0; /* Start from 0 */ lastelement = unsign; for (count = 0; count < nelem; count++) { /* Get the next element */ if (elsize == sizeof (int)) element = *((unsigned int *) unsigned_char_data); else if (elsize == sizeof (short)) element = *((unsigned short *) unsigned_char_data); else element = *unsigned_char_data; unsigned_char_data += elsize; /* Make the element unsigned */ element += unsign; /* Limit the value to 64 bits */ if (element > limit) if (elsign && (int) (element - unsign) < 0) element = 0; else element = limit; /* Add the offset to the buffer */ cbf_add_offset (data, element, lastelement); /* Is the buffer full? */ if (data->offsets == 128) { /* Write the next block as economically as possible */ cbf_onfailnez (cbf_pack_nextchunk (data, file, &chunkbits), cbf_free ((void **) data, NULL)) bitcount += chunkbits; } /* Update the previous element */ lastelement = element; } /* Flush the buffers */ while (data->offsets > 0) { cbf_onfailnez (cbf_pack_nextchunk (data, file, &chunkbits), cbf_free ((void **) data, NULL)) bitcount += chunkbits; } /* Return the number of characters written */ if (compressedsize) *compressedsize = (bitcount + 7) / 8; /* Free memory */ return cbf_free ((void **) &data, NULL); }
int cbf_decompress_packed | ( | void * | destination, |
size_t | elsize, | ||
int | elsign, | ||
size_t | nelem, | ||
size_t * | nelem_read, | ||
unsigned int | compression, | ||
cbf_file * | file | ||
) |
Definition at line 594 of file cbf_packed.c.
References bits(), CBF_ARGUMENT, cbf_failnez, cbf_get_bits(), cbf_get_integer(), CBF_SHIFT63, cbf_compress_nodestruct::count, and cbf_compress_nodestruct::next.
Referenced by cbf_decompress().
{ unsigned int next, pixel, pixelcount; unsigned int bits, element, sign, unsign, limit, count64, count; unsigned char *unsigned_char_data; unsigned int offset [4], last_element [4]; int errorcode; /* Is the element size valid? */ if (elsize != sizeof (int) && elsize != sizeof (short) && elsize != sizeof (char)) return CBF_ARGUMENT; /* Initialise the pointer */ unsigned_char_data = (unsigned char *) destination; /* Maximum limit (unsigned) is 64 bits */ if (elsize * CHAR_BIT > 64) { sign = 1 << CBF_SHIFT63; limit = ~-(sign << 1); } else { sign = 1 << (elsize * CHAR_BIT - 1); if (elsize == sizeof (int)) limit = ~0; else limit = ~-(1 << (elsize * CHAR_BIT)); } /* Offset to make the value unsigned */ if (elsign) unsign = sign; else unsign = 0; /* How many ints do we need to hold 64 bits? */ count64 = (64 + sizeof (int) * CHAR_BIT - 1) / (sizeof (int) * CHAR_BIT); /* Initialise the first element */ last_element [0] = unsign; for (count = 1; count < count64; count++) last_element [count] = 0; /* Discard the reserved entry (64 bits) */ cbf_failnez (cbf_get_integer (file, NULL, 0, 64)) /* Read the elements */ count = 0; while (count < nelem) { /* Get the next 6 bits of data */ errorcode = cbf_get_integer (file, (int *) &next, 0, 6); if (errorcode) { if (nelem_read) *nelem_read = count + pixel; return errorcode; } /* Decode bits 0-5 */ pixelcount = 1 << (next & 7); bits = cbf_packed_bits [(next >> 3) & 7]; /* Read the offsets */ if (pixelcount + count > nelem) pixelcount = nelem - count; for (pixel = 0; pixel < pixelcount; pixel++) { /* Read an offset */ if (bits) { errorcode = cbf_get_bits (file, (int *) offset, bits); if (errorcode) { if (nelem_read) *nelem_read = count + pixel; return errorcode; } /* Update the current element */ last_element [0] += offset [0]; } element = last_element [0]; /* Limit the value to fit the element size */ if (element > limit) if (elsign && (int) (element - unsign) < 0) element = 0; else element = limit; /* Make the element signed? */ element -= unsign; /* Save the element */ if (elsize == sizeof (int)) *((unsigned int *) unsigned_char_data) = element; else if (elsize == sizeof (short)) *((unsigned short *) unsigned_char_data) = element; else *unsigned_char_data = element; unsigned_char_data += elsize; } count += pixelcount; } /* Number read */ if (nelem_read) *nelem_read = count; /* Success */ return 0; }