free-image 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/HISTORY +30 -19
- data/LICENSE +20 -20
- data/README.rdoc +120 -120
- data/Rakefile +51 -47
- data/cookbook.rdoc +248 -237
- data/free-image.gemspec +30 -29
- data/lib/free-image.rb +1 -2
- data/lib/free-image/bitmap.rb +2 -2
- data/lib/free-image/enums/filters.rb +11 -11
- data/lib/free-image/errors.rb +43 -43
- data/lib/free-image/modules/conversions.rb +253 -253
- data/lib/free-image/modules/helper.rb +41 -41
- data/lib/free-image/modules/information.rb +20 -2
- data/lib/free-image/modules/modify.rb +299 -299
- data/lib/free-image/modules/pixels.rb +134 -134
- data/lib/free-image/modules/transforms.rb +90 -90
- data/lib/free-image/sources/abstract_source.rb +178 -178
- data/lib/free-image/sources/file.rb +114 -114
- data/lib/free-image/sources/io.rb +153 -153
- data/lib/free-image/sources/memory.rb +188 -188
- data/lib/free-image/types/boolean.rb +13 -13
- data/lib/free-image/types/ffi.rb +13 -13
- data/lib/free-image/types/info_header.rb +36 -0
- data/lib/free-image/types/rgb16.rb +31 -0
- data/test/cookbook.rb +45 -46
- data/test/images/sample_composite.png +0 -0
- data/test/images/test16.bmp +0 -0
- data/test/images/test16bf555.bmp +0 -0
- data/test/images/test16bf565.bmp +0 -0
- data/test/test_bitmap.rb +61 -63
- data/test/test_conversions.rb +85 -86
- data/test/test_file.rb +68 -69
- data/test/test_free_image.rb +14 -15
- data/test/test_helper.rb +14 -0
- data/test/test_information.rb +145 -117
- data/test/test_io.rb +73 -74
- data/test/test_memory.rb +83 -84
- data/test/test_modify.rb +75 -58
- data/test/test_palette.rb +44 -45
- data/test/test_pixels.rb +61 -62
- data/test/test_rgb_quad.rb +24 -25
- data/test/test_scanline.rb +64 -65
- data/test/test_suite.rb +12 -17
- data/test/test_transforms.rb +28 -29
- metadata +58 -31
@@ -7,5 +7,36 @@ module FreeImage
|
|
7
7
|
layout :red, :word,
|
8
8
|
:green, :word,
|
9
9
|
:blue, :word
|
10
|
+
# The high order bit in 16-bit 555 is empty
|
11
|
+
# Define masks to extract colors from bytes
|
12
|
+
if FFI::Platform::BYTE_ORDER == FFI::Platform::LITTLE_ENDIAN
|
13
|
+
# Little Endian (x86 / MS Windows, Linux, MacOSX) : BGR(A) order
|
14
|
+
RED_MASK = 0x7C00
|
15
|
+
GREEN_MASK = 0x03E0
|
16
|
+
BLUE_MASK = 0x001F
|
17
|
+
else
|
18
|
+
# Big Endian (PPC / Linux, MaxOSX) : RGB(A) order
|
19
|
+
RED_MASK = 0x003E
|
20
|
+
GREEN_MASK = 0x07C0
|
21
|
+
BLUE_MASK = 0xF800
|
22
|
+
end
|
23
|
+
RGB_MASK = (RED_MASK | GREEN_MASK | BLUE_MASK)
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
class RGB16BF565 < RGB16
|
28
|
+
# The 565 bitfield uses 16 bits, with 6 bits for Green
|
29
|
+
if FFI::Platform::BYTE_ORDER == FFI::Platform::LITTLE_ENDIAN
|
30
|
+
# Little Endian (x86 / MS Windows, Linux, MacOSX) : BGR(A) order
|
31
|
+
RED_MASK = 0xF800
|
32
|
+
GREEN_MASK = 0x07E0
|
33
|
+
BLUE_MASK = 0x001F
|
34
|
+
else
|
35
|
+
# Big Endian (PPC / Linux, MaxOSX) : RGB(A) order
|
36
|
+
RED_MASK = 0x001F
|
37
|
+
GREEN_MASK = 0x03E0
|
38
|
+
BLUE_MASK = 0xFC00
|
39
|
+
end
|
40
|
+
RGB_MASK = (RED_MASK | GREEN_MASK | BLUE_MASK)
|
10
41
|
end
|
11
42
|
end
|
data/test/cookbook.rb
CHANGED
@@ -1,46 +1,45 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
color[:
|
8
|
-
color[:
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
scanline
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
scanline
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
thumbnail.save("images/lena_thumbnail_border_scanline.png", :png)
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.join(File.dirname(__FILE__),'test_helper')
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
def set_to_red(color)
|
6
|
+
color[:red] = 255
|
7
|
+
color[:green] = 0
|
8
|
+
color[:blue] = 0
|
9
|
+
end
|
10
|
+
|
11
|
+
image = FreeImage::Bitmap.open('images/lena.png')
|
12
|
+
thumbnail = image.make_thumbnail(100)
|
13
|
+
|
14
|
+
# Make the bottom row red
|
15
|
+
scanline = thumbnail.scanline(0)
|
16
|
+
|
17
|
+
# Draw bottom border
|
18
|
+
(0..3).each do |index|
|
19
|
+
scanline = thumbnail.scanline(index)
|
20
|
+
scanline.each do |color|
|
21
|
+
set_to_red(color)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Draw top border
|
26
|
+
((thumbnail.height - 5)..(thumbnail.height - 1)).each do |index|
|
27
|
+
scanline = thumbnail.scanline(index)
|
28
|
+
scanline.each do |color|
|
29
|
+
set_to_red(color)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Draw left and right borders
|
34
|
+
(1..(thumbnail.height - 2)).each do |index|
|
35
|
+
scanline = thumbnail.scanline(index)
|
36
|
+
(0..4).each do |index|
|
37
|
+
set_to_red(scanline[index])
|
38
|
+
end
|
39
|
+
|
40
|
+
((thumbnail.width - 5)..(thumbnail.width - 1)).each do |index|
|
41
|
+
set_to_red(scanline[index])
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
thumbnail.save("images/lena_thumbnail_border_scanline.png", :png)
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
data/test/test_bitmap.rb
CHANGED
@@ -1,63 +1,61 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
bytes
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
assert_equal(
|
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
|
-
image
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
end
|
63
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.join(File.dirname(__FILE__),'test_helper')
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class BitmapTest < Test::Unit::TestCase
|
6
|
+
def test_bits
|
7
|
+
bytes = sample_image.bits
|
8
|
+
assert_equal(6466, bytes.size)
|
9
|
+
|
10
|
+
if defined?(Encoding)
|
11
|
+
assert_equal(Encoding::BINARY, bytes.encoding)
|
12
|
+
assert_equal(bytes.size, bytes.bytesize)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_open
|
17
|
+
bitmap = FreeImage::Bitmap.open(image_path('lena.png'))
|
18
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_open_yield
|
22
|
+
FreeImage::Bitmap.open(image_path('lena.png')) do |bitmap|
|
23
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_open_yield_error
|
28
|
+
assert_raise(ArgumentError) do
|
29
|
+
FreeImage::Bitmap.open(image_path('lena.png')) do |bitmap|
|
30
|
+
raise(ArgumentError, "Let's mess things up")
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_new_from_nil
|
36
|
+
ptr = FFI::Pointer::NULL
|
37
|
+
error = assert_raise(FreeImage::Error) do
|
38
|
+
FreeImage::Bitmap.new(ptr)
|
39
|
+
end
|
40
|
+
assert_equal("Cannot create a bitmap from a null pointer", error.message)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_clone
|
44
|
+
image = lena_image
|
45
|
+
clone = image.clone
|
46
|
+
assert(!clone.equal?(image))
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_clone_block
|
50
|
+
lena_image.clone do |image|
|
51
|
+
assert_not_nil(image)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_free
|
56
|
+
1000.times do
|
57
|
+
image = sample_image
|
58
|
+
image.free
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/test/test_conversions.rb
CHANGED
@@ -1,86 +1,85 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
bitmap
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
bitmap
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
bitmap
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
bitmap
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
bitmap
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
bitmap
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
bitmap
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
bitmap
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
bitmap
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
bitmap
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
bitmap
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.join(File.dirname(__FILE__),'test_helper')
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class ConverstionsTest < Test::Unit::TestCase
|
6
|
+
def test_convert_to_4bits
|
7
|
+
bitmap = sample_image.convert_to_4bits
|
8
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
9
|
+
assert_equal(4, bitmap.bits_per_pixel)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_convert_to_8bits
|
13
|
+
bitmap = sample_image.convert_to_8bits
|
14
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
15
|
+
assert_equal(8, bitmap.bits_per_pixel)
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_convert_to_greyscale
|
19
|
+
bitmap = sample_image.convert_to_greyscale
|
20
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
21
|
+
assert_equal(8, bitmap.bits_per_pixel)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_convert_to_16bits_555
|
25
|
+
bitmap = sample_image.convert_to_16bits_555
|
26
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
27
|
+
assert_equal(16, bitmap.bits_per_pixel)
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_convert_to_16bits_565
|
31
|
+
bitmap = sample_image.convert_to_16bits_565
|
32
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
33
|
+
assert_equal(16, bitmap.bits_per_pixel)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_convert_to_24bits
|
37
|
+
bitmap = sample_image.convert_to_24bits
|
38
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
39
|
+
assert_equal(24, bitmap.bits_per_pixel)
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_convert_to_32bits
|
43
|
+
bitmap = sample_image.convert_to_32bits
|
44
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
45
|
+
assert_equal(32, bitmap.bits_per_pixel)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_convert_to_standard_type
|
49
|
+
bitmap = sample_image.convert_to_standard_type
|
50
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
51
|
+
assert_equal(8, bitmap.bits_per_pixel)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_convert_to_type
|
55
|
+
bitmap = sample_image.convert_to_type(:rgb16)
|
56
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
57
|
+
assert_equal(48, bitmap.bits_per_pixel)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_dither
|
61
|
+
bitmap = sample_image.dither(:bayer4x4)
|
62
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
63
|
+
assert_equal(1, bitmap.bits_per_pixel)
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_threshold
|
67
|
+
bitmap = sample_image.threshold(7)
|
68
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
69
|
+
assert_equal(1, bitmap.bits_per_pixel)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_threshold_low
|
73
|
+
error = assert_raise(RangeError) do
|
74
|
+
sample_image.threshold(-1)
|
75
|
+
end
|
76
|
+
assert_equal("Value is out of range 0..255. Value: -1", error.message)
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_threshold_hight
|
80
|
+
error = assert_raise(RangeError) do
|
81
|
+
sample_image.threshold(5555)
|
82
|
+
end
|
83
|
+
assert_equal("Value is out of range 0..255. Value: 5555", error.message)
|
84
|
+
end
|
85
|
+
end
|
data/test/test_file.rb
CHANGED
@@ -1,70 +1,69 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require '
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
path
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
bitmap
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
result
|
45
|
-
assert(
|
46
|
-
|
47
|
-
|
48
|
-
tmp_file.
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
require File.join(File.dirname(__FILE__),'test_helper')
|
3
|
+
require 'test/unit'
|
4
|
+
|
5
|
+
class FIFileTest < Test::Unit::TestCase
|
6
|
+
def file(image = 'sample.png')
|
7
|
+
path = image_path(image)
|
8
|
+
FreeImage::File.new(path)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_format
|
12
|
+
assert_equal(:png, file.format)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_format_unknown
|
16
|
+
assert_equal(:unknown, file('not_an_image.txt').format)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_load
|
20
|
+
bitmap = file.open
|
21
|
+
assert_kind_of(FreeImage::Bitmap, bitmap)
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_load_format
|
25
|
+
error = assert_raise(FreeImage::Error) do
|
26
|
+
file('not_an_image.txt').open
|
27
|
+
end
|
28
|
+
assert_equal('Cannot load :unknown image format', error.to_s)
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_load_wrong_format
|
32
|
+
error = assert_raise(FreeImage::Error) do
|
33
|
+
file.open(:jpeg)
|
34
|
+
end
|
35
|
+
assert_equal("Not a JPEG file: starts with 0x89 0x50", error.to_s)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_save
|
39
|
+
tmp_file = Tempfile.new('test_free_image')
|
40
|
+
dst = FreeImage::File.new(tmp_file.path)
|
41
|
+
|
42
|
+
bitmap = file.open
|
43
|
+
result = bitmap.save(dst, :png)
|
44
|
+
assert(result)
|
45
|
+
assert(File.exists?(tmp_file))
|
46
|
+
ensure
|
47
|
+
tmp_file.close
|
48
|
+
tmp_file.unlink
|
49
|
+
end
|
50
|
+
|
51
|
+
def test_corrupt
|
52
|
+
path = image_path('corrupt.jpg')
|
53
|
+
file = FreeImage::File.new(path)
|
54
|
+
error = assert_raise(FreeImage::Error) do
|
55
|
+
file.open
|
56
|
+
end
|
57
|
+
assert_equal("Not a JPEG file: starts with 0xaa 0xc0", error.message)
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_corrupt_wrong_format
|
61
|
+
path = image_path('corrupt.jpg')
|
62
|
+
file = FreeImage::File.new(path)
|
63
|
+
error = assert_raise(FreeImage::Error) do
|
64
|
+
# Be sneaky - say png!
|
65
|
+
file.open(:png)
|
66
|
+
end
|
67
|
+
assert_equal("Could not load the image", error.message)
|
68
|
+
end
|
70
69
|
end
|