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

  1. /************************************************************************
  2.     GA-1024A / GA-1280A グラフィックライブラリ サンプルソース
  3.         BMPSAVE.C
  4.     
  5.     BMP FILE 画像セ-ブプログラム  
  6.  
  7.                 アイ・オー・データ機器 Device Inc...
  8. *************************************************************************/
  9. #include <fcntl.h>
  10.  
  11. #ifndef TURBO
  12. #include <sys\types.h>
  13. #endif
  14.  
  15. #include <sys\stat.h>
  16. #include <io.h>
  17. #include "gagraph.h"
  18.  
  19. /*
  20.  * header file for .bmp file header
  21.  */
  22. typedef    unsigned short    WORD;
  23. typedef    unsigned long    DWORD;
  24.  
  25. typedef    struct    tagBITMAPFILEHEADER {
  26.     WORD    bfType;
  27.     DWORD    bfSize;
  28.     WORD    bfReserved1;
  29.     WORD    bfReserved2;
  30.     DWORD    bfOffBits;
  31. } BITMAPFILEHEADER;
  32.  
  33. typedef    struct    tagBITMAPINFOHEADER {
  34.     DWORD    biSize;
  35.     DWORD    biWidth;
  36.     DWORD    biHeight;
  37.     WORD    biPlanes;
  38.     WORD    biBitCount;
  39.     DWORD    biCompression;
  40.     DWORD    biSizeImage;
  41.     DWORD    biXPelsPerMeter;
  42.     DWORD    biYPelsPerMeter;
  43.     DWORD    biCirUsed;
  44.     DWORD    biCirImportant;
  45. } BITMAPINFOHEADER;
  46.  
  47. typedef unsigned char  UCHAR;
  48. typedef unsigned short USHORT;
  49. typedef unsigned int   UINT;
  50. typedef unsigned long  ULONG;
  51.  
  52. #define SIZE_OF_BUFF 1024
  53. #define    COL_CNT      256
  54.  
  55. #define swap(x,y) { int z ; z = x ; x = y ; y = z; }
  56.  
  57. int     handle;        /* DOS file handle */
  58. UCHAR   buff[SIZE_OF_BUFF]; /* file buffer */
  59. UCHAR   lut[COL_CNT*3];      /* palette */
  60. int        x_wid = 320;        /* screen size */
  61. int        y_wid = 240;
  62. int     x_start = 0;
  63. int     y_start = 0;
  64. int     gmode = 0;
  65.  
  66. static UINT  gaport;
  67. static volatile UCHAR far *ga_wind;
  68.  
  69. BITMAPFILEHEADER bf = {
  70.     0x4d42,                /* ID ('B', 'M') */
  71.     0x00000000,            /* file size */
  72.     0x0000,                /* reserved */
  73.     0x0000,                /* reserved */
  74.     sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + COL_CNT * 4
  75. };
  76.  
  77. BITMAPINFOHEADER bi = {
  78.     sizeof(BITMAPINFOHEADER),    /* header size */
  79.     0x00000000,            /* width */
  80.     0x00000000,            /* height */
  81.     0x0001,                /* planes */
  82.     0x0008,                /* bit count */
  83.     0x00000000,            /* compression */
  84.     0x00000000,            /* size of image */
  85.     0x00000000,            /* x-resolution */
  86.     0x00000000,            /* y-resolution */
  87.     COL_CNT,            /* color index count */
  88.     0x00000000            /* important index count */
  89. };
  90.  
  91. UCHAR col_idx[COL_CNT][4]; /* palette data */
  92.  
  93. extern int    far GetMouse(int far *,int far *,int far *,int far *);
  94. extern int    far InitMouse();
  95.  
  96.  
  97. void ginit();
  98. void paletinit();
  99. void paletget();
  100. void hdrinit();
  101. void save(int ,int ,int,int);
  102. void error();
  103. void wait(int);
  104.  
  105. /********************************************
  106.  
  107.     main()
  108.  
  109. *********************************************/
  110. void main(argc,argv)
  111. int    argc;
  112. char    *argv[];
  113. {
  114.     int i,x,y,sx,sy,ox,oy,right,left,on;
  115.  
  116.     ox = oy = sx = sy =0;
  117.     puts("GA-1024A/GA-1280A BMP File Saver program Ver 1.00  I・O DATA DEVICE Inc");
  118.     if (argc < 2) error("usage : BMPSAVE <dst file> ");
  119.     ginit();                /* screen init */
  120.     GAcrtSel(1);
  121.     if (InitMouse()) {
  122.         error("マウスが使用できません");
  123.     }
  124.     on = 0;
  125.     gmode=1;
  126.     while (1) {
  127.         if (kbhit()) {
  128.             switch (getch()) {
  129.             case 'S':
  130.             case 's':
  131.                 break;
  132.             case 'C':
  133.             case 'c':   /* 画面モードを変える */
  134.             if (gmode & 1)
  135.                      gmode++ ;
  136.                 else 
  137.                      gmode-- ;
  138.                 GAinit(gmode);        
  139.                 GAsetVisualPlane(-1);
  140.                 wait(40);            /* CRTが同期するまで待つ */
  141.                 paletinit();
  142.                 GAsetVisualPlane(0);
  143.                 continue;
  144.             default:
  145.                 error("save aborted.");
  146.             }
  147.             break;
  148.         } else {
  149.             GetMouse(&x, &y, &left, &right);
  150.             if (x != ox || y != oy) GAdrawCursor(x,y);
  151.             if (on) {    /* 既に左ボタンを押している */
  152.                 if (right) { /* 右を押したらキャンセル */
  153.                     GAeraseCursor();
  154.                     GAbox(sx,sy,ox,oy,-1,DASH_LINE);
  155.                     GAdrawCursor(x,y);
  156.                     on = 0;
  157.                 } else if (left) { /* 左ボタンを押し続ける */
  158.                     if (x != ox || y != oy) {
  159.                         GAeraseCursor();
  160.                         GAbox(sx,sy,ox,oy,-1,DASH_LINE);
  161.                         GAbox(sx,sy,x,y,-1,DASH_LINE);
  162.                         GAdrawCursor(x,y);
  163.                     }
  164.                 } else {
  165.                     if (sx > ox) swap(sx,ox);
  166.                     if (sy > oy) swap(sy,oy);
  167.                     x_wid   = ox - sx + 1;
  168.                     y_wid   = oy - sy + 1;
  169.                     x_start = sx;
  170.                     y_start = sy;
  171.                     GAeraseCursor();
  172.                     GAbox(sx,sy,ox,oy,-1,DASH_LINE);
  173.                     break;
  174.                 }
  175.             } else {
  176.                 if (left) {
  177.                     sx = x;
  178.                     sy = y;
  179.                     on = 1;
  180.                     GAeraseCursor();
  181.                     GAbox(sx,sy,x,y,-1,DASH_LINE);
  182.                 }
  183.             }
  184.             ox = x;
  185.             oy = y;
  186.         }
  187.     }
  188.     GAeraseCursor();
  189.     GAcrtSel(0);
  190.     if ((handle=open(argv[1],O_CREAT | O_WRONLY | O_BINARY | O_TRUNC,S_IREAD | S_IWRITE))==-1) {
  191.         error("file can't open");
  192.     }
  193.     puts("now saving.");
  194.     hdrinit();
  195.     save(x_start,y_start,x_wid,y_wid);
  196.     close(handle);
  197.     GAterm( PAL_DEF );
  198.     exit(0);
  199. }
  200.  
  201. /****************************************
  202.  
  203.     初期化
  204.  
  205. *****************************************/
  206. void ginit()
  207. {
  208.     struct VDCONFIG vd;
  209.  
  210.     if (GAinit(gmode)) {
  211.         puts("GAINST が常駐していません");
  212.         exit(1);
  213.     }
  214.     paletget();
  215.     GAgetVideoConfig((void far *)&vd);
  216.     gaport = vd.port    ; /* GA レジスタのポート番号 */
  217.     ga_wind = (void far *) ((long)vd.ga_seg    << 16); /*グラフィックボードウィンドウのセグメント*/
  218. }
  219.  
  220. /************************************************
  221.  
  222.     パレット設定
  223.  
  224. *************************************************/
  225. void paletinit()
  226. {
  227.     int i;
  228.     for (i = 0 ; i<COL_CNT;i++) {
  229.         GAsetPalette((void far *)&lut[i*3],i);
  230.     }
  231. }
  232.  
  233. /****************************************************
  234.  
  235.     パレットデ-タを得る
  236.  
  237. ****************************************************/
  238. void paletget()
  239. {
  240.     int i;
  241.  
  242.     for (i = 0 ; i<COL_CNT;i++) {
  243.         GAgetPalette((void far *)&lut[i*3],i);
  244.     }
  245. }
  246.  
  247. /****************************************************
  248.  
  249.     BMPファイルのヘッダを作る
  250.  
  251. ****************************************************/
  252. void    hdrinit()
  253. {
  254.     int     i;
  255.  
  256.     bi.biWidth = (ULONG)x_wid;
  257.     bi.biHeight = (ULONG)y_wid;
  258.     bi.biSizeImage = bi.biWidth * bi.biHeight;
  259.     bf.bfSize = bf.bfOffBits + bi.biSizeImage;
  260.     for (i = 0; i < COL_CNT ; i++) {
  261.         col_idx[i][0] = lut[i*3+2]; /* BLUE  */
  262.         col_idx[i][1] = lut[i*3+1]; /* GREEN */
  263.         col_idx[i][2] = lut[i*3+0]; /* RED   */
  264.         col_idx[i][3] = 0x00;
  265.     }
  266. }
  267.  
  268.  
  269. /********************************************************
  270.  
  271.     GA-1024A/GA-1280Aの画像をファイルに書き込む
  272.  
  273. *********************************************************/
  274. void    save(int xstart,int ystart,int xwid,int ywid)
  275. {
  276.     int y;
  277.     int scan;
  278.  
  279.     scan = (xwid + 3) & 0xfffc ;
  280.     write(handle, (char *)&bf, sizeof bf);
  281.     write(handle, (char *)&bi, sizeof bi);
  282.     write(handle, (char *)col_idx[0], sizeof col_idx);
  283.     for (y = ystart+ywid - 1; y >= ystart; y--) {
  284.         GAsaveImage(xstart, y, xstart+scan-1, y, 1, buff);
  285.         write(handle,buff,scan);
  286.     }
  287. }
  288.  
  289. /********************************************************
  290.  
  291.     エラ-処理
  292.  
  293. *********************************************************/
  294. void error(s)
  295. char *s;
  296. {
  297.     puts(s);
  298.     GAcrtSel(0);
  299.     exit(1);
  300. }
  301.  
  302.  
  303. /* グラフィック コントロ-ル レジスタ ポ-ト */
  304. #define CRTC_AR 0x1e00
  305. #define CRTC_CR 0x1f00
  306. /***********************************************************
  307.  
  308.     CRTC の垂直同期信号をカウントしてウエイトする
  309.  
  310. ************************************************************/
  311. void wait(int count) 
  312. {
  313. #if 0
  314.  
  315.     int  i;
  316.     for (i=0 ; i < count ; i++) {
  317. #ifdef TURBO
  318.         outportb(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  319.         while ( inportb(CRTC_CR+gaport) & 2) ;
  320.         while ( !(inportb(CRTC_CR+gaport) & 2)) ;
  321. #else
  322.         outp(CRTC_AR+gaport,31);       /* CRTC STATUS register */
  323.         while ( inp(CRTC_CR+gaport) & 2) ;
  324.         while ( !(inp(CRTC_CR+gaport) & 2)) ;
  325. #endif
  326.     }
  327. #endif
  328. }
  329.  
  330.