home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 564a.lha / wasp_v1.21 / Src.LZH / Src / wasp.c < prev    next >
C/C++ Source or Header  |  1991-10-21  |  9KB  |  427 lines

  1. /* wasp - convert a picture from GIF, IFF, Sun rasterfile, 
  2.  * PPM or SRGR to IFF or SRGR,
  3.  * optionally scaling it and performing various operations on it.
  4.  * copyright Steven Reiz 1990, 1991, see the COPYING file for more info.
  5.  * 1/12/90 - 2/6/91, 23/6/91, 30/6/91, 3/7/91 - 8/7/91,
  6.  * 24/7/91
  7.  * usage: see the usage routine below
  8.  */
  9.  
  10. #define MAIN
  11. #include "wasp.h"
  12. #ifndef NOSH
  13. #include "wasp.sh"
  14. #endif
  15.  
  16. static short zap_option=0;
  17.  
  18. #ifdef __STDC__
  19. main(int argc, char **argv)
  20. #else
  21. main(argc, argv)
  22. int argc;
  23. char **argv;
  24. #endif
  25. {
  26. #ifdef DEBUG
  27.     resetsecs();
  28. #endif
  29.     initvars();
  30.     do_options(argc, argv);
  31.     if ((infd=open(infilename, O_RDONLY))<0)
  32.     error1(E0_FATAL, E1_IO, E2_OPEN, E3_ERRNO, infilename);
  33.     if (!read_srgr() && !read_iff() && !read_gif() && !read_ras() && !read_ppm()) {
  34.         printe("%s is not in IFF, GIF87a, Sun rasterfile, ppm or SRGR format\n", infilename);
  35.         exit(1);
  36.     }
  37.     close(infd);
  38.     if ((outfd=open(outfilename, O_WRONLY|O_CREAT|O_TRUNC, 0644))<0)
  39.     error1(E0_FATAL, E1_IO, E2_CREAT, E3_ERRNO, outfilename);
  40.     if (zap_option)
  41.     unlink(infilename);
  42.     scaley(yc, yd);
  43.     scalex(xa, xb);
  44.     do_operations(argc, argv);
  45.     assert(outputformat==SRGR || outputformat==IFF);
  46.     if (outputformat==SRGR)
  47.     write_srgr();
  48.     else
  49.     write_iff();
  50.     exit(0);
  51. }
  52.  
  53.  
  54. char *usagestr[]=
  55. {
  56.     "usage: wasp [options] [operations] [parameters] infile [outfile]",
  57.     "options:",
  58.     "-zap        : delete infile after it has been read",
  59.     "-gifmaptrunc: truncate gif colormap entries to 4 bits i.s.o. rounding them",
  60.     "-iff|srgr   : the output format, IFF ILBM or SRGR",
  61.     "-rgb n      : ilbm output mode, direct RGB with n bitplanes",
  62.     "-nocompr    : ilbm output will not be compressed",
  63.     "-asc        : autoscale, scales to fit the entire picture on the screen",
  64.     "-nohires    : asc hint, no hires output mode for pictures with more than 16 colors",
  65.     "-nohires!   : asc hint, no hires output mode",
  66.     "-scrw n     : asc hint, screen width in lores pixels",
  67.     "-scrh n     : asc hint, screen height in nolace pixels",
  68.     "-sliced     : ilbm output mode, SHAM aka sliced HAM",
  69.     "-dyn        : ilbm output mode, CTBL aka dynamic ham/hires",
  70.     "-lace|nolace: ilbm output interlace, on or off",
  71.     "-hires|lores|ham|ehb: ilbm output mode",
  72.     "operations:",
  73.     "-x a/b      : scale horizontal, producing a columns out of every b input columns",
  74.     "-y c/d      : scale vertical, producing c rows out of every d input rows",
  75.     "-clip       : cut a rectangle out of the picture and drop the rest",
  76.     "-xaverage   : same as -x 1/2, but averaging the two input columns",
  77.     "-xmirror    : mirror the picture in a vertical mirror",
  78.     "-ymirror    : mirror the picture in a horizontal mirror",
  79.     "-transpose  : mirror the picture diagonally, in mirror y=x",
  80.     "parameters:",
  81.     "-cmeth m    : counting method, all1|alldif|allfdif|j1|j21|jdif|jdifsh|jfdif|jfdifsh|hmgs|hmcubic",
  82.     "-dmeth m    : distribution method, mu|wf|ehb|mue|hs|con",
  83.     "-dmeth2 m   : iterative ham second distribution method",
  84.     "-threshold n: counting threshold",
  85.     NULL
  86. };
  87.  
  88.  
  89. #ifdef __STDC__
  90. usage(void)
  91. #else
  92. usage()
  93. #endif
  94. {
  95.     char **p;
  96.  
  97.     printe("wasp %s, %s %s, copyright 1990, 1991 by Steven Reiz\n",
  98.      version, __DATE__, __TIME__);
  99.     for (p=usagestr; *p; ++p)
  100.         printe("%s\n", *p);
  101.     exit(1);
  102. }
  103.  
  104.  
  105. #ifdef __STDC__
  106. initvars(void)
  107. #else
  108. initvars()
  109. #endif
  110. {
  111.     outputformat=IFF;
  112.     directrgb= -1;
  113.     compression=COMPR_RLE;
  114.     asc=0;
  115.     hires=HIRES_OK;
  116.     sliced=SLICED_NOT;
  117.     scrw=DEF_SCRW;
  118.     scrh=DEF_SCRH;
  119.     ymode=UNSET;
  120.     xmode=UNSET;
  121.     xa=1; xb=1; yc=1; yd=1;
  122.     countmeth=COUNTMETH_UNSET;
  123.     distrmeth=DISTRMETH_UNSET;
  124.     distrmeth2=DISTRMETH_UNSET;
  125.     curdistrmeth=DISTRMETH_UNSET;
  126.     threshold=1;
  127.     inoperation=0;
  128.     gifmaptrunc=0;
  129. #ifdef AMIGA
  130.     get_scr_size();
  131. #endif
  132. }
  133.  
  134.  
  135. #ifdef __STDC__
  136. void *cmalloc(unsigned int n)
  137. #else
  138. void *cmalloc(n)
  139. unsigned int n;
  140. #endif
  141. {
  142.     void *p;
  143.  
  144.     if (!(p=malloc(n)))
  145.     error1(E0_FATAL, E1_NOMEM, E2_UNSPEC, E3_NOMEM, n);
  146.     return p;
  147. }
  148.  
  149.  
  150. #ifdef __STDC__
  151. void *ccalloc(unsigned int n)
  152. #else
  153. void *ccalloc(n)
  154. unsigned int n;
  155. #endif
  156. {
  157.     void *p;
  158.  
  159.     if (!(p=calloc(n, 1)))
  160.     error1(E0_FATAL, E1_NOMEM, E2_UNSPEC, E3_NOMEM, n);
  161.     return p;
  162. }
  163.  
  164.  
  165. #ifdef __STDC__
  166. void *crealloc(void *q, unsigned int n)
  167. #else
  168. void *crealloc(q, n)
  169. void *q;
  170. unsigned int n;
  171. #endif
  172. {
  173.     void *p;
  174.  
  175.     if (!(p=realloc(q, n)))
  176.     error1(E0_FATAL, E1_NOMEM, E2_UNSPEC, E3_NOMEM, n);
  177.     return p;
  178. }
  179.  
  180.  
  181. #ifdef __STDC__
  182. lowcase(char *s)
  183. #else
  184. lowcase(s)
  185. char *s;
  186. #endif
  187. {
  188.     char c;
  189.  
  190.     while (c= *s) {
  191.         if (c>='A' && c<='Z')
  192.             *s=c-'A'+'a';
  193.         ++s;
  194.     }
  195. }
  196.  
  197.  
  198. #ifdef __STDC__
  199. u_long ceillog2(u_long a)
  200. #else
  201. u_long ceillog2(a)
  202. u_long a;
  203. #endif
  204. {
  205.     u_long b;
  206.     short i;
  207.  
  208.     assert(a);
  209.     b=a;
  210.     for (i= -1; b; ++i)
  211.         b>>=1;
  212.     b=1L<<i;
  213.     return (u_long)(a>b ? i+1 : i);
  214. }
  215.  
  216.  
  217. #ifdef DEBUG
  218. #ifndef AMIGA
  219. #ifdef __STDC__
  220. resetsecs(void)
  221. #else
  222. resetsecs()
  223. #endif
  224. {
  225. }
  226.  
  227.  
  228. #ifdef __STDC__
  229. long centisecs(void)
  230. #else
  231. long centisecs()
  232. #endif
  233. {
  234.     return 0;
  235. }
  236. #endif
  237. #endif
  238.  
  239.  
  240. static char *sc[]={
  241. #define S_IFF 0
  242.     "iff",
  243. #define S_SRGR 1
  244.     "srgr",
  245. #define S_RGB 2
  246.     "rgb",
  247. #define S_NOCOMPR 3
  248.     "nocompr",
  249. #define S_ASC 4
  250.     "asc",
  251. #define S_NOHIRES 5
  252.     "nohires",
  253. #define S_NOHIRES2 6
  254.     "nohires!",
  255. #define S_SCRW 7
  256.     "scrw",
  257. #define S_SCRH 8
  258.     "scrh",
  259. #define S_SLICED 9
  260.     "sliced",
  261. #define S_DYN 10
  262.     "dyn",
  263. #define S_LACE 11
  264.     "lace",
  265. #define S_NOLACE 12
  266.     "nolace",
  267. #define S_HIRES 13
  268.     "hires",
  269. #define S_LORES 14
  270.     "lores",
  271. #define S_HAM 15
  272.     "ham",
  273. #define S_EHB 16
  274.     "ehb",
  275. #define S_X 17
  276.     "x",
  277. #define S_Y 18
  278.     "y",
  279. #define S_CLIP 19
  280.     "clip",
  281. #define S_XAVERAGE 20
  282.     "xaverage",
  283. #define S_XMIRROR 21
  284.     "xmirror",
  285. #define S_YMIRROR 22
  286.     "ymirror",
  287. #define S_TRANSPOSE 23
  288.     "transpose",
  289. #define S_COUNTMETH 24
  290.     "cmeth",
  291. #define S_DISTRMETH 25
  292.     "dmeth",
  293. #define S_DISTRMETH2 26
  294.     "dmeth2",
  295. #define S_THRESHOLD 27
  296.     "threshold",
  297. #define S_ZAP 28
  298.     "zap",
  299. #define S_GIFMAPTRUNC 29
  300.     "gifmaptrunc",
  301. #define S_UNKNOWN 30
  302.     NULL
  303. };
  304.  
  305.  
  306. #ifdef __STDC__
  307. int stringcode(char *s)
  308. #else
  309. int stringcode(s)
  310. char *s;
  311. #endif
  312. {
  313.     char **p;
  314.  
  315.     if (!s || *s!='-')
  316.         return S_UNKNOWN;
  317.     ++s;
  318.     for (p=sc; *p; ++p)
  319.         if (!strcmp(s, *p))
  320.             break;
  321.     return p-sc;
  322. }
  323.  
  324.  
  325. #ifdef __STDC__
  326. do_options(int argc, char **argv)
  327. #else
  328. do_options(argc, argv)
  329. int argc;
  330. char **argv;
  331. #endif
  332. {
  333.     int i, j;
  334.  
  335.     if (argc<2)
  336.         usage();
  337.     for (i=1; i<argc && argv[i][0]=='-'; ++i) {
  338.         lowcase(argv[i]);
  339.         switch (stringcode(argv[i])) {
  340.         case S_IFF:      outputformat=IFF;     break;
  341.         case S_SRGR:     outputformat=SRGR;    break;
  342.     case S_RGB:     directrgb=atol(argv[++i]); break;
  343.     case S_NOCOMPR:  compression=COMPR_NONE; break;
  344.         case S_ASC:      asc=1;                break;
  345.         case S_NOHIRES:  hires=HIRES_MAYBE;    break;
  346.         case S_NOHIRES2: hires=HIRES_NOT;      break;
  347.         case S_SCRW:     scrw=atol(argv[++i]); break;
  348.         case S_SCRH:     scrh=atol(argv[++i]); break;
  349.         case S_SLICED:   sliced=SLICED_SHAM;   break;
  350.         case S_DYN:     sliced=SLICED_DYN;    break;
  351.         case S_LACE:     ymode=LACE;           break;
  352.         case S_NOLACE:   ymode=NOLACE;         break;
  353.         case S_HIRES:    xmode=HIRES;          break;
  354.         case S_LORES:    xmode=LORES;          break;
  355.         case S_HAM:      xmode=HAM;            break;
  356.         case S_EHB:      xmode=EHB;            break;
  357.         case S_X: 
  358.             xa=atol(argv[++i]);
  359.             xb=atol(strchr(argv[i], '/')+1);
  360.         break;
  361.         case S_Y:
  362.             yc=atol(argv[++i]);
  363.             yd=atol(strchr(argv[i], '/')+1);
  364.         break;
  365.         case S_CLIP:       /* these operations are handled below */
  366.         case S_XAVERAGE:
  367.     case S_XMIRROR:
  368.     case S_YMIRROR:
  369.     case S_TRANSPOSE:
  370.         break;
  371.         case S_COUNTMETH:
  372.         if ((j=cmethnum(argv[++i]))== -1)
  373.             usage();
  374.         countmeth=j;
  375.     break;
  376.         case S_DISTRMETH:
  377.         if ((j=dmethnum(argv[++i]))== -1)
  378.             usage();
  379.         distrmeth=j;
  380.     break;
  381.         case S_THRESHOLD: threshold=atol(argv[++i]); break;
  382.         case S_DISTRMETH2: 
  383.             xmode=HAM;
  384.         if ((j=dmethnum(argv[++i]))== -1)
  385.             usage();
  386.         distrmeth2=j;
  387.         break;
  388.     case S_ZAP: zap_option=1; break;
  389.     case S_GIFMAPTRUNC: gifmaptrunc=1; break;
  390.         default:
  391.             usage();
  392.         break;
  393.         }
  394.     }
  395.     if (argc>i+2)
  396.         usage();
  397.     infilename=argv[i];
  398.     if (argc>i+1)
  399.     outfilename=argv[i+1];
  400.     else
  401.     outfilename=NULL;
  402. }
  403.  
  404.  
  405. #ifdef __STDC__
  406. do_operations(int argc, char **argv)
  407. #else
  408. do_operations(argc, argv)
  409. int argc;
  410. char **argv;
  411. #endif
  412. {
  413.     int i;
  414.  
  415.     inoperation=1;
  416.     for (i=1; i<argc; ++i) {
  417.         switch (stringcode(argv[i])) {
  418.         case S_CLIP:      do_clipping(); break;
  419.         case S_XAVERAGE:  xaverage();    break;
  420.     case S_XMIRROR:      xmirror();     break;
  421.     case S_YMIRROR:      ymirror();     break;
  422.     case S_TRANSPOSE: transpose();     break;
  423.         }
  424.     }
  425.     inoperation=0;
  426. }
  427.