@@ -563,6 +563,12 @@ static const id_format_pair_t th18_patch[] = {
563563 { 0 , NULL }
564564};
565565
566+ enum FileType {
567+ FILETYPE_BITMAP ,
568+ FILETYPE_PNG ,
569+ FILETYPE_JPEG ,
570+ };
571+
566572static inline int
567573jfif_identify (uint8_t * jfif , uint32_t size ) {
568574 return size >= 11 &&
@@ -1532,7 +1538,7 @@ anm_replace(
15321538 return ;
15331539 }
15341540
1535- int is_png = 0 ;
1541+ int entry_filetype = FILETYPE_BITMAP ;
15361542 if (TH19_OR_NEWER (version )) {
15371543 anm_entry_t * entry = entry_first ;
15381544 const uint32_t ox = option_dont_add_offset_border ? 0 : entry -> header -> x ;
@@ -1553,7 +1559,7 @@ anm_replace(
15531559 fprintf (stderr , "%s: composing %s\n" , argv0 , filename );
15541560 image = malloc (sizeof (image_t ));
15551561 png_read_mem (image , entry -> data , entry -> thtx -> size );
1556- is_png = 1 ;
1562+ entry_filetype = FILETYPE_PNG ;
15571563 } else {
15581564 image = png_read (filename );
15591565 }
@@ -1576,7 +1582,7 @@ anm_replace(
15761582 unsigned int y ;
15771583 format_t fmt = formats [f ];
15781584
1579- if (is_png ) {
1585+ if (entry_filetype == FILETYPE_PNG ) {
15801586 if (fmt != FORMAT_BGRA8888 ) {
15811587 fprintf (stderr , "%s: %s is not FORMAT_BGRA8888\n" , argv0 , entry -> name );
15821588 exit (1 );
@@ -1609,7 +1615,7 @@ anm_replace(
16091615
16101616 free (converted_data );
16111617
1612- if (is_png ) {
1618+ if (entry_filetype == FILETYPE_PNG ) {
16131619 image_t image2 = {
16141620 .data = entry -> data ,
16151621 .width = entry -> thtx -> w ,
@@ -1638,12 +1644,16 @@ anm_replace(
16381644static unsigned char *
16391645entry_to_rgba (
16401646 anm_entry_t * entry ,
1641- int is_png )
1647+ int filetype )
16421648{
1643- if (is_png ) {
1649+ if (filetype == FILETYPE_PNG ) {
16441650 image_t image ;
16451651 png_read_mem (& image , entry -> data , entry -> thtx -> size );
16461652 return image .data ;
1653+ } else if (filetype == FILETYPE_JPEG ) {
1654+ image_t image ;
1655+ jpeg_read_mem (& image , entry -> data , entry -> thtx -> size );
1656+ return image .data ;
16471657 } else {
16481658 return format_to_rgba (entry -> data , entry -> thtx -> w * entry -> thtx -> h , entry -> thtx -> format );
16491659 }
@@ -1681,14 +1691,19 @@ anm_extract(
16811691
16821692 uint32_t ox = option_dont_add_offset_border ? 0 : entry -> header -> x ;
16831693 uint32_t oy = option_dont_add_offset_border ? 0 : entry -> header -> y ;
1684- int is_png = 0 ;
1694+ int filetype = FILETYPE_BITMAP ;
16851695
16861696 if (TH19_OR_NEWER (version )) {
16871697 if (png_identify (entry -> thtx -> data , entry -> thtx -> size ) &&
16881698 (ox || oy || entry -> next_by_name )) {
16891699 if (option_verbose >= 2 )
16901700 fprintf (stderr , "%s: composing %s\n" , argv0 , filename );
1691- is_png = 1 ;
1701+ filetype = FILETYPE_PNG ;
1702+ } else if (jfif_identify (entry -> thtx -> data , entry -> thtx -> size ) &&
1703+ (ox || oy || entry -> next_by_name )) {
1704+ if (option_verbose >= 2 )
1705+ fprintf (stderr , "%s: composing %s\n" , argv0 , filename );
1706+ filetype = FILETYPE_JPEG ;
16921707 } else {
16931708 if (option_verbose >= 2 )
16941709 fprintf (stderr , "%s: not composing %s\n" , argv0 , filename );
@@ -1716,7 +1731,7 @@ anm_extract(
17161731 if (formats [f ] == entryp -> thtx -> format ) {
17171732 ox = option_dont_add_offset_border ? 0 : entryp -> header -> x ;
17181733 oy = option_dont_add_offset_border ? 0 : entryp -> header -> y ;
1719- unsigned char * temp_data = entry_to_rgba (entryp , is_png );
1734+ unsigned char * temp_data = entry_to_rgba (entryp , filetype );
17201735 for (y = oy ; y < oy + entryp -> thtx -> h ; ++ y ) {
17211736 memcpy (image .data + y * image .width * 4 + ox * 4 ,
17221737 temp_data + (y - oy ) * entryp -> thtx -> w * 4 ,
0 commit comments