@@ -1412,7 +1412,7 @@ static int stbiw__jpg_processDU(stbi__write_context *s, int *bitBuf, int *bitCnt
1412
1412
return DU [0 ];
1413
1413
}
1414
1414
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 ) {
1416
1416
// Constants that don't pollute global namespace
1417
1417
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 };
1418
1418
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
1521
1521
s -> func (s -> context , (void * )YTable , sizeof (YTable ));
1522
1522
stbiw__putc (s , 1 );
1523
1523
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
+
1524
1538
s -> func (s -> context , (void * )head1 , sizeof (head1 ));
1525
1539
s -> func (s -> context , (void * )(std_dc_luminance_nrcodes + 1 ), sizeof (std_dc_luminance_nrcodes )- 1 );
1526
1540
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,
1625
1639
{
1626
1640
stbi__write_context s = { 0 };
1627
1641
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 );
1629
1643
}
1630
1644
1631
1645
1632
1646
#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 )
1634
1648
{
1635
1649
stbi__write_context s = { 0 };
1636
1650
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 );
1638
1652
stbi__end_write_file (& s );
1639
1653
return r ;
1640
1654
} else
0 commit comments