7979
8080#define FONT_SIZE 18 .0f
8181
82-
8382enum {
8483 E_NO_ERROR = 0 ,
8584 E_BAD_PARAMETER = 64 ,
9695// ============================================================================
9796
9897// Preferences.
99- static int display_templates = 0 ;
100- static int display_features = 0 ;
101- static int display_bins = 0 ;
98+ static int display_templates = 1 ;
99+ static int display_features = 1 ;
100+ static int display_bins = 1 ;
102101static char *inputFilePath = NULL ;
103102
104103// Input.
@@ -113,10 +112,10 @@ static double imageZoom = 1.0f;
113112
114113// Drawing.
115114// Window and GL context.
116- static SDL_GLContext gSDLContext = NULL ;
117- static int contextWidth = 0 ;
118- static int contextHeight = 0 ;
119- static SDL_Window* gSDLWindow = NULL ;
115+ static SDL_GLContext gSDLImageContext = NULL ;
116+ static int gImageContextWidth = 0 ;
117+ static int gImageContextHeight = 0 ;
118+ static SDL_Window* gSDLImageWindow = NULL ;
120119static ARGL_CONTEXT_SETTINGS_REF gArglContextSettings = NULL ;
121120static int gShowHelp = 1 ;
122121static int gShowMode = 1 ;
@@ -129,13 +128,15 @@ static char exitcode = -1;
129128// ============================================================================
130129
131130static void loadImage (void );
131+ static void getImageFeature ();
132132static void setImagePage (int page_in);
133133static void quit (int rc);
134- static void reshape (int w, int h);
134+ static float calcZoomToFit (int sourceSizeX, int sourceSizeY, int destSizeX, int destSizeY);
135+ static void reshapeImageWindow (int w, int h);
135136static void keyboard (SDL_Keycode key);
136137static void processCommandLineOptions (int argc, char *argv[]);
137138static void usage (char *com);
138- static void drawView (void );
139+ static void drawImageView (void );
139140static void drawBackground (const float width, const float height, const float x, const float y);
140141static void printHelpKeys (void );
141142static void printMode (void );
@@ -155,13 +156,21 @@ int main(int argc, char *argv[])
155156 // Preferences.
156157 processCommandLineOptions (argc, argv);
157158
158- // Create a window.
159- gSDLWindow = SDL_CreateWindow (argv[0 ],
159+ SDL_DisplayMode dm;
160+ SDL_GetCurrentDisplayMode (0 , &dm);
161+ int screenWidth = dm.w ;
162+ int screenHeight = dm.h ;
163+
164+ loadImage ();
165+ float imageZoom = calcZoomToFit (refImageX, refImageY, screenWidth - 200 , screenHeight - 200 );
166+
167+ // Create a window to display the image.
168+ gSDLImageWindow = SDL_CreateWindow (argv[0 ],
160169 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
161- 1280 , 720 ,
170+ ( int )(refImageX * imageZoom), ( int )(refImageY * imageZoom) ,
162171 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
163172 );
164- if (!gSDLWindow ) {
173+ if (!gSDLImageWindow ) {
165174 ARLOGe (" Error creating window: %s.\n " , SDL_GetError ());
166175 quit (-1 );
167176 }
@@ -172,20 +181,20 @@ int main(int argc, char *argv[])
172181 SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 16 );
173182 SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1 ); // This is the default.
174183 SDL_GL_SetSwapInterval (1 );
175- gSDLContext = SDL_GL_CreateContext (gSDLWindow );
176- if (!gSDLContext ) {
184+ gSDLImageContext = SDL_GL_CreateContext (gSDLImageWindow );
185+ if (!gSDLImageContext ) {
177186 ARLOGe (" Error creating OpenGL context: %s.\n " , SDL_GetError ());
178187 return -1 ;
179188 }
180189 int w, h;
181- SDL_GL_GetDrawableSize (SDL_GL_GetCurrentWindow () , &w, &h);
182- reshape (w, h);
190+ SDL_GL_GetDrawableSize (gSDLImageWindow , &w, &h);
191+ reshapeImageWindow (w, h);
183192
184193 EdenGLFontInit (1 ); // contextsActiveCount=1
185194 EdenGLFontSetFont (EDEN_GL_FONT_ID_Stroke_Roman);
186195 EdenGLFontSetSize (FONT_SIZE);
187196
188- loadImage ();
197+ getImageFeature ();
189198
190199 // Main loop.
191200 bool done = false ;
@@ -198,12 +207,12 @@ int main(int argc, char *argv[])
198207 break ;
199208 } else if (ev.type == SDL_WINDOWEVENT) {
200209 // ARLOGd("Window event %d.\n", ev.window.event);
201- if (ev.window .event == SDL_WINDOWEVENT_RESIZED && ev.window .windowID == SDL_GetWindowID (gSDLWindow )) {
210+ if (ev.window .event == SDL_WINDOWEVENT_RESIZED && ev.window .windowID == SDL_GetWindowID (gSDLImageWindow )) {
202211 // int32_t w = ev.window.data1;
203212 // int32_t h = ev.window.data2;
204213 int w, h;
205- SDL_GL_GetDrawableSize (gSDLWindow , &w, &h);
206- reshape (w, h);
214+ SDL_GL_GetDrawableSize (gSDLImageWindow , &w, &h);
215+ reshapeImageWindow (w, h);
207216 }
208217 } else if (ev.type == SDL_KEYDOWN) {
209218 keyboard (ev.key .keysym .sym );
@@ -215,12 +224,12 @@ int main(int argc, char *argv[])
215224 quit (0 );
216225}
217226
218- static void reshape (int w, int h)
227+ static void reshapeImageWindow (int w, int h)
219228{
220- contextWidth = w;
221- contextHeight = h;
229+ gImageContextWidth = w;
230+ gImageContextHeight = h;
222231 ARLOGd (" Resized to %dx%d.\n " , w, h);
223- drawView ();
232+ drawImageView ();
224233}
225234
226235static void quit (int rc)
@@ -249,7 +258,10 @@ static void loadImage(void)
249258 exit (0 );
250259 }
251260 ARPRINT (" end.\n " );
252-
261+ }
262+
263+ static void getImageFeature ()
264+ {
253265 if (display_templates || display_features) {
254266 cv::Mat _image = cv::Mat (refImageY, refImageX, CV_8UC1, refImage.get ());
255267 if (display_templates) {
@@ -287,7 +299,7 @@ static void setImagePage(int page_in)
287299 gArglContextSettings = arglSetupForCurrentContext (&cparam, AR_PIXEL_FORMAT_MONO);
288300 arglDistortionCompensationSet (gArglContextSettings , FALSE );
289301 arglPixelBufferDataUpload (gArglContextSettings , refImage.get ());
290- drawView ();
302+ drawImageView ();
291303}
292304
293305static void processCommandLineOptions (int argc, char *argv[])
@@ -325,23 +337,17 @@ static void processCommandLineOptions(int argc, char *argv[])
325337 else if (strcmp (&(argv[i][10 ]), " ERROR" ) == 0 ) arLogLevel = AR_LOG_LEVEL_ERROR;
326338 else usage (argv[0 ]);
327339 } else if (strcmp (argv[i], " -templates" ) == 0 ) {
328- display_defaults = 0 ;
329340 display_templates = 1 ;
330341 } else if (strcmp (argv[i], " -notemplates" ) == 0 ) {
331- display_defaults = 0 ;
332342 display_templates = 0 ;
333343 } else if (strcmp (argv[i], " -features" ) == 0 ) {
334- display_defaults = 0 ;
335344 display_features = 1 ;
336345 } else if (strcmp (argv[i], " -nofeatures" ) == 0 ) {
337- display_defaults = 0 ;
338346 display_features = 0 ;
339347 } else if ( strcmp (argv[i], " -bins" ) == 0 ) {
340- display_defaults = 0 ;
341- display_bins = 0 ;
342- } else if ( strcmp (argv[i], " -nobins" ) == 0 ) {
343- display_defaults = 0 ;
344348 display_bins = 1 ;
349+ } else if ( strcmp (argv[i], " -nobins" ) == 0 ) {
350+ display_bins = 0 ;
345351 } else {
346352 if (!inputFilePath) inputFilePath = strdup (argv[i]);
347353 else usage (argv[0 ]);
@@ -350,7 +356,6 @@ static void processCommandLineOptions(int argc, char *argv[])
350356 i++;
351357 }
352358 if (!inputFilePath) usage (argv[0 ]);
353- if (display_defaults) display_features = display_templates = display_bins = 1 ;
354359}
355360
356361static void usage ( char *com )
@@ -395,27 +400,32 @@ static void keyboard(SDL_Keycode key)
395400 break ;
396401 }
397402 if (redraw) {
398- drawView ();
403+ drawImageView ();
399404 }
400405}
401406
407+ static float calcZoomToFit (int sourceSizeX, int sourceSizeY, int destSizeX, int destSizeY)
408+ {
409+ float xzoom, yzoom;
410+ xzoom = (float )destSizeX / (float )sourceSizeX;
411+ yzoom = (float )destSizeY / (float )sourceSizeY;
412+ return (xzoom > yzoom ? yzoom : xzoom);
413+ }
414+
402415//
403416// This function is called when the window needs redrawing.
404417//
405- static void drawView (void )
418+ static void drawImageView (void )
406419{
407420 int i;
408421 int viewport[4 ];
409422
410423 if (!refImage || !gArglContextSettings ) return ;
411424
412- SDL_GL_MakeCurrent (gSDLWindow , gSDLContext );
425+ SDL_GL_MakeCurrent (gSDLImageWindow , gSDLImageContext );
413426
414- float xzoom, yzoom;
415- xzoom = (float )contextWidth / (float )refImageX;
416- yzoom = (float )contextHeight / (float )refImageY;
417- imageZoom = (xzoom > yzoom ? yzoom : xzoom);
418- ARPRINT (" %dx%d input image will display in %dx%d window at %.1f%% size\n " , refImageX, refImageY, contextWidth, contextHeight, imageZoom*100 .0f );
427+ imageZoom = calcZoomToFit (refImageX, refImageY, gImageContextWidth , gImageContextHeight );
428+ ARPRINT (" %dx%d input image will display in %dx%d window at %.1f%% size\n " , refImageX, refImageY, gImageContextWidth , gImageContextHeight , imageZoom*100 .0f );
419429
420430 viewport[0 ] = 0 ;
421431 viewport[1 ] = 0 ;
@@ -535,14 +545,14 @@ static void drawView(void)
535545 }
536546
537547 // 2D overlays in context space.
538- glViewport (0 , 0 , contextWidth, contextHeight );
548+ glViewport (0 , 0 , gImageContextWidth , gImageContextHeight );
539549 glMatrixMode (GL_PROJECTION);
540550 glLoadIdentity ();
541- glOrtho (0 , (GLdouble)contextWidth , 0 , (GLdouble)contextHeight , -1.0 , 1.0 );
551+ glOrtho (0 , (GLdouble)gImageContextWidth , 0 , (GLdouble)gImageContextHeight , -1.0 , 1.0 );
542552 glMatrixMode (GL_MODELVIEW);
543553 glLoadIdentity ();
544554
545- EdenGLFontSetViewSize (contextWidth, contextHeight );
555+ EdenGLFontSetViewSize (gImageContextWidth , gImageContextHeight );
546556 glLineWidth (1 .0f );
547557 float white[4 ] = {1 .0f , 1 .0f , 1 .0f , 1 .0f };
548558 EdenGLFontSetColor (white);
@@ -558,7 +568,7 @@ static void drawView(void)
558568 }
559569 }
560570
561- SDL_GL_SwapWindow (gSDLWindow );
571+ SDL_GL_SwapWindow (gSDLImageWindow );
562572}
563573
564574//
@@ -638,7 +648,7 @@ static void printMode()
638648 }
639649*/
640650 // Window size.
641- snprintf (text, sizeof (text), " Drawing into %dx%d window" , contextWidth, contextHeight );
651+ snprintf (text, sizeof (text), " Drawing into %dx%d window" , gImageContextWidth , gImageContextHeight );
642652 EdenGLFontDrawLine (0 , NULL , (unsigned char *)text, 2 .0f , (line - 1 )*FONT_SIZE + 2 .0f , H_OFFSET_VIEW_LEFT_EDGE_TO_TEXT_LEFT_EDGE, V_OFFSET_TEXT_TOP_TO_VIEW_TOP);
643653 line++;
644654
0 commit comments