home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma01.dms / ma01.adf / wasp / src / wasp.c < prev    next >
C/C++ Source or Header  |  1992-01-01  |  9KB  |  414 lines

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