colordom 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'bundler/setup'
4
- require 'colordom'
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require 'pry'
11
- # Pry.start
12
-
13
- require 'irb'
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
data/colordom.gemspec DELETED
@@ -1,29 +0,0 @@
1
- require_relative 'lib/colordom/version'
2
-
3
- Gem::Specification.new do |spec|
4
- spec.name = 'colordom'
5
- spec.version = Colordom::VERSION
6
- spec.authors = ['Jonian Guveli']
7
- spec.email = ['jonian@hardpixel.eu']
8
-
9
- spec.summary = %q{Extract dominant colors from images}
10
- spec.description = %q{Extract dominant colors from images using native extension implemented in Rust.}
11
- spec.homepage = 'https://github.com/hardpixel/colordom'
12
- spec.license = 'MIT'
13
-
14
- # Specify which files should be added to the gem when it is released.
15
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
16
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
17
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|samples)/}) }
18
- end
19
-
20
- spec.extensions = ['ext/Rakefile']
21
- spec.require_paths = ['lib']
22
-
23
- spec.add_runtime_dependency 'ffi'
24
- spec.add_runtime_dependency 'thermite'
25
-
26
- spec.add_development_dependency 'bundler', '~> 2.0'
27
- spec.add_development_dependency 'minitest', '~> 5.0'
28
- spec.add_development_dependency 'rake', '~> 13.0'
29
- end
data/ext/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require 'thermite/tasks'
2
-
3
- project_dir = File.dirname(File.dirname(__FILE__))
4
-
5
- Thermite::Tasks.new(
6
- cargo_project_path: project_dir,
7
- ruby_project_path: project_dir
8
- )
9
-
10
- task default: %w[thermite:build]
@@ -1,16 +0,0 @@
1
- module Colordom
2
- module Native
3
- extend FFI::Library
4
-
5
- LIBNAME = "colordom.#{FFI::Platform::LIBSUFFIX}".freeze
6
- LIBPATH = File.expand_path('..', __dir__).freeze
7
-
8
- ffi_lib File.expand_path(LIBNAME, LIBPATH)
9
-
10
- attach_function :to_histogram, :to_histogram, [:string], Result
11
- attach_function :to_mediancut, :to_mediancut, [:string, :uint8], Result
12
- attach_function :to_kmeans, :to_kmeans, [:string, :uint], Result
13
-
14
- attach_function :free, :free_result, [Result], :void
15
- end
16
- end
@@ -1,9 +0,0 @@
1
- module Colordom
2
- class Result < FFI::AutoPointer
3
- class << self
4
- def release(ptr)
5
- Native.free(ptr)
6
- end
7
- end
8
- end
9
- end
data/src/lib.rs DELETED
@@ -1,99 +0,0 @@
1
- use std::os::raw::c_char;
2
- use std::ffi::{CStr, CString};
3
-
4
- use image;
5
- use dominant_color;
6
- use kmeans_colors;
7
-
8
- use palette_extract::{Quality, MaxColors, PixelEncoding, PixelFilter};
9
- use palette::{FromColor, IntoColor, Lab, Pixel, Srgb};
10
-
11
- fn to_string(unsafe_string: *const c_char) -> String {
12
- unsafe { CStr::from_ptr(unsafe_string) }.to_str().unwrap().to_string()
13
- }
14
-
15
- fn to_char(string: String) -> *mut c_char {
16
- CString::new(string).unwrap().into_raw()
17
- }
18
-
19
- #[no_mangle]
20
- pub extern "C" fn to_histogram(path: *const c_char) -> *mut c_char {
21
- let img = match image::open(&to_string(path)) {
22
- Ok(res) => res,
23
- Err(ex) => return to_char(format!("Colordom::Error {:?}", ex.to_string()))
24
- };
25
-
26
- let has_alpha = match img.color() {
27
- image::ColorType::Rgba8 => true,
28
- image::ColorType::Rgba16 => true,
29
- image::ColorType::Rgba32F => true,
30
- _ => false
31
- };
32
-
33
- let pixels = img.as_bytes();
34
- let result = dominant_color::get_colors(&pixels, has_alpha);
35
-
36
- return to_char(format!("{:?}", result));
37
- }
38
-
39
- #[no_mangle]
40
- pub extern "C" fn to_mediancut(path: *const c_char, max_colors: u8) -> *mut c_char {
41
- let img = match image::open(&to_string(path)) {
42
- Ok(res) => res,
43
- Err(ex) => return to_char(format!("Colordom::Error {:?}", ex.to_string()))
44
- };
45
-
46
- let pixels = img.as_bytes();
47
- let result = palette_extract::get_palette_with_options(
48
- &pixels,
49
- PixelEncoding::Rgb,
50
- Quality::new(6),
51
- MaxColors::new(max_colors),
52
- PixelFilter::None
53
- );
54
-
55
- return to_char(format!("{:?}", result))
56
- }
57
-
58
- #[no_mangle]
59
- pub extern "C" fn to_kmeans(path: *const c_char, max_colors: usize) -> *mut c_char {
60
- let img = match image::open(&to_string(path)) {
61
- Ok(res) => res,
62
- Err(ex) => return to_char(format!("Colordom::Error {:?}", ex.to_string()))
63
- };
64
-
65
- let pixels = img.as_bytes();
66
- let max_iterations = 20;
67
- let converge = 1.0;
68
- let verbose = false;
69
- let seed: u64 = 0;
70
-
71
- let lab: Vec<Lab> = Srgb::from_raw_slice(&pixels)
72
- .iter()
73
- .map(|x| x.into_format().into_color())
74
- .collect();
75
-
76
- let run_result = kmeans_colors::get_kmeans_hamerly(
77
- max_colors,
78
- max_iterations,
79
- converge,
80
- verbose,
81
- &lab,
82
- seed
83
- );
84
-
85
- let result = &run_result.centroids
86
- .iter()
87
- .map(|x| Srgb::from_color(*x).into_format())
88
- .collect::<Vec<Srgb<u8>>>();
89
-
90
- return to_char(format!("{:?}", result))
91
- }
92
-
93
- #[no_mangle]
94
- pub extern "C" fn free_result(string: *mut c_char) {
95
- unsafe {
96
- if string.is_null() { return }
97
- CString::from_raw(string)
98
- };
99
- }