Пример #1
0
static void write_pointer(io *io_s, size_t length, size_t offset)
{
	/* write length huffman code */
	two_bytes huff_code, len_code = get_code_of_length(length);
	size_t huff_code_len;
	get_huffman_code_of_litlen(len_code, &huff_code, &huff_code_len);
	write_huffman_code(io_s, huff_code, huff_code_len);

	/* write extra bits of length */
	byte len_extra_bits;
	size_t len_bits_num;
	get_extra_bits_of_length(length, len_code, 
							 &len_extra_bits, &len_bits_num);
	if (len_bits_num > 0)
		write_bits(io_s, len_extra_bits, len_bits_num);
	
	/* write offset code*/
	byte off_code = get_code_of_offset(offset);
	write_bits(io_s, off_code, OFF_CODE_LEN);
	
	/* write extra bits of offset */
	two_bytes off_extra_bits;
	size_t off_bits_num;
	get_extra_bits_of_offset(offset, off_code, 
							 &off_extra_bits, &off_bits_num);
	if (off_bits_num > 0)
		write_bits(io_s, off_extra_bits, off_bits_num);
}
Пример #2
0
static void write_hval (huf_table *h, unsigned value) {
    int len = length(value);

    write_bits (h->table[len].code, h->table[len].codelen);
    if (len >= 2)
	write_bits (value, len-1);
}
int main()
{
    printf("Bit at position 2 of 2: %d\n",bit_i(2,2));
    printf("Bit at position 1 of 2: %d\n",bit_i(2,1));
    printf("Binary print of 128: ");
    write_bits(128);
    printf("\n");
    printf("Binary print of 12: ");
    write_bits(12);
    printf("\n");
    printf("Uint with bit 1 at position 13: %u\n",uint_bit_on_at_pos(13));
    printf("Number of bits on in 12: %d\n",num_of_bits_on(12));
    printf("Set bit 2 of 12 on: %d\n",set_bit_at_pos(12,2));
    printf("Unset bit 3 of 12 on: %d\n",unset_bit_at_pos(12,3));
    printf("Toggle bit 3 of 12 on (set to off): %d\n",toggle_bit_at_pos(12,3));
    printf("Toggle bit 2 of 12 on (set to on): %d\n",toggle_bit_at_pos(12,2));
    printf("Equal test (22==22): %d\n",equal(22,22));
    printf("Equal test (22==100): %d\n",equal(22,100));
    printf("Even test (23): %d\n",is_even(23));
    printf("Even test (22): %d\n",is_even(22));
    printf("Even test (10211): %d\n",is_even(10211));
    uint test_swapper = 356;
    printf("Swapping even and odd bits of: \t\t");
    write_bits(test_swapper);
    swap_even_odd(&test_swapper);
    printf("\nResult of swapping even an odd bits: \t");
    write_bits(test_swapper);
    printf("\n");
}
Пример #4
0
static void write_huf (huf_table *h) {
    int i, j;

    j = 0;
    for (i=0; i<32; i++)
	if (h->table[i].codelen > 0)
	    j = i+1;

    write_bits (j, 5);

    for (i=0; i<j; i++)
	write_bits (h->table[i].codelen, 4);
}
Пример #5
0
/*static*/ void *rnc_pack (void *original, long datalen, long *packlen) {
    int i;
    char *data = original;
    long origlen = datalen;

    packed = malloc(PACKED_DELTA);
    if (!packed) {
	perror("malloc");
	exit(1);
    }
    packedlen = PACKED_DELTA;    
    packedpos = 20;
    bwrite (packed+4, datalen);

    bitpos = 18;
    bitcount = 0;
    bitbuf = 0;
    write_bits (0, 2);

    while (datalen > 0) {
	blklen = datalen > BLOCKMAX ? BLOCKMAX : datalen;
	blkstart = WINMAX - BLOCKMAX;
	if (blkstart > origlen-datalen)
	    blkstart = origlen-datalen;
	memcpy (blk, data-blkstart, blkstart+blklen);
	for (i=0; i<HASHMAX; i++)
	    hashp[i] = -1;
	ntuple = 0;
	tuples[ntuple].rawlen = 0;
	blklen += blkstart;
	do_block();
	data += bpos - blkstart;
	datalen -= bpos - blkstart;
	write_block();
    }

    if (bitcount > 0) {
	write_bits (0, 17-bitcount);   /* force flush */
	packedpos -= 2;		       /* write_bits will have moved it on */
    }

    *packlen = packedpos;

    bwrite (packed, RNC_SIGNATURE);
    bwrite (packed+12, rnc_crc(packed+18, packedpos-18));
    bwrite (packed+10, rnc_crc(original, origlen));
    bwrite (packed+8, packedpos-18);
    packed[16] = packed[17] = 0;

    return packed;
}
Пример #6
0
Файл: encode.c Проект: elyl/lzw
void encode(t_enc *ctx)
{
  char			str[BUFFER_SIZE];
  int			size;
  int			i;
  t_elem		*tmp;
  
  init_enc(ctx);
  while ((size = read(ctx->fin, &str[0], BUFFER_SIZE)))
    {
      i = 0;
      while (i < size)
	{
	  tmp = search_str(str[i], ctx->code, ctx);
	  if (tmp == NULL)
	    {
	      write_bits(ctx, ctx->code, ctx->nbits);
	      add_str(str[i], ctx->code, ctx);
	      ctx->code = str[i];	     
	    }
	  else
	    ctx->code = tmp->code;
	  if (ctx->code_max == (1 << ctx->nbits))
	    ctx->code_max++;
	  i++;
	}
    }
}
Пример #7
0
static int write_deflate_header_stateless(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	struct isal_hufftables *hufftables = stream->hufftables;
	uint32_t count;

	if (hufftables->deflate_hdr_count + 8 >= stream->avail_out)
		return STATELESS_OVERFLOW;

	memcpy(stream->next_out, hufftables->deflate_hdr, hufftables->deflate_hdr_count);

	stream->avail_out -= hufftables->deflate_hdr_count;
	stream->total_out += hufftables->deflate_hdr_count;
	stream->next_out += hufftables->deflate_hdr_count;

	set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

	write_bits(&state->bitbuf, hufftables->deflate_hdr[hufftables->deflate_hdr_count],
		   hufftables->deflate_hdr_extra_bits);

	count = buffer_used(&state->bitbuf);
	stream->next_out = buffer_ptr(&state->bitbuf);
	stream->avail_out -= count;
	stream->total_out += count;

	return COMP_OK;
}
Пример #8
0
Файл: igzip.c Проект: ceph/isa-l
static int write_deflate_header_unaligned_stateless(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	struct isal_hufftables *hufftables = stream->hufftables;
	unsigned int count;
	uint64_t bit_count;
	uint64_t *header_next;
	uint64_t *header_end;
	uint64_t header_bits;

	if (state->bitbuf.m_bit_count == 0)
		return write_deflate_header_stateless(stream);

	if (hufftables->deflate_hdr_count + 16 >= stream->avail_out)
		return STATELESS_OVERFLOW;

	set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

	header_next = (uint64_t *) hufftables->deflate_hdr;
	header_end = header_next + hufftables->deflate_hdr_count / 8;

	header_bits = *header_next;

	if (stream->end_of_stream == 0)
		header_bits--;
	else
		state->has_eob_hdr = 1;

	header_next++;

	/* Write out Complete Header bits */
	for (; header_next <= header_end; header_next++) {
		write_bits(&state->bitbuf, header_bits, 32);
		header_bits >>= 32;
		write_bits(&state->bitbuf, header_bits, 32);
		header_bits = *header_next;
	}

	bit_count =
	    (hufftables->deflate_hdr_count & 0x7) * 8 + hufftables->deflate_hdr_extra_bits;

	if (bit_count > MAX_BITBUF_BIT_WRITE) {
		write_bits(&state->bitbuf, header_bits, MAX_BITBUF_BIT_WRITE);
		header_bits >>= MAX_BITBUF_BIT_WRITE;
		bit_count -= MAX_BITBUF_BIT_WRITE;

	}
Пример #9
0
static void g4_encode_horizontal_run (struct bit_buffer *buf,
				      bool black,
				      uint32_t run_length)
{
  uint32_t i;

  while (run_length >= 2560)
    {
      write_bits (buf, 12, 0x01f);
      run_length -= 2560;
    }

  if (run_length >= 1792)
    {
      i = (run_length - 1792) >> 6;
      write_bits (buf,
		  g4_long_makeup_code [i].count,
		  g4_long_makeup_code [i].bits);
      run_length -= (1792 + (i << 6));
    }
Пример #10
0
/* 01 - compressed with fixed Huffman codes */
static void write_static_header(io *io_s)
{
	byte header = 0;
	/* BFINAL */
	if (io_s->isfinal)
		SetBit(header, 0);
	/* BTYPE */
	SetBit(header, 1);
	
	write_bits(io_s, header, HEADER_LEN);
}
Пример #11
0
void LiquidCrystal::begin()
{
    configure_as_output(rs_pin);
    configure_as_output(enable_pin);
    configure_as_output(rw_pin);

    for(int i = 0; i < 4; i++)
        configure_as_output(data_pins[i]);

    set_pin(rs_pin, LOW);
    set_pin(enable_pin, LOW);
    set_pin(rw_pin, LOW);

    write_bits(0x03);
    etk::sleep_us(4500);
    write_bits(0x03);
    etk::sleep_us(500);
    write_bits(0x03);
    etk::sleep_us(250);
    write_bits(0x02);
    etk::sleep_ms(2);
    write_bits(0x02);
    etk::sleep_us(150);
    write_bits(LCD_2LINE);
    etk::sleep_us(150);

    command(LCD_DISPLAYCONTROL);
    clear();
    command(LCD_ENTRYMODESET | LCD_ENTRYLEFT);
    display();
}
Пример #12
0
// The size of the output buffer must not be less than
// 3/4 the size of the input buffer. For simplicity,
// make them the same size.
// return : 解码后数据长度
int CBase64::Decode ( IN const char* szDecoding, char* szOutput )
{
	CString sInput;
    int c, lp =0;
	int nDigit;
    int nDecode[ 256 ];

	ASSERT( szDecoding != NULL );
	ASSERT( szOutput != NULL );
	if( szOutput == NULL )
		return 0;
	if( szDecoding == NULL )
		return 0;
	sInput = szDecoding;
	if( sInput.GetLength() == 0 )
		return 0;

	// Build Decode Table
	//
	int i;
	for( i = 0; i < 256; i++ ) 
		nDecode[i] = -2; // Illegal digit
	for( i=0; i < 64; i++ )
	{
		nDecode[ m_sBase64Alphabet[ i ] ] = i;
		nDecode[ m_sBase64Alphabet[ i ] | 0x80 ] = i; // Ignore 8th bit
		nDecode[ '\r' ] = -1; 
		nDecode[ '\n' ] = -1; 
		nDecode[ '=' ] = -1; 
		nDecode[ '=' | 0x80 ] = -1; // Ignore MIME padding char
    }

	// Clear the output buffer
	memset( szOutput, 0, sInput.GetLength() + 1 );

	// Decode the Input
	//
	for( lp = 0, i = 0; lp < sInput.GetLength(); lp++ )
	{
		c = sInput[ lp ];
		nDigit = nDecode[ c & 0x7F ];
		if( nDigit < -1 ) 
		{
			return 0;
		} 
		else if( nDigit >= 0 ) 
			// i (index into output) is incremented by write_bits()
			write_bits( nDigit & 0x3F, 6, szOutput, i );
    }	
	return i;
}
Пример #13
0
static int writer_bits(lua_State* L)
{
	writer_t* writer = lua_touserdata(L, 1);
	lua_Integer size = luaL_checkinteger(L, 2);
	lua_Integer val  = luaL_checkinteger(L, 3);
	
	uint64_t max  = ((uint64_t)1 << (uint64_t)size) - 1;
	uint64_t uval = (uint64_t)val;
	if(uval > max)
		luaL_error(L, "unsigned overflow bits %d max %d val %d", size, max, uval);
		
	write_bits(writer, size, uval);
	return 0;
}
Пример #14
0
static int disk_flush(struct dirty_log *log)
{
	int r;
	struct log_c *lc = (struct log_c *) log->context;

	/* only write if the log has changed */
	if (!lc->touched)
		return 0;

	r = write_bits(lc);
	if (!r)
		lc->touched = 0;

	return r;
}
Пример #15
0
static void write_val(char *out, int *out_pos, uint8_t *store, int *p, short val, int vlen)
{
	int vp = 0;
	while (1) {
		//printf("OUT_POS: %d\n", *out_pos);
		int ret = write_bits(store, p, val, vlen, &vp);
		if (ret == STORE_FULL) {
			//printf("store full\n");
			out[*out_pos] = *store;
			(*out_pos)++;
			(*store) = 0;
			(*p) = 0;
		}
		else if (ret == NO_BITS_LEFT) {
			//printf("\tdone writing\n");
			break;
		}
	}
}
Пример #16
0
int main(int argc, char *argv[])
{
	int ret;
	int stype;
	int i;
	struct parsed_options options;

	init_parsed_options(&options);
	options.subdevice = -1;
	parse_options(&options, argc, argv);

	device = comedi_open(options.filename);
	if(!device){
		comedi_perror(options.filename);
		exit(-1);
	}
	if(options.subdevice < 0)
	{
		options.subdevice = comedi_find_subdevice_by_type(device, COMEDI_SUBD_DIO, 0);
		if(options.subdevice < 0){
			fprintf(stderr,"No dio subdevice found.\n");
			exit(-1);
		}
	}
	stype = comedi_get_subdevice_type(device, options.subdevice);
	if(stype != COMEDI_SUBD_DIO){
		printf("%d is not a digital I/O subdevice\n", options.subdevice);
		exit(-1);
	}

	printf("configuring pin %d for output...\n", chan_dat);
	ret = comedi_dio_config(device, options.subdevice, chan_dat, COMEDI_OUTPUT);

	printf("configuring pin %d for output...\n", chan_clk);
	ret = comedi_dio_config(device, options.subdevice, chan_clk, COMEDI_OUTPUT);

	for(i = 0; i < 0x100; i++){
		write_bits(options.subdevice, i);
	}
	//write_bits(0xa5);

	return 0;
}
Пример #17
0
static
void sync_flush(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	uint64_t bits_to_write = 0xFFFF0000, bits_len;
	uint64_t code = 0, len = 0, bytes;
	int flush_size;

	if (stream->avail_out >= 8) {
		set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

		if (!state->has_eob)
			get_lit_code(stream->hufftables, 256, &code, &len);

		flush_size = (-(state->bitbuf.m_bit_count + len + 3)) % 8;

		bits_to_write <<= flush_size + 3;
		bits_len = 32 + len + flush_size + 3;

#ifdef USE_BITBUFB		/* Write Bits Always */
		state->state = ZSTATE_NEW_HDR;
#else /* Not Write Bits Always */
		state->state = ZSTATE_FLUSH_WRITE_BUFFER;
#endif
		state->has_eob = 0;

		if (len > 0)
			bits_to_write = (bits_to_write << len) | code;

		write_bits(&state->bitbuf, bits_to_write, bits_len);

		bytes = buffer_used(&state->bitbuf);
		stream->next_out = buffer_ptr(&state->bitbuf);
		stream->avail_out -= bytes;
		stream->total_out += bytes;

		if (stream->flush == FULL_FLUSH) {
			/* Clear match history so there are no cross
			 * block length distance pairs */
			reset_match_history(stream);
		}
	}
}
Пример #18
0
static int disk_resume(struct dirty_log *log)
{
	int r;
	unsigned i;
	struct log_c *lc = (struct log_c *) log->context;
	size_t size = lc->bitset_uint32_count * sizeof(uint32_t);

	/* read the disk header */
	r = read_header(lc);
	if (r)
		return r;

	/* read the bits */
	r = read_bits(lc);
	if (r)
		return r;

	/* set or clear any new bits */
	if (lc->sync == NOSYNC)
		for (i = lc->header.nr_regions; i < lc->region_count; i++)
			/* FIXME: amazingly inefficient */
			log_set_bit(lc, lc->clean_bits, i);
	else
		for (i = lc->header.nr_regions; i < lc->region_count; i++)
			/* FIXME: amazingly inefficient */
			log_clear_bit(lc, lc->clean_bits, i);

	/* copy clean across to sync */
	memcpy(lc->sync_bits, lc->clean_bits, size);
	lc->sync_count = count_bits32(lc->clean_bits, lc->bitset_uint32_count);

	/* write the bits */
	r = write_bits(lc);
	if (r)
		return r;

	/* set the correct number of regions in the header */
	lc->header.nr_regions = lc->region_count;

	/* write the new header */
	return write_header(lc);
}
Пример #19
0
static void write_block (void) {
    int lengths[32];
    huf_table raw, dist, len;
    int i, j, k;

    for (i=0; i<32; i++)
	lengths[i] = 0;
    for (i=0; i<=ntuple; i++)
	lengths[length(tuples[i].rawlen)]++;
    build_huf (&raw, lengths);
    write_huf (&raw);

    for (i=0; i<32; i++)
	lengths[i] = 0;
    for (i=0; i<ntuple; i++)
	lengths[length(tuples[i].pos-1)]++;
    build_huf (&dist, lengths);
    write_huf (&dist);

    for (i=0; i<32; i++)
	lengths[i] = 0;
    for (i=0; i<ntuple; i++)
	lengths[length(tuples[i].len-2)]++;
    build_huf (&len, lengths);
    write_huf (&len);

    write_bits (ntuple+1, 16);

    k = blkstart;
    for (i=0; i<=ntuple; i++) {
	write_hval (&raw, tuples[i].rawlen);
	for (j=0; j<tuples[i].rawlen; j++)
	    write_literal (blk[k++]);
	if (i == ntuple)
	    break;
	write_hval (&dist, tuples[i].pos-1);
	write_hval (&len, tuples[i].len-2);
	k += tuples[i].len;
    }
}
Пример #20
0
Файл: igzip.c Проект: ceph/isa-l
static
void sync_flush(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	uint64_t bits_to_write = 0xFFFF0000, bits_len;
	uint64_t bytes;
	int flush_size;

	if (stream->avail_out >= 8) {
		set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

		flush_size = (-(state->bitbuf.m_bit_count + 3)) % 8;

		bits_to_write <<= flush_size + 3;
		bits_len = 32 + flush_size + 3;

#ifdef USE_BITBUFB		/* Write Bits Always */
		state->state = ZSTATE_NEW_HDR;
#else /* Not Write Bits Always */
		state->state = ZSTATE_FLUSH_WRITE_BUFFER;
#endif
		state->has_eob = 0;

		write_bits(&state->bitbuf, bits_to_write, bits_len);

		bytes = buffer_used(&state->bitbuf);
		stream->next_out = buffer_ptr(&state->bitbuf);
		stream->avail_out -= bytes;
		stream->total_out += bytes;

		if (stream->flush == FULL_FLUSH) {
			/* Clear match history so there are no cross
			 * block length distance pairs */
			state->file_start -= state->b_bytes_processed;
			state->b_bytes_valid -= state->b_bytes_processed;
			state->b_bytes_processed = 0;
			reset_match_history(stream);
		}
	}
}
Пример #21
0
Файл: igzip.c Проект: ceph/isa-l
static int write_deflate_header_stateless(struct isal_zstream *stream)
{
	struct isal_zstate *state = &stream->internal_state;
	struct isal_hufftables *hufftables = stream->hufftables;
	uint64_t hdr_extra_bits = hufftables->deflate_hdr[hufftables->deflate_hdr_count];
	uint32_t count;

	if (hufftables->deflate_hdr_count + 8 >= stream->avail_out)
		return STATELESS_OVERFLOW;

	memcpy(stream->next_out, hufftables->deflate_hdr, hufftables->deflate_hdr_count);

	if (stream->end_of_stream == 0) {
		if (hufftables->deflate_hdr_count > 0)
			*stream->next_out -= 1;
		else
			hdr_extra_bits -= 1;
	} else
		state->has_eob_hdr = 1;

	stream->avail_out -= hufftables->deflate_hdr_count;
	stream->total_out += hufftables->deflate_hdr_count;
	stream->next_out += hufftables->deflate_hdr_count;

	set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

	write_bits(&state->bitbuf, hdr_extra_bits, hufftables->deflate_hdr_extra_bits);

	count = buffer_used(&state->bitbuf);
	stream->next_out = buffer_ptr(&state->bitbuf);
	stream->avail_out -= count;
	stream->total_out += count;

	state->state = ZSTATE_BODY;

	return COMP_OK;
}
Пример #22
0
Файл: igzip.c Проект: ceph/isa-l
static void write_constant_compressed_stateless(struct isal_zstream *stream,
						uint32_t repeated_length)
{
	/* Assumes repeated_length is at least 1.
	 * Assumes the input end_of_stream is either 0 or 1. */
	struct isal_zstate *state = &stream->internal_state;
	uint32_t rep_bits = ((repeated_length - 1) / 258) * 2;
	uint32_t rep_bytes = rep_bits / 8;
	uint32_t rep_extra = (repeated_length - 1) % 258;
	uint32_t bytes;
	uint32_t repeated_char = *stream->next_in;
	uint8_t *start_in = stream->next_in;

	/* Guarantee there is enough space for the header even in the worst case */
	if (stream->avail_out < HEADER_LENGTH + MAX_FIXUP_CODE_LENGTH + rep_bytes + 8)
		return;

	/* Assumes the repeated char is either 0 or 0xFF. */
	memcpy(stream->next_out, repeated_char_header[repeated_char & 1], HEADER_LENGTH);

	if (stream->avail_in == repeated_length && stream->end_of_stream > 0) {
		stream->next_out[0] |= 1;
		state->has_eob_hdr = 1;
		state->has_eob = 1;
		state->state = ZSTATE_TRL;
	} else {
		state->state = ZSTATE_NEW_HDR;
	}

	memset(stream->next_out + HEADER_LENGTH, 0, rep_bytes);
	stream->avail_out -= HEADER_LENGTH + rep_bytes;
	stream->next_out += HEADER_LENGTH + rep_bytes;
	stream->total_out += HEADER_LENGTH + rep_bytes;

	set_buf(&state->bitbuf, stream->next_out, stream->avail_out);

	/* These two lines are basically a modified version of init. */
	state->bitbuf.m_bits = 0;
	state->bitbuf.m_bit_count = rep_bits % 8;

	/* Add smaller repeat codes as necessary. Code280 can describe repeat
	 * lengths of 115-130 bits. Code10 can describe repeat lengths of 10
	 * bits. If more than 230 bits, fill code with two code280s. Else if
	 * more than 115 repeates, fill with code10s until one code280 can
	 * finish the rest of the repeats. Else, fill with code10s and
	 * literals */
	if (rep_extra > 115) {
		while (rep_extra > 130 && rep_extra < 230) {
			write_bits(&state->bitbuf, CODE_10, CODE_10_LENGTH);
			rep_extra -= 10;
		}

		if (rep_extra >= 230) {
			write_bits(&state->bitbuf,
				   CODE_280 | ((rep_extra / 2 - 115) <<
					       CODE_280_LENGTH), CODE_280_TOTAL_LENGTH);
			rep_extra -= rep_extra / 2;
		}

		write_bits(&state->bitbuf,
			   CODE_280 | ((rep_extra - 115) << CODE_280_LENGTH),
			   CODE_280_TOTAL_LENGTH);

	} else {
		while (rep_extra >= 10) {

			write_bits(&state->bitbuf, CODE_10, CODE_10_LENGTH);
			rep_extra -= 10;
		}

		for (; rep_extra > 0; rep_extra--)
			write_bits(&state->bitbuf, CODE_LIT, CODE_LIT_LENGTH);
	}

	write_bits(&state->bitbuf, END_OF_BLOCK, END_OF_BLOCK_LEN);

	stream->next_in += repeated_length;
	stream->avail_in -= repeated_length;
	stream->total_in += repeated_length;

	bytes = buffer_used(&state->bitbuf);
	stream->next_out = buffer_ptr(&state->bitbuf);
	stream->avail_out -= bytes;
	stream->total_out += bytes;

	if (stream->gzip_flag)
		state->crc = crc32_gzip(state->crc, start_in, stream->next_in - start_in);

	return;
}
Пример #23
0
//---------------------------------------------------------------------------------------------
void BitMessage::write_uint16(int32_t c)
{
	write_bits(c, 16);
}
Пример #24
0
//---------------------------------------------------------------------------------------------
void BitMessage::write_uint8(int32_t c)
{
	write_bits(c, 8);  
}
Пример #25
0
//---------------------------------------------------------------------------------------------
void BitMessage::write_float(float f)
{
	write_bits(*reinterpret_cast<int32_t *>(&f), 32);  
}
Пример #26
0
int compress_block(
   unsigned char *outbuf,       /* compressed output buffer            */
   int   *obytes,       /* number of compressed bytes          */
   short *sip,          /* quantized image                     */
   const int sip_siz,   /* size of quantized image to compress */
   const int MaxCoeff,  /* Maximum values for coefficients     */
   const int MaxZRun,   /* Maximum zero runs                   */
   HUFFCODE *codes)     /* huffman code table                  */
{
   unsigned char *optr;
   int LoMaxCoeff;        /* lower (negative) MaxCoeff limit */
   short pix;             /* temp pixel pointer */
   unsigned int rcnt = 0, state;  /* zero run count and if current pixel
                             is in a zero run or just a coefficient */
   int cnt;               /* pixel counter */
   int outbit, bytes;     /* parameters used by write_bits to */
   unsigned char bits;            /* output the "coded" image to the  */
                          /* output buffer                    */

   LoMaxCoeff = 1 - MaxCoeff;
   optr = outbuf;
   outbit = 7;
   bytes = 0;
   bits = 0;
   state = COEFF_CODE;
   for (cnt = 0; cnt < sip_siz; cnt++) {
      pix = *(sip + cnt);

      switch (state) {

         case COEFF_CODE:
            if (pix == 0) {
               state = RUN_CODE;
               rcnt = 1;
               break;
            }
            if (pix > MaxCoeff) { 
               if (pix > 255) {
                  /* 16bit pos esc */
                  write_bits( &optr, (unsigned short) codes[103].code,
                              codes[103].size, &outbit, &bits, &bytes );
                  write_bits( &optr, (unsigned short) pix, 16,
                              &outbit, &bits, &bytes);
               }
               else {
                  /* 8bit pos esc */
                  write_bits( &optr, (unsigned short) codes[101].code,
                              codes[101].size, &outbit, &bits, &bytes );
                  write_bits( &optr, (unsigned short) pix, 8,
                              &outbit, &bits, &bytes);
               }
            }
            else if (pix < LoMaxCoeff) {
               if (pix < -255) {
                  /* 16bit neg esc */
                  write_bits( &optr, (unsigned short) codes[104].code,
                              codes[104].size, &outbit, &bits, &bytes );
                  write_bits( &optr, (unsigned short) -pix, 16,
                              &outbit, &bits, &bytes);
               }
               else {
                  /* 8bit neg esc */
                  write_bits( &optr, (unsigned short) codes[102].code,
                              codes[102].size, &outbit, &bits, &bytes );
                  write_bits( &optr, (unsigned short) -pix, 8,
                              &outbit, &bits, &bytes);
               }
            }
            else {
               /* within table */
               write_bits( &optr, (unsigned short) codes[pix+180].code,
                           codes[pix+180].size, &outbit, &bits, &bytes);
            }
            break;

         case RUN_CODE:
            if (pix == 0  &&  rcnt < 0xFFFF) {
               ++rcnt;
               break;
            }
            if (rcnt <= MaxZRun) {
               /* log zero run length */
               write_bits( &optr, (unsigned short) codes[rcnt].code,
                           codes[rcnt].size, &outbit, &bits, &bytes );
            }
            else if (rcnt <= 0xFF) {
               /* 8bit zrun esc */
               write_bits( &optr, (unsigned short) codes[105].code,
                           codes[105].size, &outbit, &bits, &bytes );
               write_bits( &optr, (unsigned short) rcnt, 8,
                           &outbit, &bits, &bytes);
            }
            else if (rcnt <= 0xFFFF) {
               /* 16bit zrun esc */
               write_bits( &optr, (unsigned short) codes[106].code,
                           codes[106].size, &outbit, &bits, &bytes );
               write_bits( &optr, (unsigned short) rcnt, 16,
                           &outbit, &bits, &bytes);
            }
            else {
               fprintf(stderr,
                      "ERROR : compress_block : zrun too large.\n");
               return(-47);
            }

            if(pix != 0) {
               if (pix > MaxCoeff) {
                  /** log current pix **/
                  if (pix > 255) {
                     /* 16bit pos esc */
                     write_bits( &optr, (unsigned short) codes[103].code,
                                 codes[103].size, &outbit, &bits, &bytes );
                     write_bits( &optr, (unsigned short) pix, 16,
                                 &outbit, &bits, &bytes);
                  }
                  else {
                     /* 8bit pos esc */
                     write_bits( &optr, (unsigned short) codes[101].code,
                                 codes[101].size, &outbit, &bits, &bytes );
                     write_bits( &optr, (unsigned short) pix, 8,
                                 &outbit, &bits, &bytes);
                  }
               }
               else if (pix < LoMaxCoeff) {
                  if (pix < -255) {
                     /* 16bit neg esc */
                     write_bits( &optr, (unsigned short) codes[104].code,
                                 codes[104].size, &outbit, &bits, &bytes );
                     write_bits( &optr, (unsigned short) -pix, 16,
                                 &outbit, &bits, &bytes);
                  }
                  else {
                     /* 8bit neg esc */
                     write_bits( &optr, (unsigned short) codes[102].code,
                                 codes[102].size, &outbit, &bits, &bytes );
                     write_bits( &optr, (unsigned short) -pix, 8,
                                 &outbit, &bits, &bytes);
                  }
               }
               else {
                  /* within table */
                  write_bits( &optr, (unsigned short) codes[pix+180].code,
                              codes[pix+180].size, &outbit, &bits, &bytes);
               }
               state = COEFF_CODE;
            }
            else {
               rcnt = 1;
               state = RUN_CODE;
            }
            break;
      }
   }
   if (state == RUN_CODE) {
      if (rcnt <= MaxZRun) {
         write_bits( &optr, (unsigned short) codes[rcnt].code,
                     codes[rcnt].size, &outbit, &bits, &bytes );
      }
      else if (rcnt <= 0xFF) {
         write_bits( &optr, (unsigned short) codes[105].code,
                     codes[105].size, &outbit, &bits, &bytes );
         write_bits( &optr, (unsigned short) rcnt, 8,
                     &outbit, &bits, &bytes);
      }
      else if (rcnt <= 0xFFFF) {
         write_bits( &optr, (unsigned short) codes[106].code,
                     codes[106].size, &outbit, &bits, &bytes );
         write_bits( &optr, (unsigned short) rcnt, 16,
                     &outbit, &bits, &bytes);
      }
      else {
         fprintf(stderr, "ERROR : compress_block : zrun2 too large.\n");
         return(-48);
      }
   }

   flush_bits( &optr, &outbit, &bits, &bytes);

   *obytes = bytes;
   return(0);
}
Пример #27
0
//---------------------------------------------------------------------------------------------
void BitMessage::write_int32(int32_t c)
{
	write_bits(c, 32);
}
Пример #28
0
static void write_number_length(int len)
{
	char c[3] = {0};
	sprintf(c, "%02X", len);
	write_bits(c, 2 * 8);
}
Пример #29
0
static void write_internat_code(int code)
{
	char c[3] = {0};
	sprintf(c, "%02X", code);
	write_bits(c, 2 * 8);
}
Пример #30
0
static void encode_channel(uint8_t *base_addr, const channel_block &block, const surface<float> &surf,
                           const std::vector<plane> &planes, const std::pair<float, float> range, bool negative_line_stride) {
    block_iterator it(block.w, block.h, &surf);

    uint32_t n_blocks_in_line = (surf.width() / block.w);
    uint32_t n_block_lines = (surf.height() / block.h);

    // We need to support negative line stride.
    std::vector<std::pair<std::ptrdiff_t , std::ptrdiff_t >> line_offsets;
    for (const auto & plane : planes ) {
        if (negative_line_stride) {
            std::ptrdiff_t line_stride = -static_cast<std::ptrdiff_t>(plane.line_stride);
            // Each line  is still left to right.
            line_offsets.emplace_back(line_stride, plane.size + line_stride);
        } else {
            line_offsets.emplace_back(plane.line_stride, 0);
        }
    }

    // We need to preprocess the sample array to support continuation samples.
    std::vector<sample> samples;
    for (std::size_t i = 0; i < block.samples.size();) {
        const xyuv::sample &sample = block.samples[i];
        if (!sample.has_continuation) {
            samples.push_back(sample);
            ++i;
        }
        else {
            // Create a descriptor block containing all the bits of the samples.
            xyuv::sample sample_descriptor;
            sample_descriptor.integer_bits = 0;
            sample_descriptor.fractional_bits = 0;
            sample_descriptor.has_continuation = true;
            samples.push_back(sample_descriptor);
            size_t descriptor_pos = samples.size() -1;

            // Create a stack of all the bits in the sample.
            // We need to reverse the order of the samples to encode them correctly.
            std::vector<const xyuv::sample*> bit_stack;
            do {
                // Update descriptor
                samples[descriptor_pos].integer_bits += block.samples[i].integer_bits;
                samples[descriptor_pos].fractional_bits += block.samples[i].fractional_bits;
                bit_stack.push_back(&(block.samples[i]));
            } while(block.samples[i++].has_continuation);

            for (auto rit = bit_stack.rbegin(); rit != bit_stack.rend(); ++rit) {
                samples.push_back(*(*rit));
                samples.back().has_continuation = true;
            }

            samples.back().has_continuation = false;
        }
    }

    // Finally iterate over the image
    for (uint32_t line = 0; line < n_block_lines; line++) {
        // Precompute interleaved lines
        uint32_t interleaved_line[3] = {
                get_line(line, static_cast<interleave_pattern>(0), n_block_lines),
                get_line(line, static_cast<interleave_pattern>(1), n_block_lines),
                get_line(line, static_cast<interleave_pattern>(2), n_block_lines),
        };
        for (uint32_t b = 0; b < n_blocks_in_line; b++) {
            for (std::size_t s = 0; s < samples.size(); ) {
                uint8_t integer_bits = samples[s].integer_bits;
                uint8_t fractional_bits = samples[s].fractional_bits;

                float value = *it.advance();
                unorm_t unorm = to_unorm(value, integer_bits, fractional_bits, range);

                // If we hit a continuation block here, it means that we have the
                // Total bits descriptor and should skip it for the purpose of actual storing.
                if (samples[s].has_continuation) {
                    s++;
                }

                do {
                    const xyuv::sample &sample = samples[s];

                    uint8_t * ptr_to_line =
                            // Start with offset to frame
                            base_addr +
                            // Add offset to lowest byte in plane.
                            planes[sample.plane].base_offset +
                            // Add the size of the plane if applicable.
                            line_offsets[sample.plane].second +
                            // Add offset to current line.
                            interleaved_line[static_cast<uint32_t>(planes[sample.plane].interleave_mode)] * line_offsets[sample.plane].first;

                    // Read bits written bits from LSb fractional to MSb integer bit.
                    write_bits( ptr_to_line,
                               b * planes[sample.plane].block_stride + sample.offset,
                               sample.integer_bits + sample.fractional_bits, unorm);

                } while (samples[s++].has_continuation);
            }
        }
    }
}