Basic Image display of 2D array - c

As part of a project I am doing in C, it would be helpful to produce a visual representation of the data I am working with.
My question is: for some dynamically declared array:
double *foo;
foo = (double *)malloc(sizeof(double)*x*y);
for (i=0;i<x;i++)
for (j=0;j<y;j++)
foo[i*y+j] = someValue;
If the data has some max/min value (lets say max = 100.0 and min = 5.0), what is the simplest way to represent this data graphically?
I would like some method of displaying each number as a gradient of some color, for example, the max would be black and the min would be white.
An output of a Bitmap image for example would be a good as I need to do this for multiple arrays.
thanks.

Related

Finding the correct position of a scaled element (added diagram)

Very complicated for me to explain the problem, but I will try my best.
I am making a game. There is an area of game objects and a canvas that draws every object in that area using some "draw_from" function - void draw_from(const char *obj, int x, int y, double scale) so that it looks as if a copy of that area is made on-screen.
This gives the advantage of scaling that area using the scale parameter of the draw_from() function.
However, a problem occurs when doing so. For simplicity imagine there are just two actors in that area - one that is right above the other one.
When they are scaled-down, they will appear in different vertical positions, further from each other.
I need to calculate the new correct positions for each of the objects and pass them to draw_from, but I just seem to be unable to figure out how. What is the correct way to recalculate the new positions if each of those objects is scaled down with the same value?
Here is a decent illustration of the problem more or less:
As you can tell the draw_from function will draw the object centered on the x/y coordinates. To draw an object at 0:0 (top-left corner) you must do draw_from(obj, obj->width/2, obj->height/2, 1.0); Not sure if the scaling is implemented that way exactly, but I created a function to obtain the new width and height of the scaled object:
void character_draw_get_scaled_dimensions (Actor* srcActor, double scale, double* sWidth, double* sHeight)
{
double sCharacterWidth = 0;
double sCharacterHeight = 0;
if(srcActor->width >= srcActor->height)
{
sCharacterWidth = (double)srcActor->width * scale / 1.0;
sCharacterHeight = sCharacterWidth * (double)srcActor->height / (double)srcActor->width;
}
else
{
sCharacterHeight = (double)srcActor->height * scale / 1.0;
sCharacterWidth = sCharacterHeight * (double)srcActor->width / (double)srcActor->height;
}
if(sWidth)
(*sWidth) = sCharacterWidth;
if(sHeight)
(*sHeight) = sCharacterHeight;
}
In other words, I need to maintain the distances between those objects across down-scales and I explained how draw_from and /somehow/ how its scaling works.
I need the correct parameters to pass to the draw_from's x and y arguments.
From that point, I think it will get just too broad if I continue elaborating further.
Not the solution I hoped for, but it is still a solution.
The more hacky and less practical (including performance-wise) solution is to draw every object on an offscreen canvas with a scale of 1.0 then draw from that canvas to the main canvas at any scale desired.
That way only the canvas should be repositioned and not every object. It gets really easy from there. I still would prefer the conventional purposed mathematical solution.

Need help understanding a color-mapping algorithm

So I'm working on a project that involves taking pre-existing skeleton code of an application that simulates "fluid flow and visualization" and applying different visualization techniques on it.
The first step of the project is to apply different color-mapping techniques on three different data-sets which are as follows: fluid density (rho), fluid velocity magnitude (||v||) and the force field magnitude ||f||.
The skeleton code provided already has an example that I can study to be able to determine how best to design and implement different color-maps such as red-to-white or blue-to-yellow or what have you.
The snippet of code I'm trying to understand is the following:
//rainbow: Implements a color palette, mapping the scalar 'value' to a rainbow color RGB
void rainbow(float value,float* R,float* G,float* B)
{
const float dx=0.8;
if (value<0) value=0; if (value>1) value=1;
value = (6-2*dx)*value+dx;
*R = max(0.0,(3-fabs(value-4)-fabs(value-5))/2);
*G = max(0.0,(4-fabs(value-2)-fabs(value-4))/2);
*B = max(0.0,(3-fabs(value-1)-fabs(value-2))/2);
}
The float valuebeing passed by the first parameter is, as far as I can tell, the fluid density. I've determined this by studying these two snippets.
//set_colormap: Sets three different types of colormaps
void set_colormap(float vy)
{
float R,G,B;
if (scalar_col==COLOR_BLACKWHITE)
R = G = B = vy;
else if (scalar_col==COLOR_RAINBOW)
rainbow(vy,&R,&G,&B);
else if (scalar_col==COLOR_BANDS)
{
const int NLEVELS = 7;
vy *= NLEVELS; vy = (int)(vy); vy/= NLEVELS;
rainbow(vy,&R,&G,&B);
}
glColor3f(R,G,B);
}
and
set_colormap(rho[idx0]);
glVertex2f(px0, py0);
set_colormap(rho[idx1]);
glVertex2f(px1, py1);
set_colormap(rho[idx2]);
glVertex2f(px2, py2);
set_colormap(rho[idx0]);
glVertex2f(px0, py0);
set_colormap(rho[idx2]);
glVertex2f(px2, py2);
set_colormap(rho[idx3]);
glVertex2f(px3, py3);
With all of this said, could somebody please explain to me how the first method works?
Here's the output when the method is invoked by the user and matter is injected into the window by means of using the cursor:
Whereas otherwise it would look like this (gray-scale):
I suspect that this is a variation of HSV to RGB.
The idea is that you can map your fluid density (on a linear scale) to the Hue parameter of a color in HSV format. Saturation and Value can just maintain constant value of 1. Normally Hue starts and ends at red, so you also want to shift your Hue values into the [red, blue] range. This will give you a "heatmap" of colors in HSV format depending on the fluid density, which you then have to map to RGB in the shader.
Because some of your values can be kept constant and because you don't care about any of the intermediate results, the algorithm that transforms fluid density to RGB can be simplified to the snippet above.
I'm not sure which part of the function you don't understand, so let me explain this line by line:
void rainbow(float value,float* R,float* G,float* B){
}
This part is probably clear to you - the function takes in a single density/color value and outputs a rainbow color in rgb space.
const float dx=0.8;
Next, the constant dx is initialised. I'm not sure what the name "dx" stands for, but looks like it's later used to determine which part of the color spectrum is used.
if (value<0) value=0; if (value>1) value=1;
This clamps the input to a value between 0 and 1.
value = (6-2*dx)*value+dx;
This maps the input to a value between dx and 6-dx.
*R = max(0.0,(3-fabs(value-4)-fabs(value-5))/2);
This is probably the most complicated part. If value is smaller than 4, this simplifies to max(0.0,(2*value-6)/2) or max(0.0,value-3). This means that if value is less than 3, the red output will be 0, and if it is between 3 and 4, it will be value-3.
If value is between 4 and 5, this line instead simplifies to max(0.0,(3-(value-4)-(5-value))/2) which is equal to 1. So if value is between 4 and 5, the red output will be 1.
Lastly, if value is greater than 5, this line simplifies to max(0.0,(12-2*value)/2) or just 6-value.
So the output R is 1 when value is between 4 and 5, 0 when value is smaller than 3, and something in between otherwise. The calculations for the green and blue output or pretty much the same, just with tweaked value; green is brightest for values between 2 and 4, and blue is brightest for values between 1 and 2. This way the output forms a smooth rainbow color spectrum.

MATLAB to C-code

I am following MathWorks guide to converting MATLAB code to C-code.
The first step is to enter
%#codegen
after every function that I want converted to C-code, however doing so has given me the following prompt on the code below.
function lanes=find_lanes(B,h, stats)
% Find the regions that look like lanes
%#codegen
lanes = {};
l=0;
for k = 1:length(B)
metric = stats(k).MajorAxisLength/stats(k).MinorAxisLength;
%testlane(k);
%end
%function testlane(k)
coder.inline('never');
if metric > 5 & all(B{k}(:,1)>100)
l=l+1;
lanes(l,:)=B(k);
else
delete(h(k))
end
end
end
around the curly braces:
code generation only supports cell operations for "varargin" and
"varargout"
Another prompt says
Code generation does not support variable "lanes" size growth through indexing
where lanes is mentioned for the second time.
The input Arguments for the function are:
B - Is the output of the bwboundaries Image Processing toolbox function. It is a P-by-1 cell array, where P is the number of objects and holes. Each cell in the cell array contains a Q-by-2 matrix. Each row in the matrix contains the row and column coordinates of a boundary pixel. Q is the number of boundary pixels for the corresponding region.
h - plots the boundaries of the objects with a green outline while being a matrix of size 1 X length(B), holding the values of the boundaries like so like so:
h(K)=plot(boundary(:,2), boundary(:,1), 'g', 'LineWidth', 2);//boundary(:,1) - Y coordinate, boundary(:,2) - X coordinate.
stats - 19x1 struct array acquired using the regionprops function from the Image Processing toolbox with fields:
MajorAxisLength and
MinorAxisLength (of the object)
I would really appreciate any input you can give in helping me clear this error. Thanks in Advance!
Few points about your code generation -
Only a subset of functions in MATLAB and Image Processing Toolbox support code generation - Image Processing Toolbox support for code generation.
Cell arrays do not support code generation yet - Cell array support.
In your code, it seems like your variable is growing i.e. the initial size of the array is not able to support your workflow. You should follow code generation for variable sized inputs.
I had a similar error i.e. code generation does not support variable size growth through indexing. Inside my for loop I had a statement as such which had the same error:
y(i) = k;
I introduced a temporary storage variable u and modified my code to:
u = y;
u(i) = k;
y = u;
I suggest you do the same for your variable lanes.

How should a RPG tile-based map be represented?

I have a tile-based RPG system where a specific tile type is represented by a string (i.e. Grass = "g", Dirt = "d"). The problem is that I do not know how to represent a map (a group of tiles gathered in a specific order) in a way where each tile can be accessed by their x/y coordinates efficiently. Should the maps be represented in array format :
map[0].coords[x][y] = "g";
Or perhaps in some other way?
It depends on what language you are using, but a 2-dimensional array is usually an efficient way to do this.
Accessing elements in an array is usually quick because the position of a given element in memory can be calculated based on the array indexes provided, without having to iterate over other elements. Other data structures, (eg linked lists) are much slower for this type of retrieval.
A few things, dependant on the language:
1: If possible, set constant integers for terrain type. Constants use less memory and are quicker to referance/retrieve, same with integers over strings
2: A two dimensional would probably be the most efficant way of doing it.
An example
CONST(INT) GRASS = 1;
CONST(INT) DIRT = 2;
CONST(INT) SNOW = 3;
// assuming map is an array containing objects, and coords is a 2d
// array of said object:
map[0].coords[x,y] = GRASS;
A two dimensional array is fine.
You can also use a one-dimensional array. Here's a snippet of Java code I have lying around:
char [] cells = new char[WORLD_WIDTH * WORLD_HEIGHT];
public char get(int x, int y) {
return cells[x + y * WORLD_WIDTH];
}
public void set(int x, int y, char c) {
cells[x + y * WORLD_WIDTH] = c;
}
Suppose your world is 10 by 10 tiles, then the first row is in cells[0] to cells[9], the last row in cells[90] to cells[99], and so on.
Of course, you may want to add additional checks to ensure that the x and y parameters are valid.

RGB palette designated for ordered values

I have a function f(x,y), mostly monotonic, which produces some values in the range {0.0 .. 100.0}. I would like to draw them using different colors as a 2D picture, where (x,y) are coordinates and where distinctive colors stand for distinctive values of the function. The problem is following: I don't know how to map the values of this function to RGB color space preserving the order (visibly). I have found that smth. like:
R = f(x,y) * 10.0f;
G = f(x,y) * 20.0f;
B = f(x,y) * 30.0f;
color = B<<16|G<<8|R; //#low-endian
works fine, but the resulting picture is too dark. If I increase these constants, it makes things not better, because at some moment a color component will be greater than 0xFF, so it will overflow (one color component should be in the range {0 .. 0xFF}.
Do you have any idea how to map values from {0.0 .. 100.0} to
RGB=[{0 .. 0xFF}<<16|{0 .. 0xFF}<<8|{0 .. 0xFF}] so that the resulting RGB values are visibly Ok?
PS: maybe you know, where to find more info about related theory online? I remember only Comp.Graphics by Foley/Van Dam, but I don't have this book.
UPDATE: I am looking for how to generate a chroma palette like one on the right:
You could just try clamping the values to a maximum of 255 (0xff).
R = min((int)(f(x,y) * 10.0f), 0xff);
G = min((int)(f(x,y) * 20.0f), 0xff);
B = min((int)(f(x,y) * 30.0f), 0xff);
Edit: There are a lot of different ways to convert to colors automatically, but you might find that none of them generates the exact progression you're looking for. Since you already have a picture of an acceptable palette, one method would be to create a lookup table of 256 colors.
#define RGB(R,G,B) (B<<16|G<<8|R)
int palette[256] = { RGB(0,0,0), RGB(0,0,128), ... };
int greyscale = (int)(f(x,y) * 2.559999);
assert(greyscale >= 0 && greyscale <= 255);
int rgb = palette[greyscale];
If the lookup table is too much trouble, you could also break the greyscale range into different subranges and do a linear interpolation between the endpoints of each range.
int interpolate(int from, int to, double ratio)
{
return from + (int)((to - from) * ratio);
}
if (greyscale <= 48)
{
R = 0;
G = 0;
B = interpolate(0, 255, greyscale/48.0);
}
else if (greyscale <= 96)
{
R = 0;
G = interpolate(0, 255, (greyscale-48)/48.0);
B = interpolate(255, 0, (greyscale-48)/48.0);
}
else if ...
Actually you could use the YUV color model since it is kind of based on two coordinates, i.e. U and V.
This seems more appropriate for the task.
And YUV -> RGB conversion is pretty straightforward.
You can convert RGB to HSL and increase the brightness/contrast. You can find forumlas for the conversions and other useful info on this page: http://lodev.org/cgtutor/color.html
Use a different colour-space that means you can easily assign coordinates to different colours.
YUV or YCrCb might suit as the UV or CrCb dimensions could be treated as "at right-angles"
Or HSL/HSV if one of your dimensions wraps around like the hue does.
I believe you want a heatmap based on the ordered values. Then you have different types of heatmaps. Solution here: http://www.andrewnoske.com/wiki/Code_-_heatmaps_and_color_gradients

Resources