Skip to content

Instantly share code, notes, and snippets.

@DarthSim
Last active September 8, 2022 16:57
Show Gist options
  • Save DarthSim/671d1ee8a2f03b5b9077bed494842123 to your computer and use it in GitHub Desktop.
Save DarthSim/671d1ee8a2f03b5b9077bed494842123 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <vips/vips.h>
int main( int argc, char *argv[] ) {
if( argc < 2 ) {
printf("src image path is missing\n");
return 1;
}
if( argc < 3 ) {
printf("dst image path is missing\n");
return 1;
}
vips_init("giftest");
/* Load some GIF
*/
VipsImage *img;
if( vips_gifload(argv[1], &img, "access", VIPS_ACCESS_SEQUENTIAL, "n", -1, NULL) ) {
printf("Error: %s\n", vips_error_buffer());
return 1;
}
/* Fetch its headers
*/
int n_pages, loop, *delay, n_delay;
if(
vips_image_get_int(img, "n-pages", &n_pages) ||
vips_image_get_int(img, "loop", &loop) ||
vips_image_get_array_int(img, "delay", &delay, &n_delay)
) {
printf("Error: %s\n", vips_error_buffer());
return 1;
}
int page_height = img->Ysize / n_pages;
/* Split it into frames
*/
VipsImage **pages = (VipsImage **) vips_object_local_array(VIPS_OBJECT(img), n_pages*2);
for (int i = 0; i < n_pages; i++) {
if( vips_crop (img, &pages[i], 0, i*page_height, img->Xsize, page_height, NULL) ) {
printf("Error: %s\n", vips_error_buffer());
return 1;
}
pages[n_pages+i] = vips_image_copy_memory(pages[i]);
}
/* Join the frames back to a single image
*/
VipsImage *out;
if( vips_arrayjoin(&pages[n_pages], &out, n_pages, "across", 1, NULL) ) {
printf("Error: %s\n", vips_error_buffer());
return 1;
}
g_clear_object(&img);
/* Put the headers back
*/
vips_image_set_int(out, "n-pages", n_pages);
vips_image_set_int(out, "page-height", page_height);
vips_image_set_int(out, "loop", loop);
vips_image_set_array_int(out, "delay", delay, n_delay);
/* Save the new image
*/
if( vips_gifsave (out, argv[2], NULL) ) {
printf("Error: %s\n", vips_error_buffer());
return 1;
}
g_clear_object(&out);
vips_shutdown();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment