I’ve been struggling with this challenge of converting a PImage to a byte array. I cannot get it to work. I tried all of the above etc.
My intention is to send my byte array, which is a conversion of an image I created in my Processing application, to an external MCU connected to the serial port of my computer so that this image can be displayed on a e-Paper or e-Ink device.
The e-Paper display only shows black and white images, so I am not grappling with RGB or transparency.
To test my image, I have saved the image as a JPEG and then I used this utility https://lvgl.io/tools/imageconverter to convert the JPEG as a byte array using the option “Indexed 2 colors”.
This utility works well and the generated byte array is used by the MCU to display the image.
This webpage uses a PHP script and this code is opensourced on GitHub: lv_utils/img_conv_core.php at master · lvgl/lv_utils · GitHub
The PHP code looks more complicated than it really is, as it covers all options from raw, true color and multiple alpha and indexed options. I am simply using the indexed 2 color option, which is referenced as “const CF_INDEXED_1_BIT = 8;” in the PHP script. The script also includes dithering conversion options which are not needed either.
I now want to avoid saving my Processing create image as a JPEG and simply save the pixels as a byte array.
However, I cannot get my bytes to match up with the bytes generated by the webpage.
For starters, I have an image which is 128 pixels wide and 296 pixels high. The byte array generated by the PHP script is 16 x 296. So 8 pixels is converted into one byte.
Hence, I am stuck as using get(x,y) or referencing pixels[ i ] does not work.
So I had hoped that pixelDensity work here… as according to documentation " When the pixel density is set to more than 1, it changes all of the pixel operations including the way get() , set() , blend() , copy() , and updatePixels() all work."
But this is display or monitor dependent and I got an error message. Also you cannot use anything higher than 2.
Maybe there is a simple way to use the PHP script… it uses a class structure, which is a good start, but actually there is only one function and one private function that matters, so it is relatively straightforward for the experts out there.
I got stuck on the bit manipulation section as could not figure out the java/wiring equivalent:
$w = $this->w >> 3;
if($this->w & 0x07) $w++;
$p = $w * $y + ($x >> 3) + 8; /* +8 for the palette*/
if(!isset($this->d_out[$p])) $this->d_out[$p] = 0; /*Clear the bits first*/
$this->d_out[$p] |= ($c & 0x1) << (7 - ($x & 0x7));
Any suggestions are all greatly appreciated thanks.