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-2018 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;
167 Marker current_marker;
168 CodestreamParser Parser;
169 std::list<std::string>::iterator i;
170 bool has_soc = false;
171 bool has_tlm = false;
172 int marker_count = 0;
174 Result_t result = FB.Capacity(1024*1024*4);
176 for ( i = Options.filename_list.begin(); ASDCP_SUCCESS(result) && i != Options.filename_list.end(); i++ )
178 result = Parser.OpenReadFrame(i->c_str(), FB);
180 if ( ASDCP_SUCCESS(result) )
182 const byte_t* p = FB.RoData();
183 const byte_t* end_p = p + FB.Size();
185 while ( p < end_p && ASDCP_SUCCESS(GetNextMarker(&p, current_marker)) )
189 if ( current_marker.m_Type == MRK_SOC )
193 fprintf(stderr, "Duplicate SOC detected.\n");
194 result = RESULT_FAIL;
206 fprintf(stderr, "Markers detected before SOC.\n");
207 result = RESULT_FAIL;
211 if ( Options.verbose_flag )
213 current_marker.Dump(stdout);
215 if ( Options.detail_flag )
217 hexdump(current_marker.m_Data - 2, current_marker.m_DataSize + 2, stdout);
221 if ( current_marker.m_Type == MRK_SOD )
225 else if ( current_marker.m_Type == MRK_SIZ )
227 Accessor::SIZ SIZ_(current_marker);
230 else if ( current_marker.m_Type == MRK_COD )
232 Accessor::COD COD_(current_marker);
235 else if ( current_marker.m_Type == MRK_COM )
237 Accessor::COM COM_(current_marker);
240 else if ( current_marker.m_Type == MRK_QCD )
242 Accessor::QCD QCD_(current_marker);
245 else if ( current_marker.m_Type == MRK_TLM )
251 fprintf(stderr, "Unprocessed marker - %s\n", GetMarkerString(current_marker.m_Type));
260 fprintf(stdout, "0x%02x 0x%02x 0x%02x\n", *(p+1), *(p+2), *(p+3));
272 if ( marker_count == 0 )
274 fprintf(stderr, "No JPEG 2000 marker items found.\n");
275 result = RESULT_FAIL;
279 fprintf(stderr, "Processed %d JPEG 2000 marker item%s.\n", marker_count, (marker_count==1?"":"s"));
283 fprintf(stderr, "No TLM marker found.\n");
284 result = RESULT_FAIL;
288 if ( ASDCP_FAILURE(result) )
290 fputs("Program stopped on error.\n", stderr);
292 if ( result != RESULT_FAIL )
294 fputs(result, stderr);