Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

implement roughly

  • Loading branch information...
commit c0e9d5287ad5aa5704835e38f7d987608c30ea84 0 parents
@labocho authored
18 .gitignore
@@ -0,0 +1,18 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+.DS_Store
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
4 Gemfile
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in mpo.gemspec
+gemspec
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2012 labocho
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 README.md
@@ -0,0 +1,29 @@
+# Mpo
+
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'mpo'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install mpo
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
2  Rakefile
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
21 lib/mpo.rb
@@ -0,0 +1,21 @@
+require "mpo/version"
+
+module Mpo
+ require "mpo/app2"
+ require "mpo/jpeg"
+
+ module_function
+ def from_files(left_path, right_path)
+ left = Jpeg.load_file(left_path)
+ right = Jpeg.load_file(right_path)
+
+ left.app2 = App2.new
+ right.app2 = App2ForIndivisualImage.new
+
+ left.app2.mp_index_ifd.indivisual_image_size_1 = left.file_size
+ left.app2.mp_index_ifd.indivisual_image_size_2 = right.file_size
+ left.app2.mp_index_ifd.indivisual_image_data_offset_2 = left.file_size - left.mp_header_pos
+
+ left.to_s + right.to_s
+ end
+end
158 lib/mpo/app2.rb
@@ -0,0 +1,158 @@
+require "bindata"
+
+module IFD_VALUE_TYPE
+ LONG = 0x0004
+ RATIONAL = 0x0005
+ UNDEFINED = 0x0007
+ SRATIONAL = 0x000A
+end
+
+class MPIndexIFD < BinData::Record
+ endian :big
+
+ # MP Index IFD
+ uint16 :field_count, value: 3
+
+ uint16 :mp_format_version_number_tag, value: 0xB000
+ uint16 :mp_format_version_number_type, value: IFD_VALUE_TYPE::UNDEFINED
+ uint32 :mp_format_version_number_count, value: 4
+ string :mp_format_version_number_value, value: "0100"
+
+ uint16 :number_of_images_tag, value: 0xB001
+ uint16 :number_of_images_type, value: IFD_VALUE_TYPE::LONG
+ uint32 :number_of_images_count, value: 1
+ uint32 :number_of_images_value, value: 2
+
+ uint16 :mp_entry_tag, value: 0xB002
+ uint16 :mp_entry_type, value: IFD_VALUE_TYPE::UNDEFINED
+ uint32 :mp_entry_count, value: 2 * 16
+ uint32 :mp_entry_offset, value: 0x32
+
+ # Offset of Next IFD
+ uint32 :offset_of_next_ifd, value: 0x52
+
+ # MP Entry
+ uint32 :individual_image_attribute_1, value: 0x20020002
+ uint32 :indivisual_image_size_1 # SOI-EOI のサイズ
+ uint32 :indivisual_image_data_offset_1, value: 0
+ uint16 :dependent_image_1_entry_number_1, value: 0
+ uint16 :dependent_image_2_entry_number_1, value: 0
+
+ uint32 :individual_image_attribute_2, value: 0x00020002
+ uint32 :indivisual_image_size_2 # SOI-EOI のサイズ
+ uint32 :indivisual_image_data_offset_2 # :endian からこの画像の SOI へのオフセット
+ uint16 :dependent_image_1_entry_number_2, value: 0
+ uint16 :dependent_image_2_entry_number_2, value: 0
+end
+
+class MPAttributesIFD < BinData::Record
+ endian :big
+
+ uint16 :field_count, value: 4
+
+ uint16 :mp_insivisual_image_number_tag, value: 0xB101
+ uint16 :mp_insivisual_image_number_type, value: IFD_VALUE_TYPE::LONG
+ uint32 :mp_insivisual_image_number_count, value: 1
+ uint32 :mp_insivisual_image_number_value, value: 1
+
+ uint16 :base_viewpoint_number_tag, value: 0xB204
+ uint16 :base_viewpoint_number_type, value: IFD_VALUE_TYPE::LONG
+ uint32 :base_viewpoint_number_count, value: 1
+ uint32 :base_viewpoint_number_value, value: 1
+
+ uint16 :convergence_angle_tag, value: 0xB205
+ uint16 :convergence_angle_type, value: IFD_VALUE_TYPE::SRATIONAL
+ uint32 :convergence_angle_count, value: 1
+ uint32 :convergence_angle_offset, value: 0x88 # MP ヘッダから値へのオフセット
+
+ uint16 :baseline_length_tag, value: 0xB206
+ uint16 :baseline_length_type, value: IFD_VALUE_TYPE::RATIONAL
+ uint32 :baseline_length_count, value: 1
+ uint32 :baseline_length_offset, value: 0x90 # MP ヘッダから値へのオフセット
+
+ uint32 :offset_of_next_ifd, value: 0
+
+ # Values
+ uint32 :convergence_angle_numerator, value: 0xFFFFFFFF
+ uint32 :convergence_angle_denominator, value: 0xFFFFFFFF
+
+ uint32 :baseline_length_numerator, value: 0xFFFFFFFF
+ uint32 :baseline_length_denominator, value: 0xFFFFFFFF
+end
+
+class MPAttributesIFDForIndivisualImage < BinData::Record
+ endian :big
+
+ uint16 :field_count, value: 5
+
+ uint16 :mp_format_version_tag, value: 0xB000
+ uint16 :mp_format_version_type, value: IFD_VALUE_TYPE::UNDEFINED
+ uint32 :mp_format_version_count, value: 4
+ string :mp_format_version_value, value: "0100"
+
+ uint16 :mp_insivisual_image_number_tag, value: 0xB101
+ uint16 :mp_insivisual_image_number_type, value: IFD_VALUE_TYPE::LONG
+ uint32 :mp_insivisual_image_number_count, value: 1
+ uint32 :mp_insivisual_image_number_value, value: 2 # 2 枚目
+
+ uint16 :base_viewpoint_number_tag, value: 0xB204
+ uint16 :base_viewpoint_number_type, value: IFD_VALUE_TYPE::LONG
+ uint32 :base_viewpoint_number_count, value: 1
+ uint32 :base_viewpoint_number_value, value: 1
+
+ uint16 :convergence_angle_tag, value: 0xB205
+ uint16 :convergence_angle_type, value: IFD_VALUE_TYPE::SRATIONAL
+ uint32 :convergence_angle_count, value: 1
+ uint32 :convergence_angle_offset, value: 0x4A # MP ヘッダから値へのオフセット
+
+ uint16 :baseline_length_tag, value: 0xB206
+ uint16 :baseline_length_type, value: IFD_VALUE_TYPE::RATIONAL
+ uint32 :baseline_length_count, value: 1
+ uint32 :baseline_length_offset, value: 0x52 # MP ヘッダから値へのオフセット
+
+ uint32 :offset_of_next_ifd, value: 0
+
+ # Values
+ uint32 :convergence_angle_numerator, value: 0xFFFFFFFF
+ uint32 :convergence_angle_denominator, value: 0xFFFFFFFF
+
+ uint32 :baseline_length_numerator, value: 0xFFFFFFFF
+ uint32 :baseline_length_denominator, value: 0xFFFFFFFF
+end
+
+class App2 < BinData::Record
+ string :marker, value: "\xFF\xE2"
+ uint16be :field_length, value: 0x9E
+ string :mp_format_identifier, value: "MPF\0"
+
+ # MP Header
+ string :mp_endian, value: "\x4D\x4D\x00\x2A" # Big Endian
+ uint32be :offset_of_first_ifd, value: 8
+
+ mp_index_ifd :mp_index_ifd
+
+ mp_attributes_ifd :mp_attributes_ifd
+end
+
+class App2ForIndivisualImage < BinData::Record
+ string :marker, value: "\xFF\xE2"
+ uint16be :field_length, value: 0x60
+ string :mp_format_identifier, value: "MPF\0"
+
+ # MP Header
+ string :mp_endian, value: "\x4D\x4D\x00\x2A" # Big Endian
+ uint32be :offset_of_first_ifd, value: 8
+
+ mp_attributes_ifd_for_indivisual_image :mp_attributes_ifd
+end
+
+# app2 = App2.new
+# app2.mp_index_ifd.indivisual_image_size_1 = 0
+# app2.mp_index_ifd.indivisual_image_size_2 = 0
+# app2.mp_index_ifd.indivisual_image_data_offset_2 = 0
+
+# open("app2.bin", "w"){|f| f.write app2.to_binary_s }
+
+# app2i = App2ForIndivisualImage.new
+
+# open("app2i.bin", "w"){|f| f.write app2i.to_binary_s }
105 lib/mpo/jpeg.rb
@@ -0,0 +1,105 @@
+require "stringio"
+require "debugger"
+module Mpo
+ class Jpeg
+ SOI = "\xFF\xD8" # Start Of Image
+ SOS = "\xFF\xDA" # Start of Scan
+ EOI = "\xFF\xD9" # End Of Image
+
+ APP0 = "\xFF\xE0" # JFIF
+ APP1 = "\xFF\xE1" # Exif
+ APP2 = "\xFF\xE2" # MPO
+
+ class FormatError < StandardError; end
+ class Segment
+ attr_accessor :marker, :length, :body
+ def to_s
+ [marker, [length].pack("n"), body].join
+ end
+
+ def inspect
+ "<Mpo::Jpeg::Segment marker: #{marker.dump} length: #{length}>"
+ end
+ end
+
+ attr_accessor :raw, :image
+
+ def self.load_file(path)
+ jpeg = new
+ jpeg.raw = File.read(path, encoding: "ascii-8bit")
+ jpeg.parse
+ jpeg
+ end
+
+ def segments
+ @segments ||= []
+ end
+
+ # APP1 セグメントの直後に app2 を挿入する
+ # APP1 セグメントが APP0 の直後、
+ # それもなければ最初
+ # すでに APP2 があれば置き換える
+ def app2=(app2)
+ case
+ when index_of_app2 = segments.find_index{|s| s.marker == APP2}
+ segments[index_of_app2] = app2
+ when index_of_app1 = segments.find_index{|s| s.marker == APP1}
+ segments.insert(index_of_app1 + 1, app2)
+ when index_of_app0 = segments.find_index{|s| s.marker == APP0}
+ segments.insert(index_of_app0 + 1, app2)
+ else
+ segments.insert(0, app2)
+ end
+ end
+
+ def app2
+ segments.find{|s| s.marker == APP2}
+ end
+
+ def parse
+ @io = StringIO.new(@raw)
+ raise FormatError unless @io.read(2) == SOI
+
+ while true
+ segment = Segment.new
+ segment.marker = @io.read(2)
+ segment.length = @io.read(2).unpack("n").first
+ segment.body = @io.read(segment.length - 2)
+ segments << segment
+ break if segment.marker == SOS
+ end
+
+ image_buffer = ""
+ while true
+ image_buffer << @io.read(1)
+ break if image_buffer[-2..-1] == EOI
+ end
+ @image = image_buffer[0..-3]
+ self
+ end
+
+ def image_size
+ @image.bytesize
+ end
+
+ def file_size
+ to_s.bytesize
+ end
+
+ def mp_header_pos
+ app2_pos = to_s =~ Regexp.new(APP2)
+ app2_pos + 8
+ end
+
+ def to_s
+ segments_s = segments.map{|s|
+ if s.respond_to? :to_binary_s
+ s.to_binary_s
+ else
+ s.to_s
+ end
+ }.join
+ [SOI, segments_s, image, EOI].join
+ end
+ end
+end
3  lib/mpo/version.rb
@@ -0,0 +1,3 @@
+module Mpo
+ VERSION = "0.0.1"
+end
19 mpo.gemspec
@@ -0,0 +1,19 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/mpo/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["labocho"]
+ gem.email = ["labocho@penguinlab.jp"]
+ gem.description = %q{TODO: Write a gem description}
+ gem.summary = %q{TODO: Write a gem summary}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "mpo"
+ gem.require_paths = ["lib"]
+ gem.version = Mpo::VERSION
+
+ gem.add_dependency "bindata"
+end
13 spec/mpo_spec.rb
@@ -0,0 +1,13 @@
+require "spec_helper"
+
+describe "Mpo" do
+ let (:left_file_path) { "#{File.dirname(__FILE__)}/resources/l.jpg" }
+ let (:right_file_path) { "#{File.dirname(__FILE__)}/resources/r.jpg" }
+ let (:mpo_file_path) { "#{File.dirname(__FILE__)}/resources/expected.mpo" }
+ it "should create MPO from 2 JPEGS" do
+ created = Mpo.from_files(left_file_path, right_file_path)
+ expected = File.read(mpo_file_path, encoding: "ascii-8bit")
+ created.bytesize.should == expected.bytesize
+ created.should == expected
+ end
+end
BIN  spec/resources/expected.mpo
Binary file not shown
BIN  spec/resources/l.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
BIN  spec/resources/r.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 spec/spec_helper.rb
@@ -0,0 +1,20 @@
+$LOAD_PATH.unshift File.expand_path("#{File.dirname(__FILE__)}/../lib")
+require "mpo"
+
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ # Run specs in random order to surface order dependencies. If you find an
+ # order dependency and want to debug it, you can fix the order by providing
+ # the seed, which is printed after each run.
+ # --seed 1234
+ config.order = 'random'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.