free-image 0.6.2 → 0.7.0

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