home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / fract3.zip / TARGA.C < prev    next >
C/C++ Source or Header  |  1989-06-25  |  7KB  |  347 lines

  1. /** targa.c 
  2.  
  3.  
  4. **/
  5.  
  6.  
  7. #ifdef __TURBOC__
  8. #    pragma    warn -par
  9. #endif
  10.  
  11.  
  12. #include    <stdio.h>
  13. #include    <stdlib.h>
  14. #include    <dos.h>
  15. #include    "targa.h"
  16.  
  17. #ifndef true
  18. #    define    true    1
  19. #    define    false    0
  20. #endif
  21.  
  22. /*************  ****************/
  23.  
  24. extern unsigned int _dataseg;
  25.  
  26. extern char far runningontarga[];
  27.  
  28. extern void    helptitle();
  29. extern void    helpmessage(unsigned char far *);
  30. extern int    LoadColorMap( void );
  31.  
  32. /*************  ****************/
  33.  
  34. typedef unsigned char uchar;
  35.  
  36. /*************  ****************/
  37.  
  38. void    WriteTGA( int x, int y, int index );
  39. int        ReadTGA ( int x, int y );
  40. void    LineTGA ( uchar far * lineData, int y, int cnt );
  41. void    EndTGA  ( void );
  42. void    StartTGA( int xDim, int yDim );
  43.  
  44. /*************  ****************/
  45.  
  46. int                xorTARGA;
  47. int                targaType;
  48. unsigned    tga16[ 256 ];
  49. long            tga32[ 256 ];
  50.  
  51. /*************  ****************/
  52.  
  53. static int    xdots;
  54. static int    ydots;
  55. static int    initialized;
  56.  
  57. /*************  ****************/
  58.  
  59. void            (*DoPixel) ( int x, int y, int index );
  60. void            (*PutPixel)( int x, int y, int index );
  61. void            (*PutLine) ( uchar far * lineData, int y, int cnt );
  62. unsigned    (*GetPixel)( int x, int y );
  63.  
  64. /**************************************************************************/
  65.  
  66. void
  67. TErase( color )
  68. int color;
  69. {
  70. int cnt, max, seg;
  71.  
  72.     max = targa->MaxBanks;
  73.     seg = targa->memloc;
  74.     for( cnt = 0; cnt < max; cnt += 2 ) {
  75.         outp( DESTREG, cnt );
  76.         outp( SRCREG,  cnt + 1 );
  77.         erasesegment(seg,color);        /** general.asm ?? **/
  78.     }
  79. }
  80.  
  81.  
  82. /**************************************************************************/
  83.  
  84. unsigned
  85. Row16Calculate( unsigned line, unsigned x1 )
  86. {
  87. unsigned page, offset, pixel;
  88.  
  89.     page = (line >> 5) & 15;
  90.     outp( DESTREG, page );
  91.     offset = ((line - (page << 5)) << 10) | (x1 << 1);    /* calc the pixel offset */
  92.     return( offset );
  93. }
  94.  
  95.  
  96. /**************************************************************************/
  97.  
  98. void
  99. PutPix16( int x, int y, int index )
  100. {
  101. unsigned far * ip;
  102.  
  103.     /**************/
  104. #ifdef __TURBOC__
  105.     ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
  106. #else
  107.     FP_SEG(ip) = MEMSEG;
  108.     FP_OFF(ip) = Row16Calculate( y, x );
  109. #endif
  110.     if( ! xorTARGA )
  111.         *ip = tga16[index];
  112.     else
  113.         *ip = *ip ^ 0x7fff;
  114. }
  115.  
  116.  
  117. /**************************************************************************/
  118.  
  119. unsigned
  120. GetPix16( int x, int y )
  121. {
  122. unsigned    pixel, index;
  123. unsigned far * ip;
  124. static unsigned last;
  125.     /**************/
  126. #ifdef __TURBOC__
  127.     ip = MK_FP( MEMSEG, Row16Calculate( y, x ) );
  128. #else
  129.     FP_SEG(ip) = MEMSEG;
  130.     FP_OFF(ip) = Row16Calculate( y, x );
  131. #endif
  132.     pixel = *ip & 0x7FFF;
  133.     if( pixel == tga16[last] ) return( last );
  134.     for( index = 0; index < 256; index++ )
  135.         if( pixel == tga16[index] ) {
  136.             last = index;
  137.             return( index );
  138.         }
  139.     return( 0 );
  140. }
  141.  
  142.  
  143. /**************************************************************************/
  144.  
  145. unsigned
  146. Row32Calculate( unsigned line, unsigned x1 )
  147. {
  148. int page, offset, pixel;
  149.  
  150.     page = (line >> 4) & 31;
  151.     outp( DESTREG, page );
  152.     offset = ((line - (page << 4)) << 11) | (x1 << 2);    /* calc the pixel offset */
  153.     return( offset );
  154. }
  155.  
  156.  
  157. /**************************************************************************/
  158.  
  159. void
  160. PutPix32( int x, int y, int index )
  161. {
  162. long far * lp;
  163.     /**************/
  164. #ifdef __TURBOC__
  165.     lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
  166. #else
  167.     FP_SEG(lp) = MEMSEG;
  168.     FP_OFF(lp) = Row32Calculate( y, x );
  169. #endif
  170.  
  171. /****
  172. printf( "%lx\n", tga32[index] );
  173. ****/
  174.  
  175.     if( ! xorTARGA )
  176.         *lp = tga32[index];
  177.     else
  178.         *lp = *lp ^ 0x00FFFFFF;
  179. }
  180.  
  181.  
  182. /**************************************************************************/
  183.  
  184. unsigned
  185. GetPix32( int x, int y )
  186. {
  187. int        index;
  188. long    pixel;
  189. long    far * lp;
  190. static int    last;
  191.  
  192.     /**************/
  193. #ifdef __TURBOC__
  194.     lp = MK_FP( MEMSEG, Row32Calculate( y, x ) );
  195. #else
  196.     FP_SEG(lp) = MEMSEG;
  197.     FP_OFF(lp) = Row32Calculate( y, x );
  198. #endif
  199.     pixel = *lp & 0x00FFFFFF;
  200.     if( pixel == tga32[last] ) return( last );
  201.     for( index = 0; index < 256; index++ )
  202.         if( pixel == tga32[index] ) {
  203.             last = index;
  204.             return( index );
  205.         }
  206.     return( 0 );
  207. }
  208.  
  209.  
  210. /**************************************************************************/
  211.  
  212. void 
  213. DoFirstPixel( int x, int y, int index )
  214. {
  215.     PutPixel = DoPixel;
  216.     TSetMode( targa->mode | 1 );
  217.     TErase( 0 );
  218.     (*PutPixel)( x, ydots-y, index&0xFF );
  219.     TSetMode( targa->mode & 0xFFFE );
  220. }
  221.  
  222.  
  223. /***************************************************************************/
  224.  
  225. void
  226. WriteTGA( int x, int y, int index )
  227. {
  228.     TSetMode( targa->mode | 1 );
  229.     (*PutPixel)( x, ydots-y, index&0xFF );    /* fix origin to match EGA/VGA */
  230.     TSetMode( targa->mode & 0xFFFE );
  231. }
  232.  
  233.  
  234. /***************************************************************************/
  235.  
  236. int
  237. ReadTGA( int x, int y )
  238. {
  239. int val;
  240.     TSetMode( targa->mode | 1 );
  241.     val = (*GetPixel)( x, ydots-y );
  242.     TSetMode( targa->mode & 0xFFFE );
  243.     return( val );
  244. }
  245.  
  246.  
  247. /***************************************************************************/
  248.  
  249. void
  250. LineTGA( uchar far * lineData, int y, int cnt )
  251. {
  252. /**** not yet
  253.     TSetMode( targa->mode | 1 );
  254.     TSetMode( targa->mode & 0xFFFE );
  255.      return;
  256. ****/
  257. }
  258.  
  259.  
  260. /***************************************************************************/
  261.  
  262.  
  263. void
  264. EndTGA( void )
  265. {
  266.     if( initialized ) {
  267.         GraphEnd();
  268.         initialized = 0;
  269.     }
  270.      return;
  271. }
  272.  
  273.  
  274. /***************************************************************************/
  275.  
  276. void
  277. StartTGA( int xDim, int yDim )
  278. {
  279. int    itemp;
  280. int    v1, v2, v3, index;
  281. union REGS regs;
  282.  
  283. static int onlyonce;
  284.  
  285.     /****************/
  286.     xdots = xDim;
  287.     ydots = yDim;
  288.  
  289.     /****************/
  290.     if( initialized ) return;
  291.     if( ! onlyonce ) {
  292.         onlyonce = 1;
  293.         atexit( EndTGA );
  294.     }
  295.     initialized = 1;
  296.  
  297.     /****************/
  298.     /** first, set up so default palette will be loaded if we're on a VGA **/
  299.     regs.x.ax = 0x1200;
  300.     regs.x.bx = 0x0031;
  301.     /** set mono text mode **/
  302.     regs.x.ax = 7;    /* 'cause TARGA can live at 0xA000, we DO NOT want to */
  303.     int86( 0x10, ®s, ®s ); /* have an EGA/VGA in graphics mode!! */
  304.     int86( 0x10, ®s, ®s );
  305.     helptitle();
  306.     helpmessage(runningontarga);
  307.     
  308.     /****************/
  309.     /*** look for and activate card ***/
  310.     itemp = GraphInit( 16 );
  311.     if( itemp == 0 ) {        /* all is ok */
  312.         VCenterDisplay( ydots + 1 );
  313.          targaType = targa->boardType;
  314.     }
  315.     else {    /* no targa found */
  316.     printf( "\n\nCould not find Targa Card\n ...aborting !\n" );
  317.         exit( 1 );
  318.   }
  319.     if( targaType == 16 ) {
  320.         GetPixel = GetPix16;
  321.         DoPixel = PutPix16;
  322.     }
  323.     else {
  324.         GetPixel = GetPix32;
  325.         DoPixel = PutPix32;
  326.     }
  327.     PutPixel = DoFirstPixel;    /* on first pixel --> erase */
  328.     if( yDim == 482 ) SetOverscan( 1 );
  329.     TSetMode( targa->mode & 0xFFFE );
  330.  
  331.     /****************/
  332.     if( ! LoadColorMap() ) {
  333.         printf( "\n\nColor Map Not Found\n ... aborting!\n" );
  334.         exit( 1 );
  335.     }
  336.  
  337.     /****************/
  338.      return;
  339. }
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.