Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image Magic usage #69

Open
ironlungx opened this issue Aug 29, 2024 · 3 comments
Open

Image Magic usage #69

ironlungx opened this issue Aug 29, 2024 · 3 comments

Comments

@ironlungx
Copy link

Hi, I've been a long time user of image2cpp and am considering sponsoring it. I am making a script that is similar to your website, it uses image magick to convert images to c arrays. The only thing missing is the 'swap' feature. It would be great if you could point me in the right direction on how to implement it.

p.s. sorry if the issue is irrelevant, i felt that asking you would give me the best answer.

Thanks for taking a look!

@javl
Copy link
Owner

javl commented Aug 30, 2024

Hi @ironlungx,

A commandline tool would be great! I've had plans for making one, but never really started. Feel free to use the same name (if you want), and we'll link to it from the online tool.

Regarding the swap function: some displays expect the bits in a reverse order (so 00001101 instead of 10110000) but want the byte order (groups of 8 bits) to stay the same. So the swap option reverses the contents of each byte, without reversing the order of the bytes themselves.

For example:

// This sequence of two bytes:
10000000 10110000
// becomes (note the byte order staying the same)
00000001 00001101

In the Javascript (here) it looks like this:

function bitswap(b) {
  if (settings.bitswap) {
    b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
    b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
    b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
  }
  return b;
}

This takes a decimal value, reverses its bits and returns the new (decimal) value. I'm sure you could do this in other ways as well, but bitwise operators are fast and this way both the input and output are a decimal value (no conversion to a string or something similar needed).

For clarity, this is what each of the b = ... lines does: (the x means swap).

// in: 
10110000 // 176
// first bitwise operation, swapping the first and second blocks of four bits
[1011]x[0000] => [0000] [1011]
// second bitwise operation, swapping the first two and last two groups of 2 bits
[00]x[00] [10]x[11] => [00][00] [11][10]
// third bitwise operation, swapping every bit with its neighbor:
[0]x[0] [0]x[0] [1]x[1] [1]x[0] => [0] [0] [0] [0] [1] [1] [0] [1]
// the result is the reverse, from 10110000 to 00001101

@javl
Copy link
Owner

javl commented Sep 6, 2024

Hi @ironlungx, did this help solve your problem?

@ironlungx
Copy link
Author

hi yes it did help! I'm working on it rn 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants