Class: RFreeImage::Image
- Inherits:
-
Object
- Object
- RFreeImage::Image
- Defined in:
- lib/rfreeimage/image.rb,
ext/rfreeimage/rfi_main.c
Class Method Summary collapse
- ._downscale_nocopy(img, max_size) ⇒ Object
- .from_blob(*args) ⇒ Object
- .from_blob_downscale(blob, max_size) ⇒ Object
- .from_bytes(bytes, width, height, stride, bpp) ⇒ Object
- .load_downscale(file, max_size) ⇒ Object
- .ping(file) ⇒ Object
- .ping_blob(blob) ⇒ Object
Instance Method Summary collapse
- #bgra? ⇒ Boolean
- #bpp ⇒ Object
- #buffer_addr ⇒ Object
- #bytes ⇒ Object
- #bytes? ⇒ Boolean
- #clone ⇒ Object
- #cols ⇒ Object (also: #columns)
- #crop(_left, _top, _right, _bottom) ⇒ Object
- #destroy! ⇒ Object
- #downscale(max_size) ⇒ Object
-
#draw_point(_x, _y, color, _size) ⇒ Object
draw.
- #flip_horizontal ⇒ Object
- #flip_vertical ⇒ Object
- #format ⇒ Object
- #gray? ⇒ Boolean
- #initialize(*args) ⇒ Object constructor
- #read_bytes ⇒ Object
- #release ⇒ Object
- #rescale(dst_width, dst_height, filter_type) ⇒ Object
- #resize(width, height, filter = Filter::FILTER_CATMULLROM) ⇒ Object
- #rotate(_angle) ⇒ Object
- #rows ⇒ Object
- #save(file) ⇒ Object (also: #write)
- #stride ⇒ Object
- #to_bgra ⇒ Object
- #to_blob(type) ⇒ Object
- #to_bpp(_bpp) ⇒ Object
- #to_gray ⇒ Object
Constructor Details
#initialize(*args) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'ext/rfreeimage/rfi_main.c', line 184
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
._downscale_nocopy(img, max_size) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rfreeimage/image.rb', line 73 def self._downscale_nocopy img, max_size return img if img.cols <= max_size && img.rows <= max_size nimg = img.downscale max_size img.destroy! nimg end |
.from_blob(*args) ⇒ Object
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'ext/rfreeimage/rfi_main.c', line 574
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
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'ext/rfreeimage/rfi_main.c', line 609
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
562 563 564 565 566 567 568 569 570 571 572 |
# File 'ext/rfreeimage/rfi_main.c', line 562
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
597 598 599 600 601 602 603 604 605 606 607 |
# File 'ext/rfreeimage/rfi_main.c', line 597
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
44 45 46 |
# File 'lib/rfreeimage/image.rb', line 44 def bgra? bpp == ImageBPP::BGRA end |
#bpp ⇒ Object
308 309 310 311 312 313 |
# File 'ext/rfreeimage/rfi_main.c', line 308
static VALUE Image_bpp(VALUE self)
{
struct native_image* img;
Data_Get_Struct(self, struct native_image, img);
return INT2NUM(img->bpp);
}
|
#buffer_addr ⇒ Object
367 368 369 370 371 372 373 374 375 376 |
# File 'ext/rfreeimage/rfi_main.c', line 367
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);
}
|
#bytes ⇒ Object
26 27 28 |
# File 'lib/rfreeimage/image.rb', line 26 def bytes @bytes ||= read_bytes end |
#bytes? ⇒ Boolean
378 379 380 381 382 383 |
# File 'ext/rfreeimage/rfi_main.c', line 378
static VALUE Image_has_bytes(VALUE self)
{
struct native_image* img;
Data_Get_Struct(self, struct native_image, img);
return img->handle ? Qtrue : Qfalse;
}
|
#clone ⇒ Object
428 429 430 431 432 433 434 435 436 437 |
# File 'ext/rfreeimage/rfi_main.c', line 428
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);
}
|
#cols ⇒ Object Also known as: columns
294 295 296 297 298 299 |
# File 'ext/rfreeimage/rfi_main.c', line 294
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
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 |
# File 'ext/rfreeimage/rfi_main.c', line 537
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
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'ext/rfreeimage/rfi_main.c', line 459
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
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_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_horizontal ⇒ Object
513 514 515 516 517 518 519 520 521 522 523 |
# File 'ext/rfreeimage/rfi_main.c', line 513
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_vertical ⇒ Object
525 526 527 528 529 530 531 532 533 534 535 |
# File 'ext/rfreeimage/rfi_main.c', line 525
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);
}
|
#format ⇒ Object
322 323 324 325 326 327 328 329 |
# File 'ext/rfreeimage/rfi_main.c', line 322
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
35 36 37 |
# File 'lib/rfreeimage/image.rb', line 35 def gray? bpp == ImageBPP::GRAY end |
#read_bytes ⇒ Object
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'ext/rfreeimage/rfi_main.c', line 342
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;
}
|
#release ⇒ Object
331 332 333 334 335 336 337 338 339 |
# File 'ext/rfreeimage/rfi_main.c', line 331
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
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'ext/rfreeimage/rfi_main.c', line 439
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
416 417 418 419 420 421 422 423 424 425 426 |
# File 'ext/rfreeimage/rfi_main.c', line 416
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);
}
|
#rows ⇒ Object
301 302 303 304 305 306 |
# File 'ext/rfreeimage/rfi_main.c', line 301
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'ext/rfreeimage/rfi_main.c', line 212
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;
}
|
#stride ⇒ Object
315 316 317 318 319 320 |
# File 'ext/rfreeimage/rfi_main.c', line 315
static VALUE Image_stride(VALUE self)
{
struct native_image* img;
Data_Get_Struct(self, struct native_image, img);
return INT2NUM(img->stride);
}
|
#to_bgra ⇒ Object
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
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'ext/rfreeimage/rfi_main.c', line 246
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
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'ext/rfreeimage/rfi_main.c', line 400
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_gray ⇒ Object
39 40 41 42 |
# File 'lib/rfreeimage/image.rb', line 39 def to_gray return self if gray? to_bpp 8 end |