home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma01.dms / ma01.adf / wasp / misc / wasper.c < prev   
C/C++ Source or Header  |  1991-11-14  |  4KB  |  210 lines

  1. /* wasper - copyright 1990, 1991 by Steven Reiz
  2.  * compute the difference between two SRGR files of same size
  3.  * see formats/srgr.doc for a description of the SRGR format
  4.  * 30/12/90 - 3/1/91, 30/6/91
  5.  */
  6.  
  7. #include <fcntl.h>
  8. extern void *malloc();
  9.  
  10. int fd[3];
  11. long xsz[2], ysz[2];
  12. int er_pic=0;
  13.  
  14.  
  15. main(argc, argv)
  16. int argc;
  17. char **argv;
  18. {
  19.     int i;
  20.     long t;
  21.  
  22.     if (argc<3 || argc>4) {
  23.         printf("usage: wasper srgr-file-1 srgr-file-2 [error-picture-file]\n");
  24.         exit(1);
  25.     }
  26.     if (argc>3)
  27.     er_pic=1;
  28.     for (i=1; i<=2; ++i) {
  29.         if ((fd[i-1]=open(argv[i], O_RDONLY))<0) {
  30.             perror(argv[i]);
  31.             exit(1);
  32.         }
  33.     }
  34.     if (er_pic) {
  35.     if ((fd[2]=open(argv[3], O_WRONLY|O_CREAT|O_TRUNC))<0) {
  36.         perror(argv[3]);
  37.         exit(1);
  38.     }
  39.     }
  40.     for (i=0; i<=1; ++i) {
  41.         cread(fd[i], &t, 4);
  42.         if (t!=(('S'<<24)|('R'<<16)|('G'<<8)|'R')) {
  43.             printf("%s is no SRGR file!\n", argv[i+1]);
  44.             exit(1);
  45.         }
  46.         cread(fd[i], &xsz[i], 4);
  47.         cread(fd[i], &ysz[i], 4);
  48.         printf("%s: SRGR %ldx%ld\n", argv[i+1], xsz[i], ysz[i]);
  49.     }
  50.     if (xsz[0]!=xsz[1] || ysz[0]!=ysz[1]) {
  51.         printf("the two input pictures should have exactly the same size!\n");
  52.         exit(1);
  53.     }
  54.     if (er_pic) {
  55.     t=('S'<<24)|('R'<<16)|('G'<<8)|'R';
  56.     cwrite(fd[2], &t, 4);
  57.     cwrite(fd[2], &xsz[0], 4);
  58.     cwrite(fd[2], &ysz[0], 4);
  59.     }
  60.     compute_errs();
  61.     exit(0);
  62. }
  63.  
  64.  
  65. #define BUFSZ 32760
  66. long sqr[16];
  67. long error, error2;
  68. unsigned char *buf1, *buf2, *bufo;
  69. int thischunk;
  70.  
  71. compute_errs()
  72. {
  73.     long todo;
  74.     short i;
  75.  
  76.     for (i=0; i<16; ++i)
  77.         sqr[i]=i*i;
  78.     if (!(buf1=malloc(BUFSZ))) {
  79.         printf("not enough memory\n");
  80.         exit(1);
  81.     }
  82.     if (!(buf2=malloc(BUFSZ))) {
  83.         printf("not enough memory\n");
  84.         exit(1);
  85.     }
  86.     if (er_pic && !(bufo=malloc(BUFSZ))) {
  87.     printf("not enough memory\n");
  88.     exit(1);
  89.     }
  90.     error=0;
  91.     error2=0;
  92.     todo=xsz[0]*ysz[0]*2;
  93.     while (todo>0) {
  94.         thischunk=BUFSZ;
  95.         if (thischunk>todo)
  96.             thischunk=todo;
  97.         todo-=thischunk;
  98.         cread(fd[0], buf1, thischunk);
  99.         cread(fd[1], buf2, thischunk);
  100.     if (er_pic) {
  101.         do_er_pic();
  102.         cwrite(fd[2], bufo, thischunk);
  103.     } else
  104.         do_er();
  105.         
  106.     }
  107.     printf("error: %ld, error2: %ld\n", error, error2);
  108. }
  109.  
  110.  
  111. do_er()
  112. {
  113.     unsigned char *p, *q;
  114.     long e, e2, n, d;
  115.     unsigned long c1, c2;
  116.     
  117.     p=buf1;
  118.     q=buf2;
  119.     e=e2=0;
  120.     n=thischunk-1;
  121.         do {
  122.             c1= *p++;
  123.             c2= *q++;
  124.             if ((d=(c1&0x0f)-(c2&0x0f))<0)
  125.                 d= -d;
  126.             e+=d;
  127.             e2+=sqr[d];
  128.             if ((d=(c1>>4)-(c2>>4))<0)
  129.                 d= -d;
  130.             e+=d;
  131.             e2+=sqr[d];
  132.         } while (--n>=0);
  133.     error+=e;
  134.     error2+=e2;
  135. }
  136.  
  137.  
  138. do_er_pic()
  139. {
  140.     unsigned char *p, *q, *r;
  141.     long e, e2, n, d;
  142.     unsigned long c1, c2;
  143.     
  144.     p=buf1;
  145.     q=buf2;
  146.     r=bufo;
  147.     e=e2=0;
  148.     n=thischunk-1;
  149.         do {
  150.             c1= *p++;
  151.             c2= *q++;
  152.             if ((d=(c1&0x0f)-(c2&0x0f))<0)
  153.                 d= -d;
  154.         if (d==1) {
  155.         *r=0x08;
  156.         ++e;
  157.         ++e2;
  158.         } else if (d) {
  159.         *r=0x0f;
  160.             e+=d;
  161.                 e2+=sqr[d];
  162.         } else
  163.         *r=0x00;
  164.             if ((d=(c1>>4)-(c2>>4))<0)
  165.                 d= -d;
  166.         if (d==1) {
  167.         *r++ |=0x80;
  168.         ++e;
  169.         ++e2;
  170.         } else if (d) {
  171.         *r++ |=0xf0;
  172.             e+=d;
  173.                 e2+=sqr[d];
  174.         } else
  175.         ++r;
  176.         } while (--n>=0);
  177.     error+=e;
  178.     error2+=e2;
  179. }
  180.  
  181.  
  182. cread(fde, buf, len)
  183. int fde;
  184. void *buf;
  185. int len;
  186. {
  187.     int r;
  188.  
  189.     r=read(fde, buf, len);
  190.     if (r<0) {
  191.         perror("read");
  192.         exit(1);
  193.     } else if (r!=len) {
  194.         printf("read: unexpected EOF\n");
  195.         exit(1);
  196.     }
  197. }
  198.  
  199.  
  200. cwrite(fde, buf, len)
  201. int fde;
  202. void *buf;
  203. int len;
  204. {
  205.     if (write(fde, buf, len)!=len) {
  206.         perror("write");
  207.         exit(1);
  208.     }
  209. }
  210.