home *** CD-ROM | disk | FTP | other *** search
/ Resource Library: Multimedia / Resource Library: Multimedia.iso / space / software / unix / xanim / xanim_dl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-04  |  6.0 KB  |  256 lines

  1.  
  2. /*
  3.  * xanim_dl.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed
  9.  * without fee provided that this copyright notice is preserved 
  10.  * intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include "xanim.h"
  20. #include "xanim_dl.h"
  21.  
  22. static ULONG dl_version;
  23. static ULONG dl_format;
  24. static ULONG dl_time;
  25. static UBYTE dl_title[21];
  26. static UBYTE dl_author[21];
  27. static ULONG dl_num_of_images;
  28. static ULONG dl_num_of_frames;
  29. static ULONG dl_frame_cnt;
  30. static ULONG dl_loop_frame;
  31. static ULONG *dl_frame_lst;
  32.  
  33. static ColorReg dl_cmap[DL_MAX_COLORS];
  34.  
  35. /*
  36.  *
  37.  */
  38. int Is_DL_File(filename)
  39. char *filename;
  40. {
  41.   FILE *fin;
  42.   ULONG data0,data1;
  43.  
  44.   if ( (fin=fopen(filename,"r")) == 0)
  45.   {
  46.    fprintf(stderr,"can't open %s\n",filename);
  47.    TheEnd();
  48.   }
  49.   data0 = fgetc(fin);
  50.   data1 = fgetc(fin);
  51.   fclose(fin);
  52.   if (data0 == 0x01) { fclose(fin); return(TRUE); }
  53.   if (data0 == 0x02)
  54.   {
  55.     if ( (data1 == 0x01) || (data1 == 0x02) ) return(TRUE);
  56.   }
  57.   return(FALSE);
  58. }
  59.  
  60. /*
  61.  *
  62.  */
  63. void DL_Read_File(fname,anim_hdr)
  64. ANIM_HDR *anim_hdr;
  65. char *fname;
  66. {
  67.   FILE *fin;
  68.   LONG i,j,ret,tmp;
  69.   ACTION *act;
  70.   UBYTE *pic;
  71.   ULONG pic_size;
  72.  
  73.   if ( (fin = fopen(fname,"r")) == 0)
  74.   { 
  75.     fprintf(stderr,"DL_Read_File: Can't open %s for reading.\n",fname); 
  76.     TheEnd();
  77.   }
  78.  
  79.   dl_version = fgetc(fin);  /* either 1 or 2 */
  80.  
  81.   switch(dl_version)
  82.   {
  83.     case DL_VERSION_1:
  84.         dl_format = DL_FORM_MEDIUM;
  85.         break;
  86.     case DL_VERSION_2:
  87.         dl_format = fgetc(fin);
  88.         break;
  89.     default:
  90.         break;
  91.   }
  92.  
  93.   switch(dl_format)
  94.   {
  95.     case DL_FORM_LARGE:
  96.     imagex = DL_LARGE_XSIZE;
  97.     imagey = DL_LARGE_YSIZE;
  98.     break;
  99.     case DL_FORM_MEDIUM:
  100.     imagex = DL_MEDIUM_XSIZE;
  101.     imagey = DL_MEDIUM_YSIZE;
  102.     break;
  103.     case DL_FORM_SMALL:
  104.     imagex = DL_SMALL_XSIZE;
  105.     imagey = DL_SMALL_YSIZE;
  106.     break;
  107.     default:
  108.     fprintf(stderr,"DL_Read_File: unknown format %lx\n",dl_format);
  109.     TheEnd();
  110.     break;
  111.   }
  112.  
  113.   dl_title[20] = 0;
  114.   for(i=0; i<20; i++) 
  115.   {
  116.     tmp = fgetc(fin);
  117.     dl_title[i] = (tmp)?(tmp ^ 0xff):tmp;
  118.   }
  119.  
  120.   dl_author[0] = dl_author[20] = 0;
  121.   if (dl_version == DL_VERSION_2)
  122.     for(i=0; i<20; i++)
  123.     {
  124.       tmp = fgetc(fin);
  125.       dl_author[i] = (tmp)?(tmp ^ 0xff):tmp;
  126.     }
  127.  
  128.   dl_num_of_images = fgetc(fin);
  129.  
  130.   if (dl_version == DL_VERSION_1)
  131.      dl_num_of_frames = UTIL_Get_LSB_Short(fin);
  132.   else
  133.      dl_num_of_frames = UTIL_Get_LSB_Long(fin);
  134.  
  135.   if (verbose == TRUE)
  136.   {
  137.     fprintf(stderr,"Reading DL file: version %ld  format %ld\n",
  138.     dl_version,dl_format);
  139.     fprintf(stderr,"                 images %ld  frames %ld\n",
  140.     dl_num_of_images * (dl_format * 3 + 1), dl_num_of_frames );
  141.     fprintf(stderr,"   title = %s  Author = %s\n",dl_title,dl_author);
  142.   }
  143.  
  144.   imagec = DL_MAX_COLORS;
  145.   for(i=0; i < DL_MAX_COLORS; i++)
  146.   {
  147.     dl_cmap[i].red =   ((fgetc(fin) & 0x3f) << 2);
  148.     dl_cmap[i].green = ((fgetc(fin) & 0x3f) << 2);
  149.     dl_cmap[i].blue =  ((fgetc(fin) & 0x3f) << 2);
  150.   }
  151.   ACT_Setup_CMAP(dl_cmap,DL_MAX_COLORS,&i,CMAP_DIRECT);
  152.  
  153.   
  154.   pic_size = imagex * imagey;
  155.  
  156.   dl_time = (jiffy_flag)?(jiffy_flag):(100);
  157.  
  158.   for(j = 0; j < dl_num_of_images; j++)
  159.   {
  160.     switch(dl_format)
  161.     {
  162.       case DL_FORM_LARGE: /* large */
  163.     pic = (UBYTE *) malloc( pic_size );
  164.     if (pic == 0) TheEnd1("DL: malloc failed.\n");
  165.         ret = fread(pic, pic_size, 1, fin);
  166.     if (ret != 1) TheEnd1("DL: read of image failed format 0");
  167.     DEBUG_LEVEL3 fprintf(stderr,"Read large format image\n");
  168.         ACT_Get_Next_Action(&act);
  169.     ACT_Setup_Mapped(act,dl_time,pic,dl_cmap,imagec,
  170.             0,0,imagex,imagey,FALSE,0);
  171.     break;
  172.  
  173.       case DL_FORM_MEDIUM: /* medium */
  174.       case DL_FORM_SMALL: /* medium */
  175.     {
  176.       ULONG x,y;
  177.       UBYTE *t_pic;
  178.  
  179.       tmp = DL_LARGE_XSIZE * DL_LARGE_YSIZE;
  180.       pic = (UBYTE *) malloc( tmp );
  181.       if (pic == 0) TheEnd1("DL: malloc failed.\n");
  182.           ret = fread(pic, tmp , 1, fin);
  183.       if (ret != 1) TheEnd1("DL: read of image failed format 1");
  184.       DEBUG_LEVEL3 fprintf(stderr,"Read small/medium image\n");
  185.  
  186.           for(y = 0; y < DL_LARGE_YSIZE; y += imagey)
  187.             for(x = 0; x < DL_LARGE_XSIZE; x += imagex)
  188.         {
  189.           t_pic = (UBYTE *) malloc( pic_size );
  190.           if (t_pic == 0) TheEnd1("DL: malloc failed.\n");
  191.           UTIL_Sub_Image(t_pic,pic,imagex,imagey, x, y,(DL_LARGE_XSIZE),1);
  192.               ACT_Get_Next_Action(&act);
  193.           ACT_Setup_Mapped(act,dl_time,t_pic,dl_cmap,imagec,
  194.                 0,0,imagex,imagey,FALSE,0);
  195.         }
  196.     }
  197.     break;
  198.    }
  199.  }
  200.  
  201.  dl_frame_lst = (ULONG *)malloc( dl_num_of_frames * sizeof(ULONG) );
  202.  if (dl_frame_lst == 0) TheEnd1("DL_Read_File: malloc failed");
  203.  
  204.  dl_loop_frame = 0;
  205.  switch(dl_version)
  206.  {
  207.    case DL_VERSION_1:
  208.     dl_frame_cnt = 0;
  209.     for(i=0; i < dl_num_of_frames; i++)
  210.     {
  211.       register ULONG tmp;
  212.       tmp = fgetc(fin);
  213.       dl_frame_lst[dl_frame_cnt] = (j % 10) - 1 + ((j / 10) - 1) * 4;
  214.       dl_frame_cnt++;
  215.     }
  216.     break;
  217.    case DL_VERSION_2:
  218.     DEBUG_LEVEL3 fprintf(stderr," DL reading frame lst: ");
  219.     dl_frame_cnt = 0;
  220.     for(i=0; i < dl_num_of_frames; i++)
  221.     {
  222.       register ULONG tmp;
  223.       tmp  =  fgetc(fin);
  224.       tmp |=  ( fgetc(fin) << 8); 
  225.       DEBUG_LEVEL3 fprintf(stderr,"\t<%ld %lx>",i,tmp);
  226.       if (tmp & 0x8000)
  227.       {
  228.         if (i == (dl_num_of_frames - 1) )
  229.         dl_loop_frame = (~tmp + 1) & 0x7fff + 1;
  230.         else
  231.           fprintf(stderr,"unknown frame ignored for now. %lx\n",tmp);
  232.       }
  233.       else
  234.       {
  235.         dl_frame_lst[dl_frame_cnt] = tmp;
  236.         dl_frame_cnt++;
  237.       }
  238.     }
  239.     DEBUG_LEVEL3 fprintf(stderr,"\n");
  240.     break;
  241.   }
  242.   anim_hdr->frame_lst = (int *)malloc(sizeof(int) * (dl_frame_cnt+2) ); 
  243.   if (anim_hdr->frame_lst == NULL)
  244.       TheEnd1("DL_ANIM: couldn't malloc for frame_lst\0");
  245.  
  246.   anim_hdr->frame_lst[0] = 0;            /* cmap */
  247.   for(i=1; i < dl_frame_cnt; i++)
  248.       anim_hdr->frame_lst[i] = action_start + dl_frame_lst[i] + 1;
  249.   anim_hdr->frame_lst[dl_frame_cnt + 1] = -1;
  250.   anim_hdr->loop_frame = dl_loop_frame;
  251.   anim_hdr->last_frame = dl_frame_cnt;
  252.   free(dl_frame_lst);
  253.  
  254. }
  255.  
  256.