home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 July / VPR9707B.ISO / DRIVER / IODATA / GA360 / DISK2.EXE / SAMPLE / X68LOAD.C < prev    next >
Text File  |  1993-04-01  |  10KB  |  426 lines

  1. /************************************************************************
  2.     GA-1024A / GA-1280A グラフィックライブラリ サンプルソース
  3.         X68LOAD.C
  4.     
  5.     X68000 GRAM ベタファイルロ-ダ (X68KのDB.X で W ファイル名,C00000,C7FFFF)
  6.  
  7.                 アイ・オー・データ機器 Device Inc...
  8. *************************************************************************/
  9. #include <io.h>
  10. #include <stdio.h>
  11. #include <fcntl.h>
  12. #include <math.h>
  13.  
  14. #ifndef TURBO
  15. #include <sys\types.h>
  16. #include <sys\stat.h>
  17. #include <malloc.h>
  18. #else
  19. #include <alloc.h>
  20. #endif
  21.  
  22. #include "gagraph.h"
  23.  
  24. typedef unsigned char  uchar;
  25. typedef unsigned short ushort;
  26. typedef unsigned int   uint;
  27. typedef unsigned long  ulong;
  28.  
  29. #define    SQR(x)        ((x) * (x))
  30.  
  31. #define SIZE_OF_BUFF 1024*16
  32.  
  33. #define    COL_CNT        (6 * 6 * 6)
  34.  
  35. int     handle;        /* DOS file handle */
  36. int        buff_len;    /* buff reset */
  37. uchar   buff[SIZE_OF_BUFF]; /* file buffer */
  38. uchar   buff2[1024];
  39. uchar   buff4[1024];
  40. int        x_wid = 512;        /* screen size */
  41. int        y_wid = 384;
  42. int     gmode = 1;
  43.  
  44. static unsigned int  gaport;
  45. static volatile uchar far *ga_wind;
  46.  
  47. void    ginit();
  48. void    paletinit();
  49. void    convert();
  50. void    wait(int);
  51. int     freadline(int ,int *,int );
  52. void    error(char *);
  53. void    dither4(int );
  54.  
  55. /********************************************
  56.  
  57.     main()
  58.  
  59. *********************************************/
  60. void main(argc,argv)
  61. int    argc;
  62. char    *argv[];
  63. {
  64.     int i,x,y;
  65.     int dither = 0;
  66.     int pal_flg;
  67.  
  68.     puts("GA-1024A/GA-1280A X6800 File loader program Ver 1.00 I」O DATA DEVICE Inc.");
  69.     if (argc < 2) error("usage : X68LOAD <X68000 file> [-D]");
  70.     if ((handle=open(argv[1],O_RDONLY | O_BINARY))==-1) {
  71.         error("file can't open");
  72.     }
  73.     for (i=2;i<argc;i++) {
  74.         if (argv[i][0] == '-' || argv[i][0] == '/') {
  75.             switch(argv[i][1]) {
  76.             case 'D' :
  77.             case 'd' :
  78.                 dither = 1;
  79.              break;
  80.             default:
  81.                 puts("パラメ-タ無効 ");
  82.             }
  83.         }
  84.     }
  85.     gmode = 1;
  86.     ginit();                /* screen init */
  87.     GAcrtSel(1);
  88.     if (dither) dither4(handle); 
  89.     else convert(handle);
  90.     close(handle);
  91.  
  92.     while (1) {
  93.         switch (getch()) {
  94.             case 'C':
  95.             case 'c':    /* 画面モードを変える */
  96.             if (gmode & 1)
  97.                      gmode++ ;
  98.                 else 
  99.                      gmode-- ;
  100.                 GAinit(gmode);        
  101.                 GAsetVisualPlane(-1);
  102.                 wait(40);            /* CRTが同期するまで待つ */
  103.                 paletinit();
  104.                 GAsetVisualPlane(0);
  105.                 continue;
  106.             case 'Q':
  107.             case 'q':    /* パレットを変更せずに終了 */
  108.                 pal_flg = PAL_USER;
  109.                 break;
  110.             default:
  111.                 pal_flg = PAL_DEF;
  112.                 break;
  113.         }
  114.         break;
  115.     }
  116.     GAcrtSel(CRT_PC);
  117.     GAterm( pal_flg );
  118.     exit(0);
  119. }
  120.  
  121. /************************************************
  122.  
  123.     初期化
  124.  
  125. *************************************************/
  126. void ginit()
  127. {
  128.     struct VDCONFIG vd;
  129.  
  130.     if (GAinit(gmode)) {
  131.         puts("GAINIT が常駐していません");
  132.         exit(1);
  133.     }
  134.     GAclrScreen();
  135.     paletinit();
  136.     GAgetVideoConfig((void far *)&vd);
  137.     gaport = vd.port    ; /* GA レジスタのポート番号 */
  138.     ga_wind = (void far *) ((long)vd.ga_seg    << 16); /*グラフィックボードウィンドウのセグメント*/
  139. }
  140.  
  141. /********************************************************
  142.  
  143.     パレットの初期化
  144.  
  145. *********************************************************/
  146. void paletinit()
  147. {
  148.     struct PALETTE pal;
  149.     int i;
  150.     int r,g,b;
  151.  
  152. /*  R G B 6x6x6 */
  153.     i = 0 ;
  154.     for (r=0;r<6;r++){
  155.         for (g=0;g<6;g++){
  156.             for (b=0;b<6;b++){
  157.                 pal.blue = b * 0x33;
  158.                 pal.red  = r * 0x33;
  159.                 pal.green= g * 0x33;
  160.                 GAsetPalette((void far *)&pal,i++);
  161.             }
  162.         }
  163.     }
  164. }
  165.  
  166. /***************************************************
  167.  
  168.     32768色のデ-タを誤差拡散により減色する
  169.  
  170. *****************************************************/
  171. void convert(int handle)
  172. {
  173.     int     x, y, i, c,idx;
  174.     short  *av1, *av2, *av3;
  175.     ushort  cc;
  176.  
  177.     av1 = malloc((x_wid + 2) * sizeof *av1 * 3);
  178.     av2 = malloc((x_wid + 2) * sizeof *av2 * 3);
  179.     av3 = malloc((x_wid + 2) * sizeof *av3 * 3);
  180.     memset(av2, 0, (x_wid + 2) * sizeof *av2 * 3);
  181.     memset(av3, 0, (x_wid + 2) * sizeof *av3 * 3);
  182.  
  183.     for (y = 0; y < y_wid; y++) {
  184.         if ((y % 4)==0) freadline(handle, &idx,x_wid * 2) ;
  185.         if (freadline(handle, &idx,x_wid * 2) == 0)
  186.             break;
  187.         for (x = 0; x < x_wid; x++,idx += 2) {
  188.             i = x * 3;
  189.             cc = (buff[idx] << 8) + buff[idx+1];
  190.             av1[i]     = av2[i]     + ((cc & 0x07c0)>>3) ; /* Red */
  191.             av1[i + 1] = av2[i + 1] + ((cc & 0xf800)>>8) ; /* Green */
  192.             av1[i + 2] = av2[i + 2] + ((cc & 0x003e)<<2) ; /* Blue */
  193.             av2[i]     = av3[i];
  194.             av2[i + 1] = av3[i + 1];
  195.             av2[i + 2] = av3[i + 2];
  196.             av3[i]     = 0;
  197.             av3[i + 1] = 0;
  198.             av3[i + 2] = 0;
  199.         }
  200.         for (x = 0; x < x_wid; x++) {
  201.             i = x * 3;
  202.             c = 0;
  203.             if (av1[i] >= 0x33) {
  204.                 if (av1[i] >= 0x66) {
  205.                     if (av1[i] >= 0x99) {
  206.                         if (av1[i] >= 0xcc) {
  207.                             if (av1[i] >= 0xff) {
  208.                                 av1[i] -= 0xff;
  209.                                 c += 6 * 6 * 5;
  210.                             } else {
  211.                                 av1[i] -= 0xcc;
  212.                                 c += 6 * 6 * 4;
  213.                             }
  214.                         } else {
  215.                             av1[i] -= 0x99;
  216.                             c += 6 * 6 * 3;
  217.                         }
  218.                     } else {
  219.                         av1[i] -= 0x66;
  220.                         c += 6 * 6 * 2;
  221.                     }
  222.                 } else {
  223.                     av1[i] -= 0x33;
  224.                     c += 6 * 6 * 1;
  225.                 }
  226.             }
  227.             if (av1[i + 1] >= 0x33) {
  228.                 if (av1[i + 1] >= 0x66) {
  229.                     if (av1[i + 1] >= 0x99) {
  230.                         if (av1[i + 1] >= 0xcc) {
  231.                             if (av1[i + 1] >= 0xff) {
  232.                                 av1[i + 1] -= 0xff;
  233.                                 c += 6 * 5;
  234.                             } else {
  235.                                 av1[i + 1] -= 0xcc;
  236.                                 c += 6 * 4;
  237.                             }
  238.                         } else {
  239.                             av1[i + 1] -= 0x99;
  240.                             c += 6 * 3;
  241.                         }
  242.                     } else {
  243.                         av1[i + 1] -= 0x66;
  244.                         c += 6 * 2;
  245.                     }
  246.                 } else {
  247.                     av1[i + 1] -= 0x33;
  248.                     c += 6 * 1;
  249.                 }
  250.             }
  251.             if (av1[i + 2] >= 0x33) {
  252.                 if (av1[i + 2] >= 0x66) {
  253.                     if (av1[i + 2] >= 0x99) {
  254.                         if (av1[i + 2] >= 0xcc) {
  255.                             if (av1[i + 2] >= 0xff) {
  256.                                 av1[i + 2] -= 0xff;
  257.                                 c += 5;
  258.                             } else {
  259.                                 av1[i + 2] -= 0xcc;
  260.                                 c += 4;
  261.                             }
  262.                         } else {
  263.                             av1[i + 2] -= 0x99;
  264.                             c += 3;
  265.                         }
  266.                     } else {
  267.                         av1[i + 2] -= 0x66;
  268.                         c += 2;
  269.                     }
  270.                 } else {
  271.                     av1[i + 2] -= 0x33;
  272.                     c += 1;
  273.                 }
  274.             }
  275.             switch ((rand() & 0x0300) >> 8) {
  276.             case 0:
  277.             case 1:
  278.                 av1[i + 3]     += av1[i] / 2;
  279.                 av1[i + 3 + 1] += av1[i + 1] / 2;
  280.                 av1[i + 3 + 2] += av1[i + 2] / 2;
  281.                 av2[i]     += av1[i] / 2;
  282.                 av2[i + 1] += av1[i + 1] / 2;
  283.                 av2[i + 2] += av1[i + 2] / 2;
  284.                 break;
  285.             case 2:
  286.                 av1[i + 3]     += av1[i] / 4 * 3;
  287.                 av1[i + 3 + 1] += av1[i + 1] / 4 * 3;
  288.                 av1[i + 3 + 2] += av1[i + 2] / 4 * 3;
  289.                 av2[i]     += av1[i] / 4;
  290.                 av2[i + 1] += av1[i + 1] / 4;
  291.                 av2[i + 2] += av1[i + 2] / 4;
  292.                 break;
  293.             case 3:
  294.                 av1[i + 3]     += av1[i] / 4;
  295.                 av1[i + 3 + 1] += av1[i + 1] / 4;
  296.                 av1[i + 3 + 2] += av1[i + 2] / 4;
  297.                 av2[i]     += av1[i] / 4 * 3;
  298.                 av2[i + 1] += av1[i + 1] / 4 * 3;
  299.                 av2[i + 2] += av1[i + 2] / 4 * 3;
  300.                 break;
  301.             default:
  302.                 break;
  303.             }
  304.             buff2[x] = c;
  305.         }
  306.         GArestoreImage(0,y,x_wid-1,y,1,(void far *)buff2);  
  307.     }
  308.  
  309. /*****************************************************
  310.  
  311.     32768色のデ-タを2x2のディザ-に変換する
  312.  
  313. ******************************************************/
  314. void dither4(int handle)
  315. {
  316.     uint   r,g,b,cc;
  317.     int    x,y,idx;
  318.     int    xsize = 512;
  319.     int    ysize = 384;
  320.  
  321. static char dh1b[] = { 0, 1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5 };
  322. static char dh2b[] = { 0, 0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5 };
  323. static char dh3b[] = { 0, 0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5 };
  324. static char dh4b[] = { 0, 0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5 };
  325.  
  326. static char dh1g[] = { 0, 6,6,6,6,12,12,12,12,18,18,18,18,24,24,24,24,30,30,30,30 };
  327. static char dh2g[] = { 0, 0,0,6,6, 6, 6,12,12,12,12,18,18,18,18,24,24,24,24,30,30 };
  328. static char dh3g[] = { 0, 0,0,0,6, 6, 6, 6,12,12,12,12,18,18,18,18,24,24,24,24,30 };
  329. static char dh4g[] = { 0, 0,6,6,6, 6,12,12,12,12,18,18,18,18,24,24,24,24,30,30,30 };
  330.  
  331. static char dh1r[] = { 0, 36,36,36,36,72,72,72,72,108,108,108,108,144,144,144,144,180,180,180,180 };
  332. static char dh2r[] = { 0,  0, 0,36,36,36,36,72,72,72,72,108,108,108,108,144,144,144,144,180,180 };
  333. static char dh3r[] = { 0,  0, 0, 0,36,36,36,36,72,72,72,72,108,108,108,108,144,144,144,144,180 };
  334. static char dh4r[] = { 0,  0,36,36,36,36,72,72,72,72,108,108,108,108,144,144,144,144,180,180,180 };
  335.  
  336.     
  337.     for (y = 0; y < ysize; y++) {
  338.         if ((y % 4)==0) freadline(handle, &idx,xsize * 2) ;
  339.         if (freadline(handle, &idx,xsize * 2) == 0)
  340.                 break;
  341.         for (x=0;x<xsize*2;x+=2,idx+=2) {
  342.             cc = (buff[idx]<<8) +buff[idx+1];
  343.             g = ((cc & 0xf800) >> 10) / 3;
  344.             r = ((cc & 0x07c0) >> 5)  / 3;
  345.             b = (cc & 0x003e)         / 3;
  346.             buff2[x]   = dh1r[r]+dh1g[g]+dh1b[b];
  347.             buff2[x+1] = dh2r[r]+dh2g[g]+dh2b[b];
  348.             buff4[x]   = dh3r[r]+dh3g[g]+dh3b[b];
  349.             buff4[x+1] = dh4r[r]+dh4g[g]+dh4b[b];
  350.         }
  351.         GArestoreImage(0,y*2,xsize*2-1,y*2,1,(void far *)buff2);
  352.         GArestoreImage(0,y*2+1,xsize*2-1,y*2+1,1,(void far *)buff4);
  353.     }
  354. }
  355.  
  356.  
  357. /*************************************************************
  358.  
  359.     エラ-処理
  360.  
  361. *************************************************************/
  362. void error(s)
  363. char *s;
  364. {
  365.     puts(s);
  366.     GAcrtSel(0);
  367.     exit(1);
  368. }
  369.  
  370.  
  371. /*****************************************************************
  372.  
  373.     1ライン分ファイルリード
  374.  
  375. ******************************************************************/
  376. int freadline( handl,idx,size)
  377. int  handl;
  378. int *idx;
  379. int  size;
  380. {
  381.     static int fp = 0;
  382.     static int remain = 0;
  383.     int    ret = size;
  384.     
  385.     if (remain < size) {
  386.         fp = 0;
  387.         remain = ( SIZE_OF_BUFF / size ) * size;
  388.         ret = read(handl,buff,remain);
  389.     } 
  390.     *idx = fp;
  391.     fp += size;
  392.     remain -= size;
  393.     return(ret);
  394. }
  395.  
  396.  
  397.  
  398. /* グラフィック コントロ-ル レジスタ ポ-ト */
  399. #define CRTC_AR 0x1e00
  400. #define CRTC_CR 0x1f00
  401. /***********************************************************
  402.  
  403.     CRTC の垂直同期信号をカウントしてウエイトする
  404.  
  405. ************************************************************/
  406. void wait(int count) 
  407. {
  408. #if 0
  409.     int  i;
  410.     for (i=0 ; i < count ; i++) {
  411. #ifdef TURBO
  412.         outportb(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  413.         while ( inportb(CRTC_CR+gaport) & 2) ;
  414.         while ( !(inportb(CRTC_CR+gaport) & 2)) ;
  415. #else
  416.         outp(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  417.         while ( inp(CRTC_CR+gaport) & 2) ;
  418.         while ( !(inp(CRTC_CR+gaport) & 2)) ;
  419. #endif
  420.     }
  421. #endif
  422. }
  423.  
  424.  
  425.