tifffile 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 73ee12cfcfb7b19f45966c553780006c9e2ea981
4
- data.tar.gz: aadabb05a8725757870334ebdffec87f22d0451d
3
+ metadata.gz: cba8faec8b96beb253d6d78be95612d7b75a93f0
4
+ data.tar.gz: 1206e7af1c3771ec3871197a9e4e426dffc6a617
5
5
  SHA512:
6
- metadata.gz: 7713c2989ad9be58d24c32cdb9dc63ea10971f32c376102627e442efe013b0e7d5492118ace195b65af387c0538a3e77b96d93f1d23a06f955880712d57a590a
7
- data.tar.gz: 072ec891538ef01d00efd6210417ad0d90e7cf6b877f716cf72a8caab050841f14bfc9c50d4c37d9dee9373d60d8da2bab112f53c187e30aa64d1a2f55f23612
6
+ metadata.gz: 876651d883fbf0bbbb4edffacc5ee436cbb903d5c78d34413693fad7ef72a0eaf3cca6de1d7e083c692b364d935f423fb13ad960f0b04a82b9e5f1b40028655a
7
+ data.tar.gz: ec2b257624f8cffae6e4c5bc84bf930c82ca920494579419c672b4a8923c42f7dd805f458ad09f6f35c524b05550d456b43df50ec600633ae5049b4d17b6033c
data/.gemspec CHANGED
@@ -3,7 +3,7 @@ require File.expand_path("../lib/tifffile/version", __FILE__)
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'tifffile'
5
5
  gem.version = TiffFileVersion::VERSION
6
- gem.date = '2016-03-22'
6
+ gem.date = '2016-03-21'
7
7
 
8
8
  gem.summary = "TIFF reader and writer"
9
9
  gem.description = ""
@@ -15,7 +15,7 @@ Gem::Specification.new do |gem|
15
15
  gem.homepage = 'https://github.com/kirillmakhonin/tifffile-ruby'
16
16
 
17
17
 
18
- gem.files = `git ls-files`.split($\)
18
+ gem.files = `git ls-files`.split($\) + Dir.glob("lib/**/*") - Dir.glob("ext/**/*") - Dir.glob(".idea/**/*")
19
19
 
20
20
 
21
21
  end
@@ -4,4 +4,4 @@ You are allowed to:
4
4
  1. Remove rake task
5
5
  2. Add existing rake tasks
6
6
  To add existing rake tasks automatically delete this file and reload the project.
7
- --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Compile all the extensions" fullCmd="compile" taksId="compile" /><RakeGroup description="" fullCmd="" taksId="compile"><RakeTask description="Compile tifffile" fullCmd="compile:tifffile" taksId="tifffile" /><RakeGroup description="" fullCmd="" taksId="tifffile"><RakeTask description="" fullCmd="compile:tifffile:x86_64-linux" taksId="x86_64-linux" /></RakeGroup><RakeTask description="" fullCmd="compile:x86_64-linux" taksId="x86_64-linux" /></RakeGroup><RakeGroup description="" fullCmd="" taksId="copy"><RakeGroup description="" fullCmd="" taksId="tifffile"><RakeGroup description="" fullCmd="" taksId="x86_64-linux"><RakeTask description="" fullCmd="copy:tifffile:x86_64-linux:2.2.3" taksId="2.2.3" /></RakeGroup></RakeGroup></RakeGroup><RakeTask description="" fullCmd="lib" taksId="lib" /><RakeTask description="" fullCmd="lib/tifffile" taksId="lib/tifffile" /><RakeTask description="" fullCmd="lib/tifffile/tifffile.so" taksId="lib/tifffile/tifffile.so" /><RakeTask description="" fullCmd="tmp" taksId="tmp" /><RakeTask description="" fullCmd="tmp/x86_64-linux" taksId="tmp/x86_64-linux" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage" taksId="tmp/x86_64-linux/stage" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage/lib" taksId="tmp/x86_64-linux/stage/lib" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage/lib/tifffile" taksId="tmp/x86_64-linux/stage/lib/tifffile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile" taksId="tmp/x86_64-linux/tifffile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3" taksId="tmp/x86_64-linux/tifffile/2.2.3" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3/Makefile" taksId="tmp/x86_64-linux/tifffile/2.2.3/Makefile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3/tifffile.so" taksId="tmp/x86_64-linux/tifffile/2.2.3/tifffile.so" /></RakeGroup></Settings>
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Compile all the extensions" fullCmd="compile" taksId="compile" /><RakeGroup description="" fullCmd="" taksId="compile"><RakeTask description="Compile tifffile" fullCmd="compile:tifffile" taksId="tifffile" /><RakeGroup description="" fullCmd="" taksId="tifffile"><RakeTask description="" fullCmd="compile:tifffile:x86_64-linux" taksId="x86_64-linux" /></RakeGroup><RakeTask description="" fullCmd="compile:x86_64-linux" taksId="x86_64-linux" /></RakeGroup><RakeTask description="Run RSpec code examples" fullCmd="spec" taksId="spec" /><RakeGroup description="" fullCmd="" taksId="copy"><RakeGroup description="" fullCmd="" taksId="tifffile"><RakeGroup description="" fullCmd="" taksId="x86_64-linux"><RakeTask description="" fullCmd="copy:tifffile:x86_64-linux:2.2.3" taksId="2.2.3" /></RakeGroup></RakeGroup></RakeGroup><RakeTask description="" fullCmd="lib" taksId="lib" /><RakeTask description="" fullCmd="lib/tifffile" taksId="lib/tifffile" /><RakeTask description="" fullCmd="lib/tifffile/tifffile.so" taksId="lib/tifffile/tifffile.so" /><RakeTask description="" fullCmd="tmp" taksId="tmp" /><RakeTask description="" fullCmd="tmp/x86_64-linux" taksId="tmp/x86_64-linux" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage" taksId="tmp/x86_64-linux/stage" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage/lib" taksId="tmp/x86_64-linux/stage/lib" /><RakeTask description="" fullCmd="tmp/x86_64-linux/stage/lib/tifffile" taksId="tmp/x86_64-linux/stage/lib/tifffile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile" taksId="tmp/x86_64-linux/tifffile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3" taksId="tmp/x86_64-linux/tifffile/2.2.3" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3/Makefile" taksId="tmp/x86_64-linux/tifffile/2.2.3/Makefile" /><RakeTask description="" fullCmd="tmp/x86_64-linux/tifffile/2.2.3/tifffile.so" taksId="tmp/x86_64-linux/tifffile/2.2.3/tifffile.so" /></RakeGroup></Settings>
@@ -25,4 +25,4 @@ def read(fileName):
25
25
 
26
26
  return out
27
27
 
28
- print(read('a.tiff'))
28
+ print(read('../file.tiff'))
@@ -0,0 +1,103 @@
1
+ module TiffFileTags
2
+
3
+ class Tag
4
+ attr_accessor :name
5
+ attr_accessor :id
6
+ attr_accessor :type
7
+ attr_accessor :map
8
+
9
+ # Creates a new tag.
10
+
11
+ # Examples:
12
+ #
13
+ # width = Tag.new(:width, 256, :uint)
14
+ # metric = Tag.new(:photometric, 262, :ushort, {
15
+ # min_is_white: 0,
16
+ # min_is_black: 1
17
+ # })
18
+ #
19
+ def initialize(name, id, type, map = nil)
20
+ @name = name
21
+ @id = id
22
+ @type = type
23
+ @map = map
24
+ end
25
+ end
26
+
27
+
28
+ class << self
29
+ def tags
30
+ @@tags
31
+ end
32
+ end
33
+
34
+ def tag(name)
35
+ key = name.to_s.to_sym
36
+ @@tags.has_key?(key) ? @@tags[key] : nil
37
+ end
38
+
39
+ @@tags = Hash.new do |hash, key|
40
+ raise KeyError, "Tag #{key.inspect} is unsupported. Available tags: #{hash.keys}"
41
+ end
42
+
43
+ tags[:artist] = Tag.new(:artist, 315, :string)
44
+ tags[:bad_fax_lines] = Tag.new(:bad_fax_lines, 326, :uint)
45
+ tags[:bits_per_sample] = Tag.new(:bits_per_sample, 258, :ushort)
46
+ tags[:clean_fax_data] = Tag.new(:clean_fax_data, 327, :ushort)
47
+ tags[:compression] = Tag.new(:compression, 259, :ushort,
48
+ none: 1,
49
+ CCITTFAX3: 3,
50
+ CCITTFAX4: 4,
51
+ JPEG: 7
52
+ )
53
+ tags[:consecutive_bad_fax_lines] = Tag.new(:consecutive_bad_fax_lines, 328, :uint)
54
+ tags[:copyright] = Tag.new(:copyright, 33432, :string)
55
+ tags[:data_type] = Tag.new(:data_type, 32996, :ushort)
56
+ tags[:date_time] = Tag.new(:date_time, 306, :string)
57
+ tags[:document_name] = Tag.new(:document_name, 269, :string)
58
+ tags[:fill_order] = Tag.new(:fill_order, 266, :ushort)
59
+ tags[:group3_options] = Tag.new(:group3_options, 292, :uint)
60
+ tags[:group4_options] = Tag.new(:group4_options, 293, :uint)
61
+ tags[:height] = Tag.new(:height, 257, :uint)
62
+ tags[:host_computer] = Tag.new(:host_computer, 316, :string)
63
+ tags[:image_depth] = Tag.new(:image_depth, 32997, :uint)
64
+ tags[:image_description] = Tag.new(:image_description, 270, :string)
65
+ tags[:ink_names] = Tag.new(:ink_names, 333, :string)
66
+ tags[:ink_set] = Tag.new(:ink_set, 332, :ushort)
67
+ tags[:make] = Tag.new(:make, 271, :string)
68
+ tags[:matteing] = Tag.new(:matteing, 32995, :ushort)
69
+ tags[:max_sample_value] = Tag.new(:max_sample_value, 281, :ushort)
70
+ tags[:min_sample_value] = Tag.new(:min_sample_value, 280, :ushort)
71
+ tags[:model] = Tag.new(:model, 272, :string)
72
+ tags[:orientation] = Tag.new(:orientation, 274, :ushort)
73
+ tags[:page_name] = Tag.new(:page_name, 285, :string)
74
+ tags[:planar_config] = Tag.new(:page_name, 284, :ushort)
75
+ tags[:photometric] = Tag.new(:photometric, 262, :ushort,
76
+ min_is_white: 0,
77
+ min_is_black: 1,
78
+ rgb: 2,
79
+ palette: 3,
80
+ separated: 5
81
+ )
82
+ tags[:predictor] = Tag.new(:predictor, 317, :ushort)
83
+ tags[:resolution_unit] = Tag.new(:resolution_unit, 296, :ushort, none: 1, inch: 2, centimeter: 3)
84
+ tags[:rows_per_strip] = Tag.new(:rows_per_strip, 278, :uint)
85
+ tags[:s_max_sample_value] = Tag.new(:s_max_sample_value, 341, :double)
86
+ tags[:s_min_sample_value] = Tag.new(:s_min_sample_value, 340, :double)
87
+ tags[:sample_format] = Tag.new(:sample_format, 339, :ushort)
88
+ tags[:samples_per_pixel] = Tag.new(:samples_per_pixel, 277, :ushort)
89
+ tags[:software] = Tag.new(:software, 305, :string)
90
+ tags[:sub_file_type] = Tag.new(:sub_file_type, 255, :uint)
91
+ tags[:target_printer] = Tag.new(:target_printer, 337, :string)
92
+ tags[:tile_depth] = Tag.new(:tile_depth, 32998, :uint)
93
+ tags[:tile_length] = Tag.new(:tile_length, 323, :uint)
94
+ tags[:tile_width] = Tag.new(:tile_width, 322, :uint)
95
+ tags[:width] = Tag.new(:width, 256, :uint)
96
+ tags[:x_position] = Tag.new(:x_position, 286, :float)
97
+ tags[:x_resolution] = Tag.new(:x_resolution, 282, :float)
98
+ tags[:y_cb_cr_positioning] = Tag.new(:y_cb_cr_positioning, 531, :ushort)
99
+ tags[:y_position] = Tag.new(:y_position, 286, :float)
100
+ tags[:y_resolution] = Tag.new(:y_resolution, 283, :float)
101
+
102
+
103
+ end
@@ -1,28 +1,74 @@
1
1
  require 'tifffile/version'
2
2
  require "tifffile/tifffile"
3
+ require "tags"
3
4
 
4
- module TiffFile
5
+ class TiffFile
5
6
 
6
7
  include TiffFileVersion
8
+ include TiffFileTags
7
9
 
8
- # Will write a TIFF file +destination+, that contains +values+ matrix (in one channel), each sample has +sample_size+ in bytes (1, 2, 4 or 8), +sample_unsigned+.
9
- # Also, image have +description+ and reference to +software+
10
- def self.tiff2file(destination, values, sample_size=16, sample_unsigned=false, description=nil, software=nil)
11
- self.to_tiff(destination.to_s, values, sample_size.to_i, sample_unsigned == true, description.to_s, software.to_s)
10
+
11
+ alias :valid? :is_valid?
12
+ alias :is_big_endian? :big_endian?
13
+ alias :sample_format :sample_format?
14
+
15
+ alias :unset_field :remove_field
16
+
17
+ alias :set :write_matrix
18
+ alias :get :read_matrix
19
+
20
+ alias :flush! :save
21
+ alias :save! :save
22
+
23
+ # Get field value
24
+ def get_field(fname)
25
+ field = tag fname
26
+ return nil if field.nil?
27
+
28
+ value = case field.type
29
+ when :string
30
+ get_string_field field.id
31
+ when :ushort
32
+ get_int_field field.id, 2
33
+ when :uint
34
+ get_int_field field.id, 4
35
+ when :double
36
+ get_float_field field.id, true
37
+ when :float
38
+ get_float_field field.id, true
39
+ else
40
+ nil
41
+ end
42
+
43
+ return value if field.map.nil?
44
+ field.map.has_value?(value) ? field.map.key(value).to_sym : nil
12
45
  end
13
46
 
47
+ # Set field value
48
+ def set_field(fname, fvalue)
49
+ field = tag fname
50
+ return nil if field.nil?
14
51
 
15
- # Binary get TIFF file that contains +values+ matrix (in one channel), each sample has +sample_size+ in bytes (1, 2, 4 or 8), +sample_unsigned+.
16
- # Also, image have +description+ and reference to +software+
17
- def self.tiff2binary(values, sample_size=16, description=nil, software=nil)
18
- file = Tempfile.new('tiff2binary')
19
- file.close
52
+ symkey = fname.to_s.to_sym
53
+ value = (field.map.nil? or not field.map.has_key?(symkey)) ? fvalue : field.map[symkey]
20
54
 
21
- tiff2file(file.path, values, sample_size, description, software)
55
+ case field.type
56
+ when :string
57
+ set_string_field field.id, value.to_s
58
+ when :ushort
59
+ set_int_field field.id, value.to_i, 2
60
+ when :uint
61
+ set_int_field field.id, value.to_i, 4
62
+ when :double
63
+ set_float_field field.id, value.to_i, true
64
+ when :float
65
+ set_float_field field.id, value.to_i, true
66
+ else
67
+ nil
68
+ end
22
69
 
23
- data = File.read(file)
24
- file.unlink
25
- data
26
70
  end
27
71
 
72
+
73
+
28
74
  end
@@ -1,3 +1,3 @@
1
1
  module TiffFileVersion
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -1,7 +1,43 @@
1
1
  require 'tifffile'
2
2
 
3
3
  describe "TiffFile" do
4
- it "building tiff file" do
5
- p TiffFile.tiff2file 'a.tiff', [[10, 21], [20, 30]], 8, false, "Image description", "SOFTWARE"
4
+
5
+ File_Path = "file.tiff"
6
+
7
+ # it "building tiff file" do
8
+ # #TiffFile.tiff2file 'a.tiff', [[10, 20], [20, 30]]
9
+ #
10
+ # File.unlink(File_Path) if File.exist? File_Path
11
+ #
12
+ # t = TiffFile.new File_Path, "w"
13
+ # #p "Is big endian? = #{t.big_endian?.inspect}"
14
+ # #p "Inspect is a #{t.inspect}"
15
+ # #p "Methods is a #{t.methods}"
16
+ # m = [[10.23, 20.15], [30.60, 50.45]]
17
+ #
18
+ # #t.set_field :copyright, "COPYRIGHT"
19
+ # #puts "Copyyright #{t.get_field :copyright}"
20
+ # ans = t.write_matrix(m, 4, false)
21
+ # p ans.inspect
22
+ # end
23
+
24
+ it "reading tiff file" do
25
+ raise Exception.new("Cannot find file #{File_Path}") if not File.exist? File_Path
26
+
27
+ t = TiffFile.new File_Path, "r"
28
+
29
+ puts "Validation: #{t.is_valid?}"
30
+
31
+ puts "Width: #{t.get_int_field 256, 2}"
32
+ puts "FF Set: #{t.set_float_field 341, 5555.28, true}"
33
+ puts "FF Get: #{t.get_float_field 341, true}"
34
+ puts "FF Get unsetted: #{t.get_float_field 340, true}"
35
+
36
+ puts "Width is #{t.get_field :width}"
37
+ puts "Photometric is #{t.get_field :photometric}"
38
+
39
+ ans = t.read_matrix
40
+
41
+ puts ans.inspect
6
42
  end
7
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tifffile
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
  - Kirill Makhonin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-22 00:00:00.000000000 Z
11
+ date: 2016-03-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: ''
14
14
  email: kroks.rus@gmail.com
@@ -19,16 +19,12 @@ files:
19
19
  - ".gemspec"
20
20
  - ".gitignore"
21
21
  - ".idea/.rakeTasks"
22
- - ".idea/encodings.xml"
23
- - ".idea/misc.xml"
24
- - ".idea/modules.xml"
25
- - ".idea/tifffile-ruby.iml"
26
22
  - LICENSE
27
23
  - Rakefile
28
24
  - analyzer.py
29
- - ext/tifffile/extconf.rb
30
- - ext/tifffile/tifffile.cpp
25
+ - lib/tags.rb
31
26
  - lib/tifffile.rb
27
+ - lib/tifffile/tifffile.so
32
28
  - lib/tifffile/version.rb
33
29
  - spec/tifffile_spec.rb
34
30
  homepage: https://github.com/kirillmakhonin/tifffile-ruby
@@ -51,8 +47,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
47
  version: '0'
52
48
  requirements: []
53
49
  rubyforge_project:
54
- rubygems_version: 2.4.5.1
50
+ rubygems_version: 2.4.8
55
51
  signing_key:
56
52
  specification_version: 4
57
53
  summary: TIFF reader and writer
58
54
  test_files: []
55
+ has_rdoc:
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="Encoding">
4
- <file url="PROJECT" charset="UTF-8" />
5
- </component>
6
- </project>
@@ -1,14 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectLevelVcsManager" settingsEditedManually="false">
4
- <OptionsSetting value="true" id="Add" />
5
- <OptionsSetting value="true" id="Remove" />
6
- <OptionsSetting value="true" id="Checkout" />
7
- <OptionsSetting value="true" id="Update" />
8
- <OptionsSetting value="true" id="Status" />
9
- <OptionsSetting value="true" id="Edit" />
10
- <ConfirmationsSetting value="0" id="Add" />
11
- <ConfirmationsSetting value="0" id="Remove" />
12
- </component>
13
- <component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-2.2.3 [global]" project-jdk-type="RUBY_SDK" />
14
- </project>
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/tifffile-ruby.iml" filepath="$PROJECT_DIR$/.idea/tifffile-ruby.iml" />
6
- </modules>
7
- </component>
8
- </project>
@@ -1,20 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="RUBY_MODULE" version="4">
3
- <component name="FacetManager">
4
- <facet type="gem" name="Ruby Gem">
5
- <configuration>
6
- <option name="GEM_APP_ROOT_PATH" value="$MODULE_DIR$" />
7
- <option name="GEM_APP_TEST_PATH" value="" />
8
- <option name="GEM_APP_LIB_PATH" value="$MODULE_DIR$/lib" />
9
- </configuration>
10
- </facet>
11
- </component>
12
- <component name="NewModuleRootManager">
13
- <content url="file://$MODULE_DIR$">
14
- <excludeFolder url="file://$MODULE_DIR$/.bundle" />
15
- <excludeFolder url="file://$MODULE_DIR$/vendor/bundle" />
16
- </content>
17
- <orderEntry type="inheritedJdk" />
18
- <orderEntry type="sourceFolder" forTests="false" />
19
- </component>
20
- </module>
@@ -1,62 +0,0 @@
1
- require "mkmf"
2
-
3
- # Add C++ support
4
-
5
- have_library( 'stdc++' );
6
- $CFLAGS << " -Wall"
7
-
8
-
9
-
10
-
11
- HEADER_DIRS = [
12
- # First search /opt/local for macports
13
- '/opt/local/include',
14
-
15
- # Then search /usr/local for people that installed from source
16
- '/usr/local/include',
17
-
18
- # Check the ruby install locations
19
- #INCLUDEDIR,
20
-
21
- # Finally fall back to /usr
22
- '/usr/include',
23
- ]
24
-
25
- LIB_DIRS = [
26
- # First search /opt/local for macports
27
- '/opt/local/lib',
28
-
29
- # Then search /usr/local for people that installed from source
30
- '/usr/local/lib',
31
-
32
- # Check the ruby install locations
33
- #LIBDIR,
34
-
35
- # Finally fall back to /usr
36
- '/usr/lib',
37
- ]
38
-
39
-
40
- libtiff_dirs = dir_config('libtiff', '/opt/local/include', '/opt/local/lib')
41
-
42
- unless ["", ""] == libtiff_dirs
43
- HEADER_DIRS.unshift libtiff_dirs.first
44
- LIB_DIRS.unshift libtiff_dirs[1]
45
- end
46
-
47
- unless find_header('tiffio.h', *HEADER_DIRS)
48
- abort "libtiff is missing. please install libtiff6-dev"
49
- end
50
-
51
- unless find_library('tiff', 'TIFFOpen', *LIB_DIRS)
52
- abort "libtiff is missing. please install libtiff-dev"
53
- end
54
-
55
-
56
-
57
- abort "missing malloc()" unless have_func "malloc"
58
- abort "missing free()" unless have_func "free"
59
-
60
-
61
- create_makefile "tifffile/tifffile"
62
-
@@ -1,237 +0,0 @@
1
- #include <ruby.h>
2
- #include <stdio.h>
3
- #include <stdlib.h>
4
- #include <string>
5
- #include "tiffio.h"
6
- #include <vector>
7
- #include <algorithm>
8
-
9
- using namespace std;
10
-
11
- template <class T>
12
- static bool rows_size_compare(std::vector<T> & a, std::vector<T> & b)
13
- {
14
- return (a.size() < b.size());
15
- }
16
-
17
- template <class T>
18
- void calculate_width_height(std::vector<std::vector<T> > & data, uint32 & width, uint32 & height){
19
- height = data.size();
20
- width = std::max_element(data.begin(), data.end(), rows_size_compare<T>)->size();
21
- }
22
-
23
-
24
- template <class T>
25
- void write_data_to_row_buffer(std::vector<T> & data, unsigned char *buf, uint64 buff_size, uint8 sampleperpixel, uint8 bitspersample){
26
- memset(buf, 0, buff_size);
27
- buff_size = std::min<uint64>(buff_size, sizeof(T) * data.size());
28
- memcpy(buf, (void *)(&(data[0])), buff_size);
29
- }
30
-
31
-
32
- template <class T>
33
- bool write_tiff_file(std::string filename, std::vector<std::vector<T> > & data, uint8 sampleperpixel, uint8 bitspersample, std::string description, std::string software){
34
- try {
35
- TIFF* tif = TIFFOpen(filename.c_str(), "w");
36
-
37
- uint32 width, height;
38
- calculate_width_height(data, width, height);
39
-
40
- TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
41
- TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
42
- TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sampleperpixel);
43
- TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
44
- TIFFSetField(tif, TIFFTAG_ORIENTATION, (int)ORIENTATION_TOPLEFT);
45
- TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
46
- TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
47
- TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
48
- TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, (char*)description.c_str());
49
- TIFFSetField(tif, TIFFTAG_SOFTWARE, (char*)software.c_str());
50
-
51
- uint64 linebytes = sampleperpixel * width * (bitspersample / 8);
52
- unsigned char *buf = NULL;
53
- buf = (unsigned char *)_TIFFmalloc(linebytes);
54
-
55
- TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
56
-
57
- for (uint32 row = 0; row < height; row++){
58
- std::vector<T> row_data = data[row];
59
- write_data_to_row_buffer(row_data, buf, linebytes, sampleperpixel, bitspersample);
60
- if (TIFFWriteScanline(tif, buf, row, 0) < 0)
61
- break;
62
- }
63
-
64
- if (buf)
65
- _TIFFfree(buf);
66
-
67
- TIFFClose(tif);
68
- }
69
- catch (...){
70
- return false;
71
- }
72
- return true;
73
- }
74
-
75
-
76
- bool get_matrix_element_type(VALUE data_matrix, bool &is_float){
77
- if (!RB_TYPE_P(data_matrix, T_ARRAY) || RARRAY_LEN(data_matrix) < 1)
78
- return false;
79
-
80
- VALUE first_row = RARRAY_PTR(data_matrix)[0];
81
-
82
- if (!RB_TYPE_P(first_row, T_ARRAY) || RARRAY_LEN(first_row) < 1)
83
- return false;
84
-
85
- VALUE first_item = RARRAY_PTR(first_row)[0];
86
-
87
- if (!RB_TYPE_P(first_item, T_FIXNUM) && !RB_TYPE_P(first_item, T_FLOAT))
88
- return false;
89
-
90
- is_float = RB_TYPE_P(first_item, T_FLOAT);
91
-
92
- return true;
93
- }
94
-
95
- template <class F, class T>
96
- vector<vector<T> > matrix_type_cast(vector<vector<F> > & from){
97
- vector<vector<T> > result;
98
- result.resize(from.size());
99
-
100
- for (int i = 0; i < from.size(); i++)
101
- result[i].assign(from[i].begin(), from[i].end());
102
-
103
- return result;
104
- };
105
-
106
- VALUE TiffFile = Qnil;
107
-
108
- VALUE method_tifffile_converter_to_tiff(VALUE self, VALUE filename, VALUE data_matrix, VALUE sample_size, VALUE sample_unsigned, VALUE description, VALUE software){
109
- data_matrix = rb_check_array_type(data_matrix);
110
-
111
-
112
- Check_Type(filename, T_STRING);
113
- Check_Type(data_matrix, T_ARRAY);
114
- Check_Type(sample_size, T_FIXNUM);
115
- Check_Type(description, T_STRING);
116
- Check_Type(software, T_STRING);
117
-
118
- uint8 size = std::min<uint8>(NUM2USHORT(sample_size), 8);
119
-
120
- bool unsigned_flag = RTEST(sample_unsigned);
121
-
122
- bool is_float_matrix;
123
- if (!get_matrix_element_type(data_matrix, is_float_matrix))
124
- rb_raise(rb_eRuntimeError, "Invalid matrix items type");
125
-
126
- if (size != 1 && size != 2 && size != 4 && size != 8)
127
- rb_raise(rb_eRuntimeError, "Sample size should be in range [1, 2, 4, 8]");
128
-
129
- if (is_float_matrix && (size % 4) != 0)
130
- rb_raise(rb_eRuntimeError, "Sample size should be 4 or 8 on non-fixnum matrix");
131
-
132
- vector<vector<double> > data_d;
133
- vector<double> temp_d;
134
- vector<vector<int64> > data_i;
135
- vector<int64> temp_i;
136
-
137
- VALUE row, item;
138
-
139
- if (is_float_matrix)
140
- data_d.resize(RARRAY_LEN(data_matrix));
141
- else
142
- data_i.resize(RARRAY_LEN(data_matrix));
143
-
144
-
145
- for (long i = 0; i < RARRAY_LEN(data_matrix); i++){
146
- row = RARRAY_PTR(data_matrix)[i];
147
-
148
- if (!RB_TYPE_P(row, T_ARRAY))
149
- rb_raise(rb_eRuntimeError, "Matrix row %ld is not an array", i);
150
-
151
- // Clear row buffer
152
- if (is_float_matrix){
153
- temp_d.clear();
154
- temp_d.resize(RARRAY_LEN(row));
155
- } else {
156
- temp_i.clear();
157
- temp_i.resize(RARRAY_LEN(row));
158
- }
159
-
160
-
161
- // Read row
162
- for (long j = 0; j < RARRAY_LEN(row); j++){
163
- item = RARRAY_PTR(row)[j];
164
- Check_Type(item, is_float_matrix ? T_FLOAT : T_FIXNUM);
165
- if (is_float_matrix)
166
- temp_d[j] = RFLOAT_VALUE(item);
167
- else
168
- temp_i[j] = FIX2LONG(item);
169
- }
170
-
171
- if (is_float_matrix)
172
- data_d[i] = temp_d;
173
- else
174
- data_i[i] = temp_i;
175
- }
176
-
177
-
178
- bool result = false;
179
- if (is_float_matrix){
180
- if (size == 8)
181
- result = write_tiff_file<double>(StringValuePtr(filename), data_d, 1, 64, StringValuePtr(description), StringValuePtr(software));
182
- else if (size == 4){
183
- vector<vector<float> > cast_buffer = matrix_type_cast<double, float>(data_d);
184
- result = write_tiff_file<float>(StringValuePtr(filename), cast_buffer, 1, 32, StringValuePtr(description), StringValuePtr(software));
185
- }
186
- else
187
- rb_raise(rb_eRuntimeError, "unrecognized sample size for float matrix : %d", size);
188
- }
189
- else {
190
- if (unsigned_flag){
191
- if (size == 8){
192
- vector<vector<uint64> > cast_buffer = matrix_type_cast<int64, uint64>(data_i);
193
- result = write_tiff_file<uint64>(StringValuePtr(filename), cast_buffer, 1, 64, StringValuePtr(description), StringValuePtr(software));
194
- }
195
- else if (size == 4){
196
- vector<vector<uint32> > cast_buffer = matrix_type_cast<int64, uint32>(data_i);
197
- result = write_tiff_file<uint32>(StringValuePtr(filename), cast_buffer, 1, 32, StringValuePtr(description), StringValuePtr(software));
198
- }
199
- else if (size == 2){
200
- vector<vector<uint16> > cast_buffer = matrix_type_cast<int64, uint16>(data_i);
201
- result = write_tiff_file<uint16>(StringValuePtr(filename), cast_buffer, 1, 16, StringValuePtr(description), StringValuePtr(software));
202
- }
203
- else if (size == 1){
204
- vector<vector<uint8> > cast_buffer = matrix_type_cast<int64, uint8>(data_i);
205
- result = write_tiff_file<uint8>(StringValuePtr(filename), cast_buffer, 1, 8, StringValuePtr(description), StringValuePtr(software));
206
- }
207
- else
208
- rb_raise(rb_eRuntimeError, "unrecognized sample size for unsigned numeric matrix : %d", size);
209
- }
210
- else {
211
- if (size == 8){
212
- result = write_tiff_file<int64>(StringValuePtr(filename), data_i, 1, 64, StringValuePtr(description), StringValuePtr(software));
213
- }
214
- else if (size == 4){
215
- vector<vector<int32> > cast_buffer = matrix_type_cast<int64, int32>(data_i);
216
- result = write_tiff_file<int32>(StringValuePtr(filename), cast_buffer, 1, 32, StringValuePtr(description), StringValuePtr(software));
217
- }
218
- else if (size == 2){
219
- vector<vector<int16> > cast_buffer = matrix_type_cast<int64, int16>(data_i);
220
- result = write_tiff_file<int16>(StringValuePtr(filename), cast_buffer, 1, 16, StringValuePtr(description), StringValuePtr(software));
221
- }
222
- else if (size == 1){
223
- vector<vector<int8> > cast_buffer = matrix_type_cast<int64, int8>(data_i);
224
- result = write_tiff_file<int8>(StringValuePtr(filename), cast_buffer, 1, 8, StringValuePtr(description), StringValuePtr(software));
225
- }
226
- else
227
- rb_raise(rb_eRuntimeError, "unrecognized sample size for signed numeric matrix : %d", size);
228
- }
229
- }
230
-
231
- return result ? Qtrue : Qfalse;
232
- }
233
-
234
- extern "C" void Init_tifffile(){
235
- TiffFile = rb_define_module("TiffFile");
236
- rb_define_singleton_method(TiffFile, "to_tiff", (VALUE(*)(ANYARGS))method_tifffile_converter_to_tiff, 6);
237
- }