@@ -1412,7 +1412,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt
14121412 return DU [0 ];
14131413}
14141414
1415- static int stbi_write_jpg_core (stbi__write_context * s , int width , int height , int comp , const void * data , int quality ) {
1415+ static int stbi_write_jpg_core (stbi__write_context * s , int width , int height , int comp , const void * data , int quality , const char * parameters ) {
14161416 // Constants that don't pollute global namespace
14171417 static const unsigned char std_dc_luminance_nrcodes [] = {0 ,0 ,1 ,5 ,1 ,1 ,1 ,1 ,1 ,1 ,0 ,0 ,0 ,0 ,0 ,0 ,0 };
14181418 static const unsigned char std_dc_luminance_values [] = {0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 };
@@ -1521,6 +1521,20 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in
15211521 s -> func (s -> context , (void * )YTable , sizeof (YTable ));
15221522 stbiw__putc (s , 1 );
15231523 s -> func (s -> context , UVTable , sizeof (UVTable ));
1524+
1525+ // comment block with parameters of generation
1526+ if (parameters != NULL ) {
1527+ stbiw__putc (s , 0xFF /* comnent */ );
1528+ stbiw__putc (s , 0xFE /* marker */ );
1529+ size_t param_length = std ::min (2 + strlen ("parameters" ) + 1 + strlen (parameters ) + 1 , (size_t ) 0xFFFF );
1530+ stbiw__putc (s , param_length >> 8 ); // no need to mask, length < 65536
1531+ stbiw__putc (s , param_length & 0xFF );
1532+ s -> func (s -> context , (void * )"parameters" , strlen ("parameters" ) + 1 ); // std::string is zero-terminated
1533+ s -> func (s -> context , (void * )parameters , std ::min (param_length , (size_t ) 65534 ) - 2 - strlen ("parameters" ) - 1 );
1534+ if (param_length > 65534 ) stbiw__putc (s , 0 ); // always zero-terminate for safety
1535+ if (param_length & 1 ) stbiw__putc (s , 0xFF ); // pad to even length
1536+ }
1537+
15241538 s -> func (s -> context , (void * )head1 , sizeof (head1 ));
15251539 s -> func (s -> context , (void * )(std_dc_luminance_nrcodes + 1 ), sizeof (std_dc_luminance_nrcodes )- 1 );
15261540 s -> func (s -> context , (void * )std_dc_luminance_values , sizeof (std_dc_luminance_values ));
@@ -1625,16 +1639,16 @@ STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x,
16251639{
16261640 stbi__write_context s = { 0 };
16271641 stbi__start_write_callbacks (& s , func , context );
1628- return stbi_write_jpg_core (& s , x , y , comp , (void * ) data , quality );
1642+ return stbi_write_jpg_core (& s , x , y , comp , (void * ) data , quality , NULL );
16291643}
16301644
16311645
16321646#ifndef STBI_WRITE_NO_STDIO
1633- STBIWDEF int stbi_write_jpg (char const * filename , int x , int y , int comp , const void * data , int quality )
1647+ STBIWDEF int stbi_write_jpg (char const * filename , int x , int y , int comp , const void * data , int quality , const char * parameters )
16341648{
16351649 stbi__write_context s = { 0 };
16361650 if (stbi__start_write_file (& s ,filename )) {
1637- int r = stbi_write_jpg_core (& s , x , y , comp , data , quality );
1651+ int r = stbi_write_jpg_core (& s , x , y , comp , data , quality , parameters );
16381652 stbi__end_write_file (& s );
16391653 return r ;
16401654 } else
0 commit comments