home *** CD-ROM | disk | FTP | other *** search
- Xref: wupost alt.sex.pictures.d:7004 alt.sources:3953 alt.binaries.pictures.d:712 alt.graphics.pixutils:1685
- Newsgroups: alt.sex.pictures.d,alt.sources,alt.binaries.pictures.d,alt.graphics.pixutils
- Path: wupost!zaphod.mps.ohio-state.edu!hobbes.physics.uiowa.edu!news.iastate.edu!pv7411.vincent.iastate.edu!edsall
- From: edsall@iastate.edu (David M Edsall)
- Subject: Improved contact sheet program
- Message-ID: <edsall.683774048@pv7411.vincent.iastate.edu>
- Summary: Will now check for existing contact sheets
- Keywords: C, pbmplus
- Sender: news@news.iastate.edu (USENET News System)
- Organization: Iowa State University, Ames IA
- Date: Mon, 2 Sep 1991 01:14:08 GMT
- Lines: 470
-
-
-
-
- This is an improved version of the contact sheet program written in C that
- was posted by Daniel Glasser in May. The original would not check to see if
- existing contact sheets of the same basename existed and it also would include
- these contacts in the newly generated contacts. I like this program because it
- runs faster than any of the shell scripts previously posted. The improvements
- I have included are
-
- 1) The user can set the initial page number of the set of contact sheets
- generated by setting the environment variable STARTPAGE
-
- 2) If STARTPAGE has not been set, the program will determine which contact
- sheets of the same basename exist and will count the number of these which
- exist. If we call that number "n", then the next page generated will have
- the suffix "n+1"
-
- 3) When compiling the contact sheets, this program will not include the
- previously generated contact sheets of the same name. This was useful for
- me in that I roll my images off onto tape but I like to keep an archive of
- what I have on disk. This way I do not have to keep moving my previously
- generated contacts back and forth between my image directory and some
- temporary directory while I compile the new contact sheets.
-
-
- I have included the shell script that I use for my contacts which sets the
- environment variables that are important for me. Note for readers who have
- never seen this before: you must have the pbmplus package to run this and
- this program uses UNIX procedures to get environment variable settings from
- within the program. It is therefore OS dependent but it would probably not
- be that hard to replace the "getenv" functions within the program with
- appropriate calls for your OS.
-
- - dave
-
-
- #!/bin/sh
- # to extract, remove the header and type "sh filename"
- if `test ! -s ./csheet.c`
- then
- echo "writing ./csheet.c"
- cat > ./csheet.c << '\End\Of\Shar\'
- /* Csheet -- A program to make contact sheets out of .gif files.
-
- Version: 0.02
- Original Author: Daniel A. Glasser (dag%gorgon@persoft.com or
- dag@persoft.com)
- Modified By: David M. Edsall (edsall@iastate.edu)
- Date: 1 September 1991
-
- The author hereby places this program in the public domain.
-
- Anybody can do anything with this code, even clean it up and
- call it h{er,is} own (though I'd appreciate some little credit
- somewhere). There is no warrenty expressed or implied in this
- program. The author assumes no liability for damage caused by
- this program or by programmers who go berzerk after trying to
- read this program. You are on your own.
-
- The author admits that this program is awful. Don't judge
- his programming talent by the junk that's here. I used to be
- a competition programmer in college. You learn to code fast
- when maintainability and style don't matter. I program for
- a living, and what I write for work bears little or no
- resemblence to this mess. Please don't show this junk to
- any beginning C programmer. It's bad for their teeth.
-
- ---------------------------------------
-
- This program is a translation into C of the Bourne shell
- script called "contact", posted to alt.sex.pictures.d
- by Ron Schnell and modified by rekers@cwi.nl. I had problems
- with the original because I run on a Sys-V system with a
- 14 character file name limit. The posted scripts immediately
- ran afowl of this limit. My modifications to the shell script
- made it almost work, but things were still strange. I hacked
- this together in about an hour, and touched it up after some
- debugging. I'm posting it about 4 hours old.
-
- Note that this program differs from the original shell script
- in several important ways --
-
- 1) It takes file name parameters rather than having a
- hard coded source directory.
- 2) The output and work directories can be changed without
- recompiling through shell variables.
- 3) ditto for the output file name
- 4) ditto for the maximum image height
- 5) It should take care of non-full last pages.
-
- This code is blecherous, uncommented garbage! It doesn't catch
- signals, have command line options, or create its own pipes. It
- uses "system()" for everything, and does not protect the user's
- temporary files against other users' temporary files. All of these
- things could be added without too much difficulty, but I'm lazy.
-
- Usage: csheet <gif_file_list>
-
- where <gif file list> is a space separated list of the files
- (including paths if not in the current directory) to be included
- in the contact sheet. These files are expected to have the
- extension ".gif", and if they don't, .gif is _not_ appended.
-
- A recent version of the PBMplus utility package (or at least
- part of said package) must be accessable through the user's
- search path (PATH) for system() to do its stuff.
-
- The program looks in the environment for four symbols,
-
- SHEETWORK (defaults to /tmp)
- The directory the work gets done in. It
- is a good idea to set this environment variable
- to something other than /tmp, just in case someone
- else is trying to run this program at the same
- time you are. There should be several megabytes
- free in the file system containing this directory.
-
- SHEETDEST (defaults to .)
- The directory in which the resulting .gif file
- will be placed.
-
- SHEETNAME (defaults to sheet)
- The first part of the name of the contact sheet
- file. It will have a two-digit sequence number
- appended to it, followed by ".gif".
-
- SHEETHEIGHT (defaults to 768)
- This specifies the maximum height of the contact
- sheet (in scan lines).
-
- STARTPAGE (defaults to 0 )
- This specifies the page to begin with
-
- You can override the default default values at compile time
- by defining DEFWORK, DEFDEST, DEFPREFIX, and DEFHEIGHT, respectively.
-
- bugs: If the files names are not in wildcard expansion order
- in the argument list to the program, the labels on the
- images might be wrong.
-
- I'm sure there are many more, I've not seen them yet.
-
- todo: The program should take switches to override both the
- defaults and the environment. Someone who cares should
- add this.
-
- It's rather difficult to interrupt this program...
- Appropriate signals should be caught and the return
- status from 'system()' should be examined so the
- program can tell when a child has been interrupted
- rather than just failed. Some cleanup of the work
- directory should be done.
-
- There is no need to use the basename of the .gif
- file as part of the temporary file names. A unique
- prefix should be generated for each instance of this
- program along with the sequence number of the file
- in the command line, thereby eliminating a bunch of
- problems with wild-card expansion order and conflicts
- between concurrent executions of this program.
-
- */
-
- #include <stdio.h>
-
- #ifndef DEFWORK
- #define DEFWORK "/tmp"
- #endif
-
- #ifndef DEFDEST
- #define DEFDEST "."
- #endif
-
- #ifndef DEFPREFIX
- #define DEFPREFIX "sheet"
- #endif
-
- #ifndef DEFHEIGHT
- #define DEFHEIGHT 768
- #endif
-
- char command[257];
- int atoi();
- char *strchr();
- char *strrchr();
- char *getenv();
- int strlen();
-
- char *workdir;
- char *destdir;
- char *prefix;
- int maxheight;
-
- void panic(str)
- char *str;
- {
- fprintf("Panic on command\n%s\nPlease clean up %s yourself.\n",
- str, workdir);
- exit(1);
- }
-
- int basename(dest, src, len)
- char *dest;
- char *src;
- int len;
- {
- char *x;
- int l;
-
- while (x = strchr(src, '/'))
- src = x + 1;
-
- if ((x = strrchr(src, '.')) == NULL)
- {
- x = src + strlen(src);
- }
-
- for (l=0; l < len; ++l)
- {
- if ((src == x) || (*src == '\0'))
- break;
- *dest++ = *src++;
- }
- *dest = '\0';
- return l;
- }
-
- int getval(str, fld)
- char *str;
- int fld;
- {
- --fld;
- while(fld)
- {
- while ((*str != ' ') && (*str != '\t'))
- {
- if (*str == '\0')
- return 0;
- ++str;
- }
- while ((*str == ' ') || (*str == '\t'))
- ++str;
- --fld;
- }
- if (*str == '\0')
- return 0;
- else
- return atoi(str);
- }
-
- void linedone(row, page)
- int row;
- int page;
- {
- printf("Compiling row %d of contact sheet %d\n", row, page);
- sprintf(command,
- "pnmcat -white -lr %s/*.s* | ppmquant 256 > %s/row%d.ppm",
- workdir, workdir, row * 2 - 1);
- printf("%s\n", command);
- if (system(command)) panic(command);
-
- sprintf(command,
- "pnmcat -white -lr %s/fname? > %s/row%d.ppm",
- workdir, workdir, row * 2);
- printf("%s\n", command);
- if (system(command)) panic(command);
- sprintf(command,
- "rm -f %s/*.s* %s/fname?", workdir, workdir);
- printf("%s\n", command);
- if (system(command)) panic(command);
- }
-
- void pagedone(row, page)
- int row;
- int page;
- {
- char result[128];
- FILE *fp;
- int height;
-
- printf("Assembling contact sheet %d\n", page);
- sprintf(command,
- "pnmcat -white -tb %s/row?.ppm > %s/page%02d.ppm",
- workdir, workdir, page);
-
- printf("%s\n", command);
- if (system(command)) panic(command);
-
- sprintf(command, "rm -f %s/row?.ppm", workdir);
- printf("%s\n", command);
- if (system(command)) panic(command);
-
- sprintf(command, "pnmfile %s/page%02d.ppm > %s/size",
- workdir, page, workdir);
- printf("%s\n", command);
- if (system(command)) panic(command);
- sprintf(command, "%s/size", workdir);
- if ((fp = fopen(command, "r")) == NULL)
- {
- perror(command);
- fprintf(stderr, "Fatal error, you'll have to clean %s up\n",
- workdir);
- exit(1);
- }
- fgets(result, 128, fp);
- fclose(fp);
- printf("rm %s\n", command);
- unlink(command);
- height = getval(result, 6);
-
- if (height == 0) height = maxheight + 1;
-
- printf("Page height is %d\n", height);
- if (height > maxheight)
- {
- printf("Scaling down to %d scans.\n", maxheight);
- sprintf(command,
- "pnmscale -ysize %d < %s/page%02d.ppm | ppmquant 256 | ppmtogif > %s/%s%02d.gif",
- maxheight, workdir, page, destdir, prefix, page);
- }
- else
- {
- sprintf(command,
- "ppmquant 256 < %s/page%02d.ppm | ppmtogif > %s/%s%02d.gif",
- workdir, page, destdir, prefix, page);
- }
- printf("%s\n", command);
- if (system(command)) panic(command);
-
- sprintf(command, "rm -f %s/page%02d.ppm", workdir, page);
- printf("%s\n", command);
- if (system(command)) panic(command);
-
- printf("\n++++ file %s/%s%02d.gif is complete!\n",
- destdir, prefix, page);
- }
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- char base[32];
- char workfile[64];
- char *tmp;
- int curfile;
- int totalfiles;
- int curpage;
- int totalpages;
- int currow;
- int i;
-
- if (argc <= 1)
- {
- fprintf(stderr, "useage: %s list-of-gif-files\n",
- argv[0]);
- exit(1);
- }
- if ((workdir = getenv("SHEETWORK")) == NULL)
- workdir = DEFWORK;
- if ((destdir = getenv("SHEETDEST")) == NULL)
- destdir = DEFDEST;
- if ((prefix = getenv("SHEETNAME")) == NULL)
- prefix = DEFPREFIX;
-
- if (tmp = getenv("SHEETHEIGHT"))
- {
- if ((maxheight = atoi(tmp)) <= 0)
- maxheight = DEFHEIGHT;
- }
- else
- maxheight = DEFHEIGHT;
-
- if (tmp = getenv("STARTPAGE"))
- {
- if ((curpage = atoi(tmp)) <= 0)
- curpage = 1;
- }
- else
- {
- curpage = 1;
- for (i=1; i<argc; ++i)
- {
- basename(base, argv[i], sizeof(base));
- if ( strncmp(base,prefix,strlen(prefix)) == 0 ) ++curpage;
- }
- }
-
- curfile = 0;
- currow = 0;
- totalpages = 0;
- totalfiles = 0;
-
- for (i=1; i<argc; ++i)
- {
- basename(base, argv[i], sizeof(base));
- if ( strncmp(base,prefix,strlen(prefix)) == 0 ) continue;
- printf("\nProcessing %s\n", argv[i]);
- strcpy(workfile, workdir);
- strcat(workfile, "/");
- strcat(workfile, base);
- strcat(workfile, ".spm");
-
- sprintf(command,
- "giftoppm < %s | pnmscale -xsize 102 | ppmquant 256 > %s",
- argv[i], workfile);
- printf("%s\n", command);
- if (system(command) != 0)
- {
- printf("Error processing %s, skipping...", argv[i]);
- unlink(workfile);
- continue;
- }
- sprintf(command,
- "pbmtext %s.gif | pnmscale -xsize 102 -ysize 20 > %s/fname%d",
- base, workdir, curfile);
- if (system(command)) panic(command);
- ++curfile;
- ++totalfiles;
-
- if (curfile >= 5)
- {
- currow++;
- curfile = 0;
- linedone(currow, curpage);
-
- if (currow == 4)
- {
- pagedone(currow, curpage);
- ++curpage;
- ++totalpages;
- currow = 0;
- } /* end of page done */
- } /* end of row done */
- } /* end of for () */
- if (currow != 0 || curfile != 0)
- {
- currow++;
- if (curfile != 0) linedone(currow, curpage);
- pagedone(currow, curpage);
- ++curpage;
- ++totalpages;
- }
- printf("\nAll done, %d pages produced from %d image files.\n",
- totalpages, totalfiles );
- exit(0);
- }
- \End\Of\Shar\
- else
- echo "will not over write ./csheet.c"
- fi
- if `test ! -s ./csheet.csh`
- then
- echo "writing ./csheet.csh"
- cat > ./csheet.csh << '\End\Of\Shar\'
- #
- setenv SHEETDEST /home/edsall/images/gif
- setenv SHEETNAME images
- setenv SHEETWORK ~/tmp
- setenv STARTPAGE 40
- $xbin/csheet $gif/*.gif
- exit
- \End\Of\Shar\
- else
- echo "will not over write ./csheet.csh"
- fi
- echo "Finished archive 1 of 1"
- exit
-
-
-