|
NCBI C++ ToolKit
|
00001 /* $Id: conv_image.cpp 33815 2007-05-04 17:18:18Z kazimird $ 00002 * =========================================================================== 00003 * 00004 * PUBLIC DOMAIN NOTICE 00005 * National Center for Biotechnology Information 00006 * 00007 * This software/database is a "United States Government Work" under the 00008 * terms of the United States Copyright Act. It was written as part of 00009 * the author's official duties as a United States Government employee and 00010 * thus cannot be copyrighted. This software/database is freely available 00011 * to the public for use. The National Library of Medicine and the U.S. 00012 * Government have not placed any restriction on its use or reproduction. 00013 * 00014 * Although all reasonable efforts have been taken to ensure the accuracy 00015 * and reliability of the software and data, the NLM and the U.S. 00016 * Government do not and cannot warrant the performance or results that 00017 * may be obtained by using this software or data. The NLM and the U.S. 00018 * Government disclaim all warranties, express or implied, including 00019 * warranties of performance, merchantability or fitness for any particular 00020 * purpose. 00021 * 00022 * Please cite the author in any work or product based on this material. 00023 * 00024 * =========================================================================== 00025 * 00026 * Authors: Mike DiCuccio 00027 * 00028 * File Description: 00029 * CConvImageApp: Convert an image from one format to another 00030 */ 00031 00032 00033 #include <ncbi_pch.hpp> 00034 #include <corelib/ncbiapp.hpp> 00035 #include <corelib/ncbiargs.hpp> 00036 #include <corelib/ncbienv.hpp> 00037 #include <corelib/ncbireg.hpp> 00038 #include <corelib/ncbitime.hpp> 00039 00040 #include <util/image/image_io.hpp> 00041 #include <util/image/image_util.hpp> 00042 00043 USING_NCBI_SCOPE; 00044 00045 00046 00047 class CConvImageApp : public CNcbiApplication 00048 { 00049 public: 00050 virtual void Init(void); 00051 virtual int Run(void); 00052 virtual void Exit(void); 00053 }; 00054 00055 00056 00057 ///////////////////////////////////////////////////////////////////////////// 00058 // Init test for all different types of arguments 00059 00060 00061 void CConvImageApp::Init(void) 00062 { 00063 // Create command-line argument descriptions class 00064 auto_ptr<CArgDescriptions> arg_desc(new CArgDescriptions); 00065 00066 // Specify USAGE context 00067 arg_desc->SetUsageContext(GetArguments().GetProgramBasename(), 00068 "Image read/write test application"); 00069 00070 arg_desc->AddKey("in", "InputImage", "Image to convert", 00071 CArgDescriptions::eString); 00072 arg_desc->AddKey("out", "OutputImage", "Resulting image", 00073 CArgDescriptions::eString); 00074 00075 arg_desc->AddOptionalKey("fmt", "OutputFormat", 00076 "Format of the new image", 00077 CArgDescriptions::eString); 00078 arg_desc->SetConstraint("fmt", 00079 &(*new CArgAllow_Strings(), 00080 "jpeg", "tiff", "png", "bmp", 00081 "sgi", "raw", "gif")); 00082 00083 // Setup arg.descriptions for this application 00084 SetupArgDescriptions(arg_desc.release()); 00085 } 00086 00087 00088 00089 int CConvImageApp::Run(void) 00090 { 00091 CArgs args = GetArgs(); 00092 00093 string in_file = args["in"].AsString(); 00094 string out_file = args["out"].AsString(); 00095 00096 CStopWatch sw; 00097 sw.Start(); 00098 CRef<CImage> image(CImageIO::ReadImage(in_file)); 00099 double read_time = sw.Elapsed(); 00100 00101 if ( !image ) { 00102 LOG_POST(Error << "error: can't read image from " << in_file); 00103 return 1; 00104 } 00105 00106 LOG_POST(Info << "read " << in_file << " (" 00107 << image->GetWidth() << ", " << image->GetHeight() 00108 << ") in " << read_time << " seconds"); 00109 00110 CImageIO::EType fmt = CImageIO::GetTypeFromFileName(out_file); 00111 if (args["fmt"].HasValue()) { 00112 string str = args["fmt"].AsString(); 00113 if (str == "jpeg") { 00114 fmt = CImageIO::eJpeg; 00115 } else if (str == "png") { 00116 fmt = CImageIO::ePng; 00117 } else if (str == "tiff") { 00118 fmt = CImageIO::eTiff; 00119 } else if (str == "bmp") { 00120 fmt = CImageIO::eBmp; 00121 } else if (str == "raw") { 00122 fmt = CImageIO::eRaw; 00123 } else if (str == "sgi") { 00124 fmt = CImageIO::eSgi; 00125 } else if (str == "gif") { 00126 fmt = CImageIO::eGif; 00127 } 00128 } 00129 00130 CImageIO::WriteImage(*image, out_file, fmt); 00131 double write_time = sw.Elapsed(); 00132 LOG_POST(Info << "wrote image in " << write_time - read_time 00133 << " seconds"); 00134 00135 return 0; 00136 } 00137 00138 00139 ///////////////////////////////////////////////////////////////////////////// 00140 // Cleanup 00141 00142 00143 void CConvImageApp::Exit(void) 00144 { 00145 SetDiagStream(0); 00146 } 00147 00148 00149 00150 ///////////////////////////////////////////////////////////////////////////// 00151 // MAIN 00152 00153 00154 int main(int argc, const char* argv[]) 00155 { 00156 // Execute main application function 00157 return CConvImageApp().AppMain(argc, argv, 0, eDS_Default, 0); 00158 }
1.7.5.1
Modified on Wed May 23 13:05:35 2012 by modify_doxy.py rev. 337098