77#include <string.h>
88
99#include "stable-diffusion.h"
10- #include "./gif.h" // charlietangora/gif-h
10+
11+ #ifndef MSF_GIF_IMPL
12+ #define MSF_GIF_IMPL
13+ #endif
14+ #include "./msf_gif.h" //notnullnotvoid/msf_gif
15+
1116
1217#ifndef INCLUDE_STB_IMAGE_WRITE_H
1318#include "stb_image_write.h"
@@ -396,37 +401,25 @@ int create_mjpg_avi_membuf_from_sd_images(sd_image_t* images, int num_images, in
396401// ---------------- Helper: create_gif_buf_from_sd_images ----------------
397402// Builds a GIF in memory from an array of sd_image_t. Returns 0 on success, -1 on failure.
398403// Caller must free(*out_data) when done.
399- int create_gif_buf_from_sd_images (sd_image_t * images , int num_images , int fps , int quality , uint8_t * * out_data , size_t * out_len )
404+ int create_gif_buf_from_sd_images (sd_image_t * images , int num_images , int fps , uint8_t * * out_data , size_t * out_len )
400405{
401406 if (!images || num_images <= 0 || !out_data || !out_len ) return -1 ;
402407
403408 // basic parameter heuristics
404409 if (fps <= 0 ) fps = 16 ;
405410 uint32_t delay = (uint32_t )(100 / fps ); // hundredths of a second per frame
406411
407- // map quality [1..100] to bitDepth and dithering
408- if (quality < 1 ) quality = 1 ;
409- if (quality > 100 ) quality = 100 ;
410412 int bitDepth = 8 ;
411413 bool dither = false;
412- // if(quality >= 80) { bitDepth = 8; dither = false; }
413- // else if(quality >= 50) { bitDepth = 6; dither = false; }
414- // else { bitDepth = 5; dither = true; }
415414
416415 // assume all images same size; use first
417416 uint32_t width = images [0 ].width ;
418417 uint32_t height = images [0 ].height ;
419418
420- GifWriter gw ;
421- memset (& gw , 0 , sizeof (gw ));
422-
423- if (!GifBegin (& gw , width , height , delay , bitDepth , dither ))
424- {
425- if (gw .oldImage ) GIF_FREE (gw .oldImage );
426-
427- fprintf (stderr , "Error: GifBegin failed.\n" );
428- return -1 ;
429- }
419+ int centisecondsPerFrame = delay ;
420+ int quality = 16 ;
421+ MsfGifState gifState = {};
422+ msf_gif_begin (& gifState , width , height );
430423
431424 // Feed frames
432425 for (int i = 0 ; i < num_images ; i ++ )
@@ -435,8 +428,6 @@ int create_gif_buf_from_sd_images(sd_image_t* images, int num_images, int fps, i
435428
436429 if (img -> width != width || img -> height != height ) {
437430 fprintf (stderr , "Frame %d has mismatched dimensions.\n" , i );
438- GifEnd (& gw );
439- memfile_free (& gw .mem );
440431 return -1 ;
441432 }
442433
@@ -445,8 +436,6 @@ int create_gif_buf_from_sd_images(sd_image_t* images, int num_images, int fps, i
445436 int channels = img -> channel ;
446437 if (channels != 3 && channels != 4 ) {
447438 fprintf (stderr , "Unsupported channel count: %d\n" , channels );
448- GifEnd (& gw );
449- memfile_free (& gw .mem );
450439 return -1 ;
451440 }
452441
@@ -464,11 +453,10 @@ int create_gif_buf_from_sd_images(sd_image_t* images, int num_images, int fps, i
464453 frame_rgba = img -> data ; // already RGBA
465454 }
466455
467- if (!GifWriteFrame (& gw , frame_rgba , width , height , delay , bitDepth , dither ))
456+ int res = msf_gif_frame (& gifState , frame_rgba , centisecondsPerFrame , quality , width * 4 ); //frame
457+ if (!res )
468458 {
469459 fprintf (stderr , "GIF Write Failed\n" );
470- GifEnd (& gw );
471- memfile_free (& gw .mem );
472460 return -1 ;
473461 }
474462
@@ -477,20 +465,9 @@ int create_gif_buf_from_sd_images(sd_image_t* images, int num_images, int fps, i
477465 }
478466 }
479467
480- if (!GifEnd (& gw ))
481- {
482- memfile_free (& gw .mem );
483- return -1 ;
484- }
485-
486- uint8_t * buf = memfile_detach (& gw .mem , out_len );
487- if (!buf )
488- {
489- memfile_free (& gw .mem );
490- return -1 ;
491- }
492-
493- * out_data = buf ;
468+ MsfGifResult result = msf_gif_end (& gifState );
469+ * out_data = (uint8_t * )result .data ;
470+ * out_len = result .dataSize ;
494471 return 0 ;
495472}
496473
0 commit comments