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

  1. /************************************************************************
  2.     GA-1024A / GA-1280A グラフィックライブラリ サンプルソース
  3.         RGBLOAD.C
  4.     
  5.     RGBファイルロ-ダ
  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. int        x_wid = 640;        /* screen size */
  40. int        y_wid = 768;
  41. int     gmode = 1;
  42.  
  43. static unsigned int  gaport;
  44. static volatile uchar far *ga_wind;
  45.  
  46. static uchar radj[256];
  47. static uchar gadj[256];
  48. static uchar badj[256];
  49.  
  50. void ginit();
  51. void paletinit();
  52. void convert();
  53. void save();
  54. void    wait(int);
  55. int     freadline(int ,int *,int );
  56. void    error(char *);
  57.  
  58. /********************************************
  59.  
  60.     main()
  61.  
  62. *********************************************/
  63. void main(argc,argv)
  64. int    argc;
  65. char    *argv[];
  66. {
  67.     int i,x,y;
  68.     int pal_flg;
  69.  
  70.     puts("GA-1024 RGB File loader program Ver 1.00 I」O DATA DEVICE Inc.");
  71.     if (argc < 2) error("usage : RGBLOAD <RGB file > -Xxxx -Yxxx");
  72.     if ((handle=open(argv[1],O_RDONLY | O_BINARY))==-1) {
  73.         error("file can't open");
  74.     }
  75.     for (i=2;i<argc;i++) {
  76.         if (argv[i][0] == '-' || argv[i][0] == '/') {
  77.             switch(argv[i][1]) {
  78.             case 'X' :
  79.             case 'x' :
  80.                 x_wid = atoi(&argv[i][2]);
  81.              break;
  82.             case 'Y' :
  83.             case 'y' :
  84.                 y_wid = atoi(&argv[i][2]);
  85.              break;
  86.             default:
  87.                 puts("パラメ-タ無効 ");
  88.             }
  89.         }
  90.     }
  91.     gmode = 1;
  92.     ginit();                /* screen init */
  93.     GAcrtSel(1);
  94.     convert(handle);
  95.     close(handle);
  96.  
  97.     while (1) {
  98.         switch (getch()) {
  99.             case 'C':
  100.             case 'c':    /* 画面モードを変える */
  101.             if (gmode & 1)
  102.                      gmode++ ;
  103.                 else 
  104.                      gmode-- ;
  105.                 GAinit(gmode);        
  106.                 GAsetVisualPlane(-1);
  107.                 wait(40);            /* CRTが同期するまで待つ */
  108.                 paletinit();
  109.                 GAsetVisualPlane(0);
  110.                 continue;
  111.             case 'Q':
  112.             case 'q':    /* パレットを変更しないで終了する */
  113.                 pal_flg = PAL_USER;
  114.                 break;
  115.             default:
  116.                 pal_flg = PAL_DEF;
  117.                 break;
  118.         }
  119.         break;
  120.     }
  121.     GAcrtSel(CRT_PC);
  122.     GAterm( pal_flg );
  123.     exit(0);
  124. }
  125.  
  126. /************************************************
  127.  
  128.     初期化
  129.  
  130. *************************************************/
  131. void ginit()
  132. {
  133.     struct VDCONFIG vd;
  134.  
  135.     if (GAinit(gmode)) {
  136.         puts("GAINST が常駐していません");
  137.         exit(1);
  138.     }
  139.     GAclrScreen();
  140.     paletinit();
  141.     GAgetVideoConfig((void far *)&vd);
  142.     gaport = vd.port    ; /* GA レジスタのポート番号 */
  143.     ga_wind = (void far *) ((long)vd.ga_seg    << 16); /*グラフィックボードウィンドウのセグメント*/
  144. }
  145.  
  146. /*********************************************************
  147.  
  148.     パレットの初期化
  149.  
  150. **********************************************************/
  151. void paletinit()
  152. {
  153.     struct PALETTE pal;
  154.     int i;
  155.     int r,g,b;
  156.  
  157. /*  R G B 6x6x6 */
  158.     i = 0 ;
  159.     for (r=0;r<6;r++){
  160.         for (g=0;g<6;g++){
  161.             for (b=0;b<6;b++){
  162.                 pal.blue = b * 0x33;
  163.                 pal.red  = r * 0x33;
  164.                 pal.green= g * 0x33;
  165.                 GAsetPalette((void far *)&pal,i++);
  166.             }
  167.         }
  168.     }
  169. }
  170.  
  171. /***************************************************
  172.  
  173.     フルカラ-のデ-タを誤差拡散により減色する
  174.  
  175. *****************************************************/
  176. void convert(int handle)
  177. {
  178.     int     x, y, i, c,idx;
  179.     short  *av1, *av2, *av3;
  180.     av1 = malloc((x_wid + 2) * sizeof *av1 * 3);
  181.     av2 = malloc((x_wid + 2) * sizeof *av2 * 3);
  182.     av3 = malloc((x_wid + 2) * sizeof *av3 * 3);
  183.     memset(av2, 0, (x_wid + 2) * sizeof *av2 * 3);
  184.     memset(av3, 0, (x_wid + 2) * sizeof *av3 * 3);
  185.  
  186.     for (y = 0; y < y_wid; y++) {
  187.         if (freadline(handle, &idx,x_wid * 3) == 0)
  188.             break;
  189.         for (x = 0; x < x_wid; x++,idx += 3) {
  190.             i = x * 3;
  191.             av1[i]     = av2[i]     + buff[idx];    /* Red */
  192.             av1[i + 1] = av2[i + 1] + buff[idx+1];  /* Green */
  193.             av1[i + 2] = av2[i + 2] + buff[idx+2]; /* Blue */
  194.             av2[i]     = av3[i];
  195.             av2[i + 1] = av3[i + 1];
  196.             av2[i + 2] = av3[i + 2];
  197.             av3[i]     = 0;
  198.             av3[i + 1] = 0;
  199.             av3[i + 2] = 0;
  200.         }
  201.         for (x = 0; x < x_wid; x++) {
  202.             i = x * 3;
  203.             c = 0;
  204.             if (av1[i] >= 0x33) {
  205.                 if (av1[i] >= 0x66) {
  206.                     if (av1[i] >= 0x99) {
  207.                         if (av1[i] >= 0xcc) {
  208.                             if (av1[i] >= 0xff) {
  209.                                 av1[i] -= 0xff;
  210.                                 c += 6 * 6 * 5;
  211.                             } else {
  212.                                 av1[i] -= 0xcc;
  213.                                 c += 6 * 6 * 4;
  214.                             }
  215.                         } else {
  216.                             av1[i] -= 0x99;
  217.                             c += 6 * 6 * 3;
  218.                         }
  219.                     } else {
  220.                         av1[i] -= 0x66;
  221.                         c += 6 * 6 * 2;
  222.                     }
  223.                 } else {
  224.                     av1[i] -= 0x33;
  225.                     c += 6 * 6 * 1;
  226.                 }
  227.             }
  228.             if (av1[i + 1] >= 0x33) {
  229.                 if (av1[i + 1] >= 0x66) {
  230.                     if (av1[i + 1] >= 0x99) {
  231.                         if (av1[i + 1] >= 0xcc) {
  232.                             if (av1[i + 1] >= 0xff) {
  233.                                 av1[i + 1] -= 0xff;
  234.                                 c += 6 * 5;
  235.                             } else {
  236.                                 av1[i + 1] -= 0xcc;
  237.                                 c += 6 * 4;
  238.                             }
  239.                         } else {
  240.                             av1[i + 1] -= 0x99;
  241.                             c += 6 * 3;
  242.                         }
  243.                     } else {
  244.                         av1[i + 1] -= 0x66;
  245.                         c += 6 * 2;
  246.                     }
  247.                 } else {
  248.                     av1[i + 1] -= 0x33;
  249.                     c += 6 * 1;
  250.                 }
  251.             }
  252.             if (av1[i + 2] >= 0x33) {
  253.                 if (av1[i + 2] >= 0x66) {
  254.                     if (av1[i + 2] >= 0x99) {
  255.                         if (av1[i + 2] >= 0xcc) {
  256.                             if (av1[i + 2] >= 0xff) {
  257.                                 av1[i + 2] -= 0xff;
  258.                                 c += 5;
  259.                             } else {
  260.                                 av1[i + 2] -= 0xcc;
  261.                                 c += 4;
  262.                             }
  263.                         } else {
  264.                             av1[i + 2] -= 0x99;
  265.                             c += 3;
  266.                         }
  267.                     } else {
  268.                         av1[i + 2] -= 0x66;
  269.                         c += 2;
  270.                     }
  271.                 } else {
  272.                     av1[i + 2] -= 0x33;
  273.                     c += 1;
  274.                 }
  275.             }
  276.             switch ((rand() & 0x0300) >> 8) {
  277.             case 0:
  278.             case 1:
  279.                 av1[i + 3]     += av1[i] / 2;
  280.                 av1[i + 3 + 1] += av1[i + 1] / 2;
  281.                 av1[i + 3 + 2] += av1[i + 2] / 2;
  282.                 av2[i]     += av1[i] / 2;
  283.                 av2[i + 1] += av1[i + 1] / 2;
  284.                 av2[i + 2] += av1[i + 2] / 2;
  285.                 break;
  286.             case 2:
  287.                 av1[i + 3]     += av1[i] / 4 * 3;
  288.                 av1[i + 3 + 1] += av1[i + 1] / 4 * 3;
  289.                 av1[i + 3 + 2] += av1[i + 2] / 4 * 3;
  290.                 av2[i]     += av1[i] / 4;
  291.                 av2[i + 1] += av1[i + 1] / 4;
  292.                 av2[i + 2] += av1[i + 2] / 4;
  293.                 break;
  294.             case 3:
  295.                 av1[i + 3]     += av1[i] / 4;
  296.                 av1[i + 3 + 1] += av1[i + 1] / 4;
  297.                 av1[i + 3 + 2] += av1[i + 2] / 4;
  298.                 av2[i]     += av1[i] / 4 * 3;
  299.                 av2[i + 1] += av1[i + 1] / 4 * 3;
  300.                 av2[i + 2] += av1[i + 2] / 4 * 3;
  301.                 break;
  302.             default:
  303.                 break;
  304.             }
  305.             buff2[x] = c;
  306.         }
  307.         GArestoreImage(0,y,x_wid-1,y,1,(void far *)buff2);  
  308.     }
  309.  
  310. /**************************************************
  311.  
  312.     エラ-処理
  313.  
  314. ***************************************************/
  315. void error(s)
  316. char *s;
  317. {
  318.     puts(s);
  319.     GAcrtSel(0);
  320.     exit(1);
  321. }
  322.  
  323.  
  324. /*****************************************************************
  325.  
  326.     1ライン分ファイルリード
  327.  
  328. ******************************************************************/
  329. int freadline( handl,idx,size)
  330. int  handl;
  331. int *idx;
  332. int  size;
  333. {
  334.     static int fp = 0;
  335.     static int remain = 0;
  336.     int    ret = size;
  337.     
  338.     if (remain < size) {
  339.         fp = 0;
  340.         remain = ( SIZE_OF_BUFF / size ) * size;
  341.         ret = read(handl,buff,remain);
  342.     } 
  343.     *idx = fp;
  344.     fp += size;
  345.     remain -= size;
  346.     return(ret);
  347. }
  348.  
  349.  
  350.  
  351. /* グラフィック コントロ-ル レジスタ ポ-ト */
  352. #define CRTC_AR 0x1e00
  353. #define CRTC_CR 0x1f00
  354. /***********************************************************
  355.  
  356.     CRTC の垂直同期信号をカウントしてウエイトする
  357.  
  358. ************************************************************/
  359. void wait(int count) 
  360. {
  361.     int  i;
  362.     for (i=0 ; i < count ; i++) {
  363. #ifdef TURBO
  364.         outportb(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  365.         while ( inportb(CRTC_CR+gaport) & 2) ;
  366.         while ( !(inportb(CRTC_CR+gaport) & 2)) ;
  367. #else
  368.         outp(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  369.         while ( inp(CRTC_CR+gaport) & 2) ;
  370.         while ( !(inp(CRTC_CR+gaport) & 2)) ;
  371. #endif
  372.     }
  373. }
  374.  
  375.  
  376.