Class: Rixmap::Format::PNG::AdaptiveFilter

Inherits:
Object
  • Object
show all
Defined in:
lib/rixmap/format/png/imageio.rb

Overview

スキャンライン用フィルタ実装.

FIXME これ超重い気がする

Instance Method Summary collapse

Constructor Details

#initialize(mode) ⇒ AdaptiveFilter

デフォルトフィルタ実装を初期化します.

Parameters:



18
19
20
21
22
# File 'lib/rixmap/format/png/imageio.rb', line 18

def initialize(mode)
  @prev = Rixmap::Binary.new()
  @mode = mode
  @size = mode.channels.size
end

Instance Method Details

#filt(line, method = ADAPTIVEFILTER_NONE) ⇒ Array

フィルタ関数

Parameters:

  • line (Array)

    対象スキャンライン

  • method (Integer) (defaults to: ADAPTIVEFILTER_NONE)

    フィルタメソッド

Returns:

  • (Array)

    フィルタ処理後スキャンライン



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/rixmap/format/png/imageio.rb', line 29

def filt(line, method = ADAPTIVEFILTER_NONE)
  base_line = Rixmap::Binary.new(line)
  filt_line = Rixmap::Binary.new()
  case method
  when ADAPTIVEFILTER_NONE
    filt_line += base_line
  when ADAPTIVEFILTER_SUB
    base_line.each_with_index do |byte, i|
      prev_idx  = i - @size
      prev_byte = if prev_idx < 0
                    0
                  else
                    base_line[prev_idx]
                  end
      filt_line[i] = byte - prev_byte
    end
  when ADAPTIVEFILTER_UP
    base_line.each_with_index do |byte, i|
      filt_line[i] = byte - @prev[i].to_i
    end
  when ADAPTIVEFILTER_AVERAGE
    base_line.each_with_index do |byte, i|
      before_idx  = i - @size
      before_byte = if before_idx < 0
                      0
                    else
                      base_line[before_idx]
                    end
      prev_byte = @prev[i].to_i
      filt_line[i] = byte - ((before_byte + prev_byte) / 2.0).floor
    end
  when ADAPTIVEFILTER_PEATH
    base_line.each_with_index do |byte, i|
      before_idx  = i - @size
      byte_a, byte_c = if before_idx < 0
                         [0, 0]
                       else
                         [base_line[before_idx], @prev[before_idx].to_i]
                       end
      byte_b = @prev[i].to_i
      filt_line[i] = byte - self.peath_predicator(byte_a, byte_b, byte_c)
    end
  else
    raise NotImplementedError.new("Unsupported Filter Method: #{method}")
  end

  @prev = base_line
  filt_line.unshift(method)
  return filt_line
end

#recon(line) ⇒ Object

復元関数

Parameters:

  • line (Array)

    対象スキャンライン



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/rixmap/format/png/imageio.rb', line 83

def recon(line)
  base_line  = Rixmap::Binary.new(line)
  recon_line = Rixmap::Binary.new
  method = base_line.shift

  case method
  when ADAPTIVEFILTER_NONE
    recon_line += base_line
  when ADAPTIVEFILTER_SUB
    base_line.each_with_index do |byte, i|
      prev_idx  = i - @size
      prev_byte = if prev_idx < 0
                    0
                  else
                    recon_line[prev_idx]
                  end
      recon_line[i] = byte + prev_byte
    end
  when ADAPTIVEFILTER_UP
    base_line.each_with_index do |byte, i|
      recon_line[i] = byte + @prev[i].to_i
    end
  when ADAPTIVEFILTER_AVERAGE
    base_line.each_with_index do |byte, i|
      before_idx  = i - @size
      before_byte = if before_idx < 0
                      0
                    else
                      recon_line[before_idx]
                    end
      prev_byte = @prev[i].to_i
      recon_line[i] = byte + ((before_byte + prev_byte) / 2.0).floor
    end
  when ADAPTIVEFILTER_PEATH
    base_line.each_with_index do |byte, i|
      before_idx  = i - @size
      byte_a, byte_c = if before_idx < 0
                         [0, 0]
                       else
                         [recon_line[before_idx], @prev[before_idx].to_i]
                       end
      byte_b = @prev[i].to_i
      recon_line[i] = byte + self.peath_predicator(byte_a, byte_b, byte_c)
    end
  else
    raise NotImplementedError.new("Unsupported Filter Method: #{method}")
  end

  @prev = recon_line
  return recon_line
end