format icon indicating copy to clipboard operation
format copied to clipboard

"Invalid filter" error is thrown when loading a PNG image.

Open Johanny opened this issue 11 years ago • 7 comments

As we discussed in the mailing list, an "Invalid Filter" error is thrown when loading the PNG at the bottom of this post. The error is thrown by this piece of code :

https://github.com/HaxeFoundation/format/blob/master/format/png/Tools.hx

inline function decodeLine(y, f, getValue) {
    switch( f ) {
    case 0:
        for( x in 0...width ) {
            decode(getValue);
            bgra.set(w++,vb);
            bgra.set(w++,vg);
            bgra.set(w++,vr);
            bgra.set(w++,va);
        }
    case 1:
        var cr = 0, cg = 0, cb = 0, ca = 0;
        for( x in 0...width ) {
            decode(getValue);
            cb += vb;   bgra.set(w++,cb);
            cg += vg;   bgra.set(w++,cg);
            cr += vr;   bgra.set(w++,cr);
            ca += va;   bgra.set(w++,ca);
            bgra.set(w++, va);
        }
    case 2:
        var stride = y == 0 ? 0 : width * 4;
        for( x in 0...width ) {
            decode(getValue);
            bgra.set(w, vb + bgra.get(w - stride)); w++;
            bgra.set(w, vg + bgra.get(w - stride)); w++;
            bgra.set(w, vr + bgra.get(w - stride)); w++;
            bgra.set(w, va + bgra.get(w - stride)); w++;
        }
    case 3:
        var cr = 0, cg = 0, cb = 0, ca = 0;
        var stride = y == 0 ? 0 : width * 4;
        for( x in 0...width ) {
            decode(getValue);
            cb = (vb + ((cb + bgra.get(w - stride)) >> 1)) & 0xFF;  bgra.set(w++, cb);
            cg = (vg + ((cg + bgra.get(w - stride)) >> 1)) & 0xFF;  bgra.set(w++, cg);
            cr = (vr + ((cr + bgra.get(w - stride)) >> 1)) & 0xFF;  bgra.set(w++, cr);
            cr = (va + ((ca + bgra.get(w - stride)) >> 1)) & 0xFF;  bgra.set(w++, ca);
        }
    case 4:
        var stride = width * 4;
        var cr = 0, cg = 0, cb = 0, ca = 0;
        for( x in 0...width ) {
            decode(getValue);
            cb = (filter(bgra, x, y, stride, cb, w) + vb) & 0xFF; bgra.set(w++, cb);
            cg = (filter(bgra, x, y, stride, cg, w) + vg) & 0xFF; bgra.set(w++, cg);
            cr = (filter(bgra, x, y, stride, cr, w) + vr) & 0xFF; bgra.set(w++, cr);
            ca = (filter(bgra, x, y, stride, ca, w) + va) & 0xFF; bgra.set(w++, ca);
        }
    default:
        throw "Invalid filter "+f;
    }
}

The error occur when loading the PNG with Heaps.

ship

Johanny avatar Oct 13 '14 17:10 Johanny

Having the same error here:

faulty

I've looked at the output of the Inflate.run and it doesn't look anything like what the function expects @Johanny could you give a link to the mailing list discussion, please? Did you figure what the problem was? (I'd not mind fixing it, but if I could skip the investigation, it would still help).

OK, found it, not much info here though :( https://groups.google.com/forum/#!searchin/haxelang/invalid$20filter/haxelang/waVJZdAcRtw/IC4tNSBKyzYJ

wvxvw avatar Jan 21 '15 10:01 wvxvw

What's the id of the filter that is said to be invalid? By looking at https://github.com/HaxeFoundation/format/blob/master/format/png/Tools.hx#L153 , it seems that it implements all required filters by the spec ( http://www.w3.org/TR/PNG-Filters.html )

waneck avatar Jan 21 '15 11:01 waneck

@waneck It's not the id of the filter, the whole image payload seems to be garbled. When I trace the values I get from the data (f in the code) they range from 0 to 255, certainly not what they should be doing.

Here's the precise output:

0 56 221 19 56 221 19 56 221 19 56 221 19 56 221 19 56 221 19 56 221
19 56 0 58 241 16 58 227 15 55 231 15 56 233 15 56 240 16 58 241 16 58
241 16 58 0 58 241 16 58 241 16 58 241 16 58 241 16 58 241 16 58 241
16 58 241 16 58 0 58 241 16 58 241 16 58 241 16 58 241 16 58 241 16 58
241 16 58 241 16 58 0 58 241 16 58 241 16 58 241 16 58 241 16 58 241
16 58 241 16 58 241 16 58 0 56 221 19 56 221 19 56 221 19 56 221 19 56
221 19 56 221 19 56 221 19 56 0 58 241 16 58 241 16 58 241 16 58 241
16 58 241 16 58 241 16 58 241 16 58 0 58 241 16 58 241 16 58 241 16 58
241 16 58 241 16 58 241 16 58 241 16 58 0 56 0 16 58 241 16 58 246 192
202 255 255 255 250 220 226 237 16 57 230 72 101 255 255 255 237 16 57
255 255 255 241 16 58 241 16 58 249 212 219 239 16 58 251 229 233 246
182 194 240 16 58 244 168 182 241 16 58 246 182 194 237 16 57 239 16
57 255 255 255 250 220 226 255 255 255 255 255 255 238 16 57 233 72
102 241 16 58 241 16 58 0 16 58 241 16 58 250 220 226 237 97 123 248
192 202 241 16 58 241 119 141 241 16 58 237 16 57 239 16 58 238 16 57
241 16 58 244 119 142 241 16 58 235 16 56 246 182 194 255 255 255 238
16 57 239 16 57 237 16 57 239 16 58 239 16 58 244 137 157 239 16 58
241 119 141 237 16 57 241 16 58 241 16 58 241 16 58 241 16 58 0 16 58
241 16 58 235 44 80 255 255 255 244 119 142 237 16 57 239 16 57 237 16
57 236 16 57 242 119 142 255 255 255 241 16 58 227 15 55 239 16 58 250
220 226 239 16 58 245 182 194 236 16 57 241 16 58 241 16 58 241 16 58
241 16 58 241 16 58 241 16 58 241 16 58 241 16 58 241 16 58 241 16 58
241 16 58 241 16 58 0 16 58 241 16 58 244 98 125 254 246 247 255 255
255 239 16 58 253 229 233 255 255 255 255 255 255 248 154 172 241 16
58 254 246 247 251 203 212 241 16 58 241 16 58 241 16 58 241 16 58 241
16 58 241 16 58

wvxvw avatar Jan 21 '15 11:01 wvxvw

Okay, it seems that the problem is that the png reader code is expecting that a new filter value is defined for each row. It seems that the whole problem is with this. If you want to investigate, you can maybe read the png docs about filtering and scanline width, or maybe add some traces on another png reader implementation that reads it correctly. For reference, I think the culprit is here https://github.com/HaxeFoundation/format/blob/master/format/png/Tools.hx#L392 (as in what defines that scanline width)

waneck avatar Jan 21 '15 12:01 waneck

I haven't investigated too much, but it seems to be related with interlaced images. See https://code.google.com/p/javapng/source/browse/trunk/javapng2/src/main/com/sixlegs/png/ImageFactory.java#140

waneck avatar Jan 21 '15 12:01 waneck

It's not grayscale, the color mode is identified correctly: ColTrue. I will look into Java code some time later.

wvxvw avatar Jan 21 '15 13:01 wvxvw

Ah yeah, I mentioned the wrong line. I meant the same part but on color :)

waneck avatar Jan 21 '15 14:01 waneck