1 /* $Id: sub_image.cpp 618367 2020-10-19 14:54:50Z grichenk $
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * Authors: Mike DiCuccio
27 *
28 * File Description:
29 * Test app -- tests various aspects of image conversion / manipulation
30 */
31
32
33 #include <ncbi_pch.hpp>
34 #include <corelib/ncbiapp.hpp>
35 #include <corelib/ncbiargs.hpp>
36 #include <corelib/ncbienv.hpp>
37 #include <corelib/ncbireg.hpp>
38 #include <corelib/ncbitime.hpp>
39
40 #include <util/image/image_io.hpp>
41
42 USING_NCBI_SCOPE;
43
44
45
46 class CSubImageApp : public CNcbiApplication
47 {
48 public:
49 virtual void Init(void);
50 virtual int Run(void);
51 virtual void Exit(void);
52 };
53
54
55
56 /////////////////////////////////////////////////////////////////////////////
57 // Init test for all different types of arguments
58
59
Init(void)60 void CSubImageApp::Init(void)
61 {
62 // Create command-line argument descriptions class
63 unique_ptr<CArgDescriptions> arg_desc(new CArgDescriptions);
64
65 // Specify USAGE context
66 arg_desc->SetUsageContext(GetArguments().GetProgramBasename(),
67 "Image read/write test application");
68
69 arg_desc->AddDefaultPositional("image", "Image to crop",
70 CArgDescriptions::eInputFile, "-",
71 CArgDescriptions::fBinary);
72
73 arg_desc->AddOptionalKey("out", "OutFile",
74 "File name of resultant image",
75 CArgDescriptions::eString);
76
77 arg_desc->AddKey("x", "PosX", "Crop X Position",
78 CArgDescriptions::eInteger);
79 arg_desc->AddKey("y", "PosX", "Crop X Position",
80 CArgDescriptions::eInteger);
81 arg_desc->AddKey("wd", "Width", "Crop Width",
82 CArgDescriptions::eInteger);
83 arg_desc->AddKey("ht", "Height", "Crop Height",
84 CArgDescriptions::eInteger);
85
86 // Setup arg.descriptions for this application
87 SetupArgDescriptions(arg_desc.release());
88 }
89
90
91
Run(void)92 int CSubImageApp::Run(void)
93 {
94 const CArgs& args = GetArgs();
95
96 CNcbiIstream& istr = args["image"].AsInputFile();
97 int x = args["x"].AsInteger();
98 int y = args["y"].AsInteger();
99 int w = args["wd"].AsInteger();
100 int h = args["ht"].AsInteger();
101
102 CStopWatch sw;
103 sw.Start();
104 CRef<CImage> image(CImageIO::ReadSubImage(istr, x, y, w, h));
105 double read_time = sw.Elapsed();
106
107 if ( !image ) {
108 ERR_POST(Error << "error: can't get subimage");
109 return 1;
110 }
111
112 LOG_POST(Info << "read (" << x << ", " << y << ", " << x+w
113 << ", " << y+h << ") in " << read_time << " seconds");
114
115 if (args["out"]) {
116 CImageIO::WriteImage(*image, args["out"].AsString());
117 double write_time = sw.Elapsed();
118 LOG_POST(Info << "wrote image in " << write_time - read_time
119 << " seconds");
120 }
121
122 return 0;
123 }
124
125
126 /////////////////////////////////////////////////////////////////////////////
127 // Cleanup
128
129
Exit(void)130 void CSubImageApp::Exit(void)
131 {
132 SetDiagStream(0);
133 }
134
135
136
137 /////////////////////////////////////////////////////////////////////////////
138 // MAIN
139
140
main(int argc,const char * argv[])141 int main(int argc, const char* argv[])
142 {
143 // Execute main application function
144 return CSubImageApp().AppMain(argc, argv);
145 }
146