bitbank2 / PNGenc

An embedded-friendly PNG encoder
Apache License 2.0
39 stars 8 forks source link

Arduino Portenta or Nicla-Vision frameBuffer to PNG #8

Closed hpssjellis closed 1 year ago

hpssjellis commented 2 years ago

Hi Larry

Both your PNG libraries seem to run fine on the Portenta and Nicla Vision. I don't need to save the data to SD card I just need to convert the Arduino camera FrameBuffer to PNG and then serial.print it to the monitor (or client.write it to a website).

I think I can do the conversion but if you have any suggestions, or problems you think I might have, I would be interested in hearing them.

defined at https://github.com/arduino/ArduinoCore-mbed/blob/master/libraries/Camera/src/camera.h

class FrameBuffer {
    private:
        int32_t _fb_size;
        uint8_t *_fb;
        bool _isAllocated;
    public:
        FrameBuffer(int32_t x, int32_t y, int32_t bpp);
        FrameBuffer(int32_t address);
        FrameBuffer();
        uint32_t getBufferSize();
        uint8_t* getBuffer();
        void setBuffer(uint8_t *buffer);
        bool hasFixedSize();
        bool isAllocated();
};

camera example at

https://github.com/arduino/ArduinoCore-mbed/blob/master/libraries/Camera/examples/CameraCaptureRawBytes/CameraCaptureRawBytes.ino

#include "camera.h"

#ifdef ARDUINO_NICLA_VISION
  #include "gc2145.h"
  GC2145 galaxyCore;
  Camera cam(galaxyCore);
  #define IMAGE_MODE CAMERA_RGB565
#else
  #include "himax.h"
  HM01B0 himax;
  Camera cam(himax);
  #define IMAGE_MODE CAMERA_GRAYSCALE
#endif

  if (cam.grabFrame(fb, 3000) == 0) {
    Serial.write(fb.getBuffer(), cam.frameSize());
  }
hpssjellis commented 2 years ago

A good starting point would be how to Serial.print() the encoded PNG?

bitbank2 commented 2 years ago

Are you asking how to output it as BASE64 or hex to the serial monitor?

hpssjellis commented 2 years ago

As far as I know a Base64 image to show on a webpage is just the raw data from a PNG presented in this form:

data:image/png;base64,R0lGODdhAQABAPAAAP8AAAAAACwAAAAAAQABAAACAkQBADs=

The above is a very small red dot.

For my code I am trying to write the PNG to the serial monitor in HEX format and then to a webpage using client.write(); in the format above so that the PNG can be shown on the webpage. I guess I could also save the PNG as a file on the SD card and display it on the webpage as a PNG, but I am trying to skip that step.

An extra problem is client.write() needs the entire set of data in one shot and not to loop through each digit as it is much too slow.

My testing (Portenta and Nicla-Vision) repository is here https://github.com/hpssjellis/my-examples-for-the-arduino-portentaH7/tree/master/research/camera-webserver-save

bitbank2 commented 2 years ago

I'm still not clear on what part you need help with. Your png encoding example looks ok, but you don't do anything with the output data. I would suggest writing the hex data to the http connection is reasonable sized chunks (e.g. 512 bytes at a time). See how that works. Are you unclear how to format the text to be acceptable for the web connection? You can use a keep-alive type of connection and write multiple times before closing it.