ndav-magick-image 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/ndav/magick/image.rb +23 -37
- data/ndav-magick-image.gemspec +1 -1
- data/test/helper.rb +5 -4
- data/test/test_converter.rb +3 -6
- data/test/test_from_ndav.rb +20 -28
- data/test/test_memory_viewable.rb +0 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 96acb84dd6f8a244f0951db50db36caf76cdf29d87804a45cac2fb356b699e28
|
|
4
|
+
data.tar.gz: 5a5c8fce397292d41afa18d60cbdbd6e8948c40a014143ce3a003b88aea66444
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d2dcc5754ad77b221630fde7deb4a6f8b36067bc938e3b0d3a2f3d5623c675d44cfba2c41527cade9e7eb4e5a2acda4dbf7cf53dbfb8c5ee9a62aec5df4825a9
|
|
7
|
+
data.tar.gz: 2b76f5e90698374a5788c212837a6cb466977479e18514606b10061be1da6e43047e712530824a6ca67f51e1e9060ba3a7e170f3e78a404d4a0238ea503cd451
|
data/README.md
CHANGED
|
@@ -60,9 +60,9 @@ The shape is `[height, width, channels]`. Channels vary based on the image attri
|
|
|
60
60
|
|
|
61
61
|
### format and item size ###
|
|
62
62
|
|
|
63
|
-
Currently, this bridge exports `Magick::Image`'s
|
|
63
|
+
Currently, this bridge exports `Magick::Image`'s MemoryView and imports from `NDAV` using `Magick::QuantumPixel`. Therefore, the MemoryView's format and item_size depend on the build options of ImageMagick. You can check the format with `NDAV::Magick::Image::MemoryViewable::FORMAT`.
|
|
64
64
|
|
|
65
|
-
|
|
65
|
+
It's under consideration whether other formats should be supported or not.
|
|
66
66
|
|
|
67
67
|
EXAMPLE
|
|
68
68
|
-------
|
data/lib/ndav/magick/image.rb
CHANGED
|
@@ -5,6 +5,15 @@ class NDAV
|
|
|
5
5
|
module Magick
|
|
6
6
|
module Image
|
|
7
7
|
IS_HDRI_SUPPORTED = ::Magick::Magick_features.match?(/\bHDRI\b/)
|
|
8
|
+
is_little_endian = IO::Buffer::HOST_ENDIAN == IO::Buffer::LITTLE_ENDIAN
|
|
9
|
+
SUPPORTED_FORMATS =
|
|
10
|
+
case [::Magick::MAGICKCORE_QUANTUM_DEPTH, IS_HDRI_SUPPORTED]
|
|
11
|
+
in [ 8, false] then ["C"]
|
|
12
|
+
in [16, false] then ["S", is_little_endian ? "v" : "n"]
|
|
13
|
+
in [32, false] then ["L", is_little_endian ? "V" : "N"]
|
|
14
|
+
in [ 8, true ] | [16, true] | [32, true] then ["f", is_little_endian ? "e" : "g"]
|
|
15
|
+
in [64, true ] then ["d", is_little_endian ? "E" : "G"]
|
|
16
|
+
end
|
|
8
17
|
COLORSPACE_TO_MAP = {
|
|
9
18
|
::Magick::GRAYColorspace => "I",
|
|
10
19
|
::Magick::LinearGRAYColorspace => "I",
|
|
@@ -21,55 +30,32 @@ class NDAV
|
|
|
21
30
|
3 => "RGB",
|
|
22
31
|
4 => "RGBA"
|
|
23
32
|
}
|
|
24
|
-
FORMAT_TO_TYPE = Hash.new {|types, format| types[format] = type_from_format(format)}
|
|
25
|
-
|
|
26
|
-
class << self
|
|
27
|
-
# TODO: Support more formats like "CCC"
|
|
28
|
-
def type_from_format(format)
|
|
29
|
-
item_desc = NDAV::ITEM_DESCS[format]
|
|
30
|
-
return unless item_desc.length == 1
|
|
31
|
-
|
|
32
|
-
component = item_desc[0]
|
|
33
|
-
return unless component.repeat == 1
|
|
34
|
-
|
|
35
|
-
case component.format
|
|
36
|
-
when MemoryViewable::FORMAT then ::Magick::QuantumPixel
|
|
37
|
-
when "C" then ::Magick::CharPixel
|
|
38
|
-
when "S" then ::Magick::ShortPixel
|
|
39
|
-
when "L" then ::Magick::IntegerPixel
|
|
40
|
-
when "f" then ::Magick::FloatPixel
|
|
41
|
-
when "d" then ::Magick::DoublePixel
|
|
42
|
-
else raise "Unsupported format: #{format}"
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
33
|
|
|
47
34
|
module FromNDAV
|
|
48
35
|
# TODO: Support more formats
|
|
36
|
+
# TODO: Support CMYK
|
|
49
37
|
def from_ndav(ndav)
|
|
50
|
-
raise "
|
|
51
|
-
raise "
|
|
38
|
+
Kernel.raise "ndim must be 2 or 3 but is #{ndav.ndim}" unless [2, 3].include? ndav.ndim
|
|
39
|
+
Kernel.raise "Currently, supports only row-major contiguous NDAV" unless ndav.row_major_contiguous?
|
|
40
|
+
|
|
41
|
+
item_desc = NDAV::ITEM_DESCS[ndav.format]
|
|
42
|
+
Kernel.raise "Unsupported format: #{ndav.format}. Must be single character" unless item_desc.length == 1
|
|
43
|
+
component = item_desc[0]
|
|
44
|
+
Kernel.raise "Unsupported format: #{ndav.format}. Must not be repeated" unless component.repeat == 1
|
|
45
|
+
Kernel.raise "Unsupported format: #{ndav.format}. Supported chars: #{SUPPORTED_FORMATS.join(', ')}" unless SUPPORTED_FORMATS.include? component.format
|
|
52
46
|
|
|
53
47
|
height, width, channels = ndav.shape
|
|
54
48
|
map = CHANNELS_TO_MAP[channels]
|
|
55
|
-
raise "Unsupported channels: #{channels}" unless map
|
|
49
|
+
Kernel.raise "Unsupported channels: #{channels}" unless map
|
|
56
50
|
|
|
57
51
|
image = ::Magick::Image.new(width, height)
|
|
58
|
-
image.import_pixels 0, 0, width, height, map, ndav.to_s,
|
|
52
|
+
image.import_pixels 0, 0, width, height, map, ndav.to_s, ::Magick::QuantumPixel
|
|
59
53
|
image
|
|
60
54
|
end
|
|
61
55
|
end
|
|
62
|
-
|
|
56
|
+
|
|
63
57
|
module MemoryViewable
|
|
64
|
-
FORMAT =
|
|
65
|
-
in [ 8, true ] then "f"
|
|
66
|
-
in [ 8, false] then "C"
|
|
67
|
-
in [16, true ] then "f"
|
|
68
|
-
in [16, false] then "S"
|
|
69
|
-
in [32, true ] then "f"
|
|
70
|
-
in [32, false] then "L"
|
|
71
|
-
in [64, true ] then "d"
|
|
72
|
-
end
|
|
58
|
+
FORMAT = SUPPORTED_FORMATS.first
|
|
73
59
|
ITEM_SIZE = NDAV::ITEM_SIZES[FORMAT]
|
|
74
60
|
|
|
75
61
|
private
|
|
@@ -90,7 +76,7 @@ class NDAV
|
|
|
90
76
|
}
|
|
91
77
|
end
|
|
92
78
|
end
|
|
93
|
-
|
|
79
|
+
|
|
94
80
|
module Converter
|
|
95
81
|
def MagickImage(array, *, **)
|
|
96
82
|
if array.kind_of? ::Magick::Image
|
data/ndav-magick-image.gemspec
CHANGED
data/test/helper.rb
CHANGED
|
@@ -5,14 +5,15 @@ require "fiddle"
|
|
|
5
5
|
require "tmpdir"
|
|
6
6
|
|
|
7
7
|
module Helper
|
|
8
|
+
FORMAT = NDAV::Magick::Image::MemoryViewable::FORMAT
|
|
9
|
+
|
|
8
10
|
private
|
|
9
11
|
|
|
10
|
-
def make_image(width, height)
|
|
12
|
+
def make_image(width, height, former = [255, 255, 255], latter = [0, 255, 0])
|
|
11
13
|
ratio = (2 ** ::Magick::MAGICKCORE_QUANTUM_DEPTH - 1) / 255
|
|
12
14
|
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
15
|
-
# ].flatten.collect {|item| round_trip(item * ratio, NDAV::Magick::Image::MemoryViewable::FORMAT)}
|
|
15
|
+
[former * width] * (height / 2),
|
|
16
|
+
[latter * width] * (height / 2)
|
|
16
17
|
].flatten.collect {|item| item * ratio}
|
|
17
18
|
end
|
|
18
19
|
end
|
data/test/test_converter.rb
CHANGED
|
@@ -18,12 +18,9 @@ class TestConverter < Test::Unit::TestCase
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def test_magick_image_from_ndav
|
|
21
|
-
array =
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
]
|
|
25
|
-
ptr = Fiddle::Pointer[array.flatten.pack("C*")]
|
|
26
|
-
ndav = ::NDAV.new(ptr, shape: [@height, @width, 3])
|
|
21
|
+
array = make_image(@width, @height)
|
|
22
|
+
ptr = Fiddle::Pointer[array.flatten.pack("#{FORMAT}*")]
|
|
23
|
+
ndav = ::NDAV.new(ptr, format: FORMAT, shape: [@height, @width, 3])
|
|
27
24
|
|
|
28
25
|
assert_instance_of ::Magick::Image, ::NDAV::Converter::MagickImage(ndav)
|
|
29
26
|
end
|
data/test/test_from_ndav.rb
CHANGED
|
@@ -7,66 +7,58 @@ class TestFromNDAV < Test::Unit::TestCase
|
|
|
7
7
|
end
|
|
8
8
|
|
|
9
9
|
def test_basic
|
|
10
|
-
array =
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
]
|
|
14
|
-
ptr = Fiddle::Pointer[array.flatten.pack("C*")]
|
|
15
|
-
ndav = ::NDAV.new(ptr, shape: [@height, @width, 3])
|
|
10
|
+
array = make_image(@width, @height)
|
|
11
|
+
ptr = Fiddle::Pointer[array.flatten.pack("#{FORMAT}*")]
|
|
12
|
+
ndav = ::NDAV.new(ptr, format: FORMAT, shape: [@height, @width, 3])
|
|
16
13
|
image = ::Magick::Image.from_ndav(ndav)
|
|
17
14
|
|
|
18
15
|
assert_equal @width, image.columns
|
|
19
16
|
assert_equal @height, image.rows
|
|
20
17
|
assert_false image.gray?
|
|
21
18
|
assert_false image.alpha?
|
|
22
|
-
assert_equal array.flatten,
|
|
19
|
+
assert_equal array.flatten,
|
|
20
|
+
image.export_pixels_to_str(0, 0, @width, @height, "RGB", ::Magick::QuantumPixel).unpack("#{FORMAT}*")
|
|
23
21
|
end
|
|
24
22
|
|
|
25
23
|
def test_alpha
|
|
26
|
-
array = [
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
]
|
|
30
|
-
ptr = Fiddle::Pointer[array.flatten.pack("C*")]
|
|
31
|
-
ndav = ::NDAV.new(ptr, shape: [@height, @width, 4])
|
|
24
|
+
array = make_image(@width, @height, [255, 255, 255, 128], [0, 255, 0, 128])
|
|
25
|
+
ptr = Fiddle::Pointer[array.flatten.pack("#{FORMAT}*")]
|
|
26
|
+
ndav = ::NDAV.new(ptr, format: FORMAT, shape: [@height, @width, 4])
|
|
32
27
|
image = ::Magick::Image.from_ndav(ndav)
|
|
33
28
|
|
|
34
29
|
assert_equal @width, image.columns
|
|
35
30
|
assert_equal @height, image.rows
|
|
36
31
|
assert_false image.gray?
|
|
37
32
|
assert_true image.alpha?
|
|
38
|
-
assert_equal array.flatten,
|
|
33
|
+
assert_equal array.flatten,
|
|
34
|
+
image.export_pixels_to_str(0, 0, @width, @height, "RGBA", ::Magick::QuantumPixel).unpack("#{FORMAT}*")
|
|
39
35
|
end
|
|
40
36
|
|
|
41
37
|
def test_grayscale
|
|
42
|
-
array = [
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
]
|
|
46
|
-
ptr = Fiddle::Pointer[array.flatten.pack("C*")]
|
|
47
|
-
ndav = ::NDAV.new(ptr, shape: [@height, @width, 1])
|
|
38
|
+
array = make_image(@width, @height, [255], [128])
|
|
39
|
+
ptr = Fiddle::Pointer[array.flatten.pack("#{FORMAT}*")]
|
|
40
|
+
ndav = ::NDAV.new(ptr, format: FORMAT, shape: [@height, @width, 1])
|
|
48
41
|
image = ::Magick::Image.from_ndav(ndav)
|
|
49
42
|
|
|
50
43
|
assert_equal @width, image.columns
|
|
51
44
|
assert_equal @height, image.rows
|
|
52
45
|
assert_true image.gray?
|
|
53
46
|
assert_false image.alpha?
|
|
54
|
-
assert_equal array.flatten,
|
|
47
|
+
assert_equal array.flatten,
|
|
48
|
+
image.export_pixels_to_str(0, 0, @width, @height, "R", ::Magick::QuantumPixel).unpack("#{FORMAT}*")
|
|
55
49
|
end
|
|
56
50
|
|
|
57
51
|
def test_gray_alpha
|
|
58
|
-
array = [
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
]
|
|
62
|
-
ptr = Fiddle::Pointer[array.flatten.pack("C*")]
|
|
63
|
-
ndav = ::NDAV.new(ptr, shape: [@height, @width, 2])
|
|
52
|
+
array = make_image(@width, @height, [255, 128], [128, 128])
|
|
53
|
+
ptr = Fiddle::Pointer[array.flatten.pack("#{FORMAT}*")]
|
|
54
|
+
ndav = ::NDAV.new(ptr, format: FORMAT, shape: [@height, @width, 2])
|
|
64
55
|
image = ::Magick::Image.from_ndav(ndav)
|
|
65
56
|
|
|
66
57
|
assert_equal @width, image.columns
|
|
67
58
|
assert_equal @height, image.rows
|
|
68
59
|
assert_true image.gray?
|
|
69
60
|
assert_true image.alpha?
|
|
70
|
-
assert_equal array.flatten,
|
|
61
|
+
assert_equal array.flatten,
|
|
62
|
+
image.export_pixels_to_str(0, 0, @width, @height, "RA", ::Magick::QuantumPixel).unpack("#{FORMAT}*")
|
|
71
63
|
end
|
|
72
64
|
end
|