added Mikey's patch for Solaris
[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-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   bool has_soc = false;
171
172   Result_t result = FB.Capacity(1024*1024*4);
173   
174   for ( i = Options.filename_list.begin(); ASDCP_SUCCESS(result) && i != Options.filename_list.end(); i++ )
175     {
176       result = Parser.OpenReadFrame(i->c_str(), FB);
177
178       if ( ASDCP_SUCCESS(result) )
179         {
180           const byte_t* p = FB.RoData();
181           const byte_t* end_p = p + FB.Size();
182
183           while ( p < end_p && ASDCP_SUCCESS(GetNextMarker(&p, MyMarker)) )
184             {
185               if ( MyMarker.m_Type == MRK_SOC )
186                 {
187                   if ( has_soc )
188                     {
189                       fprintf(stderr, "Duplicate SOC detected.\n");
190                       result = RESULT_FAIL;
191                       break;
192                     }
193                   else
194                     {
195                       has_soc = true;
196                       continue;
197                     }
198
199                   if  ( ! has_soc )
200                     {
201                       fprintf(stderr, "Markers detected before SOC.\n");
202                       result = RESULT_FAIL;
203                       break;
204                     }
205                 }
206
207               if ( Options.verbose_flag )
208                 {
209                   MyMarker.Dump(stdout);
210
211                   if ( Options.detail_flag )
212                     hexdump(MyMarker.m_Data - 2, MyMarker.m_DataSize + 2, stdout);
213                 }
214
215               if ( MyMarker.m_Type == MRK_SOD )
216                 {
217                   p = end_p;
218                 }
219               else if ( MyMarker.m_Type == MRK_SIZ )
220                 {
221                   Accessor::SIZ SIZ_(MyMarker);
222                   SIZ_.Dump(stdout);
223                 }
224               else if ( MyMarker.m_Type == MRK_COD )
225                 {
226                   Accessor::COD COD_(MyMarker);
227                   COD_.Dump(stdout);
228                 }
229               else if ( MyMarker.m_Type == MRK_COM )
230                 {
231                   Accessor::COM COM_(MyMarker);
232                   COM_.Dump(stdout);
233                 }
234               else if ( MyMarker.m_Type == MRK_QCD )
235                 {
236                   Accessor::QCD QCD_(MyMarker);
237                   QCD_.Dump(stdout);
238                 }
239               else
240                 {
241                   fprintf(stderr, "Unprocessed marker - %s\n", GetMarkerString(MyMarker.m_Type));
242                 }
243             }
244
245           /*
246           while ( p < end_p )
247             {
248               if ( *p == 0xff )
249                 {
250                   fprintf(stdout, "0x%02x 0x%02x 0x%02x\n", *(p+1), *(p+2), *(p+3));
251                   p += 4;
252                 }
253               else
254                 {
255                   ++p;
256                 }
257             }
258           */
259         }
260     }
261
262   if ( ASDCP_FAILURE(result) )
263     {
264       fputs("Program stopped on error.\n", stderr);
265
266       if ( result != RESULT_FAIL )
267         {
268           fputs(result, stderr);
269           fputc('\n', stderr);
270         }
271
272       return 1;
273     }
274
275   return 0;
276 }
277
278 //
279 // end j2c-test.cpp
280 //