1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
RSpec.describe Magick::Image, '#import_pixels' do
def import(image, pixels, type, expected = 0.0)
image = Magick::Image.new(image.columns, image.rows)
image.import_pixels(0, 0, image.columns, image.rows, 'RGB', pixels, type)
_, diff = image.compare_channel(image, Magick::MeanAbsoluteErrorMetric)
expect(diff).to be_within(0.1).of(expected)
end
it 'works' do
image = described_class.new(20, 20)
pixels = image.export_pixels(0, 0, image.columns, 1, 'RGB')
result = image.import_pixels(0, 0, image.columns, 1, 'RGB', pixels)
expect(result).to be(image)
expect { image.import_pixels }.to raise_error(ArgumentError)
expect { image.import_pixels(0) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0, image.columns) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0, image.columns, 1) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0, image.columns, 1, 'RGB') }.to raise_error(ArgumentError)
expect { image.import_pixels('x', 0, image.columns, 1, 'RGB', pixels) }.to raise_error(TypeError)
expect { image.import_pixels(0, 'x', image.columns, 1, 'RGB', pixels) }.to raise_error(TypeError)
expect { image.import_pixels(0, 0, 'x', 1, 'RGB', pixels) }.to raise_error(TypeError)
expect { image.import_pixels(0, 0, image.columns, 'x', 'RGB', pixels) }.to raise_error(TypeError)
expect { image.import_pixels(0, 0, image.columns, 1, [2], pixels) }.to raise_error(TypeError)
expect { image.import_pixels(-1, 0, image.columns, 1, 'RGB', pixels) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, -1, image.columns, 1, 'RGB', pixels) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0, -1, 1, 'RGB', pixels) }.to raise_error(ArgumentError)
expect { image.import_pixels(0, 0, image.columns, -1, 'RGB', pixels) }.to raise_error(ArgumentError)
# pixel array is too small
expect { image.import_pixels(0, 0, image.columns, 2, 'RGB', pixels) }.to raise_error(ArgumentError)
# pixel array doesn't contain a multiple of the map length
pixels.shift
expect { image.import_pixels(0, 0, image.columns, 1, 'RGB', pixels) }.to raise_error(ArgumentError)
end
it 'raises an error given UndefinedPixel' do
image = described_class.new(20, 20)
pixels = image.export_pixels(0, 0, 20, 20, 'RGB').pack('D*')
expect do
image.import_pixels(0, 0, 20, 20, 'RGB', pixels, Magick::UndefinedPixel)
end.to raise_error(ArgumentError, /UndefinedPixel/)
end
it 'works with different pixel types' do
image = described_class.read(File.join(IMAGES_DIR, 'Flower_Hat.jpg')).first
is_hdri_support = Magick::Magick_features.include?('HDRI')
pixels = image.export_pixels(0, 0, image.columns, image.rows, 'RGB')
case Magick::MAGICKCORE_QUANTUM_DEPTH
when 8
packed_pixels = pixels.pack('C*')
import(image, packed_pixels, Magick::CharPixel)
packed_pixels = pixels.pack('F*') if is_hdri_support
import(image, packed_pixels, Magick::QuantumPixel)
spixels = pixels.map { |px| px * 257 }
packed_pixels = spixels.pack('S*')
import(image, packed_pixels, Magick::ShortPixel)
ipixels = pixels.map { |px| px * 16_843_009 }
packed_pixels = ipixels.pack('I*')
import(image, packed_pixels, Magick::LongPixel)
when 16
cpixels = pixels.map { |px| px / 257 }
packed_pixels = cpixels.pack('C*')
import(image, packed_pixels, Magick::CharPixel)
packed_pixels = pixels.pack('S*')
import(image, packed_pixels, Magick::ShortPixel)
packed_pixels = pixels.pack('F*') if is_hdri_support
import(image, packed_pixels, Magick::QuantumPixel)
ipixels = pixels.map { |px| px * 65_537 }
ipixels.pack('I*')
# Diff s/b 0.0 but never is.
# import(image, packed_pixels, Magick::LongPixel, 430.7834)
when 32
cpixels = pixels.map { |px| px / 16_843_009 }
packed_pixels = cpixels.pack('C*')
import(image, packed_pixels, Magick::CharPixel)
spixels = pixels.map { |px| px / 65_537 }
packed_pixels = spixels.pack('S*')
import(image, packed_pixels, Magick::ShortPixel)
packed_pixels = pixels.pack('I*')
import(image, packed_pixels, Magick::LongPixel)
packed_pixels = pixels.pack('D*') if is_hdri_support
import(image, packed_pixels, Magick::QuantumPixel)
when 64
cpixels = pixels.map { |px| px / 72_340_172_838_076_673 }
packed_pixels = cpixels.pack('C*')
import(image, packed_pixels, Magick::CharPixel)
spixels = pixels.map { |px| px / 281_479_271_743_489 }
packed_pixels = spixels.pack('S*')
import(image, packed_pixels, Magick::ShortPixel)
ipixels = pixels.map { |px| px / 4_294_967_297 }
packed_pixels = ipixels.pack('I*')
import(image, packed_pixels, Magick::LongPixel)
packed_pixels = pixels.pack('Q*')
import(image, packed_pixels, Magick::QuantumPixel)
end
end
end
|