8
8
9
9
/* data to pass to encoder thread */
10
10
typedef struct {
11
- int complete ;
12
11
const char * path ;
12
+ GifskiError err ;
13
13
gifski * g ;
14
14
} gifski_encoder_thread_info ;
15
15
16
16
/* gifski_write() blocks until main thread calls gifski_end_adding_frames() */
17
17
void * gifski_encoder_thread (void * data ){
18
18
gifski_encoder_thread_info * info = data ;
19
- if (gifski_write (info -> g , info -> path ) != GIFSKI_OK )
20
- REprintf ("Failure writing to '%s'\n" , info -> path );
21
- gifski_drop (info -> g );
22
- info -> complete = 1 ;
19
+ info -> err = gifski_write (info -> g , info -> path );
23
20
return NULL ;
24
21
}
25
22
@@ -39,20 +36,18 @@ SEXP R_png_to_gif(SEXP png_files, SEXP gif_file, SEXP width, SEXP height, SEXP d
39
36
40
37
/* create encoder thread; TODO: maybe we can use multiple encoder threads? */
41
38
pthread_t encoder_thread ;
42
- gifski_encoder_thread_info info = {0 , CHAR (STRING_ELT (gif_file , 0 )), g };
39
+ gifski_encoder_thread_info info = {CHAR (STRING_ELT (gif_file , 0 )), GIFSKI_OK , g };
43
40
if (pthread_create (& encoder_thread , NULL , gifski_encoder_thread , & info ))
44
41
Rf_error ("Failed to create encoder thread" );
45
42
46
43
/* add png frames in main thread */
47
44
for (size_t i = 0 ; i < Rf_length (png_files ); i ++ ){
48
- if (info .complete ){
49
- gifski_drop (g );
50
- Rf_error ("Writer thread has died" );
51
- }
45
+ if (info .err != GIFSKI_OK )
46
+ goto cleanup ;
52
47
if (gifski_add_frame_png_file (g , i , CHAR (STRING_ELT (png_files , i )), Rf_asInteger (delay )) != GIFSKI_OK )
53
48
Rprintf ("Failed to add frame %d\n" , i );
54
49
if (Rf_asLogical (progress ))
55
- Rprintf ("\rFrame %d (%d%%)" , i , (i + 1 ) * 100 / Rf_length (png_files ));
50
+ Rprintf ("\rFrame %d (%d%%)" , ( i + 1 ) , (i + 1 ) * 100 / Rf_length (png_files ));
56
51
}
57
52
58
53
/* This will finalize the encoder thread as well */
@@ -65,6 +60,11 @@ SEXP R_png_to_gif(SEXP png_files, SEXP gif_file, SEXP width, SEXP height, SEXP d
65
60
/* wait for the encoder thread to finish */
66
61
if (pthread_join (encoder_thread , NULL ))
67
62
Rf_error ("Failed to join encoder_thread" );
63
+
64
+ cleanup :
65
+ gifski_drop (g );
66
+ if (info .err != GIFSKI_OK )
67
+ Rf_error ("Failure writing image %s" , info .path );
68
68
return gif_file ;
69
69
}
70
70
0 commit comments