File: jpeg.rb

package info (click to toggle)
ruby-fastimage 2.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 29,008 kB
  • sloc: ruby: 1,442; xml: 7; makefile: 2; sh: 1
file content (52 lines) | stat: -rw-r--r-- 1,431 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module FastImageParsing
  class IOStream < SimpleDelegator # :nodoc:
    include StreamUtil
  end
  
  class Jpeg < ImageBase # :nodoc:
    def dimensions
      exif = nil
      state = nil
      loop do
        state = case state
        when nil
          @stream.skip(2)
          :started
        when :started
          @stream.read_byte == 0xFF ? :sof : :started
        when :sof
          case @stream.read_byte
          when 0xe1 # APP1
            skip_chars = @stream.read_int - 2
            data = @stream.read(skip_chars)
            io = StringIO.new(data)
            if io.read(4) == "Exif"
              io.read(2)
              new_exif = Exif.new(IOStream.new(io)) rescue nil
              exif ||= new_exif # only use the first APP1 segment
            end
            :started
          when 0xe0..0xef
            :skipframe
          when 0xC0..0xC3, 0xC5..0xC7, 0xC9..0xCB, 0xCD..0xCF
            :readsize
          when 0xFF
            :sof
          else
            :skipframe
          end
        when :skipframe
          skip_chars = @stream.read_int - 2
          @stream.skip(skip_chars)
          :started
        when :readsize
          @stream.skip(3)
          height = @stream.read_int
          width = @stream.read_int
          width, height = height, width if exif && exif.rotated?
          return [width, height, exif ? exif.orientation : 1]
        end
      end
    end
  end
end