How to reverse an array in groups in C - c

I'm trying to do and make a software driver for one old device and i added bitmap support to it successfully. The problem is that all bitmaps are vertically flipped and I need somehow to flip them around.
My array has 230400 elements(for the resolution 320x240 as 320*3 colors is 960 elements per one y pixel, so 230400 in total)
I tried this one from Reversing arrays in C yesterday but today i realised that i need to switch every 960 elements from start to end to (i think) have it vertically flipped.
I also tried one array reverse guide on the internet but it reversed all elements and so colors changed and it flipped horizontally after sucessfully flipping vertically
For importing the 320x240 bitmap i used https://stackoverflow.com/a/9296467
,renamed data do BMPdata and added my piece of code to the B,G,R to R,G,B code
data[(i+y*320)] = BMPdata[i];
data[(i+y*320)+1] = BMPdata[i+1];
data[(i+y*320)+2] = BMPdata[i+2];
This piece of code sets every pixel in red(first line), green(second), blue(third) to the needed value from BMPdata array.
So i expect it to flip vertically because the one used before was not working and the second one which reverses the whole array from 0-230400 to 230400-0 changed also colors(which is logical for me) but also flipped the image horizontally so i also can't use that.
I want it to have every 960 elements switch from start to end without changing anything in them, so in example:
i have array of 5760 elements
960-1920: Test of the second line.
1920-2880: Third test
2880-3840: 4th line of text
3840-4800: Nearly the last thing.
4800-5760: Last line.
I expect it to flip like this:
0-960: Last line.
960-1920: Nearly the last thing.
1920-2880: 4th line of text
2880-3840: Third test
3840-4800: Test of the second line.
4800-5760: This is a test of the first line.
But it looks like it flipped like this with the reverse array thing:
0-960: .enil tsaL
960-1920: .gniht tsal eht ylraeN
1920-2880: txet fo enil ht4
2880-3840: tset drihT
3840-4800: .enil dnoces eht fo tseT
4800-5760: .enil tsrif eht fo tset a si sihT

There are several options. Just two of them are for example:
Instead of a 1-D byte array use a 2-D byte array with Y in the "outer" dimension and (X * colours) in the "inner" dimension. Use memcpy() to copy a whole pixel row without changing the sequence of bytes in it.
Define structs for pixel and row and define the bitmap as 1-D array of rows. You can copy a struct value (one row) by a simple assignment which will be compiled into a memcpy() behind the scene.
The beginner's way could also be to use two nested loops, one for Y and one for (X * colours). The Y loop will flip the bitmap while the X loop will keep the sequence.
I would go for the struct as it is more readable and implements what you mean. And you don't want to work on bytes, you want to work on pixels and rows.

Related

How do I replace an array element in LabView? (2d array of pictures)

so I have a final project for a class where I need to make a video game in LabView. The issue I'm having at the moment is that I can't figure out the 'right' way to put 'yourShip.png' into the 2d array of 2d pictures at [0,0]. Every tutorial I can find basically has exactly what I have down below in the screenshot, and it makes sense to me. However, running the program quickly shows that it does nothing.
To describe the code, I have a path constant that leads to the picture, which feeds to a draw flattened pixelmap function. Up to this point I know the code works, since creating a test indicator reveals as such. However, next I try to use the replace array subset function to replace the (default blank) 2d picture at [0,0] with yourShip.png. 'screen' is a 5x5 2d array of 2d pictures. The local variable of the same name being outputted to is indeed the very same array.
My main guess with why my code doesn't work is because of the way I'm taking screen as the input variable and then outputting to it via a local variable. However, if this is wrong, I'm confused with how I should do it. All I want to do is 'spawn' the image at the correct index.
The replace array subset works quite literally, i.e. it can only replace existing elements.
If there is no element at the specified index because the array is smaller, the function will do just nothing.
I guess your array is empty, so, initialize your screen array first to a size of at least 1x1.

glDrawArrays with multiple triangles in a single array with a persistent first and count

I have a bunch of polygon data that I want to draw. I pulled out that drawing code so that it currently looks like this
for (int Index = 0; Index < Count; Index++) {
glDrawArrays(GL_TRIANGLE_FAN, Index * 4, 4);
}
I have a giant one dimensional array filled with multiple triangles, a new triangle every 4 vertices. Are there any OpenGL functions that I can replace this entire loop with so that I don't have the overhead of each glDrawArrays call? glMultiDrawArrays is sort of what I want, except I don't need a different first (well, I need a different first, but multiplied by a constant) and count each time.
I thought something like glVertexAttribDivisor would work, but nothing was drawing after I added it, I'm not completely sure if it works without instancing either.
EDIT: I am using a triangle fan, and I am buffering the vertices every frame.
glDrawElements() + GL_PRIMITIVE_RESTART & glPrimitiveRestartIndex().

Strange behavior with Matlab array

I am having some trouble manually creating a histogram of intensity values from a grayscale image. Below is the code that I am using the create the bins for the plot that I want to create. The code works fine for every bin except for the last two. For some reason if the intensity is 254 or 255 it puts both values into the 254 bin and no values are accumulated in the 255 bin.
bins= zeros(1,256);
[x,y]=size(grayImg);
for i = 1:x
for j = 1:y
current = grayImg(i,j);
bins(current+1) = bins(current+1) + 1;
end
end
plot(bins);
I do not understand why this behavior is happening. I have printed out the count of 254 intensities and 255 intensities and they are both correct. However, when using the above code to accumulate the intensity values it does not work correctly.
Edit: Added the image I am using, the incorrect graph(the one I get with above code), and the correct one
A. The first problem with your code is the initial definition of bins. It seems that you come from C or somthing like that, but the definition should be- bins=zeros(1,256);
B. The second point is that you don't need the nested loop, you have a matlab function especially for that:
bins=hist(grayImg(:),1:256); % now, you don't need the pre-definition for 'bins'.
plot(bins);
C. Try to use functions like bar or imhist or hist(grayImg(:)), it may save you all this, and give a nice plot.

Storing realcolor images in an array for MATLAB

I am working on an m file that would take out single frames from a bigger image and play them as an animation. So far I managed to create the algorithm to locate and crop individual frames.
I can also store them in cell arrays. Almost everything is already done really.
My problem is that I can't get them to animate. I used the animation functions but they do not work. The reason being is that they are in cell arrays instead of just 4D arrays.
I want to store each frame in a nXmX3X(frame_number) array. How can I do that? How can I replace only the nXm part of an array?
Thank you.
if you have a cell array cFrames with n cells each storing the k-th frame of size m-by-n-by-3, you can use cat to create the desired 4D array
>> frames4d = cat(4, cFrames{:} );
Note: all frames in cFrames must have the same size for this to work.

Confused in how to print an array hellically

I have been trying to figure out how can we print a array hellically but i am stuck on how to get started.Any algorithms or ideas will be very helpful.Thanks
HELLICALLY means printing the array in concentric circular shape
If I'm interpreting what you're saying correctly, you want to print the contents of an array, but in a spiral.
I would start by allocating a big rectangular block of memory (a 2-D array) and initializing it to zero. This represents the screen. Then devise a function for determining the coordinates of the next point in the circle and make some coordinate variables initialized to the origin point. Fill the screen by dropping array members wherever they go.
Print out the screen-array, one row at a time, but substitute space for zero.
Screen size and next-coordinate-function are left as exercises for the reader.

Resources