@ -31,13 +31,17 @@ boolean
empty_closure_output_buffer ( j_compress_ptr cinfo ) {
Nan : : HandleScope scope ;
closure_destination_mgr * dest = ( closure_destination_mgr * ) cinfo - > dest ;
Local < Object > buf = Nan : : NewBuffer ( ( char * ) dest - > buffer , dest - > bufsize ) . ToLocalChecked ( ) ;
Local < Value > argv [ 3 ] = {
// emit "data"
Local < Value > argv [ 2 ] = {
Nan : : Null ( )
, buf
, Nan : : New < Integer > ( dest - > bufsize )
} ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 3 , argv ) ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 2 , argv ) ;
dest - > buffer = ( JOCTET * ) malloc ( dest - > bufsize ) ;
cinfo - > dest - > next_output_byte = dest - > buffer ;
cinfo - > dest - > free_in_buffer = dest - > bufsize ;
return true ;
@ -47,26 +51,24 @@ void
term_closure_destination ( j_compress_ptr cinfo ) {
Nan : : HandleScope scope ;
closure_destination_mgr * dest = ( closure_destination_mgr * ) cinfo - > dest ;
/* emit remaining data */
size_t remaining = dest - > bufsize - cinfo - > dest - > free_in_buffer ;
Local < Object > buf = Nan : : NewBuffer ( ( char * ) dest - > buffer , remaining ) . ToLocalChecked ( ) ;
Local < Object > buf = Nan : : NewBuffer ( ( char * ) dest - > buffer , dest - > bufsize ) . ToLocalChecked ( ) ;
Local < Value > data_argv [ 3 ] = {
Local < Value > data_argv [ 2 ] = {
Nan : : Null ( )
, buf
, Nan : : New < Number > ( remaining )
} ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 3 , data_argv ) ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 2 , data_argv ) ;
// emit "end"
Local < Value > end_argv [ 3 ] = {
Local < Value > end_argv [ 2 ] = {
Nan : : Null ( )
, Nan : : Null ( )
, Nan : : New < Integer > ( 0 )
} ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 3 , end_argv ) ;
Nan : : MakeCallback ( Nan : : GetCurrentContext ( ) - > Global ( ) , ( v8 : : Local < v8 : : Function > ) dest - > closure - > fn , 2 , end_argv ) ;
}
void
@ -82,7 +84,7 @@ jpeg_closure_dest(j_compress_ptr cinfo, closure_t * closure, int bufsize){
sizeof ( closure_destination_mgr ) ) ;
}
dest = ( closure_destination_mgr * ) cinfo - > dest ;
dest = ( closure_destination_mgr * ) cinfo - > dest ;
cinfo - > dest - > init_destination = & init_closure_destination ;
cinfo - > dest - > empty_output_buffer = & empty_closure_output_buffer ;
@ -96,16 +98,6 @@ jpeg_closure_dest(j_compress_ptr cinfo, closure_t * closure, int bufsize){
cinfo - > dest - > free_in_buffer = dest - > bufsize ;
}
void
jpeg_free_custom_allocations ( j_compress_ptr cinfo ) {
closure_destination_mgr * dest ;
dest = ( closure_destination_mgr * ) cinfo - > dest ;
if ( dest - > buffer ) {
free ( dest - > buffer ) ;
dest - > buffer = NULL ;
}
}
void
write_to_jpeg_stream ( cairo_surface_t * surface , int bufsize , int quality , bool progressive , closure_t * closure ) {
int w = cairo_image_surface_get_width ( surface ) ;
@ -148,7 +140,6 @@ write_to_jpeg_stream(cairo_surface_t *surface, int bufsize, int quality, bool pr
}
free ( dst ) ;
jpeg_finish_compress ( & cinfo ) ;
jpeg_free_custom_allocations ( & cinfo ) ;
jpeg_destroy_compress ( & cinfo ) ;
}