cmdln_demo.cpp
上传用户:yhdzpy8989
上传日期:2007-06-13
资源大小:13604k
文件大小:8k
- /*
- * ===========================================================================
- * PRODUCTION $Log: cmdln_demo.cpp,v $
- * PRODUCTION Revision 1000.2 2004/06/01 20:49:44 gouriano
- * PRODUCTION PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- * PRODUCTION
- * ===========================================================================
- */
- /* $Id: cmdln_demo.cpp,v 1000.2 2004/06/01 20:49:44 gouriano Exp $
- * ===========================================================================
- *
- * PUBLIC DOMAIN NOTICE
- * National Center for Biotechnology Information
- *
- * This software/database is a "United States Government Work" under the
- * terms of the United States Copyright Act. It was written as part of
- * the author's official duties as a United States Government employee and
- * thus cannot be copyrighted. This software/database is freely available
- * to the public for use. The National Library of Medicine and the U.S.
- * Government have not placed any restriction on its use or reproduction.
- *
- * Although all reasonable efforts have been taken to ensure the accuracy
- * and reliability of the software and data, the NLM and the U.S.
- * Government do not and cannot warrant the performance or results that
- * may be obtained by using this software or data. The NLM and the U.S.
- * Government disclaim all warranties, express or implied, including
- * warranties of performance, merchantability or fitness for any particular
- * purpose.
- *
- * Please cite the author in any work or product based on this material.
- *
- * ===========================================================================
- *
- * Authors: Andrey Yazhuk
- *
- * File Description:
- * Class CCmdLineGraphDemoApp is a command line demo application using
- * "gui_graph" library to render Chart graph and saving it in a file.
- */
-
- #include <ncbi_pch.hpp>
- #include <gui/opengl.h>
- #include <corelib/ncbiapp.hpp>
- #include <corelib/ncbiargs.hpp>
- #include <util/image/image_io.hpp>
- #include <gui/opengl/mesa/glcgi_image.hpp>
- #include <gui/opengl/mesa/gloscontext.hpp>
- #include <gui/opengl/glcolortable.hpp>
- #include <util/image/image.hpp>
- #include <util/image/image_io.hpp>
- #include <gui/opengl/glpane.hpp>
- #include <gui/graph/combo_chart.hpp>
- #include <gui/graph/graph_panel.hpp>
- #include <gui/graph/legend.hpp>
- #include <math.h>
- #include <stdio.h>
- USING_NCBI_SCOPE;
- //
- // content-type headers for images
- //
- struct SContentType
- {
- CImageIO::EType m_Type;
- const char* m_Encoding;
- };
- static const SContentType kContentTypes[] = {
- { CImageIO::eBmp, "bmp" },
- { CImageIO::eGif, "gif" },
- { CImageIO::eJpeg, "jpeg" },
- { CImageIO::ePng, "png" },
- { CImageIO::eSgi, "sgi" },
- { CImageIO::eTiff, "tiff" },
- { CImageIO::eXpm, "xmp" },
- // must be last
- { CImageIO::eUnknown, NULL }
- };
- /////////////////////////////////////////////////////////////////////////////
- // CCmdLineGraphDemoApp::
- class CCmdLineGraphDemoApp : public CNcbiApplication
- {
- public:
- CCmdLineGraphDemoApp();
- protected:
- virtual void Init(void);
- virtual int Run(void);
- virtual void Exit(void);
- bool GenerateImage();
- void Render();
- protected:
- // dimensions of our virtual frame buffer
- size_t m_Width;
- size_t m_Height;
- // the image format we will emit - default is PNG
- CImageIO::EType m_Format;
- // our off-screen renderer
- CRef<CGlOsContext> m_Context;
- };
- /////////////////////////////////////////////////////////////////////////////
- // CCmdLineGraphDemoApp constructor
- CCmdLineGraphDemoApp::CCmdLineGraphDemoApp()
- : m_Width(640),
- m_Height(480),
- m_Format(CImageIO::eUnknown)
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- // Init test for all different types of arguments
- void CCmdLineGraphDemoApp::Init(void)
- {
- // Create command-line argument descriptions class
- auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
- arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
- "CCmdLineGraphDemoApp demo program");
- arg_desc->AddPositional
- ("filename",
- "Output file name",
- CArgDescriptions::eOutputFile,
- CArgDescriptions::fPreOpen | CArgDescriptions::fBinary);
- arg_desc->AddOptionalPositional
- ("fmt",
- "Graphical format - one of the following: bmp, gif, jpeg, png, sgi, tiff, xmp",
- CArgDescriptions::eString);
- // Setup arg.descriptions for this application
- SetupArgDescriptions(arg_desc.release());
- }
- /////////////////////////////////////////////////////////////////////////////
- // Run test (printout arguments obtained from command-line)
- int CCmdLineGraphDemoApp::Run(void)
- {
- // Get arguments
- CArgs args = GetArgs();
- string sFilename = args["filename"].AsString();
- if (args["fmt"]) {
- string sFormat = args["fmt"].AsString();
- for( int i = 0; ;i++ ) {
- const SContentType& Type = kContentTypes[i];
- if (Type.m_Encoding == NULL || sFormat == Type.m_Encoding) {
- m_Format = Type.m_Type;
- break;
- }
- }
- if(m_Format == CImageIO::eUnknown) {
- cout << "Unknown graphical format: " << sFormat << endl;
- return 1;
- }
- } else m_Format = CImageIO::ePng;
- //select image format
- if (GenerateImage())
- cout << "Done." << endl;
- CImageIO::WriteImage(m_Context->GetBuffer(), sFilename, m_Format);
- return 0;
- }
- /////////////////////////////////////////////////////////////////////////////
- // InitContext
- bool CCmdLineGraphDemoApp::GenerateImage()
- {
- // initalizing drawing context with specified Width and Height
- m_Context.Reset(new CGlOsContext(m_Width, m_Height));
- if (m_Context && m_Context->MakeCurrent() ) {
- // call the rendering fucntion
- Render();
- // call glFinish() - VERY IMPORTANT FOR OFF_SCREEN RENDERING
- // this forces any buffered OpenGL requests to complete now.
- glFinish();
- // final image preparation
- // the image is upside-down because frame buffers are, in general,
- // upside-down. Also, we have an alpha channel we need to flatten.
- m_Context->SetBuffer().SetDepth(3);
- m_Context->SetBuffer().Flip();
- return true;
- } else {
- LOG_POST(Error << "CCmdLineGraphDemoApp::GenerateImage(): "
- "Failed to initialized drawing context.");
- return false;
- }
-
- }
- void CCmdLineGraphDemoApp::Render()
- {
- //Create graph
- int ElemsN = 20;
- int SeriesN = 4;
- CComboChartDataSource *pChartDS = new CComboChartDataSource(SeriesN, ElemsN);
- pChartDS->CreateArrays();
- // generating data for the Data Source
- for( int i = 0; i< SeriesN; i++ ) {
- CComboChartDataSource::TValueCont& Cont = pChartDS->GetValueContainer(i);
- char S[30];
- sprintf(S, "Series %d", i);
- pChartDS->GetLabelContainer()[i] = S; // graph name
- for( int j = 0; j < ElemsN; j++ ) {
- Cont[j] = sin(i + (i/10 + 0.05)*j) + j * 0.01;
- }
- }
- CComboChart* pChart = new CComboChart();
- //pChart->SetStyle(CComboChart::eBarChart); //optional style
- pChart->SetDataSource(pChartDS);
- pChart->AssignAutoMarkers();
- pChart->AssignAutoColors();
- // create and setup Graph Panel
- CGraphPanel Panel;
- Panel.Create();
- Panel.SetRect(TVPRect(0, 0, m_Width - 1, m_Height - 1));
- Panel.SetLayout(100, 25, 100, 45);
- Panel.AddGraph(static_cast<IGraph*>(pChart));
- Panel.GetLegend()->SetDataSource(static_cast<IGraphDataSource*>(pChart));
- Panel.GetGraphPane().ZoomAll();
- Panel.Render();
- delete pChartDS;
- delete pChart;
- }
- /////////////////////////////////////////////////////////////////////////////
- // Cleanup
- void CCmdLineGraphDemoApp::Exit(void)
- {
- SetDiagStream(0);
- }
- /////////////////////////////////////////////////////////////////////////////
- // MAIN
- int main(int argc, const char* argv[])
- {
- // Execute main application function
- return CCmdLineGraphDemoApp().AppMain(argc, argv, 0, eDS_Default, 0);
- }
- /*
- * ===========================================================================
- * $Log: cmdln_demo.cpp,v $
- * Revision 1000.2 2004/06/01 20:49:44 gouriano
- * PRODUCTION: UPGRADED [GCC34_MSVC7] Dev-tree R1.6
- *
- * Revision 1.6 2004/05/21 22:27:43 gorelenk
- * Added PCH ncbi_pch.hpp
- *
- * Revision 1.5 2004/03/16 17:16:18 yazhuk
- * Updated include to glpane.hpp
- *
- * Revision 1.4 2003/12/10 21:32:30 ucko
- * +<stdio.h> for sprintf()
- *
- * Revision 1.3 2003/08/15 17:38:46 ucko
- * Fixed CVS keywords so they'll actually expand.
- *
- * ===========================================================================
- */