Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 841
  • Last Modified:

Plotting pixels in Windows using Visual C++

 I'm writing a data acquisition program which takes an array of [512][512] data values for an image. One problem I'm having programming this in Visual C++ 6 on an NT platform is memory allocation for an array that large. The bigger problem I'm having is figuring out how to code a window that will display the 512x512 image and update it as fast as possible with realtime data from the data acquisition board. I could use help on allocating the array, plotting the pixels individually, or general tips on the best way to display each image and update it as fast as possible using an array or the individual pixels, either in a console or mfc application. Thanks for the help!
0
fumita
Asked:
fumita
1 Solution
 
nietodCommented:
Unless the data type is large, You should be able to allocate the array using new with no problem at all.  Assuming you are storing ints the array would be only about a meg.  No big deal.  For doubles it would be 2 meg, still no problem

Have you tried using new?  if so what problem did you have?
0
 
nietodCommented:
In what way is the image updated?  i.e how does it change with time?  does it scroll (like a storage scope, for example) or does the entire image change (like say a spectral graph).

Have you been able to handle the real-time data aquistion part?  That is the tough part.  If you have handled that, then displaying it won't be too bad (but you may have to be satisfied with updates that lag some.)
0
 
Answers2000Commented:
I would suggest storing the array as a DIB (basically a block of memory with a handle to it, there's some MFC examples at www.codeguru.com). DIB = Device Independant Bitmap

The actual size to store this will depend on how many colors you need.
e.g.
Monochrome = 512 X 512 X 1 bits = 512 X 512 X 1  / 8 byes
16 color (4bits per pixel) = 512 X 512 X 4 bits = 512 X 512 X 4 / 8 bytes
etc.

The advantage of using a bitmap (DIB or DDB) is that you can throw whole sets of pixels to the screen using BitBlt

The advantage of using a DIB (as opposed to DDB) is that you can access the bitmap as a raw block of memory.

Lookup CreateDIBSection  API call.

Do you want specifics on DIBs or has codeguru the examples you need ?

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
nietodCommented:
That is probably the way to go, but maybe not.  For example, if speed is of the essence and if the plot changes only slightly with time (like if it scrolls) it may be better to work with the image right on the screen.  This is likely to give faster updates at times, although you may have to accept some limitations (like the fact that you might "loose"  parts of the image that are covered by a window or if part is uncovered by a window, it may take it longer to update it, that sort of thing.)
0
 
Hans_KloseCommented:
>One problem I'm having programming this in Visual C++ 6 on an >NT platform is memory allocation for an array that large.
 
The Array for 512x512 256 color array or bit map is only about a .25 Mb so with the flat memory model that is nothing.

 
>The bigger problem I'm having is figuring out how to code a >window that will display the 512x512 image and update it as >fast as possible with realtime data from the data acquisition >board.
Here you have  number of chices, you can for example
1) use SetPixelV of the CDC class.First set a rectangle in your client area for 512x512 for the display, then create your array of bytes, also create a 256 array of COLORREF, then just set the
dc.SetPixelV(indexX, indexY, arra[value_value_from_other_array))
(Do this if only few values change)
2) create two bitmaps, one is the visible surface and the other is created with CreateCompatibleBitmap(...) and is the buffer. Update the buffer then BitBlt(...) it to the visible surface.
(but do this only if many bits change in between the updates).
3) If you absolutly must have speed you can create a use DirectX to create a DirectDraw surface which memory you can update directly.(Lots of work but http://www.vcdj.com/vcdj/default.asp has a good tutorial)

>general tips on the best way to display each image and update >it as fast as possible using an array or the individual pixels, >either in a console or mfc application.
If your data is stored in some kind of a big class or struct don't store that directly, just store the array of bytes and convert the valure you want to plot.
0
 
fumitaAuthor Commented:
Ok, thanks for the ideas.
When it comes to allocating the array with new, i've tried several methods, some of which have passed the compiling stage, but cause an exception due to a memory access violation when i try running the program. If I wanted to allocate an array of integers, size 512x512, what would be the best way to do that in C++? Using new, or a loop of mallocs? Could anyone give me a code example?

For the image updating, it will be taking real-time data from a micro-camera, so the image will be updated as an entire object, probabbly not pixel by pixel. thanks for the ideas with the bitmaps, i'm not too familiar with working with those, so i'll try and brush up on them and give it a shot.
0
 
nietodCommented:
Can the array dimensions be fixed, or do you need them to vary at run-time?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now