home *** CD-ROM | disk | FTP | other *** search
/ FreeWare Collection 2 / FreeSoftwareCollection2pd199x-jp.img / prnout / src / img.c < prev    next >
Text File  |  1990-06-14  |  11KB  |  485 lines

  1. /*****************************************
  2.  
  3.     グラフィック・イメ-ジをプリンタに出力
  4.  
  5.     1989.12.08          make by Ken
  6.  
  7. *******************************************/
  8. #include    <stdio.h>
  9. #include    "defs.h"
  10.  
  11. #define DSP_X   0
  12. #define DSP_Y   19
  13.  
  14. typedef struct {
  15.     short int   tf_tag;
  16.     short int   tf_type;
  17.     long int    tf_len;
  18.     long int    tf_data;
  19. } TIF_IFD;
  20.  
  21. extern char    *subname();
  22. extern char    skl_chr[];
  23.  
  24. static int     off_x=0;
  25. static int     spc_x=0;
  26. static int     off_y=0;
  27. static int     max_x=0;
  28. static int     max_y=0;
  29. static int     img_s=0;
  30. static int     pam_x=0;
  31. static int     big_cnt=4;
  32. static int     skl_ptr=0;
  33. static int     skl_cnt=0;
  34. static UNSIG   bit2_ptn[]={ 0x00,0x10,0x21,0x13,0x33 };
  35. static UNSIG   bit3_ptn[]={
  36.     0x000,0x020,0x202,0x141,0x252,0x525,0x563,0x573,0x757,0x777 };
  37. static UNSIG   bit4_ptn[]={
  38.     0x0000, 0x0020, 0x0204, 0x2042, 0x4218, 0x4252, 0x2495, 0x5a45,
  39.     0xa5a5, 0xa7a5, 0xada7, 0xbdb8, 0xdbeb, 0xfd7d, 0xf7bf, 0xfbff,
  40.     0xffff };
  41. static UCHAR   img_buf[4096 * 3];
  42. static struct {
  43.     UCHAR   b,r,g;
  44. } pal_tbl[256];
  45.  
  46. int    swap(p)
  47. char    *p;
  48. {
  49.     long    i;
  50.     char    *s;
  51.  
  52.     s = (char *)&i;
  53.     p += 4;
  54.     *(s++) = *(--p); 
  55.     *(s++) = *(--p); 
  56.     *(s++) = *(--p); 
  57.     *(s++) = *(--p); 
  58.     return i;
  59. }
  60. int    dsp_skl()
  61. {
  62.     skl_ptr += 272;
  63.     while ( skl_ptr >= max_y ) {
  64.         locate(DSP_X+1+(skl_cnt / 8),DSP_Y+1);
  65.         repchr(1,skl_chr[skl_cnt % 8]);
  66.         skl_ptr -= max_y;
  67.         skl_cnt++;
  68.     }
  69.     if ( kbhit() != 0 ) {
  70.     getch();
  71.     return ERR;
  72.     }
  73.     return FALSE;
  74. }
  75. void    dsp_img(x,y,fc)
  76. int     x,y;
  77. {
  78.     x *= big_cnt;
  79.     y *= big_cnt;
  80.     x = (x >> 3) + off_x;
  81.     y = (y >> 3) + off_y;
  82.  
  83.     line(off_x,off_y,x,y,PSET,0,fc,LINE_3);
  84. }
  85. void    dsp_bit(x,y,c)
  86. int    x,y,c;
  87. {
  88.     static int old_c=0,old_x;
  89.  
  90.     if ( x == 0 )
  91.     old_c = c;
  92.     old_c = (old_c + c) / 2;
  93.     x *= big_cnt;
  94.     y *= big_cnt;
  95.     x = (x >> 3) + off_x;
  96.     y = (y >> 3) + off_y;
  97.     if ( old_x != x )
  98.         line(x,y,x,y,PSET,15 - (old_c >> 4),LINE,LINE_1);
  99.     old_x = x;
  100. }
  101. void    prn_img(x,y)
  102. int     x,y;
  103. {
  104.     char    tmp[20];
  105.  
  106.     if ( typ_flg == DUMMY )
  107.         return;
  108.  
  109.     x *= big_cnt;
  110.     fnt_spc(spc_x);
  111.     switch(typ_flg) {
  112.     case FUJITU:
  113.         sprintf(tmp,"\x1bQ%d W",x);
  114.         prn_line_out(tmp);
  115.         break;
  116.     case ESCP:
  117.         PRB_byte("\x1b\x2a\x27",3);
  118.         PRB_chr(x & 0xFF);
  119.         PRB_chr(x >> 8);
  120.         break;
  121.     case PC98:
  122.         sprintf(tmp,"\x1bJ%04d",x);
  123.         PRB_byte(tmp,6);
  124.         break;
  125.     }
  126.     PRB_byte(img_buf,x * 3);
  127.     prn_line_out("\x0D\x0A");
  128.     memset(img_buf,0,4096*3);
  129. }
  130. void    set_img(x,y,r,g,b)
  131. int     x,y,r,g,b;
  132. {
  133.     int     cl,ptn,bit,i;
  134.     UCHAR   *p;
  135.  
  136.     cl = (25500 - (r * 30 + g * 59 + b * 11)) / 100;
  137.     dsp_bit(x,y,cl);
  138.     cl >>= (8 - big_cnt);
  139.  
  140.     if ( big_cnt == 1 ) {
  141.         y %= 24;
  142.         p = img_buf + x * 3 + y / 8;
  143.         if ( cl > 0 )
  144.             *p |= (0x80 >> (y % 8));
  145.     } else if ( big_cnt == 2 ) {
  146.     y %= 12;
  147.     p = img_buf + x * 6 + (y / 4);
  148.     ptn = bit2_ptn[cl];
  149.         for ( i = 0 ; i < 2 ; i++ ) {
  150.             bit = ptn & 0x0003; ptn >>= 4;
  151.         bit <<= (6 - ((y % 4) * 2));
  152.             *p |= bit;
  153.             p += 3;
  154.         }
  155.     } else if ( big_cnt == 3 ) {
  156.         y %= 8;
  157.         p = img_buf + x * 9 + (y / 3);
  158.     ptn = bit3_ptn[cl];
  159.     for ( i = 0 ; i < 3 ; i++ ) {
  160.         bit = ptn & 0x0007; ptn >>= 4;
  161.         switch(y) {
  162.         case 0: bit <<= 5; *p |= bit; break;
  163.         case 1: bit <<= 2; *p |= bit; break;
  164.         case 2: *p |= (bit >> 1); bit <<= 7; *(p+1) |= bit; break;
  165.         case 3: bit <<= 4; *p |= bit; break;
  166.         case 4: bit <<= 1; *p |= bit; break;
  167.         case 5: *p |= (bit >> 2); bit <<= 6; *(p+1) |= bit; break;
  168.         case 6: bit <<= 3; *p |= bit; break;
  169.         case 7: *p |= bit; break;
  170.         }
  171.             p += 3;
  172.         }
  173.     } else if ( big_cnt == 4 ) {
  174.         y %= 6;
  175.         p = img_buf + x * 12 + (y / 2);
  176.     ptn = bit4_ptn[cl];
  177.     for ( i = 0 ; i < 4 ; i++ ) {
  178.         bit = ptn & 0x000F; ptn >>= 4;
  179.             if ( (y & 1) == 0 )
  180.                 bit <<= 4;
  181.             *p |= bit;
  182.             p += 3;
  183.         }
  184.     }
  185.     PRB_out();
  186. }
  187. void    TIF_get1(fp)
  188. FILE    *fp;
  189. {
  190.     int     cl;
  191.     UCHAR   bit,mask;
  192.     int     x,y,i;
  193.  
  194.     mask = 0;
  195.     for ( i = y = 0 ; y < max_y ; y++ ) {
  196.     for ( x = 0 ; x < max_x ; x++ ) {
  197.         if ( (mask >>= 1) == 0 ) {
  198.         fread(&bit,1,1,fp);
  199.         mask = 0x80;
  200.         }
  201.         cl = ((bit & mask) == 0 ? 0:255);
  202.         set_img(x,y,cl,cl,cl);
  203.     }
  204.     if ( ++i == (24 / big_cnt) ) {
  205.         prn_img(max_x,y);
  206.         i = 0;
  207.     }
  208.     if ( dsp_skl() != FALSE )
  209.         return;
  210.     }
  211.     if ( i > 0 )
  212.     prn_img(max_x,y);
  213. }
  214. void    TIF_get4(fp)
  215. FILE    *fp;
  216. {
  217.     UCHAR   ch;
  218.     int     cl;
  219.     int     x,y,i;
  220.     static struct {
  221.     short int   r,g,b;
  222.     } ct[]={
  223.     {0,0,0},{0,0,128},{128,0,0},{128,0,128},
  224.     {0,128,0},{0,128,128},{128,128,0},{128,128,128},
  225.     {64,64,64},{0,0,255},{255,0,0},{255,0,255},
  226.     {0,255,0},{0,255,255},{255,255,0},{255,255,255}
  227.     };
  228.  
  229.     for ( i = y = 0 ; y < max_y ; y++ ) {
  230.     for ( x = 0 ; x < max_x ; x += 2 ) {
  231.         fread(&ch,1,1,fp);
  232.         cl = ch & 0x0F;
  233.         set_img(x,y,ct[cl].r,ct[cl].g,ct[cl].b);
  234.         cl = ch >> 4;
  235.         set_img(x+1,y,ct[cl].r,ct[cl].g,ct[cl].b);
  236.     }
  237.     if ( ++i == (24 / big_cnt) ) {
  238.         prn_img(max_x,y);
  239.         i = 0;
  240.     }
  241.     if ( dsp_skl() != FALSE )
  242.         return;
  243.     }
  244.     if ( i > 0 )
  245.     prn_img(max_x,y);
  246. }
  247. void    TIF_get8(fp)
  248. FILE    *fp;
  249. {
  250.     UCHAR   bit;
  251.     int     x,y,i;
  252.  
  253.     for ( i = y = 0 ; y < max_y ; y++ ) {
  254.     for ( x = 0 ; x < max_x ; x++ ) {
  255.         fread(&bit,1,1,fp);
  256.         set_img(x,y,pal_tbl[bit].r,pal_tbl[bit].g,pal_tbl[bit].b);
  257.     }
  258.     if ( ++i == (24 / big_cnt) ) {
  259.         prn_img(max_x,y);
  260.         i = 0;
  261.     }
  262.     if ( dsp_skl() != FALSE )
  263.         return;
  264.     }
  265.     if ( i > 0 )
  266.     prn_img(max_x,y);
  267. }
  268. void    TIF_get16(fp)
  269. FILE    *fp;
  270. {
  271.     short int bit;
  272.     int     r,g,b;
  273.     int     x,y,i;
  274.  
  275.     for ( i = y = 0 ; y < max_y ; y++ ) {
  276.     for ( x = 0 ; x < max_x ; x++ ) {
  277.         fread(&bit,1,2,fp);
  278.         b = (bit & 0x1F) << 3; bit >>= 5;
  279.         r = (bit & 0x1F) << 3; bit >>= 5;
  280.         g = (bit & 0x1F) << 3;
  281.         set_img(x,y,r,g,b);
  282.     }
  283.     if ( ++i == (24 / big_cnt) ) {
  284.         prn_img(max_x,y);
  285.         i = 0;
  286.     }
  287.     if ( dsp_skl() != FALSE )
  288.         return;
  289.     }
  290.     if ( i > 0 )
  291.     prn_img(max_x,y);
  292. }
  293. void    gra_start()
  294. {
  295.     int     i;
  296.  
  297.     pp_box(0,0);
  298.     locate(DSP_X+1,DSP_Y+1);
  299.     repchr(34,' ');
  300.     locate(DSP_X+1,DSP_Y+3);
  301.     printf("%-34s","只今グラフィック・デ-タを印刷してます");
  302.     switch(typ_flg) {
  303.     case FUJITU:
  304.         prn_line_out("\x1bQ3 \x5c\x1c%\x22\x74");
  305.         break;
  306.     case ESCP:
  307.         PRB_byte("\x1B\x33\x18",3);
  308.         break;
  309.     case PC98:
  310.         prn_line_out("\x1bT22");
  311.         break;
  312.     }
  313.     if ( typ_flg != DUMMY ) {
  314.     for ( i = yousi[paper].pmrg ; i > 0 ; i-- )
  315.             prn_line_out("\x0D\x0A");
  316.     }
  317.     memset(img_buf,0,4096*3);
  318. }
  319. void    big_setup()
  320. {
  321.     big_cnt = pam_x / max_x;
  322.     if ( big_cnt <= 0 ) big_cnt = 1;
  323.     if ( big_cnt > 4 ) big_cnt = 4;
  324.     dsp_img(max_x,max_y,SBOX);
  325. }
  326. void    gra_end()
  327. {
  328.     if ( typ_flg != DUMMY )
  329.     PRB_chr('\x0C');
  330.     locate(DSP_X+1,DSP_Y+3);
  331.     printf("%-34s","グラフィックの印刷を終了しました");
  332. }
  333. void    pal_init()
  334. {
  335.     int     i,bit;
  336.  
  337.     for ( i = 0 ; i < 256 ; i++ ) {
  338.     bit = i;
  339.     pal_tbl[i].b = (bit & 0x07) << 5; bit >>= 3;
  340.     pal_tbl[i].r = (bit & 0x07) << 5; bit >>= 3;
  341.     pal_tbl[i].g = (bit & 0x03) << 6;
  342.     }
  343. }
  344. int    TIF_img(fp)
  345. FILE    *fp;
  346. {
  347.     short int  i;
  348.     long    l;
  349.     TIF_IFD ifd;
  350.     char    tmp[8];
  351.  
  352.     gra_start();
  353.     if ( fread(tmp,1,4,fp) < 4 )
  354.     return ERR;
  355.     if ( tmp[0] != 0x49 || tmp[1] != 0x49 || tmp[2] != 0x2A )
  356.     return ERR;
  357.     if ( fread(&l,4,1,fp) < 1 )
  358.     return ERR;
  359.     fseek(fp,l,0);
  360.     if ( fread(&i,2,1,fp) < 1 )
  361.     return ERR;
  362.     while ( i-- > 0) {
  363.     if ( fread(&ifd,sizeof(TIF_IFD),1,fp) < 1 )
  364.         return ERR;
  365.     if ( ifd.tf_tag == 0 )
  366.         break;
  367.     switch(ifd.tf_tag){
  368.         case 0x0100: max_x = ifd.tf_data; break;
  369.         case 0x0101: max_y = ifd.tf_data; break;
  370.         case 0x0102: img_s = ifd.tf_data; break;
  371.         case 0x0111: l = ifd.tf_data; break;
  372.     }
  373.     }
  374.     big_setup();
  375.     fseek(fp,l,0);
  376.     switch(img_s) {
  377.     case 1:  TIF_get1(fp); break;
  378.     case 4:  TIF_get4(fp); break;
  379.     case 8:  pal_init(); TIF_get8(fp); break;
  380.     case 16: TIF_get16(fp); break;
  381.     }
  382.     gra_end();
  383.     return FALSE;
  384. }
  385. void    PIC_img(fp)
  386. FILE    *fp;
  387. {
  388.     gra_start();
  389.     max_x = 320; max_y = 240;
  390.     img_s = 16;
  391.     big_setup();
  392.     TIF_get16(fp);
  393.     gra_end();
  394. }
  395. void    GED_img(fp)
  396. FILE    *fp;
  397. {
  398.     short int    i;
  399.  
  400.     gra_start();
  401.     fseek(fp,0x000Cl,0);
  402.     if ( fread(&i,2,1,fp) < 1 )
  403.     return;
  404.     max_x = i + 1;
  405.     if ( fread(&i,2,1,fp) < 1 )
  406.     return;
  407.     max_y = i + 1;
  408.     img_s = 16;
  409.     fseek(fp,0x0100l,0);
  410.     big_setup();
  411.     TIF_get16(fp);
  412.     gra_end();
  413. }
  414. void    GRP_img(fp)
  415. FILE    *fp;
  416. {
  417.     long    l;
  418.     char    tmp[8];
  419.  
  420.     gra_start();
  421.     if ( fread(tmp,1,4,fp) < 4 )
  422.     return;
  423.     if ( fread(tmp,1,4,fp) < 4 )
  424.     return;
  425.     max_x = swap(tmp);
  426.     if ( fread(tmp,1,4,fp) < 4 )
  427.     return;
  428.     max_y = swap(tmp);
  429.     if ( fread(tmp,1,4,fp) < 4 )
  430.     return;
  431.     if ( (img_s = swap(tmp)) == 15 )
  432.     img_s = 16;
  433.     switch(img_s) {
  434.     case 4:
  435.         l = 0x0050l;
  436.         break;
  437.     case 8:
  438.         fseek(fp,0x0020l,0);
  439.         fread(pal_tbl,1,768,fp);
  440.         l = 0x0320l; 
  441.         break;
  442.     case 16:
  443.         l = 0x0020l;
  444.         break;
  445.     }
  446.     fseek(fp,l,0);
  447.     big_setup();
  448.     switch(img_s) {
  449.     case 4:  TIF_get4(fp); break;
  450.     case 8:  TIF_get8(fp); break;
  451.     case 16: TIF_get16(fp); break;
  452.     }
  453.     gra_end();
  454. }
  455. int     gra_img(fp,file)
  456. FILE    *fp;
  457. char    *file;
  458. {
  459.     char    *p;
  460.  
  461.     off_x = (yousi[paper].lmrg * (yousi[paper].spc + 24));
  462.     spc_x = off_x / 2;
  463.     off_x += (printer[typ_flg].yoko * 1800L) / 254L;
  464.     off_x = (off_x >> 4) + GRA_OFFX;
  465.     pam_x = (yousi[paper].lmax * (yousi[paper].spc + 24)) / 2;
  466.  
  467.     off_y = (printer[typ_flg].tate * 1800L) / 254L;
  468.     off_y += (yousi[paper].pmrg * (yousi[paper].lf + 24));
  469.     off_y = (off_y >> 3) + GRA_OFFY;
  470.     skl_cnt = skl_ptr = 0;
  471.  
  472.     p = subname(file);
  473.     if ( strcmp(p,"TIF") == 0 )
  474.         TIF_img(fp);
  475.     else if ( strcmp(p,"PIC") == 0 )
  476.         PIC_img(fp);
  477.     else if ( strcmp(p,"GED") == 0 || strcmp(p,"FIG") == 0 )
  478.         GED_img(fp);
  479.     else if ( strcmp(p,"GRP") == 0 )
  480.         GRP_img(fp);
  481.     else
  482.         return FALSE;
  483.     return TRUE;
  484. }
  485.