removed nascent ST2052-1 support pending completion
[asdcplib.git] / src / j2c-test.cpp
1 /*
2 Copyright (c) 2005-2010, 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-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);
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        MyMarker;
168   CodestreamParser Parser;
169   std::list<std::string>::iterator i;
170
171   Result_t result = FB.Capacity(1024*1024*4);
172   
173   for ( i = Options.filename_list.begin(); ASDCP_SUCCESS(result) && i != Options.filename_list.end(); i++ )
174     {
175       result = Parser.OpenReadFrame(i->c_str(), FB);
176
177       if ( ASDCP_SUCCESS(result) )
178         {
179           const byte_t* p = FB.RoData();
180           const byte_t* end_p = p + FB.Size();
181
182           while ( p < end_p && ASDCP_SUCCESS(GetNextMarker(&p, MyMarker)) )
183             {
184               if ( Options.verbose_flag )
185                 {
186                   MyMarker.Dump(stdout);
187
188                   if ( Options.detail_flag )
189                     hexdump(MyMarker.m_Data - 2, MyMarker.m_DataSize + 2, stdout);
190                 }
191
192               if ( MyMarker.m_Type == MRK_SOD )
193                 {
194                   p = end_p;
195                 }
196               else if ( MyMarker.m_Type == MRK_SIZ )
197                 {
198                   Accessor::SIZ SIZ_(MyMarker);
199                   SIZ_.Dump(stdout);
200                 }
201               else if ( MyMarker.m_Type == MRK_COD )
202                 {
203                   Accessor::COD COD_(MyMarker);
204                   COD_.Dump(stdout);
205                 }
206               else if ( MyMarker.m_Type == MRK_COM )
207                 {
208                   Accessor::COM COM_(MyMarker);
209                   COM_.Dump(stdout);
210                 }
211             }
212         }
213     }
214
215   if ( ASDCP_FAILURE(result) )
216     {
217       fputs("Program stopped on error.\n", stderr);
218
219       if ( result != RESULT_FAIL )
220         {
221           fputs(result, stderr);
222           fputc('\n', stderr);
223         }
224
225       return 1;
226     }
227
228   return 0;
229 }
230
231 //
232 // end j2c-test.cpp
233 //