PDA

View Full Version : Reading of ascii-file to QPixMap



fluxar
5th October 2007, 13:17
Hi, I have a set of textfiles (ascii) that contains values. Each value represents a pixel that I somehow have to draw to screen, and each file contains thousands of lines which together represents an image. An example of a file:


x y value
...
0.35040E+01 0.29280E+01 0.30385E+01
0.35040E+01 0.29760E+01 0.31362E+01
0.35040E+01 0.30240E+01 0.32857E+01
//empty line that separates x-values
0.35520E+01 0.27840E+01 0.31625E+01
0.35520E+01 0.28320E+01 0.31206E+01
0.35520E+01 0.28800E+01 0.30607E+01
...

My question is, what would be the best approach to read a file of this type into a QPixMap (which then will be drawn to a QGraphicsView)? Or, should i preferably use something else than QPixMap?

The x/y-values aren`t that important, the main point is that each value corresponds to a pixel. I have thought of reading them into an array, but I am quite uncertain of how I should use this array further.

jacek
5th October 2007, 15:57
If that's an image, then you should use QImage, which allows you to change particular pixels. You can use QTextStream to read data.

fluxar
23rd October 2007, 14:34
I`ve managed to display the data(arrays of floats 100x100) as images(QImage) now, but I have some trouble with applying colors to the images in an appropriate way. Basically, each float corresponds to a color(pixel) in an image, so I have to map each float to a color and then apply this color to a specific pixel.

I was thinking about using QLinearGradient as a lookup-table for colors, but it doesnt look like it suits my needs. Maybe I need to make my own, but I want to if anyone has any input before I do so.

What I need is:
A linear-gradient/lookup-table where I can add colors (between range A to B) and then get the right color by writing something like QRgb color = gradient.getColor(floatdata[x][y]).

So what do you think, do I have to write my own gradient class, or is there some other approach I could use?

jacek
23rd October 2007, 23:06
The linear gradient doesn't do any special magic. You only need this formula:

x = s*x_a + (1-s)*x_b

and apply it to every colour component. For s = 0 you'll get colour "a" and for s = 1 --- "b". Although such tranformation might not be linear (for example if you change s from 0.2 to 0.3 the perceptual change of colour might be different than the one when you change s from 0.7 to 0.8), but you can avoid this by using an uniform colour space.

If you want a colour scale that goes from red to green, you can try this:

qreal redHue = QColor( Qt::red ).hueF();
qreal greenHue = QColor( Qt::green ).hueF();
pixelColour = QColor::fromHsvF( redHue + (value / maxValue) * (greenHue - redHue), 1.0, 1.0 );

Also take a look at Qwt's Spectrogram plots (http://qwt.sourceforge.net/spectrogramscreenshots.html).

fluxar
15th November 2007, 14:31
Thanks, Qwt looks interesting and I might use it at later point. A spectrogram plot is basically what I have made now, but mine version is "lighter".

I have a lot of these plots, each represented as a QGraphicsItem. These items are added to a scene.

My problem now is that I need to click at some position within an item and display the value at this point for each of the items. So, for example if I click at the position (1,1) (in item coordinates), the value at this point is displayed at the bottom of the item. Then I somehow have to notify all the other items about the "clicked-position" (in item coordinates, since they all have the same size) so that they also can display their value at this specific point.

I have managed to make this work only for a single item, (only the value within a single item is displayed). I did this by reimplementing the item's mousePressedEvent(...).

My question is:
If I click at an item, what is the best approach to send the clicked-position (in itemcoordinates) to all of the items in the scene?
I have thought of reimplementing the scene's mousePressedEvent(..), but I'm not sure if it will work.