Topic: Accessing image buffer in Appgraphics

Hello

I would like to produce fast calculations of bitmap graphics, e.g. high resolution mandelbrot set (2500 x 2000 pixels).
I manage to do this by calling putpixel(px,py,creatergb(ih,ih,ih)) for each point. However, this is very slow bcause every putpixel is essentially a call to the windows GDI system.

Logically there must be a way to define an image buffer
type(imagetype):: iarr
...
call allocateimage(iarr, 2500, 2000)
....
manipulate, access iarr(x,y)
....
then
call pasteimage(1,1,iarr)

and finally

call freeimage(iarr)

How does one access the image buffer?

Regards,
Roland

Re: Accessing image buffer in Appgraphics

Roland,

AppGraphics does not support accessing the image buffer.  The Fortran imagetype derived type contains a member of type(c_ptr) that is actually a Windows HBITMAP handle.  While you could possibly access the bytes of an HBITMAP by converting it to a device-independent bitmap and using some additional Windows API calls to access the DIBSECTION, I wouldn't recommend it.  Additionally, accessing the bitmap handle isn't part of the AppGraphics API, so there's always the chance it would be broken in a future release.

You can get some mild speed improvement by moving to double-buffering, but it wouldn't be an order of magnitude faster.  It might only improve things by a factor of 2.  The putpixel call is expensive because it first looks up the current window and drawing context from arrays before actually drawing the single pixel.  After the pixel is drawn, it triggers a Windows invalidation for a rectangle around that single pixel.  I don't believe the bottleneck is the Windows GDI in this case.

A better image drawing routine or a more efficient pixel operation would be preferable, but they don't exist in AppGraphics right now.

Jeff Armstrong
Approximatrix, LLC

Re: Accessing image buffer in Appgraphics

Just vague idea: create your image using Cairo library, directly from Simply Fortran (see topic below: https://forums.approximatrix.com/viewtopic.php?id=781), and then display it in AppGraphics window.
Unfortunately, AppGraphics does not support direct import of png files...

Re: Accessing image buffer in Appgraphics

Hello Jeff and Carlos
Having read your answeres and scanned through the Forum I tried DISLIN. Little search reveiles that DISLIN comes with a good example for Mandelbrot set calculation, including a simple GUI with zoom-functions, progress bar etc. Keeping in mind that the goal is to do fast calculation of large bitmaps (>2000 * 2000 pixels) and display the bitmaps, I increased the mandelbrot matrix from 600 * 600 pixels to 2000 * 2000 pixels. The progress bar shows that calculation of the mandelbrot set indeed is very fast. However, the display of the image takes considerable time. So I think that DISLIN stores an intermediate image file on disk and then loads the image file for display.
I did a lot of programming with Borland Delphi and Lazarus / Freepascal on Windows, also using BGI. As drawing of bitmaps within BGI is possible, I have difficulties to understand why this should not be possible within the AppGraphix framework. See e.g. the definition of "putimage" in BGI:
void putimage (int left, int top, void *bitmap, int op);
I have no experience with GTK+ / Cairo and presently can't figure out how to deal with that. From the link Carlos provided I don't  understand how to do this.
What about using openGL or Cimg (Cimg.eu)? Any hints or proposals?
Roland

Re: Accessing image buffer in Appgraphics

Just to complete this discussion: Playing with GTK-Fortran examples I realized that within the frame of GTK+ bitmaps can be handeled by buffers. One of the examples is the mandelbrot-pixbuf.f90 and demonstrates exactly the solution to my question.
Regards, Roland

Re: Accessing image buffer in Appgraphics

Roland,

GTK would certainly provide the buffers you're looking to work with.  AppGraphics is just limited in order to keep things simple.  I've been looking a bit more into how you could actually read and write numeric values to an AppGraphics bitmap, but I haven't come up with a simple solution.

Jeff Armstrong
Approximatrix, LLC

Re: Accessing image buffer in Appgraphics

High Jeff,
thanks for your effort! I played a lot with GTK-Fortran library and it works rather good.
there are, however, two concerns:
1. Its only possible to use GTK+32bit in the compiler options.
2. Only small memory model is possible. With the examples I am playing with, this is probably no problem. But I would like to add Fourier transforms to the image; this might lead to memory problems I fear.
Regards, Roland

Re: Accessing image buffer in Appgraphics

Roland,

Our GTK+ packages are only 32-bit at this time.  When these packages were originally assembled, that was the only "stable" version of GTK+ for Windows.  I can revisit this, though.  With 32-bit on Windows, you would be limited to about 1.5GB of memory allocation, which is still substantial.

Changing the memory model is only useful for increasing the amount of statically allocated memory the program can use.  By statically allocated, I am referring mostly to arrays who's sizes are pre-determined.  If you use Fortran's ALLOCATE statement, though, you can vastly exceed the limits imposed by a different memory model. 

Just as a reference, using the medium memory model, even with a 64-bit program, will still limit you to 2GB of statically declared memory.

Jeff Armstrong
Approximatrix, LLC