@@ -144,7 +144,7 @@ struct GIFFrame {
144
144
left : u16 ,
145
145
top : u16 ,
146
146
image : ImgVec < u8 > ,
147
- pal : Vec < RGBA8 > ,
147
+ pal : Vec < RGB8 > ,
148
148
dispose : DisposalMethod ,
149
149
transparent_index : Option < u8 > ,
150
150
}
@@ -424,7 +424,8 @@ impl Writer {
424
424
} ;
425
425
liq. set_quality ( 0 , quality) ?;
426
426
if self . settings . s . quality < 50 {
427
- liq. set_max_colors ( u32:: from ( self . settings . s . quality * 2 ) . max ( 16 ) . next_power_of_two ( ) ) ?;
427
+ let min_colors = 5 + self . fixed_colors . len ( ) as u32 ;
428
+ liq. set_max_colors ( u32:: from ( self . settings . s . quality * 2 ) . max ( min_colors) . next_power_of_two ( ) . min ( 256 ) ) ?;
428
429
}
429
430
let ( buf, width, height) = image. into_contiguous_buf ( ) ;
430
431
let mut img = liq. new_image ( buf, width, height, 0. ) ?;
@@ -759,23 +760,24 @@ impl Writer {
759
760
fn remap_frames ( & self , mut inputs : OrdQueueIter < RemapMessage > , write_queue : Sender < FrameMessage > ) -> CatResult < ( ) > {
760
761
let mut frame_index = 0 ;
761
762
let first_frame = inputs. next ( ) . ok_or ( Error :: NoFrames ) ?;
762
- let mut screen = gif_dispose:: Screen :: new ( first_frame. liq_image . width ( ) , first_frame. liq_image . height ( ) , RGBA8 :: new ( 0 , 0 , 0 , 0 ) , None ) ;
763
+ let mut screen = gif_dispose:: Screen :: new ( first_frame. liq_image . width ( ) , first_frame. liq_image . height ( ) , None ) ;
763
764
764
765
let mut next_frame = Some ( first_frame) ;
765
766
while let Some ( RemapMessage { ordinal_frame_number, end_pts, dispose, liq, remap, liq_image, out_buf, has_next_frame} ) = next_frame {
766
- let screen_width = screen. pixels . width ( ) as u16 ;
767
- let screen_height = screen. pixels . height ( ) as u16 ;
768
- let mut screen_after_dispose = screen. dispose ( ) ;
767
+ let pixels = screen. pixels_rgba ( ) ;
768
+ let screen_width = pixels. width ( ) as u16 ;
769
+ let screen_height = pixels. height ( ) as u16 ;
770
+ let mut screen_after_dispose = screen. dispose_only ( ) ;
769
771
770
- let ( mut image8, mut image8_pal) = {
771
- let bg = if frame_index != 0 { Some ( screen_after_dispose. pixels ( ) ) } else { None } ;
772
+ let ( mut image8, image8_pal) = {
773
+ let bg = if frame_index != 0 { Some ( screen_after_dispose. pixels_rgba ( ) ) } else { None } ;
772
774
self . remap ( liq, remap, liq_image, bg, out_buf) ?
773
775
} ;
774
776
775
- let transparent_index = transparent_index_from_palette ( & mut image8_pal, image8. as_mut ( ) ) ;
777
+ let ( image8_pal , transparent_index) = transparent_index_from_palette ( image8_pal, image8. as_mut ( ) ) ;
776
778
777
779
let ( left, top) = if frame_index != 0 && has_next_frame {
778
- let ( left, top, new_width, new_height) = trim_image ( image8. as_ref ( ) , & image8_pal, transparent_index, dispose, screen_after_dispose. pixels ( ) )
780
+ let ( left, top, new_width, new_height) = trim_image ( image8. as_ref ( ) , & image8_pal, transparent_index, dispose, screen_after_dispose. pixels_rgba ( ) )
779
781
. unwrap_or ( ( 0 , 0 , 1 , 1 ) ) ;
780
782
if new_width != image8. width ( ) || new_height != image8. height ( ) {
781
783
let new_buf = image8. sub_image ( left. into ( ) , top. into ( ) , new_width, new_height) . to_contiguous_buf ( ) . 0 . into_owned ( ) ;
@@ -811,12 +813,12 @@ impl Writer {
811
813
}
812
814
}
813
815
814
- fn transparent_index_from_palette ( image8_pal : & mut [ RGBA < u8 > ] , mut image8 : ImgRefMut < u8 > ) -> Option < u8 > {
816
+ fn transparent_index_from_palette ( mut image8_pal : Vec < RGBA8 > , mut image8 : ImgRefMut < u8 > ) -> ( Vec < RGB8 > , Option < u8 > ) {
815
817
// Palette may have multiple transparent indices :(
816
818
let mut transparent_index = None ;
817
819
for ( i, p) in image8_pal. iter_mut ( ) . enumerate ( ) {
818
820
if p. a <= 128 {
819
- p . a = 0 ;
821
+ * p = RGBA8 :: new ( 71 , 80 , 76 , 0 ) ;
820
822
let new_index = i as u8 ;
821
823
if let Some ( old_index) = transparent_index {
822
824
image8. pixels_mut ( ) . filter ( |px| * * px == new_index) . for_each ( |px| * px = old_index) ;
@@ -831,7 +833,7 @@ fn transparent_index_from_palette(image8_pal: &mut [RGBA<u8>], mut image8: ImgRe
831
833
Some ( idx as u8 ) == transparent_index || color. a > 128 || !image8. pixels( ) . any( |px| px == idx as u8 )
832
834
} ) ) ;
833
835
834
- transparent_index
836
+ ( image8_pal . into_iter ( ) . map ( |r| r . rgb ( ) ) . collect ( ) , transparent_index)
835
837
}
836
838
837
839
/// When one thread unexpectedly fails, all other threads fail with Aborted, but that Aborted isn't the relevant cause
@@ -847,7 +849,7 @@ fn combine_res(res1: Result<(), Error>, res2: Result<(), Error>) -> Result<(), E
847
849
}
848
850
}
849
851
850
- fn trim_image ( mut image_trimmed : ImgRef < u8 > , image8_pal : & [ RGBA8 ] , transparent_index : Option < u8 > , dispose : DisposalMethod , mut screen : ImgRef < RGBA8 > ) -> Option < ( u16 , u16 , usize , usize ) > {
852
+ fn trim_image ( mut image_trimmed : ImgRef < u8 > , image8_pal : & [ RGB8 ] , transparent_index : Option < u8 > , dispose : DisposalMethod , mut screen : ImgRef < RGBA8 > ) -> Option < ( u16 , u16 , usize , usize ) > {
851
853
let is_matching_pixel = move |px : u8 , bg : RGBA8 | -> bool {
852
854
if Some ( px) == transparent_index {
853
855
if dispose == DisposalMethod :: Keep {
@@ -858,7 +860,7 @@ fn trim_image(mut image_trimmed: ImgRef<u8>, image8_pal: &[RGBA8], transparent_i
858
860
bg. a == 0
859
861
}
860
862
} else {
861
- image8_pal. get ( px as usize ) . copied ( ) . unwrap_or_default ( ) == bg
863
+ image8_pal. get ( px as usize ) . map ( |px| px . alpha ( 255 ) ) . unwrap_or_default ( ) == bg
862
864
}
863
865
} ;
864
866
0 commit comments