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