home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / sys / amiga / graphics / 5778 < prev    next >
Encoding:
Text File  |  1992-08-25  |  9.3 KB  |  447 lines

  1. Newsgroups: comp.sys.amiga.graphics
  2. Path: sparky!uunet!gumby!destroyer!gatech!taco!hgm
  3. From: hgm@ncsu.edu (Hal G. Meeks)
  4. Subject: Re: .DL anim viewer?
  5. Message-ID: <hgm.714784050@fbi.cc.ncsu.edu>
  6. Sender: news@ncsu.edu (USENET News System)
  7. Organization: North Carolina State University
  8. References: <1992Aug25.052621.24860@uwm.edu> <1992Aug25.201236.23217@cs.hut.fi>
  9. Date: Tue, 25 Aug 1992 23:07:30 GMT
  10. Lines: 435
  11.  
  12. ged@phantom.cs.hut.fi (Camillo S{rs) writes:
  13.  
  14. >In <1992Aug25.052621.24860@uwm.edu> jdblake@csd4.csd.uwm.edu (John David Blake) writes:
  15.  
  16. >>Does anyone out there know of an FTP site where I could get a .DL anim
  17. >>format viewer? (For Amiga, of course)
  18.  
  19. This is what I've been doing: 
  20.  
  21. 1. Use the DL2GL converter to get a gl file that I can work with. 
  22. 2. Splitting up the resulting GL file with glsplit, a neat utility that comes with one of the GL
  23.    viewers for the amiga (the one written by John Bickers). 
  24. 3. Using HamLabPlus to convert the resulting files into HAM iff images. 
  25. 4. Recompress the iff files into an anim format file with Crygenic software's BuildAnim. 
  26.  
  27. Seems like a lot of work.....but the the results look a lot better than any of the gl viewers
  28. I've seen for the amiga. Since HamLabPlus has an Arexx port, at some point I'll get annoyed with
  29. having to do all of this by hand, and will write a little program to do it for me. 
  30.  
  31. --hal
  32.  
  33. ------------------cut here-------------------
  34. /*
  35.  * dltogl.c -- convert .DL animations into .GL animations.
  36.  *
  37.  * usage: dltogl file.dl [file.gl]
  38.  *
  39.  * If no .gl file is specified, the images and control file will be
  40.  * written to separate files.  The control file will always be "dl.txt",
  41.  * the images "clpN.clp" and a palette file called "palette.pic".
  42.  *
  43.  * Author:
  44.  *    George Phillips
  45.  *    <phillips@cs.ubc.ca>
  46.  */
  47.  
  48. #include <stdio.h>
  49. #ifdef __TURBOC__
  50. #include <alloc.h>
  51. #else
  52. extern char* malloc();
  53. #endif
  54.  
  55. #define FOW    "wb"
  56.  
  57. FILE*    gl;
  58. int        gl_fileno;
  59.  
  60. FILE* file_open();
  61. int file_close();
  62.  
  63. #define isneg16(x)    ((x) & 0x8000)
  64. #define neg16(x)    ((~(x) + 1) & 0x7fff)
  65.  
  66. main(argc, argv)
  67. int        argc;
  68. char*    argv[];
  69. {
  70.     unsigned int i, j;
  71.     FILE* fp;
  72.     int num_scrn;
  73.     int ctl_len;
  74.     int ch;
  75.     int format;
  76.     static char title[21];
  77.     static char author[21];
  78.     static unsigned char pal[256*3];
  79.     unsigned char* screen;
  80.     static unsigned char halfscreen[160*100];
  81.     static char fname[32];
  82.     int picnum;
  83.     int images_per_screen;
  84.     FILE* ctl;
  85.     int fx, fy;
  86.     int* cmd;
  87.     int labelpos;
  88.     int cmdnum;
  89.  
  90.     if (argc != 2 && argc != 3)
  91.         die("usage: gltodl file.dl [file.gl]");
  92.  
  93.     if (!(fp = fopen(argv[1], "rb"))) {
  94.         fprintf(stderr, "gltodl: can't open %s\n", argv[1]);
  95.         exit(1);
  96.     }
  97.  
  98.     gl = 0;
  99.     gl_fileno = 0;
  100.     if (argc == 3 && !(gl = fopen(argv[2], FOW))) {
  101.         fprintf(stderr, "gltodl: can't open %s for writing\n", argv[2]);
  102.         exit(1);
  103.     }
  104.  
  105.     if (fgetc(fp) != 2)
  106.         die("gltodl: only version 2 files can be handled");
  107.  
  108.     switch (format = fgetc(fp)) {
  109.     case 0:
  110.         fx = fy = 0;
  111.         images_per_screen = 1;
  112.         break;
  113.     case 1:
  114.         fx = 80;
  115.         fy = 50;
  116.         images_per_screen = 4;
  117.         break;
  118.     default:
  119.         die("gltodl: only large and medium formats are handled");
  120.         break;
  121.     }
  122.  
  123.     title[20] = author[20] = 0;
  124.     for (i = 0; i < 20; i++)
  125.         title[i] = fgetc(fp) ^ 255;
  126.  
  127.     for (i = 0; i < 20; i++)
  128.         author[i] = fgetc(fp) ^ 255;
  129.  
  130.     for (i = 0; i < 20; i++) {
  131.         if ((unsigned char)title[i] == 255) title[i] = 0;
  132.         if ((unsigned char)author[i] == 255) author[i] = 0;
  133.     }
  134.  
  135.     num_scrn = fgetc(fp);
  136.     ctl_len = fgetc(fp);
  137.  
  138.     if (!(cmd = (int*)malloc(ctl_len * sizeof(int))))
  139.         die("dltogl: out of memory");
  140.  
  141.     gl_numfiles(num_scrn * images_per_screen + 1 + 1);
  142.  
  143.     /* mebbe this is the border colour? */
  144.     for (i = 0; i < 3; i++)
  145.         fgetc(fp);
  146.  
  147.     for (i = 0; i < 256; i++) {
  148.         pal[i*3] = fgetc(fp);
  149.         pal[i*3+1] = fgetc(fp);
  150.         pal[i*3+2] = fgetc(fp);
  151.     }
  152.  
  153.     if (!(screen = malloc(320 * 200)))
  154.         die("dltogl: not enough memory.");
  155.  
  156.     memset(screen, 0, 320 * 200);
  157.     writepic(screen, 320, 200, "palette.pic", pal);
  158.  
  159.     picnum = 0;
  160.     for (j = 0; j < num_scrn; j++) {
  161.         fread(screen, 320 * 200, 1, fp);
  162.         switch (format) {
  163.         case 0: /* large */
  164.             sprintf(fname, "clp%d.clp", picnum++);
  165.             writepic(screen, 320, 200, fname, (unsigned char*)0);
  166.             break;
  167.         case 1: /* medium */
  168.             for (i = 0; i < 4; i++) {
  169.                 unsigned char*    src;
  170.                 unsigned char*    dst;
  171.                 int                row;
  172.                 int                col;
  173.  
  174.                 sprintf(fname, "clp%d.clp", picnum++);
  175.                 src = screen + (i % 2) * 160 + (i / 2) * 100 * 320;
  176.                 dst = halfscreen;
  177.                 for (row = 0; row < 100; row++) {
  178.                     for (col = 0; col < 160; col++)
  179.                         *dst++ = *src++;
  180.                     src += 160;
  181.                 }
  182.                 writepic(halfscreen, 160, 100, fname, (unsigned char*)0);
  183.             }
  184.         }
  185.     }
  186.  
  187.     ctl = file_open("dl.txt", FOW);
  188.  
  189.     fprintf(ctl, "; This GL file was converted from DL format by dltogl\r\n");
  190.     fprintf(ctl, "; Title:  %s\r\n", title);
  191.     fprintf(ctl, "; Author: %s\r\n", author);
  192.     /* could print all the other keeno information */
  193.  
  194.     fprintf(ctl, "video l\r\n");
  195.     fprintf(ctl, "pload palette,1\r\n");
  196.     fprintf(ctl, "palette 1\r\n");
  197.     fprintf(ctl, "pfree 1\r\n");
  198.     for (i = 0; i < num_scrn * images_per_screen; i++)
  199.         fprintf(ctl, "cload clp%d,%d\r\n", i, i + 1);
  200.  
  201.     for (i = 0; i < ctl_len; i++) {
  202.         j = fgetc(fp);
  203.         j += fgetc(fp) << 8;
  204.         cmd[i] = j;
  205.     }
  206.  
  207.     labelpos = 0;
  208.     if (isneg16(cmd[ctl_len - 1])) {
  209.         labelpos = neg16(cmd[ctl_len - 1]) + 1;
  210.         ctl_len--;    /* ignore that last command */
  211.     }
  212.  
  213.     for (i = 0, cmdnum = 0; i < ctl_len; i++, cmdnum++) {
  214.         if (cmdnum == labelpos)
  215.             fprintf(ctl, "forever:\r\n");
  216.         if (isneg16(cmd[i])) {
  217.             i++;
  218.             fprintf(ctl, "waitkey %d\r\n", cmd[i]);
  219.         }
  220.         else
  221.             fprintf(ctl, "putup %d,%d,%d,10\r\n", fx, fy, cmd[i] + 1);
  222.     }
  223.     fprintf(ctl, "goto forever\r\n");
  224.     fputc(26, ctl);
  225.     file_close(ctl);
  226.  
  227.     fclose(fp);
  228.  
  229.     if (gl)
  230.         fclose(gl);
  231.  
  232.     exit(0);
  233. }
  234.  
  235. die(s)char*s;{fprintf(stderr,"%s\n",s);exit(1);}
  236.  
  237. #define BLOCKSIZE    (8192)
  238.  
  239. writepic(pixel, width, height, filename, cmap)
  240. unsigned char* pixel;
  241. int    width;
  242. int    height;
  243. char* filename;
  244. unsigned char* cmap;
  245. {
  246.     FILE*    fp;
  247.     unsigned int i;
  248.     static unsigned char buf[BLOCKSIZE];
  249.     unsigned char* p;
  250.     int row, col;
  251.  
  252.     fp = file_open(filename, FOW);
  253.  
  254.     /* write header */
  255.     writeshort(fp, 0x1234);    /* magic number */
  256.     writeshort(fp, width);
  257.     writeshort(fp, height);
  258.     writeshort(fp, 0);
  259.     writeshort(fp, 0);
  260.     fputc(8, fp);                        /* bits per pixel */
  261.     fputc(0xff, fp);                    /* byte here is always 255 */
  262.     fputc('L', fp);                        /* video mode */
  263.     if (cmap) {
  264.         writeshort(fp, 4);        /* extra information descriptor */
  265.         writeshort(fp, 768);    /* extra information length */
  266.         fwrite(cmap, 768, 1, fp);
  267.     }
  268.     else {
  269.         writeshort(fp, 0);
  270.         writeshort(fp, 0);
  271.     }
  272.  
  273.     /* number of packed blocks in file */
  274.     writeshort(fp, ((long)width * height + BLOCKSIZE - 1) / BLOCKSIZE);
  275.  
  276.     i = 0;
  277.     for (row = height - 1; row >= 0; row--) {
  278.         p = pixel + row * width;
  279.         for (col = 0; col < width; col++) {
  280.             buf[i++] = *p++;
  281.             if (i == BLOCKSIZE) {
  282.                 outpackblock(fp, buf, i);
  283.                 i = 0;
  284.             }
  285.         }
  286.     }
  287.     if (i > 0)
  288.         outpackblock(fp, buf, i);
  289.  
  290.     file_close(fp);
  291. }
  292.  
  293. outpackblock(fp, buf, len)
  294. FILE*    fp;
  295. char*    buf;
  296. int    len;
  297. {
  298.     static char    packbuf[BLOCKSIZE * 4 - 1];    /* overkill */
  299.     int    packlen;
  300.  
  301.     packlen = packblock(buf, packbuf, len);
  302.     fwrite(packbuf, packlen, 1, fp);
  303. }
  304.  
  305. packblock(src, dest, len)
  306. char*    src;
  307. char*    dest;
  308. int        len;
  309. {
  310.     unsigned char*    s;
  311.     unsigned char*    r;
  312.     unsigned char*    d;
  313.     static int    overhead[256];
  314.     int    i;
  315.     int    esc;
  316.     int    escpresent;
  317.     int    minover;
  318.     int    packlen;
  319.  
  320.     for (i = 0; i < 256; i++)
  321.         overhead[i] = 0;
  322.  
  323.     /* do first pass to find optimal esc byte */
  324.     for (s = src; s < src + len; s = r) {
  325.         for (r = s; *r == *s && r < src + len; r++)
  326.             ;
  327.         if (r - s < 4)
  328.             overhead[*s]++;
  329.     }
  330.  
  331.     minover = len + 1;
  332.     for (i = 0; i < 256; i++) {
  333.         if (overhead[i] < minover) {
  334.             minover = overhead[i];
  335.             esc = i;
  336.         }
  337.     }
  338.     escpresent = overhead[esc] == 0;
  339.  
  340.     /* now run-length encode on the second pass */
  341.  
  342.     d = dest + 5;
  343.     for (s = src; s < src + len; s = r) {
  344.         for (r = s; *r == *s && r < src + len; r++)
  345.             ;
  346.         if (r - s < 4 && (!escpresent || *s != esc)) {
  347.             while (s < r)
  348.                 *d++ = *s++;
  349.         }
  350.         else {
  351.             if (r - s > 255) {
  352.                 *d++ = esc;
  353.                 *d++ = 0;
  354.                 *d++ = (r - s) & 0xff;
  355.                 *d++ = ((r - s) >> 8) & 0xff;
  356.                 *d++ = *s++;
  357.             }
  358.             else {
  359.                 *d++ = esc;
  360.                 *d++ = r - s;
  361.                 *d++ = *s++;
  362.             }
  363.         }
  364.     }
  365.  
  366.     packlen = d - dest;
  367.  
  368.     /* fill in the block header */
  369.  
  370.     *dest++ = packlen & 0xff;            /* packed block size */
  371.     *dest++ = ((packlen) >> 8) & 0xff;
  372.     *dest++ = len & 0xff;
  373.     *dest++ = (len >> 8) & 0xff;        /* unpacked block size */
  374.     *dest++ = esc;                        /* escape byte */
  375.  
  376.     return packlen;
  377. }
  378.  
  379. writeshort(fp, s)
  380. FILE*    fp;
  381. int        s;
  382. {
  383.     fputc(s & 255, fp);
  384.     fputc((s >> 8) & 255, fp);
  385. }
  386.  
  387. writelong(fp, l)
  388. FILE*    fp;
  389. long    l;
  390. {
  391.     fputc(l & 255, fp);
  392.     fputc((l >> 8) & 255, fp);
  393.     fputc((l >> 16) & 255, fp);
  394.     fputc((l >> 24) & 255, fp);
  395. }
  396.  
  397. long gl_eof;
  398. long gl_filestart;
  399.  
  400. gl_numfiles(n)
  401. int    n;
  402. {
  403.     if (gl) {
  404.         fseek(gl, 0, 0);
  405.         writeshort(gl, n * 17);
  406.         gl_eof = 2 + n * 17;
  407.     }
  408. }
  409.  
  410. FILE* file_open(name, mode)
  411. char*    name;
  412. char*    mode;
  413. {
  414.     FILE*    fp;
  415.     char    fname[14];
  416.  
  417.     if (!gl) {
  418.         if (!(fp = fopen(name, mode))) {
  419.             fprintf(stderr, "dltogl: can't open %s\n", name);
  420.             exit(1);
  421.         }
  422.         return fp;
  423.     }
  424.  
  425.     fseek(gl, 2 + gl_fileno++ * 17, 0);
  426.     writelong(gl, gl_eof);
  427.     memset(fname, 0, 13);
  428.     strcpy(fname, name);
  429.     fwrite(fname, 13, 1, gl);
  430.     fseek(gl, gl_filestart = gl_eof + 4, 0);
  431.     return gl;
  432. }
  433.  
  434. int file_close(fp)
  435. FILE*    fp;
  436. {
  437.     if (!gl)
  438.         return fclose(fp);
  439.  
  440.     gl_eof = ftell(gl);
  441.     fseek(gl, gl_filestart - 4, 0);
  442.     writelong(gl, gl_eof - gl_filestart);
  443. }
  444. --
  445. hgm@ccvr1.cc.ncsu.edu    "The trick is -- find a name for it.
  446.                          Then you can sell it. End of the Revolution."
  447.