A PNG image viewer using LibCapy

In this article I'll show how to create a simple GUI application, called Cavy, to display images in PNG format using LibCapy's CapyDisplay object. The application will take a list of image's paths in argument, display them one by one, and allow to zoom in/out. The control of the application will be possible via the keyboard.

(The complete code is available at the end of the article)

First, the main function. I create the structure to manage the application, parse the arguments, init the app and run it until the user closes the window.

The app structure contains the following data:

I also define a few constants and shortcut macros to make my life easier.

The arguments from the command line are simple. There is the display size (optional), and everything else is considered to be a path to an image to be displayed (invalid path or image will be ignored)

I'll use try/catch to handle exceptions. At application level there is only one exception: when the user gave no image to display in arguments.

Setting exception management in the main:

Initialise necessary data when instanciating the app structure.

Then the app initialisation function. I load the first valid PNG image in the list of arguments, calculate the dimension of the display based on the dimension of this image and the size given in argument, and finally create the display.

The LoadImg function loads the previous/next image in the list of arguments, ignoring any invalid argument.

The main loop of the app looks for keystrokes and process them until the display is closed. As the app doesn't need to be particularly responsive, take care to sleep and do nothing if there is nothing to do (not refreshing the displayed image if not necessary).

And that's it. The complete code:

Compile with the following Makefile:

Copyright 2021 Baillehache Pascal