2 Copyright (c) 2005-2014, John Hurst
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
8 1. Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 3. The name of the author may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 /*! \file j2c-test.cpp
29 \brief JP2K parser test
33 #include <KM_fileio.h>
38 using namespace ASDCP;
39 using namespace ASDCP::JP2K;
43 //------------------------------------------------------------------------------------------
45 // command line option parser class
47 static const char* PROGRAM_NAME = "j2c-test"; // program name for messages
49 // Macros used to test command option data state.
51 // Increment the iterator, test for an additional non-option command line argument.
52 // Causes the caller to return if there are no remaining arguments or if the next
53 // argument begins with '-'.
54 #define TEST_EXTRA_ARG(i,c) if ( ++i >= argc || argv[(i)][0] == '-' ) \
56 fprintf(stderr, "Argument not found for option %c.\n", (c)); \
61 banner(FILE* stream = stderr)
65 Copyright (c) 2005-2010 John Hurst\n\n\
66 %s is part of asdcplib.\n\
67 asdcplib may be copied only under the terms of the license found at\n\
68 the top of every file in the asdcplib distribution kit.\n\n\
69 Specify the -h (help) option for further information about %s\n\n",
70 PROGRAM_NAME, ASDCP::Version(), PROGRAM_NAME, PROGRAM_NAME);
75 usage(FILE* stream = stderr)
78 USAGE: %s [-h|-help] [-V]\n\
80 %s [-r] [-v] <filename> [...]\n\
85 -v - Print extra detail\n\
87 NOTES: o There is no option grouping, all options must be distinct arguments.\n\
88 o All option arguments must be separated from the option by whitespace.\n\
89 \n", PROGRAM_NAME, PROGRAM_NAME);
99 bool error_flag; // true if the given options are in error or not complete
100 bool version_flag; // true if the version display option was selected
101 bool verbose_flag; // true if the verbose option was selected
102 bool detail_flag; // true if the version display option was selected
103 bool help_flag; // true if the help display option was selected
104 std::list<std::string> filename_list;
106 CommandOptions(int argc, const char** argv) :
107 error_flag(true), version_flag(false), verbose_flag(false),
108 detail_flag(false), help_flag(false)
110 for ( int i = 1; i < argc; i++ )
112 if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
114 switch ( argv[i][1] )
116 case 'V': version_flag = true; break;
117 case 'h': help_flag = true; break;
118 case 'r': detail_flag = true; break;
119 case 'v': verbose_flag = true; break;
122 fprintf(stderr, "Unrecognized option: %c\n", argv[i][1]);
128 filename_list.push_back(argv[i]);
132 if ( filename_list.empty() )
134 fputs("Input j2c filename(s) required.\n", stderr);
147 main(int argc, const char** argv)
149 CommandOptions Options(argc, argv);
151 if ( Options.version_flag )
154 if ( Options.help_flag )
157 if ( Options.version_flag || Options.help_flag )
160 if ( Options.error_flag )
162 fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
166 ASDCP::JP2K::FrameBuffer FB;
168 CodestreamParser Parser;
169 std::list<std::string>::iterator i;
170 bool has_soc = false;
172 Result_t result = FB.Capacity(1024*1024*4);
174 for ( i = Options.filename_list.begin(); ASDCP_SUCCESS(result) && i != Options.filename_list.end(); i++ )
176 result = Parser.OpenReadFrame(i->c_str(), FB);
178 if ( ASDCP_SUCCESS(result) )
180 const byte_t* p = FB.RoData();
181 const byte_t* end_p = p + FB.Size();
183 while ( p < end_p && ASDCP_SUCCESS(GetNextMarker(&p, MyMarker)) )
185 if ( MyMarker.m_Type == MRK_SOC )
189 fprintf(stderr, "Duplicate SOC detected.\n");
190 result = RESULT_FAIL;
201 fprintf(stderr, "Markers detected before SOC.\n");
202 result = RESULT_FAIL;
207 if ( Options.verbose_flag )
209 MyMarker.Dump(stdout);
211 if ( Options.detail_flag )
212 hexdump(MyMarker.m_Data - 2, MyMarker.m_DataSize + 2, stdout);
215 if ( MyMarker.m_Type == MRK_SOD )
219 else if ( MyMarker.m_Type == MRK_SIZ )
221 Accessor::SIZ SIZ_(MyMarker);
224 else if ( MyMarker.m_Type == MRK_COD )
226 Accessor::COD COD_(MyMarker);
229 else if ( MyMarker.m_Type == MRK_COM )
231 Accessor::COM COM_(MyMarker);
234 else if ( MyMarker.m_Type == MRK_QCD )
236 Accessor::QCD QCD_(MyMarker);
241 fprintf(stderr, "Unprocessed marker - %s\n", GetMarkerString(MyMarker.m_Type));
250 fprintf(stdout, "0x%02x 0x%02x 0x%02x\n", *(p+1), *(p+2), *(p+3));
262 if ( ASDCP_FAILURE(result) )
264 fputs("Program stopped on error.\n", stderr);
266 if ( result != RESULT_FAIL )
268 fputs(result, stderr);