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

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