Class: Composite::Image

Inherits:
Object
  • Object
show all
Defined in:
ext/giflib/composite.c

Instance Method Summary collapse

Constructor Details

#initialize(rubyGifString) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/giflib/composite.c', line 118

static VALUE initialize(VALUE self, VALUE rubyGifString) {
    struct RubyImage *rubyImage;
    struct RubyString *cGifString;
    int errorCode, i;
    struct GraphicsControlBlock *gcb;
   
    Check_Type(rubyGifString, T_STRING);
    
    Data_Get_Struct(self, struct RubyImage, rubyImage);
    cGifString = getRubyString(rubyGifString);

    // Read gif from Ruby
    if ( (rubyImage->gifFileType = DGifOpen(cGifString, readGifFromMemory, &errorCode)) == NULL ) {
        rb_raise(rb_eException, "Could not read gif, giflib error code %d.", errorCode);
    }
    if (DGifSlurp(rubyImage->gifFileType) == GIF_ERROR) {
        rb_raise(rb_eException, "Could not decode gif, giflib error code %d.", rubyImage->gifFileType->Error);
    }
    if ( (gcb = calloc(rubyImage->gifFileType->ImageCount, sizeof(struct GraphicsControlBlock))) == NULL) {
        rb_raise(rb_eException, "Insufficient memory.");
    }
    
    rubyImage->graphicsControlBlock = gcb;
    for (i = 0; i < rubyImage->gifFileType->ImageCount; i++) {
        readGraphicsControlBlock(rubyImage, i);
    }
    return self;
}

Instance Method Details

#addFrame(image) ⇒ Object



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# File 'ext/giflib/composite.c', line 307

static VALUE addFrame(VALUE self, VALUE image) {
    struct RubyImage *current;
    struct RubyImage *newImage;
    GifFileType *currentGif;
    GifFileType *imageGif;
    GraphicsControlBlock *first;
    GraphicsControlBlock *last;
    
    Data_Get_Struct(self, struct RubyImage, current);
    Data_Get_Struct(image, struct RubyImage, newImage);
    currentGif = current->gifFileType;
    imageGif = newImage->gifFileType;

    GifMakeSavedImage(currentGif, imageGif->SavedImages);

    current->graphicsControlBlock = realloc(current->graphicsControlBlock, sizeof(GraphicsControlBlock) * currentGif->ImageCount);
    if (current->graphicsControlBlock == NULL) {
        rb_raise(rb_eException, "Insufficient memory");
    }
    // Copy contents of first Graphics control block into last Graphics control block (so that it is not jibberish)
    first = current->graphicsControlBlock;
    last = current->graphicsControlBlock + currentGif->ImageCount - 1;
    last->DisposalMode = first->DisposalMode;
    last->UserInputFlag = first->UserInputFlag;
    last->DelayTime = first->DelayTime;
    last->TransparentColor = first->TransparentColor;
    return self;
}

#compose(image, x, y) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'ext/giflib/composite.c', line 275

static VALUE compose(VALUE self, VALUE image, VALUE x, VALUE y) {
	struct RubyImage *current;
	struct RubyImage *compose;
	GifFileType *gifCurrent;
	GifFileType *gifCompose;
	int gifX;
	int gifY;

	Check_Type(x, T_FIXNUM);
	Check_Type(y, T_FIXNUM);

	Data_Get_Struct(self, struct RubyImage, current);
	Data_Get_Struct(image, struct RubyImage, compose);
	gifCurrent = current->gifFileType;
	gifCompose = compose->gifFileType;
	gifX = FIX2INT(x);
	gifY = FIX2INT(y);

	if (gifX < 0) {
		rb_raise(rb_eException, "Compose x value must be >= 0 (not %d).", gifX);
	} else if (gifY < 0) {
		rb_raise(rb_eException, "Compose y value must be >= 0 (not %d).", gifY);
	} else if (gifX + gifCompose->SWidth > gifCurrent->SWidth) {
		rb_raise(rb_eException, "Composite would extend over the end of the current image (current image width: %d, x: %d, composite image width %d)", gifCurrent->SWidth, gifX, gifCompose->SWidth);
	} else if (gifY + gifCompose->SHeight > gifCurrent->SHeight) {
		rb_raise(rb_eException, "Composite would extend over the end of the current image (current image height: %d, y: %d, composite image height %d)", gifCurrent->SHeight, gifY, gifCompose->SHeight);
	}
	
	giflibCompose(gifCurrent, gifCompose, gifX, gifY);
	return Qnil;
}

#encodeObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'ext/giflib/composite.c', line 219

static VALUE encode(VALUE self) {
	struct RubyImage *rubyImage;
	struct RubyString *rubyString;
    int i;
    VALUE result;
    
    Data_Get_Struct(self, struct RubyImage, rubyImage);

	rubyString = newRubyString();
    for (i = 0; i < rubyImage->gifFileType->ImageCount; i++) {
        writeGraphicsControlBlock(rubyImage, i);
    }
	writeToMemory(rubyImage->gifFileType, rubyString);
    
	result = rb_str_new(rubyString->text, rubyString->current);
    return result;
}

#getDelayTimeForFrame(frame) ⇒ Object



356
357
358
359
360
361
362
363
364
365
# File 'ext/giflib/composite.c', line 356

static VALUE getDelayTimeForFrame(VALUE self, VALUE frame) {
    struct RubyImage *current;
    int frameIndex, delayTime;
    
    Data_Get_Struct(self, struct RubyImage, current);
    frameIndex = FIX2INT(frame);
    delayTime = getDelayTimeFromGraphicsControlBlock(current, frameIndex);
    
    return INT2FIX(delayTime);
}

#getHeightObject



243
244
245
246
247
# File 'ext/giflib/composite.c', line 243

static VALUE getHeight(VALUE self) {
	struct RubyImage * rubyImage;
	Data_Get_Struct(self, struct RubyImage, rubyImage);
	return INT2FIX(rubyImage->gifFileType->SHeight);
}

#getImageCountObject



249
250
251
252
253
# File 'ext/giflib/composite.c', line 249

static VALUE getImageCount(VALUE self) {
	struct RubyImage * rubyImage;
	Data_Get_Struct(self, struct RubyImage, rubyImage);
	return INT2FIX(rubyImage->gifFileType->ImageCount);
}

#getNFramesObject



336
337
338
339
340
341
# File 'ext/giflib/composite.c', line 336

static VALUE getNFrames(VALUE self) {
    struct RubyImage *current;
    
    Data_Get_Struct(self, struct RubyImage, current);
    return INT2FIX(current->gifFileType->ImageCount);
}

#getSavedImageExtensionBlockCount(index) ⇒ Object



367
368
369
370
371
372
373
374
# File 'ext/giflib/composite.c', line 367

static VALUE getSavedImageExtensionBlockCount(VALUE self, VALUE index) {
    struct RubyImage *current;
    int intIndex;
    
    Data_Get_Struct(self, struct RubyImage, current);
    intIndex = FIX2INT(index);
    return INT2FIX(current->gifFileType->SavedImages[intIndex].ExtensionBlockCount);
}

#getWidthObject



237
238
239
240
241
# File 'ext/giflib/composite.c', line 237

static VALUE getWidth(VALUE self) {
	struct RubyImage * rubyImage;
	Data_Get_Struct(self, struct RubyImage, rubyImage);
	return INT2FIX(rubyImage->gifFileType->SWidth);
}

#setDelayTimeForFrame(frame, delay) ⇒ Object



343
344
345
346
347
348
349
350
351
352
353
354
# File 'ext/giflib/composite.c', line 343

static VALUE setDelayTimeForFrame(VALUE self, VALUE frame, VALUE delay) {
    // http://giflib.sourceforge.net/gif_lib.html#idp49255104
    struct RubyImage *current;
    int frameIndex, delayTime;
    
    Data_Get_Struct(self, struct RubyImage, current);
    frameIndex = FIX2INT(frame);
    delayTime = FIX2INT(delay);
    
    setDelayTimeForGraphicsControlBlock(current, frameIndex, delayTime);
    return Qnil;
}