free-image 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. data/HISTORY +5 -0
  2. data/LICENSE +21 -0
  3. data/README.rdoc +99 -0
  4. data/Rakefile +47 -0
  5. data/cookbook.rdoc +239 -0
  6. data/free-image.gemspec +30 -0
  7. data/lib/free-image.rb +101 -0
  8. data/lib/free-image/bitmap.rb +154 -0
  9. data/lib/free-image/enums/color_types.rb +24 -0
  10. data/lib/free-image/enums/dithers.rb +24 -0
  11. data/lib/free-image/enums/filters.rb +12 -0
  12. data/lib/free-image/enums/formats.rb +84 -0
  13. data/lib/free-image/enums/image_types.rb +36 -0
  14. data/lib/free-image/errors.rb +44 -0
  15. data/lib/free-image/modules/conversions.rb +166 -0
  16. data/lib/free-image/modules/helper.rb +42 -0
  17. data/lib/free-image/modules/icc.rb +41 -0
  18. data/lib/free-image/modules/information.rb +305 -0
  19. data/lib/free-image/modules/modify.rb +261 -0
  20. data/lib/free-image/modules/pixels.rb +135 -0
  21. data/lib/free-image/modules/transforms.rb +83 -0
  22. data/lib/free-image/palette.rb +44 -0
  23. data/lib/free-image/scanline.rb +151 -0
  24. data/lib/free-image/sources/abstract_source.rb +172 -0
  25. data/lib/free-image/sources/file.rb +115 -0
  26. data/lib/free-image/sources/io.rb +154 -0
  27. data/lib/free-image/sources/memory.rb +189 -0
  28. data/lib/free-image/types/boolean.rb +14 -0
  29. data/lib/free-image/types/complex.rb +9 -0
  30. data/lib/free-image/types/ffi.rb +14 -0
  31. data/lib/free-image/types/rgb16.rb +11 -0
  32. data/lib/free-image/types/rgb_quad.rb +82 -0
  33. data/lib/free-image/types/rgb_triple.rb +42 -0
  34. data/lib/free-image/types/rgba16.rb +12 -0
  35. data/lib/free-image/types/rgbaf.rb +12 -0
  36. data/lib/free-image/types/rgbf.rb +11 -0
  37. data/test/cookbook.rb +46 -0
  38. data/test/images/gradient.png +0 -0
  39. data/test/images/lena.png +0 -0
  40. data/test/images/lena.tiff +0 -0
  41. data/test/images/lena_flipped.png +0 -0
  42. data/test/images/lena_rescale_bicubic.png +0 -0
  43. data/test/images/lena_rescale_bilinear.png +0 -0
  44. data/test/images/lena_rescale_box.png +0 -0
  45. data/test/images/lena_rescale_bspline.png +0 -0
  46. data/test/images/lena_rescale_catmullrom.png +0 -0
  47. data/test/images/lena_rescale_lanczos3.png +0 -0
  48. data/test/images/lena_rotate_45.png +0 -0
  49. data/test/images/lena_rotate_ex_45_masked.png +0 -0
  50. data/test/images/lena_rotate_ex_45_mirrored.png +0 -0
  51. data/test/images/lena_rotate_ex_45_top_left.png +0 -0
  52. data/test/images/lena_thumbnail.png +0 -0
  53. data/test/images/lena_thumbnail_border_enlarge.png +0 -0
  54. data/test/images/lena_thumbnail_border_paste.png +0 -0
  55. data/test/images/lena_thumbnail_border_scanline.png +0 -0
  56. data/test/images/not_an_image.txt +0 -0
  57. data/test/images/sample.png +0 -0
  58. data/test/images/sample_composite_color.png +0 -0
  59. data/test/test_bitmap.rb +43 -0
  60. data/test/test_conversions.rb +86 -0
  61. data/test/test_file.rb +51 -0
  62. data/test/test_free_image.rb +15 -0
  63. data/test/test_helper.rb +35 -0
  64. data/test/test_information.rb +118 -0
  65. data/test/test_io.rb +53 -0
  66. data/test/test_memory.rb +65 -0
  67. data/test/test_modify.rb +59 -0
  68. data/test/test_palette.rb +45 -0
  69. data/test/test_pixels.rb +62 -0
  70. data/test/test_rgb_quad.rb +26 -0
  71. data/test/test_scanline.rb +65 -0
  72. data/test/test_suite.rb +19 -0
  73. data/test/test_transforms.rb +30 -0
  74. metadata +169 -0
@@ -0,0 +1,15 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class FreeImageTest < Test::Unit::TestCase
7
+ def test_copyright
8
+ assert_equal("This program uses FreeImage, a free, open source image library supporting all common bitmap formats. See http://freeimage.sourceforge.net for details",
9
+ FreeImage.copyright)
10
+ end
11
+
12
+ def test_version
13
+ assert_equal("3.15.1", FreeImage.version)
14
+ end
15
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: UTF-8
2
+
3
+ # To make testing/debugging easier, test within this source tree versus an installed gem
4
+ dir = File.dirname(__FILE__)
5
+ ROOT_DIR = File.expand_path(File.join(dir, '..'))
6
+ LIB_DIR = File.expand_path(File.join(ROOT_DIR, 'lib'))
7
+
8
+ $LOAD_PATH << LIB_DIR
9
+
10
+ require 'rubygems'
11
+ require 'free-image'
12
+ require 'tempfile'
13
+
14
+ def image_path(image)
15
+ File.join(ROOT_DIR, 'test', 'images', image)
16
+ end
17
+
18
+ def image_data(image)
19
+ path = File.join(ROOT_DIR, 'test', 'images', image)
20
+ result = nil
21
+ File.open(path, 'rb') do |file|
22
+ result = file.read
23
+ end
24
+ result
25
+ end
26
+
27
+ def sample_image
28
+ path = File.join(ROOT_DIR, 'test', 'images', 'sample.png')
29
+ FreeImage::Bitmap.open(path)
30
+ end
31
+
32
+ def lena_image
33
+ path = File.join(ROOT_DIR, 'test', 'images', 'lena.png')
34
+ FreeImage::Bitmap.open(path)
35
+ end
@@ -0,0 +1,118 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class InformationTest < Test::Unit::TestCase
7
+ def test_background_color
8
+ background_color = sample_image.background_color
9
+ assert_kind_of(FreeImage::RGBQuad, background_color)
10
+ assert_equal(112, background_color[:red])
11
+ assert_equal(176, background_color[:green])
12
+ assert_equal(240, background_color[:blue])
13
+ end
14
+
15
+ def test_background_color_set
16
+ image = sample_image
17
+ rgb = FreeImage::RGBQuad.create(7, 8, 9)
18
+
19
+ image.background_color = rgb
20
+ background_color = image.background_color
21
+ assert_equal(rgb, background_color)
22
+ end
23
+
24
+ def test_bits_per_pixel
25
+ assert_equal(8, sample_image.bits_per_pixel)
26
+ end
27
+
28
+ def test_blue_mask
29
+ assert_equal(0, sample_image.blue_mask)
30
+ assert_equal(FreeImage::RGBQuad::BLUE_MASK, lena_image.blue_mask)
31
+ end
32
+
33
+ def test_color_type
34
+ assert_equal(:palette, sample_image.color_type)
35
+ end
36
+
37
+ def test_dib_size
38
+ assert_equal(52664, sample_image.dib_size)
39
+ end
40
+
41
+ def test_dots_per_meter_x
42
+ assert_equal(2835, sample_image.dots_per_meter_x)
43
+ end
44
+
45
+ def test_dots_per_meter_x_set
46
+ image = sample_image
47
+ image.dots_per_meter_x = 3000
48
+ assert_equal(3000, image.dots_per_meter_x)
49
+ end
50
+
51
+ def test_dots_per_meter_y
52
+ assert_equal(2835, sample_image.dots_per_meter_y)
53
+ end
54
+
55
+ def test_dots_per_meter_y_set
56
+ image = sample_image
57
+ image.dots_per_meter_y = 4000
58
+ assert_equal(4000, image.dots_per_meter_y)
59
+ end
60
+
61
+ def test_green_mask
62
+ assert_equal(0, sample_image.green_mask)
63
+ assert_equal(FreeImage::RGBQuad::GREEN_MASK, lena_image.green_mask)
64
+ end
65
+
66
+ def test_has_background_color
67
+ assert(sample_image.has_background_color)
68
+ end
69
+
70
+ def test_has_pixels
71
+ assert(sample_image.has_pixels)
72
+ end
73
+
74
+ def test_height
75
+ assert_equal(215, sample_image.height)
76
+ end
77
+
78
+ def test_image_type
79
+ assert_equal(:bitmap, sample_image.image_type)
80
+ end
81
+
82
+ def test_line
83
+ assert_equal(240, sample_image.line)
84
+ end
85
+
86
+ def test_pitch
87
+ assert_equal(240, sample_image.pitch)
88
+ end
89
+
90
+ def test_red_mask
91
+ assert_equal(0, sample_image.red_mask)
92
+ assert_equal(FreeImage::RGBQuad::RED_MASK, lena_image.red_mask)
93
+ end
94
+
95
+ def test_transparent
96
+ assert(sample_image.transparent)
97
+ end
98
+
99
+ def test_transparent_set
100
+ image = sample_image
101
+ image.transparent = false
102
+ assert(!image.transparent)
103
+ end
104
+
105
+ def test_transparent_index
106
+ assert_equal(6, sample_image.transparent_index)
107
+ end
108
+
109
+ def test_transparent_index_set
110
+ image = sample_image
111
+ image.transparent_index = 4
112
+ assert_equal(4, image.transparent_index)
113
+ end
114
+
115
+ def test_width
116
+ assert_equal(240, sample_image.width)
117
+ end
118
+ end
@@ -0,0 +1,53 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class IoTest < Test::Unit::TestCase
7
+ def io(image = 'sample.png')
8
+ path = image_path(image)
9
+ file = File.open(path)
10
+ FreeImage::IO.new(file)
11
+ end
12
+
13
+ def test_format
14
+ assert_equal(:png, io.format)
15
+ end
16
+
17
+ def test_format_unknown
18
+ assert_equal(:unknown, io('not_an_image.txt').format)
19
+ end
20
+
21
+ def test_load
22
+ bitmap = io.open
23
+ assert_kind_of(FreeImage::Bitmap, bitmap)
24
+ end
25
+
26
+ def test_load_unknown
27
+ error = assert_raise(FreeImage::Error) do
28
+ io('not_an_image.txt').open
29
+ end
30
+ assert_equal("Cannot load :unknown image format",
31
+ error.message)
32
+ end
33
+
34
+ def test_load_wrong_format
35
+ error = assert_raise(FreeImage::Error) do
36
+ io.open(:jpeg)
37
+ end
38
+ assert_equal("Not a JPEG file: starts with 0x89 0x50", error.to_s)
39
+ end
40
+
41
+ def test_save
42
+ tmp_file = Tempfile.new('test_free_image')
43
+ dst = FreeImage::IO.new(tmp_file)
44
+
45
+ bitmap = io.open
46
+ result = bitmap.save(dst, :png)
47
+ assert(result)
48
+ assert(File.exists?(tmp_file))
49
+ ensure
50
+ tmp_file.close
51
+ tmp_file.unlink
52
+ end
53
+ end
@@ -0,0 +1,65 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class MemoryTest < Test::Unit::TestCase
7
+ def memory(image = 'sample.png')
8
+ data = image_data(image)
9
+ FreeImage::Memory.new(data)
10
+ end
11
+
12
+ def test_wrap
13
+ assert_not_nil(memory)
14
+ end
15
+
16
+ def test_format
17
+ assert_equal(:png, memory.format)
18
+ end
19
+
20
+ def test_format_unknown
21
+ assert_equal(:unknown, memory('not_an_image.txt').format)
22
+ end
23
+
24
+ def test_load
25
+ bitmap = memory.open
26
+ assert_kind_of(FreeImage::Bitmap, bitmap)
27
+ end
28
+
29
+ def test_load_unknown
30
+ error = assert_raise(FreeImage::Error) do
31
+ memory('not_an_image.txt').open
32
+ end
33
+ assert_equal("Cannot load :unknown image format",
34
+ error.message)
35
+ end
36
+
37
+ def test_load_wrong_format
38
+ error = assert_raise(FreeImage::Error) do
39
+ memory.open(:jpeg, 0)
40
+ end
41
+ assert_equal("Not a JPEG file: starts with 0x89 0x50", error.to_s)
42
+ end
43
+
44
+ def test_bytes
45
+ bitmap1 = FreeImage::Bitmap.open(image_path('sample.png'))
46
+
47
+ memory = FreeImage::Memory.new(image_data('sample.png'))
48
+ bitmap2 = memory.open
49
+
50
+ assert_equal(bitmap1.bits, bitmap2.bits)
51
+ end
52
+
53
+ def test_save
54
+ dst = FreeImage::Memory.new
55
+
56
+ bitmap = memory.open
57
+ result = bitmap.save(dst, :png)
58
+
59
+ assert(result)
60
+ assert_not_nil(dst.memory.bytes)
61
+ if defined?(Encoding)
62
+ assert_equal(dst.memory.bytes.encoding, Encoding::BINARY)
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,59 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class ModifyTest < Test::Unit::TestCase
7
+ def test_copy
8
+ bitmap1 = FreeImage::Bitmap.open(image_path('sample.png'))
9
+ bitmap2 = bitmap1.copy(0, 0, 50, 60)
10
+
11
+ assert_kind_of(FreeImage::Bitmap, bitmap2)
12
+ assert_equal(50, bitmap2.width)
13
+ assert_equal(60, bitmap2.height)
14
+ end
15
+
16
+ def test_expand_canvas
17
+ bitmap = FreeImage::Bitmap.open(image_path('sample.png'))
18
+ color = FreeImage::RGBQuad.create(15, 30, 45)
19
+
20
+ result = bitmap.enlarge_canvas(1, 1, 1, 1, color)
21
+ assert_kind_of(FreeImage::Bitmap, result)
22
+ end
23
+
24
+ def test_fill_background
25
+ bitmap = FreeImage::Bitmap.open(image_path('sample.png'))
26
+ color = FreeImage::RGBQuad.create(15, 30, 45)
27
+
28
+ result = bitmap.fill_background!(color)
29
+ assert(result)
30
+ end
31
+
32
+ def test_paste
33
+ bitmap1 = FreeImage::Bitmap.open(image_path('sample.png'))
34
+ bitmap2 = FreeImage::Bitmap.open(image_path('sample.png'))
35
+ result = bitmap1.paste!(bitmap2, 0, 0, 0.5)
36
+
37
+ assert_kind_of(TrueClass, result)
38
+ assert_equal(240, bitmap2.width)
39
+ assert_equal(215, bitmap2.height)
40
+ end
41
+
42
+ def test_rescale
43
+ bitmap1 = FreeImage::Bitmap.open(image_path('sample.png'))
44
+ bitmap2 = bitmap1.rescale(120, 100, :box)
45
+
46
+ assert_kind_of(FreeImage::Bitmap, bitmap2)
47
+ assert_equal(120, bitmap2.width)
48
+ assert_equal(100, bitmap2.height)
49
+ end
50
+
51
+ def test_thumbnail
52
+ bitmap1 = FreeImage::Bitmap.open(image_path('sample.png'))
53
+ bitmap2 = bitmap1.make_thumbnail(100)
54
+
55
+ assert_kind_of(FreeImage::Bitmap, bitmap2)
56
+ assert_equal(100, bitmap2.width)
57
+ assert_equal(90, bitmap2.height)
58
+ end
59
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class PaletteTest < Test::Unit::TestCase
7
+ def test_palette
8
+ palette = sample_image.palette
9
+ assert_not_nil(palette)
10
+ assert_kind_of(FreeImage::Palette, palette)
11
+ end
12
+
13
+ def test_size
14
+ palette = sample_image.palette
15
+ assert_equal(256, palette.size)
16
+ end
17
+
18
+ def test_index
19
+ palette = sample_image.palette
20
+ rgb = palette[27]
21
+
22
+ assert_kind_of(FreeImage::RGBQuad, rgb)
23
+ assert_equal(201, rgb[:red])
24
+ assert_equal(253, rgb[:green])
25
+ assert_equal(0, rgb[:blue])
26
+ end
27
+
28
+ def test_index_too_small
29
+ palette = sample_image.palette
30
+
31
+ error = assert_raise(RangeError) do
32
+ palette[-1]
33
+ end
34
+ assert_equal("Value is out of range 0..256. Value: -1", error.message)
35
+ end
36
+
37
+ def test_index_too_large
38
+ palette = sample_image.palette
39
+
40
+ error = assert_raise(RangeError) do
41
+ palette[300]
42
+ end
43
+ assert_equal("Value is out of range 0..256. Value: 300", error.message)
44
+ end
45
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'test/unit'
5
+
6
+ class PixelTest < Test::Unit::TestCase
7
+ def test_bits
8
+ bytes = sample_image.bits
9
+ assert_not_nil(bytes)
10
+ end
11
+
12
+ def test_pixel_index
13
+ index = sample_image.pixel_index(0, 0)
14
+ assert_equal(113, index)
15
+
16
+ index = lena_image.pixel_index(0, 0)
17
+ assert_nil(index)
18
+ end
19
+
20
+ def test_set_pixel_index
21
+ image = sample_image
22
+ result = image.set_pixel_index(0, 0, 14)
23
+ assert(result)
24
+
25
+ index = image.pixel_index(0, 0)
26
+ assert_equal(14, index)
27
+ end
28
+
29
+ def test_set_pixel_invalid
30
+ image = lena_image
31
+ result = image.set_pixel_index(0, 0, 14)
32
+ assert(!result)
33
+ end
34
+
35
+ def test_pixel_color
36
+ color = lena_image.pixel_color(0, 0)
37
+ assert_kind_of(FreeImage::RGBQuad, color)
38
+
39
+ color = sample_image.pixel_color(0, 0)
40
+ assert_nil(color)
41
+ end
42
+
43
+ def test_set_pixel_color
44
+ image = lena_image
45
+
46
+ color1 = FreeImage::RGBQuad.create(0, 0, 255)
47
+ result = image.set_pixel_color(0, 0, color1)
48
+ assert(result)
49
+
50
+ color2 = image.pixel_color(0, 0)
51
+ assert_equal(color1, color2)
52
+ end
53
+
54
+ def test_set_pixel_invalid
55
+ # Pixel colors aren't supported on paletted images
56
+ image = sample_image
57
+
58
+ color1 = FreeImage::RGBQuad.create(0, 0, 255)
59
+ result = image.set_pixel_color(0, 0, color1)
60
+ assert(!result)
61
+ end
62
+ end