Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/awk -f
- BEGIN {
- print "Start Input" | "cat >&2"
- x = 0;
- y = 1;
- p = 0;
- printf "/X+X--/!!!!!!!!//$"
- }
- function abs(x) {
- return ( x < 0) ? -x : x;
- }
- function reduceRGB( i) {
- rgb[1] = and( rgb[1], 0xE0 ); #11100000
- rgb[2] = and( rgb[2], 0xE0 ); #11100000
- if ( rgb[3] > 127 ) { rgb[3] = 127; }
- rgb[3] = and( rgb[3], 0xE0 ); #11100000
- return lshift(rgb[1],1) + rshift(rgb[2],2) + rshift(rgb[3],5);
- #R 11100000 |
- #G 111000|00
- #B 111|00000
- #C 111111111| <- 9 bit Farbinfo
- }
- function getColor(color) {
- rgb[1] = rshift(and(color, 0x1C0),1);
- rgb[2] = lshift(and(color, 0x038),2);
- rgb[3] = lshift(and(color, 0x007),5);
- }
- /WH/ {
- w=$2; h=$3;
- }
- !/WH/ {
- for ( i=1; i<=NF; ++i ) {
- rgb[p%3 + 1] = $i;
- if ( (++p)%3 == 0 ) {
- ++x;
- if ( x % 10 == 0 ) {
- printf("%c[2K Y: %7.3f %% X: %7.3f %%\r", 27, 100.0*(y-1)/h, 100.0*(x-1)/w) | "cat >&2";
- }
- if ( x > w ) {
- ++y; x = 1
- }
- # ++histogramm[1,rgb[1]+1]; ++histogramm[2,rgb[2]+1]; ++histogramm[3,rgb[3]+1];
- data[x,y] = reduceRGB();
- colorExists[data[x,y]+1]=1;
- }
- }
- }
- function setDir(dest) {
- if ( dir == dest ) return;
- if ( (dest - dir == 1) || (dir == 4 && dest == 1) ) {
- printf("+");
- } else
- if ( (dest - dir == -1) || (dir == 1 && dest == 4) ) {
- printf("-");
- } else
- if ( abs(dest - dir) == 2 ) {
- printf("++");
- } else {
- return;
- }
- dir = dest;
- }
- function moveTo(x,y ,i,dX,dY) {
- dX = x - penX;
- dY = y - penY;
- if ( dY > 0 ) { setDir(4); }
- else if ( dY < 0 ) { setDir(2); dY = -dY; }
- if ( dY < 8 ) {
- for ( i=1; i<=dY; ++i ) {
- printf("X");
- }
- } else {
- undo="";
- while ( dY != 0 ) {
- if ( and(dY,1) ) {
- printf("X");
- }
- dY = rshift(dY,1);
- if (dY) {
- printf("*");
- undo = undo "/";
- }
- }
- printf("%s",undo);
- }
- if ( dX > 0 ) { setDir(1); }
- else if ( dX < 0 ) { setDir(3); dX = -dX; }
- if ( dX < 8 ) {
- for ( i=1; i<=dX; ++i ) {
- printf("X");
- }
- } else {
- undo="";
- while ( dX != 0 ) {
- if ( and(dX,1) ) {
- printf("X");
- }
- dX = rshift(dX,1);
- if (dX) {
- printf("*");
- undo = undo "/";
- }
- }
- printf("%s",undo);
- }
- penX = x;
- penY = y;
- #setDir((y%2==0)?3:1);
- #setDir(1);
- }
- END {
- printf("%c[2K\r", 27) | "cat >&2";
- print "Start Output ", w, h | "cat >&2";
- # print "R", "G", "B" > "histogramm"
- # for ( i = 1; i<=256; ++i ) {
- # if (!histogramm[1,i]) histogramm[1,i]=0;
- # if (!histogramm[2,i]) histogramm[2,i]=0;
- # if (!histogramm[3,i]) histogramm[3,i]=0;
- # print histogramm[1,i], histogramm[2,i], histogramm[3,i] > "histogramm"
- # }
- print "";
- for ( c = 1; c <= 512; ++c ) {
- if ( colorExists[c] ) {
- getColor(c-1);
- printf("#%02x%02x%02x[", rgb[1], rgb[2], rgb[3]);
- penX = 1;
- penY = 1;
- dir = 1;
- for ( y=1; y<=h; ++y) {
- for ( x=(y%2==0)?w:1; (y%2==0)?x>=1:x<=w; (y%2==0)?--x:++x) {
- if ( x % 30 == 0 ) {
- printf("%c[2K C: %7.3f %% Y: %7.3f %% X: %7.3f %%\r", 27, 100.0*(c-1)/512, 100.0*(y-1)/h, 100.0*((y%2==0)?w-x:x-1)/w) | "cat >&2";
- }
- # For each pixel
- if ( data[x,y] == c-1 ) {
- if ( penX != x || penY != y ) {
- moveTo(x,y);
- }
- printf("F");
- if ( dir == 1 ) { ++penX; }
- else if ( dir == 2 ) { --penY; }
- else if ( dir == 3 ) { --penX; }
- else if ( dir == 4 ) { ++penY; }
- }
- }
- }
- print "]";
- }
- }
- print "";
- printf("%c[2KAll done.\n", 27) | "cat >&2";
- }
Advertisement
Add Comment
Please, Sign In to add comment