Skip to content

Commit

Permalink
Fix load_data method #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgrosser committed Jan 10, 2024
1 parent 6130455 commit 46bc5fa
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Rszr 1.4.0 (Jan 11, 2024)

* Fix `load_data` (@mantas)

## Rszr 1.3.0 (Aug 25, 2022)

* Alpha channel control
Expand Down
10 changes: 7 additions & 3 deletions ext/rszr/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static VALUE rszr_image__initialize(VALUE self, VALUE rb_width, VALUE rb_height)
}


static VALUE rszr_image_s__load(VALUE klass, VALUE rb_path)
static VALUE rszr_image_s__load(VALUE klass, VALUE rb_path, VALUE rb_immediately)
{
rszr_image_handle * handle;
Imlib_Image image;
Expand All @@ -64,7 +64,11 @@ static VALUE rszr_image_s__load(VALUE klass, VALUE rb_path)
path = StringValueCStr(rb_path);

imlib_set_cache_size(0);
image = imlib_load_image_without_cache(path);
if (RTEST(rb_immediately)) {
image = imlib_load_image_immediately_without_cache(path);
} else {
image = imlib_load_image_without_cache(path);
}

if (!image) {
image = imlib_load_image_with_error_return(path, &error);
Expand Down Expand Up @@ -668,7 +672,7 @@ void Init_rszr_image()
rb_define_alloc_func(cImage, rszr_image_s_allocate);

// Class methods
rb_define_private_method(rb_singleton_class(cImage), "_load", rszr_image_s__load, 1);
rb_define_private_method(rb_singleton_class(cImage), "_load", rszr_image_s__load, 2);

// Instance methods
rb_define_method(cImage, "width", rszr_image_width, 0);
Expand Down
8 changes: 5 additions & 3 deletions lib/rszr/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class << self
def load(path, autorotate: Rszr.autorotate, **opts)
path = path.to_s
raise FileNotFound unless File.exist?(path)
image = _load(path)
image = _load(path, opts[:immediately])
autorotate(image, path) if autorotate
image
end
Expand All @@ -21,7 +21,7 @@ def load(path, autorotate: Rszr.autorotate, **opts)
def load_data(data, autorotate: Rszr.autorotate, **opts)
raise LoadError, 'Unknown format' unless format = identify(data)
with_tempfile(format, data) do |file|
load(file.path, autorotate: autorotate, **opts)
load(file.path, autorotate: autorotate, **opts.merge(immediately: true))
end
end

Expand All @@ -41,7 +41,9 @@ def format=(fmt)
self._format = fmt
end

alias_method :alpha?, :alpha
def alpha?
!!alpha
end

def [](x, y)
if x >= 0 && x <= width - 1 && y >= 0 && y <= height - 1
Expand Down
2 changes: 1 addition & 1 deletion lib/rszr/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Rszr
VERSION = '1.3.0'
VERSION = '1.4.0'
end
4 changes: 3 additions & 1 deletion spec/rszr_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
end

it 'loads image from memory' do
expect(Rszr::Image.load_data(RSpec.root.join('images/test.jpg').binread).format).to eq('jpeg')
image = Rszr::Image.load_data(RSpec.root.join('images/test.jpg').binread)
expect(image.format).to eq('jpeg')
expect(image[1,1]).to have_attributes(green: 93, blue: 112, red: 78)
end

it 'loads images with uppercase extension' do
Expand Down

0 comments on commit 46bc5fa

Please sign in to comment.