Skip to content

Commit 8693351

Browse files
committed
fix merge
1 parent 6ce094b commit 8693351

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/codecs/jpeg/encoder.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ impl<W: Write> BitWriter<W> {
250250
self.write_bits(code, size)
251251
}
252252

253+
#[cfg(feature = "benchmarks")]
253254
fn write_block_old(
254255
&mut self,
255256
block: &[i32; 64],
@@ -260,7 +261,7 @@ impl<W: Write> BitWriter<W> {
260261
// Differential DC encoding
261262
let dcval = block[0];
262263
let diff = dcval - prevdc;
263-
let (size, value) = encode_coefficient(diff);
264+
let (size, value) = encode_coefficient_old(diff);
264265

265266
self.huffman_encode(size, dctable)?;
266267
self.write_bits(value, size)?;
@@ -277,7 +278,7 @@ impl<W: Write> BitWriter<W> {
277278
zero_run -= 16;
278279
}
279280

280-
let (size, value) = encode_coefficient(block[k as usize]);
281+
let (size, value) = encode_coefficient_old(block[k as usize]);
281282
let symbol = (zero_run << 4) | size;
282283

283284
self.huffman_encode(symbol, actable)?;
@@ -855,6 +856,27 @@ fn build_quantization_segment(m: &mut Vec<u8>, precision: u8, identifier: u8, qt
855856
}
856857
}
857858

859+
#[cfg(feature = "benchmarks")]
860+
fn encode_coefficient_old(coefficient: i32) -> (u8, u16) {
861+
let mut magnitude = coefficient.unsigned_abs() as u16;
862+
let mut num_bits = 0u8;
863+
864+
while magnitude > 0 {
865+
magnitude >>= 1;
866+
num_bits += 1;
867+
}
868+
869+
let mask = (1 << num_bits as usize) - 1;
870+
871+
let val = if coefficient < 0 {
872+
(coefficient - 1) as u16 & mask
873+
} else {
874+
coefficient as u16 & mask
875+
};
876+
877+
(num_bits, val)
878+
}
879+
858880
#[inline]
859881
fn encode_coefficient(coefficient: i32) -> (u8, u16) {
860882
// since this is inlined, in the main AC case the compiler figures out that coefficient cannot be zero, so BSR on x86 doesn't need a branch

0 commit comments

Comments
 (0)