mirror of
https://github.com/qpdf/qpdf.git
synced 2024-09-27 20:49:06 +00:00
Add signed support to BitStream and BitWriter
This commit is contained in:
parent
bf2fb239d7
commit
d9c9049708
@ -30,6 +30,23 @@ BitStream::getBits(int nbits)
|
|||||||
this->bits_available, nbits);
|
this->bits_available, nbits);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
long long
|
||||||
|
BitStream::getBitsSigned(int nbits)
|
||||||
|
{
|
||||||
|
unsigned long long bits = read_bits(this->p, this->bit_offset,
|
||||||
|
this->bits_available, nbits);
|
||||||
|
long long result = 0;
|
||||||
|
if (static_cast<long long>(bits) > 1 << (nbits - 1))
|
||||||
|
{
|
||||||
|
result = static_cast<long long>(bits - (1 << nbits));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = static_cast<long long>(bits);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
BitStream::skipToNextByte()
|
BitStream::skipToNextByte()
|
||||||
{
|
{
|
||||||
|
@ -17,6 +17,21 @@ BitWriter::writeBits(unsigned long long val, unsigned int bits)
|
|||||||
write_bits(this->ch, this->bit_offset, val, bits, this->pl);
|
write_bits(this->ch, this->bit_offset, val, bits, this->pl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
BitWriter::writeBitsSigned(long long val, unsigned int bits)
|
||||||
|
{
|
||||||
|
unsigned long long uval = 0;
|
||||||
|
if (val < 0)
|
||||||
|
{
|
||||||
|
uval = static_cast<unsigned long long>((1 << bits) + val);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
uval = static_cast<unsigned long long>(val);
|
||||||
|
}
|
||||||
|
writeBits(uval, bits);
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
BitWriter::flush()
|
BitWriter::flush()
|
||||||
{
|
{
|
||||||
|
@ -15,6 +15,8 @@ class BitStream
|
|||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
unsigned long long getBits(int nbits);
|
unsigned long long getBits(int nbits);
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
|
long long getBitsSigned(int nbits);
|
||||||
|
QPDF_DLL
|
||||||
void skipToNextByte();
|
void skipToNextByte();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -16,6 +16,8 @@ class BitWriter
|
|||||||
BitWriter(Pipeline* pl);
|
BitWriter(Pipeline* pl);
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
void writeBits(unsigned long long val, unsigned int bits);
|
void writeBits(unsigned long long val, unsigned int bits);
|
||||||
|
QPDF_DLL
|
||||||
|
void writeBitsSigned(long long val, unsigned int bits);
|
||||||
// Force any partial byte to be written to the pipeline.
|
// Force any partial byte to be written to the pipeline.
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
void flush();
|
void flush();
|
||||||
|
@ -124,6 +124,13 @@ test()
|
|||||||
b.skipToNextByte();
|
b.skipToNextByte();
|
||||||
std::cout << b.getBits(8) << std::endl;
|
std::cout << b.getBits(8) << std::endl;
|
||||||
std::cout << std::endl;
|
std::cout << std::endl;
|
||||||
|
b.reset();
|
||||||
|
std::cout << b.getBitsSigned(3) << std::endl;
|
||||||
|
std::cout << b.getBitsSigned(6) << std::endl;
|
||||||
|
std::cout << b.getBitsSigned(5) << std::endl;
|
||||||
|
std::cout << b.getBitsSigned(1) << std::endl;
|
||||||
|
std::cout << b.getBitsSigned(17) << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
// Write tests
|
// Write tests
|
||||||
|
|
||||||
@ -159,6 +166,13 @@ test()
|
|||||||
bw.writeBits(0xABUL, 8);
|
bw.writeBits(0xABUL, 8);
|
||||||
bw.flush();
|
bw.flush();
|
||||||
print_buffer(bp);
|
print_buffer(bp);
|
||||||
|
bw.writeBitsSigned(-1, 3); // 111
|
||||||
|
bw.writeBitsSigned(-12, 6); // 110100
|
||||||
|
bw.writeBitsSigned(4, 3); // 100
|
||||||
|
bw.writeBitsSigned(-4, 3); // 100
|
||||||
|
bw.writeBitsSigned(-1, 1); // 1
|
||||||
|
bw.flush();
|
||||||
|
print_buffer(bp);
|
||||||
|
|
||||||
delete bp;
|
delete bp;
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,12 @@ byte offset = 8, bit offset = 7, bits available = 0
|
|||||||
21
|
21
|
||||||
101
|
101
|
||||||
|
|
||||||
|
-1
|
||||||
|
-22
|
||||||
|
5
|
||||||
|
0
|
||||||
|
-39559
|
||||||
|
|
||||||
ch = f0, bit_offset = 2
|
ch = f0, bit_offset = 2
|
||||||
ch = 00, bit_offset = 6
|
ch = 00, bit_offset = 6
|
||||||
ch = 14, bit_offset = 0
|
ch = 14, bit_offset = 0
|
||||||
@ -56,4 +62,6 @@ f5 15 65 79 12 89 75 4b
|
|||||||
|
|
||||||
f0 ab
|
f0 ab
|
||||||
|
|
||||||
|
fa 49
|
||||||
|
|
||||||
done
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user