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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '0996608a78022414f8bd891b56e9505ae3f0f1784efe2b32e55b158ddfe53fce'
4
- data.tar.gz: a6e1042355ee3cc29adfe82d1b50d805eefc151d0961e8304fc61f0284eb0fcc
3
+ metadata.gz: 96acb84dd6f8a244f0951db50db36caf76cdf29d87804a45cac2fb356b699e28
4
+ data.tar.gz: 5a5c8fce397292d41afa18d60cbdbd6e8948c40a014143ce3a003b88aea66444
5
5
  SHA512:
6
- metadata.gz: 07c973f3747bd2358672c1729700f1a09f0d0d95cf33ee292e133962cf1f54e2158a5189001f2848246678c6e3d49136e05aeb1975f7947546cbbf0f61210c23
7
- data.tar.gz: b8062885f5f5e2e53ca33cbab91ca36ca1aff098001b66444f3da463e611e20ea951600e7b863eed7b09942604d274bfc97184db8a0a199ed9f06226c1792c35
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 MemoryCiew 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`.
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
- On the other hand, this bridge can import MemoryViews whose format differs from `NDAV::Magick::Image::MemoryViewable::FORMAT`. Formats that are not currently supported might be supported in the future.
65
+ It's under consideration whether other formats should be supported or not.
66
66
 
67
67
  EXAMPLE
68
68
  -------
@@ -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 "Currently, supports only row-major contiguous NDAV" unless ndav.row_major_contiguous?
51
- raise "ndim must be 2 or 3 but is #{ndav.ndim}" unless [2, 3].include? ndav.ndim
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, FORMAT_TO_TYPE[ndav.format]
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 = case [::Magick::MAGICKCORE_QUANTUM_DEPTH, NDAV::Magick::Image::IS_HDRI_SUPPORTED]
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
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "ndav-magick-image"
3
- s.version = "0.0.2"
3
+ s.version = "0.0.3"
4
4
  s.authors = ["Kitaiti Makoto"]
5
5
  s.summary = "N-Dimensional Array View for RMagick"
6
6
  s.licenses = ["MIT"]
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
- [[255, 255, 255] * width] * (height / 2),
14
- [[ 0, 255, 0] * width] * (height / 2)
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
@@ -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
- [[255, 255, 255] * @width] * (@height / 2),
23
- [[ 0, 255, 0] * @width] * (@height / 2)
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
@@ -7,66 +7,58 @@ class TestFromNDAV < Test::Unit::TestCase
7
7
  end
8
8
 
9
9
  def test_basic
10
- array = [
11
- [[255, 255, 255] * @width] * (@height / 2),
12
- [[ 0, 255, 0] * @width] * (@height / 2)
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, image.export_pixels_to_str.unpack("C*")
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
- [[255, 255, 255, 128] * @width] * (@height / 2),
28
- [[ 0, 255, 0, 128] * @width] * (@height / 2)
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, image.export_pixels_to_str(0, 0, @width, @height, "RGBA").unpack("C*")
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
- [[255] * @width] * (@height / 2),
44
- [[128] * @width] * (@height / 2)
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, image.export_pixels_to_str(0, 0, @width, @height, "R").unpack("C*")
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
- [[255, 128] * @width] * (@height / 2),
60
- [[128, 128] * @width] * (@height / 2)
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, image.export_pixels_to_str(0, 0, @width, @height, "RA").unpack("C*")
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
@@ -1,7 +1,6 @@
1
1
  require_relative "helper"
2
2
 
3
3
  class TestMemoryViewable < Test::Unit::TestCase
4
- FORMAT = NDAV::Magick::Image::MemoryViewable::FORMAT
5
4
  ITEM_SIZE = NDAV::ITEM_SIZES[FORMAT]
6
5
 
7
6
  def setup
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ndav-magick-image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kitaiti Makoto