Class: ImageInspector::Image
- Inherits:
-
Object
- Object
- ImageInspector::Image
- Defined in:
- lib/imageinspector.rb
Overview
Parse image header and retrieve its basic properties. The code is inspired by Sam Stephenson’s snippet which demonstrates how to determine a JPEG image size ( see snippets.dzone.com/posts/show/805) and Paul Schreiber’s code for TIFF (see paulschreiber.com/blog/2010/06/10/tiff-file-dimensions-in-ruby/)
Supported formats are: TIFF, PNG, JPEG and JPEG2000.
Constant Summary collapse
- @@gc =
(IO.method_defined? :getbyte) ? (:getbyte) : (:getc)
Instance Attribute Summary collapse
-
#compression ⇒ Object
readonly
Image format and compression method.
-
#cspace ⇒ Object
readonly
Image depth, color space, palette (for indexed images) and transparency data (for PNG).
-
#depth ⇒ Object
readonly
Image depth, color space, palette (for indexed images) and transparency data (for PNG).
-
#format ⇒ Object
readonly
Image format and compression method.
-
#height ⇒ Object
readonly
Return the obvious.
-
#palette ⇒ Object
readonly
Image depth, color space, palette (for indexed images) and transparency data (for PNG).
-
#tags ⇒ Object
readonly
Return TIFF tags as a hash for TIFF images or JPEG images with EXIF data.
-
#trans ⇒ Object
readonly
Image depth, color space, palette (for indexed images) and transparency data (for PNG).
-
#width ⇒ Object
readonly
Return the obvious.
-
#x_dpi ⇒ Object
readonly
Return image resolution (always in pixels per inch, even if it is differently specified in the source image).
-
#y_dpi ⇒ Object
readonly
Return image resolution (always in pixels per inch, even if it is differently specified in the source image).
Instance Method Summary collapse
-
#getRawData ⇒ Object
Return image data (possibly compressed) for a previously initialized image as a sring.
-
#initialize(input = nil) ⇒ Image
constructor
Set all image attributes to nil and open an image if an optional argument is specified.
- #nextImage ⇒ Object
-
#open(input) ⇒ Object
Accepts either a file name or a stream-like object.
Constructor Details
#initialize(input = nil) ⇒ Image
Set all image attributes to nil and open an image if an optional argument is specified.
77 78 79 80 |
# File 'lib/imageinspector.rb', line 77 def initialize( input=nil ) clearData() open( input ) unless input.nil? end |
Instance Attribute Details
#compression ⇒ Object (readonly)
Image format and compression method
68 69 70 |
# File 'lib/imageinspector.rb', line 68 def compression @compression end |
#cspace ⇒ Object (readonly)
Image depth, color space, palette (for indexed images) and transparency data (for PNG)
66 67 68 |
# File 'lib/imageinspector.rb', line 66 def cspace @cspace end |
#depth ⇒ Object (readonly)
Image depth, color space, palette (for indexed images) and transparency data (for PNG)
66 67 68 |
# File 'lib/imageinspector.rb', line 66 def depth @depth end |
#format ⇒ Object (readonly)
Image format and compression method
68 69 70 |
# File 'lib/imageinspector.rb', line 68 def format @format end |
#height ⇒ Object (readonly)
Return the obvious.
61 62 63 |
# File 'lib/imageinspector.rb', line 61 def height @height end |
#palette ⇒ Object (readonly)
Image depth, color space, palette (for indexed images) and transparency data (for PNG)
66 67 68 |
# File 'lib/imageinspector.rb', line 66 def palette @palette end |
#tags ⇒ Object (readonly)
Return TIFF tags as a hash for TIFF images or JPEG images with EXIF data. Otherwise this property is nil.
71 72 73 |
# File 'lib/imageinspector.rb', line 71 def @tags end |
#trans ⇒ Object (readonly)
Image depth, color space, palette (for indexed images) and transparency data (for PNG)
66 67 68 |
# File 'lib/imageinspector.rb', line 66 def trans @trans end |
#width ⇒ Object (readonly)
Return the obvious.
61 62 63 |
# File 'lib/imageinspector.rb', line 61 def width @width end |
#x_dpi ⇒ Object (readonly)
Return image resolution (always in pixels per inch, even if it is differently specified in the source image).
64 65 66 |
# File 'lib/imageinspector.rb', line 64 def x_dpi @x_dpi end |
#y_dpi ⇒ Object (readonly)
Return image resolution (always in pixels per inch, even if it is differently specified in the source image).
64 65 66 |
# File 'lib/imageinspector.rb', line 64 def y_dpi @y_dpi end |
Instance Method Details
#getRawData ⇒ Object
Return image data (possibly compressed) for a previously initialized image as a sring. For JPEG and JPEG2000 this would be the whole image as it is stored on the disk, while for TIFF and PNG all headers are stripped and a raw data stream is returned.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/imageinspector.rb', line 106 def getRawData() raise "The image has not been properly initialized" if @width.nil? or @input.nil? begin if @input.kind_of? IO or @input.kind_of? StringIO ret = concatDataBlocks( @input ) else File.open( @input, 'rb' ) { |io| ret = concatDataBlocks( io ) } end return ret rescue Exception => e $stderr.puts( "Could not read data from #{@fname}: " << e. ) end end |
#nextImage ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/imageinspector.rb', line 121 def nextImage() if @format.eql? :TIFF and @next_off > 0 begin if @input.kind_of? IO or @input.kind_of? StringIO tiffNext( @input ) else File.open( @input, 'rb' ) { |io| tiffNext( io ) } end return true rescue Exception => e $stderr.puts( "Could not read data from #{@fname}: " << e. ) end end false end |
#open(input) ⇒ Object
Accepts either a file name or a stream-like object.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/imageinspector.rb', line 83 def open( input ) @input = input begin if input.kind_of? IO or input.kind_of? StringIO @fname = '<STREAM>' byFormat( input ) else @fname = input File.open( input, 'rb' ) { |io| byFormat( io ) } end rescue Exception => e $stderr.puts( "Could not read data from #{@fname}: " << e. ) clearData() @input = nil end end |