9 #define OC(x,xi) ((x)*factor+(xi))
10 #define BM(x,xi,y,yi) bm[OC(y,yi)*w+OC(x,xi)]
12 #define COMP(r,g,b) ((r) | ((g)<<8) | ((b) << 16))
14 #define R(comp) ((comp) & 0xff)
15 #define G(comp) (((comp)>>8) & 0xff)
16 #define B(comp) (((comp)>>16) & 0xff)
18 #define CHOICEFUNC(np1, np2) ( random() & 1 )
33 int inpixels
, outpixels
;
36 setvbuf(stdout
, 0, _IOFBF
, 1024*128);
38 fprintf(stderr
, "Usage: tomono factor\n");
41 factor
= atoi(argv
[1]);
43 if ( sscanf(lbuf
, "(%d,%d)", &w
, &h
) != 2) {
44 fprintf(stderr
, "%s: bad size spec: %s\n", argv
[0], lbuf
);
49 printf("(%d,%d)\n", nw
, nh
);
50 if ( (bm
= (long *)malloc(h
*w
*sizeof(long))) == 0) {
51 fprintf(stderr
, "%s: No memory\n", argv
[0]);
54 if ( (nbm
= (long *)malloc(nh
*nw
*sizeof(long))) == 0) {
55 fprintf(stderr
, "%s: No memory\n", argv
[0]);
58 while( !feof(stdin
) ) {
60 if ( feof(stdin
) ) break;
62 fprintf(stderr
, "Reading %d\n", h
*w
*sizeof(long));
63 if ( (i
=fread(bm
, 1, h
*w
*sizeof(long), stdin
)) != h
*w
*sizeof(long)) {
64 fprintf(stderr
, "%s: short read, %d wanted %d\n", argv
[0],
69 ** Compute picture blackness.
72 inpixels
= countpixels(0,0,w
,h
);
73 greyness
= (double)inpixels
/(h
*w
*inlevels
);
74 fprintf(stderr
, "%3.1f%% grey\n", 100.0*greyness
);
75 outpixels
= (int)(greyness
*outlevels
*nh
*nw
);
76 fprintf(stderr
, "Inpixels: %d (%d) Outpixels %d\n", inpixels
, inpixels
/inlevels
, outpixels
);
77 resid
= fillpixels(0,0,nw
,nh
,0,0,w
,h
,outpixels
);
78 if ( resid
> 1 ) fprintf(stderr
, "Residue: %d pixels\n", resid
);
79 fprintf(stderr
, "Writing %d\n", (nh
*nw
)*sizeof(long));
80 fwrite(nbm
, 1, (nh
*nw
)*sizeof(long), stdout
);
85 countpixels(x0
,y0
,x1
,y1
)
91 for(x
=x0
; x
<x1
; x
++) {
100 fillpixels(x0
,y0
,x1
,y1
,ox0
,oy0
,ox1
,oy1
,npixels
)
102 int m
, om
, p1
, p2
, np1
, np2
, rp
, resid
;
104 if ( npixels
== 0 ) return 0;
105 if ( x0
+1 >= x1
&& y0
+1 >= y1
) {
107 nbm
[y0
*nw
+x0
] = 0xffffff;
108 /* fprintf(stderr, "->%d,%d\n", x0,y0); */
113 if ( x1
-x0
< y1
-y0
) {
117 m
= y0
+1+(random()%(y1
-y0
-1));
118 /* fprintf(stderr,"%d,%d %d,%d Y %d\n", x0, x1, y0, y1, m); */
119 /* om = (oy0+oy1)/2; */ om
= m
;
120 p1
= countpixels(ox0
,oy0
,ox1
,om
);
121 p2
= countpixels(ox0
,om
,ox1
,oy1
);
122 np1
= (int)(((float)p1
/(p1
+p2
))*npixels
);
123 np2
= (int)(((float)p2
/(p1
+p2
))*npixels
);
124 rp
= npixels
- np1
- np2
;
131 if ( CHOICEFUNC(np1
, np2
) ) {
132 resid
= fillpixels(x0
,y0
,x1
,m
,ox0
,oy0
,ox1
,om
,np1
+resid
);
133 resid
= fillpixels(x0
,m
,x1
,y1
,ox0
,om
,ox1
,oy1
,np2
+resid
);
135 resid
= fillpixels(x0
,m
,x1
,y1
,ox0
,om
,ox1
,oy1
,np2
+resid
);
136 resid
= fillpixels(x0
,y0
,x1
,m
,ox0
,oy0
,ox1
,om
,np1
+resid
);
142 m
= x0
+1+(random()%(x1
-x0
-1));
143 /* fprintf(stderr,"%d,%d %d,%d X %d\n", x0, x1, y0, y1, m); */
144 /* om = (ox0+ox1)/2; */ om
= m
;
145 p1
= countpixels(ox0
,oy0
,om
,oy1
);
146 p2
= countpixels(om
,oy0
,ox1
,oy1
);
147 np1
= (int)(((float)p1
/(p1
+p2
))*npixels
);
148 np2
= (int)(((float)p2
/(p1
+p2
))*npixels
);
149 rp
= npixels
- np1
- np2
;
156 if ( CHOICEFUNC(np1
, np2
) ) {
157 resid
= fillpixels(x0
,y0
,m
,y1
,ox0
,oy0
,om
,oy1
,np1
+resid
);
158 resid
= fillpixels(m
,y0
,x1
,y1
,om
,oy0
,ox1
,oy1
,np2
+resid
);
160 resid
= fillpixels(m
,y0
,x1
,y1
,om
,oy0
,ox1
,oy1
,np2
+resid
);
161 resid
= fillpixels(x0
,y0
,m
,y1
,ox0
,oy0
,om
,oy1
,np1
+resid
);