Skip to content

Commit

Permalink
Drop fiddle, use C
Browse files Browse the repository at this point in the history
  • Loading branch information
mtgrosser committed Mar 7, 2019
1 parent 6e049aa commit aef8014
Show file tree
Hide file tree
Showing 19 changed files with 513 additions and 241 deletions.
2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.3.8
2.6.1
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ begin
rescue LoadError
# no rspec available
end

require 'rake/extensiontask'

Rake::ExtensionTask.new('rszr') do |ext|
ext.lib_dir = 'lib/rszr'
end
40 changes: 40 additions & 0 deletions benchmark/memory.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require 'memory_profiler'
require 'rszr'
require 'mini_magick'
require 'gd2-ffij'

ITERATIONS = 100

original = Pathname.new(__FILE__).dirname.join('../spec/images/test.jpg')
resized = Pathname.new(__FILE__).dirname.join('output.jpg')

mini_magick = MemoryProfiler.report do
ITERATIONS.times do
image = MiniMagick::Image.open(original.to_s)
image.resize '800x532'
image.write resized.to_s
image = nil
end
end

mini_magick.pretty_print(scale_bytes: true)

gd2 = MemoryProfiler.report do
image = GD2::Image.import(original.to_s)
image.resize! 800, 532
image.export resized.to_s
image = nil
end

gd2.pretty_print(scale_bytes: true)

rszr = MemoryProfiler.report do
ITERATIONS.times do
image = Rszr::Image.load(original.to_s)
image.resize! 800, 532
image.save resized.to_s
image = nil
end
end

rszr.pretty_print(scale_bytes: true)
5 changes: 4 additions & 1 deletion benchmark/benchmark.rb → benchmark/speed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
image = MiniMagick::Image.open(original.to_s)
image.resize '800x532'
image.write resized.to_s
image = nil
end
end

x.report 'GD2' do
ITERATIONS.times do
image = GD2::Image.import(original.to_s)
image.resize! 800, 532
image.export resized.to_s
image = nil
end
end

Expand All @@ -30,6 +32,7 @@
image = Rszr::Image.load(original.to_s)
image.resize! 800, 532
image.save resized.to_s
image = nil
end
end
end
63 changes: 63 additions & 0 deletions ext/rszr/errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef RUBY_RSZR_ERRORS
#define RUBY_RSZR_ERRORS

#include "rszr.h"
#include "errors.h"

VALUE eRszrError = Qnil;
VALUE eRszrFileNotFound = Qnil;
VALUE eRszrTransformationError = Qnil;
VALUE eRszrErrorWithMessage = Qnil;
VALUE eRszrLoadError = Qnil;
VALUE eRszrSaveError = Qnil;

static const char * const sRszrErrorMessages[] =
{
"File does not exist",
"File is a directory",
"Read permission denied",
"Unsupported format",
"Path too long",
"Non-existant path component",
"Path component is not a directory",
"Path outside address space",
"Too many symbolic links",
"Out of memory",
"Out of file descriptors",
"Write permission denied",
"Out of disk space",
"Unknown error"
};
const int RSZR_MAX_ERROR_INDEX = 13;

void Init_rszr_errors()
{
eRszrError = rb_define_class_under(mRszr, "Error", rb_eStandardError);
eRszrFileNotFound = rb_define_class_under(mRszr, "FileNotFound", eRszrError);
eRszrTransformationError = rb_define_class_under(mRszr, "TransformationError", eRszrError);
eRszrErrorWithMessage = rb_define_class_under(mRszr, "ErrorWithMessage", eRszrError);
eRszrLoadError = rb_define_class_under(mRszr, "LoadError", eRszrErrorWithMessage);
eRszrSaveError = rb_define_class_under(mRszr, "SaveError", eRszrErrorWithMessage);
}

static void rszr_raise_error_with_message(VALUE rb_error_class, Imlib_Load_Error error)
{
int error_index = (int) error - 1;
if (error_index < 1 || error_index > RSZR_MAX_ERROR_INDEX)
error_index = 13;
VALUE rb_error = rb_exc_new2(rb_error_class, sRszrErrorMessages[error_index]);
rb_exc_raise(rb_error);
}

void rszr_raise_load_error(Imlib_Load_Error error)
{
rszr_raise_error_with_message(eRszrLoadError, error);
}

void rszr_raise_save_error(Imlib_Load_Error error)
{
rszr_raise_error_with_message(eRszrSaveError, error);
}


#endif
15 changes: 15 additions & 0 deletions ext/rszr/errors.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef RUBY_RSZR_ERRORS_H
#define RUBY_RSZR_ERRORS_H

void Init_rszr_errors();
void rszr_raise_load_error(Imlib_Load_Error error);
void rszr_raise_save_error(Imlib_Load_Error error);

extern VALUE eRszrError;
extern VALUE eRszrFileNotFound;
extern VALUE eRszrTransformationError;
extern VALUE eRszrErrorWithMessage;
extern VALUE eRszrLoadError;
extern VALUE eRszrSaveError;

#endif
37 changes: 37 additions & 0 deletions ext/rszr/extconf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'mkmf'
require 'rbconfig'
=begin
LIBDIR = RbConfig::CONFIG['libdir']
INCLUDEDIR = RbConfig::CONFIG['includedir']
HEADER_DIRS = [
'/opt/local/include',
'/usr/local/include',
INCLUDEDIR,
'/usr/include',
]
LIB_DIRS = [
'/opt/local/lib',
'/usr/local/lib',
LIBDIR,
'/usr/lib',
]
dir_config('Imlib2', HEADER_DIRS, LIB_DIRS)
=end
imlib2_config = with_config('imlib2-config', 'imlib2-config')

$CFLAGS << ' -DX_DISPLAY_MISSING ' << `#{imlib2_config} --cflags`.chomp
$LDFLAGS << ' ' << `#{imlib2_config} --libs`.chomp
$LDFLAGS.gsub!(/\ -lX11\ -lXext/, '') if RUBY_PLATFORM =~ /darwin/

unless find_header('Imlib2.h')
abort 'imlib2 development headers are missing'
end

unless find_library('imlib2', 'imlib_set_cache_size')
abort 'imlib2 is missing'
end

create_makefile 'rszr'
Loading

0 comments on commit aef8014

Please sign in to comment.