Release me
[asdcplib.git] / src / j2c-test.cpp
1 /*
2 Copyright (c) 2005-2014, John Hurst
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
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.
15
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.
26 */
27 /*! \file    j2c-test.cpp
28     \version $Id$
29     \brief   JP2K parser test
30 */
31
32 #include <AS_DCP.h>
33 #include <KM_fileio.h>
34 #include <KM_util.h>
35 #include <JP2K.h>
36
37 using namespace Kumu;
38 using namespace ASDCP;
39 using namespace ASDCP::JP2K;
40
41
42
43 //------------------------------------------------------------------------------------------
44 //
45 // command line option parser class
46
47 static const char* PROGRAM_NAME = "j2c-test";    // program name for messages
48
49 // Macros used to test command option data state.
50
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] == '-' ) \
55                                  { \
56                                    fprintf(stderr, "Argument not found for option %c.\n", (c)); \
57                                    return; \
58                                  }
59 //
60 void
61 banner(FILE* stream = stderr)
62 {
63   fprintf(stream, "\n\
64 %s (asdcplib %s)\n\n\
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);
71 }
72
73 //
74 void
75 usage(FILE* stream = stderr)
76 {
77   fprintf(stream, "\
78 USAGE: %s [-h|-help] [-V]\n\
79 \n\
80        %s [-r] [-v] <filename> [...]\n\
81 \n\
82   -V           - Show version\n\
83   -h           - Show help\n\
84   -r           - Show raw data\n\
85   -v           - Print extra detail\n\
86 \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);
90 }
91
92 //
93 //
94 class CommandOptions
95 {
96   CommandOptions();
97
98 public:
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;
105
106   CommandOptions(int argc, const char** argv) :
107     error_flag(true), version_flag(false), verbose_flag(false),
108     detail_flag(false), help_flag(false)
109   {
110     for ( int i = 1; i < argc; i++ )
111       {
112         if ( argv[i][0] == '-' && isalpha(argv[i][1]) && argv[i][2] == 0 )
113           {
114             switch ( argv[i][1] )
115               {
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;
120
121               default:
122                 fprintf(stderr, "Unrecognized option: %c\n", argv[i][1]);
123                 return;
124               }
125           }
126         else
127           {
128             filename_list.push_back(argv[i]);
129           }
130       }
131
132     if ( filename_list.empty() )
133       {
134         fputs("Input j2c filename(s) required.\n", stderr);
135         return;
136       }
137
138     error_flag = false;
139   }
140 };
141
142
143
144
145 //
146 int
147 main(int argc, const char** argv)
148 {
149   CommandOptions Options(argc, argv);
150
151   if ( Options.version_flag )
152     banner();
153
154   if ( Options.help_flag )
155     usage();
156
157   if ( Options.version_flag || Options.help_flag )
158     return 0;
159
160   if ( Options.error_flag )
161     {
162       fprintf(stderr, "There was a problem. Type %s -h for help.\n", PROGRAM_NAME);
163       return 3;
164     }
165
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;
173
174   Result_t result = FB.Capacity(1024*1024*4);
175   
176   for ( i = Options.filename_list.begin(); ASDCP_SUCCESS(result) && i != Options.filename_list.end(); i++ )
177     {
178       result = Parser.OpenReadFrame(i->c_str(), FB);
179
180       if ( ASDCP_SUCCESS(result) )
181         {
182           const byte_t* p = FB.RoData();
183           const byte_t* end_p = p + FB.Size();
184
185           while ( p < end_p && ASDCP_SUCCESS(GetNextMarker(&p, current_marker)) )
186             {
187               ++marker_count;
188
189               if ( current_marker.m_Type == MRK_SOC )
190                 {
191                   if ( has_soc )
192                     {
193                       fprintf(stderr, "Duplicate SOC detected.\n");
194                       result = RESULT_FAIL;
195                       break;
196                     }
197                   else
198                     {
199                       has_soc = true;
200                       continue;
201                     }
202                 }
203
204               if  ( ! has_soc )
205                 {
206                   fprintf(stderr, "Markers detected before SOC.\n");
207                   result = RESULT_FAIL;
208                   break;
209                 }
210
211               if ( Options.verbose_flag )
212                 {
213                   current_marker.Dump(stdout);
214
215                   if ( Options.detail_flag )
216                     {
217                       hexdump(current_marker.m_Data - 2, current_marker.m_DataSize + 2, stdout);
218                     }
219                 }
220
221               if ( current_marker.m_Type == MRK_SOD )
222                 {
223                   p = end_p;
224                 }
225               else if ( current_marker.m_Type == MRK_SIZ )
226                 {
227                   Accessor::SIZ SIZ_(current_marker);
228                   SIZ_.Dump(stdout);
229                 }
230               else if ( current_marker.m_Type == MRK_COD )
231                 {
232                   Accessor::COD COD_(current_marker);
233                   COD_.Dump(stdout);
234                 }
235               else if ( current_marker.m_Type == MRK_COM )
236                 {
237                   Accessor::COM COM_(current_marker);
238                   COM_.Dump(stdout);
239                 }
240               else if ( current_marker.m_Type == MRK_QCD )
241                 {
242                   Accessor::QCD QCD_(current_marker);
243                   QCD_.Dump(stdout);
244                 }
245               else if ( current_marker.m_Type == MRK_TLM )
246                 {
247                   has_tlm = true;
248                 }
249               else
250                 {
251                   fprintf(stderr, "Unprocessed marker - %s\n", GetMarkerString(current_marker.m_Type));
252                 }
253             }
254
255           /*
256           while ( p < end_p )
257             {
258               if ( *p == 0xff )
259                 {
260                   fprintf(stdout, "0x%02x 0x%02x 0x%02x\n", *(p+1), *(p+2), *(p+3));
261                   p += 4;
262                 }
263               else
264                 {
265                   ++p;
266                 }
267             }
268           */
269         }
270     }
271
272   if ( marker_count == 0 )
273     {
274       fprintf(stderr, "No JPEG 2000 marker items found.\n");
275       result = RESULT_FAIL;
276     }
277   else
278     {
279       fprintf(stderr, "Processed %d JPEG 2000 marker item%s.\n", marker_count, (marker_count==1?"":"s"));
280
281       if  ( ! has_tlm )
282         {
283           fprintf(stderr, "No TLM marker found.\n");
284           result = RESULT_FAIL;
285         }
286     }
287
288   if ( ASDCP_FAILURE(result) )
289     {
290       fputs("Program stopped on error.\n", stderr);
291
292       if ( result != RESULT_FAIL )
293         {
294           fputs(result, stderr);
295           fputc('\n', stderr);
296         }
297
298       return 1;
299     }
300
301   return 0;
302 }
303
304 //
305 // end j2c-test.cpp
306 //