cedricve / raspicam

AVA RaspiCam: C++ API for using Raspberry camera with/without OpenCv
320 stars 138 forks source link

RGBA capture support #48

Open bpacholek opened 6 years ago

bpacholek commented 6 years ago

Adds support for the RGBA encoding. Useful for usage together with the Broadcom VideoCore JPEG Encoder (brcmjpeg) from RPi Userland.

For example together with this lib: https://github.com/ideaconnect/raspijpeg/blob/master/raspijpeg.cpp

For example:

int main()
{
    raspicam::RaspiCam Camera; //Camera object
    Camera.setWidth(1920);
    Camera.setHeight(1080);
    Camera.setCaptureSize(1920, 1080);
    Camera.setFormat(raspicam::RASPICAM_FORMAT_RGBA);
    Camera.open();
    //waiting 3 secs for camera to settle
    sleep(3);
    //capture
    int bufsize = Camera.getImageTypeSize(Camera.getFormat());
    unsigned char *data = new unsigned char[bufsize];
    Camera.grab();
    Camera.retrieve(data); 

  //here some interaction with the data: check, amendments: easier to do with RGBA than YUV

    raspijpeg::jpegdata returndata = raspijpeg::encode(data, bufsize, 1920, 1080, 90, PIXEL_FORMAT_RGBA);
    for (int i = 0; i < returndata.length; i++) {
        cout << returndata.data[i];
    }
}

Without RGBA support it would be needed to add the alpha channel to the RGB array using for example:

void addalpha(unsigned char* rgba, unsigned char* rgb, const int count) {
    for(int i = 0, rgbai = 0 ;i < count; i=i+3, rgbai = rgbai+4) {
        rgba[rgbai] = rgb[i];
        rgba[rgbai+1] = rgb[i+1];
        rgba[rgbai+2] = rgb[i+2];
        rgba[rgbai+3] = 0;
    }
}