The example program on this page may be used, distributed and modified without limitation.

Show Image

This example reads and displays an image in any supported image format (GIF, BMP, PPM, XMP etc.)
#include "showimg.h"
#include <qmenubar.h>
#include <qfiledlg.h>
#include <qmsgbox.h>
#include <qpopmenu.h>
#include <qlabel.h>
#include <qpainter.h>
#include <qkeycode.h>
#include <qapp.h>

/*
  In the constructor, we just pass the standard parameters on to
  QWidget.

  The menu uses a single slot to simplify the process of adding
  more items to the options menu.
*/
ImageViewer::ImageViewer( QWidget *parent, const char *name )
    : QWidget( parent, name ),
      conversion_flags( PreferDither ),
      filename( 0 ),
      helpmsg( 0 )
{
    pickx = -1;

    menubar = new QMenuBar(this);
    menubar->setSeparator( QMenuBar::InWindowsStyle );

    QStrList fmt = QImage::outputFormats();
    saveimage = new QPopupMenu();
    savepixmap = new QPopupMenu();
    for (const char* f = fmt.first(); f; f = fmt.next()) {
        saveimage->insertItem( f );
        savepixmap->insertItem( f );
    }
    connect( saveimage, SIGNAL(activated(int)), this, SLOT(saveImage(int)) );
    connect( savepixmap, SIGNAL(activated(int)), this, SLOT(savePixmap(int)) );

    file = new QPopupMenu();
    menubar->insertItem( "&File", file );
    file->insertItem( "Open...", this,  SLOT(openFile()), CTRL+Key_O );
    si = file->insertItem( "Save image", saveimage );
    sp = file->insertItem( "Save pixmap", savepixmap );
    file->insertSeparator();
    file->insertItem( "Quit", qApp,  SLOT(quit()), CTRL+Key_Q );

    options =  new QPopupMenu();
    menubar->insertItem( "&Options", options );
    ac = options->insertItem( "AutoColor" );
    co = options->insertItem( "ColorOnly" );
    mo = options->insertItem( "MonoOnly" );
    options->insertSeparator();
    fd = options->insertItem( "DiffuseDither" );
    bd = options->insertItem( "OrderedDither" );
    td = options->insertItem( "ThresholdDither" );
    options->insertSeparator();
    ta = options->insertItem( "ThresholdAlphaDither" );
    ba = options->insertItem( "OrderedAlphaDither" );
    fa = options->insertItem( "DiffuseAlphaDither" );
    options->insertSeparator();
    ad = options->insertItem( "PreferDither" );
    dd = options->insertItem( "AvoidDither" );
    options->setCheckable( TRUE );
    setMenuItemFlags();

    menubar->insertSeparator();

    QPopupMenu* help = new QPopupMenu();
    menubar->insertItem( "&Help", help );
    help->insertItem( "Help!", this, SLOT(giveHelp()), CTRL+Key_H );

    connect( options, SIGNAL(activated(int)), this, SLOT(doOption(int)) );

    status = new QLabel(this);
    status->setFrameStyle( QFrame::WinPanel | QFrame::Sunken );
    status->setFixedHeight( fontMetrics().height() + 4 );

    setMouseTracking( TRUE );
}

/*
  This function modifies the conversion_flags when an options menu item
  is selected, then ensures all menu items are up to date, and reconverts
  the image if possibly necessary.
*/
void ImageViewer::doOption(int item)
{
    if ( options->isItemChecked( item ) ) return; // They are all radio buttons

    int ocf = conversion_flags;

    if ( item == ac ) {
        conversion_flags = conversion_flags & ~ColorMode_Mask | AutoColor;
    } else if ( item == co ) {
        conversion_flags = conversion_flags & ~ColorMode_Mask | ColorOnly;
    } else if ( item == mo ) {
        conversion_flags = conversion_flags & ~ColorMode_Mask | MonoOnly;
    } else if ( item == fd ) {
        conversion_flags = conversion_flags & ~Dither_Mask | DiffuseDither;
    } else if ( item == bd ) {
        conversion_flags = conversion_flags & ~Dither_Mask | OrderedDither;
    } else if ( item == td ) {
        conversion_flags = conversion_flags & ~Dither_Mask | ThresholdDither;
    } else if ( item == ta ) {
        conversion_flags = conversion_flags & ~AlphaDither_Mask | ThresholdAlphaDither;
    } else if ( item == fa ) {
        conversion_flags = conversion_flags & ~AlphaDither_Mask | DiffuseAlphaDither;
    } else if ( item == ba ) {
        conversion_flags = conversion_flags & ~AlphaDither_Mask | OrderedAlphaDither;
    } else if ( item == ad ) {
        conversion_flags = conversion_flags & ~DitherMode_Mask | PreferDither;
    } else if ( item == dd ) {
        conversion_flags = conversion_flags & ~DitherMode_Mask | AvoidDither;
    }

    if ( ocf != conversion_flags ) {
        setMenuItemFlags();
        // And reconvert...
        reconvertImage();
        repaint();                      // show image in widget
    }
}

/*
  Set the options menu to reflect the conversion_flags value.
*/
void ImageViewer::setMenuItemFlags()
{
    // File
    bool valid_image = pm.size() != QSize( 0, 0 );
    file->setItemEnabled( si, valid_image );
    file->setItemEnabled( sp, valid_image );

    // Options
    bool may_need_color_dithering =
            image.depth() == 32 && QPixmap::defaultDepth() <= 8;
    bool may_need_dithering = may_need_color_dithering
         || image.depth() > 1 && options->isItemChecked(mo)
         || image.depth() > 1 && QPixmap::defaultDepth() == 1;
    bool has_alpha_mask = image.hasAlphaBuffer();

    options->setItemEnabled( fd, may_need_dithering );
    options->setItemEnabled( bd, may_need_dithering );
    options->setItemEnabled( td, may_need_dithering );

    options->setItemEnabled( ta, has_alpha_mask );
    options->setItemEnabled( fa, has_alpha_mask );
    options->setItemEnabled( ba, has_alpha_mask );

    options->setItemEnabled( ad, may_need_color_dithering );
    options->setItemEnabled( dd, may_need_color_dithering );

    options->setItemChecked( ac, (conversion_flags & ColorMode_Mask) == AutoColor );
    options->setItemChecked( co, (conversion_flags & ColorMode_Mask) == ColorOnly );
    options->setItemChecked( mo, (conversion_flags & ColorMode_Mask) == MonoOnly );
    options->setItemChecked( fd, (conversion_flags & Dither_Mask) == DiffuseDither );
    options->setItemChecked( bd, (conversion_flags & Dither_Mask) == OrderedDither );
    options->setItemChecked( td, (conversion_flags & Dither_Mask) == ThresholdDither );
    options->setItemChecked( ta, (conversion_flags & AlphaDither_Mask) == ThresholdAlphaDither );
    options->setItemChecked( fa, (conversion_flags & AlphaDither_Mask) == DiffuseAlphaDither );
    options->setItemChecked( ba, (conversion_flags & AlphaDither_Mask) == OrderedAlphaDither );
    options->setItemChecked( ad, (conversion_flags & DitherMode_Mask) == PreferDither );
    options->setItemChecked( dd, (conversion_flags & DitherMode_Mask) == AvoidDither );
}

void ImageViewer::updateStatus()
{
    if ( pm.size() == QSize( 0, 0 ) ) {
        if ( filename )
            status->setText("Could not load image");
        else
            status->setText("No image - select Open from File menu.");
    } else {
        QString message, moremsg;
        message.sprintf("%dx%d", image.width(), image.height());
        if ( pm.size() != pmScaled.size() ) {
            moremsg.sprintf(" [%dx%d]", pmScaled.width(),
                pmScaled.height());
            message += moremsg;
        }
        moremsg.sprintf(", %d bits ", image.depth());
        message += moremsg;
        if (image.valid(pickx,picky)) {
            moremsg.sprintf("(%d,%d)=#%0*x ",
                          pickx, picky,
                          image.hasAlphaBuffer() ? 8 : 6,
                          image.pixel(pickx,picky));
            message += moremsg;
        }
        if ( image.numColors() > 0 ) {
            moremsg.sprintf(", %d colors", image.numColors());
            message += moremsg;
        }
        if ( image.hasAlphaBuffer() ) {
            if ( image.depth() == 8 ) {
                int i;
                bool alpha[256];
                int nalpha=0;

                for (i=0; i<256; i++)
                    alpha[i] = FALSE;

                for (i=0; i<image.numColors(); i++) {
                    int alevel = image.color(i) >> 24;
                    if (!alpha[alevel]) {
                        alpha[alevel] = TRUE;
                        nalpha++;
                    }
                }
                moremsg.sprintf(", %d alpha levels", nalpha);
            } else {
                // Too many pixels to bother counting.
                moremsg = ", 8-bit alpha channel";
            }
            message += moremsg;
        }
        status->setText(message);
    }
}

/*
  This function saves the image.
*/
void ImageViewer::saveImage( int item )
{
    const char* fmt = saveimage->text(item);
    QString savefilename = QFileDialog::getSaveFileName(0, 0, 0, filename);
    if ( !savefilename.isEmpty() )
        if ( !image.save( savefilename, fmt ) )
            QMessageBox::warning( this, "Save failed", "Error saving file" );
}

/*
  This function saves the converted image.
*/
void ImageViewer::savePixmap( int item )
{
    const char* fmt = savepixmap->text(item);
    QString savefilename = QFileDialog::getSaveFileName(0, 0, 0, filename);
    if ( !savefilename.isEmpty() )
        if ( !pmScaled.save( savefilename, fmt ) )
            QMessageBox::warning( this, "Save failed", "Error saving file" );
}

/*
  This function is the slot for processing the Open menu item.
*/
void ImageViewer::openFile()
{
    QString newfilename = QFileDialog::getOpenFileName();
    if ( !newfilename.isEmpty() ) {
        loadImage( newfilename ) ;
        repaint();                      // show image in widget
    }
}

/*
  This function loads an image from a file and resizes the widget to
  exactly fit the image size. If the file was not found or the image
  format was unknown it will resize the widget to fit the errorText
  message (see above) displayed in the current font.

  Returns TRUE if the image was successfully loaded.
*/

bool ImageViewer::loadImage( const char *fileName )
{
    filename = fileName;
    bool ok = FALSE;
    if ( filename ) {
        QApplication::setOverrideCursor( waitCursor ); // this might take time
        ok = image.load(filename, 0);
        if ( ok )
            ok = reconvertImage();
        if ( ok ) {
            setCaption( filename );                     // set window caption
            int w = pm.width();
            int h = pm.height();

            const int reasonable_width = 128;
            if ( w < reasonable_width ) {
                // Integer scale up to something reasonable
                int multiply = ( reasonable_width + w - 1 ) / w;
                w *= multiply;
                h *= multiply;
            }

            h += menubar->heightForWidth(w) + status->height();
            resize( w, h );                             // we resize to fit image
        } else {
            pm.resize(0,0);                             // couldn't load image
            update();
        }
        QApplication::restoreOverrideCursor();  // restore original cursor
    }
    updateStatus();
    setMenuItemFlags();
    return ok;
}

bool ImageViewer::reconvertImage()
{
    bool success = FALSE;

    QApplication::setOverrideCursor( waitCursor ); // this might take time
    if ( pm.convertFromImage(image, conversion_flags) )
    {
        pmScaled = pm;
        resize( width(), height() );
        success = TRUE;                         // load successful
    } else {
        pm.resize(0,0);                         // couldn't load image
    }
    updateStatus();
    setMenuItemFlags();
    QApplication::restoreOverrideCursor();      // restore original cursor
    return success;                             // TRUE if loaded OK
}

/*
  This functions scales the pixmap in the member variable "pm" to fit the
  widget size and  puts the resulting pixmap in the member variable "pmScaled".
*/

void ImageViewer::scale()
{
    int h = height() - menubar->heightForWidth( width() ) - status->height();

    QApplication::setOverrideCursor( waitCursor ); // this might take time
    if ( width() == pm.width() && h == pm.height() )
    {                                           // no need to scale if widget
        pmScaled = pm;                          // size equals pixmap size
    } else {
        QWMatrix m;                             // transformation matrix
        m.scale(((double)width())/pm.width(),   // define scale factors
                ((double)h)/pm.height());
        pmScaled = pm.xForm( m );               // create scaled pixmap
    }
    QApplication::restoreOverrideCursor();      // restore original cursor
}

/*
  The resize event handler, if a valid pixmap was loaded it will call
  scale() to fit the pixmap to the new widget size.
*/

void ImageViewer::resizeEvent( QResizeEvent * )
{
    status->setGeometry(0, height() - status->height(),
                        width(), status->height());

    if ( pm.size() == QSize( 0, 0 ) )           // we couldn't load the image
        return;

    int h = height() - menubar->heightForWidth( width() ) - status->height();
    if ( width() != pmScaled.width() || h != pmScaled.height())
    {                                           // if new size,
        scale();                                // scale pmScaled to window
        updateStatus();
    }
}

/*
  Record the pixel position of interest.
*/
void ImageViewer::mouseMoveEvent( QMouseEvent *e )
{
    if ( pm.size() != QSize( 0, 0 ) ) {
        int h = height() - menubar->heightForWidth( width() ) - status->height();
        int npickx = e->x() * image.width() / width();
        int npicky = (e->y()-menubar->heightForWidth( width() )) * image.height() / h;
        if (npickx != pickx || npicky != picky ) {
            pickx = npickx;
            picky = npicky;
            updateStatus();
        }
    }
}

/*
  Draws the portion of the scaled pixmap that needs to be updated or prints
  an error message if no legal pixmap has been loaded.
*/

void ImageViewer::paintEvent( QPaintEvent *e )
{
    if ( pm.size() != QSize( 0, 0 ) ) {         // is an image loaded?
        QPainter painter(this);
        painter.setClipRect(e->rect());
        painter.drawPixmap(0, menubar->heightForWidth( width() ), pmScaled);
    }
}

/*
  Explain anything that might be confusing.
*/
void ImageViewer::giveHelp()
{
    if (!helpmsg) {
        QString helptext = "Usage: showimg [filename]";
        QStrList support = QImage::inputFormats();
        helptext += "\n\nSupported input formats:\n";
        int lastnl = helptext.length();
        helptext += "  ";
        const char* f = support.first();
        helptext += f;
        f = support.next();
        for (; f; f = support.next()) {
            helptext += ',';
            if ( helptext.length() - lastnl > 40 ) {
                helptext += "\n  ";
                lastnl = helptext.length() - 2;
            } else {
                helptext += ' ';
            }
            helptext += f;
        }
        helpmsg = new QMessageBox( "Help", helptext,
            QMessageBox::Information, QMessageBox::Ok, 0, 0, 0, 0, FALSE );
    }
    helpmsg->show();
    helpmsg->raise();
}

Generated at 23:50, 1998/03/16 for Qt version 1.33 by the webmaster at Troll Tech