2 * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <mv_private.h>
18 #include <grpcpp/grpcpp.h>
20 #include <opencv2/core.hpp>
21 #include <opencv2/imgcodecs.hpp>
22 #include <opencv2/imgproc/imgproc.hpp>
23 #include <opencv2/opencv.hpp>
25 #include "mv_util_visualizer_rd.grpc.pb.h"
26 #include "mv_util_visualizer_2d.h"
28 static int win_w = 1920;
29 static int win_h = 1080;
30 static bool initialized = false;
35 ImageClientImpl(std::shared_ptr<grpc::Channel> channel) : stub_(NLImageService::NewStub(channel))
38 void DrawImage(mv_source_h source)
40 unsigned int width = 0u, height = 0u;
41 unsigned int bufferSize = 0u;
42 unsigned char *buffer = NULL;
44 mv_source_get_width(source, &width);
45 mv_source_get_height(source, &height);
46 mv_source_get_buffer(source, &buffer, &bufferSize);
48 cv::Mat _frame, frame;
50 _frame = cv::Mat(cv::Size(width, height), CV_8UC4, buffer);
51 cv::cvtColor(_frame, frame, cv::COLOR_RGBA2BGR);
53 std::vector<int> qualityType;
54 std::vector<uchar> buff;
55 qualityType.push_back(cv::IMWRITE_JPEG_QUALITY);
56 qualityType.push_back(90);
57 cv::imencode(".jpg", frame, buff, qualityType);
59 NLImageDrawRequest *request = new NLImageDrawRequest;
61 unsigned long length = buff.size();
63 std::string str(buff.begin(), buff.end());
64 std::string NLstring(str);
66 tmp.set_length(length);
67 tmp.set_data(NLstring);
69 request->mutable_image()->set_data(tmp.data());
70 request->mutable_image()->set_length(tmp.length());
74 grpc::ClientContext context;
75 grpc::Status status = stub_->DrawImage(&context, *request, &reply);
79 std::unique_ptr<NLImageService::Stub> stub_;
82 static int create_window_surface()
84 int err = MEDIA_VISION_ERROR_NONE;
85 err = egl_init_with_platform_window_surface(2, 8, 0, 0, win_w, win_h);
86 if (MEDIA_VISION_ERROR_NONE != err) {
87 LOGE("egl_init_with_platform_window_surface: %i", err);
88 return MEDIA_VISION_ERROR_INTERNAL;
90 err = init_2d_renderer(win_w, win_h);
91 if (MEDIA_VISION_ERROR_NONE != err) {
92 LOGE("init_2d_renderer: %i", err);
93 return MEDIA_VISION_ERROR_INTERNAL;
95 glClearColor(0.f, 0.f, 0.f, 1.0f);
96 glClear(GL_COLOR_BUFFER_BIT);
97 glViewport(0, 0, win_w, win_h);
99 return MEDIA_VISION_ERROR_NONE;
102 int mv_util_visualizer_2d(mv_source_h source, const char *url)
106 int err = MEDIA_VISION_ERROR_NONE;
107 texture_2d_t captex = { 0 };
109 if (url != NULL) { // remote display
110 ImageClientImpl imageChannel(grpc::CreateChannel(url, grpc::InsecureChannelCredentials()));
111 imageChannel.DrawImage(source);
113 return MEDIA_VISION_ERROR_NONE;
118 if (create_window_surface() != MEDIA_VISION_ERROR_NONE) {
119 return MEDIA_VISION_ERROR_INTERNAL;
124 err = load_texture(source, &texid, &texw, &texh);
125 if (MEDIA_VISION_ERROR_NONE != err) {
126 LOGE("load_texture: %i", err);
127 return MEDIA_VISION_ERROR_INTERNAL;
130 captex.texid = texid;
132 captex.height = texh;
133 captex.format = pixfmt_fourcc('R', 'G', 'B', 'A');
135 float scale = (float) win_h / (float) texh;
137 err = draw_2d_texture(&captex, (win_w - scale * texw) * 0.5f, 0, scale * texw, scale * texh, 0);
138 if (MEDIA_VISION_ERROR_NONE != err) {
139 LOGE("draw_2d_texture_ex: %i", err);
144 if (MEDIA_VISION_ERROR_NONE != err) {
145 LOGE("egl_swap: %i", err);
149 glDeleteTextures(1, reinterpret_cast<GLuint *>(&texid));