image2cpp icon indicating copy to clipboard operation
image2cpp copied to clipboard

Image Magic usage

Open ironlungx opened this issue 1 year ago • 3 comments

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!

ironlungx avatar Aug 29 '24 21:08 ironlungx

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 avatar Aug 30 '24 11:08 javl

Hi @ironlungx, did this help solve your problem?

javl avatar Sep 06 '24 11:09 javl

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

ironlungx avatar Sep 06 '24 14:09 ironlungx