Hi,
I am using QNX 6.2.1. I tried to get the following code to work.
I just wanted to do a simple program that would display a few images, but when I couldn’t get the sample program to work I got kinda discourage.
If you can give me some pointers I would be very greatful to you.
Thanks again for your help.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <malloc.h>
#include <assert.h>
#include <ctype.h>
#include <signal.h>
#include <Ph.h>
#include <Pt.h>
#include <photon/PxImage.h>
void *memory_allocate( long nbytes, int type );
void *memory_free( void *memory, int type );
void *warning( char *msg );
void *error( char *msg );
void *progress( int percent );
int UseShmem = 1;
int main( int argc, char *argv[] )
{
int c;
PtArg_t arg[5];
PtWidget_t *window;
char fname[255] = { 0 };
int Query = 0;
PhImage_t *img;
PxMethods_t methods;
/* initialize widget library and attach to Photon */
if( PtInit( NULL ) )
PtExit( EXIT_FAILURE );
while( ( c = getopt( argc, argv,
"f:QS" ) ) != -1 ) {
switch( c ) {
case 'f': // filename
strncpy(fname, optarg, 200 );
break;
case 'Q': // query file
Query = 1;
break;
case 'S':
UseShmem^=1;
break;
}
}
memset( &methods, 0, sizeof( PxMethods_t ) );
methods.px_alloc = memory_allocate;
methods.px_free = memory_free;
methods.px_warning = warning;
methods.px_error = error;
methods.px_progress = progress;
if( Query )
methods.flags |= PX_QUERY;
else
methods.flags |= PX_LOAD;
if( ( img = PxLoadImage( fname,
&methods ) ) == NULL ) {
fprintf( stderr, "Error loading/query %s\n",
fname );
PtExit( EXIT_FAILURE );
}
/* Make sure PhReleaseImage() releases any allocated
members of the PhImage_t structure. */
img->flags |= Ph_RELEASE_IMAGE_ALL;
if( Query ) {
printf( "Image width: %d\n", img->size.w );
printf( "Image height: %d\n", img->size.h );
printf( "Image BPL: %d\n", img->bpl );
printf( "Image colors: %d\n", img->colors );
printf( "Image type: %d\n", img->type );
PtExit( EXIT_SUCCESS );
}
/* increase the draw buffer */
PgSetDrawBufferSize( 0x8000 );
/* create a window */
PtSetArg( &arg[0], Pt_ARG_DIM, &img->size, 0 );
PtSetArg( &arg[1], Pt_ARG_WINDOW_TITLE,
"Photon Image Viewer", 0 );
window = PtCreateWidget( PtWindow, Pt_NO_PARENT, 2, arg );
/* Create a label widget with the image. Remember that
the widget creates a copy of the PhImage_t structure.
The widget doesn't copy data pointed to by the
PhImage_t members. */
PtSetArg( &arg[0], Pt_ARG_LABEL_TYPE, Pt_IMAGE, 0 );
PtSetArg( &arg[1], Pt_ARG_LABEL_IMAGE, img, 0 );
PtCreateWidget( PtLabel, window, 2, arg );
/* Free the PhImage_t structure (but not its contents). */
free( img );
PtRealizeWidget( window );
PtMainLoop();
return EXIT_SUCCESS;
}
void *memory_allocate( long nbytes, int type )
{
if( type == PX_IMAGE && UseShmem ) {
return( PgShmemCreate( nbytes, NULL ) );
}
else {
return( calloc( 1, nbytes ) );
}
}
void *memory_free( void *memory, int type )
{
if( type == PX_IMAGE && UseShmem ) {
PgShmemDestroy( memory );
}
else {
free( memory );
}
return NULL;
}
void *warning( char *msg )
{
printf( "%s\n", msg );
return NULL;
}
void *error( char *msg )
{
printf( "%s\n", msg );
PtExit( EXIT_FAILURE );
return NULL;
}
void *progress( int percent )
{
printf( "Load Status: %d.%d percent\n",
percent >> 16, percent & 0xffff );
return NULL;
}