|
|
1.1 ! root 1: #include <stdio.h> ! 2: #include <sys/types.h> ! 3: #include <sys/stat.h> ! 4: #include "defines.h" ! 5: ! 6: #define XTIMES Times ! 7: #define YTIMES Times ! 8: #define Min(a,b) ((a)>(b)?(b):(a)) ! 9: #define Max(a,b) ((a)<(b)?(b):(a)) ! 10: ! 11: int Dither=0, BW=1, Times=6, Reso = 4; ! 12: int laplace = 0; ! 13: ! 14: unsigned char nq[9]; ! 15: char *malloc(); ! 16: unsigned char *rgb; ! 17: ! 18: Usage(str) ! 19: char *str; ! 20: { fprintf(stderr, "qprep: %s\n", str); ! 21: fprintf(stderr, "usage: qprep -dmL [N ...] file\n"); ! 22: fprintf(stderr, "d dither image +- N [2-255]\n"); ! 23: fprintf(stderr, "m enlargement [1- ], defaults to 3(2k) or 6(4k)\n"); ! 24: fprintf(stderr, "L if bw image, apply 1/2 laplace filter\n"); ! 25: exit(1); ! 26: } ! 27: ! 28: main(argc, argv) ! 29: char **argv; ! 30: { ! 31: int i=1, base=2; /* base of option arguments */ ! 32: char c; ! 33: ! 34: if (argc > 1 && argv[1][0] == '-') ! 35: { base++; ! 36: while ((c = argv[1][i++]) != '\0') ! 37: switch (c) { ! 38: /* dither */ case 'd': if (argc >= base) ! 39: { sscanf(argv[base-1], "%d", &Dither); ! 40: base++; ! 41: if (Dither < 0) Dither = -Dither; ! 42: break; ! 43: } else ! 44: Usage("missing argument for `d' flag"); ! 45: ! 46: /* multiply */ case 'm': if (argc >= base) ! 47: { sscanf(argv[base-1], "%d", &Times); ! 48: base++; ! 49: break; ! 50: } else ! 51: Usage("missing argument for `m' flag"); ! 52: /* laplace */ case 'L': laplace = 2; break; ! 53: default : Usage("unknown option"); ! 54: } ! 55: } ! 56: if (base != argc) ! 57: Usage("bad arglist"); ! 58: ! 59: prep(argv[base-1]); ! 60: exit(0); ! 61: } ! 62: ! 63: prep(name) ! 64: char *name; ! 65: { int fd, h, w; ! 66: ! 67: if ((fd = open(name, 0)) == -1) ! 68: { perror(name); ! 69: exit(1); ! 70: } ! 71: h = w = dimension(fd); ! 72: printf("%s: %dx%d\n", name, w, h); ! 73: if (!(rgb = (unsigned char *) malloc(w*h* sizeof(unsigned char)))) ! 74: { fprintf(stderr, "sorry, not enough memory\n"); ! 75: exit(1); ! 76: } ! 77: read(fd, (char *)rgb, w*h); ! 78: close(fd); ! 79: ! 80: if (laplace) ! 81: filter(rgb, h, w); ! 82: if (Dither) ! 83: { prerand(); ! 84: onedither(rgb, h, w, 1); ! 85: } else ! 86: straight(rgb, h, w, 1); ! 87: } ! 88: ! 89: straight(from, h, w, n) ! 90: unsigned char *from; ! 91: { ! 92: register i, j, k; ! 93: register unsigned char *p, *q; ! 94: unsigned char obuf[8192]; ! 95: int chunk = w*XTIMES; ! 96: ! 97: for (i = 0, p = from; i < h; i++) ! 98: { q = obuf; ! 99: for (j = 0; j < w; j++, p += n) ! 100: for (k = 0; k < XTIMES; k++) ! 101: *q++ = *p; ! 102: for (k = 0; k < YTIMES; k++) ! 103: write(1, obuf, chunk); ! 104: } ! 105: } ! 106: ! 107: short Nrand[5000]; ! 108: ! 109: prerand() ! 110: { register int i, D1=Dither, D2=Dither/2; ! 111: ! 112: for (i = 0; i < 5000; i++) ! 113: Nrand[i] = (short) (nrand(D1) - D2); ! 114: } ! 115: ! 116: onedither(from, h, w, n) ! 117: unsigned char *from; ! 118: { ! 119: register int c, m, kk=0; ! 120: register unsigned char *op, *q; ! 121: unsigned char *p, obuf[8192]; ! 122: int i, j, k; ! 123: int chunk = w*XTIMES; ! 124: ! 125: if(w<=0 || XTIMES<=0) abort(); ! 126: for (i = 0, p = from; i < h; i++, p = op) ! 127: for (k = 0; k < YTIMES; k++) ! 128: { q = obuf; ! 129: op = p; ! 130: j = w; ! 131: do{ ! 132: m = XTIMES; ! 133: do{ ! 134: c = *op + Nrand[kk]; ! 135: if (++kk >= 5000) kk = 0; ! 136: if(c<0) ! 137: c=0; ! 138: if(c>255) ! 139: c=255; ! 140: *q++ = c; ! 141: }while(--m); ! 142: op += n; ! 143: }while(--j); ! 144: write(1, obuf, chunk); ! 145: } ! 146: } ! 147: ! 148: dimension(fd) ! 149: { struct stat bam; ! 150: int N; ! 151: extern float fsqrt(); ! 152: ! 153: if (fstat(fd, &bam)==0) ! 154: { N = bam.st_size; ! 155: N = (int) fsqrt((double)N+1.0); ! 156: return N; ! 157: } ! 158: return 0; ! 159: } ! 160: ! 161: filter(old, h, w) ! 162: register unsigned char *old; ! 163: { ! 164: register unsigned char *new; ! 165: register int i, a, x, y; ! 166: ! 167: if (!(new = (unsigned char *) malloc(w*h* sizeof(unsigned char)))) ! 168: { fprintf(stderr, "sorry, not enough memory for filter\n"); ! 169: exit(1); ! 170: } ! 171: for (y = 1; y < h-1; y++) ! 172: for (x = 1; x < w-1; x++) ! 173: { i = y*w+x; ! 174: a = old[i-1]+old[i+1]+old[i-w]+old[i+w]+ ! 175: old[i-w-1]+old[i+w+1]+old[i-w+1]+old[i+w-1]; ! 176: a = 5*old[i] - a/2; ! 177: new[i] = (a > 255)?255:(a < 0)?0:a; ! 178: } ! 179: for (y = 1; y < h-1; y++) ! 180: for (x = 1; x < w-1; x++) ! 181: { i = y*w+x; ! 182: old[i] = new[i]; ! 183: } ! 184: free((char *)new); ! 185: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.