GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Fri Aug 29, 2014 8:04 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: image is not visible
PostPosted: Sat Feb 18, 2012 11:32 pm 
Offline

Joined: Sat Feb 18, 2012 10:34 pm
Posts: 4
hi,

I am new with gtk and hope to get help here.

My application is a simulation in ADA which is calling some image processing functions written in C.
Within the C-functions images shall be displayed and lines plotted on the images. I read several tutorials but cannot get it run.
There is an initialization, the simulation loop processing sequences of images and a finalization.
In the simulation loop the images shall be displayed.

Initialization:
Code:
#define WIN_SIZE 512
static GtkWidget *gtkPlotWindow;

int InitPlotWindow()
{
    char*   args[] = { "" };
    int     argc   = 1;
    char**  argv   = args;

    gtk_init( &argc, &argv );
    gtkPlotWindow = gtk_window_new( GTK_WINDOW_TOPLEVEL );

    gtk_widget_set_size_request( gtkPlotWindow, WIN_SIZE, WIN_SIZE);
    gtk_window_set_title( (GtkWindow*)gtkPlotWindow,"Monitor" );
    gtk_widget_set_app_paintable(gtkPlotWindow, TRUE);
    gtk_widget_show_all( gtkPlotWindow );

  return 0;
};


For display of images and lines in the simulation loop there are two C-functions.
PlotBitMap() is called first then PlotLine():
Code:
int PlotBitmap( unsigned short *img)
{  /*img is pointing to an array of gray values*/
    static unsigned char im[ WIN_SIZE][ WIN_SIZE][ 4 ];
    ...
    int              w, h;
    cairo_surface_t *image;
    ...
    /* here the image of gray values, img, is converted to an RGB24-image, im */
   ...
   
   /*with the following code, here commented out, the images are displayed
      but the lines are not visible */
//     gdk_draw_rgb_image(gtkPlotWindow->window,
//                        gtkPlotWindow->style->fg_gc[GTK_STATE_NORMAL],
//                        4,4, /* x , y */
//                        WIN_SIZE,WIN_SIZE ,/* width,height */
//                        GDK_RGB_DITHER_NORMAL,
//                        (guchar*)im,
//                        WIN_SIZE*3);

  /* using cairo for display of the images: */
      image = cairo_image_surface_create_for_data((guchar*)im,CAIRO_FORMAT_RGB24,512,512,512);
  cairo_t* cairoWindowHandler = gdk_cairo_create ( gtkPlotWindow->window );

  cairo_set_source_surface (cairoWindowHandler, image, 0, 0);
  cairo_paint (cairoWindowHandler);

  cairo_surface_destroy (image);
  cairo_destroy( cairoWindowHandler );

return 0;
};

Code:
int PlotLine( int x1, int y1, int x2, int y2, int color )
{
  cairo_t* cairoWindowHandler = gdk_cairo_create ( gtkPlotWindow->window );

  if( color == 1 )
  {
    cairo_set_source_rgb( cairoWindowHandler, 1, 0, 0 );
  }
  else if( color == 2 )
  ...

  cairo_set_line_width ( cairoWindowHandler, 1.0 );
  cairo_move_to ( cairoWindowHandler, x1, y1 );
  cairo_line_to ( cairoWindowHandler , x2, y2 );
  cairo_stroke ( cairoWindowHandler );

  cairo_destroy( cairoWindowHandler );

  return 0;
}


With the code shown above nothing at all is displayed.
Replacing within cairo_image_surface_create_for_data() the image format CAIRO_FORMAT_RGB24 by CAIRO_FORMAT_A8 shows a gray window and the plotted lines.

Any help is welcome

gtkNiko

_________________
gtkNiko


Top
 Profile  
 
 Post subject: Re: image is not visible
PostPosted: Sun Feb 19, 2012 12:09 pm 
Offline
Never Seen the Sunlight

Joined: Thu Mar 24, 2011 2:10 pm
Posts: 328
Location: Sydney, Australia
I'm guesing if it works for displaying an 8bit image and not a 32bit image (i.e. 24+8 unused bits) then it is because your stride 512 bytes is not right. It should be width x 4bytes = 2096, though you may want to make use of cairo_format_stride_for_width() to figure it out in a safer manner.
Not too sure why your not getting a display at all though.
I'm used to there being a gtk_main call to run gtk but if you can get an alpha image than I guess that must be fine.
My guess is that it has something to do with the way the image is formatted but without knowledge of how it was generatedI cn't really tell.


Top
 Profile  
 
 Post subject: Re: image is not visible
PostPosted: Sun Feb 19, 2012 1:33 pm 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hello.

You can only draw on widgets from within GtkWidget::expose-event signal handler (or GtkWidget::draw signal handler in GTK+-3.x). You should probably draw your stuff onto some temporary surface (something like cairo_image_surface), invalidate widget and blit your image onto widget from expose event handler.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: image is not visible
PostPosted: Sun Feb 19, 2012 8:02 pm 
Offline

Joined: Sat Feb 18, 2012 10:34 pm
Posts: 4
hi,

anyhow my reply with quote does not appear in the forum. So I am repeating the reply.

Quote:
You can only draw on widgets from within GtkWidget::expose-event signal handler ...

I know, the normal way of using GTK is to start the event loop with gtk_main() called within the main() program. In my application the main program is in ADA and I don't know a GTK version for ADA. Even if it exists I guess it would not work because I have a mix of languages, ADA and C. By the way, I tried to call gtk_main() from within my C-functions and the whole program hangs up.

Principally there is a possibility to display "something" with my function PlotBitmap(), i.e. without the event signal handler:
- The images are displayed correctly with gdk_draw_rgb_image(). But then no lines are visible.
- Using CAIRO_FORMAT_A8 instead of CAIRO_FORMAT_RGB24 within cairo_image_surface_create_for_data()
displays a window shaded in gray together with lines. Of course the image is not the expected one because
CAIRO_FORMAT_A8 is the wrong format. But there is something displayed. And it is really updated because the
content varies from loop to loop.

My function PlotBitmap() is using the same cairo function calls as an event signal handler would do.
What is the difference calling gtk or cairo functions from an event signal handler or from a C-function?

Thank you for your tips

gtkNiko

_________________
gtkNiko


Top
 Profile  
 
 Post subject: Re: image is not visible
PostPosted: Mon Feb 20, 2012 8:50 am 
Offline
Never Seen the Sunlight

Joined: Wed Jul 23, 2008 10:31 am
Posts: 2406
Location: Slovenia
Hi.

It's not possible to create GUI application without main loop, since there is no way to have working signal system. And drawing onto widget from outside the expose-event signal handler just happens to work cca. 60% in your case. GTK+ doesn't expect user to be painting over widgets from outside the expose-event handlers and nothing is prepared for you at that moment. Switch GTK+ version, underlying OS or any other factor and produced results can be entirely different. Also, see what happens if the displayed window is obscured by other windows and the uncovered again. Since there is no way of telling your app to update the display, you end up with one giant mess.

Proper way of doing what you need to do would be to spawn another thread/process and let that process handle drawing.

Cheers,
Tadej


Top
 Profile  
 
 Post subject: Re: image is not visible
PostPosted: Mon Feb 20, 2012 8:09 pm 
Offline

Joined: Sat Feb 18, 2012 10:34 pm
Posts: 4
hi,

thank you for your tip.
What you propose looks much more difficult than I expected.
I will think about it.

_________________
gtkNiko


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group