home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / 3DTOSHI2.ZIP / mpgfx / source / gfximage.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-14  |  7.1 KB  |  308 lines

  1.  
  2. // gfximage.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "gfximage.h"
  8.  
  9. IMAGE::IMAGE ()
  10.   {
  11.     Buffer = NULL;
  12.     Width = Height = 0;
  13.     BytesPerRow = 0;
  14.     RowInc = 0;
  15.     Bits = 0;
  16.     Transparent = NOTRANSPARENT;
  17.     Direction = IMAGE_TOPDOWN;
  18.     SetViewPort ( 0, 0, -1, -1 );
  19.     Attached = FALSE;
  20.     hMemDisplay = NULL;
  21.     DIBSection = NULL;
  22.     ForDebug = FALSE;
  23.   } // End of Constructor for IMAGE
  24.  
  25. IMAGE::IMAGE ( LONG NewDirection )
  26.   {
  27.     Buffer = NULL;
  28.     Width = Height = 0;
  29.     BytesPerRow = 0;
  30.     RowInc = 0;
  31.     Bits = 0;
  32.     Transparent = NOTRANSPARENT;
  33.     Direction = NewDirection;
  34.     SetViewPort ( 0, 0, -1, -1 );
  35.     Attached = FALSE;
  36.     hMemDisplay = NULL;
  37.     DIBSection = NULL;
  38.     ForDebug = FALSE;
  39.   } // End of Constructor for IMAGE
  40.  
  41. IMAGE::~IMAGE ()
  42.   {
  43.     Destroy ();
  44.   } // End of Destructor for IMAGE
  45.  
  46. VOID IMAGE::SetDebugFlag ( BOOLEAN IsForDebug )
  47.   {
  48.     ForDebug = IsForDebug;
  49.   } // End of SetDebugFlag for IMAGE
  50.  
  51. BOOLEAN IMAGE::Create ( LONG NewFormat, LONG Wd, LONG Ht )
  52.   {
  53.     LONG Size;
  54.     LONG BitSize;
  55.  
  56.     if (NewFormat==IMAGE_8BIT)
  57.       BitSize = 8;
  58.     else if (NewFormat==IMAGE_16BIT)
  59.       BitSize = 16;
  60.     else if (NewFormat==IMAGE_24BIT)
  61.       BitSize = 24;
  62.     else
  63.       {
  64.         Error.SetError ( ERR_NOVALID );
  65.         return FALSE;
  66.       } // End else  
  67.     
  68.     if ((Wd<=0)||(Ht<=0))
  69.       {
  70.         Error.SetError ( ERR_NOVALID );
  71.         return FALSE;
  72.       } // End if
  73.  
  74.     Destroy ();
  75.  
  76.     Width = Wd;
  77.     Height = Ht;
  78.     Format = NewFormat;
  79.     Bits = BitSize;
  80.     BytesPerPixel = Bits/8;
  81.     BytesPerRow = Width*BytesPerPixel;
  82.     if (Direction==IMAGE_TOPDOWN)
  83.       RowInc = BytesPerRow;
  84.     else
  85.       RowInc = -BytesPerRow;  
  86.  
  87.     Size = BytesPerRow*Ht*(Bits/8);
  88.  
  89.     #if defined (__FORDOS__)
  90.       if (ForDebug)
  91.         {
  92.           #if defined (__FORWATCOM__)
  93.             Buffer = (BYTE*)0xA0000;
  94.           #endif
  95.         } // End if
  96.       else
  97.         {
  98.           Buffer = new BYTE [Size];
  99.         } // End else
  100.     #else
  101.       Buffer = new BYTE [Size];
  102.     #endif
  103.  
  104.     if (Buffer==NULL)
  105.       {
  106.         Width = Height = Bits = BytesPerRow = RowInc = 0;
  107.         Error.SetError ( ERR_NOMEMORY );
  108.         return FALSE;
  109.       } // End if
  110.     SetViewPort ( 0, 0, Width-1, Height-1 );
  111.     return SUCCESS;
  112.   } // End of Create for IMAGE
  113.  
  114. BOOLEAN IMAGE::Create ( HDISPLAY hDisplay, LONG NewFormat, LONG Wd, LONG Ht )
  115.   {
  116.     if (hDisplay)
  117.       {}
  118.     #if defined (__FORWIN32__)
  119.       Destroy ();
  120.       
  121.       BITMAPINFO *Info;
  122.       Info = (BITMAPINFO*)malloc(sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
  123.       if (Info==NULL)
  124.         return FAILURE;
  125.  
  126.       LONG BitSize;
  127.       
  128.       if (NewFormat==IMAGE_8BIT)
  129.         BitSize = 8;
  130.       else if (NewFormat==IMAGE_16BIT)
  131.         BitSize = 16;
  132.       else if (NewFormat==IMAGE_24BIT)
  133.         BitSize = 24;
  134.       else
  135.         {
  136.           Error.SetError ( ERR_NOVALID );
  137.           return FALSE;
  138.         } // End else  
  139.  
  140.       Info->bmiHeader.biSize = sizeof ( BITMAPINFOHEADER );
  141.  
  142.       Wd = (Wd+3) & ~3;
  143.       
  144.       Info->bmiHeader.biWidth = Wd;
  145.       if (Direction==IMAGE_TOPDOWN)
  146.         Info->bmiHeader.biHeight = -Ht;
  147.       else  
  148.         Info->bmiHeader.biHeight = Ht;
  149.         
  150.       Info->bmiHeader.biPlanes = 1;
  151.       Info->bmiHeader.biBitCount = (SHORT)BitSize;
  152.       Info->bmiHeader.biCompression = BI_RGB;
  153.       Info->bmiHeader.biSizeImage = (DWORD)Wd*(DWORD)Ht*(BitSize/8);
  154.       Info->bmiHeader.biXPelsPerMeter = 0;
  155.       Info->bmiHeader.biYPelsPerMeter = 0;
  156.       Info->bmiHeader.biClrUsed = 0;
  157.       Info->bmiHeader.biClrImportant = 0;
  158.  
  159.       WORD *Pal;
  160.       INT i;
  161.  
  162.       // Create Identity Palette
  163.       Pal = (WORD*)&(Info->bmiColors[0]);
  164.       for (i=0;i<256;i++)
  165.         {
  166.           *Pal++ = (WORD)i;   
  167.         } // End for
  168.         
  169.       DIBSection = CreateDIBSection ( hDisplay, Info, DIB_PAL_COLORS,
  170.                                       (VOID**)&Buffer, 0, 0 );
  171.  
  172.       hMemDisplay = CreateCompatibleDC ( hDisplay );
  173.       OldBitmap = (HBITMAP)SelectObject(hMemDisplay,DIBSection);
  174.           
  175.       Width = Info->bmiHeader.biWidth;
  176.       Height = Info->bmiHeader.biHeight;
  177.       if (Height<0)
  178.         Height = -Height;
  179.         
  180.       Bits = BitSize;
  181.       BytesPerPixel = Bits/8;
  182.       BytesPerRow = Width*BytesPerPixel;
  183.       if (Direction==IMAGE_TOPDOWN)
  184.         RowInc = BytesPerRow;
  185.       else
  186.         RowInc = -BytesPerRow;  
  187.       
  188.       free ( Info );      
  189.       if (DIBSection==NULL)
  190.         {
  191.           Destroy ();  
  192.           return FAILURE;
  193.         } // End if
  194.         
  195.       Attached = TRUE;
  196.       SetViewPort ( 0, 0, Width-1, Height-1 );
  197.       return SUCCESS;    
  198.     #else
  199.       return Create ( NewFormat, Wd, Ht );  
  200.     #endif  
  201.   } // End of Create for IMAGE
  202.  
  203. VOID IMAGE::Destroy ()
  204.   {
  205.     #if defined (__FORWIN32__)
  206.       if (Attached)
  207.         {
  208.           if (hMemDisplay!=NULL)
  209.             {
  210.               SelectObject ( hMemDisplay, OldBitmap );
  211.               DeleteDC ( hMemDisplay );  
  212.             } // End if
  213.           if (DIBSection!=NULL)
  214.             {
  215.               DeleteObject ( DIBSection );  
  216.             } // End if
  217.           hMemDisplay = NULL;
  218.           DIBSection = NULL;
  219.           Buffer = NULL;
  220.           Attached = FALSE;  
  221.         } // End if
  222.     #endif
  223.       
  224.     #if defined (__FORDOS__)
  225.       if (ForDebug)
  226.         Buffer = NULL;
  227.     #endif
  228.  
  229.     if (Buffer!=NULL)
  230.       delete Buffer;
  231.     Buffer = NULL;
  232.     Width = Height = Bits = BytesPerRow = RowInc= 0;
  233.     SetViewPort ( 0, 0, -1, -1 );
  234.   } // End of Destroy for IMAGE
  235.  
  236. BYTE* IMAGE::SetOffset ( LONG x, LONG y )
  237.   {
  238.     if (Direction==IMAGE_TOPDOWN)
  239.       {  
  240.         Offset = y*BytesPerRow+x*BytesPerPixel;
  241.       } // End if
  242.     else
  243.       {
  244.         y = Height - y - 1;  
  245.         Offset = y*BytesPerRow+x*BytesPerPixel;
  246.       } // End else    
  247.     return Buffer+Offset;
  248.   } // End of SetOffset for IMAGE
  249.  
  250. BYTE* IMAGE::GetNextRow ( LONG RowDirection )
  251.   {
  252.     if (RowDirection==ROW_DOWN)
  253.       Offset += RowInc;
  254.     else
  255.       Offset -= RowInc;   
  256.     return Buffer+Offset;
  257.   } // End of GetNextRow for IMAGE
  258.  
  259. VOID IMAGE::SetViewPort ( LONG x1, LONG y1, LONG x2, LONG y2 )
  260.   {
  261.     if (x1>x2)
  262.       SwapValue ( &x1, &x2 );
  263.     if (y1>y2)
  264.       SwapValue ( &y1, &y2 );
  265.  
  266.     if (x1<0)
  267.       x1 = 0;
  268.     else if (x1>=Width)
  269.       x1 = Width-1;
  270.  
  271.     if (x2<-1)
  272.       x2 = -1;
  273.     else if (x2>=Width)
  274.       x2 = Width-1;
  275.  
  276.     if (y1<0)
  277.       y1 = 0;
  278.     else if (y1>=Height)
  279.       y1 = Height-1;
  280.  
  281.     if (y2<0)
  282.       y2 = 0;
  283.     else if (y2>=Height)
  284.       y2 = Height-1;
  285.  
  286.     ViewPort.x1 = x1;
  287.     ViewPort.y1 = y1;
  288.     ViewPort.x2 = x2;
  289.     ViewPort.y2 = y2;
  290.   } // End of SetViewPort for IMAGE
  291.  
  292. VOID IMAGE::Clear ( LONG Color )
  293.   {
  294.     if (Buffer==NULL)
  295.       return;
  296.  
  297.     LONG Size;
  298.     Size = Height*BytesPerRow;
  299.     memset ( Buffer, Color, Size );
  300.   } // End of Clear for IMAGE
  301.  
  302. BOOLEAN IMAGE::IsCreated ()
  303.   {
  304.     if (Buffer==NULL)
  305.       return FALSE; 
  306.     return TRUE;  
  307.   } // End of IsCreated for IMAGE
  308.