Class: GD::Gif

Inherits:
Object
  • Object
show all
Defined in:
ext/gd/gif.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

GD::Gif.new(“file.gif”, loop: true)



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'ext/gd/gif.c', line 54

static VALUE gd_gif_initialize(int argc, VALUE *argv, VALUE self) {
  VALUE path, opts;
  rb_scan_args(argc, argv, "11", &path, &opts);

  gd_gif_wrapper *gif;
  TypedData_Get_Struct(self, gd_gif_wrapper, &gd_gif_type, gif);

  const char *filename = StringValueCStr(path);
  gif->out = fopen(filename, "wb");
  if (!gif->out) rb_sys_fail(filename);

  gif->started = 0;
  gif->prev = NULL;
  gif->prev_rb = Qnil;

  gif->loop = 0;
  if (opts != Qnil) {
    VALUE loopv = rb_hash_aref(opts, ID2SYM(rb_intern("loop")));
    if (loopv == Qfalse) {
      gif->loop = -1;
    }
  }

  return self;
}

Instance Method Details

#add_frame(*args) ⇒ Object

gif.add_frame(img, delay: 50)



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'ext/gd/gif.c', line 83

static VALUE gd_gif_add_frame(int argc, VALUE *argv, VALUE self) {
  VALUE rb_img, opts;
  rb_scan_args(argc, argv, "11", &rb_img, &opts);

  gd_image_wrapper *wrap;
  TypedData_Get_Struct(rb_img, gd_image_wrapper, &gd_image_type, wrap);

  gd_gif_wrapper *gif;
  TypedData_Get_Struct(self, gd_gif_wrapper, &gd_gif_type, gif);

  int delay = 50;
  if (opts != Qnil) {
    VALUE d = rb_hash_aref(opts, ID2SYM(rb_intern("delay")));
    if (!NIL_P(d)) delay = NUM2INT(d);
  }

  if (!gif->started) {
    gdImageGifAnimBegin(wrap->img, gif->out, 1, gif->loop);
    gif->started = 1;
  }

  gdImageGifAnimAdd(
    wrap->img,
    gif->out,
    1,
    0, 0,
    delay,
    1,
    gif->prev
  );

  gif->prev = wrap->img;
  gif->prev_rb = rb_img;   /* prevent GC */

  return Qtrue;
}

#closeObject

gif.close



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'ext/gd/gif.c', line 123

static VALUE gd_gif_close(VALUE self) {
  gd_gif_wrapper *gif;
  TypedData_Get_Struct(self, gd_gif_wrapper, &gd_gif_type, gif);

  if (gif->out) {
    if (gif->started) {
      gdImageGifAnimEnd(gif->out);
    }
    fclose(gif->out);
    gif->out = NULL;
  }

  gif->prev = NULL;
  gif->prev_rb = Qnil;

  return Qtrue;
}