Class: RFreeImage::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/rfreeimage/image.rb,
ext/rfreeimage/rfi_main.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'ext/rfreeimage/rfi_main.c', line 263

static VALUE Image_initialize(int argc, VALUE *argv, VALUE self)
{
	struct native_image* img;
	/* unwrap */
	Data_Get_Struct(self, struct native_image, img);

	switch (argc)
	{
		case 1:
			rd_image(self, argv[0], img, 0, 0, 0);
			break;
		case 2:
			rd_image(self, argv[0], img, NUM2INT(argv[1]), 0, 0);
			break;
		case 3:
			rd_image(self, argv[0], img, NUM2INT(argv[1]), 0, NUM2INT(argv[2]));
			break;
		default:
			rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
			break;
	}

	return self;
}

Class Method Details

.from_blob(*args) ⇒ Object



653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
# File 'ext/rfreeimage/rfi_main.c', line 653

static VALUE Image_from_blob(int argc, VALUE *argv, VALUE self)
{
	ALLOC_NEW_IMAGE(v, img);

	switch (argc)
	{
		case 1:
			rd_image_blob(self, argv[0], img, 0, 0, 0);
			break;
		case 2:
			rd_image_blob(self, argv[0], img, NUM2INT(argv[1]), 0, 0);
			break;
		case 3:
			rd_image_blob(self, argv[0], img, NUM2INT(argv[1]), 0, NUM2INT(argv[2]));
			break;
		default:
			rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
			break;
	}

	return v;
}

.from_blob_downscale(blob, max_size) ⇒ Object



63
64
65
66
67
# File 'lib/rfreeimage/image.rb', line 63

def self.from_blob_downscale blob, max_size
  # only work on jpeg
  img = Image.from_blob blob, 0, max_size
  _downscale_nocopy img, max_size
end

.from_bytes(bytes, width, height, stride, bpp) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
# File 'ext/rfreeimage/rfi_main.c', line 688

static VALUE Image_from_bytes(VALUE self, VALUE bytes, VALUE width,
		VALUE height, VALUE stride, VALUE bpp)
{
	long f_len;
	int _bpp = NUM2INT(bpp);
	char *ptr;
	unsigned char *p;
	int i;
	int src_stride = NUM2INT(stride);
	FIBITMAP *h;

	ALLOC_NEW_IMAGE(v, img);

	if (_bpp != 8 && _bpp != 32)
		rb_raise(rb_eArgError, "bpp must be 8 or 32");
	Check_Type(bytes, T_STRING);
	f_len = RSTRING_LEN(bytes);
	if (f_len < (long)src_stride * NUM2INT(height))
		rb_raise(rb_eArgError, "buffer too small");

	h = FreeImage_Allocate(NUM2INT(width), NUM2INT(height), _bpp, 0, 0, 0);
	if (!h)
		rb_raise(rb_eArgError, "fail to allocate image");

	img->handle = h;
	img->w = FreeImage_GetWidth(h);
	img->h = FreeImage_GetHeight(h);
	img->bpp = FreeImage_GetBPP(h);
	img->stride = FreeImage_GetPitch(h);
	img->fif = FIF_BMP;

	/* up-side-down */
	p = FreeImage_GetBits(img->handle);
	ptr = RSTRING_PTR(bytes) + img->h * src_stride;
	for(i = 0; i < img->h; i++) {
		ptr -= src_stride;
		memcpy(p, ptr, img->stride);
		p += img->stride;
	}

	return v;
}

.load_downscale(file, max_size) ⇒ Object



57
58
59
60
61
# File 'lib/rfreeimage/image.rb', line 57

def self.load_downscale file, max_size
  # only work on jpeg
  img = Image.new file, 0, max_size
  _downscale_nocopy img, max_size
end

.ping(file) ⇒ Object



641
642
643
644
645
646
647
648
649
650
651
# File 'ext/rfreeimage/rfi_main.c', line 641

static VALUE Image_ping(VALUE self, VALUE file)
{
	ALLOC_NEW_IMAGE(v, img);

	rd_image(self, file, img, 0, 1, 0);
	if (img->handle)
		FreeImage_Unload(img->handle);
	img->handle = NULL;

	return v;
}

.ping_blob(blob) ⇒ Object



676
677
678
679
680
681
682
683
684
685
686
# File 'ext/rfreeimage/rfi_main.c', line 676

static VALUE Image_ping_blob(VALUE self, VALUE blob)
{
	ALLOC_NEW_IMAGE(v, img);

	rd_image_blob(self, blob, img, 0, 1, 0);
	if (img->handle)
		FreeImage_Unload(img->handle);
	img->handle = NULL;

	return v;
}

Instance Method Details

#bgra?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/rfreeimage/image.rb', line 44

def bgra?
	bpp == ImageBPP::BGRA
end

#bppObject



387
388
389
390
391
392
# File 'ext/rfreeimage/rfi_main.c', line 387

static VALUE Image_bpp(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	return INT2NUM(img->bpp);
}

#buffer_addrObject



446
447
448
449
450
451
452
453
454
455
# File 'ext/rfreeimage/rfi_main.c', line 446

static VALUE Image_buffer_addr(VALUE self)
{
	struct native_image* img;
	const char *p;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	p = (const char*)FreeImage_GetBits(img->handle);
	return ULONG2NUM((uintptr_t)p);
}

#bytesObject



26
27
28
# File 'lib/rfreeimage/image.rb', line 26

def bytes
	@bytes ||= read_bytes
end

#bytes?Boolean

Returns:

  • (Boolean)


457
458
459
460
461
462
# File 'ext/rfreeimage/rfi_main.c', line 457

static VALUE Image_has_bytes(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	return img->handle ? Qtrue : Qfalse;
}

#cloneObject



507
508
509
510
511
512
513
514
515
516
# File 'ext/rfreeimage/rfi_main.c', line 507

static VALUE Image_clone(VALUE self)
{
	struct native_image *img;
	FIBITMAP *nh;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	nh = FreeImage_Clone(img->handle);
	return rfi_get_image(nh);
}

#colsObject Also known as: columns



373
374
375
376
377
378
# File 'ext/rfreeimage/rfi_main.c', line 373

static VALUE Image_cols(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	return INT2NUM(img->w);
}

#crop(_left, _top, _right, _bottom) ⇒ Object



616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
# File 'ext/rfreeimage/rfi_main.c', line 616

static VALUE Image_crop(VALUE self, VALUE _left, VALUE _top, VALUE _right, VALUE _bottom)
{
	struct native_image *img;
	FIBITMAP *nh;
	int left = NUM2INT(_left);
	int top = NUM2INT(_top);
	int right = NUM2INT(_right);
	int bottom = NUM2INT(_bottom);
	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	if (left < 0 || top < 0
		|| right < left || bottom < top
		|| bottom > img->h || right > img->w )
		rb_raise(rb_eArgError, "Invalid boundary");

	nh = FreeImage_Copy(img->handle, left, top, right, bottom);
	return rfi_get_image(nh);
}

#destroy!Object



30
31
32
33
# File 'lib/rfreeimage/image.rb', line 30

def destroy!
	release
	@bytes = nil
end

#downscale(max_size) ⇒ Object



538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
# File 'ext/rfreeimage/rfi_main.c', line 538

static VALUE Image_downscale(VALUE self, VALUE max_size) {
	// down-sample resize
	struct native_image *img;
	FIBITMAP *nh;
	int i;
	int j;
	int w;
	int h;
	int scale;
	int mlen;
	int msize = NUM2INT(max_size);

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	mlen = img->w > img->h ? img->w : img->h;
	if (msize <= 0 || msize >= mlen) {
		nh = FreeImage_Copy(img->handle, 0, 0, img->w, img->h);
	} else {
		unsigned char *ph;
		unsigned char *pnh;
		int dst_stride;
		if (img->bpp != 8 && img->bpp != 32)
			rb_raise(rb_eArgError, "bpp not supported");

		scale = (mlen + msize - 1)  / msize;
		w = img->w / scale;
		h = img->h / scale;
		nh = FreeImage_Allocate(w, h, img->bpp, 0, 0, 0);
		if (!nh)
			rb_raise(rb_eArgError, "fail to allocate image");

		ph = FreeImage_GetBits(img->handle);
		pnh = FreeImage_GetBits(nh);
		dst_stride = FreeImage_GetPitch(nh);
		if (img->bpp == 8) {
			for(i = 0; i < h; i++) {
				for(j = 0; j < w; j++)
					*(pnh + j) = *(ph + j * scale);
				ph += img->stride * scale;
				pnh += dst_stride;
			}
		} else if (img->bpp == 32) {
			for(i = 0; i < h; i++) {
				for(j = 0; j < w; j++)
					*((unsigned int*)pnh + j) = *((unsigned int*)ph + j * scale);
				ph += img->stride * scale;
				pnh += dst_stride;
			}
		}
	}
	return rfi_get_image(nh);
}

#draw_point(_x, _y, color, _size) ⇒ Object

draw



732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
# File 'ext/rfreeimage/rfi_main.c', line 732

static VALUE Image_draw_point(VALUE self, VALUE _x, VALUE _y, VALUE color, VALUE _size)
{
	struct native_image* img;
	int x = NUM2INT(_x);
	int y = NUM2INT(_y);
	int size = NUM2INT(_size);
	int hs = size / 2, i, j;
	unsigned int bgra = NUM2UINT(color);
	if (size < 0)
		rb_raise(rb_eArgError, "Invalid point size: %d", size);
	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	for(i = -hs; i <= hs; i++) {
		for(j = -hs; j <= hs; j++) {
			if (i*i + j*j <= hs*hs)
				FreeImage_SetPixelColor(img->handle, x + i, img->h - (y + j) - 1, (RGBQUAD*)&bgra);
		}
	}

	return self;
}

#flip_horizontalObject



592
593
594
595
596
597
598
599
600
601
602
# File 'ext/rfreeimage/rfi_main.c', line 592

static VALUE Image_flip_horizontal(VALUE self) {
	struct native_image *img;
	FIBITMAP *nh;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	nh = FreeImage_Clone(img->handle);
	if(FreeImage_FlipHorizontal(nh) == FALSE)
		rb_raise(Class_RFIError, "Malloc Failed");
	return rfi_get_image(nh);
}

#flip_verticalObject



604
605
606
607
608
609
610
611
612
613
614
# File 'ext/rfreeimage/rfi_main.c', line 604

static VALUE Image_flip_vertical(VALUE self) {
	struct native_image *img;
	FIBITMAP *nh;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	nh = FreeImage_Clone(img->handle);
	if(FreeImage_FlipVertical(nh) == FALSE)
		rb_raise(Class_RFIError, "Malloc Failed");
	return rfi_get_image(nh);
}

#formatObject



401
402
403
404
405
406
407
408
# File 'ext/rfreeimage/rfi_main.c', line 401

static VALUE Image_format(VALUE self)
{
	struct native_image* img;
	const char *p;
	Data_Get_Struct(self, struct native_image, img);
	p = FreeImage_GetFormatFromFIF(img->fif);
	return rb_str_new(p, strlen(p));
}

#gray?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rfreeimage/image.rb', line 35

def gray?
	bpp == ImageBPP::GRAY
end

#read_bytesObject



421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# File 'ext/rfreeimage/rfi_main.c', line 421

static VALUE Image_read_bytes(VALUE self)
{
	struct native_image* img;
	const char *p;
	char *ptr;
	unsigned stride_dst;
	int i;
	VALUE v;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	p = (const char*)FreeImage_GetBits(img->handle);
	stride_dst = img->w * (img->bpp / 8);
	v = rb_str_new(NULL, stride_dst * img->h);

	/* up-side-down */
	ptr = RSTRING_PTR(v) + img->h * stride_dst;
	for(i = 0; i < img->h; i++) {
		ptr -= stride_dst;
		memcpy(ptr, p, stride_dst);
		p += img->stride;
	}
	return v;
}

#releaseObject



410
411
412
413
414
415
416
417
418
# File 'ext/rfreeimage/rfi_main.c', line 410

static VALUE Image_release(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	if (img->handle)
		FreeImage_Unload(img->handle);
	img->handle = NULL;
	return Qnil;
}

#rescale(dst_width, dst_height, filter_type) ⇒ Object



518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'ext/rfreeimage/rfi_main.c', line 518

static VALUE Image_rescale(VALUE self, VALUE dst_width, VALUE dst_height, VALUE filter_type)
{
	struct native_image *img;
	FIBITMAP *nh;
	int w = NUM2INT(dst_width);
	int h = NUM2INT(dst_height);
	int f = NUM2INT(filter_type);
	if (w <= 0 || h <= 0)
		rb_raise(rb_eArgError, "Invalid size");

	if (f < FILTER_BOX || f > FILTER_LANCZOS3)
		rb_raise(rb_eArgError, "Invalid image filter type");

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	nh = FreeImage_Rescale(img->handle, w, h, f);
	return rfi_get_image(nh);
}

#resize(width, height, filter = Filter::FILTER_CATMULLROM) ⇒ Object



53
54
55
# File 'lib/rfreeimage/image.rb', line 53

def resize(width, height, filter = Filter::FILTER_CATMULLROM)
	return self.rescale(width, height, filter)
end

#rotate(_angle) ⇒ Object



495
496
497
498
499
500
501
502
503
504
505
# File 'ext/rfreeimage/rfi_main.c', line 495

static VALUE Image_rotate(VALUE self, VALUE _angle)
{
	struct native_image *img;
	FIBITMAP *nh;
	double angle = NUM2DBL(_angle);

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	nh = FreeImage_Rotate(img->handle, angle, NULL);
	return rfi_get_image(nh);
}

#rowsObject



380
381
382
383
384
385
# File 'ext/rfreeimage/rfi_main.c', line 380

static VALUE Image_rows(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	return INT2NUM(img->h);
}

#save(file) ⇒ Object Also known as: write



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'ext/rfreeimage/rfi_main.c', line 291

static VALUE Image_save(VALUE self, VALUE file)
{
	char *filename;
	struct native_image* img;
	BOOL result;
	FREE_IMAGE_FORMAT out_fif;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	Check_Type(file, T_STRING);
	filename = rfi_value_to_str(file);

	out_fif = FreeImage_GetFIFFromFilename(filename);
	if (out_fif == FIF_UNKNOWN) {
		free(filename);
		rb_raise(Class_RFIError, "Invalid format");
	}

	if (out_fif == FIF_JPEG && img->bpp != 8 && img->bpp != 24) {
		FIBITMAP *to_save = FreeImage_ConvertTo24Bits(img->handle);
		result = FreeImage_Save(out_fif, to_save, filename, JPEG_BASELINE);
		FreeImage_Unload(to_save);
	} else {
		result = FreeImage_Save(out_fif, img->handle, filename, 0);
	}

	free(filename);

	if(!result)
		rb_raise(rb_eIOError, "Fail to save image");
	return Qnil;
}

#strideObject



394
395
396
397
398
399
# File 'ext/rfreeimage/rfi_main.c', line 394

static VALUE Image_stride(VALUE self)
{
	struct native_image* img;
	Data_Get_Struct(self, struct native_image, img);
	return INT2NUM(img->stride);
}

#to_bgraObject



48
49
50
51
# File 'lib/rfreeimage/image.rb', line 48

def to_bgra
	return self if bgra?
	to_bpp 32
end

#to_blob(type) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# File 'ext/rfreeimage/rfi_main.c', line 325

static VALUE Image_to_blob(VALUE self, VALUE type)
{
	char *filetype;
	struct native_image* img;
	FIMEMORY *hmem;
	BOOL result;
	FREE_IMAGE_FORMAT out_fif;
	VALUE ret;
	BYTE *raw = NULL;
	DWORD file_size;

	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);

	Check_Type(type, T_STRING);
	filetype = rfi_value_to_str(type);

	out_fif = FreeImage_GetFIFFromFormat(filetype);
	free(filetype);
	if (out_fif == FIF_UNKNOWN)
		rb_raise(Class_RFIError, "Invalid format");

	hmem = FreeImage_OpenMemory(0, 0);
	if (!hmem)
		rb_raise(rb_eIOError, "Fail to allocate blob");
	if (out_fif == FIF_JPEG && img->bpp != 8 && img->bpp != 24) {
		FIBITMAP *to_save = FreeImage_ConvertTo24Bits(img->handle);
		result = FreeImage_SaveToMemory(out_fif, to_save, hmem, JPEG_BASELINE);
		FreeImage_Unload(to_save);
	} else {
		result = FreeImage_SaveToMemory(out_fif, img->handle, hmem, 0);
	}

	if(!result) {
		FreeImage_CloseMemory(hmem);
		rb_raise(rb_eIOError, "Fail to save image to blob");
	}
	file_size = FreeImage_TellMemory(hmem);
	FreeImage_SeekMemory(hmem, 0, SEEK_SET);
	FreeImage_AcquireMemory(hmem, &raw, &file_size);
	ret = rb_str_new((char*)raw, (long)file_size);
	FreeImage_CloseMemory(hmem);
	return ret;
}

#to_bpp(_bpp) ⇒ Object



479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'ext/rfreeimage/rfi_main.c', line 479

static VALUE Image_to_bpp(VALUE self, VALUE _bpp)
{
	struct native_image *img;
	FIBITMAP *nh;
	int bpp = NUM2INT(_bpp);
	Data_Get_Struct(self, struct native_image, img);
	RFI_CHECK_IMG(img);
	if (bpp == img->bpp)
		return self;

	nh = convert_bpp(img->handle, bpp);
	if (!nh) rb_raise(rb_eArgError, "Invalid bpp");

	return rfi_get_image(nh);
}

#to_grayObject



39
40
41
42
# File 'lib/rfreeimage/image.rb', line 39

def to_gray
	return self if gray?
	to_bpp 8
end