home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3293 < prev    next >
Encoding:
Internet Message Format  |  1991-05-07  |  10.3 KB

  1. From: dag@gorgon.uucp (Daniel A. Glasser)
  2. Newsgroups: alt.sex.pictures.d,alt.sources
  3. Subject: yagcsp (Yet Another GIF Contact Sheet Program)
  4. Message-ID: <1991May06.033638.3758@gorgon.uucp>
  5. Date: 6 May 91 03:36:38 GMT
  6.  
  7. #!/bin/sh
  8. # This is a shell script.  It's not important, really, but if you strip
  9. # off everything that comes before these shell comments and feed the
  10. # result to sh (not csh) you should get the file csheet.c.  Note
  11. # that this is a hand-made shell archive and it will not protect you
  12. # against overwriting a file by the same name.  Use at your own risk.
  13. #
  14. echo Extracting csheet.c
  15. cat >csheet.c <<EndOfTerribleCProgram
  16. /* Csheet -- A program to make contact sheets out of .gif files.
  17.  
  18.     Version:    0.01
  19.     Author:        Daniel A. Glasser (dag%gorgon@persoft.com or
  20.                        dag@persoft.com)
  21.     Date:        May 5, 1991
  22.  
  23.     The author hereby places this program in the public domain.
  24.  
  25.     Anybody can do anything with this code, even clean it up and
  26.     call it h{er,is} own (though I'd appreciate some little credit
  27.     somewhere).  There is no warrenty expressed or implied in this
  28.     program.  The author assumes no liability for damage caused by
  29.     this program or by programmers who go berzerk after trying to
  30.     read this program.  You are on your own.
  31.  
  32.     The author admits that this program is awful.  Don't judge
  33.     his programming talent by the junk that's here.  I used to be
  34.     a competition programmer in college.  You learn to code fast
  35.     when maintainability and style don't matter.  I program for
  36.     a living, and what I write for work bears little or no
  37.     resemblence to this mess.  Please don't show this junk to
  38.     any beginning C programmer.  It's bad for their teeth.
  39.  
  40.     ---------------------------------------
  41.  
  42.     This program is a translation into C of the Bourne shell
  43.     script called "contact", posted to alt.sex.pictures.d
  44.     by Ron Schnell and modified by rekers@cwi.nl.  I had problems
  45.     with the original because I run on a Sys-V system with a
  46.     14 character file name limit.  The posted scripts immediately
  47.     ran afowl of this limit.  My modifications to the shell script
  48.     made it almost work, but things were still strange.  I hacked
  49.     this together in about an hour, and touched it up after some
  50.     debugging.  I'm posting it about 4 hours old.
  51.  
  52.     Note that this program differs from the original shell script
  53.     in several important ways --
  54.  
  55.     1)    It takes file name parameters rather than having a
  56.         hard coded source directory.
  57.     2)    The output and work directories can be changed without
  58.         recompiling through shell variables.
  59.     3)    ditto for the output file name
  60.     4)    ditto for the maximum image height
  61.     5)    It should take care of non-full last pages.
  62.  
  63.     This code is blecherous, uncommented garbage!  It doesn't catch
  64.     signals, have command line options, or create its own pipes.  It
  65.     uses "system()" for everything, and does not protect the user's
  66.     temporary files against other users' temporary files.  All of these
  67.     things could be added without too much difficulty, but I'm lazy.
  68.  
  69.     Usage:    csheet <gif_file_list>
  70.  
  71.     where <gif file list> is a space separated list of the files
  72.     (including paths if not in the current directory) to be included
  73.     in the contact sheet.  These files are expected to have the
  74.     extension ".gif", and if they don't, .gif is _not_ appended.
  75.  
  76.     A recent version of the PBMplus utility package (or at least
  77.     part of said package) must be accessable through the user's
  78.     search path (PATH) for system() to do its stuff.
  79.  
  80.     The program looks in the environment for four symbols,
  81.  
  82.         SHEETWORK    (defaults to /tmp)
  83.             The directory the work gets done in.  It
  84.             is a good idea to set this environment variable
  85.             to something other than /tmp, just in case someone
  86.             else is trying to run this program at the same
  87.             time you are.  There should be several megabytes
  88.             free in the file system containing this directory.
  89.  
  90.         SHEETDEST    (defaults to .)
  91.             The directory in which the resulting .gif file
  92.             will be placed.
  93.  
  94.         SHEETNAME    (defaults to sheet)
  95.             The first part of the name of the contact sheet
  96.             file.  It will have a two-digit sequence number
  97.             appended to it, followed by ".gif".
  98.  
  99.         SHEETHEIGHT    (defaults to 768)
  100.             This specifies the maximum height of the contact
  101.             sheet (in scan lines).
  102.  
  103.     You can override the default default values at compile time
  104.     by defining DEFWORK, DEFDEST, DEFPREFIX, and DEFHEIGHT, respectively.
  105.  
  106.     bugs:    If the files names are not in wildcard expansion order
  107.         in the argument list to the program, the labels on the
  108.         images might be wrong.
  109.  
  110.         I'm sure there are many more, I've not seen them yet.
  111.  
  112.     todo:    The program should take switches to override both the
  113.         defaults and the environment.  Someone who cares should
  114.         add this.
  115.  
  116.         It's rather difficult to interrupt this program...
  117.         Appropriate signals should be caught and the return
  118.         status from 'system()' should be examined so the
  119.         program can tell when a child has been interrupted
  120.         rather than just failed.  Some cleanup of the work
  121.         directory should be done.
  122.  
  123.         There is no need to use the basename of the .gif
  124.         file as part of the temporary file names.  A unique
  125.         prefix should be generated for each instance of this
  126.         program along with the sequence number of the file
  127.         in the command line, thereby eliminating a bunch of
  128.         problems with wild-card expansion order and conflicts
  129.         between concurrent executions of this program.
  130.  
  131. */
  132.  
  133. #include <stdio.h>
  134.  
  135. #ifndef    DEFWORK
  136. #define    DEFWORK    "/tmp"
  137. #endif
  138.  
  139. #ifndef    DEFDEST
  140. #define    DEFDEST    "."
  141. #endif
  142.  
  143. #ifndef    DEFPREFIX
  144. #define    DEFPREFIX "sheet"
  145. #endif
  146.  
  147. #ifndef    DEFHEIGHT
  148. #define    DEFHEIGHT 768
  149. #endif
  150.  
  151. char command[257];
  152. int atoi();
  153. char *strchr();
  154. char *strrchr();
  155. char *getenv();
  156. int strlen();
  157.  
  158. char *workdir;
  159. char *destdir;
  160. char *prefix;
  161. int  maxheight;
  162.  
  163. void panic(str)
  164. char *str;
  165. {
  166.     fprintf("Panic on command\n%s\nPlease clean up %s yourself.\n",
  167.         str, workdir);
  168.     exit(1);
  169. }
  170.  
  171. int basename(dest, src, len)
  172. char *dest;
  173. char *src;
  174. int len;
  175. {
  176.     char *x;
  177.     int l;
  178.  
  179.     while (x = strchr(src, '/'))
  180.         src = x + 1;
  181.  
  182.     if ((x = strrchr(src, '.')) == NULL)
  183.     {
  184.         x = src + strlen(src);
  185.     }
  186.  
  187.     for (l=0; l < len; ++l)
  188.     {
  189.         if ((src == x) || (*src == '\0'))
  190.             break;
  191.         *dest++ = *src++;
  192.     }
  193.     *dest = '\0';
  194.     return l;
  195. }
  196.  
  197. int getval(str, fld)
  198. char *str;
  199. int fld;
  200. {
  201.     --fld;
  202.     while(fld)
  203.     {
  204.         while ((*str != ' ') && (*str != '\t'))
  205.         {
  206.             if (*str == '\0')
  207.                 return 0;
  208.             ++str;
  209.         }
  210.         while ((*str == ' ') || (*str == '\t'))
  211.             ++str;
  212.         --fld;
  213.     }
  214.     if (*str == '\0')
  215.         return 0;
  216.     else
  217.         return atoi(str);
  218. }
  219.  
  220. void linedone(row, page)
  221. int row;
  222. int page;
  223. {
  224.     printf("Compiling row %d of contact sheet %d\n", row, page);
  225.     sprintf(command,
  226.         "pnmcat -white -lr %s/*.s* | ppmquant 256 > %s/row%d.ppm",
  227.             workdir, workdir, row * 2 - 1);
  228.     printf("%s\n", command);
  229.     if (system(command)) panic(command);
  230.     
  231.     sprintf(command,
  232.         "pnmcat -white -lr %s/fname? > %s/row%d.ppm",
  233.             workdir, workdir, row * 2);
  234.     printf("%s\n", command);
  235.     if (system(command)) panic(command);
  236.     sprintf(command,
  237.         "rm -f %s/*.s* %s/fname?", workdir, workdir);
  238.     printf("%s\n", command);
  239.     if (system(command)) panic(command);
  240. }
  241.  
  242. void pagedone(row, page)
  243. int row;
  244. int page;
  245. {
  246.     char result[128];
  247.     FILE *fp;
  248.     int height;
  249.  
  250.     printf("Assembling contact sheet %d\n", page);
  251.     sprintf(command,
  252.         "pnmcat -white -tb %s/row?.ppm > %s/page%02d.ppm",
  253.             workdir, workdir, page);
  254.     
  255.     printf("%s\n", command);
  256.     if (system(command)) panic(command);
  257.  
  258.     sprintf(command, "rm -f %s/row?.ppm", workdir);
  259.     printf("%s\n", command);
  260.     if (system(command)) panic(command);
  261.  
  262.     sprintf(command, "pnmfile %s/page%02d.ppm > %s/size",
  263.                 workdir, page, workdir);
  264.     printf("%s\n", command);
  265.     if (system(command)) panic(command);
  266.     sprintf(command, "%s/size", workdir);
  267.     if ((fp = fopen(command, "r")) == NULL)
  268.     {
  269.         perror(command);
  270.         fprintf(stderr, "Fatal error, you'll have to clean %s up\n",
  271.                 workdir);
  272.         exit(1);
  273.     }
  274.     fgets(result, 128, fp);
  275.     fclose(fp);
  276.     printf("rm %s\n", command);
  277.     unlink(command);
  278.     height = getval(result, 6);
  279.  
  280.     if (height == 0) height = maxheight + 1;
  281.  
  282.     printf("Page height is %d\n", height);
  283.     if (height > maxheight)
  284.     {
  285.         printf("Scaling down to %d scans.\n", maxheight);
  286.         sprintf(command,
  287. "pnmscale -ysize %d < %s/page%02d.ppm | ppmquant 256 | ppmtogif > %s/%s%02d.gif",
  288.             maxheight, workdir, page, destdir, prefix, page);
  289.     }
  290.     else
  291.     {
  292.         sprintf(command,
  293.         "ppmquant 256 < %s/page%02d.ppm | ppmtogif > %s/%s%02d.gif",
  294.             workdir, page, destdir, prefix, page);
  295.     }
  296.     printf("%s\n", command);
  297.     if (system(command)) panic(command);
  298.  
  299.     sprintf(command, "rm -f %s/page%02d.ppm", workdir, page);
  300.     printf("%s\n", command);
  301.     if (system(command)) panic(command);
  302.  
  303.     printf("++++  file %s/%s%02d.gif is complete!\n",
  304.              destdir, prefix, page);
  305. }
  306.  
  307. main(argc, argv)
  308. int argc;
  309. char **argv;
  310. {
  311.     char base[32];
  312.     char workfile[64];
  313.     char *tmp;
  314.     int curfile;
  315.     int curpage;
  316.     int currow;
  317.     int i;
  318.  
  319.     if (argc <= 1)
  320.     {
  321.         fprintf(stderr, "useage: %s list-of-gif-files\n",
  322.             argv[0]);
  323.         exit(1);
  324.     }
  325.     if ((workdir = getenv("SHEETWORK")) == NULL)
  326.         workdir = DEFWORK;
  327.     if ((destdir = getenv("SHEETDEST")) == NULL)
  328.         destdir = DEFDEST;
  329.     if ((prefix = getenv("SHEETNAME")) == NULL)
  330.         prefix = DEFPREFIX;
  331.  
  332.     if (tmp = getenv("SHEETHEIGHT"))
  333.     {
  334.         if ((maxheight = atoi(tmp)) <= 0)
  335.             maxheight = DEFHEIGHT;
  336.     }
  337.     else
  338.         maxheight = DEFHEIGHT;
  339.         
  340.     curfile = 0;
  341.     currow = 0;
  342.     curpage = 0;
  343.  
  344.     for (i=1; i<argc; ++i)
  345.     {
  346.         printf("Processing %s\n", argv[i]);
  347.         basename(base, argv[i], sizeof(base));
  348.         strcpy(workfile, workdir);
  349.         strcat(workfile, "/");
  350.         strcat(workfile, base);
  351.         strcat(workfile, ".spm");
  352.  
  353.         sprintf(command, 
  354. "giftoppm < %s | pnmscale -xsize 102 | ppmquant 256 > %s",
  355.                 argv[i], workfile);
  356.         printf("%s\n", command);
  357.         if (system(command) != 0)
  358.         {
  359.             printf("Error processing %s, skipping...", argv[i]);
  360.             unlink(workfile);
  361.             continue;
  362.         }
  363.         sprintf(command,
  364. "pbmtext %s.gif | pnmscale -xsize 102 -ysize 20 > %s/fname%d",
  365.                 base, workdir, curfile);
  366.         if (system(command)) panic(command);
  367.         ++curfile;
  368.  
  369.         if (curfile >= 5)
  370.         {
  371.             currow++;
  372.             curfile = 0;
  373.             linedone(currow, curpage);
  374.  
  375.             if (currow == 4)
  376.             {
  377.                 pagedone(currow, curpage);
  378.                 ++curpage;
  379.                 currow = 0;
  380.             } /* end of page done */
  381.         } /* end of row done */
  382.     } /* end of for () */
  383.     if (currow != 0 || curfile != 0)
  384.     {
  385.         currow++;
  386.         if (curfile != 0) linedone(currow, curpage);
  387.         pagedone(currow, curpage);
  388.         ++curpage;
  389.     }
  390.     printf("All done, %d pages produced from %d image files.\n",
  391.             curpage, argc - 1);
  392.     exit(0);
  393. }
  394. EndOfTerribleCProgram
  395. echo done extracting csheet.c
  396. exit 0
  397. -- 
  398. Daniel A. Glasser                       One of those things that goes
  399. dag%gorgon@persoft.com                  "BUMP! (ouch!)" in the night.
  400.