home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume02 / image < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  58.0 KB

  1. From mipos3!intelca!oliveb!ames!necntc!ncoast!allbery Fri Jan 29 08:03:34 PST 1988
  2. Article 266 of comp.sources.misc:
  3. Path: td2cad!mipos3!intelca!oliveb!ames!necntc!ncoast!allbery
  4. From: everson@compsci.bristol.ac.uk (Phill Everson)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i019: Image processing programs
  7. Message-ID: <7130@ncoast.UUCP>
  8. Date: 26 Jan 88 04:07:33 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Organization: Dept of Computer Science, University of Bristol, UK.
  11. Lines: 2834
  12. Approved: allbery@ncoast.UUCP
  13. X-Archive: comp.sources.misc/8801/19
  14. Comp.sources.misc: Volume 2, Issue 19
  15. Submitted-By: Phill Everson <everson@compsci.bristol.ac.uk>
  16. Archive-Name: image
  17.  
  18. Comp.sources.misc: Volume 2, Issue 19
  19. Submitted-By: Phill Everson <everson@compsci.bristol.ac.uk>
  20. Archive-Name: image
  21.  
  22. # To unbundle, sh this file
  23.  echo README 1>&2
  24. cat >README <<'End of README'
  25. In response to a plea on the net:
  26.  
  27. This is a family of image processing programs written by
  28. Phill Everson <everson@uk.aucc.bristol.cs: JANET
  29.                everson%uk.aucc.bristol.cs@nss.cs.ucl.ac.uk: ARPA ? >
  30. with help from Gareth Waddell (notably for the dynamic array library)
  31. at Bristol University in the UK for SUN workstations, both colour and 
  32. black/white.  It includes tools to display images on Colour monitors, 
  33. B/W monitors, to convolve a filter over an image, to create a histogram 
  34. of the greylevels in an image, to normalise an image, to threshold an image, 
  35. to convert an image to Postscript and ... (read the manual page alv(1) 
  36. for other features).
  37.  
  38. Alv stands for Autonomous Land Vehicle, the research project that these
  39. were originally developed for. Some of the tools have been used MUCH
  40. more than others and so can be regarded as being pretty much correct
  41. (dsp, cdsp, convolve, pixval, imagelw, subarea, subsample, coltable, hist
  42. & invert).
  43. If any of the others seem to be playing up it is possible that 
  44. there is a bug in there somewhere -- some tools were added at the 
  45. request of others who promised to test them and have never been heard 
  46. of since! Please send me any bug reports (&fixes?) to me.
  47.  
  48. *************************************************************************
  49. To get this system up and on the road:
  50.  
  51.     1. Edit the Makefile, changing the directory paths for
  52.        BINDIR, LIBDIR, INCDIR, MANDIR & FILDIR to suit your system.
  53.     2. Type 'make' and everything will be compiled and installed.
  54.     3. Read the manual page alv(1). It can be formatted from this 
  55.        directory by typing 'nroff -man alv.1 | more'.
  56. *************************************************************************
  57.  
  58. This family of programs has 3 manual pages; alv(1), alv(3) & alv(5).
  59. alv(1) has a general description of each of the programs and what each 
  60. of them should do. alv(3) is a description of the library created 
  61. and alv(5) is a description of the file format used for an image.
  62. (I've also included the manual page dynamem(3) for a dynamic memory 
  63. allocation library which is used by the alv library and which someone may 
  64. find useful.)
  65.  
  66. The method that we have found works best is that everyone working
  67. on vision-type programs uses the same file format (see alv(5)) and
  68. most people will use the core tools to display images etc and the
  69. library functions for their own programs.
  70.  
  71. These are and will be used a lot here, so if anybody adds or modifies them,
  72. please send me details and I'll collect, collate and possibly post notable 
  73. additions at a later date. Thanks.
  74.  
  75. I hope they're of some use.
  76.  
  77.                 Phill Everson
  78. End of README
  79.  echo Makefile 1>&2
  80. cat >Makefile <<'End of Makefile'
  81. #
  82. # ALV image processing programs
  83. # written by Phill Everson
  84. #            Computer Science Dept.
  85. #            Bristol University UK
  86. #            21st January, 1988
  87. #
  88. # <everson@uk.aucc.bristol.cs: JANET
  89. #  everson%uk.aucc.bristol.cs@nss.cs.ucl.ac.uk: ARPA ? >
  90. #
  91.  
  92. # BINDIR - location of compiled binaries
  93. # LIBDIR - location of compiled libraries
  94. # INCDIR - location of include files
  95. # MANDIR - location of top level of manual
  96. # FILDIR - location of filters (for convolve)
  97.  
  98. BINDIR = /usr/kukini/alv/vision/bin
  99. LIBDIR = /usr/kukini/alv/vision/lib
  100. INCDIR = /usr/kukini/alv/vision/include
  101. MANDIR = /usr/kukini/alv/vision/man
  102. FILDIR = /usr/kukini/alv/vision/usr/filters
  103. CFLAGS = -g -I$(INCDIR) 
  104.    ARC = vision.a
  105.  DLIBS = -lsuntool -lsunwindow -lpixrect -L$(LIBDIR) -lalv
  106.  OLIBS = -L$(LIBDIR) -lalv
  107.  PROGS = cdsp dsp convert addhead hist imagelw invert pixval retitle \
  108.          subarea subsample thresh normalise ffill convolve range striphead 
  109.  
  110. install: setup all docs filters
  111.     mv $(PROGS) $(BINDIR)
  112.  
  113. setup:
  114.     cp image.h $(INCDIR)
  115.  
  116. all: lib cdsp dsp convert addhead hist imagelw invert pixval retitle \
  117.      subarea subsample thresh normalise ffill convolve range striphead
  118.  
  119. filters: 
  120.     @cp avg1 lap1 sobel1 xdiff1 $(FILDIR)
  121.  
  122. archive: clean
  123.     @ar crv $(ARC) *
  124.     @compress $(ARC)
  125.     @mv $(ARC).Z $(ARCHIVE_DIR)
  126.     
  127. docs:
  128.     @cp alv.1 $(MANDIR)/man1
  129.     @cp alv.3 $(MANDIR)/man3
  130.     @cp alv.5 $(MANDIR)/man5
  131.     @cp dynamem.3 $(MANDIR)/man3
  132.  
  133. cdsp: cdsp.o 
  134.     cc $(CFLAGS) cdsp.o -o cdsp $(DLIBS)
  135.  
  136. dsp: dsp.o
  137.     cc $(CFLAGS) dsp.o -o dsp $(DLIBS)
  138.  
  139. hist: hist.o
  140.     cc $(CFLAGS) hist.o -o hist $(DLIBS) -lm
  141.  
  142. convert: convert.o
  143.     cc $(CFLAGS) convert.o -o convert -L$(LIBDIR) -ldynamem
  144.  
  145. addhead: addhead.o
  146.     cc $(CFLAGS) addhead.o -o addhead -L$(LIBDIR) -ldynamem
  147.  
  148. striphead: striphead.o
  149.     cc $(CFLAGS) striphead.o -o striphead $(OLIBS)
  150.  
  151. convolve.o: convolve.c
  152.     cc $(CFLAGS) -DFILTERS_DIR=$(FILDIR) -c convolve.c
  153.  
  154. convolve: convolve.o
  155.     cc $(CFLAGS) convolve.o -o convolve $(OLIBS)
  156.  
  157. invert: invert.o
  158.     cc $(CFLAGS) invert.o -o invert $(OLIBS)
  159.  
  160. pixval: pixval.o
  161.     cc $(CFLAGS) pixval.o -o pixval $(OLIBS)
  162.  
  163. retitle: retitle.o
  164.     cc $(CFLAGS) retitle.o -o retitle $(OLIBS)
  165.  
  166. range: range.o
  167.     cc $(CFLAGS) range.o -o range $(OLIBS)
  168.  
  169. imagelw: imagelw.o
  170.     cc $(CFLAGS) imagelw.o -o imagelw $(OLIBS)
  171.  
  172. subsample: subsample.o
  173.     cc $(CFLAGS) subsample.o -o subsample $(OLIBS)
  174.  
  175. subarea: subarea.o
  176.     cc $(CFLAGS) subarea.o -o subarea $(OLIBS)
  177.  
  178. thresh: thresh.o
  179.     cc $(CFLAGS) thresh.o -o thresh $(OLIBS) -lm
  180.  
  181. normalise: normalise.o
  182.     cc $(CFLAGS) normalise.o -o normalise $(OLIBS)
  183.  
  184. ffill: ffill.o
  185.     cc $(CFLAGS) ffill.o -o ffill $(OLIBS)
  186.  
  187. lib: alvlib.o dynamem.o
  188.     ar rc libalv.a alvlib.o dynamem.o
  189.     ranlib libalv.a
  190.     ar rc libdynamem.a dynamem.o
  191.     ranlib libdynamem.a
  192.     mv libalv.a libdynamem.a $(LIBDIR)
  193.     touch lib
  194.     
  195. wc: 
  196.     @wc *.c | sort -n
  197.  
  198. clean:
  199.     @touch dummy.o
  200.     @/bin/rm *.o
  201.  
  202. troff:
  203.     troff -t -man alv.1 | thack | pspr
  204.     troff -t -man alv.3 | thack | pspr
  205.     troff -t -man alv.5 | thack | pspr
  206. End of Makefile
  207.  echo addhead.c 1>&2
  208. cat >addhead.c <<'End of addhead.c'
  209. #include <stdio.h>
  210.  
  211. main(argc,argv)
  212. int argc;
  213. char **argv;
  214. {
  215.     int i,j;
  216.     int xsize,ysize;
  217.     unsigned char **pic;
  218.     char *file, *title;
  219.     FILE *fp;
  220.  
  221.     if (argc!=5) {
  222.         fprintf(stderr,"Usage: %s: x y filename title\n",argv[0]);
  223.         exit(-1);
  224.         }
  225.  
  226.     xsize=atoi(argv[1]);
  227.     ysize=atoi(argv[2]);
  228.     file =argv[3];
  229.     title =argv[4];
  230.  
  231.     if (!(fp=fopen(file,"r"))) {
  232.         fprintf(stderr,"Cannot open %s\n",file);
  233.         exit(-1);
  234.         }
  235.  
  236.     pic = (unsigned char **) dynamem (&pic, sizeof(unsigned char), 2, ysize, xsize);
  237.  
  238.     putw(xsize<<16|ysize,stdout);
  239.  
  240.     for (i=0;i<252;i++,*title?title++:0)
  241.         putchar(*title?*title:0);
  242.     
  243.     for (j=0;j<ysize;j++)
  244.         for (i=0;i<xsize;i++)
  245.             pic[j][i]=getc(fp);
  246.  
  247.     for (j=0;j<ysize;j++)
  248.         for(i=0;i<xsize;i++)
  249.             putchar(pic[j][i]);
  250.     
  251.     fclose(fp);
  252. }
  253. End of addhead.c
  254.  echo alv.1 1>&2
  255. cat >alv.1 <<'End of alv.1'
  256. .\" .TH name section cent-foot
  257. .TH ALV 1 "4th December 1987"
  258. .SH NAME
  259. .\" name \- function
  260. alv \- standard ALV tools
  261. .SH SYNOPSIS
  262. .\" Bold keywords, Italic variables, [] options, | alternatives.
  263. wind_opts = [
  264. .BI -X n
  265. ] [
  266. .BI -Y n
  267. ] [
  268. .BI -x n
  269. ] [
  270. .BI -y n
  271. ]
  272. .br
  273. std_opts = 
  274. .RB [ -H ]
  275. [
  276. .BI -T title
  277. ]
  278. .RI [ filename ]
  279. .br
  280. .B dsp
  281. .RB [ -m ]
  282. .RB [ wind_opts ]
  283. .RB [ std_opts ]
  284. .br
  285. .B cdsp
  286. .RB [ -m ]
  287. .RB [ wind_opts ]
  288. .RB [ std_opts ]
  289. .br
  290. .B imagelw
  291. .RB [ wind_opts ]
  292. .RB [ std_opts ]
  293. .RB [ -t ]
  294. .RB [ -m ]
  295. .RB [ -c ]
  296. .RB [ - ]
  297. .br
  298. .B hist
  299. .RB [ -l ]
  300. .RB [ -v ]
  301. .RB [ wind_opts ]
  302. .RB [ std_opts ]
  303. .br
  304. .B thresh
  305. [
  306. .BI -t n
  307. ]
  308. .RB [ std_opts ]
  309. .br
  310. .B invert
  311. .RB [ std_opts ]
  312. .br
  313. .B normalise
  314. .RB [ std_opts ]
  315. .br
  316. .B range
  317. [
  318. .BI -l n
  319. ]
  320. [
  321. .BI -h n
  322. ]
  323. .RB [ std_opts ]
  324. .br
  325. .B convolve
  326. [
  327. .BI -f filtername
  328. ]
  329. .RB [ -e ]
  330. .RB [ -F ]
  331. .RB [ std_opts ]
  332. .br
  333. .B subsample
  334. .I xsize
  335. .I ysize
  336. .RB [ std_opts ]
  337. .br
  338. .B subarea
  339. [
  340. .BI -x n
  341. ]
  342. [
  343. .BI -y n
  344. ]
  345. [
  346. .BI -X n
  347. ]
  348. [
  349. .BI -Y n
  350. ]
  351. .RB [ std_opts ]
  352. .br
  353. .B pixval
  354. [
  355. .BI -x n
  356. ]
  357. [
  358. .BI -y n
  359. ]
  360. [
  361. .BI -X n
  362. ]
  363. [
  364. .BI -Y n
  365. ]
  366. .RB [ std_opts ]
  367. .br
  368. .B ffill
  369. .I "xpos ypos"
  370. .BI -b n 
  371. ]
  372. .RB [ std_opts ]
  373. .br
  374. .B retitle
  375. .RB [ std_opts ]
  376. .br
  377. .B convert
  378. .I xsize
  379. .I ysize
  380. .I filename
  381. .I title
  382. .br
  383. .B addhead
  384. .I xsize
  385. .I ysize
  386. .I filename
  387. .I title
  388. .br
  389. .B striphead
  390. .RB [ std_opts ]
  391. .br
  392.  
  393. .SH DESCRIPTION
  394. .\" Italic files, commands, IR manual-entry (manual-section)
  395. These tools comprise
  396. the majority of those
  397. needed for work on the
  398. .I ALV
  399. and
  400. .I Bubble
  401. projects.
  402. They use a standard
  403. file format, see 
  404. .IR alv (5).
  405. .I Dsp
  406. and
  407. .I cdsp
  408. display pictures
  409. on a monochrome
  410. and colour display
  411. respectively. (Place the mouse in the window
  412. created by 
  413. .I coltable 
  414. on a Colour Sun to see
  415. the image in false colour).
  416. .I Imagelw
  417. displays the picture on the
  418. Apple Laserwriter in
  419. The Sun Lounge.
  420. .I Hist
  421. displays histogram
  422. displays, either
  423. linearly or logorithmically.
  424. .I Thresh
  425. thresholds data on a
  426. picture using either
  427. a specified threshold
  428. value or an automatically
  429. generated one.
  430. .I Invert
  431. inverts all the data in
  432. an image.
  433. .I Normalise
  434. normalises the histogram
  435. of an image.
  436. .I Range 
  437. stretches the grey level distribution
  438. of an image.
  439. .I Convolve
  440. performs a convolution
  441. on an image using 
  442. a specified filter.
  443. .I Subsample
  444. reduces the picture
  445. size by subsampling
  446. the original picture
  447. (but can also be used
  448. to enlarge).
  449. .I Subarea
  450. creates a new picture
  451. which is a part of the
  452. original picture.
  453. .I Pixval
  454. enables a decimal valued
  455. print of an area of an
  456. image to be generated.
  457. .I Ffill
  458. flood fills an image based
  459. around a small point, with
  460. a specified boundary colour.
  461. .I Retitle
  462. gives a picture a new title.
  463. .I Convert
  464. converts a file stored in the old 
  465. alv file format to the new.
  466. .I Addhead
  467. adds a header, thus converting
  468. it to the standard file format, 
  469. to a file produced
  470. by, for example, the 
  471. .I "ALV Framegrabber."
  472. .I Striphead
  473. removes a header from an image.
  474. .SH OPTIONS
  475. .\" Itemised list of options
  476. .TP 4
  477. Window Options
  478. -Xn           window x size is n
  479. .br
  480. -Yn           window y size is n
  481. .br
  482. -xn           window x offset is n
  483. .br
  484. -yn           window y offset is n
  485. .br
  486. .TP 4
  487. Standard Options
  488. -H            give help list
  489. .br
  490. -Ttitle       output-image title
  491. .br
  492. filename      read from filename
  493. .br
  494. .TP 4
  495. Dsp/Cdsp Options
  496. -m            use backing pixrect
  497. .br
  498. .TP 4
  499. Imagelw Options
  500. -T            produce TeX special file
  501. .br
  502. -m            send mail on completion of print
  503. .br
  504. -c            automatically scale picture to fit page
  505. .br
  506. -             write Postscript program to standard output
  507. .br
  508. .TP 4
  509. Hist Options
  510. -l            logarithmic scaling
  511. .br
  512. -v            vertical lines off
  513. .br
  514. .TP 4
  515. Thresh Options
  516. -tn           threshold value
  517. .br
  518. .TP 4
  519. Normalise Options
  520. -ln           least normalise value
  521. .br
  522. -hn           highest normalise value
  523. .br
  524. .TP 4
  525. Convolve Options
  526. -ffiltername  use filter filtername
  527. .br
  528. -e            enhanced scaling
  529. .br
  530. -F            list all filters
  531. .br
  532. .TP 4
  533. Pixval Options
  534. -Xn           x size
  535. .br
  536. -Yn           y size
  537. .br
  538. -xn           x offset
  539. .br
  540. -yn           y offset
  541. .br
  542. .TP 4
  543. Ffill Options
  544. -bn           boundary grey level
  545. .br
  546. .\".SH FILES
  547. .\" List of all files used by the program
  548. .SH "SEE ALSO"
  549. .\" List of references, textual, and MAN pages.
  550. alv(3), alv(5).
  551. .\".SH DIAGNOSTICS
  552. .\" List of error messages and return codes the user may expect
  553. .SH BUGS
  554. .\" Known Limitations or Desirable additions to the command
  555. The code to pick out the filename from the command arguments 
  556. list is unreliable in some cases. (It will core dump). 
  557. This can be avoided by using cat to pipe the file into the
  558. command.
  559. End of alv.1
  560.  echo alv.3 1>&2
  561. cat >alv.3 <<'End of alv.3'
  562. .\" .TH name section cent-foot left-foot cent-head
  563. .TH ALV 3 "4th December 1987"
  564. .SH NAME
  565. .\" name \- function
  566. alv \- standard library functions
  567. .SH SYNOPSIS
  568. .\" Bold keywords, Italic variables, [] options, | alternatives.
  569. typedef struct img {
  570. .br
  571.     unsigned char **image;
  572. .br
  573.     short xsize, ysize;
  574. .br
  575.     char title[252];
  576. .br
  577. } IMAGE;
  578. .sp
  579. int winxsize, winysize,
  580. .br
  581.     winxoff,  winyoff;
  582. .br
  583. int helpflag;
  584. .br
  585. char *newtitle;
  586. .sp
  587. IMAGE *readimage(fp)
  588. .br
  589. FILE *fp;
  590. .sp
  591. writeimage(fp,image)
  592. .br
  593. FILE *fp;
  594. .br
  595. IMAGE *image;
  596. .sp
  597. IMAGE *newimage(xsize,ysize,title)
  598. .br
  599. int xsize,ysize;
  600. .br
  601. char *title
  602. .sp
  603. subsample(image1,image2)
  604. .br
  605. IMAGE *image1,*image2;
  606. .sp
  607. subarea(image1,image2,xoff,yoff)
  608. .br
  609. IMAGE *image1,*image2;
  610. .br
  611. int xoff,yoff;
  612. .sp
  613. stdopts(argc,argv)
  614. .br
  615. int *argc;
  616. .br
  617. char **argv;
  618. .sp
  619. windopts(argc,argv)
  620. .br
  621. int *argc;
  622. .br
  623. char **argv;
  624. .SH DESCRIPTION
  625. .\" Italic files, commands, IR manual-entry (manual-section)
  626. .I Readin
  627. reads in an image from
  628. the specified file, and
  629. creates an image structure
  630. with all the relevant
  631. data contained in it.
  632. .I Writeout
  633. writes the specified
  634. image to the specified
  635. file.
  636. .I Newimage
  637. creates a new
  638. image of the specified
  639. dimensions.
  640. .I Subsample
  641. reproduces the whole
  642. of
  643. .I image1
  644. in
  645. .I image2
  646. suitably scaled (up or
  647. down) so as to make
  648. it fit into the available
  649. room in
  650. .IR image2 .
  651. .I Subarea
  652. reproduces the relevant
  653. part of
  654. .I image1
  655. in
  656. .I image2
  657. so that
  658. .I image2
  659. has the same resolution as
  660. .I image1
  661. and is as full as possible
  662. (note that it is possible
  663. to use this also to increase
  664. the size of an image).
  665. .I Stdopts
  666. parses the
  667. .B -t
  668. and
  669. .B -h options
  670. setting
  671. .I helpflag
  672. and
  673. .IR newtitle .
  674. .I Windopts
  675. parses the
  676. .BR -x , -y , -X ,
  677. .B -Y
  678. options setting
  679. the variables
  680. .IR winxsize , winysize , winxoff ,
  681. .IR winyoff .
  682. .\".SH FILES
  683. .\" List of all files used by the program
  684. .SH "SEE ALSO"
  685. .\" List of references, textual, and MAN pages.
  686. alv(1),alv(3).
  687. .\".SH DIAGNOSTICS
  688. .\" List of error messages and return codes the user may expect
  689. .\".SH BUGS
  690. .\" Known Limitations or Desirable additions to the command
  691. End of alv.3
  692.  echo alv.5 1>&2
  693. cat >alv.5 <<'End of alv.5'
  694. .\" .TH name section cent-foot left-foot cent-head
  695. .TH ALV 5 "4th December 1987"
  696. .SH NAME
  697. .\" name \- function
  698. alv \- common file format
  699. .SH SYNOPSIS
  700. .\" Bold keywords, Italic variables, [] options, | alternatives.
  701. typedef struct img {
  702. .br
  703.     unsigned char **image;
  704. .br
  705.     short xsize, ysize;
  706. .br
  707.     char title[252];
  708. .br
  709. } IMAGE;
  710. .SH DESCRIPTION
  711. .\" Italic files, commands, IR manual-entry (manual-section)
  712. This is the newly approved
  713. "standard" file format for 
  714. image processing work
  715. done by the
  716. .I alv
  717. group at Bristol University.
  718. The file consists of a 256-byte
  719. header followed by
  720. .I ysize
  721. rows each
  722. .I xsize
  723. in length.
  724. The header consists of a
  725. two-byte value for
  726. .IR xsize ,
  727. a two-byte value for
  728. .IR ysize ,
  729. and a 252-byte title,
  730. which may be interpreted
  731. as appropriate by any tool.
  732. .\".SH OPTIONS
  733. .\" Itemised list of options
  734. .\".SH FILES
  735. .\" List of all files used by the program
  736. .SH "SEE ALSO"
  737. .\" List of references, textual, and MAN pages.
  738. alv(1),alv(3)
  739. .\".SH DIAGNOSTICS
  740. .\" List of error messages and return codes the user may expect
  741. .\".SH BUGS
  742. .\" Known Limitations or Desirable additions to the command
  743. End of alv.5
  744.  echo alvlib.c 1>&2
  745. cat >alvlib.c <<'End of alvlib.c'
  746. #include <stdio.h> 
  747. #include "image.h" 
  748.  
  749. int winxsize=0,winysize=0,
  750.     winxoff=0, winyoff=0;
  751. int helpflag;
  752. char *newtitle=0;
  753.  
  754. IMAGE *readimage(fp)
  755. FILE *fp;
  756. {
  757.     int i, j, xsize, ysize;
  758.     char title[252];
  759.     IMAGE *img, *newimage();
  760.  
  761.     i=getw(fp);
  762.  
  763.     for(j=0;j<252;j++)
  764.         title[j]=getc(fp);
  765.  
  766.     img = newimage(xsize=(i>>16),ysize=(i&65535),title);
  767.  
  768.     for(j=0;j<ysize;j++)
  769.         for(i=0;i<xsize;i++)
  770.             (img->image[j][i])=getc(fp);     /* UGH! */
  771.  
  772.     return img;
  773. }
  774.  
  775. writeimage(fp,image)
  776. FILE *fp;
  777. IMAGE *image;
  778. {
  779.     int i,j;
  780.  
  781.     putw(image->xsize<<16|image->ysize,fp);
  782.  
  783.     for(j=0;j<252;j++)
  784.         putc(image->title[j],fp);
  785.  
  786.     for(j=0;j<image->ysize;j++)
  787.         for(i=0;i<image->xsize;i++)
  788.             putc(image->image[j][i],fp);
  789.  
  790. }
  791.  
  792. IMAGE *newimage(xsize,ysize,title)
  793. int xsize,ysize;
  794. char *title;
  795. {
  796. IMAGE *img;
  797. int i;
  798.  
  799.     if ((img=(IMAGE *)malloc(sizeof(IMAGE)))==NULL)
  800.         return 0;
  801.     if ((img->image = (unsigned char **) dynamem (&(img->image), sizeof(unsigned char), 2, ysize, xsize))==NULL)
  802.         return 0;
  803.     img->xsize=xsize;
  804.     img->ysize=ysize;
  805.     for (i=0;i<252;i++)
  806.         img->title[i]=0;
  807.     strcpy(img->title,title);
  808.     return img;
  809. }
  810.  
  811. subsample(image1,image2)
  812. IMAGE *image1,*image2;
  813. {
  814. int cvx,cvy,lcx,lcy,hcx,hcy;
  815. register int i,j,i1,j1;
  816. int total;
  817.  
  818.     cvx=(100*image1->xsize)/image2->xsize;
  819.     cvy=(100*image1->ysize)/image2->ysize;
  820.  
  821.     for (j=0;j<image2->ysize;j++)
  822.         for (i=0;i<image2->xsize;i++)
  823.         {
  824.             hcx=i*cvx/100;
  825.             hcy=j*cvy/100;
  826.             lcx=(i-1)*cvx/100+1;
  827.             lcy=(j-1)*cvy/100+1;
  828.             lcx=(lcx<0)?0:(lcx>hcx)?hcx:lcx;
  829.             lcy=(lcy<0)?0:(lcy>hcy)?hcy:lcy;
  830.  
  831.             total=0;
  832.             for (j1=lcy;j1<=hcy;j1++)
  833.                 for (i1=lcx;i1<=hcx;i1++)
  834.                     total+=image1->image[j1][i1];
  835.             image2->image[j][i]=total/((hcx+1-lcx)*(hcy+1-lcy));
  836.         }
  837. }
  838.  
  839. subarea(image1,image2,xoff,yoff)
  840. IMAGE *image1,*image2;
  841. int xoff,yoff;
  842. {
  843. int xmin,ymin,
  844.     xmax,ymax;
  845. int i,j;
  846.  
  847.     xmin=(xoff<0)?0:xoff;
  848.     ymin=(yoff<0)?0:yoff;
  849.     xmax=image2->xsize+xoff;
  850.     ymax=image2->ysize+yoff;
  851.     xmax=(xmax>image1->xsize)?image1->xsize:xmax;
  852.     ymax=(ymax>image1->ysize)?image1->ysize:ymax;
  853.  
  854.     for (j=ymin;j<ymax;j++)
  855.         for (i=xmin;i<xmax;i++)
  856.             image2->image[j-yoff][i-xoff]=image1->image[j][i];
  857. }
  858.  
  859. stdopts(argc,argv)
  860. char **argv;
  861. int *argc;
  862. {
  863. int testflag;
  864. char **p;
  865.  
  866.     helpflag=0;
  867.     for (argv++;*argv;argv++)
  868.         if (**argv=='-') {
  869.             testflag=1;
  870.             switch ((*argv)[1]) {
  871.             case 'H': helpflag = 1;
  872.                       break;
  873.             case 'T': newtitle= *argv+2;
  874.                       break;
  875.             default:
  876.                 testflag=0;
  877.             }
  878.             if (testflag)
  879.             {
  880.                 for (p=argv;*p;p++)
  881.                     p[0] = p[1];
  882.                 (*argc)--;
  883.                 argv--;
  884.             }
  885.         }
  886. }
  887.  
  888. windopts(argc,argv)
  889. char **argv;
  890. int *argc;
  891. {
  892. int testflag;
  893. char **p;
  894.  
  895.     for (argv++;*argv;argv++)
  896.         if (**argv=='-') {
  897.             testflag=1;
  898.             switch ((*argv)[1]) {
  899.             case 'X': winxsize=atoi(*argv+2);
  900.                       break;
  901.             case 'Y': winysize=atoi(*argv+2);
  902.                       break;
  903.             case 'x': winxoff=atoi(*argv+2);
  904.                       break;
  905.             case 'y': winyoff=atoi(*argv+2);
  906.                       break;
  907.             default:
  908.                 testflag=0;
  909.             }
  910.             if (testflag)
  911.             {
  912.                 for (p=argv;*p;p++)
  913.                     p[0] = p[1];
  914.                 (*argc)--;
  915.                 argv--;
  916.             }
  917.         }
  918. }
  919. End of alvlib.c
  920.  echo avg1 1>&2
  921. cat >avg1 <<'End of avg1'
  922. 7 7
  923.  
  924. 1 1 1 1 1 1 1
  925. 1 1 1 1 1 1 1
  926. 1 1 1 1 1 1 1
  927. 1 1 1 1 1 1 1
  928. 1 1 1 1 1 1 1
  929. 1 1 1 1 1 1 1
  930. 1 1 1 1 1 1 1
  931. End of avg1
  932.  echo cdsp.c 1>&2
  933. cat >cdsp.c <<'End of cdsp.c'
  934. #include <suntool/sunview.h>
  935. #include <suntool/canvas.h>
  936. #include "image.h"
  937. #include <stdio.h>
  938.  
  939. int boxsize = 1, mflag = 0;
  940. static void getcoords();
  941. static void repaint_canvas();
  942. IMAGE *image;
  943.  
  944. main(argc, argv)
  945. int argc;
  946. char **argv;
  947.  
  948. {
  949.     Frame frame;
  950.     Canvas canvas;
  951.     Pixwin *pw;
  952.     Cursor cursor;
  953.     char cmsname[CMS_NAMESIZE];
  954.     u_char red[256], green[256], blue[256];
  955.     int i, j, x, y;
  956.     FILE *fp, *fopen();
  957.     u_char *pi;
  958.     char *imagefile=NULL;
  959.  
  960.     stdopts(&argc,argv);
  961.     windopts(&argc,argv);
  962.  
  963.     for (argv++;*argv;*argv++)
  964.         if (**argv=='-' && (*argv)[1]=='m') 
  965.             mflag = 1;
  966.         else imagefile= *argv;
  967.  
  968.     if (imagefile==NULL)
  969.         fp=stdin;
  970.     else if (!(fp = fopen(imagefile,"r"))) {
  971.         fprintf(stderr,"Cannot open %s\n",imagefile);
  972.         exit(-1);
  973.         }
  974.  
  975.     image = readimage(fp);
  976.  
  977.     /* Initialise variables used to set colour map segment */
  978.     for (i = 0; i < 256; i++) {
  979.         red[i] = i;
  980.         green[i] = i;
  981.         blue[i] = i;
  982.     }
  983.  
  984.     cursor = cursor_create(CURSOR_CROSSHAIR_COLOR, 255,
  985.                    CURSOR_SHOW_CURSOR, FALSE,
  986.                    CURSOR_SHOW_CROSSHAIRS, TRUE,
  987.                    CURSOR_CROSSHAIR_LENGTH, 20,
  988.                    CURSOR_CROSSHAIR_GAP, 5,
  989.                    0);
  990.  
  991.     /* create frame and canvas */
  992.     frame = window_create(NULL, FRAME,
  993.                   FRAME_LABEL, (newtitle)?newtitle:image->title,
  994.                   WIN_X, winxoff,
  995.                   WIN_Y, winyoff,
  996.                   WIN_HEIGHT, (winxsize)?winxsize:image->ysize * boxsize + 22,
  997.                   WIN_WIDTH, (winysize)?winysize:image->xsize * boxsize + 10,
  998.                   0);
  999.  
  1000.     if (mflag)
  1001.         canvas = window_create(frame, CANVAS,
  1002.                        CANVAS_HEIGHT, image->ysize * boxsize,
  1003.                        CANVAS_WIDTH, image->xsize * boxsize,
  1004.                        WIN_CURSOR, cursor,
  1005.                        WIN_EVENT_PROC, getcoords,
  1006.                        0);
  1007.     else
  1008.         canvas = window_create(frame, CANVAS,
  1009.                        CANVAS_HEIGHT, image->ysize * boxsize,
  1010.                        CANVAS_WIDTH, image->xsize * boxsize,
  1011.                        WIN_CURSOR, cursor,
  1012.                        WIN_EVENT_PROC, getcoords,
  1013.                        CANVAS_RETAINED, FALSE,
  1014.                        CANVAS_REPAINT_PROC, repaint_canvas,
  1015.                        0);
  1016.  
  1017.     /* get the canvas pixwin to draw into */
  1018.     pw = canvas_pixwin(canvas);
  1019.  
  1020.     /* Set colour map segment */
  1021.     pw_setcmsname(pw, "grays");
  1022.     pw_putcolormap(pw, 0, 256, red, green, blue);
  1023.  
  1024.     if (mflag) {
  1025.         /* Enable batching for efficiency */
  1026.         pw_batch_on(pw);
  1027.  
  1028.         /* Draw gray boxes */
  1029.         pi = image->image[0];
  1030.         for (y = 0; y < image->ysize; y++)
  1031.             for (x = 0; x < image->xsize; x++) {
  1032.                 pw_put(pw, x, y, *pi);
  1033.                 pi++;
  1034.             }
  1035.         pw_batch_off(pw);
  1036.     }
  1037.  
  1038.     window_main_loop(frame);
  1039. }
  1040.  
  1041. static void
  1042. repaint_canvas(canvas, pw, repaint_area)
  1043. Canvas canvas;
  1044. Pixwin *pw;
  1045. Rectlist *repaint_area;
  1046. {
  1047.     u_char *pi;
  1048.     int x, y;
  1049.     int xstart, ystart, xstop, ystop;
  1050.  
  1051.     xstart = repaint_area->rl_bound.r_left / boxsize;
  1052.     ystart = repaint_area->rl_bound.r_top / boxsize;
  1053.     xstop = xstart + 1 + repaint_area->rl_bound.r_width / boxsize;
  1054.     ystop = ystart + 1 + repaint_area->rl_bound.r_height / boxsize;
  1055.  
  1056.     xstop = (xstop > image->xsize) ? image->xsize : xstop;
  1057.     ystop = (ystop > image->ysize) ? image->ysize : ystop;
  1058.  
  1059.     /* Enable locking for efficiency */
  1060.  
  1061.     /* Draw gray boxes */
  1062.     for (y = ystart; y < ystop; y++) {
  1063.         pi = &(image->image[y][xstart]);
  1064.         pw_lock(pw, &(repaint_area->rl_bound));
  1065.         for (x = xstart; x < xstop; x++) {
  1066.             /* Fill in a square box */
  1067.             pw_put(pw, x, y, *pi);
  1068.             pi++;
  1069.         }
  1070.         pw_unlock(pw);
  1071.     }
  1072. }
  1073.  
  1074. static void
  1075. getcoords(canvas, event, arg)
  1076. Canvas canvas;
  1077. Event *event;
  1078. caddr_t arg;
  1079. {
  1080.     if ((event_id(event) == MS_LEFT) && event_is_down(event)) {
  1081.         printf("%s: locator x,y : %d %d \n", (newtitle)?newtitle:image->title,
  1082.                event_x(event) / boxsize, event_y(event) / boxsize);
  1083.     }
  1084. }
  1085. End of cdsp.c
  1086.  echo coltable.c 1>&2
  1087. cat >coltable.c <<'End of coltable.c'
  1088. #include <suntool/sunview.h>
  1089. #include <suntool/canvas.h>
  1090. #include <stdio.h>
  1091. #include <math.h>
  1092.  
  1093. #define MINC 0
  1094.  
  1095. main(argc,argv)
  1096. int argc;
  1097. char ** argv;
  1098.  
  1099.    {
  1100.     Frame frame;
  1101.     Canvas canvas;
  1102.     Pixwin *pw;
  1103.     char cmsname[CMS_NAMESIZE];
  1104.     u_char red[256], green[256], blue[256];
  1105.     int i,ii,j,x,y;
  1106.     int delt,rem,region;
  1107.     int range ;
  1108.     float factor;
  1109.     char *tabname;
  1110.  
  1111.     range = 255 - MINC;
  1112.     delt = 43;
  1113.     factor = ((double)range)/(delt-1);
  1114.     tabname = (argv[0][0] == 'i') ? "icols" : "cols";
  1115.  
  1116.     /* Initialise variables used to set colour map segment */
  1117.     for (ii=0 ; ii<256 ; ii++)
  1118.     {
  1119.        i = (argv[0][0] == 'i') ? 255 - ii : ii ;
  1120.  
  1121.        region = ii/delt;
  1122.        rem = ii % delt;
  1123.  
  1124.        switch (region) {
  1125.  
  1126.        case 0 :
  1127.           red[i]   =  range +MINC ;
  1128.           green[i] =  range - rem* factor +MINC ;
  1129.           blue[i]  =  range - rem* factor +MINC;
  1130.           break;
  1131.  
  1132.        case 1 :
  1133.           red[i] = range +MINC;
  1134.           green[i] = rem * factor +MINC ;
  1135.           blue[i] = 0 +MINC ;
  1136.           break;
  1137.  
  1138.        case 2:
  1139.           red[i] = range - rem* factor +MINC ;
  1140.           green[i] = range +MINC ;
  1141.           blue[i] = 0 +MINC ;
  1142.           break;
  1143.  
  1144.        case 3:
  1145.           red[i] = 0 +MINC ;
  1146.           green[i] = range +MINC ;
  1147.           blue[i] = rem * factor +MINC ;
  1148.           break;
  1149.    
  1150.        case 4:
  1151.           red[i] = 0 +MINC ;
  1152.           green[i] = range - rem * factor +MINC ;
  1153.           blue[i] = range +MINC ;
  1154.           break;
  1155.  
  1156.        case 5:
  1157.        default:
  1158.           red[i] = rem * factor +MINC ;
  1159.           blue[i] = range +MINC ;
  1160.           green[i] = 0 +MINC ;
  1161.           break;
  1162.  
  1163.        } /* end switch */
  1164.  
  1165.     /* cludge to give more yellow etc */
  1166.     red[i] = sqrt((double)(red[i]-MINC))*sqrt((double)range) +MINC;
  1167.     blue[i] = sqrt((double)(blue[i]-MINC))*sqrt((double)range) +MINC;
  1168.     green[i] = sqrt((double)(green[i]-MINC))*sqrt((double)range) +MINC;
  1169.     }
  1170.  
  1171.     /* create frame and canvas */
  1172.     frame = window_create(NULL, FRAME,
  1173.     WIN_HEIGHT,    50,
  1174.     WIN_WIDTH,     100,
  1175.     0);
  1176.     canvas = window_create(frame,CANVAS, 0);
  1177.  
  1178.  
  1179.     /* get the cancas pixwin to draw into */
  1180.     pw = canvas_pixwin(canvas);
  1181.  
  1182.     /* Set colour map segment */
  1183.     pw_setcmsname(pw, tabname);
  1184.     pw_putcolormap(pw, 0, 256, red, green, blue);
  1185.  
  1186.     pw_batch_on(pw);
  1187.  
  1188.     window_main_loop(frame);
  1189.  
  1190.     exit(0);
  1191.    }
  1192. End of coltable.c
  1193.  echo convert.c 1>&2
  1194. cat >convert.c <<'End of convert.c'
  1195. #include <stdio.h>
  1196.  
  1197. main(argc,argv)
  1198. int argc;
  1199. char **argv;
  1200. {
  1201.     int i,j;
  1202.     int xsize,ysize;
  1203.     unsigned char **pic;
  1204.     char *file, *title;
  1205.     FILE *fp;
  1206.  
  1207.     if (argc!=5) {
  1208.         fprintf(stderr,"Usage: %s: x y filename title\n",argv[0]);
  1209.         exit(-1);
  1210.         }
  1211.  
  1212.     xsize=atoi(argv[1]);
  1213.     ysize=atoi(argv[2]);
  1214.     file =argv[3];
  1215.     title =argv[4];
  1216.  
  1217.     if (!(fp=fopen(file,"r"))) {
  1218.         fprintf(stderr,"Cannot open %s\n",file);
  1219.         exit(-1);
  1220.         }
  1221.  
  1222.     pic = (unsigned char **) dynamem (&pic, sizeof(unsigned char), 2, ysize, xsize);
  1223.  
  1224.     putw(xsize<<16|ysize,stdout);
  1225.  
  1226.     for (i=0;i<252;i++,*title?title++:0)
  1227.         putchar(*title?*title:0);
  1228.     
  1229.     for (i=0;i<xsize;i++)
  1230.         for (j=0;j<ysize;j++)
  1231.             pic[j][i]=getc(fp);
  1232.  
  1233.     for (j=0;j<ysize;j++)
  1234.         for(i=0;i<xsize;i++)
  1235.             putchar(pic[j][i]);
  1236.     
  1237.     fclose(fp);
  1238. }
  1239. End of convert.c
  1240.  echo convolve.c 1>&2
  1241. cat >convolve.c <<'End of convolve.c'
  1242. #include <stdio.h>
  1243. #include "image.h"
  1244.  
  1245. char *filtername=NULL;
  1246. int eflag = 0,
  1247.     fflag = 0;
  1248.  
  1249. typedef struct {
  1250.     char **image;
  1251.     short xsize,ysize;
  1252.     char title[252];
  1253. } FILTER;
  1254.  
  1255. main(argc,argv)
  1256. char **argv;
  1257. {
  1258.     char *imagefile=NULL;
  1259.     FILE *fp;
  1260.     IMAGE *image, *outimage;
  1261.     FILTER *filter, *loadfilter();
  1262.  
  1263.     stdopts(&argc,argv);
  1264.  
  1265.     if (helpflag) {
  1266.         help(*argv);
  1267.         exit(0);
  1268.     }
  1269.     
  1270.     for(argv++;*argv;argv++) 
  1271.         if ((**argv)=='-')
  1272.             switch ((*argv)[1]) {
  1273.             case 'e':
  1274.                 eflag=1;
  1275.                 break;
  1276.             case 'f':
  1277.                 filtername=argv[0]+2;
  1278.                 break;
  1279.             case 'F':
  1280.                 fflag=1;
  1281.                 break;
  1282.             }
  1283.         else imagefile= *argv;
  1284.     
  1285.     if (fflag) 
  1286.         execlp("ls","ls","-C","FILTERS_DIR");
  1287.     
  1288.     if (filtername) 
  1289.         filter=loadfilter(filtername);
  1290.     
  1291.     if (imagefile) {
  1292.         if (!(fp=fopen(imagefile,"r"))) {
  1293.             fprintf(stderr,"Couldn't open %s\n",imagefile);
  1294.             exit(-1);
  1295.         }
  1296.     }
  1297.     else fp = stdin;
  1298.     
  1299.     image=readimage(fp);
  1300.     outimage=newimage(image->xsize,image->ysize,(newtitle)?newtitle:image->title);
  1301.     if (filtername) {
  1302.         strcat(outimage->title," | ");
  1303.         strcat(outimage->title,filtername);
  1304.     }
  1305.  
  1306.     runfilter(filter,image,outimage);
  1307.  
  1308.     writeimage(stdout,outimage);
  1309. }
  1310.  
  1311. FILTER *
  1312. loadfilter(name)
  1313. char *name;
  1314. {
  1315.     char buf[BUFSIZ];
  1316.     FILE *fp;
  1317.     FILTER *filter;
  1318.     int xsize,ysize,ival;
  1319.     register int i,j;
  1320.  
  1321.     strcpy(buf,"FILTERS_DIR");
  1322.     strcat(buf,"/");
  1323.     strcat(buf,name);
  1324.  
  1325.     if (!(fp=fopen(buf,"r"))) {
  1326.         fprintf(stderr,"Couldn't open filter %s\n",name);
  1327.         exit(-1);
  1328.     }
  1329.     
  1330.     fscanf(fp,"%d %d",&xsize,&ysize);
  1331.     filter=(FILTER *)newimage(xsize,ysize,name);
  1332.  
  1333.     for(j=0;j<ysize;j++)
  1334.         for(i=0;i<xsize;i++) {
  1335.             fscanf(fp,"%d",&ival);
  1336.             filter->image[j][i]=ival;
  1337.         }
  1338.     
  1339.     return filter;
  1340. }
  1341.  
  1342. runfilter(filter,image,outimage)
  1343. IMAGE *image, *outimage;
  1344. FILTER *filter;
  1345. {
  1346.     int nflag = 0,
  1347.         scale,
  1348.         total;
  1349.     short max= -32768,
  1350.           min=  32767,
  1351.           **tempimage;
  1352.     int mfy = filter->ysize>>1,
  1353.         mfx = filter->xsize>>1;
  1354.     register int fi,fj,i,j;
  1355.     int sneg=0,
  1356.         spos=0,
  1357.         fred;
  1358.  
  1359.     for(j=0;j<filter->ysize;j++)
  1360.         for(i=0;i<filter->xsize;i++)
  1361.             if (filter->image[j][i]<0) {
  1362.                 nflag=1;
  1363.                 sneg -= filter->image[j][i];
  1364.             }
  1365.             else spos += filter->image[j][i];
  1366.  
  1367.     scale = (sneg > spos) ? sneg : spos;
  1368.     scale = (nflag) ? scale*2 : scale;
  1369.  
  1370.     if (!eflag)
  1371.         for(j=0;j<image->ysize-filter->ysize;j++)
  1372.             for(i=0;i<image->xsize-filter->ysize;i++) {
  1373.                 total=0;
  1374.                 for(fj=0;fj<filter->ysize;fj++)
  1375.                     for(fi=0;fi<filter->xsize;fi++) {
  1376.                         fred = image->image[j+fj][i+fi];
  1377.                         total += fred * filter->image[fj][fi];
  1378.                     }
  1379.                 outimage->image[j+mfy][i+mfx] = total / scale + ((nflag) ? 128 : 0);
  1380.             }
  1381.     else {
  1382.         if (!(tempimage=(short **)
  1383.                 dynamem(&tempimage,sizeof(short),2,image->ysize,image->xsize))) {
  1384.             fprintf(stderr,"Get some more VM !!!\n");
  1385.             exit(-1);
  1386.         }
  1387.         for(j=0;j<image->ysize-filter->ysize;j++)
  1388.             for(i=0;i<image->xsize-filter->ysize;i++) {
  1389.                 total=0;
  1390.                 for(fj=0;fj<filter->ysize;fj++)
  1391.                     for(fi=0;fi<filter->xsize;fi++) {
  1392.                         fred = image->image[j+fj][i+fi];
  1393.                         total += filter->image[fj][fi] * fred;
  1394.                     }
  1395.                 tempimage[j+mfy][i+mfx] = total;
  1396.                 max=(max>total)?max:total;
  1397.                 min=(min<total)?min:total;
  1398.             }
  1399.  
  1400.         scale=(max>-min)?max:-min;
  1401.         scale=(nflag)?scale*2:scale;
  1402.  
  1403.         for (j=0; j< image->ysize; j++)
  1404.             for (i=0; i<image->xsize;i++)
  1405.                 outimage->image[j][i]=tempimage[j][i]*256/scale+((nflag)?128:0);
  1406.     }
  1407. }
  1408.  
  1409. help(name)
  1410. char *name;
  1411. {
  1412.     printf("Convolution Filter\n");
  1413.     printf("------------------\n");
  1414.     printf("\nUsage: %s [file]\n",name);
  1415.     printf("\nOptions:\n");
  1416.     printf("  -F       list filters\n");
  1417.     printf("  -ffilter use filter\n");
  1418.     printf("  -e       enhanced scaling\n");
  1419.     printf("  -h       help\n");
  1420. }
  1421. End of convolve.c
  1422.  echo defs.h 1>&2
  1423. cat >defs.h <<'End of defs.h'
  1424. #define YES     1
  1425. #define NO   0
  1426. #define FILTERS_DIR        "/usr/aloha/alv/everson/usr/filters"
  1427. #define ERROR(S)    { fprintf(stderr,"%s: %s\n",progname,S); exit(1); }
  1428. #define FIL_MAX     31            /* max dimension of a filter */
  1429. #define XSIZE        256
  1430. #define    YSIZE        240
  1431.  
  1432. int filter[FIL_MAX][FIL_MAX];            /* filter array */
  1433. char progname[10];        /* program name */
  1434. short old_maxch,        /* max pixel value in the picture */
  1435.       old_minch,        /* min pixel value in the picture */
  1436.       maxch,            /* max pixel value in image */
  1437.       minch,            /* min pixel value in image */
  1438.       abs_minch;        /* min absolute pixel value in image */
  1439.  
  1440. unsigned char **pic,
  1441.               **image;;
  1442. short **eimage;
  1443.  
  1444. int pic_trunc;                /* truncation value */
  1445. int xsize,
  1446.     ysize,
  1447.     m_fil,                /* height of filter */
  1448.     n_fil,                /* width of filter */
  1449.     fil_scale;            /* scaling factor used to scale image to character
  1450.                            values after running the filter */
  1451. extern int negfilter,
  1452.            enhanced;
  1453. End of defs.h
  1454.  echo dsp.c 1>&2
  1455. cat >dsp.c <<'End of dsp.c'
  1456. #include <suntool/sunview.h>
  1457. #include <suntool/canvas.h>
  1458. #include <stdio.h>
  1459. #include "image.h"
  1460. #include "pattern3.h"
  1461.  
  1462. int boxsize = 3;
  1463. static void getcoords();
  1464. static void repaint_canvas();
  1465. IMAGE *image;
  1466. int *p_pat;
  1467. int mflag = 0;
  1468. Pixrect *pattern;
  1469.  
  1470. main(argc, argv)
  1471. int argc;
  1472. char **argv;
  1473. {
  1474.     Frame frame;
  1475.     Canvas canvas;
  1476.     Pixwin *pw;
  1477.     char cmsname[CMS_NAMESIZE];
  1478.     int i, j, x, y;
  1479.     FILE *fp, *fopen();
  1480.     u_char *pi;
  1481.     char *imagefile=NULL;
  1482.  
  1483.     stdopts(&argc,argv);
  1484.     windopts(&argc,argv);
  1485.  
  1486.     for (argv++;*argv;*argv++)
  1487.         if (*argv[0]=='-' && (*argv)[1]=='m') 
  1488.             mflag = 1;
  1489.         else imagefile= *argv;
  1490.  
  1491.     if (imagefile==NULL)
  1492.         fp=stdin;
  1493.     else if (!(fp = fopen(imagefile,"r"))) {
  1494.         fprintf(stderr,"Cannot open %s\n",imagefile);
  1495.         exit(-1);
  1496.         }
  1497.     image = readimage(fp);
  1498.  
  1499.     /* create frame and canvas */
  1500.     frame = window_create(NULL, FRAME,
  1501.                   FRAME_LABEL, (newtitle)?newtitle:image->title,
  1502.                   WIN_X, winxoff,
  1503.                   WIN_Y, winyoff,
  1504.                   WIN_HEIGHT, (winxsize)?winxsize:image->ysize * boxsize + 22,
  1505.                   WIN_WIDTH, (winysize)?winysize:image->xsize * boxsize + 10,
  1506.                   0);
  1507.  
  1508.  
  1509.     if (mflag)
  1510.         canvas = window_create(frame, CANVAS,
  1511.                        CANVAS_HEIGHT, image->ysize * boxsize,
  1512.                        CANVAS_WIDTH, image->xsize * boxsize,
  1513.                        WIN_EVENT_PROC, getcoords,
  1514.                        0);
  1515.     else
  1516.         canvas = window_create(frame, CANVAS,
  1517.                        CANVAS_FAST_MONO, TRUE,
  1518.                        CANVAS_HEIGHT, image->ysize * boxsize,
  1519.                        CANVAS_WIDTH, image->xsize * boxsize,
  1520.                        WIN_EVENT_PROC, getcoords,
  1521.                        CANVAS_RETAINED, FALSE,
  1522.                        CANVAS_REPAINT_PROC, repaint_canvas,
  1523.                        0);
  1524.  
  1525.  
  1526.     /* get the canvas pixwin to draw into */
  1527.     pw = canvas_pixwin(canvas);
  1528.  
  1529.     /* create dot pattern */
  1530.     pattern = mem_create(boxsize, boxsize * 50, 1);
  1531.     p_pat = &pat[0];
  1532.     for (y = 0; y < boxsize * 10; y++)
  1533.         for (x = 0; x < boxsize; x++, p_pat++)
  1534.             pr_rop(pattern, x, y, 1, 1,
  1535.                    (*p_pat) ? PIX_CLR : PIX_SET,
  1536.                    (Pixrect *) 0, 0, 0);
  1537.  
  1538.     if (mflag) {
  1539.         /* Draw gray boxes */
  1540.         for (y = 0; y < image->ysize; y++) {
  1541.             pi = image->image[y];
  1542.             for (x = 0; x < image->xsize; x++) {
  1543.                 /* Fill in a square box */
  1544.                 pw_rop(pw, x * boxsize, y * boxsize,
  1545.                        boxsize, boxsize,
  1546.                        PIX_SRC, pattern, 0, boxsize * ((*pi + 26) / 27));
  1547.                 pi++;
  1548.             }
  1549.         }
  1550.     }
  1551.  
  1552.     window_main_loop(frame);
  1553. }
  1554.  
  1555. static void
  1556. repaint_canvas(canvas, pw, repaint_area)
  1557. Canvas canvas;
  1558. Pixwin *pw;
  1559. Rectlist *repaint_area;
  1560.  
  1561. {
  1562.     u_char *pi;
  1563.     int x, y;
  1564.     int xstart, ystart, xstop, ystop;
  1565.  
  1566.     xstart = repaint_area->rl_bound.r_left / boxsize;
  1567.     ystart = repaint_area->rl_bound.r_top / boxsize;
  1568.     xstop = xstart + 1 + repaint_area->rl_bound.r_width / boxsize;
  1569.     ystop = ystart + 1 + repaint_area->rl_bound.r_height / boxsize;
  1570.  
  1571.     xstop = (xstop > image->xsize) ? image->xsize : xstop;
  1572.     ystop = (ystop > image->ysize) ? image->ysize : ystop;
  1573.  
  1574.     /* Enable locking for efficiency */
  1575.  
  1576.     /* Draw gray boxes */
  1577.     for (y = ystart; y < ystop; y++) {
  1578.         pi = &(image->image[y][xstart]);
  1579.         pw_lock(pw, &(repaint_area->rl_bound));
  1580.         for (x = xstart; x < xstop; x++) {
  1581.             /* Fill in a square box */
  1582.             pw_rop(pw, x * boxsize, y * boxsize,
  1583.                    boxsize, boxsize,
  1584.               PIX_SRC, pattern, 0, boxsize * ((*pi + 26) / 27));
  1585.             pi++;
  1586.         }
  1587.         pw_unlock(pw);
  1588.     }
  1589. }
  1590.  
  1591. static void
  1592. getcoords(canvas, event, arg)
  1593. Canvas canvas;
  1594. Event *event;
  1595. caddr_t arg;
  1596.  
  1597. {
  1598.     if ((event_id(event) == MS_LEFT) && event_is_down(event)) {
  1599.         printf("%s: locator x,y : %d %d  greylevel = %d\n", (newtitle)?newtitle:image->title,
  1600.                event_x(event) / boxsize, event_y(event) / boxsize,
  1601.          image->image[event_y(event) / boxsize][event_x(event) / boxsize]);
  1602.     }
  1603. }
  1604. End of dsp.c
  1605.  echo dynamem.3 1>&2
  1606. cat >dynamem.3 <<'End of dynamem.3'
  1607. .\" .TH name section cent-foot left-foot cent-head
  1608. .TH DYNAMEM 3 "28 August 1987"
  1609. .SH NAME
  1610. .\" name \- function
  1611. dynamem, freeup \- multidimensional dynamic array handling
  1612. .SH SYNOPSIS
  1613. .\" Bold keywords, Italic variables, [] options, | alternatives.
  1614. .br
  1615. .B char *dynamem(pointer,element_size,
  1616. .br
  1617. .B                 number_dimensions,dimensions ...)
  1618. .br
  1619. .B char **pointer;
  1620.  
  1621. .br
  1622. .B freeup(pointer)
  1623. .br
  1624. .B char *pointer;
  1625.  
  1626. .SH DESCRIPTION
  1627. .\" Italic files, commands, IR manual-entry (manual-section)
  1628. .I dynamem 
  1629. is the multidimensional analogue to malloc().
  1630. It is passed a number of arguments: a pointer which on
  1631. exiting the procedure will point to the begining of the
  1632. array, the element size, the number of dimensions 
  1633. required, followed by a list of the dimension sizes. 
  1634. To declare a 4 dimensional array normally one would code:
  1635. .DS L
  1636.  
  1637. int array[10][11][12][13];
  1638.  
  1639. .DE
  1640. however, this array is then fixed at compile time. This
  1641. same array can be declared dynamically at run time 
  1642. using the following code:
  1643. .DS L
  1644.  
  1645. int ****array;
  1646.  
  1647. array = (int ****) dynamem(&array, sizeof(int), 4, 10, 11, 12, 13);
  1648.  
  1649. .DE
  1650. (Note that the number of levels of indirection in the cast
  1651. is equal to the number of dimensions in the array.)
  1652. This enables array sizes to be fixed via, for example, 
  1653. command line arguments. 
  1654. .PP
  1655. .I freeup
  1656. is the 
  1657. .I dynamem
  1658. analogue to free(). When passed an array previously 
  1659. dynamically declared by 
  1660. .I dynamem
  1661. the function returns this memory to the system.
  1662. .PP
  1663. .I dynamem
  1664. attempts to set up the array required in the same way that 
  1665. it would be set up by the compiler at compile time. Thus
  1666. a multidimensional dynamically array declared using 
  1667. .I dynamem
  1668. can be used in exactly the same way as a fixed array declared
  1669. by the compiler. There is obviously some overhead in the actual
  1670. setting up of the array; however, this is minimal: 
  1671. when dynamically allocating 2 arrays of 346000
  1672. unsigned characters and one of the same number of shorts all in
  1673. two dimensions, the run time of a convolution of a 7x7 Lapacian-
  1674. Marr filter over an image of size 720 by 480 varied as follows:
  1675. .sp 1
  1676. time convolve -fbfilt -X720 -Y480 -e < bubble2 > test.1
  1677. .br
  1678.       222.0 real       213.4 user         1.6 sys  
  1679. .sp 1
  1680. time convolve -fbfilt -e < bubble2 > test.2
  1681. .br
  1682.       225.2 real       212.5 user         2.7 sys  
  1683. .sp 1
  1684. which is probably adequate. From this we can see that 
  1685. it takes 1.1 secs for the fixed array to be set up
  1686. and zeroed and only 0.9 secs for the array to be
  1687. dynamically declared using
  1688. .IR dynamem ; 
  1689. however, using dynamem the array is not initialized to
  1690. 0 and this is the reason for the 0.2 speed increase.
  1691. .SH FILES
  1692. .\" List of all files used by the program
  1693. /users/alv/everson/usr/lib/dynamem.a
  1694. .SH "SEE ALSO"
  1695. .\" List of references, textual, and MAN pages.
  1696. malloc(3), convolve(1)
  1697. .SH DIAGNOSTICS
  1698. .\" List of error messages and return codes the user may expect
  1699. .br
  1700. .I dynamem
  1701. returns NULL if it is unable to allocate sufficient
  1702. memory for the array.
  1703. End of dynamem.3
  1704.  echo dynamem.c 1>&2
  1705. cat >dynamem.c <<'End of dynamem.c'
  1706.  
  1707. /*
  1708.  * dynamem allocates a d dimensional array, whose dimensions are stored in a
  1709.  * list starting at d1. Each array element is of size s. p is a pointer with
  1710.  * d levels of indirection to the memory area 
  1711.  */
  1712.  
  1713. char *
  1714. dynamem(p, s, d, d1)
  1715. char **p;
  1716. int s, d, d1;
  1717. {
  1718.     int max,        /* size of array to be declared */
  1719.     *q;            /* pointer to dimension list */
  1720.     char **r,        /* pointer to begining of the array of the
  1721.                  * pointers for a dimension */
  1722.     **s1, *t, *tree;    /* base pointer to begining of first array */
  1723.     int i,            /* loop counters */
  1724.      j;
  1725.  
  1726.     r = &tree;
  1727.     q = &d1;        /* first dimension */
  1728.     max = 1;
  1729.     for (i = 0; i < d - 1; i++, q++) {    /* for each of the dimensions
  1730.                          * but the last */
  1731.         max *= (*q);
  1732.         if ((r[0] = (char *) malloc((unsigned) max * sizeof (char **))) == 0) {
  1733.             freeup(tree);
  1734.             return 0;
  1735.         }
  1736.         r = (char **) r[0];    /* step through to begining of next
  1737.                      * dimension array */
  1738.     }
  1739.     max *= s * (*q);    /* grab actual array memory */
  1740.     if ((r[0] = (char *) malloc((unsigned) max)) == 0) {
  1741.         freeup(tree);
  1742.         return 0;
  1743.     }
  1744.  
  1745.     /*
  1746.      * r is now set to point to the begining of each array so that we can
  1747.      * use it to scan down each array rather than having to go across and
  1748.      * then down 
  1749.      */
  1750.     r = (char **) tree;    /* back to the begining of list of arrays */
  1751.     q = &d1;        /* back to the first dimension */
  1752.     max = 1;
  1753.     for (i = 0; i < d - 2; i++, q++) {    /* we deal with the last
  1754.                          * array of pointers later on */
  1755.         max *= (*q);    /* number of elements in this dimension */
  1756.         for (j = 1, s1 = r + 1, t = r[0]; j < max; j++)    /* scans down array for
  1757.                                  * first and subsequent
  1758.                                  * elements */
  1759.  
  1760.             /*
  1761.              *  modify each of the pointers so that it points to
  1762.              * the correct position (sub-array) of the next
  1763.              * dimension array. s1 is the current position in the
  1764.              * current array. t is the current position in the
  1765.              * next array. t is incremented before s is, but it
  1766.              * starts off one behind. *(q+1) is the dimension of
  1767.              * the next array. 
  1768.              */
  1769.             *s1++ = (t += sizeof (char **) * *(q + 1));
  1770.         r = (char **) r[0];    /* step through to begining of next
  1771.                      * dimension array */
  1772.     }
  1773.     max *= (*q);        /* max is total number of elements in the
  1774.                  * last pointer array */
  1775.     for (j = 1, s1 = r + 1, t = r[0]; j < max; j++)    /* same as previous
  1776.                              * loop, but different
  1777.                              * size factor */
  1778.         *s1++ = (t += s * *(q + 1));
  1779.     return tree;        /* return base pointer */
  1780. }
  1781.  
  1782. /*
  1783.  * freeup releases all memory that we have already declared analogous to
  1784.  * free() when using malloc() 
  1785.  */
  1786. freeup(r)
  1787. char *r;
  1788. {
  1789.     char **p;
  1790.  
  1791.     for (p = &r; p; p = (char **) *p)
  1792.         free(*p);
  1793. }
  1794. End of dynamem.c
  1795.  echo ffill.c 1>&2
  1796. cat >ffill.c <<'End of ffill.c'
  1797. #include <stdio.h>
  1798. #include "image.h"
  1799.  
  1800. IMAGE *image;
  1801.  
  1802. main(argc,argv)
  1803. char **argv;
  1804. {
  1805.     FILE *fp;
  1806.     int xpos = -1, ypos = -1;
  1807.     int boundary = 0;
  1808.     char *imagefile=NULL;
  1809.  
  1810.     stdopts(&argc,argv);
  1811.  
  1812.     for (argv++;*argv;*argv++)
  1813.         if (**argv=='-')
  1814.             switch ((*argv)[1]) {
  1815.             case 'b':
  1816.                 boundary=atoi(argv[0]+2);
  1817.                 break;
  1818.             }
  1819.         else if (xpos<0)
  1820.             xpos=atoi(*argv+2);
  1821.         else if (ypos<0)
  1822.             ypos=atoi(*argv+2);
  1823.         else
  1824.             imagefile = *argv;
  1825.  
  1826.     if (imagefile==NULL)
  1827.         fp=stdin;
  1828.     else if (!(fp=fopen(imagefile,"r"))) {
  1829.         fprintf(stderr,"Cannot open %s\n",imagefile);
  1830.         exit(-1);
  1831.         }
  1832.  
  1833.     image=readimage(fp);
  1834.  
  1835.     if (!newtitle)
  1836.         sprintf(image->title,"%s | ffill %d %d %d\n",image->title,xpos,ypos,boundary);
  1837.     else
  1838.         strcpy(image->title,newtitle);
  1839.     ffill(xpos,ypos,boundary);
  1840.  
  1841.     writeimage(stdout,image);
  1842.     
  1843. }
  1844.  
  1845. ffill(x,y,n)
  1846. {
  1847. int i,j,k;
  1848.  
  1849.     if (x<0 || x>=image->xsize || y<0 || y>=image->ysize|| ftest(x,y)==n)
  1850.         return;
  1851.     fset (x,y,n);
  1852.     for (i=x-1;i>=0;i--) {
  1853.         if (ftest(i,y)!=n)
  1854.             fset(i,y,n);
  1855.         else 
  1856.             break;
  1857.         }
  1858.     for (j=x+1;j<image->xsize;j++)
  1859.         if (ftest(j,y)!=n)
  1860.             fset(j,y,n);
  1861.         else 
  1862.             break;
  1863.     for (k=i+1;k<j;k++)
  1864.     {
  1865.         if (y>0 && ftest(k,y-1)!=n)
  1866.             ffill(k,y-1,n);
  1867.         if (y<image->ysize-1 && ftest(k,y+1)!=n)
  1868.             ffill(k,y+1,n);
  1869.     }
  1870. }
  1871.  
  1872. ftest(x,y)
  1873. {
  1874.     return image->image[y][x];
  1875. }
  1876.  
  1877. fset(x,y,n)
  1878. {
  1879.     image->image[y][x]=n;
  1880. }
  1881.  
  1882. End of ffill.c
  1883.  echo hist.c 1>&2
  1884. cat >hist.c <<'End of hist.c'
  1885. #include <suntool/sunview.h>
  1886. #include <suntool/canvas.h>
  1887. #include <stdio.h>
  1888. #include <math.h>
  1889. #include "image.h"
  1890.  
  1891. static short histicon[] = 
  1892. {
  1893. #include "hist.icon"
  1894. };
  1895. DEFINE_ICON_FROM_IMAGE(icon,histicon);
  1896.  
  1897. #define GREYLEVELS    256
  1898. #define HSIZE    128
  1899. #define HEADER    10
  1900. #define BOTTOM    20
  1901. #define BOXSIZE    3
  1902. #define MIN(a,b)    ((a) < (b) ? (a) : (b))
  1903. #define MAX(a,b)    ((a) > (b) ? (a) : (b))
  1904.  
  1905. IMAGE *image;
  1906. int vflag=1,
  1907.     lflag=0;
  1908. Frame frame;
  1909. Canvas canvas;
  1910. Menu menu;
  1911. Pixwin *pw;
  1912. int hist[GREYLEVELS],
  1913.     n=0;
  1914.  
  1915. main(argc,argv)
  1916. int argc;
  1917. char **argv;
  1918. {
  1919.     double mean(),
  1920.            lhist[GREYLEVELS],
  1921.            ldummy,
  1922.            lmaxh= -10000.0;
  1923.     register short i,j;
  1924.     int c,
  1925.         mo,
  1926.         len,
  1927.         dummy,
  1928.         maxh=0,
  1929.         maxc=0,
  1930.         minc=GREYLEVELS;
  1931.     Pixrect *pattern;
  1932.     char *imagefile=NULL;
  1933.     char message[BUFSIZ];
  1934.     FILE *fp;
  1935.  
  1936.     stdopts(&argc,argv);
  1937.     windopts(&argc,argv);
  1938.  
  1939.     for (argv++;*argv;*argv++)
  1940.         if (**argv=='-')
  1941.             switch ((*argv)[1]) {
  1942.             case 'l':
  1943.                 lflag=1;
  1944.                 break;
  1945.             case 'v':
  1946.                 vflag=0;
  1947.                 break;
  1948.             }
  1949.         else imagefile = *argv;
  1950.  
  1951.     if (imagefile==NULL)
  1952.         fp=stdin;
  1953.     else if (!(fp=fopen(imagefile,"r"))) {
  1954.         fprintf(stderr,"Cannot open %s\n",imagefile);
  1955.         exit(-1);
  1956.         }
  1957.  
  1958.     image=readimage(fp);
  1959.     
  1960.     for (i=0;i<GREYLEVELS;i++)
  1961.         hist[i]=0;
  1962.  
  1963.     for(j=0;j<image->ysize;j++) 
  1964.         for (i=0;i<image->xsize;i++) {
  1965.             c = MAX(image->image[j][i],0);
  1966.             c = MIN(c,GREYLEVELS);
  1967.             if (c>maxc) 
  1968.                 maxc = c;
  1969.             else if (c<minc) 
  1970.                 minc = c;
  1971.             hist[c]++;
  1972.             if (maxh<hist[c])
  1973.                 maxh=hist[c];
  1974.             n++;
  1975.         }
  1976.  
  1977.     fprintf(stderr,"mean pixel = %3g\n",mean());
  1978.     fprintf(stderr,"median pixel = %d\n",median());
  1979.     fprintf(stderr,"mode pixel = %d\n",mo=mode());
  1980.     fprintf(stderr,"max pixel = %d\nmin pixel = %d\n",maxc,minc);
  1981.  
  1982.     strcpy(message,(lflag==1)?"logarithmic histogram" : "histogram");
  1983.     strcat(message,": ");
  1984.     strcat(message,image->title);
  1985.  
  1986.        /* create frame and canvas */
  1987.        frame = window_create(NULL, FRAME,
  1988.              FRAME_LABEL, message, 
  1989.              WIN_X, winxoff,
  1990.              WIN_Y, winyoff,
  1991.              WIN_HEIGHT, (winxsize)?winxsize:HSIZE+28+HEADER+BOTTOM,
  1992.              WIN_WIDTH, (winysize)?winysize:(GREYLEVELS*BOXSIZE)+10,
  1993.              FRAME_ICON,&icon,
  1994.              0);
  1995.        canvas = window_create(frame, CANVAS,
  1996.               CANVAS_FAST_MONO, TRUE,
  1997.               CANVAS_HEIGHT, HSIZE+HEADER+BOTTOM,
  1998.               CANVAS_WIDTH, GREYLEVELS*BOXSIZE,
  1999.               0);
  2000.       
  2001.     /* get the cancas pixwin to draw into */
  2002.        pw = canvas_pixwin(canvas);
  2003.  
  2004.       /* Enable batching for efficiency */
  2005.        pw_batch_on(pw);
  2006.  
  2007.        if (!lflag) { 
  2008.            for (i = 0; i < GREYLEVELS; i++) {
  2009.             dummy = hist[i]*HSIZE/maxh;
  2010.             if (!dummy && hist[i]) 
  2011.                 dummy = 1;
  2012.              if (vflag) {
  2013.                 pw_vector(pw,i*BOXSIZE,HSIZE+HEADER-dummy,i*BOXSIZE,HSIZE+HEADER,PIX_SET,1);
  2014.                 pw_vector(pw,i*BOXSIZE+BOXSIZE,HSIZE+HEADER-dummy,i*BOXSIZE+BOXSIZE,HSIZE+HEADER,PIX_SET,1);
  2015.                 }
  2016.             pw_rop(pw,i*BOXSIZE,HSIZE+HEADER-dummy,BOXSIZE,1,PIX_SET,(Pixrect *)0,0,0);
  2017.                }    
  2018.         }
  2019.     else {
  2020.         for (i=0;i<GREYLEVELS;i++) {
  2021.             lhist[i] = log((double)hist[i]+1.0);        /* add 1 to avoid log(0) */
  2022.             if (lmaxh<lhist[i])
  2023.                 lmaxh=lhist[i];
  2024.             }
  2025.         for (i=0;i<GREYLEVELS;i++) {
  2026.             ldummy = lhist[i]*HSIZE /lmaxh; 
  2027.             if (dummy!=0.0 && lhist[i]==0.0)
  2028.                 ldummy = 1.0;
  2029.              if (vflag) {
  2030.                 pw_vector(pw,i*BOXSIZE,HSIZE+HEADER-(int)ldummy,i*BOXSIZE,HSIZE+HEADER,PIX_SET,1);
  2031.                 pw_vector(pw,i*BOXSIZE+BOXSIZE,HSIZE+HEADER-(int)ldummy,i*BOXSIZE+BOXSIZE,HSIZE+HEADER,PIX_SET,1);
  2032.                 }
  2033.             pw_rop(pw,i*BOXSIZE,HSIZE+HEADER-(int)ldummy,BOXSIZE,1,
  2034.             PIX_SET,(Pixrect *)0,0,0);
  2035.             }
  2036.         }
  2037.  
  2038.     /* print bottom axis */
  2039.     pw_vector(pw,0,HSIZE+HEADER,image->xsize*BOXSIZE,HSIZE+HEADER,PIX_SET,1);
  2040.     for(i=0;i<GREYLEVELS;i++) {
  2041.         len = (i%100==0) ? 10 :
  2042.               (i%50==0)  ? 8 :
  2043.               (i%10==0)  ? 6 :
  2044.               (i%2==0)   ? 5 : 0;
  2045.         if (len)
  2046.             pw_vector(pw,i*BOXSIZE,HSIZE+HEADER,i*BOXSIZE,HSIZE+HEADER+len,PIX_SET,1);
  2047.         }
  2048.  
  2049.     pw_batch_off(pw);
  2050.        window_main_loop(frame);
  2051.  
  2052.     exit(0);
  2053. }
  2054.  
  2055. double mean()
  2056. {
  2057.     int i;
  2058.     double m=0;
  2059.  
  2060.     for (i=0;i<GREYLEVELS;i++)  {
  2061.         m += hist[i]*i;    
  2062.         }
  2063.     return m/n;
  2064. }
  2065.     
  2066. median()
  2067. {
  2068.     int i=0,
  2069.         count=0;
  2070.     
  2071.     while (count<n/2) 
  2072.         count += hist[i++];
  2073.     return i-1;
  2074. }
  2075.  
  2076. mode()
  2077. {
  2078.     int i=0;
  2079.     int mo=0,
  2080.         m=0;
  2081.  
  2082.     for(i=0;i<GREYLEVELS;i++)
  2083.         if (hist[i]>m) {
  2084.             m=hist[i];
  2085.             mo=i;
  2086.             }
  2087.  
  2088.     return mo;
  2089. }
  2090.  
  2091. End of hist.c
  2092.  echo hist.icon 1>&2
  2093. cat >hist.icon <<'End of hist.icon'
  2094. /* Format_version=1, Width=64, Height=64, Depth=1, Valid_bits_per_item=16
  2095.  */
  2096.     0xFFFF,0xFFFF,0xFFFF,0xFFFF,0x8000,0x0000,0x0000,0x0001,
  2097.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2098.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2099.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2100.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2101.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2102.     0x8000,0x0000,0x2000,0x0001,0x8000,0x0000,0x2000,0x0001,
  2103.     0x8000,0x0000,0x2000,0x0001,0x8000,0x0000,0x2000,0x0001,
  2104.     0x8002,0x0800,0x2008,0x0001,0x8002,0x0800,0x2008,0x0001,
  2105.     0x8002,0x0800,0x2008,0x0001,0x8002,0x0800,0x2008,0x0001,
  2106.     0x800A,0x0A00,0x2008,0x8081,0x800A,0x0A00,0x2008,0x8081,
  2107.     0x800A,0x0A00,0x2008,0x8081,0x800A,0x0A00,0x2008,0x8081,
  2108.     0x800A,0x0A00,0x2808,0x8081,0x800A,0x0A00,0x2808,0x8081,
  2109.     0x800A,0x0A00,0x2808,0x8081,0x800A,0x0A00,0x2808,0x8081,
  2110.     0x800A,0x8A00,0x2888,0x8281,0x800A,0x8A00,0x2888,0x8281,
  2111.     0x800A,0x8A00,0x2888,0x8281,0x800A,0x8A00,0x2888,0x8281,
  2112.     0x800A,0x8A00,0xA8A8,0x8281,0x800A,0x8A00,0xA8A8,0x8281,
  2113.     0x800A,0x8A00,0xA8A8,0x8281,0x800A,0x8A02,0xA8A8,0x8A81,
  2114.     0x802A,0x8A8A,0xAAAA,0x8A81,0x802A,0x8A8A,0xAAAA,0x8A81,
  2115.     0x802A,0x8A8A,0xAAAA,0x8A81,0x802A,0x8A8A,0xAAAA,0x8A81,
  2116.     0x80AA,0xAAAA,0xAAAA,0xAA81,0x80AA,0xAAAA,0xAAAA,0xAA81,
  2117.     0x80AA,0xAAAA,0xAAAA,0xAA81,0x80FF,0xFFFF,0xFFFF,0xFF81,
  2118.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2119.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2120.     0xA010,0x0040,0x0000,0x0001,0xA000,0x0040,0x0000,0x0001,
  2121.     0xAC70,0x71F1,0xC34B,0x0E35,0xB210,0x8842,0x24CC,0x912B,
  2122.     0xA210,0x8042,0x2448,0x012B,0xA210,0x7042,0x2448,0x0F2B,
  2123.     0xA210,0x0842,0x2448,0x112B,0xA210,0x8842,0x24C8,0x112B,
  2124.     0xA210,0x7031,0xC348,0x0F2B,0x8000,0x0000,0x0040,0x0001,
  2125.     0x8000,0x0000,0x0440,0x0001,0x8000,0x0000,0x0380,0x0001,
  2126.     0x8000,0x0000,0x0000,0x0001,0x8000,0x0000,0x0000,0x0001,
  2127.     0x8000,0x0000,0x0000,0x0001,0xFFFF,0xFFFF,0xFFFF,0xFFFF
  2128. End of hist.icon
  2129.  echo image.h 1>&2
  2130. cat >image.h <<'End of image.h'
  2131. typedef struct img {
  2132.     unsigned char **image;
  2133.     short xsize, ysize;
  2134.     char title[252];
  2135. } IMAGE;
  2136.  
  2137. IMAGE *readimage(), *newimage();
  2138.  
  2139. extern int winxsize,winysize,
  2140.            winxoff, winyoff;
  2141. extern int helpflag;
  2142. extern char *newtitle;
  2143. End of image.h
  2144.  echo imagelw.c 1>&2
  2145. cat >imagelw.c <<'End of imagelw.c'
  2146. #include <stdio.h>
  2147. #include <ctype.h> 
  2148. #include "image.h"
  2149. #define MIDX        285             /* center of output page */
  2150. #define MIDY        396
  2151.  
  2152. IMAGE *image;
  2153.  
  2154. main (argc,argv)
  2155. int argc;
  2156. char **argv;
  2157. {
  2158.     int c,x=0,y=0,r=0;
  2159.     register int i,j;
  2160.     double sx=1.0,sy=1.0,atof();
  2161.     char *hex();
  2162.     char title[512],buf[512];
  2163.     FILE *fp,*p1;
  2164.     int sflag=0,mflag=0,oflag=0,cflag=0;
  2165.     char *imagefile=NULL;
  2166.     
  2167.     stdopts(&argc,argv);
  2168.     windopts(&argc,argv);
  2169.  
  2170.     for (argv++;*argv;*argv++)
  2171.         if (**argv=='-')
  2172.             switch ((*argv)[1]) {
  2173.             case 't':
  2174.                 sflag=1;
  2175.                 break;
  2176.             case 'm':
  2177.                 mflag=1;
  2178.                 break;
  2179.             case 'c':
  2180.                 cflag=1;
  2181.                 break;
  2182.             case '\0':
  2183.                 oflag=1;
  2184.                 break;
  2185.             }
  2186.         else
  2187.             imagefile = *argv;
  2188.  
  2189.     if (imagefile==NULL)
  2190.         fp=stdin;
  2191.     else if (!(fp=fopen(imagefile,"r"))) {
  2192.         fprintf(stderr,"Cannot open %s\n",imagefile);
  2193.         exit(-1);
  2194.         }
  2195.  
  2196.     image=readimage(fp);
  2197.     
  2198.     if (oflag || sflag)
  2199.         p1=stdout;
  2200.     else if((p1=popen(strcat("lpr -Plw -v",((mflag)?" -m":"")),"w"))==NULL) {
  2201.             fprintf(stderr,"Cannot open pipe to lpr\n");
  2202.             exit(-1);
  2203.             }
  2204.     
  2205.     fprintf(p1,"/picstr %d string def\n",image->xsize);
  2206.  
  2207.     if (image->title[0]) {
  2208.         fprintf(p1,"/Helvetica-Bold findfont\n");
  2209.         fprintf(p1,"12 scalefont setfont\n");
  2210.         fprintf(p1,"285 (%s) stringwidth pop 2 div sub 720 moveto\n",image->title);
  2211.         fprintf(p1,"(%s) show\n",image->title);
  2212.         }
  2213.     if (cflag) {
  2214.         sx=7.91*300/image->xsize;
  2215.         sy=8.5*300/image->ysize;                /* avoid title area */
  2216.         sx=sy=(sx>sy) ? sy:sx;
  2217.         x= -sx*image->xsize*72/300/2;
  2218.         y= -sy*image->ysize*72/300/2;
  2219.         }
  2220.     if (sflag) 
  2221.         fprintf(p1," 0 0 translate\n");
  2222.     else fprintf(p1,"%d %d translate\n",MIDX+x,MIDY-36+y);
  2223.  
  2224.     fprintf(p1,"%d %d scale\n",(int)(image->xsize/300.0*72.0*sx),(int)(image->ysize/300.0*72.0*sy));
  2225.     fprintf(p1,"%d %d 8\n",image->xsize,image->ysize);
  2226.     fprintf(p1,"[ %d 0 0 -%d 0 %d ]\n",image->xsize,image->ysize,image->ysize);
  2227.     fprintf(p1,"{currentfile\npicstr readhexstring pop}\nimage\n");
  2228.  
  2229.     for(j=0;j<image->ysize;j++)
  2230.         for(i=0;i<image->xsize;i++)
  2231.             fputs(hex(image->image[j][i]),p1);
  2232.  
  2233.     if (!sflag)
  2234.         fprintf(p1,"showpage\n");
  2235.     if (!(oflag || sflag))
  2236.         pclose(p1);
  2237. }
  2238.  
  2239. char *hex(d)
  2240. {
  2241.     static char a[10];
  2242.  
  2243.     sprintf(a,"%02x",d);
  2244.     return(a);
  2245. }
  2246. End of imagelw.c
  2247.  echo invert.c 1>&2
  2248. cat >invert.c <<'End of invert.c'
  2249. #include <stdio.h>
  2250. #include "image.h"
  2251.  
  2252. main(argc,argv)
  2253. char **argv;
  2254. {
  2255.     int c;
  2256.     register int i,j;
  2257.     char *imagefile=NULL;
  2258.     IMAGE *image;
  2259.     FILE *fp;
  2260.     
  2261.     stdopts(&argc,argv);
  2262.     if (argc-1>=1)
  2263.         imagefile= *++argv;
  2264.  
  2265.     if (imagefile==NULL)
  2266.         fp=stdin;
  2267.     else if (!(fp=fopen(imagefile,"r"))) {
  2268.         fprintf(stderr,"Cannot open %s\n",imagefile);
  2269.         exit(-1);
  2270.         }
  2271.  
  2272.     image=readimage(fp);
  2273.  
  2274.     if (!newtitle)
  2275.         strcat(image->title," | invert");
  2276.     else
  2277.         strcpy(image->title,newtitle);
  2278.  
  2279.     for(j=0;j<image->ysize;j++)
  2280.         for(i=0;i<image->xsize;i++)
  2281.             image->image[j][i]=abs(255 - image->image[j][i]);
  2282.  
  2283.  
  2284.     writeimage(stdout,image);
  2285.     fclose(fp);
  2286. }
  2287. End of invert.c
  2288.  echo lap1 1>&2
  2289. cat >lap1 <<'End of lap1'
  2290. 3 3
  2291.  
  2292.  0 -1  0
  2293. -1  4 -1
  2294.  0 -1  0
  2295. End of lap1
  2296.  echo normalise.c 1>&2
  2297. cat >normalise.c <<'End of normalise.c'
  2298. #include <stdio.h>
  2299. #include "image.h"
  2300.  
  2301. #define GREYLEVELS     256
  2302.  
  2303. IMAGE *image;
  2304.  
  2305. main(argc, argv)
  2306. char **argv;
  2307. {
  2308.  
  2309.     int c, low= -1, high= -1;
  2310.     char buff[BUFSIZ];
  2311.     register int i,j;
  2312.     char *imagefile = NULL;
  2313.     FILE *fp;
  2314.  
  2315.     stdopts(&argc, argv);
  2316.  
  2317.     imagefile = (*argv++)?*argv:NULL;
  2318.  
  2319.     if (imagefile == NULL)
  2320.         fp = stdin;
  2321.     else if (!(fp = fopen(imagefile, "r"))) {
  2322.         fprintf(stderr, "Cannot open %s\n", imagefile);
  2323.         exit(-1);
  2324.     }
  2325.  
  2326.     image = readimage(fp);
  2327.  
  2328.     if (!newtitle)
  2329.         strcat(image->title," | normalise");
  2330.     else
  2331.         strcpy(image->title,newtitle);
  2332.  
  2333.     norm(image);
  2334.  
  2335.     writeimage(stdout,image);
  2336. }
  2337.  
  2338. norm(image)
  2339. IMAGE *image;
  2340. {
  2341.     register int i,j;
  2342.     int x;
  2343.     int totpix=image->xsize * image->ysize;
  2344.     int hist[GREYLEVELS][2];
  2345.     int sum;
  2346.  
  2347.     for (i = 0; i < GREYLEVELS; i++) 
  2348.         hist[i][0] = hist[i][1] = 0;
  2349.  
  2350.     for (j=0;j<image->ysize;j++)
  2351.         for (i=0;i<image->xsize;i++) 
  2352.             hist[image->image[j][i]][0]++;
  2353.  
  2354.     sum = 0;
  2355.     for (i = 0; i < GREYLEVELS; i++) {
  2356.         sum += hist[i][0];
  2357.         x = sum * GREYLEVELS/ totpix;
  2358.         x = (x > 255) ? 255 : x;
  2359.         hist[i][1] = x;
  2360.     }
  2361.  
  2362.     for(j=0;j<image->ysize;j++)
  2363.         for(i=0;i<image->xsize;i++)
  2364.             image->image[j][i]=hist[image->image[j][i]][1];
  2365. }
  2366. End of normalise.c
  2367.  echo pattern3.h 1>&2
  2368. cat >pattern3.h <<'End of pattern3.h'
  2369. /* file pattern.h */
  2370. int pat[] = 
  2371. {    0,0,0,
  2372.     0,0,0,
  2373.     0,0,0,
  2374.         0,0,0,
  2375.         0,1,0,
  2376.         0,0,0,
  2377.     0,0,0,
  2378.     1,0,1,
  2379.     0,0,0,
  2380.         0,0,1,
  2381.         0,1,0,
  2382.         1,0,0,
  2383.     0,1,0,
  2384.     1,0,1,
  2385.     0,1,0,
  2386.         1,0,1,
  2387.         0,1,0,
  2388.         1,0,1,
  2389.     1,1,1,
  2390.     0,1,0,
  2391.     1,0,1,
  2392.         1,1,1,
  2393.         1,1,0,
  2394.         1,0,1,
  2395.     1,1,1,
  2396.     1,0,1,
  2397.     1,1,1,
  2398.         1,1,1,
  2399.         1,1,1,
  2400.         1,1,1};
  2401. End of pattern3.h
  2402.  echo pixval.c 1>&2
  2403. cat >pixval.c <<'End of pixval.c'
  2404. #include <stdio.h>
  2405. #include <string.h>
  2406. #include "image.h"
  2407.  
  2408. IMAGE *image,
  2409.       *image1;
  2410.  
  2411. main (argc,argv)
  2412. int argc;
  2413. char **argv;
  2414. {
  2415.     register int i,j;
  2416.     char *imagefile=NULL;
  2417.     FILE *fp;
  2418.  
  2419.     stdopts(&argc,argv);
  2420.     windopts(&argc,argv);
  2421.  
  2422.     if (argc>1)
  2423.         imagefile = argv[1];
  2424.  
  2425.     if (imagefile==NULL)
  2426.         fp=stdin;
  2427.     else if (!(fp=fopen(imagefile,"r"))) {
  2428.         fprintf(stderr,"Cannot open %s\n",imagefile);
  2429.         exit(-1);
  2430.         }
  2431.  
  2432.     image=readimage(fp);
  2433.     image1=newimage(winxsize,winysize,"");
  2434.  
  2435.     subarea(image,image1,winxoff,winyoff);
  2436.     
  2437.     for (j=0;j<image1->ysize;j++) {
  2438.         for(i=0;i<image1->xsize;i++)
  2439.             printf("%4d",image1->image[j][i]);
  2440.         putchar('\n');
  2441.         }
  2442. }
  2443. End of pixval.c
  2444.  echo range.c 1>&2
  2445. cat >range.c <<'End of range.c'
  2446. #include <stdio.h>
  2447. #include "image.h"
  2448.  
  2449. IMAGE *image;
  2450.  
  2451. main(argc, argv)
  2452. char **argv;
  2453. {
  2454.  
  2455.     int c, low= 0, high= 255;
  2456.     char buff[BUFSIZ];
  2457.     register int i,j;
  2458.     char *imagefile = NULL;
  2459.     FILE *fp;
  2460.  
  2461.     stdopts(&argc, argv);
  2462.  
  2463.     for (argv++; *argv; *argv++)
  2464.         if (**argv == '-')
  2465.             switch ((*argv)[1]) {
  2466.             case 'l':
  2467.                 low = atoi(argv[0] + 2);
  2468.                 break;
  2469.             case 'h':
  2470.                 high = atoi(argv[0] + 2);
  2471.                 break;
  2472.             }
  2473.         else
  2474.             imagefile = *argv;
  2475.  
  2476.     if (imagefile == NULL)
  2477.         fp = stdin;
  2478.     else if (!(fp = fopen(imagefile, "r"))) {
  2479.         fprintf(stderr, "Cannot open %s\n", imagefile);
  2480.         exit(-1);
  2481.     }
  2482.  
  2483.     image = readimage(fp);
  2484.  
  2485.     if (!newtitle)
  2486.     {
  2487.         strcat(image->title," | range");
  2488.         sprintf(buff," -l%d",low);
  2489.         strcat(image->title,buff);
  2490.         sprintf(buff," -h%d",high);
  2491.         strcat(image->title,buff);
  2492.     }
  2493.     else
  2494.         strcpy(image->title,newtitle);
  2495.  
  2496.     for(j=0;j<image->ysize;j++)
  2497.         for(i=0;i<image->xsize;i++) {
  2498.             c = (image->image[j][i] - low) * 255 / (high - low);
  2499.             c = (c < 0) ? 0 : c;
  2500.             image->image[j][i]= (c > 255) ? 255 : c;
  2501.             }
  2502.  
  2503.     writeimage(stdout,image);
  2504. }
  2505. End of range.c
  2506.  echo retitle.c 1>&2
  2507. cat >retitle.c <<'End of retitle.c'
  2508. #include <stdio.h>
  2509. #include "image.h"
  2510.  
  2511. main(argc,argv)
  2512. char **argv;
  2513. {
  2514. IMAGE *image1,*image2;
  2515. FILE *fp;
  2516.  
  2517.     stdopts(&argc,argv);
  2518.     if (helpflag)
  2519.     {
  2520.         help(*argv);
  2521.         exit(0);
  2522.     }
  2523.     if (argc>1)
  2524.     {
  2525.         if (!(fp=fopen(argv[1],"r")))
  2526.         {
  2527.             fprintf(stderr,"%s: cannot open %s\n",argv[0],argv[1]);
  2528.             exit(-1);
  2529.         }
  2530.         image1=readimage(fp);
  2531.     }
  2532.     else
  2533.         image1=readimage(stdin);
  2534.  
  2535.     if (newtitle)
  2536.     {
  2537.         image2=newimage(image1->xsize,image1->ysize,newtitle);
  2538.         image2->image=image1->image;
  2539.         writeimage(stdout,image2);
  2540.     }
  2541.     else
  2542.         writeimage(stdout,image1);
  2543. }
  2544.  
  2545. help(name)
  2546. char *name;
  2547. {
  2548.     printf("Retitling Filter\n");
  2549.     printf("----------------\n");
  2550.     printf("\nUsage: %s [file]\n",name);
  2551.     printf("\nOptions:\n");
  2552.     printf("  -h      help\n");
  2553.     printf("  -ttitle new title\n");
  2554. }
  2555. End of retitle.c
  2556.  echo sobel1 1>&2
  2557. cat >sobel1 <<'End of sobel1'
  2558. 5 5
  2559.  
  2560.  1  2  0 -2 -1
  2561.  2  4  0 -4 -2
  2562.  2  6  0 -6 -2
  2563.  2  4  0 -4 -2
  2564.  1  2  0 -2 -1
  2565.  
  2566.  
  2567. End of sobel1
  2568.  echo striphead.c 1>&2
  2569. cat >striphead.c <<'End of striphead.c'
  2570. #include <stdio.h>
  2571. #include "image.h"
  2572.  
  2573. IMAGE *image;
  2574.  
  2575. main(argc,argv)
  2576. int argc;
  2577. char **argv;
  2578. {
  2579.     int i,j;
  2580.     int xsize,ysize;
  2581.     unsigned char **pic;
  2582.     char *file, *title;
  2583.     FILE *fp;
  2584.  
  2585.     stdopts(&argc,argv);
  2586.  
  2587.     file = (*argv++)?*argv:NULL;
  2588.  
  2589.     if (file==NULL) {
  2590.         if (!(fp=fopen(file,"r"))) {
  2591.             fprintf(stderr,"Cannot open %s\n",file);
  2592.             exit(-1);
  2593.             }
  2594.         }
  2595.     else fp=stdin;
  2596.  
  2597.     image=readimage(fp);
  2598.  
  2599.     for (j=0;j<image->ysize;j++)
  2600.         for (i=0;i<image->xsize;i++)
  2601.             putchar(image->image[j][i]);
  2602.  
  2603.     fclose(fp);
  2604. }
  2605. End of striphead.c
  2606.  echo subarea.c 1>&2
  2607. cat >subarea.c <<'End of subarea.c'
  2608. #include <stdio.h>
  2609. #include "image.h"
  2610.  
  2611. main(argc,argv)
  2612. char **argv;
  2613. {
  2614. FILE *fp;
  2615.  
  2616.     stdopts(&argc,argv);
  2617.     windopts(&argc,argv);
  2618.  
  2619.     if (helpflag)
  2620.     {
  2621.         help(*argv);
  2622.         exit(0);
  2623.     }
  2624.     if (argc<1)
  2625.     {
  2626.         fprintf(stderr,"Usage: %s [filename]\n",argv[0]);
  2627.         exit(-1);
  2628.     }
  2629.     if (winxoff<0 || winyoff<0 || winxsize<0 || winysize<0)
  2630.     {
  2631.         fprintf(stderr,"Negative Values not permitted !!\n");
  2632.         exit(-1);
  2633.     }
  2634.     if (argc==1)
  2635.         subar(stdin);
  2636.     else
  2637.         if (!(fp=fopen(argv[1],"r")))
  2638.         {
  2639.             fprintf(stderr,"%s: cannot open %s\n",argv[0],argv[1]);
  2640.             exit(-1);
  2641.         }
  2642.         else
  2643.         {
  2644.             subar(fp);
  2645.             fclose(fp);
  2646.         }
  2647. }
  2648.  
  2649. subar(fp)
  2650. FILE *fp;
  2651. {
  2652. IMAGE *image1,*image2;
  2653.  
  2654.     image1=readimage(fp);
  2655.  
  2656.     if (newtitle)
  2657.         image2=newimage(winxsize,winysize,newtitle);
  2658.     else
  2659.     {
  2660.         image2=newimage(winxsize,winysize,image1->title);
  2661.         strcat(image2->title," | subarea");
  2662.     }
  2663.  
  2664.     subarea(image1,image2,winxoff,winyoff);
  2665.  
  2666.     writeimage(stdout,image2);
  2667. }
  2668.  
  2669. help(name)
  2670. char *name;
  2671. {
  2672.     printf("Subsample Filter\n");
  2673.     printf("----------------\n");
  2674.     printf("\nUsage: %s -xn -yn -Xn -Yn [file]\n",name);
  2675.     printf("\nOptions:\n");
  2676.     printf("  -h      help\n");
  2677.     printf("  -ttitle new title\n");
  2678. }
  2679. End of subarea.c
  2680.  echo subsample.c 1>&2
  2681. cat >subsample.c <<'End of subsample.c'
  2682. #include <stdio.h>
  2683. #include "image.h"
  2684.  
  2685. main(argc,argv)
  2686. char **argv;
  2687. {
  2688. int newx,newy;
  2689. FILE *fp;
  2690.  
  2691.     stdopts(&argc,argv);
  2692.     if (helpflag)
  2693.     {
  2694.         help(*argv);
  2695.         exit(0);
  2696.     }
  2697.     if (argc<3)
  2698.     {
  2699.         fprintf(stderr,"Usage: %s newx newy [filename]\n",argv[0]);
  2700.         exit(-1);
  2701.     }
  2702.     newx=atoi(argv[1]);
  2703.     newy=atoi(argv[2]);
  2704.     if (newx<=0 || newy<=0)
  2705.     {
  2706.         fprintf(stderr,"Negative Values not permitted !!\n");
  2707.         exit(-1);
  2708.     }
  2709.     if (argc==3)
  2710.         subsamp(stdin,newx,newy);
  2711.     else
  2712.         if (!(fp=fopen(argv[3],"r")))
  2713.         {
  2714.             fprintf(stderr,"%s: cannot open %s\n",argv[0],argv[3]);
  2715.             exit(-1);
  2716.         }
  2717.         else
  2718.         {
  2719.             subsamp(fp,newx,newy);
  2720.             fclose(fp);
  2721.         }
  2722. }
  2723.  
  2724. subsamp(fp,x,y)
  2725. FILE *fp;
  2726. {
  2727. IMAGE *image1,*image2;
  2728.  
  2729.     image1=readimage(fp);
  2730.  
  2731.     if (newtitle)
  2732.         image2=newimage(x,y,newtitle);
  2733.     else
  2734.     {
  2735.         image2=newimage(x,y,image1->title);
  2736.         strcat(image2->title," | subsample");
  2737.     }
  2738.  
  2739.     subsample(image1,image2);
  2740.  
  2741.     writeimage(stdout,image2);
  2742. }
  2743.  
  2744. help(name)
  2745. char *name;
  2746. {
  2747.     printf("Subsample Filter\n");
  2748.     printf("----------------\n");
  2749.     printf("\nUsage: %s xsize ysize [file]\n",name);
  2750.     printf("\nOptions:\n");
  2751.     printf("  -h      help\n");
  2752.     printf("  -ttitle new title\n");
  2753. }
  2754. End of subsample.c
  2755.  echo thresh.c 1>&2
  2756. cat >thresh.c <<'End of thresh.c'
  2757. #include <math.h>
  2758. #include <stdio.h>
  2759. #include "image.h"
  2760.  
  2761. #define MAX 255
  2762. #define MIN 0
  2763. main(argc, argv)
  2764. int argc;
  2765. char **argv;
  2766. {
  2767.     register int i,j;
  2768.     char buff[BUFSIZ];
  2769.     int thresh;
  2770.     FILE *fp;
  2771.     IMAGE *image, *image2;
  2772.  
  2773.     stdopts(&argc,argv);
  2774.  
  2775.     if (argc>1 && argv[1][0]=='-' && argv[1][1]=='t')
  2776.         thresh = atoi(argv[1]+2);
  2777.     else
  2778.         thresh = -1;
  2779.     
  2780.     for (*argv++;*argv;argv++)
  2781.         if (**argv!='-')
  2782.             break;
  2783.  
  2784.     if (*argv)
  2785.     {
  2786.         if (!(fp=fopen(*argv,"r")))
  2787.         {
  2788.             fprintf(stderr,"Cannot Open %s\n",*argv);
  2789.             exit(-1);
  2790.         }
  2791.     }
  2792.     else
  2793.         fp=stdin;
  2794.  
  2795.     image=readimage(fp);
  2796.     fclose(fp);
  2797.     image2=newimage(image->xsize,image->ysize,(newtitle)?newtitle:image->title);
  2798.  
  2799.     if (!newtitle)
  2800.     {
  2801.         strcat(image2->title," | thresh");
  2802.         if (thresh >= 0) {
  2803.             sprintf(buff," -t%d",thresh);
  2804.             strcat(image2->title,buff); 
  2805.         }
  2806.     }
  2807.     else strcpy(image2->title,newtitle);
  2808.     
  2809.     if (thresh == -1)
  2810.         thresh=autothresh(image);
  2811.  
  2812.     for (j=0;j<image->ysize;j++)
  2813.         for (i=0;i<image->xsize;i++)
  2814.             image2->image[j][i]=image->image[j][i]<thresh?MIN:MAX;
  2815.  
  2816.     writeimage(stdout,image2);
  2817. }
  2818.  
  2819. autothresh(image)
  2820. IMAGE *image;
  2821. {
  2822.     register int x, y;
  2823.     int mean, sumx = 0, sumxsqrd = 0;
  2824.     double sd, nsd = 1.5;
  2825.  
  2826.     for (y = 0; y < image->ysize; y++) 
  2827.         for (x = 0; x < image->xsize; x++) {
  2828.             sumx += image->image[y][x];
  2829.             sumxsqrd += image->image[y][x] * image->image[y][x];
  2830.         }
  2831.  
  2832.     mean = sumx / (image->xsize * image->ysize);
  2833.     sd = sqrt((double) sumxsqrd / (image->xsize * image->ysize) - (mean * mean));
  2834.     x=nsd*sd;
  2835.     fprintf(stderr,"Threshold = %d\n",(int)(mean + x /* (nsd * sd)*/));
  2836.     return (int)(mean + x /*(nsd * sd)*/);
  2837. }
  2838. End of thresh.c
  2839.  echo xdiff1 1>&2
  2840. cat >xdiff1 <<'End of xdiff1'
  2841. 7 7
  2842.  
  2843. 1 1 1 0 -1 -1 -1
  2844. 1 1 1 0 -1 -1 -1
  2845. 1 1 1 0 -1 -1 -1
  2846. 1 1 1 0 -1 -1 -1
  2847. 1 1 1 0 -1 -1 -1
  2848. 1 1 1 0 -1 -1 -1
  2849. 1 1 1 0 -1 -1 -1
  2850. End of xdiff1
  2851.  
  2852.  
  2853.