Clean up write_buffer_to_img parameter
authorKwanghoon Son <k.son@samsung.com>
Tue, 9 Jul 2024 01:49:20 +0000 (10:49 +0900)
committerKwanghoon Son <k.son@samsung.com>
Fri, 26 Jul 2024 03:49:18 +0000 (12:49 +0900)
Change-Id: I8446491bd75033dea874c15e5769a3269eeaac40
Signed-off-by: Kwanghoon Son <k.son@samsung.com>
mv_barcode/barcode_generator/include/BarcodeGenerator.h
mv_barcode/barcode_generator/src/BarcodeGenerator.cpp
mv_barcode/barcode_generator/src/mv_barcode_generate.cpp

index 5118bd43c8dc40ce78f26fd724a14cda711f9741..7c551487402217585b579793e8965df7112b75d8 100644 (file)
@@ -53,10 +53,10 @@ struct BarcodeConfig {
        std::string bgcolor { "ffffff" };
        mv_barcode_generate_attr_shape_e data_shape { MV_BARCODE_GENERATE_ATTR_SHAPE_RECT };
        mv_barcode_generate_attr_shape_e finder_shape { MV_BARCODE_GENERATE_ATTR_SHAPE_RECT };
+       std::string logo_path {};
 };
 
-int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, mv_barcode_image_format_e image_format,
-                                               const int imageWidth, const int imageHeight, mv_engine_config_h engine_cfg);
+int write_buffer_to_img(zint_symbol *symbol, const BarcodeConfig &config);
 
 BarcodeImageFormat convertImageFormat(mv_barcode_image_format_e format);
 
index 054b38e10ca6dff47b003edcc4b439de6c2b0da8..0c0ee88845ed4db062d392833f3286d4cbcb676a 100644 (file)
@@ -307,75 +307,55 @@ static void __round_rect(cv::Mat &inOutImg, cv::Rect &rect, cv::Scalar &color)
        cv::circle(inOutImg, cv::Point(rect.x + 3 * gapW, rect.y + 3 * gapH), gapW, color, -1);
 }
 
-static int __draw_logo(cv::Mat &image, Common::EngineConfig *engineCfg)
+static int __draw_logo(cv::Mat &image, const BarcodeConfig &config)
 {
-       std::string logoPath;
-       int error = engineCfg->getStringAttribute(std::string(MV_BARCODE_GENERATE_ATTR_EMBED_IMG_PATH), &logoPath);
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("getStringAttribute failed error : %d", error);
-               return error;
-       }
-       std::string backgroundColor;
-       error = engineCfg->getStringAttribute(std::string(MV_BARCODE_GENERATE_ATTR_COLOR_BACK), &backgroundColor);
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("getStringAttribute failed error : %d", error);
-               return error;
-       }
-       int finderShape;
-       error = engineCfg->getIntegerAttribute(std::string(MV_BARCODE_GENERATE_ATTR_FINDER_SHAPE), &finderShape);
-       if (error != BARCODE_ERROR_NONE) {
-               LOGE("getIntegerAttribute failed error : %d", error);
-               return error;
-       }
+       auto logo = cv::imread(config.logo_path.c_str(), cv::IMREAD_COLOR);
 
-       if (!logoPath.empty()) {
-               auto logo = cv::imread(logoPath.c_str(), cv::IMREAD_COLOR);
-
-               int color_decimal = strtol(backgroundColor.c_str(), nullptr, 16);
-               int B = color_decimal % 256;
-               color_decimal /= 256;
-               int G = color_decimal % 256;
-               color_decimal /= 256;
-               int R = color_decimal;
-
-               auto color_frame = cv::Scalar(B, G, R);
-               auto color_black = cv::Scalar(255, 255, 255);
-
-               auto logoSize = image.size() / 5;
-               auto logoFrameSize = logoSize + logoSize / 10;
-
-               cv::resize(logo, logo, logoSize);
-               cv::Mat maskMat = cv::Mat::zeros(image.size(), image.type());
-               cv::Mat logoMat = cv::Mat::zeros(image.size(), image.type());
-               auto logoRect = cv::Rect((image.size() - logoSize) / 2, logoSize);
-               auto logoFrameRect = cv::Rect((image.size() - logoFrameSize) / 2, logoFrameSize);
-               logo.copyTo(logoMat(logoRect));
-
-               switch (finderShape) {
-               case MV_BARCODE_GENERATE_ATTR_SHAPE_ROUND_RECT:
-                       __round_rect(maskMat, logoRect, color_black);
-                       __round_rect(image, logoFrameRect, color_frame);
-                       break;
-               case MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE:
-                       cv::circle(maskMat, image.size() / 2, logoSize.width / 2, color_black, -1);
-                       cv::circle(image, image.size() / 2, logoFrameSize.width / 2, color_frame, -1);
-                       break;
-
-               default:
-                       cv::rectangle(maskMat, logoRect, color_black, -1);
-                       cv::rectangle(image, logoFrameRect, color_frame, -1);
-                       break;
-               }
-               cv::multiply(cv::Scalar::all(1.0) - maskMat, image, image);
-               cv::multiply(maskMat / 255, logoMat, logoMat);
-               cv::add(logoMat, image, image);
+       int color_decimal = strtol(config.bgcolor.c_str(), nullptr, 16);
+       int R = color_decimal % 256;
+       color_decimal /= 256;
+       int G = color_decimal % 256;
+       color_decimal /= 256;
+       int B = color_decimal;
+
+       auto color_frame = cv::Scalar(B, G, R);
+       auto color_white = cv::Scalar(255, 255, 255);
+
+       auto logoSize = image.size() / 5;
+       auto logoFrameSize = logoSize + logoSize / 10;
+
+       cv::resize(logo, logo, logoSize);
+       cv::Mat maskMat = cv::Mat::zeros(image.size(), image.type());
+       cv::Mat logoMat = cv::Mat::zeros(image.size(), image.type());
+       auto logoRect = cv::Rect((image.size() - logoSize) / 2, logoSize);
+       auto logoFrameRect = cv::Rect((image.size() - logoFrameSize) / 2, logoFrameSize);
+       logo.copyTo(logoMat(logoRect));
+
+       switch (config.finder_shape) {
+       case MV_BARCODE_GENERATE_ATTR_SHAPE_ROUND_RECT:
+               __round_rect(maskMat, logoRect, color_white);
+               __round_rect(image, logoFrameRect, color_frame);
+               break;
+       case MV_BARCODE_GENERATE_ATTR_SHAPE_CIRCLE:
+               cv::circle(maskMat, image.size() / 2, logoSize.width / 2, color_white, -1);
+               cv::circle(image, image.size() / 2, logoFrameSize.width / 2, color_frame, -1);
+               break;
+
+       default:
+               cv::rectangle(maskMat, logoRect, color_white, -1);
+               cv::rectangle(image, logoFrameRect, color_frame, -1);
+               break;
        }
-       return error;
+       cv::multiply(cv::Scalar::all(1.0) - maskMat, image, image);
+       cv::multiply(maskMat / 255, logoMat, logoMat);
+       cv::add(logoMat, image, image);
+
+       return MEDIA_VISION_ERROR_NONE;
 }
 
-int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, mv_barcode_image_format_e image_format,
-                                               const int imageWidth, const int imageHeight, mv_engine_config_h engine_cfg)
+int write_buffer_to_img(zint_symbol *symbol, const BarcodeConfig &config)
 {
+       std::string imageFileName = config.image_path;
        /* find directory */
        std::string prefix_imageFileName = imageFileName.substr(0, imageFileName.find_last_of('/'));
        LOGD("prefix_path: %s", prefix_imageFileName.c_str());
@@ -389,7 +369,7 @@ int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, m
        /* check current extension */
        std::vector<std::string> expectedExtensions;
        std::vector<int> compressionParams;
-       auto imageFormat = convertImageFormat(image_format);
+       auto imageFormat = convertImageFormat(config.image_format);
        int error = getFormatEncodingInfo(imageFormat, expectedExtensions, compressionParams);
 
        if (BARCODE_ERROR_NONE != error || expectedExtensions.empty()) {
@@ -419,11 +399,10 @@ int write_buffer_to_img(zint_symbol *symbol, const std::string &imageFileName, m
                resultFilePath += expectedExtensions[0];
 
        cv::Mat image(symbol->bitmap_height, symbol->bitmap_width, CV_8UC3, symbol->bitmap);
-       cv::resize(image, image, cv::Size(imageWidth, imageHeight), 0, 0, cv::INTER_AREA);
+       cv::resize(image, image, cv::Size(config.image_width, config.image_height), 0, 0, cv::INTER_AREA);
 
-       auto engineCfg = static_cast<Common::EngineConfig *>(engine_cfg);
-       if (engineCfg) {
-               error = __draw_logo(image, engineCfg);
+       if (!config.logo_path.empty()) {
+               error = __draw_logo(image, config);
                if (error != MEDIA_VISION_ERROR_NONE) {
                        LOGE("__draw_logo error code : %d", error);
                        return error;
@@ -479,6 +458,8 @@ int BarcodeConfig::update(mv_engine_config_h engine_cfg)
        if (!engine_cfg)
                return MEDIA_VISION_ERROR_NONE;
 
+       auto engineCfg = static_cast<Common::EngineConfig *>(engine_cfg);
+
        int value;
        int error = mv_engine_config_get_int_attribute(engine_cfg, MV_BARCODE_GENERATE_ATTR_TEXT, &value);
        if (error != MEDIA_VISION_ERROR_NONE) {
@@ -548,6 +529,12 @@ int BarcodeConfig::update(mv_engine_config_h engine_cfg)
        free(fgcolor);
        free(bgcolor);
 
+       error = engineCfg->getStringAttribute(std::string(MV_BARCODE_GENERATE_ATTR_EMBED_IMG_PATH), &logo_path);
+       if (error != BARCODE_ERROR_NONE) {
+               LOGE("getStringAttribute failed error : %d", error);
+               return error;
+       }
+
        return MEDIA_VISION_ERROR_NONE;
 }
 
index 4d6d0a75ab37f39d5b5cb6745f62821fec6957ab..858de94c0399ad213dc4b659c34a337d4897a40a 100644 (file)
@@ -117,7 +117,8 @@ int mv_barcode_generate_image(mv_engine_config_h engine_cfg, const char *message
                return MEDIA_VISION_ERROR_INVALID_DATA;
        }
 
-       BarcodeConfig config { message, type, qr_enc_mode, qr_ecc, qr_version, image_path, image_height, image_format };
+       BarcodeConfig config { message,    type,                qr_enc_mode,  qr_ecc,      qr_version,
+                                                  image_path, image_width, image_height, image_format };
 
        int ret = config.update(engine_cfg);
        if (ret != MEDIA_VISION_ERROR_NONE) {
@@ -136,8 +137,7 @@ int mv_barcode_generate_image(mv_engine_config_h engine_cfg, const char *message
                return ret;
        }
 
-       ret = MediaVision::Barcode::write_buffer_to_img(symbol, string(image_path), image_format, image_width, image_height,
-                                                                                                       engine_cfg);
+       ret = MediaVision::Barcode::write_buffer_to_img(symbol, config);
 
        ZBarcode_Delete(symbol);
        MEDIA_VISION_FUNCTION_LEAVE();