Module: Origami::Filter::Predictor

Defined in:
lib/origami/filters/predictors.rb

Constant Summary collapse

NONE =
1
TIFF =
2
PNG_NONE =
10
PNG_SUB =
11
PNG_UP =
12
PNG_AVERAGE =
13
PNG_PAETH =
14
PNG_OPTIMUM =
15

Class Method Summary collapse

Class Method Details

.do_png_post_prediction(data, bpp, bpr) ⇒ Object



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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/origami/filters/predictors.rb', line 100

def self.do_png_post_prediction(data, bpp, bpr)
    result = ""
    uprow = "\0" * bpr
    thisrow = "\0" * bpr
    nrows = (data.size + bpr - 1) / bpr

    nrows.times do |irow|
        line = data[irow * bpr, bpr]
        predictor = 10 + line[0].ord
        line[0] = "\0"

        for i in (1..line.size-1)
            up = uprow[i].ord

            if bpp > i
                left = upleft = 0
            else
                left = line[i-bpp].ord
                upleft = uprow[i-bpp].ord
            end

            case predictor
            when PNG_NONE
                thisrow = line
            when PNG_SUB
                thisrow[i] = ((line[i].ord + left) & 0xFF).chr
            when PNG_UP
                thisrow[i] = ((line[i].ord + up) & 0xFF).chr
            when PNG_AVERAGE
                thisrow[i] = ((line[i].ord + ((left + up) / 2)) & 0xFF).chr
            when PNG_PAETH
                p = left + up - upleft
                pa, pb, pc = (p - left).abs, (p - up).abs, (p - upleft).abs

                thisrow[i] = ((line[i].ord +
                    case [ pa, pb, pc ].min
                    when pa then left
                    when pb then up
                    when pc then upleft
                    end
                ) & 0xFF).chr
            else
                unless Origami::OPTIONS[:ignore_png_errors]
                    raise PredictorError.new("Unknown PNG predictor : #{predictor}", input_data: data, decoded_data: result)
                end

                # behave as PNG_NONE
                thisrow = line
            end
        end

        result << thisrow[1..-1]
        uprow = thisrow
    end

    result
end

.do_png_pre_prediction(data, predictor, bpp, bpr) ⇒ Object



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/origami/filters/predictors.rb', line 158

def self.do_png_pre_prediction(data, predictor, bpp, bpr)
    result = ""
    nrows = data.size / bpr

    line = "\0" + data[-bpr, bpr]

    (nrows-1).downto(0) do |irow|
        uprow =
        if irow == 0
            "\0" * (bpr+1)
        else
            "\0" + data[(irow-1)*bpr,bpr]
        end

        bpr.downto(1) do |i|
            up = uprow[i].ord
            left = line[i-bpp].ord
            upleft = uprow[i-bpp].ord

            case predictor
            when PNG_SUB
                line[i] = ((line[i].ord - left) & 0xFF).chr
            when PNG_UP
                line[i] = ((line[i].ord - up) & 0xFF).chr
            when PNG_AVERAGE
                line[i] = ((line[i].ord - ((left + up) / 2)) & 0xFF).chr
            when PNG_PAETH
                p = left + up - upleft
                pa, pb, pc = (p - left).abs, (p - up).abs, (p - upleft).abs

                line[i] = ((line[i].ord -
                    case [ pa, pb, pc ].min
                    when pa then left
                    when pb then up
                    when pc then upleft
                    end
                ) & 0xFF).chr
            when PNG_NONE
            else
                raise PredictorError.new("Unsupported PNG predictor : #{predictor}", input_data: data)
            end
        end

        line[0] = (predictor - 10).chr
        result = line + result

        line = uprow
    end

    result
end

.do_post_prediction(data, predictor: NONE, colors: 1, bpc: 8, columns: 1) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/origami/filters/predictors.rb', line 71

def self.do_post_prediction(data, predictor: NONE, colors: 1, bpc: 8, columns: 1)
    return data if predictor == NONE

    unless (1..4) === colors
        raise PredictorError.new("Colors must be between 1 and 4", input_data: data)
    end

    unless [1,2,4,8,16].include?(bpc)
        raise PredictorError.new("BitsPerComponent must be in 1, 2, 4, 8 or 16", input_data: data)
    end

    # components per line
    nvals = columns * colors

    # bytes per pixel
    bpp = (colors * bpc + 7) >> 3

    # bytes per row
    bpr = ((nvals * bpc + 7) >> 3) + 1

    if predictor == TIFF
        do_tiff_post_prediction(data, colors, bpc, columns)
    elsif predictor >= 10 # PNG
        do_png_post_prediction(data, bpp, bpr)
    else
        raise PredictorError.new("Unknown predictor : #{predictor}", input_data: data)
    end
end

.do_pre_prediction(data, predictor: NONE, colors: 1, bpc: 8, columns: 1) ⇒ Object



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
# File 'lib/origami/filters/predictors.rb', line 38

def self.do_pre_prediction(data, predictor: NONE, colors: 1, bpc: 8, columns: 1)
    return data if predictor == NONE

    unless (1..4) === colors.to_i
        raise PredictorError.new("Colors must be between 1 and 4", input_data: data)
    end

    unless [1,2,4,8,16].include?(bpc.to_i)
        raise PredictorError.new("BitsPerComponent must be in 1, 2, 4, 8 or 16", input_data: data)
    end

    # components per line
    nvals = columns * colors

    # bytes per pixel
    bpp = (colors * bpc + 7) >> 3

    # bytes per row
    bpr = (nvals * bpc + 7) >> 3

    unless data.size % bpr == 0
        raise PredictorError.new("Invalid data size #{data.size}, should be multiple of bpr=#{bpr}", input_data: data)
    end

    if predictor == TIFF
        do_tiff_pre_prediction(data, colors, bpc, columns)
    elsif predictor >= 10 # PNG
        do_png_pre_prediction(data, predictor, bpp, bpr)
    else
        raise PredictorError.new("Unknown predictor : #{predictor}", input_data: data)
    end
end

.do_tiff_post_prediction(data, colors, bpc, columns) ⇒ Object

:nodoc:



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/origami/filters/predictors.rb', line 210

def self.do_tiff_post_prediction(data, colors, bpc, columns) #:nodoc:
    bpr = (colors * bpc * columns + 7) >> 3
    nrows = data.size / bpr
    bitmask = (1 << bpc) - 1
    result = Utils::BitWriter.new

    nrows.times do |irow|
        line = Utils::BitReader.new(data[irow * bpr, bpr])

        pixel = ::Array.new(colors, 0)
        columns.times do
            diffpixel = ::Array.new(colors) { line.read(bpc) }
            pixel = pixel.zip(diffpixel).map!{|c, diff| (c + diff) & bitmask}

            pixel.each do |c|
                result.write(c, bpc)
            end
        end

        result.final
    end

    result.final.to_s
end

.do_tiff_pre_prediction(data, colors, bpc, columns) ⇒ Object

:nodoc:



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/origami/filters/predictors.rb', line 235

def self.do_tiff_pre_prediction(data, colors, bpc, columns) #:nodoc:
    bpr = (colors * bpc * columns + 7) >> 3
    nrows = data.size / bpr
    bitmask = (1 << bpc) - 1
    result = Utils::BitWriter.new

    nrows.times do |irow|
        line = Utils::BitReader.new(data[irow * bpr, bpr])

        diffpixel = ::Array.new(colors, 0)
        columns.times do
            pixel = ::Array.new(colors) { line.read(bpc) }
            diffpixel = diffpixel.zip(pixel).map!{|diff, c| (c - diff) & bitmask}

            diffpixel.each do |c|
                result.write(c, bpc)
            end
        end

        result.final
    end

    result.final.to_s
end