home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / fmge / fmge.c next >
Text File  |  1990-06-14  |  5KB  |  229 lines

  1. /* 
  2.     FM-GRAPHIC EDITOR file loader
  3. */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <egb.h>
  8. #include "tiff.h"
  9.  
  10. #define II(s)  (short)(*((unsigned char *)&s) * 256 + *((unsigned char *)&s+1))
  11. #define ERR(e) { fmge_err = e ; return -1 ; }
  12.  
  13. #define ERR_FILE 1
  14. #define ERR_FORM 2
  15.  
  16. typedef union {
  17.     long p ;
  18.     struct {
  19.         char b,r,g,i ;
  20.     } col ;
  21. } PACK ;
  22.  
  23. struct {
  24.     short id ;
  25.     short recs ;
  26.     short sx ;
  27.     short sy ;
  28.     short wx ;
  29.     short wy ;
  30.     char mode ;
  31.     char _padding[243] ;
  32. } header ;
  33.  
  34. int fmge_err ;
  35. int fmge_sx,fmge_sy,fmge_wx,fmge_wy,fmge_mode ;
  36. int xb,bit ;
  37.  
  38. int gethead( fp )
  39. FILE *fp ;
  40. {
  41.     if( fread( &header, sizeof(header), 1, fp ) == 0 ) ERR(ERR_FILE) ;
  42.     if( header.id != 0x4d46 ) ERR(ERR_FORM) ;
  43.     fmge_sx = II(header.sx) ;
  44.     fmge_sy = II(header.sy) ;
  45.     fmge_wx = II(header.wx) + 1 ; xb = (fmge_wx + 7) / 8 ;
  46.     fmge_wy = II(header.wy) + 1 ;
  47.     fmge_mode = header.mode ;
  48.     return( 0 ) ;
  49. }
  50.  
  51. void packpixel( sp, dp)
  52. unsigned char *sp ;
  53. long *dp ;
  54. {
  55.     long pix,pm  ;
  56.     unsigned char *cp ;
  57.     register unsigned char mask ;
  58.     register int c,j,i ;
  59.  
  60.     for( i = 0 ; i < xb ; i++,sp++ ) {
  61.         mask = 0x80 ;
  62.         for( j = 0 ; j < 8 ; j++ ) {
  63.             cp = sp ;
  64.             pix = 0 ;
  65.             pm = 1 << (8 - bit) ;
  66.             for( c = 0 ; c < bit*3 ; c++ ) {
  67.                 if( *cp & mask ) 
  68.                     pix |= pm ;
  69.                 pm <<= 1 ;
  70.                 if( c % bit == bit - 1 ) pm <<= 8 - bit ;
  71.                 cp += xb ;
  72.             }
  73.             mask >>= 1 ;
  74.             *dp++ = pix ;
  75.         }
  76.     }
  77. }
  78.  
  79. long *ldbitdat( fp, wx,wy )
  80. FILE *fp ;
  81. int wx,wy ;
  82. {
  83.     int size, y ;
  84.     long *dbp,*bp ;
  85.     unsigned char *sbp ;
  86.  
  87.     size = wx * wy * 4 + 28 ;
  88.     if( (dbp = (long *)malloc( size )) == 0 ) error("Memory") ;
  89.     if( (sbp = (unsigned char *)malloc( xb * 3 * bit )) == 0 ) 
  90.         error("Memory") ;
  91.     bp = dbp ;
  92.     for( y = 0 ; y < wy ; y++ ) {
  93.         fread(sbp, bit * 3 , xb, fp ) ;
  94.         packpixel( sbp, bp ) ;
  95.         bp += wx ;
  96.     }
  97.     free( sbp ) ;
  98.     return dbp ;
  99. }
  100.  
  101. sample( work, bp, sx,sy, wx, wy, dwy, mag )
  102. char *work ;
  103. long *bp ;
  104. int sx,sy,wx,wy,dwy,mag ;
  105. {
  106.     long swy,my,pg,pr,pb ;
  107.     short *lb, *lbp ;
  108.     long  *sbp ;
  109.     int x,y,m ;
  110.     char para[16] ;
  111.     PACK bit ;
  112.  
  113.     lb = (short *)malloc( wx * 2 ) ;
  114.     swy = dwy * mag ;
  115.  
  116.     DWORD( para + 0 ) = (unsigned int)lb ;
  117.     WORD( para + 4 ) = 0x14 ;
  118.     WORD( para + 6 ) = sx ;
  119.     WORD( para +10 ) = sx + wx - 1 ;
  120.  
  121.     my = 0 ;
  122.     for( y = 0 ; y < dwy ; y++ ) {
  123.         lbp = lb ;
  124.         sbp = bp ;
  125.         for( x = 0 ; x < wx ; x++ ) {
  126.             pg = pr = pb = 0 ;
  127.             for( m = 0 ; m < mag ; m++ ) {
  128.                 bit.p = *(sbp + ((my + m)*wy/swy)*wx ) ;    
  129.                 pg += bit.col.g ;
  130.                 pr += bit.col.r ;
  131.                 pb += bit.col.b ;
  132.             }
  133.             pg = (pg / mag) & 0xF8 ;
  134.             pr = (pr / mag) & 0xF8 ;
  135.             pb = (pb / mag) & 0xF8 ;
  136.             *lbp++ = (pg << 7) + (pr << 2) + (pb >> 3) ;
  137.             sbp++ ;
  138.         }
  139.         my += mag ;
  140.         WORD( para + 8 ) = WORD( para +12 ) = y + sy ;
  141.         EGB_putBlock( work,0,para ) ;
  142.     }
  143.     free( lb ) ;
  144. }
  145.  
  146. char work[1600] ;
  147. extern int optind ;
  148. extern char *optarg ;
  149.  
  150. void main( argc,argv ) 
  151. int argc ;
  152. char **argv ;
  153. {
  154.     FILE *fp ;
  155.     long *buf ;
  156.     int mag = 3 ;
  157.     int fwy = 240 ;
  158.     int wf = 1 ;
  159.     int dsy,dwy,c ;
  160.     char name[80] ;
  161.  
  162.     if( argc < 2 ) usage() ;
  163.     while( ( c = getopt( argc,argv, "s:h:v")) != EOF ) {
  164.         switch( c ) {
  165.         case 's':
  166.             mag = atoi(optarg) ;
  167.             break ;
  168.         case 'h':
  169.             fwy = atoi(optarg) ;
  170.             break ;
  171.         case 'v':
  172.             wf = 0 ;
  173.             break ;
  174.         case '?':
  175.             usage() ;
  176.         }
  177.     }
  178.     if( optind == argc ) usage() ;
  179.     strcpy(name, argv[optind] ) ;
  180.     strcat(name, ".FMG" ) ;
  181.     if( (fp = fopen( name,"rb" )) == NULL ) error( "No File!") ;
  182.     if( gethead( fp ) != 0 ) error( "File Format" ) ;
  183.  
  184.     if( fmge_mode == 0xFF ) bit = 4 ; else bit = 6 ;
  185.  
  186.     printf( "Now Loading data of FM-Graphic Editor\n" ) ; 
  187.     buf = ldbitdat( fp, fmge_wx, fmge_wy ) ; 
  188.     fclose( fp ) ;
  189.  
  190.     EGB_init(work,1536) ;
  191.     EGB_displayPage( work,1,3) ;
  192.     EGB_resolution( work, 0, 10 ) ;
  193.     EGB_displayStart( work, 2, 2, 2 ) ;
  194.     EGB_displayStart( work, 3, 320, 240 ) ;
  195.     dsy = fmge_sy * fwy / 200 ;
  196.     dwy = fmge_wy * fwy / 200 ;
  197.     sample( work, buf, fmge_sx, dsy, fmge_wx, fmge_wy, dwy, mag ) ;
  198.     free(buf) ;
  199.     if( wf ) {
  200.         getchar() ;
  201.         strcpy( name, argv[optind] ) ;
  202.         strcat( name, ".TIF" ) ;
  203.         if(( fp = fopen( name, "wb" )) == NULL ) error( "Write File" ) ;
  204.         setTIFFmode( TIFF_32K ) ;
  205.         mkIFD() ;
  206.         tiff_width = fmge_wx ;
  207.         tiff_length = dwy ;
  208.         putIFD( fp ) ;
  209.         freeIFD() ;
  210.         svram( fp, work, fmge_sx, dsy, fmge_wx, dwy, 16, 0) ;
  211.         fclose( fp ) ;
  212.     }
  213.     getchar();
  214. }
  215.  
  216. error( msg ) 
  217. char *msg ;
  218. {
  219.     printf( "******** %s Error\n", msg ) ;
  220.     exit( 1 );
  221. }
  222.  
  223. usage()
  224. {
  225. printf( "USAGE : run386 fmge [-s <smooth>] [-h <height>] [-v] file(.FMG)\n" ) ;
  226.     exit( 1 );
  227. }
  228.  
  229.