# uyvy/app.cpp

/*******************************************************************************
    Copyright (c) 2021-2022 Qualcomm Technologies, Inc.
    All rights reserved.
    
    Redistribution and use in source and binary forms, with or without
    modification, are permitted (subject to the limitations in the disclaimer
    below) provided that the following conditions are met:
    
    * Redistributions of source code must retain the above copyright notice, this
        list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
        this list of conditions and the following disclaimer in the documentation
        and/or other materials provided with the distribution.
    * Neither the name of Qualcomm Technologies, Inc. nor the names of its
        contributors may be used to endorse or promote products derived from this
        software without specific prior written permission.
    
    NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY
    THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
    CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
    NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
    OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
    OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
    ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    
    @brief
    Program to run FastADAS UYVY camera conversion pipeline(s).
    *******************************************************************************/
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <fadas.h>
    #include "util.h"
    
    #ifdef USE_OPENCV
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/calib3d.hpp>
    #include <opencv2/opencv.hpp>
    #include <opencv2/core/mat.hpp>
    #endif

    int main( int argc, char** argv )
    {
        int retVal = 0;
    
        if( FADAS_ERROR_NONE != FadasInit( nullptr ) )
        {
            UTIL_ERROR( "FAILED:  FadasInit" );
            return -1;
        }
    
        // Initialize images
        const uint64_t balign = 128;
        FadasImage_t uyvyImg = { 0 };
        FadasImage_t rgbImg = { 0 };
        FadasImage_t nrmImg = { 0 };
        FadasImage_t nrgbImg = { 0 };
    
        if( FadasCreateImage( FADAS_BUF_TYPE_IN, 1920, 1020, FADAS_IMAGE_FORMAT_UYVY,
                              &uyvyImg, balign ) != FADAS_ERROR_NONE )
        {
            UTIL_ERROR( "FadasCreateImage failed" );
            FadasDeInit();
            return -1;
        }
    
        if( FadasCreateImage( FADAS_BUF_TYPE_INOUT, 1280, 680, FADAS_IMAGE_FORMAT_RGB888,
                              &rgbImg, balign ) != FADAS_ERROR_NONE )
        {
            UTIL_ERROR( "FadasCreateImage failed" );
            FadasDestroyImage(&uyvyImg);
            FadasDeInit();
            return -1;
        }
    
        if( FadasCreateImage( FADAS_BUF_TYPE_OUT, 1280, 680, FADAS_IMAGE_FORMAT_RGB888, &nrmImg, balign ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "FadasCreateImage failed" );
    
        if( FadasCreateImage( FADAS_BUF_TYPE_OUT, 1280, 680, FADAS_IMAGE_FORMAT_RGB888, &nrgbImg, balign ) != FADAS_ERROR_NONE )
            UTIL_ERROR( "FadasCreateImage failed" );
    
        FadasNormlzParams_t normlz[3];
        normlz[0] = { 126.999f, 1.111f, 3.321f };
        normlz[1] = { 126.888f, 1.222f, 4.432f };
        normlz[2] = { 126.777f, 1.333f, 5.654f };

        // Let feature set ROI to entire image unless we only want to focus in on one ROI (e.g., bounding box)
        FadasROI_t roi = { 0 };

        // Initialize worker pools that would be used over-and-over in camera loop
    
        // UYVY to RGB color conversion coupled to bi-linear interpolated downscaling
        int32_t n_threads_affinity[4] = { 0, 1, 2, 3 };
        void* wrkrs = FadasCvtYUV_CreateWorkers( 4, n_threads_affinity, FADAS_CVTYUV_PIPELINE_DownscaleUYVYAndRGB888 );
        if( nullptr == wrkrs )
        {
            UTIL_ERROR( "Error creating worker pool\n" );
            FadasDestroyImage(&rgbImg);
            FadasDestroyImage(&uyvyImg);
            FadasDeInit();
            return -1;
        }
    
        // UYVY to RGB color conversion coupled to box interpolated downscaling and renormalization
        void* wrkrs_nrm = FadasCvtYUV_CreateWorkers( 1, n_threads_affinity, FADAS_CVTYUV_PIPELINE_DownscaleUYVYToRGB888AndNormi8 );
        if( nullptr == wrkrs_nrm )
            UTIL_ERROR( "Error creating worker pool\n" );

        // Begin infinite camera loop
        {
            // Copy image from file to represent camera
            // NOTE:  real cameras just need uyvyImg.image pointed to their buffer rather than copying bytes.
            UTIL_ReadRAW( "img1.uyvy", (2 * uyvyImg.props.width), uyvyImg.props.height, uyvyImg.props.stride[0], (uint8_t*)uyvyImg.plane[0] );

            // Downscale and convert to RGB
            if( FadasCvtYUV_RunMT( wrkrs, &uyvyImg, &rgbImg, &roi ) != FADAS_ERROR_NONE )
            {
                UTIL_ERROR( "Error in RunMT\n" );
                retVal = -1;
            }
    
            // normalize image (separately from pipeline)
            FadasCvtYUV_Renormalize888u8i8( (uint8_t*)rgbImg.plane[0], rgbImg.props, normlz[0], normlz[1], normlz[2], (int8_t*)nrmImg.plane[0], nrmImg.props.stride[0] );

            // Downscale, convert to RGB, and re-normalize (in one pipeline)
            if( FadasCvtYUV_RunMT( wrkrs_nrm, &uyvyImg, &nrgbImg, &roi, normlz ) != FADAS_ERROR_NONE )
                UTIL_ERROR( "Error in RunMT (norm)\n" );

            UTIL_WritePPM( "tmp_img1_cc.ppm", 1280, 680, 255, (uint8_t*)rgbImg.plane[0], rgbImg.props.stride[0] );
            UTIL_WritePPM( "tmp_img1_cc_nrm.ppm", 1280, 680, 255, (uint8_t*)nrmImg.plane[0], nrmImg.props.stride[0] );
            UTIL_WritePPM( "tmp_img1_cc_nrm2.ppm", 1280, 680, 255, (uint8_t*)nrgbImg.plane[0], nrgbImg.props.stride[0] );
    
    #ifdef USE_OPENCV
            cv::Mat oimg( rgbImg.props.height, rgbImg.props.width, CV_8UC3, rgbImg.plane[0], rgbImg.props.stride[0] );
            cv::Mat oimg_dsply( rgbImg.props.height, rgbImg.props.width, CV_8UC3 );
            cv::cvtColor( oimg, oimg_dsply, cv::COLOR_RGB2BGR );
            cv::namedWindow( "rgbImg", cv::WINDOW_AUTOSIZE );
            cv::imshow( "rgbImg", oimg_dsply );
    
            cv::Mat oimg_nrm( nrmImg.props.height, nrmImg.props.width, CV_8UC3, nrmImg.plane[0], nrmImg.props.stride[0] );
            cv::cvtColor( oimg_nrm, oimg_dsply, cv::COLOR_RGB2BGR );
            cv::namedWindow( "nrmImg", cv::WINDOW_AUTOSIZE );
            cv::imshow( "nrmImg", oimg_dsply );
    
            cv::Mat oimg_nrm2( nrgbImg.props.height, nrgbImg.props.width, CV_8UC3, nrgbImg.plane[0], nrgbImg.props.stride[0] );
            cv::cvtColor( oimg_nrm2, oimg_dsply, cv::COLOR_RGB2BGR );
            cv::namedWindow( "nrm2Img", cv::WINDOW_AUTOSIZE );
            cv::imshow( "nrm2Img", oimg_dsply );
    
            cv::waitKey( 0 );
    #endif
    
        }
    
        // Clean up thread pool
        if( FadasCvtYUV_DestroyWorkers( wrkrs ) != FADAS_ERROR_NONE )
        {
            UTIL_ERROR( "Error in FadasCvtYUV_DestroyWorkers\n" );
            retVal = -1;
        }
    
        if( FadasCvtYUV_DestroyWorkers( wrkrs_nrm ) != FADAS_ERROR_NONE )
        {
            UTIL_ERROR( "Error in FadasCvtYUV_DestroyWorkers\n" );
            retVal = -1;
        }
    
        // cleanup
        FadasDestroyImage( &uyvyImg );
        FadasDestroyImage( &rgbImg );
        FadasDestroyImage( &nrmImg );
        FadasDestroyImage( &nrgbImg );
        FadasDeInit();
    
        return retVal;
    }
    Copy to clipboard

Last Published: Sep 30, 2024

[Previous Topic
cqp/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/cqp.md) [Next Topic
uyuv\_nv12\_y/app.cpp](https://docs.qualcomm.com/bundle/publicresource/80-63309-1/topics/uyvy-nv12-y.md)