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

  1.  
  2. // gfxcltbl.cpp
  3. //
  4. // Copyright (c) 1995 by Toshiaki Tsuji, all rights reserved.
  5.  
  6. #include "stdgfx.h"
  7. #include "gfxcltbl.h"
  8. #include "gfxfiles.h"
  9.  
  10. COLORTABLE::COLORTABLE () : MYOBJECT ()
  11.   {
  12.     NumColors = NumLevels = 0;
  13.     TableType = 0;
  14.     Table = NULL;
  15.     ProcessIndicator = NULL;
  16.     Pal = new RGBPALETTE ();
  17.   } // End of Constructor for COLORTABLE
  18.  
  19. COLORTABLE::~COLORTABLE ()
  20.   {
  21.     Destroy ();
  22.     if (Pal!=NULL)
  23.       delete Pal;
  24.     Pal = NULL;  
  25.   } // End of Destructor for COLORTABLE
  26.  
  27. BOOLEAN COLORTABLE::Create ( LONG NumL, LONG NumCol )
  28.   {
  29.     LONG Size;
  30.     Destroy ();
  31.     Size = NumL*NumCol;
  32.     Table = new BYTE [Size];
  33.     if (Table==NULL)
  34.       return FAILURE;
  35.     memset ( Table, 0, Size);
  36.     NumLevels = NumL;
  37.     NumColors = NumCol;  
  38.     return SUCCESS;
  39.   } // End of Create for COLORTABLE
  40.  
  41. VOID COLORTABLE::Destroy ()
  42.   {
  43.     if (Table!=NULL)
  44.       delete Table;
  45.     Table = NULL;
  46.     NumColors = NumLevels = 0;
  47.     TableType = 0;
  48.   } // End of Destroy for COLORTABLE     
  49.  
  50. VOID COLORTABLE::CopyPalette ( RGBPALETTE *Pal1 )
  51.   {
  52.     INT i;
  53.     if (Pal1==NULL)
  54.       return;
  55.  
  56.     RGBCOLOR *Entry1,*Entry;
  57.     Entry1 = Pal1->GetEntry ();
  58.     Entry = Pal->GetEntry ();
  59.     for (i=0;i<256;i++)
  60.       {
  61.         Entry[i] = Entry1[i];  
  62.       } // End for  
  63.   } // End of CopyPalette for COLORTABLE
  64.  
  65. BOOLEAN COLORTABLE::CreateMatchTable ( RGBPALETTE *Pal2 )
  66.   {
  67.     RGBCOLOR *Entry;
  68.     RGBPALETTE *Pal1;
  69.  
  70.     Pal1 = Pal;
  71.     Entry = Pal2->GetEntry ();
  72.     
  73.     if (Create ( 1, 256 )==FAILURE)
  74.       {
  75.         Error.SetError ( ERR_NOMEMORY );
  76.         return FAILURE;
  77.       } // End if
  78.     
  79.     LONG i;
  80.     for (i=0;i<256;i++)
  81.       {
  82.         Table[i] = (BYTE)Pal1->GetClosestColor ( Entry[i] );
  83.       } // End for
  84.  
  85.     TableType = TABLE_MATCH;    
  86.     return SUCCESS;  
  87.   } // End of CreateMatchtable for COLORTABLE
  88.  
  89. BOOLEAN COLORTABLE::CreateShadeTable ( LONG NumLevel, LONG Highest, LONG Lowest )
  90.   {
  91.     INT i,j;
  92.     
  93.     if (NumLevel==0)
  94.       {
  95.         Error.SetError ( ERR_NOVALID );
  96.         return FAILURE;
  97.       } // End if  
  98.       
  99.     if (Highest<Lowest)
  100.       SwapValue ( &Highest, &Lowest );
  101.       
  102.     if (Highest<0)
  103.       Highest = 0; 
  104.     if (Lowest<0)
  105.       Lowest = 0;     
  106.  
  107.     if (Create ( NumLevel, 256 )==FAILURE)
  108.       {
  109.         Error.SetError ( ERR_NOMEMORY );
  110.         return FAILURE;
  111.       } // End if
  112.       
  113.     RGBCOLOR *Entry;    
  114.  
  115.     Entry = Pal->GetEntry ();
  116.  
  117.     INT R,G,B;
  118.     INT BaseR,BaseG,BaseB;
  119.  
  120.     double HighRatio,LowRatio,StepRatio;
  121.     double CurIntense;
  122.    
  123.     HighRatio = (double)Highest/100.0;
  124.     LowRatio = (double)Lowest/100.0;
  125.  
  126.     StepRatio = (HighRatio-LowRatio)/NumLevels;
  127.     
  128.     float Percentage = (float)0.0;    
  129.  
  130.     for (i=0;i<256;i++)
  131.       {
  132.         BaseR = (Entry[i].Red);
  133.         BaseG = (Entry[i].Green);
  134.         BaseB = (Entry[i].Blue);
  135.         
  136.         CurIntense = LowRatio;
  137.  
  138.         j = 0;
  139.         while ((CurIntense<=1.0)&&(j<NumLevels))
  140.           {
  141.             RGBCOLOR Color;
  142.             R = (INT)(CurIntense*BaseR);
  143.             G = (INT)(CurIntense*BaseG);
  144.             B = (INT)(CurIntense*BaseB);
  145.  
  146.             if (R>255)
  147.               Color.Red = 255;
  148.             else
  149.               Color.Red = (BYTE)R;  
  150.               
  151.             if (G>255)
  152.               Color.Green = 255;
  153.             else
  154.               Color.Green = (BYTE)G;  
  155.               
  156.             if (B>255)
  157.               Color.Blue = 255;
  158.             else
  159.               Color.Blue = (BYTE)B;  
  160.               
  161.             Table[j*256+i] = (BYTE)Pal->GetClosestColor ( Color );
  162.             j++;
  163.             CurIntense += StepRatio;
  164.           } // End while
  165.           
  166.         while (j<NumLevels)
  167.           {
  168.             RGBCOLOR Color;
  169.  
  170.             if (BaseR==0)
  171.               R = (INT)((CurIntense-1.0)*255);
  172.             else  
  173.               R = (INT)(CurIntense*BaseR);
  174.  
  175.             if (BaseG==0)
  176.               G = (INT)((CurIntense-1.0)*255);
  177.             else  
  178.               G = (INT)(CurIntense*BaseG);
  179.  
  180.             if (BaseB==0)
  181.               B = (INT)((CurIntense-1.0)*255);
  182.             else  
  183.               B = (INT)(CurIntense*BaseB);
  184.  
  185.             if (R>255)
  186.               Color.Red = 255;
  187.             else
  188.               Color.Red = (BYTE)R;  
  189.               
  190.             if (G>255)
  191.               Color.Green = 255;
  192.             else
  193.               Color.Green = (BYTE)G;  
  194.               
  195.             if (B>255)
  196.               Color.Blue = 255;
  197.             else
  198.               Color.Blue = (BYTE)B;  
  199.               
  200.             Table[j*256+i] = (BYTE)Pal->GetClosestColor ( Color );
  201.             j++;
  202.             CurIntense += StepRatio;
  203.           } // End while
  204.           
  205.         if (ProcessIndicator!=NULL)
  206.           {
  207.             Percentage += ((float)100/256);
  208.             if (ProcessIndicator ( Percentage ))
  209.               return FAILURE;
  210.           } // End if
  211.       } // End for
  212.     TableType = TABLE_SHADE;    
  213.     return SUCCESS;
  214.   } // End of CreateShadeTable for COLORTABLE                                       
  215.  
  216. BOOLEAN COLORTABLE::CreateHazeTable ( RGBCOLOR TargetColor, LONG NumLevel )
  217.   {
  218.     INT i,j;
  219.     
  220.     if (NumLevel==0)
  221.       {
  222.         Error.SetError ( ERR_NOVALID );
  223.         return FAILURE;
  224.       } // End if  
  225.       
  226.     if (Create ( NumLevel, 256 )==FAILURE)
  227.       {
  228.         Error.SetError ( ERR_NOMEMORY );
  229.         return FAILURE;
  230.       } // End if
  231.       
  232.     RGBCOLOR *Entry;
  233.     float Percentage;
  234.     
  235.     Entry = Pal->GetEntry ();
  236.  
  237.     double R,G,B;
  238.     double lR,lG,lB;  // Lowest
  239.     double dR,dG,dB;
  240.  
  241.     Percentage = (float)0.0;
  242.         
  243.     for (i=0;i<256;i++)
  244.       {
  245.         R = (double)(Entry[i].Red);
  246.         G = (double)(Entry[i].Green);
  247.         B = (double)(Entry[i].Blue);
  248.         
  249.         lR = (double)(TargetColor.Red);
  250.         lG = (double)(TargetColor.Green);
  251.         lB = (double)(TargetColor.Blue);
  252.         
  253.         dR = (R-lR) / NumLevels; 
  254.         dG = (G-lG) / NumLevels; 
  255.         dB = (B-lB) / NumLevels; 
  256.         
  257.         R = lR;
  258.         G = lG;
  259.         B = lB;
  260.         
  261.         for (j=0;j<NumLevels;j++)  
  262.           {
  263.             RGBCOLOR Color;
  264.             R += dR;
  265.             G += dG;
  266.             B += dB;
  267.             Color.Red = (BYTE)R;
  268.             Color.Green = (BYTE)G;
  269.             Color.Blue = (BYTE)B;
  270.             Table[j*256+i] = (BYTE)Pal->GetClosestColor ( Color );
  271.           } // End for
  272.           
  273.         if (ProcessIndicator!=NULL)
  274.           {
  275.             Percentage += ((float)100/256);
  276.             if (ProcessIndicator ( Percentage ))
  277.               return FAILURE;
  278.           } // End if
  279.       } // End for
  280.     TableType = TABLE_HAZE;    
  281.     return SUCCESS;
  282.   } // End of CreateHazeTable for COLORTABLE                                      
  283.  
  284. BOOLEAN COLORTABLE::CreateBlendTable ( LONG NumLevel, float StartRatio,
  285.                                        float EndRatio )
  286.   {
  287.     INT i,j,k;
  288.     
  289.     if (NumLevel==0)
  290.       {
  291.         Error.SetError ( ERR_NOVALID );
  292.         return FAILURE;
  293.       } // End if  
  294.       
  295.     if (Create ( NumLevel, 256*256 )==FAILURE)
  296.       {
  297.         Error.SetError ( ERR_NOMEMORY );
  298.         return FAILURE;
  299.       } // End if
  300.  
  301.     if (StartRatio>100)
  302.       StartRatio = (float)100.0;
  303.     else if (StartRatio<0)
  304.       StartRatio = (float)0.0;
  305.           
  306.     if (EndRatio>100)
  307.       EndRatio = (float)100.0;
  308.     else if (EndRatio<0)
  309.       EndRatio = (float)0.0;
  310.       
  311.     RGBCOLOR *Entry;
  312.     float Percentage;
  313.     
  314.     Entry = Pal->GetEntry ();
  315.  
  316.     Percentage = (float)0.0;
  317.  
  318.     float SrcR,SrcG,SrcB;
  319.     float TargetR,TargetG,TargetB;
  320.     float StartR,StartG,StartB;
  321.     float EndR,EndG,EndB;
  322.     float dR,dG,dB;
  323.  
  324.     StartRatio /= 100;    
  325.     EndRatio /= 100;
  326.     
  327.     for (i=0;i<256;i++)
  328.       {
  329.         SrcR = (float)Entry[i].Red;  
  330.         SrcG = (float)Entry[i].Green;  
  331.         SrcB = (float)Entry[i].Blue;
  332.         
  333.         for (j=0;j<256;j++)
  334.           {
  335.             TargetR = (float)Entry[j].Red;  
  336.             TargetG = (float)Entry[j].Green;  
  337.             TargetB = (float)Entry[j].Blue;
  338.             
  339.             StartR = SrcR*(1-StartRatio) + TargetR*StartRatio;
  340.             StartG = SrcG*(1-StartRatio) + TargetG*StartRatio;
  341.             StartB = SrcB*(1-StartRatio) + TargetB*StartRatio;
  342.             
  343.             if (StartR>255) StartR = (float)255.0;
  344.             if (StartG>255) StartG = (float)255.0;
  345.             if (StartB>255) StartB = (float)255.0;
  346.               
  347.             EndR = SrcR*(1-EndRatio) + TargetR*EndRatio;
  348.             EndG = SrcG*(1-EndRatio) + TargetR*EndRatio;
  349.             EndB = SrcB*(1-EndRatio) + TargetR*EndRatio;
  350.             
  351.             if (EndR>255) EndR = (float)255.0;
  352.             if (EndG>255) EndG = (float)255.0;
  353.             if (EndB>255) EndB = (float)255.0;
  354.  
  355.             dR = (EndR-StartR) / (float)NumLevels;
  356.             dG = (EndG-StartG) / (float)NumLevels;
  357.             dB = (EndB-StartB) / (float)NumLevels;
  358.  
  359.             RGBCOLOR Color;
  360.             
  361.             for (k=0;k<NumLevels;k++)  
  362.               {
  363.                 Color.Red = (BYTE)StartR;  
  364.                 Color.Green = (BYTE)StartG;  
  365.                 Color.Blue = (BYTE)StartB;
  366.                 
  367.                 Table[k*256*256+i*256+j] = (BYTE)Pal->GetClosestColor ( Color );
  368.  
  369.                 StartR += dR;
  370.                 StartG += dG;
  371.                 StartB += dB;
  372.               } // End for
  373.           
  374.             if (ProcessIndicator!=NULL)
  375.               {
  376.                 Percentage += ((float)100/(256*256));
  377.                 if (ProcessIndicator ( Percentage ))
  378.                   return FAILURE;
  379.               } // End if
  380.           } // End for
  381.       } // End for
  382.       
  383.     TableType = TABLE_BLEND;    
  384.     return SUCCESS;  
  385.   } // End of CreateBlend Table for COLORTABLE
  386.  
  387. BOOLEAN COLORTABLE::Save ( STRING FileName )
  388.   {
  389.     FILEHANDLE f;
  390.     
  391.     if (TableType==0)
  392.       return FAILURE;      
  393.     if (Table==NULL)
  394.       return FAILURE;
  395.       
  396.     f = File.Open ( FileName, OPEN_WRITE | OPEN_BINARY );
  397.     if (f==NULL)
  398.       return FAILURE;
  399.  
  400.     CHAR Str[128];
  401.  
  402.     sprintf ( Str, "coltable" );
  403.     if (File.Write ( f, Str, 128 )==FAILURE)
  404.       {
  405.         File.Close ( f );  
  406.         return FAILURE;
  407.       } // End if  
  408.                 
  409.     #if defined (__MSBFIRST__)
  410.       SwapDWord ( (DWORD*)&TableType );
  411.       SwapDWord ( (DWORD*)&NumColors );
  412.       SwapDWord ( (DWORD*)&NumLevels );
  413.     #endif
  414.  
  415.     if (File.Write ( f, &TableType, sizeof(LONG) )==FAILURE)
  416.       {
  417.         File.Close ( f );  
  418.         return FAILURE;
  419.       } // End if
  420.     if (File.Write ( f, &NumColors, sizeof(LONG) )==FAILURE)
  421.       {
  422.         File.Close ( f );  
  423.         return FAILURE;
  424.       } // End if
  425.     if (File.Write ( f, &NumLevels, sizeof(LONG) )==FAILURE)
  426.       {
  427.         File.Close ( f );  
  428.         return FAILURE;
  429.       } // End if
  430.  
  431.     #if defined (__MSBFIRST__)
  432.       SwapDWord ( (DWORD*)&TableType );
  433.       SwapDWord ( (DWORD*)&NumColors );
  434.       SwapDWord ( (DWORD*)&NumLevels );
  435.     #endif
  436.  
  437.     LONG Size;
  438.     Size = NumColors*NumLevels;  
  439.     if (File.Write ( f, Table, Size )==FAILURE)
  440.       {
  441.         File.Close ( f );  
  442.         return FAILURE;
  443.       } // End if
  444.       
  445.     // Save the palette associated to this table
  446.     if (File.Write ( f, Pal->GetEntry(), (int)256*sizeof(RGBCOLOR) )==FAILURE)
  447.       {
  448.         File.Close ( f );  
  449.         return FAILURE;
  450.       } // End if 
  451.       
  452.     File.Close ( f );
  453.     return SUCCESS;  
  454.   } // End of Save for COLORTABLE
  455.  
  456. BOOLEAN COLORTABLE::Load ( STRING FileName )
  457.   {
  458.     CHAR Str[128];
  459.     FILEHANDLE f;
  460.     
  461.     f = File.Open ( FileName, OPEN_READ | OPEN_BINARY );
  462.     if (f==NULL)
  463.       return FAILURE;
  464.  
  465.     if (File.Read ( f, &(Str[0]), 128 )==FAILURE)
  466.       {
  467.         File.Close ( f );  
  468.         return FAILURE;
  469.       } // End if
  470.  
  471.     if (strncmp(Str,"coltable",8)!=0)
  472.       {
  473.         File.Close ( f );  
  474.         return FAILURE;
  475.       } // End if
  476.  
  477.     LONG Type,NumC,NumL;      
  478.     if (File.Read ( f, &Type, sizeof(LONG) )==FAILURE)
  479.       {
  480.         File.Close ( f );  
  481.         return FAILURE;
  482.       } // End if
  483.  
  484.     if (File.Read ( f, &NumC, sizeof(LONG) )==FAILURE)
  485.       {
  486.         File.Close ( f );  
  487.         return FAILURE;
  488.       } // End if
  489.     NumColors = NumC;
  490.       
  491.     if (File.Read ( f, &NumL, sizeof(LONG) )==FAILURE)
  492.       {
  493.         File.Close ( f );  
  494.         return FAILURE;
  495.       } // End if
  496.     NumLevels = NumL;   
  497.  
  498.     #if defined (__MSBFIRST__)
  499.       SwapDWord ( (DWORD*)&TableType );
  500.       SwapDWord ( (DWORD*)&NumColors );
  501.       SwapDWord ( (DWORD*)&NumLevels );
  502.     #endif
  503.  
  504.     if (Create(NumLevels,NumColors)==FAILURE)
  505.       {
  506.         File.Close ( f );
  507.         return FAILURE;  
  508.       } // End if
  509.       
  510.     TableType = Type;
  511.     
  512.     LONG Size;
  513.     Size = NumColors*NumLevels;
  514.  
  515.     if (File.Read ( f, Table, Size )==FAILURE)
  516.       {
  517.         File.Close ( f );  
  518.         return FAILURE;
  519.       } // End if
  520.       
  521.     // Load the palette associated to this table
  522.     if (File.Read ( f, Pal->GetEntry(), (int)256*sizeof(RGBCOLOR) )==FAILURE)
  523.       {
  524.         File.Close ( f );  
  525.         return FAILURE;
  526.       } // End if 
  527.       
  528.     File.Close ( f );
  529.     return SUCCESS;  
  530.   } // End of Load for COLORTABLE
  531.  
  532. VOID COLORTABLE::SetProcessIndicator ( COLTBLPROC Proc )
  533.   {
  534.     ProcessIndicator = Proc;  
  535.   } // End of SetProcessIndicator for COLORTABLE
  536.  
  537.   
  538.  
  539.