home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / ProgressGraph.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  6.2 KB  |  241 lines

  1. /* 
  2.  *  ProgressGraph.cpp
  3.  *
  4.  *    Copyright (C) Alberto Vigata - July 2000 - ultraflask@yahoo.com
  5.  *
  6.  *  This file is part of FlasKMPEG, a free MPEG to MPEG/AVI converter
  7.  *    
  8.  *  FlasKMPEG is free software; you can redistribute it and/or modify
  9.  *  it under the terms of the GNU General Public License as published by
  10.  *  the Free Software Foundation; either version 2, or (at your option)
  11.  *  any later version.
  12.  *   
  13.  *  FlasKMPEG is distributed in the hope that it will be useful,
  14.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  *  GNU General Public License for more details.
  17.  *   
  18.  *  You should have received a copy of the GNU General Public License
  19.  *  along with GNU Make; see the file COPYING.  If not, write to
  20.  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
  21.  *
  22.  */
  23.  
  24. #include "ProgressGraph.h"
  25.  
  26.  
  27. #define NORMAL_FRAMECOLOR RGB(36, 90, 180)
  28. #define KEY_FRAMECOLOR RGB(78, 121, 220)
  29. #define KNOBCOLOR RGB(221, 159, 77)
  30. #define BACKGROUNDCOLOR RGB(60, 60, 60)
  31. #define PANELCOLOR RGB(80, 80, 80)
  32. #define WHITE RGB(255, 255, 255)
  33. #define RED RGB(255, 0, 0)
  34.  
  35. //////////////////////////////////////////////////////////////////////
  36. // Construction/Destruction
  37. //////////////////////////////////////////////////////////////////////
  38.  
  39. CProgressGraph::CProgressGraph()
  40. {
  41.   m_nMinFramesize = 0;
  42.   m_nMaxFramesize = 0;
  43.   m_nTotalFramesize = 0;
  44.   m_nFramecount = 0;  
  45.   m_nFramedelay = 0;
  46. }
  47.  
  48. CProgressGraph::~CProgressGraph()
  49. {
  50.  
  51. }
  52.  
  53. bool CProgressGraph::Initialize(HWND hWnd, long nWidth, long nHeight, long nFrameDelay)
  54. {
  55.  
  56.   if ( CCanvas::Initialize(hWnd, nWidth, nHeight) )
  57.   {
  58.     // Set widths of the vectors
  59.     m_nBarCount = m_nWidth / PROGRESSBAR_WIDTH;
  60.     m_vFrames.resize( m_nBarCount );
  61.     m_vBars.resize( m_nBarCount );
  62.   
  63.     TProgressBar sBar;
  64.     TProgressFrame sFrame;
  65.   
  66.     m_nFramedelay = nFrameDelay;
  67.  
  68.     memset( &sBar, 0, sizeof TProgressBar );
  69.     memset( &sFrame, 0, sizeof TProgressFrame );
  70.   
  71.     // Set defaults
  72.     for(int i=0; i<m_nBarCount; i++)
  73.     {
  74.       m_vFrames[i] = sFrame;
  75.       m_vBars[i] = sBar;
  76.     }
  77.  
  78.     m_nMinFramesize = 999999999;
  79.     m_nMaxFramesize = 0;
  80.     m_nTotalFramesize = 0;
  81.     m_nFramecount = 0;  
  82.  
  83.     m_hWnd = hWnd;
  84.  
  85.     return true;
  86.   }
  87.   return false;
  88. }
  89.  
  90. void CProgressGraph::StreamInfo(ui64 size )
  91. {
  92.   m_nTotalSize = size; 
  93.  
  94.   if( !m_nFramecount || !m_nFramedelay )
  95.     return;
  96.  
  97.   // Calculate avg bitrate in bytes/sec
  98.   m_nAverageBitrate = (m_nTotalSize * (ui64)100000) / (ui64)(m_nFramecount * m_nFramedelay);
  99.   // Convert to kilobits / sec
  100.   m_nAverageBitrate = ( m_nAverageBitrate * (ui64)8 ) / (ui64)1000;
  101. }
  102. void CProgressGraph::NewFrame(long size, bool bKeyframe )
  103. {
  104.   if(!m_bInitialized)
  105.     return;
  106.  
  107.   m_nFramecount++;
  108.   m_nTotalFramesize += size;
  109.  
  110.   if(size > m_nMaxFramesize) m_nMaxFramesize = size;
  111.   if(size < m_nMinFramesize) m_nMinFramesize = size;
  112.  
  113.   // Calculate avg bitrate in bytes/sec
  114.   m_nAverageVideoBitrate = (m_nTotalFramesize * 100000) / (m_nFramecount * m_nFramedelay);
  115.   // Convert to kilobits / sec
  116.   m_nAverageVideoBitrate = ( m_nAverageVideoBitrate * 8 ) / 1000;
  117.  
  118.   // Add the new frame to the list
  119.   m_vFrames.pop_front();
  120.  
  121.   TProgressFrame sFrame;
  122.   sFrame.bKeyFrame = bKeyframe;
  123.   sFrame.nValue = size;
  124.  
  125.   m_vFrames.push_back( sFrame );
  126.  
  127.  
  128.   //Find Max in frames
  129.   int maxpos=0;
  130.   int maxvalue=0;
  131.  
  132.   for( int i=0; i<m_nBarCount; i++ )
  133.   {
  134.     if(m_vFrames[i].nValue > maxvalue)
  135.     {
  136.       maxpos = i;
  137.       maxvalue = m_vFrames[i].nValue;
  138.     }
  139.   }
  140.  
  141.   // Adjust the bars
  142.   for( i=0; i<m_nBarCount; i++)
  143.   {
  144.     if(maxvalue) m_vBars[i].nHeight = (m_vFrames[i].nValue * m_nHeight) / maxvalue;
  145.     m_vBars[i].bKeyFrame = m_vFrames[i].bKeyFrame;
  146.   }
  147. }
  148.  
  149. void CProgressGraph::Draw()
  150. {
  151.   if(!m_bInitialized)
  152.     return;
  153.  
  154.   // Draw the rectangles
  155.  
  156.  
  157.   TRectangle sRectpar;
  158.   TRect sRect;  
  159.  
  160.   sRectpar.bFilled = true;
  161.  
  162.   // Draw Background
  163.   SetBackground(BACKGROUNDCOLOR);
  164.  
  165.   // Adjust the bars
  166.   for( int i=0; i<m_nBarCount; i++)
  167.   {
  168.     if( m_vBars[i].nHeight != 0 )
  169.     {
  170.  
  171.  
  172.       sRect.nLeft = i*PROGRESSBAR_WIDTH;
  173.       sRect.nRight = sRect.nLeft + PROGRESSBAR_WIDTH;
  174.       sRect.nTop = m_nHeight - m_vBars[i].nHeight;
  175.       sRect.nBottom = m_nHeight;
  176.  
  177.       sRectpar.nBorder = 0;
  178.       sRectpar.nColor = m_vBars[i].bKeyFrame ? KEY_FRAMECOLOR : NORMAL_FRAMECOLOR;
  179.       sRectpar.sRect = sRect;
  180.       DrawRectangle(&sRectpar);
  181.     }
  182.   }
  183.  
  184.   // Draw keyframes knots
  185.   for( i=0; i<m_nBarCount; i++)
  186.   {
  187.     if( m_vBars[i].nHeight!=0 && m_vBars[i].bKeyFrame )
  188.     {
  189.      
  190.       sRect.nLeft = i*PROGRESSBAR_WIDTH - 1;
  191.       sRect.nRight = sRect.nLeft + PROGRESSBAR_WIDTH + 1;
  192.       sRect.nTop = m_nHeight - m_vBars[i].nHeight - 1;
  193.       sRect.nBottom = sRect.nTop + PROGRESSBAR_WIDTH + 1;
  194.       
  195.       sRectpar.nBorder = 0;
  196.       sRectpar.nBorderColor = KNOBCOLOR;
  197.       sRectpar.nColor = KNOBCOLOR;
  198.  
  199.       sRectpar.sRect = sRect;
  200.       DrawRectangle(&sRectpar);
  201.     }
  202.   }
  203.  
  204.   // Legend
  205.   DrawRectangle( 10, 10, 13, 13, KNOBCOLOR);
  206.   DrawText( "Keyframes", 15, 7, WHITE );
  207.  
  208.   // Stats
  209.   long nStatsLeft  = m_nWidth - 150;
  210.   long nStatsRight = m_nWidth - 10;
  211.  
  212.   DrawRectangle( nStatsLeft, 10, nStatsRight, 60, NORMAL_FRAMECOLOR, 2, WHITE);
  213.   char szTemp[256];
  214.  
  215.   if( m_nMinFramesize >= 1024 )
  216.     sprintf(szTemp, "Min: %d kb", m_nMinFramesize >> 10);
  217.   else
  218.     sprintf(szTemp, "Min: %d bytes", m_nMinFramesize );
  219.  
  220.   DrawText( szTemp, nStatsLeft +2, 12, WHITE );
  221.  
  222.   sprintf(szTemp, "Max: %d kb", m_nMaxFramesize >> 10);
  223.   DrawText( szTemp, nStatsLeft +2, 23, WHITE );
  224.  
  225.   if( m_nAverageVideoBitrate > 1000 )
  226.     sprintf(szTemp, "Video bitrate: %.3f Mbits/s", (float)m_nAverageVideoBitrate/1000.0 );
  227.   else
  228.     sprintf(szTemp, "Video bitrate: %.d kbits/s", m_nAverageVideoBitrate );
  229.  
  230.   DrawText( szTemp, nStatsLeft +2, 34, WHITE );
  231.   
  232.   if( m_nAverageBitrate > 1000 )
  233.     sprintf(szTemp, "Total bitrate: %.3f Mbits/s", (float)m_nAverageBitrate/1000.0 );
  234.   else
  235.     sprintf(szTemp, "Total bitrate: %.d kbits/s", m_nAverageBitrate );
  236.  
  237.   DrawText( szTemp, nStatsLeft +2, 45, WHITE );
  238.   // Draw the whole thing
  239.   CCanvas::DrawCanvas(0,0);
  240. }
  241.