modality_vision/image_helper : add image_helper directory for helping image load... 84/265184/1
authordyamy-lee <dyamy.lee@samsung.com>
Wed, 29 Sep 2021 06:58:29 +0000 (15:58 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Tue, 12 Oct 2021 14:31:35 +0000 (23:31 +0900)
Change-Id: Ibaa8d20816ac3af8d2a04b9685740ecad176d285

src/modules/modality_vision/image_helper/ImageHelper.cpp [new file with mode: 0644]
src/modules/modality_vision/image_helper/ImageHelper.h [new file with mode: 0644]
src/modules/modality_vision/image_helper/image_helper.cpp [new file with mode: 0644]
src/modules/modality_vision/image_helper/image_helper.h [new file with mode: 0644]
src/modules/modality_vision/image_helper/meson.build [new file with mode: 0644]

diff --git a/src/modules/modality_vision/image_helper/ImageHelper.cpp b/src/modules/modality_vision/image_helper/ImageHelper.cpp
new file mode 100644 (file)
index 0000000..1272a38
--- /dev/null
@@ -0,0 +1,219 @@
+/**
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "ImageHelper.h"
+#include <vector>
+
+#include <cstring>
+
+#include <setjmp.h>
+
+#include <opencv2/core.hpp>
+#include <opencv2/highgui.hpp>
+#include <opencv2/imgproc.hpp>
+#include <opencv2/imgproc/imgproc_c.h>
+#include <opencv2/imgcodecs/legacy/constants_c.h>
+
+/**
+ * @file   ImageHelper.cpp
+ * @brief  The ImageHelper class methods implementation.
+ */
+
+namespace MediaVision {
+namespace Common {
+
+namespace {
+
+const int OPEN_CV_CHANNELS = 3;
+const mv_colorspace_e OPEN_CV_COLOR = MEDIA_VISION_COLORSPACE_RGB888;
+const int QUADRANGLE_VERTICES = 4;
+
+std::vector<std::string> getJPGExtensions()
+{
+       std::vector<std::string> extensions;
+       extensions.push_back(".jpg");
+       extensions.push_back(".jpe");
+       extensions.push_back(".jpeg");
+       return extensions;
+}
+
+} /* anonymous namespace */
+
+int ImageHelper::loadImageToBuffer(
+               const char *filePath,
+               unsigned char **pDataBuffer,
+               unsigned long *pBufferSize,
+               ImageData *pImageData)
+{
+       if (filePath == NULL || pDataBuffer == NULL ||
+               pBufferSize == NULL || pImageData == NULL)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       cv::Mat image;
+       image = cv::imread(filePath);
+
+       if (!image.data)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       try {
+               cv::cvtColor(image, image, CV_BGR2RGB);
+       } catch (cv::Exception &e) {
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+       }
+
+       *pBufferSize = image.total() * image.elemSize();
+       (*pDataBuffer) = new unsigned char[*pBufferSize];
+       std::memcpy(*pDataBuffer, image.data, *pBufferSize);
+
+       pImageData->imageWidth = image.cols;
+       pImageData->imageHeight = image.rows;
+       pImageData->imageColorspace = OPEN_CV_COLOR;
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int ImageHelper::saveImageFromBuffer(
+               const char *filePath,
+               unsigned char *pDataBuffer,
+               const ImageData& imageData,
+               int quality)
+{
+       if (filePath == NULL || pDataBuffer == NULL)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       static const std::string DEFAULT_FILE_PATH = "out";
+       static const std::vector<std::string> JPG_EXTENSIONS = getJPGExtensions();
+
+       bool rightExtensionFlag = false;
+
+       std::string resultFilePath(filePath);
+       if (resultFilePath.empty()) {
+               resultFilePath = DEFAULT_FILE_PATH;
+       } else {
+               for (size_t extNum = 0; extNum < JPG_EXTENSIONS.size(); ++extNum) {
+                       if (resultFilePath.size() >= JPG_EXTENSIONS[extNum].size()) {
+                               std::string givenExtension = resultFilePath.substr(
+                                               resultFilePath.length() - JPG_EXTENSIONS[extNum].size(),
+                                               JPG_EXTENSIONS[extNum].size());
+
+                               std::transform(
+                                       givenExtension.begin(), givenExtension.end(),
+                                       givenExtension.begin(), ::tolower);
+
+                               if (givenExtension == JPG_EXTENSIONS[extNum]) {
+                                       rightExtensionFlag = true;
+                                       break;
+                               }
+                       }
+               }
+       }
+       if (!rightExtensionFlag)
+               resultFilePath += JPG_EXTENSIONS[0];
+
+       if (quality <= 0 || quality > 100)
+               quality = 100;
+
+       unsigned int width = imageData.imageWidth;
+       unsigned int height = imageData.imageHeight;
+
+       int conversionType = -1; // Type of conversion from given colorspace to BGR
+       unsigned int channelsNumber = 0;
+       switch (imageData.imageColorspace) {
+       case MEDIA_VISION_COLORSPACE_INVALID:
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+       case MEDIA_VISION_COLORSPACE_Y800:
+               channelsNumber = 1;
+               conversionType = CV_GRAY2BGR;
+               break;
+       case MEDIA_VISION_COLORSPACE_I420:
+               channelsNumber = 1;
+               height *= 1.5;
+               conversionType = CV_YUV2BGR_I420;
+               break;
+       case MEDIA_VISION_COLORSPACE_NV12:
+               channelsNumber = 1;
+               height *= 1.5;
+               conversionType = CV_YUV2BGR_NV12;
+               break;
+       case MEDIA_VISION_COLORSPACE_YV12:
+               channelsNumber = 1;
+               height *= 1.5;
+               conversionType = CV_YUV2BGR_YV12;
+               break;
+       case MEDIA_VISION_COLORSPACE_NV21:
+               channelsNumber = 1;
+               height *= 1.5;
+               conversionType = CV_YUV2BGR_NV21;
+               break;
+       case MEDIA_VISION_COLORSPACE_YUYV:
+               channelsNumber = 2;
+               conversionType = CV_YUV2BGR_YUYV;
+               break;
+       case MEDIA_VISION_COLORSPACE_UYVY:
+               channelsNumber = 2;
+               conversionType = CV_YUV2BGR_UYVY;
+               break;
+       case MEDIA_VISION_COLORSPACE_422P:
+               channelsNumber = 2;
+               conversionType = CV_YUV2BGR_Y422;
+               break;
+       case MEDIA_VISION_COLORSPACE_RGB565:
+               channelsNumber = 2;
+               conversionType = CV_BGR5652BGR;
+               break;
+       case MEDIA_VISION_COLORSPACE_RGB888:
+               channelsNumber = 3;
+               conversionType = CV_RGB2BGR;
+               break;
+       case MEDIA_VISION_COLORSPACE_RGBA:
+               channelsNumber = 4;
+               conversionType = CV_RGBA2BGR;
+               break;
+       default:
+               return MEDIA_VISION_ERROR_NOT_SUPPORTED_FORMAT;
+       }
+
+       const int depth = CV_8U;
+       cv::Mat cvImage(cv::Size(width, height),
+                                       CV_MAKETYPE(depth, channelsNumber), pDataBuffer);
+       cv::Mat cvBGRImage;
+       cv::cvtColor(cvImage, cvBGRImage, conversionType);
+
+       std::vector<int> compression_params(2);
+       compression_params[0] = CV_IMWRITE_JPEG_QUALITY;
+       compression_params[1] = quality;
+
+       if (!cv::imwrite(resultFilePath, cvBGRImage, compression_params))
+               return MEDIA_VISION_ERROR_INVALID_OPERATION;
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+int ImageHelper::destroyLoadedBuffer(unsigned char *pDataBuffer)
+{
+       if (!pDataBuffer) {
+               return MEDIA_VISION_ERROR_NONE;
+       }
+
+       delete [] pDataBuffer;
+       pDataBuffer = NULL;
+
+       return MEDIA_VISION_ERROR_NONE;
+}
+
+
+} /* Common */
+} /* MediaVision */
diff --git a/src/modules/modality_vision/image_helper/ImageHelper.h b/src/modules/modality_vision/image_helper/ImageHelper.h
new file mode 100644 (file)
index 0000000..72b8984
--- /dev/null
@@ -0,0 +1,116 @@
+/**
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MEDIA_VISION_IMAGEHELPER_H__
+#define __MEDIA_VISION_IMAGEHELPER_H__
+
+#include <mv_common.h>
+
+#include <stddef.h>
+#include <stdio.h>
+
+/**
+ * @file   ImageHelper.h
+ * @brief  ImageHelper class definition.
+ */
+
+namespace cv {
+       template<typename _Tp> class Scalar_;
+       typedef Scalar_<double> Scalar;
+
+       class VideoCapture;
+}
+
+namespace MediaVision {
+namespace Common {
+
+/**
+ * @class ImageHelper
+ * @brief Helper class that provides set of useful methods
+ *        for image management.
+ */
+class ImageHelper {
+public:
+       /**
+        * @brief Structure to keep information about width, height and colorspace of an image.
+        */
+       struct ImageData {
+               unsigned int imageWidth;                 /**< Image width */
+               unsigned int imageHeight;                /**< Image height */
+               mv_colorspace_e imageColorspace;         /**< Image colorspace */
+       };
+
+       /**
+         * @brief Loads image from file to the buffer of unsigned chars.
+         *
+         * @since_tizen 3.0
+         * @param [in]  filePath       Path to the image file to be loaded to the
+         *                             @a pDataBuffer
+         * @param [out] pDataBuffer    The buffer of unsigned chars where image data
+         *                             will be stored
+         * @param [out] pBufferSize    The size of the @a pDataBuffer
+         * @param [out] pImageData     The image data (structure that keeps
+         *                             information about image width, height,
+         *                             and colorspace)
+         * @return @c 0 on success, otherwise a negative error value
+         *
+         * @see ImageHelper::saveImageFromBuffer()
+         * @see ImageHelper::destroyLoadedBuffer()
+         */
+       static int loadImageToBuffer(
+                       const char *filePath,
+                       unsigned char **pDataBuffer,
+                       unsigned long *pBufferSize,
+                       ImageData *pImageData);
+
+       /**
+        * @brief Saves image stored into @a pDataBuffer to the file in jpeg format.
+        *
+        * @since_tizen 3.0
+        * @param [in] filePath       The path to the file where image will be saved
+        * @param [in] pDataBuffer    Data buffer that contains image data
+        * @param [in] imageData      The image data (structure that keeps
+        *                            information about image width, height,
+        *                            and colorspace)
+        * @param [in] quality        Quality for the output jpeg file (0..100)
+        * @return @c 0 on success, otherwise a negative error value
+        *
+        * @see ImageHelper::loadImageToBuffer()
+        */
+       static int saveImageFromBuffer(
+                       const char *filePath,
+                       unsigned char *pDataBuffer,
+                       const ImageData& imageData,
+                       int quality = 100);
+
+       /**
+        * @brief Destroys loaded buffer by loadImageToBuffer().
+        *
+        * @since_tizen 3.0
+        * @param [out] pDataBuffer    The buffer of unsigned chars where image data
+        *                             will be stored
+        * @return @c 0 on success, otherwise a negative error value
+        *
+        * @see ImageHelper::loadImageToBuffer()
+        */
+       static int destroyLoadedBuffer(unsigned char *pDataBuffer);
+
+};
+
+} /* Common */
+} /* MediaVision */
+
+#endif /* __MEDIA_VISION_IMAGEHELPER_H__ */
diff --git a/src/modules/modality_vision/image_helper/image_helper.cpp b/src/modules/modality_vision/image_helper/image_helper.cpp
new file mode 100644 (file)
index 0000000..b844882
--- /dev/null
@@ -0,0 +1,83 @@
+/**
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "image_helper.h"
+
+#include "ImageHelper.h"
+
+#include <opencv2/core.hpp>
+
+/**
+ * @file   image_helper.cpp
+ * @brief  image_helper.h functions implementation.
+ */
+
+using namespace MediaVision::Common;
+
+image_data_s convertToCData(ImageHelper::ImageData data)
+{
+       image_data_s ret;
+       ret.image_width = data.imageWidth;
+       ret.image_height = data.imageHeight;
+       ret.image_colorspace = data.imageColorspace;
+       return ret;
+}
+
+ImageHelper::ImageData convertToCppData(image_data_s data)
+{
+       ImageHelper::ImageData ret;
+       ret.imageWidth = data.image_width;
+       ret.imageHeight = data.image_height;
+       ret.imageColorspace = data.image_colorspace;
+       return ret;
+}
+
+int load_image_to_buffer(
+       const char *file_path,
+       unsigned char **data_buffer,
+       unsigned long *buffer_size,
+       image_data_s *image_data)
+{
+       if (image_data == NULL)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       int err;
+       ImageHelper::ImageData imageData;
+       err = ImageHelper::loadImageToBuffer(file_path, data_buffer, buffer_size, &imageData);
+
+       if (err == MEDIA_VISION_ERROR_NONE)
+               *image_data = convertToCData(imageData);
+
+       return err;
+}
+
+int save_image_from_buffer(
+       const char *file_path,
+       unsigned char *data_buffer,
+       const image_data_s *image_data,
+       int quality)
+{
+       if (image_data == NULL)
+               return MEDIA_VISION_ERROR_INVALID_PARAMETER;
+
+       ImageHelper::ImageData imageData = convertToCppData(*image_data);
+       return ImageHelper::saveImageFromBuffer(file_path, data_buffer, imageData, quality);
+}
+
+int destroy_loaded_buffer(unsigned char *data_buffer)
+{
+       return ImageHelper::destroyLoadedBuffer(data_buffer);
+}
\ No newline at end of file
diff --git a/src/modules/modality_vision/image_helper/image_helper.h b/src/modules/modality_vision/image_helper/image_helper.h
new file mode 100644 (file)
index 0000000..621ead3
--- /dev/null
@@ -0,0 +1,109 @@
+/**
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MEDIA_VISION_IMAGE_HELPER_H__
+#define __MEDIA_VISION_IMAGE_HELPER_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif /* __cplusplus */
+
+#include <mv_common.h>
+
+#ifdef __cplusplus
+#include <cstddef>
+#include <cstdio>
+#else
+#include <stddef.h>
+#include <stdio.h>
+#endif
+
+/**
+ * @file   image_helper.h
+ * @brief  Helper functions that provides set of useful methods for image management
+ *         (c wrapper of ImageHelper class).
+ */
+
+/**
+ * @brief Structure to keep information about width, height and colorspace of an image.
+ *
+ * @since_tizen 3.0
+ */
+typedef struct {
+       unsigned int image_width;                 /**< Image width */
+       unsigned int image_height;                /**< Image height */
+       mv_colorspace_e image_colorspace;         /**< Image colorspace */
+} image_data_s;
+
+/**
+ * @brief Loads image from file to the buffer of unsigned chars.
+ *
+ * @since_tizen 3.0
+ * @param [in]  file_path      Path to the image file to be loaded to the
+ *                             @a pDataBuffer
+ * @param [out] data_buffer    The buffer of unsigned chars where image data
+ *                             will be stored
+ * @param [out] buffer_size    The size of the @a pDataBuffer
+ * @param [out] image_data     The image data (structure that keeps
+ *                             information about image width, height,
+ *                             and colorspace)
+ * @return @c 0 on success, otherwise a negative error value
+ *
+ * @see save_image_from_buffer()
+ * @see destroy_loaded_buffer()
+ */
+int load_image_to_buffer(
+               const char *file_path,
+               unsigned char **data_buffer,
+               unsigned long *buffer_size,
+               image_data_s *image_data);
+
+/**
+ * @brief Saves image stored into @a pDataBuffer to the file in jpeg format.
+ *
+ * @since_tizen 3.0
+ * @param [in] file_path      The path to the file where image will be saved
+ * @param [in] data_buffer    Data buffer that contains image data
+ * @param [in] image_data     The image data (structure that keeps
+ *                            information about image width, height,
+ *                            and colorspace)
+ * @param [in] quality        Quality for the output jpeg file (0..100)
+ * @return @c 0 on success, otherwise a negative error value
+ *
+ * @see load_image_to_buffer()
+ */
+int save_image_from_buffer(
+               const char *file_path,
+               unsigned char *data_buffer,
+               const image_data_s *image_data,
+               int quality);
+
+/**
+ * @brief Destroys loaded buffer by load_image_to_buffer().
+ *
+ * @since_tizen 3.0
+ * @param [in] data_buffer    Data buffer to be deallocated
+ * @return @c 0 on success, otherwise a negative error value
+ *
+ * @see load_image_to_buffer()
+ */
+int destroy_loaded_buffer(unsigned char *data_buffer);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+
+#endif /* __IMAGE_HELPER_H__*/
diff --git a/src/modules/modality_vision/image_helper/meson.build b/src/modules/modality_vision/image_helper/meson.build
new file mode 100644 (file)
index 0000000..e03b3ef
--- /dev/null
@@ -0,0 +1,31 @@
+image_helper_srcs = [
+       'image_helper.cpp',
+       'ImageHelper.cpp'
+]
+
+image_helper_include_dirs = include_directories('.')
+
+media_vision_dep = dependency('capi-media-vision', method : 'pkg-config')
+opencv_dep = dependency('opencv', method : 'pkg-config')
+
+image_helper_deps = [
+       dlog_dep,
+       media_vision_dep,
+       opencv_dep
+]
+
+lib_image_helper = static_library(
+       'image_helper',
+       image_helper_srcs,
+       include_directories : [image_helper_include_dirs],
+       dependencies : [image_helper_deps],
+       install_dir : mmi_manager_prefix_libdir,
+       install : false,
+       pie : true
+)
+
+image_helper_declared_dep = declare_dependency(
+       link_with : lib_image_helper,
+       dependencies : [image_helper_deps],
+       include_directories : [image_helper_include_dirs]
+)
\ No newline at end of file