Elffuss BraidHub

kikocis/ncnn public

master · 11.6 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
4//
5// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
6// in compliance with the License. You may obtain a copy of the License at
7//
8// https://opensource.org/licenses/BSD-3-Clause
9//
10// Unless required by applicable law or agreed to in writing, software distributed
11// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12// CONDITIONS OF ANY KIND, either express or implied. See the License for the
13// specific language governing permissions and limitations under the License.
14
15#include "simpleocv.h"
16
17#if NCNN_SIMPLEOCV
18
19#include <stdio.h>
20
21#define STB_IMAGE_IMPLEMENTATION
22#if __ARM_NEON
23#define STBI_NEON
24#endif
25#define STBI_NO_THREAD_LOCALS
26#define STBI_ONLY_JPEG
27#define STBI_ONLY_PNG
28#define STBI_ONLY_BMP
29#define STBI_ONLY_PNM
30#include "stb_image.h"
31
32#define STB_IMAGE_WRITE_IMPLEMENTATION
33#include "stb_image_write.h"
34
35namespace cv {
36
37Mat imread(const std::string& path, int flags)
38{
39 int desired_channels = 0;
40 if (flags == IMREAD_UNCHANGED)
41 {
42 desired_channels = 0;
43 }
44 else if (flags == IMREAD_GRAYSCALE)
45 {
46 desired_channels = 1;
47 }
48 else if (flags == IMREAD_COLOR)
49 {
50 desired_channels = 3;
51 }
52 else
53 {
54 // unknown flags
55 return Mat();
56 }
57
58 int w;
59 int h;
60 int c;
61 unsigned char* pixeldata = stbi_load(path.c_str(), &w, &h, &c, desired_channels);
62 if (!pixeldata)
63 {
64 // load failed
65 return Mat();
66 }
67
68 if (desired_channels)
69 {
70 c = desired_channels;
71 }
72
73 // copy pixeldata to Mat
74 Mat img;
75 if (c == 1)
76 {
77 img.create(h, w, CV_8UC1);
78 }
79 else if (c == 3)
80 {
81 img.create(h, w, CV_8UC3);
82 }
83 else if (c == 4)
84 {
85 img.create(h, w, CV_8UC4);
86 }
87 else
88 {
89 // unexpected channels
90 stbi_image_free(pixeldata);
91 return Mat();
92 }
93
94 memcpy(img.data, pixeldata, w * h * c);
95
96 stbi_image_free(pixeldata);
97
98 // // resolve exif orientation
99 // {
100 // std::ifstream ifs;
101 // ifs.open(filename.c_str(), std::ifstream::in);
102 //
103 // if (ifs.good())
104 // {
105 // ExifReader exif_reader(ifs);
106 // if (exif_reader.parse())
107 // {
108 // ExifEntry_t e = exif_reader.getTag(ORIENTATION);
109 // int orientation = e.field_u16;
110 // if (orientation >= 1 && orientation <= 8)
111 // rotate_by_orientation(img, img, orientation);
112 // }
113 // }
114 //
115 // ifs.close();
116 // }
117
118 // rgb to bgr
119 if (c == 3)
120 {
121 uchar* p = img.data;
122 for (int i = 0; i < w * h; i++)
123 {
124 std::swap(p[0], p[2]);
125 p += 3;
126 }
127 }
128 if (c == 4)
129 {
130 uchar* p = img.data;
131 for (int i = 0; i < w * h; i++)
132 {
133 std::swap(p[0], p[2]);
134 p += 4;
135 }
136 }
137
138 return img;
139}
140
141Mat imdecode(const std::vector<uchar>& buf, int flags)
142{
143 int desired_channels = 0;
144 if (flags == IMREAD_UNCHANGED)
145 {
146 desired_channels = 0;
147 }
148 else if (flags == IMREAD_GRAYSCALE)
149 {
150 desired_channels = 1;
151 }
152 else if (flags == IMREAD_COLOR)
153 {
154 desired_channels = 3;
155 }
156 else
157 {
158 // unknown flags
159 return Mat();
160 }
161
162 int w;
163 int h;
164 int c;
165 unsigned char* pixeldata = stbi_load_from_memory(&buf.front(), buf.size(), &w, &h, &c, desired_channels);
166 if (!pixeldata)
167 {
168 // load failed
169 return Mat();
170 }
171
172 if (desired_channels)
173 {
174 c = desired_channels;
175 }
176
177 // copy pixeldata to Mat
178 Mat img;
179 if (c == 1)
180 {
181 img.create(h, w, CV_8UC1);
182 }
183 else if (c == 3)
184 {
185 img.create(h, w, CV_8UC3);
186 }
187 else if (c == 4)
188 {
189 img.create(h, w, CV_8UC4);
190 }
191 else
192 {
193 // unexpected channels
194 stbi_image_free(pixeldata);
195 return Mat();
196 }
197
198 memcpy(img.data, pixeldata, w * h * c);
199
200 stbi_image_free(pixeldata);
201
202 // rgb to bgr
203 if (c == 3)
204 {
205 uchar* p = img.data;
206 for (int i = 0; i < w * h; i++)
207 {
208 std::swap(p[0], p[2]);
209 p += 3;
210 }
211 }
212 if (c == 4)
213 {
214 uchar* p = img.data;
215 for (int i = 0; i < w * h; i++)
216 {
217 std::swap(p[0], p[2]);
218 p += 4;
219 }
220 }
221
222 return img;
223}
224
225bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params)
226{
227 const char* _ext = strrchr(path.c_str(), '.');
228 if (!_ext)
229 {
230 // missing extension
231 return false;
232 }
233
234 std::string ext = _ext;
235 Mat img = m.clone();
236
237 // bgr to rgb
238 int c = 0;
239 if (img.type() == CV_8UC1)
240 {
241 c = 1;
242 }
243 else if (img.type() == CV_8UC3)
244 {
245 c = 3;
246 uchar* p = img.data;
247 for (int i = 0; i < img.cols * img.rows; i++)
248 {
249 std::swap(p[0], p[2]);
250 p += 3;
251 }
252 }
253 else if (img.type() == CV_8UC4)
254 {
255 c = 4;
256 uchar* p = img.data;
257 for (int i = 0; i < img.cols * img.rows; i++)
258 {
259 std::swap(p[0], p[2]);
260 p += 4;
261 }
262 }
263 else
264 {
265 // unexpected image channels
266 return false;
267 }
268
269 bool success = false;
270
271 if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
272 {
273 int quality = 95;
274 for (size_t i = 0; i < params.size(); i += 2)
275 {
276 if (params[i] == IMWRITE_JPEG_QUALITY)
277 {
278 quality = params[i + 1];
279 break;
280 }
281 }
282 success = stbi_write_jpg(path.c_str(), img.cols, img.rows, c, img.data, quality);
283 }
284 else if (ext == ".png" || ext == ".PNG")
285 {
286 success = stbi_write_png(path.c_str(), img.cols, img.rows, c, img.data, 0);
287 }
288 else if (ext == ".bmp" || ext == ".BMP")
289 {
290 success = stbi_write_bmp(path.c_str(), img.cols, img.rows, c, img.data);
291 }
292 else
293 {
294 // unknown extension type
295 return false;
296 }
297
298 return success;
299}
300
301void imshow(const std::string& name, const Mat& m)
302{
303 NCNN_LOGE("imshow save image to %s.png", name.c_str());
304
305 imwrite(name + ".png", m);
306}
307
308int waitKey(int delay)
309{
310 NCNN_LOGE("waitKey stub");
311 return -1;
312}
313
314#if NCNN_PIXEL
315void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags)
316{
317 (void)flags;
318
319 int srcw = src.cols;
320 int srch = src.rows;
321
322 int w = size.width;
323 int h = size.height;
324
325 if (w == 0 || h == 0)
326 {
327 w = srcw * sw;
328 h = srch * sh;
329 }
330
331 if (w == 0 || h == 0)
332 return;
333
334 if (w == srcw && h == srch)
335 {
336 dst = src.clone();
337 return;
338 }
339
340 cv::Mat tmp(h, w, src.c);
341 if (tmp.empty())
342 return;
343
344 if (src.c == 1)
345 ncnn::resize_bilinear_c1(src.data, srcw, srch, tmp.data, w, h);
346 else if (src.c == 3)
347 ncnn::resize_bilinear_c3(src.data, srcw, srch, tmp.data, w, h);
348 else if (src.c == 4)
349 ncnn::resize_bilinear_c4(src.data, srcw, srch, tmp.data, w, h);
350
351 dst = tmp;
352}
353#endif // NCNN_PIXEL
354
355#if NCNN_PIXEL_DRAWING
356
357void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness)
358{
359 Rect rec;
360 rec.x = std::min(pt1.x, pt2.x);
361 rec.y = std::min(pt1.y, pt2.y);
362 rec.width = std::max(pt1.x, pt2.x) - rec.x;
363 rec.height = std::max(pt1.y, pt2.y) - rec.y;
364 rectangle(img, rec, color, thickness);
365}
366
367void rectangle(Mat& img, Rect rec, const Scalar& _color, int thickness)
368{
369 unsigned int color = 0;
370 unsigned char* border_color = (unsigned char*)&color;
371
372 if (img.c == 1)
373 {
374 border_color[0] = _color[0];
375 ncnn::draw_rectangle_c1(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
376 }
377 else if (img.c == 3)
378 {
379 border_color[0] = _color[0];
380 border_color[1] = _color[1];
381 border_color[2] = _color[2];
382 ncnn::draw_rectangle_c3(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
383 }
384 else if (img.c == 4)
385 {
386 border_color[0] = _color[0];
387 border_color[1] = _color[1];
388 border_color[2] = _color[2];
389 border_color[3] = _color[3];
390 ncnn::draw_rectangle_c4(img.data, img.cols, img.rows, rec.x, rec.y, rec.width, rec.height, color, thickness);
391 }
392}
393
394void circle(Mat& img, Point center, int radius, const Scalar& _color, int thickness)
395{
396 unsigned int color = 0;
397 unsigned char* border_color = (unsigned char*)&color;
398
399 if (img.c == 1)
400 {
401 border_color[0] = _color[0];
402 ncnn::draw_circle_c1(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
403 }
404 else if (img.c == 3)
405 {
406 border_color[0] = _color[0];
407 border_color[1] = _color[1];
408 border_color[2] = _color[2];
409 ncnn::draw_circle_c3(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
410 }
411 else if (img.c == 4)
412 {
413 border_color[0] = _color[0];
414 border_color[1] = _color[1];
415 border_color[2] = _color[2];
416 border_color[3] = _color[3];
417 ncnn::draw_circle_c4(img.data, img.cols, img.rows, center.x, center.y, radius, color, thickness);
418 }
419}
420
421void line(Mat& img, Point p0, Point p1, const Scalar& _color, int thickness)
422{
423 unsigned int color = 0;
424 unsigned char* border_color = (unsigned char*)&color;
425
426 if (img.c == 1)
427 {
428 border_color[0] = _color[0];
429 ncnn::draw_line_c1(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
430 }
431 else if (img.c == 3)
432 {
433 border_color[0] = _color[0];
434 border_color[1] = _color[1];
435 border_color[2] = _color[2];
436 ncnn::draw_line_c3(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
437 }
438 else if (img.c == 4)
439 {
440 border_color[0] = _color[0];
441 border_color[1] = _color[1];
442 border_color[2] = _color[2];
443 border_color[3] = _color[3];
444 ncnn::draw_line_c4(img.data, img.cols, img.rows, p0.x, p0.y, p1.x, p1.y, color, thickness);
445 }
446}
447
448void putText(Mat& img, const std::string& text, Point org, int fontFace, double fontScale, Scalar _color, int thickness)
449{
450 const int fontpixelsize = 20 * fontScale;
451
452 unsigned int color = 0;
453 unsigned char* border_color = (unsigned char*)&color;
454
455 if (img.c == 1)
456 {
457 border_color[0] = _color[0];
458 ncnn::draw_text_c1(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
459 }
460 else if (img.c == 3)
461 {
462 border_color[0] = _color[0];
463 border_color[1] = _color[1];
464 border_color[2] = _color[2];
465 ncnn::draw_text_c3(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
466 }
467 else if (img.c == 4)
468 {
469 border_color[0] = _color[0];
470 border_color[1] = _color[1];
471 border_color[2] = _color[2];
472 border_color[3] = _color[3];
473 ncnn::draw_text_c4(img.data, img.cols, img.rows, text.c_str(), org.x, org.y - fontpixelsize * 2, fontpixelsize, color);
474 }
475}
476
477Size getTextSize(const std::string& text, int fontFace, double fontScale, int thickness, int* baseLine)
478{
479 const int fontpixelsize = 20 * fontScale;
480
481 int w;
482 int h;
483 ncnn::get_text_drawing_size(text.c_str(), fontpixelsize, &w, &h);
484
485 *baseLine = 0;
486
487 return Size(w, h);
488}
489
490#endif // NCNN_PIXEL_DRAWING
491
492} // namespace cv
493
494#endif // NCNN_SIMPLEOCV