Skip to content

Commit 3e9da15

Browse files
committed
2.0.5: 3 changes
- Follow cargo clippy - Remove unneeded println! - Make "not found" index in index_stream as black transparent pixel
1 parent 6726d67 commit 3e9da15

2 files changed

Lines changed: 28 additions & 30 deletions

File tree

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ permissions:
1010
'on':
1111
push:
1212
branches:
13-
- main
13+
- "2.x.x"
1414
tags-ignore:
1515
- '**'
1616
paths-ignore:

src/lib.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl Gif {
3535
for frame in frames_iter {
3636
buffers.push(frame.decode());
3737
}
38-
return buffers;
38+
buffers
3939
}
4040
}
4141

@@ -66,7 +66,7 @@ impl Frame {
6666
#[napi]
6767
pub fn decode(&self) -> Buffer {
6868
let mut buffer: Vec<u8> = Vec::new();
69-
for index in (&self.index_stream).into_iter() {
69+
for index in self.index_stream.iter() {
7070
match self.color_table.get(*index as usize) {
7171
Some(color) => {
7272
buffer.push(color.red.try_into().unwrap());
@@ -81,10 +81,7 @@ impl Frame {
8181
}
8282
}
8383
None => {
84-
for _ in 0..3 {
85-
buffer.push(255);
86-
}
87-
buffer.push(0);
84+
buffer.resize(buffer.len() + 4, 0);
8885
}
8986
}
9087
}
@@ -144,28 +141,26 @@ pub struct Color {
144141
pub blue: u32,
145142
}
146143

147-
///
148-
149144
#[napi(js_name = "Decoder")]
150145
struct Decoder {}
151146

152147
#[napi]
153148
impl Decoder {
154149
#[napi]
155150
pub fn decode_path(file_path: String) -> Result<Gif> {
156-
let contents = match std::fs::read(&file_path) {
151+
let contents = match std::fs::read(file_path) {
157152
Ok(contents) => contents,
158153
Err(err) => return Err(Error::from_reason(err.to_string())),
159154
};
160155
let contents = contents.as_slice();
161-
return Self::decode_internal(contents);
156+
Self::decode_internal(contents)
162157
}
163158

164159
#[napi]
165160
pub fn decode_buffer(buffer: Buffer) -> Result<Gif> {
166161
let contents: Vec<u8> = buffer.into();
167162
let contents = contents.as_slice();
168-
return Self::decode_internal(contents);
163+
Self::decode_internal(contents)
169164
}
170165

171166
fn decode_internal(contents: &[u8]) -> Result<Gif> {
@@ -210,7 +205,7 @@ impl Decoder {
210205
let mut offset: usize = 13;
211206

212207
// Global Color Table
213-
let length: usize = 3 * 2 << gif.lsd.global_color_size;
208+
let length: usize = (3 * 2) << gif.lsd.global_color_size;
214209
let mut i: usize = offset;
215210

216211
if gif.lsd.global_color_flag {
@@ -258,7 +253,7 @@ impl Decoder {
258253
green: (green as u32),
259254
blue: (blue as u32),
260255
});
261-
i = i + 3;
256+
i += 3;
262257
}
263258
Self::increment_offset(&mut offset, length);
264259
gif.global_table = global_color_vector;
@@ -333,7 +328,7 @@ impl Decoder {
333328
// Trailer
334329
#[cfg(debug_assertions)]
335330
println!("End of file.");
336-
return Ok(gif);
331+
Ok(gif)
337332
}
338333
fn skip(offset: &mut usize, contents: &[u8]) -> Result<()> {
339334
loop {
@@ -431,7 +426,7 @@ impl Decoder {
431426
))
432427
}
433428
};
434-
return Ok(());
429+
Ok(())
435430
}
436431
fn handle_graphic_control_extension(
437432
offset: &mut usize,
@@ -516,7 +511,7 @@ impl Decoder {
516511
println!("Image Descriptor Offset: {}", *offset);
517512

518513
let frame_index = gif.frames.len() - 1;
519-
let mut parsed_frame = &mut gif.frames[frame_index];
514+
let parsed_frame = &mut gif.frames[frame_index];
520515

521516
match contents.get(*offset..*offset + 2) {
522517
Some(left_bytes) => {
@@ -587,7 +582,7 @@ impl Decoder {
587582

588583
// Local Color Table (Check local color table flag)
589584
if (packed_field & 0b1000_0000) != 0 {
590-
let length: usize = 3 * 2 << (packed_field & 0b0000_0111) as u32;
585+
let length: usize = (3 * 2) << (packed_field & 0b0000_0111) as u32;
591586
let mut i: usize = *offset;
592587
let mut local_color_vector: Vec<Color> = Vec::new();
593588

@@ -630,7 +625,7 @@ impl Decoder {
630625
green: (green as u32),
631626
blue: (blue as u32),
632627
});
633-
i = i + 3;
628+
i += 3;
634629
}
635630
Self::increment_offset(offset, length);
636631
parsed_frame.color_table = local_color_vector;
@@ -699,7 +694,7 @@ impl Decoder {
699694
}
700695
};
701696
Self::increment_offset(offset, 1);
702-
if data_sub_blocks_count <= 0 {
697+
if data_sub_blocks_count == 0 {
703698
break;
704699
}
705700
let offset_add: usize = *offset + data_sub_blocks_count as usize;
@@ -744,18 +739,18 @@ impl Decoder {
744739
}
745740
in_code = code;
746741
if code == available {
747-
*pixel_stack.index_mut(top as usize) = first as u8;
742+
*pixel_stack.index_mut(top) = first;
748743
top += 1;
749744
code = old_code as u32;
750745
}
751746
while code > clear_code {
752-
*pixel_stack.index_mut(top as usize) = suffix[code as usize];
747+
*pixel_stack.index_mut(top) = suffix[code as usize];
753748
top += 1;
754749
code = prefix[code as usize] as u32;
755750
}
756-
first = suffix[code as usize] & 0xFF;
751+
first = suffix[code as usize];
757752

758-
*pixel_stack.index_mut(top as usize) = first;
753+
*pixel_stack.index_mut(top) = first;
759754
top += 1;
760755

761756
if available < MAX_STACK_SIZE as u32 {
@@ -773,17 +768,15 @@ impl Decoder {
773768
index_stream.push(pixel_stack[top]);
774769
n += 1;
775770
}
776-
for _ in index_stream.len()..npix as usize {
777-
index_stream.push(0);
778-
}
771+
index_stream.resize(npix as usize, 0);
779772
if parsed_frame.im.interlace_flag {
780773
index_stream = Self::deinterlace(&mut index_stream, parsed_frame.im.width as usize);
781774
}
782775
parsed_frame.index_stream = index_stream;
783776
Ok(())
784777
}
785778
// deinterlace function from https://github.com/matt-way/gifuct-js/blob/master/src/deinterlace.js
786-
fn deinterlace(index_stream: &mut Vec<u8>, width: usize) -> Vec<u8> {
779+
fn deinterlace(index_stream: &mut [u8], width: usize) -> Vec<u8> {
787780
let mut new_index_stream = vec![0; index_stream.len()];
788781
let rows = index_stream.len() / width;
789782

@@ -804,7 +797,7 @@ impl Decoder {
804797
to_row += steps[pass];
805798
}
806799
}
807-
return new_index_stream;
800+
new_index_stream
808801
}
809802
fn handle_plain_text_extension(offset: &mut usize, gif: &mut Gif, contents: &[u8]) -> Result<()> {
810803
// Plain Text Extension (Optional)
@@ -861,7 +854,12 @@ impl Decoder {
861854
Ok(parsed_application) => {
862855
application = parsed_application;
863856
}
864-
Err(err) => println!("Attempt to get application failed: {}", err),
857+
Err(err) => {
858+
return Err(Error::from_reason(format!(
859+
"Attempt to get application failed: {}",
860+
err
861+
)))
862+
}
865863
},
866864
None => {
867865
return Err(Error::from_reason(

0 commit comments

Comments
 (0)