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 namespace cv {
22 
imread(const std::string & path,int flags)23 Mat imread(const std::string& path, int flags)
24 {
25     (void)flags;
26 
27     // read pgm/ppm
28     FILE* fp = fopen(path.c_str(), "rb");
29     if (!fp)
30         return Mat();
31 
32     Mat m;
33 
34     char magic[3];
35     int w, h;
36     int nscan = fscanf(fp, "%2s\n%d %d\n255\n", magic, &w, &h);
37     if (nscan == 3 && magic[0] == 'P' && (magic[1] == '5' || magic[1] == '6'))
38     {
39         if (magic[1] == '5')
40         {
41             m.create(h, w, CV_8UC1);
42         }
43         else if (magic[1] == '6')
44         {
45             m.create(h, w, CV_8UC3);
46         }
47         if (m.empty())
48         {
49             fclose(fp);
50             return Mat();
51         }
52 
53         fread(m.data, 1, m.total(), fp);
54     }
55 
56     fclose(fp);
57 
58     return m;
59 }
60 
imwrite(const std::string & path,const Mat & m)61 void imwrite(const std::string& path, const Mat& m)
62 {
63     // write pgm/ppm
64     FILE* fp = fopen(path.c_str(), "wb");
65     if (!fp)
66         return;
67 
68     if (m.channels() == 1)
69     {
70         fprintf(fp, "P5\n%d %d\n255\n", m.cols, m.rows);
71     }
72     else if (m.channels() == 3)
73     {
74         fprintf(fp, "P6\n%d %d\n255\n", m.cols, m.rows);
75     }
76 
77     fwrite(m.data, 1, m.total(), fp);
78 
79     fclose(fp);
80 }
81 
82 #if NCNN_PIXEL
resize(const Mat & src,Mat & dst,const Size & size,float sw,float sh,int flags)83 void resize(const Mat& src, Mat& dst, const Size& size, float sw, float sh, int flags)
84 {
85     (void)flags;
86 
87     int srcw = src.cols;
88     int srch = src.rows;
89 
90     int w = size.width;
91     int h = size.height;
92 
93     if (w == 0 || h == 0)
94     {
95         w = srcw * sw;
96         h = srch * sh;
97     }
98 
99     if (w == 0 || h == 0)
100         return;
101 
102     if (w == srcw && h == srch)
103     {
104         dst = src.clone();
105         return;
106     }
107 
108     cv::Mat tmp(h, w, src.c);
109     if (tmp.empty())
110         return;
111 
112     if (src.c == 1)
113         ncnn::resize_bilinear_c1(src.data, srcw, srch, tmp.data, w, h);
114     else if (src.c == 3)
115         ncnn::resize_bilinear_c3(src.data, srcw, srch, tmp.data, w, h);
116     else if (src.c == 4)
117         ncnn::resize_bilinear_c4(src.data, srcw, srch, tmp.data, w, h);
118 
119     dst = tmp;
120 }
121 #endif // NCNN_PIXEL
122 
123 } // namespace cv
124 
125 #endif // NCNN_SIMPLEOCV
126