ebc68b51928b665a89e3b9977823c51a47aca5a8
[detafelator.git] / detafelator.c
1 #include <jh.h>
2 #include <assert.h>
3 #include <stdbool.h>
4
5 int avg(int a, int b) { return (a+b)/2; }
6
7 int nuldif(int n) { return abs(128-n); }
8
9 int blimround(int a) { return (a<0)?0:((a>255)?255:a); }
10
11 unsigned char *pxptr(pnm_image *img, int x, int y) {
12   return img->data + 3*(x + y*img->width);
13 }
14
15 #define TRESH 4
16
17 int main(int argc, char **argv) {
18   pnm_image *iimg = CHK_PTR(read_image("/dev/stdin"), "can't read image from stdin", 1);
19
20   int rowsize = 3*iimg->width;
21
22   for (int y=0; y<iimg->height-1; y++) {
23     unsigned char *irow = iimg->data + rowsize*y;
24     //unsigned char *orow = oimg->data + rowsize*y;
25     for (int x=0; x<iimg->width-1; x++) {
26       unsigned char *ipx = irow + 3*x;
27       for (int i=0; i<3; i++) {
28         unsigned char *ipxc = ipx+i;
29         int xdiff = ipxc[3]-ipxc[0];
30         int ydiff = ipxc[rowsize]-ipxc[0];
31         ipxc[0] = blimround(128+xdiff/*+ydiff*/);
32       }
33     }
34   }
35
36   pnm_image *oimg = CHK_PTR(dup_image(iimg), "can't dup image", 1);
37
38   for (int x=5; x<iimg->width; x++) {
39     for (int y=/*1*/0; y<iimg->height; y++) {
40       bool notresh = true;
41       if (nuldif(*pxptr(iimg,x,y))>TRESH) notresh=false;
42       for (int x_=x-5; x_<x; x_++) if (nuldif(*pxptr(iimg,x_,y))>TRESH) notresh=false;
43       //for (int y_=y-10; y_<y; y_++) if (nuldif(*pxptr(iimg,x,y_))>TRESH) notresh=false;
44       if (notresh) {
45         pxptr(oimg,x,y)[0]=255;
46       } else {
47         pxptr(oimg,x,y)[0]=blimround(
48           /*avg(*/pxptr(oimg,x-1,y)[0]/*,pxptr(oimg,x,y-1)[0])*/ - (pxptr(iimg, x, y)[0]-128)
49         );
50       }
51     }
52   }
53
54   if (write_image(oimg, "/dev/stdout")) xperror("unable to write image to stdout", 1);
55   return 0;
56 }