+
+ int bytes_per_pixel = bpp>>3;
+ int w = width*bytes_per_pixel;
+ int back_x = bytes_per_pixel;
+ int back_y = y?width*bytes_per_pixel:0;
+
+ unsigned char*pairs[5];
+ pairs[0] = calloc(1, 8192);
+ pairs[1] = calloc(1, 8192);
+ pairs[2] = calloc(1, 8192);
+ pairs[3] = calloc(1, 8192);
+ pairs[4] = calloc(1, 8192);
+
+ unsigned char old[5];
+ int l = bytes_per_pixel - 1;
+ old[0] = src[l];
+ old[1] = src[l];
+ old[2] = src[l] - src[l-back_y];
+ old[3] = src[l] - src[l-back_y];
+ old[4] = src[l] - PaethPredictor(0, src[l-back_y], 0);
+
+ int different_pairs[5] = {0,0,0,0,0};
+
+ int x;
+ for(x=bytes_per_pixel;x<w;x++) {
+ unsigned char dest[5];
+ dest[0] = src[x];
+ dest[1] = src[x] - src[x-back_x];
+ dest[2] = src[x] - src[x-back_y];
+ dest[3] = src[x] - (src[x-back_x] + src[x-back_y])/2;
+ dest[4] = src[x] - PaethPredictor(src[x-back_x], src[x-back_y], src[x-back_x-back_y]);
+
+ int i;
+ for(i=0;i<5;i++) {
+ int v = dest[i]<<8|old[i];
+ int p = v>>3;
+ int b = 1<<(v&7);
+ if(!pairs[i][p]&b) {
+ pairs[i][p]|=b;
+ different_pairs[i]++;
+ }
+ }
+ memcpy(old, dest, sizeof(old));
+ }