-1

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
Morc
  • 1
  • 2
  • Why flip them? BMP files, for example, let the origin be at the top left or the bottom left. You just need to know the order when you read the data. – stark Sep 01 '19 at 14:09

1 Answers1

0

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.

the busybee
  • 10,755
  • 3
  • 13
  • 30