rfreeimage 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f1966079f9e10d783543577040e00a934b11dfd7
4
- data.tar.gz: 5662dfe703c70495676d945bdaa24e0b92fef816
3
+ metadata.gz: dea720ff7ff2d05a1a87c52999ad187daacc09d1
4
+ data.tar.gz: 4c617e08df0c12dfc6cb791264a4388a61194375
5
5
  SHA512:
6
- metadata.gz: 82c912070bb034f7ee8ee955ab13945b2018f57cc47c8f8bf77a3d239d8c80fa82dadc8cdd2e32ccee20ecc759a9170a1fb8379525398d3e4759cbd1b520ed01
7
- data.tar.gz: bda3b9a43fc0418ce7de317c151927cb91a40db8895275520377daf1efc6ccd4d02132c7f9eef58ff8259a5e12706cdda40806316a003e124490a917e8dd55f8
6
+ metadata.gz: 4f226e0e67c1e32cdc83a5b8c12009fe56db8a5d1699a9acb7db78bdf9b14b18caec5dd4a720eefb09ea704b1ca3fc77da4b20720ce195a28a703890835a7726
7
+ data.tar.gz: ad4c922848a717c938b05511424e170754eff6f1bac293e0f38f1d872a62d93bad999b017ed84bc5466840b56204bfe523723be7c9ac3b3968064a33108e05c6
@@ -85,7 +85,8 @@ convert_bpp(FIBITMAP *orig, unsigned int bpp) {
85
85
  }
86
86
 
87
87
  static void
88
- rd_image(VALUE clazz, VALUE file, struct native_image *img, unsigned int bpp, BOOL ping)
88
+ rd_image(VALUE clazz, VALUE file, struct native_image *img, unsigned int bpp, BOOL ping,
89
+ int max_size_hint)
89
90
  {
90
91
  char *filename;
91
92
  int flags = 0;
@@ -99,8 +100,11 @@ rd_image(VALUE clazz, VALUE file, struct native_image *img, unsigned int bpp, BO
99
100
  free(filename);
100
101
  rb_raise(rb_eIOError, "Invalid image file");
101
102
  }
103
+ if (max_size_hint < 0 || max_size_hint > 65535)
104
+ rb_raise(rb_eArgError, "Invalid max_size_hint");
102
105
 
103
106
  if (ping) flags |= FIF_LOAD_NOPIXELS;
107
+ if (!ping) flags |= max_size_hint << 16;
104
108
  // use JPEG_ACCURATE to keep sync with opencv
105
109
  if (in_fif == FIF_JPEG)
106
110
  flags |= JPEG_EXIFROTATE | JPEG_ACCURATE;
@@ -125,7 +129,7 @@ rd_image(VALUE clazz, VALUE file, struct native_image *img, unsigned int bpp, BO
125
129
  }
126
130
 
127
131
  static void
128
- rd_image_blob(VALUE clazz, VALUE blob, struct native_image *img, unsigned int bpp, BOOL ping)
132
+ rd_image_blob(VALUE clazz, VALUE blob, struct native_image *img, unsigned int bpp, BOOL ping, int max_size_hint)
129
133
  {
130
134
  FIBITMAP *h = NULL, *orig = NULL;
131
135
  FIMEMORY *fmh;
@@ -140,8 +144,11 @@ rd_image_blob(VALUE clazz, VALUE blob, struct native_image *img, unsigned int bp
140
144
  FreeImage_CloseMemory(fmh);
141
145
  rb_raise(rb_eIOError, "Invalid image blob");
142
146
  }
147
+ if (max_size_hint < 0 || max_size_hint > 65535)
148
+ rb_raise(rb_eArgError, "Invalid max_size_hint");
143
149
 
144
150
  if (ping) flags |= FIF_LOAD_NOPIXELS;
151
+ if (!ping) flags |= max_size_hint << 16;
145
152
  if (in_fif == FIF_JPEG)
146
153
  flags |= JPEG_EXIFROTATE | JPEG_ACCURATE;
147
154
  orig = FreeImage_LoadFromMemory(in_fif, fmh, flags);
@@ -174,10 +181,13 @@ VALUE Image_initialize(int argc, VALUE *argv, VALUE self)
174
181
  switch (argc)
175
182
  {
176
183
  case 1:
177
- rd_image(self, argv[0], img, 0, 0);
184
+ rd_image(self, argv[0], img, 0, 0, 0);
178
185
  break;
179
186
  case 2:
180
- rd_image(self, argv[0], img, NUM2INT(argv[1]), 0);
187
+ rd_image(self, argv[0], img, NUM2INT(argv[1]), 0, 0);
188
+ break;
189
+ case 3:
190
+ rd_image(self, argv[0], img, NUM2INT(argv[1]), 0, NUM2INT(argv[2]));
181
191
  break;
182
192
  default:
183
193
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
@@ -437,6 +447,60 @@ VALUE Image_rescale(VALUE self, VALUE dst_width, VALUE dst_height, VALUE filter_
437
447
  return rfi_get_image(nh);
438
448
  }
439
449
 
450
+ VALUE Image_downscale(VALUE self, VALUE max_size) {
451
+ // down-sample resize
452
+ struct native_image *img;
453
+ FIBITMAP *nh;
454
+ int i;
455
+ int j;
456
+ int w;
457
+ int h;
458
+ int scale;
459
+ int mlen;
460
+ int msize = NUM2INT(max_size);
461
+
462
+ Data_Get_Struct(self, struct native_image, img);
463
+ RFI_CHECK_IMG(img);
464
+
465
+ mlen = img->w > img->h ? img->w : img->h;
466
+ if (msize <= 0 || msize >= mlen) {
467
+ nh = FreeImage_Copy(img->handle, 0, 0, img->w, img->h);
468
+ } else {
469
+ unsigned char *ph;
470
+ unsigned char *pnh;
471
+ int dst_stride;
472
+ if (img->bpp != 8 && img->bpp != 32)
473
+ rb_raise(rb_eArgError, "bpp not supported");
474
+
475
+ scale = (mlen + msize - 1) / msize;
476
+ w = img->w / scale;
477
+ h = img->h / scale;
478
+ nh = FreeImage_Allocate(w, h, img->bpp, 0, 0, 0);
479
+ if (!nh)
480
+ rb_raise(rb_eArgError, "fail to allocate image");
481
+
482
+ ph = FreeImage_GetBits(img->handle);
483
+ pnh = FreeImage_GetBits(nh);
484
+ dst_stride = FreeImage_GetPitch(nh);
485
+ if (img->bpp == 8) {
486
+ for(i = 0; i < h; i++) {
487
+ for(j = 0; j < w; j++)
488
+ *(pnh + j) = *(ph + j * scale);
489
+ ph += img->stride * scale;
490
+ pnh += dst_stride;
491
+ }
492
+ } else if (img->bpp == 32) {
493
+ for(i = 0; i < h; i++) {
494
+ for(j = 0; j < w; j++)
495
+ *((unsigned int*)pnh + j) = *((unsigned int*)ph + j * scale);
496
+ ph += img->stride * scale;
497
+ pnh += dst_stride;
498
+ }
499
+ }
500
+ }
501
+ return rfi_get_image(nh);
502
+ }
503
+
440
504
  VALUE Image_crop(VALUE self, VALUE _left, VALUE _top, VALUE _right, VALUE _bottom)
441
505
  {
442
506
  struct native_image *img;
@@ -466,7 +530,7 @@ VALUE Image_ping(VALUE self, VALUE file)
466
530
  {
467
531
  ALLOC_NEW_IMAGE(v, img);
468
532
 
469
- rd_image(self, file, img, 0, 1);
533
+ rd_image(self, file, img, 0, 1, 0);
470
534
  if (img->handle)
471
535
  FreeImage_Unload(img->handle);
472
536
  img->handle = NULL;
@@ -481,10 +545,13 @@ VALUE Image_from_blob(int argc, VALUE *argv, VALUE self)
481
545
  switch (argc)
482
546
  {
483
547
  case 1:
484
- rd_image_blob(self, argv[0], img, 0, 0);
548
+ rd_image_blob(self, argv[0], img, 0, 0, 0);
485
549
  break;
486
550
  case 2:
487
- rd_image_blob(self, argv[0], img, NUM2INT(argv[1]), 0);
551
+ rd_image_blob(self, argv[0], img, NUM2INT(argv[1]), 0, 0);
552
+ break;
553
+ case 3:
554
+ rd_image_blob(self, argv[0], img, NUM2INT(argv[1]), 0, NUM2INT(argv[2]));
488
555
  break;
489
556
  default:
490
557
  rb_raise(rb_eArgError, "wrong number of arguments (%d for 1)", argc);
@@ -498,7 +565,7 @@ VALUE Image_ping_blob(VALUE self, VALUE blob)
498
565
  {
499
566
  ALLOC_NEW_IMAGE(v, img);
500
567
 
501
- rd_image_blob(self, blob, img, 0, 1);
568
+ rd_image_blob(self, blob, img, 0, 1, 0);
502
569
  if (img->handle)
503
570
  FreeImage_Unload(img->handle);
504
571
  img->handle = NULL;
@@ -635,6 +702,7 @@ void Init_rfreeimage(void)
635
702
  rb_define_method(Class_Image, "to_bpp", Image_to_bpp, 1);
636
703
  rb_define_method(Class_Image, "rotate", Image_rotate, 1);
637
704
  rb_define_method(Class_Image, "rescale", Image_rescale, 3);
705
+ rb_define_method(Class_Image, "downscale", Image_downscale, 1);
638
706
  rb_define_method(Class_Image, "crop", Image_crop, 4);
639
707
  rb_define_method(Class_Image, "to_blob", Image_to_blob, 1);
640
708
 
@@ -54,8 +54,27 @@ module RFreeImage
54
54
  return self.rescale(width, height, filter)
55
55
  end
56
56
 
57
+ def self.load_downscale file, max_size
58
+ # only work on jpeg
59
+ img = Image.new file, 0, max_size
60
+ _downscale_nocopy img, max_size
61
+ end
62
+
63
+ def self.from_blob_downscale blob, max_size
64
+ # only work on jpeg
65
+ img = Image.from_blob blob, 0, max_size
66
+ _downscale_nocopy img, max_size
67
+ end
57
68
 
58
69
  alias_method :write, :save
59
70
  alias_method :columns, :cols
60
- end
71
+
72
+ private
73
+ def self._downscale_nocopy img, max_size
74
+ return img if img.cols <= max_size && img.rows <= max_size
75
+ nimg = img.downscale max_size
76
+ img.destroy!
77
+ nimg
78
+ end
79
+ end
61
80
  end
@@ -1,3 +1,3 @@
1
1
  module RFreeImage
2
- VERSION = '0.1.6'
2
+ VERSION = '0.1.7'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rfreeimage
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuheng Chen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-10 00:00:00.000000000 Z
11
+ date: 2015-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler