static void convertRow(const bit * const bitrow, unsigned int const cols, bool const pack, bool const delta, bool * const rowIsBlankP) { unsigned int rightmostBlackCol; findRightmostBlackCol(bitrow, cols, rowIsBlankP, &rightmostBlackCol); if (!*rowIsBlankP) { unsigned int const nzcol = rightmostBlackCol + 1; /* Number of columns excluding white right margin */ unsigned int const rucols = ((nzcol + 7) / 8) * 8; /* 'nzcol' rounded up to nearest multiple of 8 */ unsigned int col; memset(rowBuffer, 0, rowBufferSize); rowBufferIndex = 0; /* Generate the unpacked data */ for (col = 0; col < nzcol; ++col) putbit(bitrow[col]); /* Pad out to a full byte with white */ for (col = nzcol; col < rucols; ++col) putbit(0); putflush(); /* Try optional compression algorithms */ if (pack) packbits(); else packBufferIndex = rowBufferIndex + 999; if (delta) { /* May need to temporarily bump the row buffer index up to whatever the previous line's was - if this line is shorter than the previous would otherwise leave dangling cruft. */ unsigned int const savedRowBufferIndex = rowBufferIndex; if (rowBufferIndex < prevRowBufferIndex) rowBufferIndex = prevRowBufferIndex; deltarow(); rowBufferIndex = savedRowBufferIndex; } else deltaBufferIndex = packBufferIndex + 999; } }
int main(int argc, const char *argv[]) { FILE * ifP; bit * bitrow; int rows; int cols; int format; unsigned int padright; unsigned int row; const char * inputFile; pm_proginit(&argc, argv); if (argc-1 < 1) inputFile = "-"; else { inputFile = argv[1]; if (argc-1 > 2) pm_error("Too many arguments. The only argument is the optional " "input file name"); } ifP = pm_openr(inputFile); pbm_readpbminit(ifP, &cols, &rows, &format); if (rows > INT16MAX || cols > INT16MAX) pm_error("Input image is too large."); bitrow = pbm_allocrow(cols); /* Compute padding to round cols up to the nearest multiple of 16. */ padright = ((cols + 15) / 16) * 16 - cols; putinit(cols, rows); for (row = 0; row < rows; ++row) { unsigned int col; pbm_readpbmrow(ifP, bitrow, cols, format); for (col = 0; col < cols; ++col) putbit(bitrow[col]); for (col = 0; col < padright; ++col) putbit(0); } if (ifP != stdin) fclose(ifP); putrest(); return 0; }
void putbits(int ch, int n, int msb) { int j; n %= sizeof(ch) * 8; for(j=0; j<n; j++) { if(msb) putbit(BIT(ch, j)); else putbit(BIT(ch, n-j-1)); } }
// write an n-bit (bits) number (val) to the output steam static void putval(int val, int bits) { int i; if (bits <= 0) return; for (i = 0; i < bits; i++) putbit(val & (1 << i)); }
// Puts integer n in b-bit representation into file_out void Bitstream::putint(int n, int b, ofstream &file_out){ //assert( n <= pow(2, b)); //cout << "Bit rep is: " << b << endl; for(int i=0; i<b; i++) { int mask = mask_one << (b-1); int bit = (mask == ((unsigned int) n & mask )); //cout << "Putting bit " << bit << endl; putbit(bit , file_out); n = n << 1; } }
int main( int argc, char* argv[]) { FILE* ifp; bit* bitrow; int rows, cols, format, row, col; pbm_init( &argc, argv ); if ( argc > 2 ) pm_usage( "[pbmfile]" ); if ( argc == 2 ) ifp = pm_openr( argv[1] ); else ifp = stdin; pbm_readpbminit( ifp, &cols, &rows, &format ); if( rows>INT16MAX || cols>INT16MAX ) pm_error ("Input image is too large."); bitrow = pbm_allocrow( cols ); putinit (rows, cols); for ( row = 0; row < rows; ++row ) { #ifdef DEBUG fprintf (stderr, "row %d\n", row); #endif pbm_readpbmrow( ifp, bitrow, cols, format ); for ( col = 0; col < cols; ++col ) putbit( bitrow[col] ); putrow( ); } flushrow (); pm_close( ifp ); exit( 0 ); }
/* comp compress plen 1-bit pixels from src to dest */ int compress_rle(const uint8_t *src, int w, int h, char *prefix, char *suffix) { int pos; int rlen; int len; printf("const uint8_t PROGMEM %s%s[] = {", prefix, suffix); fflush(stdout); memset(&cs, 0, sizeof(cs)); cs.src = src; cs.bit = 1; cs.w = w; cs.h = h; // header putval(w-1, 8); putval(h-1, 8); putval(getcol(0), 1); // first colour pos = 0; // span data while (pos < w*h) { rlen = find_rlen(pos, w*h); pos += rlen; putsplen(rlen-1); } // pad with zeros and flush while (cs.bit != 0x1) putbit(0); printf("\n};\n"); return cs.out_pos; // bytes }
lzo2a_999_compress_callback ( const lzo_byte *in , lzo_uint in_len, lzo_byte *out, lzo_uintp out_len, lzo_voidp wrkmem, lzo_progress_callback_t cb, lzo_uint max_chain ) { lzo_byte *op; lzo_byte *bitp = 0; lzo_uint m_len, m_off; LZO_COMPRESS_T cc; LZO_COMPRESS_T * const c = &cc; lzo_swd_t * const swd = (lzo_swd_t *) wrkmem; int r; lzo_uint32 b = 0; /* bit buffer */ unsigned k = 0; /* bits in bit buffer */ #if defined(__LZO_QUERY_COMPRESS) if (__LZO_IS_COMPRESS_QUERY(in,in_len,out,out_len,wrkmem)) return __LZO_QUERY_COMPRESS(in,in_len,out,out_len,wrkmem,1,lzo_sizeof(lzo_swd_t)); #endif /* sanity check */ if (!lzo_assert(LZO2A_999_MEM_COMPRESS >= lzo_sizeof(lzo_swd_t))) return LZO_E_ERROR; c->init = 0; c->ip = c->in = in; c->in_end = in + in_len; c->cb = cb; c->m1 = c->m2 = c->m3 = c->m4 = 0; op = out; r = init_match(c,swd,NULL,0,0); if (r != 0) return r; if (max_chain > 0) swd->max_chain = max_chain; r = find_match(c,swd,0,0); if (r != 0) return r; while (c->look > 0) { int lazy_match_min_gain = 0; int extra1 = 0; int extra2 = 0; lzo_uint ahead = 0; LZO_UNUSED(extra1); m_len = c->m_len; m_off = c->m_off; #if (N >= 8192) if (m_off >= 8192) { if (m_len < M3_MIN_LEN) m_len = 0; else lazy_match_min_gain = 1; } else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { lazy_match_min_gain = 2; extra1 = 3; extra2 = 2; } else if (m_len >= 10) lazy_match_min_gain = 1; else if (m_len >= 3) { lazy_match_min_gain = 1; extra1 = 1; } else m_len = 0; /* try a lazy match */ if (lazy_match_min_gain > 0 && c->look > m_len) { int lit = swd->b_char; r = find_match(c,swd,1,0); assert(r == 0); assert(c->look > 0); #if (N >= 8192) if (m_off < 8192 && c->m_off >= 8192) lazy_match_min_gain += extra1; else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { if (!(c->m_len >= M1_MIN_LEN && c->m_len <= M1_MAX_LEN && c->m_off <= 256)) lazy_match_min_gain += extra2; } if (c->m_len >= M1_MIN_LEN && c->m_len <= M1_MAX_LEN && c->m_off <= 256) { lazy_match_min_gain -= 1; } if (lazy_match_min_gain < 1) lazy_match_min_gain = 1; if (c->m_len >= m_len + lazy_match_min_gain) { c->lazy++; #if !defined(NDEBUG) m_len = c->m_len; m_off = c->m_off; assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, m_len) == 0); assert(m_len >= 3 || (m_len >= 2 && m_off <= 256)); #endif /* code literal */ putbit(0); putbyte(lit); c->lit_bytes++; continue; } else ahead = 1; assert(m_len > 0); } if (m_len == 0) { /* a literal */ putbit(0); putbyte(swd->b_char); c->lit_bytes++; r = find_match(c,swd,1,0); assert(r == 0); } else { assert(m_len >= M1_MIN_LEN); assert(m_off > 0); assert(m_off <= N); /* 2 - code match */ if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { putbit(1); putbit(0); putbits(2,m_len - M1_MIN_LEN); putbyte(m_off - 1); c->m1++; } #if (N >= 8192) else if (m_off >= 8192) { unsigned len = m_len; assert(m_len >= M3_MIN_LEN); putbit(1); putbit(1); putbyte(m_off & 31); putbyte(m_off >> 5); putbit(1); len -= M3_MIN_LEN - 1; while (len > 255) { len -= 255; putbyte(0); } putbyte(len); c->m4++; } #endif else { assert(m_len >= 3); putbit(1); putbit(1); if (m_len <= 9) { putbyte(((m_len - 2) << 5) | (m_off & 31)); putbyte(m_off >> 5); c->m2++; } else {
lzo2a_999_compress_callback(const lzo_bytep in , lzo_uint in_len, lzo_bytep out, lzo_uintp out_len, lzo_voidp wrkmem, lzo_callback_p cb, lzo_uint max_chain) { lzo_bytep op; lzo_bytep bitp = 0; lzo_uint m_len, m_off; LZO_COMPRESS_T cc; LZO_COMPRESS_T* const c = &cc; lzo_swd_p const swd = (lzo_swd_p) wrkmem; int r; lzo_uint32_t b = 0; /* bit buffer */ unsigned k = 0; /* bits in bit buffer */ /* sanity check */ LZO_COMPILE_TIME_ASSERT(LZO2A_999_MEM_COMPRESS >= SIZEOF_LZO_SWD_T) c->init = 0; c->ip = c->in = in; c->in_end = in + in_len; c->cb = cb; c->m1 = c->m2 = c->m3 = c->m4 = 0; op = out; r = init_match(c, swd, NULL, 0, 0); if (r != 0) return r; if (max_chain > 0) swd->max_chain = max_chain; r = find_match(c, swd, 0, 0); if (r != 0) return r; while (c->look > 0) { lzo_uint lazy_match_min_gain = 0; #if (SWD_N >= 8192) lzo_uint extra1 = 0; #endif lzo_uint extra2 = 0; lzo_uint ahead = 0; m_len = c->m_len; m_off = c->m_off; #if (SWD_N >= 8192) if (m_off >= 8192) { if (m_len < M3_MIN_LEN) m_len = 0; else lazy_match_min_gain = 1; } else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { lazy_match_min_gain = 2; #if (SWD_N >= 8192) extra1 = 3; #endif extra2 = 2; } else if (m_len >= 10) lazy_match_min_gain = 1; else if (m_len >= 3) { lazy_match_min_gain = 1; #if (SWD_N >= 8192) extra1 = 1; #endif } else m_len = 0; /* try a lazy match */ if (lazy_match_min_gain > 0 && c->look > m_len) { unsigned char lit = LZO_BYTE(swd->b_char); r = find_match(c, swd, 1, 0); assert(r == 0); LZO_UNUSED(r); assert(c->look > 0); #if (SWD_N >= 8192) if (m_off < 8192 && c->m_off >= 8192) lazy_match_min_gain += extra1; else #endif if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { if (!(c->m_len >= M1_MIN_LEN && c->m_len <= M1_MAX_LEN && c->m_off <= 256)) lazy_match_min_gain += extra2; } if (c->m_len >= M1_MIN_LEN && c->m_len <= M1_MAX_LEN && c->m_off <= 256) { lazy_match_min_gain -= 1; } if ((lzo_int) lazy_match_min_gain < 1) lazy_match_min_gain = 1; if (c->m_len >= m_len + lazy_match_min_gain) { c->lazy++; #if !defined(NDEBUG) m_len = c->m_len; m_off = c->m_off; assert(lzo_memcmp(c->ip - c->look, c->ip - c->look - m_off, m_len) == 0); assert(m_len >= 3 || (m_len >= 2 && m_off <= 256)); #endif /* code literal */ putbit(0); putbyte(lit); c->lit_bytes++; continue; } else ahead = 1; assert(m_len > 0); } if (m_len == 0) { /* a literal */ putbit(0); putbyte(swd->b_char); c->lit_bytes++; r = find_match(c, swd, 1, 0); assert(r == 0); LZO_UNUSED(r); } else { assert(m_len >= M1_MIN_LEN); assert(m_off > 0); assert(m_off <= SWD_N); /* 2 - code match */ if (m_len >= M1_MIN_LEN && m_len <= M1_MAX_LEN && m_off <= 256) { putbit(1); putbit(0); putbits(2, m_len - M1_MIN_LEN); putbyte(m_off - 1); c->m1++; } #if (SWD_N >= 8192) else if (m_off >= 8192) { unsigned len = m_len; assert(m_len >= M3_MIN_LEN); putbit(1); putbit(1); putbyte(m_off & 31); putbyte(m_off >> 5); putbit(1); len -= M3_MIN_LEN - 1; while (len > 255) { len -= 255; putbyte(0); } putbyte(len); c->m4++; } #endif else { assert(m_len >= 3); putbit(1); putbit(1); if (m_len <= 9) { putbyte(((m_len - 2) << 5) | (m_off & 31)); putbyte(m_off >> 5); c->m2++; } else {