Solved

Plotting pixels in Windows using Visual C++

Posted on 1998-12-09
7
807 Views
Last Modified: 2013-12-14
 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
Comment
Question by:fumita
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1179757
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
 
LVL 22

Expert Comment

by:nietod
ID: 1179758
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
 
LVL 8

Expert Comment

by:Answers2000
ID: 1179759
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
ID: 1179760
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
 
LVL 1

Accepted Solution

by:
Hans_Klose earned 100 total points
ID: 1179761
>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
 

Author Comment

by:fumita
ID: 1179762
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
 
LVL 22

Expert Comment

by:nietod
ID: 1179763
Can the array dimensions be fixed, or do you need them to vary at run-time?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now