| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2021 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 "imreadwrite.h" |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | |
| 19 | #define STB_IMAGE_IMPLEMENTATION |
| 20 | #define STBI_NO_THREAD_LOCALS |
| 21 | #define STBI_ONLY_JPEG |
| 22 | #define STBI_ONLY_PNG |
| 23 | #define STBI_ONLY_BMP |
| 24 | #define STBI_ONLY_PNM |
| 25 | #include "../../src/stb_image.h" |
| 26 | |
| 27 | #define STB_IMAGE_WRITE_IMPLEMENTATION |
| 28 | #include "../../src/stb_image_write.h" |
| 29 | |
| 30 | namespace cv { |
| 31 | |
| 32 | Mat imread(const std::string& path, int flags) |
| 33 | { |
| 34 | int desired_channels = 0; |
| 35 | if (flags == IMREAD_UNCHANGED) |
| 36 | { |
| 37 | desired_channels = 0; |
| 38 | } |
| 39 | else if (flags == IMREAD_GRAYSCALE) |
| 40 | { |
| 41 | desired_channels = 1; |
| 42 | } |
| 43 | else if (flags == IMREAD_COLOR) |
| 44 | { |
| 45 | desired_channels = 3; |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | // unknown flags |
| 50 | return Mat(); |
| 51 | } |
| 52 | |
| 53 | int w; |
| 54 | int h; |
| 55 | int c; |
| 56 | unsigned char* pixeldata = stbi_load(path.c_str(), &w, &h, &c, desired_channels); |
| 57 | if (!pixeldata) |
| 58 | { |
| 59 | // load failed |
| 60 | return Mat(); |
| 61 | } |
| 62 | |
| 63 | if (desired_channels) |
| 64 | { |
| 65 | c = desired_channels; |
| 66 | } |
| 67 | |
| 68 | // copy pixeldata to Mat |
| 69 | Mat img; |
| 70 | if (c == 1) |
| 71 | { |
| 72 | img.create(h, w, CV_8UC1); |
| 73 | } |
| 74 | else if (c == 3) |
| 75 | { |
| 76 | img.create(h, w, CV_8UC3); |
| 77 | } |
| 78 | else if (c == 4) |
| 79 | { |
| 80 | img.create(h, w, CV_8UC4); |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | // unexpected channels |
| 85 | stbi_image_free(pixeldata); |
| 86 | return Mat(); |
| 87 | } |
| 88 | |
| 89 | memcpy(img.data, pixeldata, static_cast<size_t>(w) * static_cast<size_t>(h) * static_cast<size_t>(c)); |
| 90 | |
| 91 | stbi_image_free(pixeldata); |
| 92 | |
| 93 | // // resolve exif orientation |
| 94 | // { |
| 95 | // std::ifstream ifs; |
| 96 | // ifs.open(filename.c_str(), std::ifstream::in); |
| 97 | // |
| 98 | // if (ifs.good()) |
| 99 | // { |
| 100 | // ExifReader exif_reader(ifs); |
| 101 | // if (exif_reader.parse()) |
| 102 | // { |
| 103 | // ExifEntry_t e = exif_reader.getTag(ORIENTATION); |
| 104 | // int orientation = e.field_u16; |
| 105 | // if (orientation >= 1 && orientation <= 8) |
| 106 | // rotate_by_orientation(img, img, orientation); |
| 107 | // } |
| 108 | // } |
| 109 | // |
| 110 | // ifs.close(); |
| 111 | // } |
| 112 | |
| 113 | // rgb to bgr |
| 114 | if (c == 3) |
| 115 | { |
| 116 | uchar* p = img.data; |
| 117 | for (int i = 0; i < w * h; i++) |
| 118 | { |
| 119 | std::swap(p[0], p[2]); |
| 120 | p += 3; |
| 121 | } |
| 122 | } |
| 123 | if (c == 4) |
| 124 | { |
| 125 | uchar* p = img.data; |
| 126 | for (int i = 0; i < w * h; i++) |
| 127 | { |
| 128 | std::swap(p[0], p[2]); |
| 129 | p += 4; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | return img; |
| 134 | } |
| 135 | |
| 136 | bool imwrite(const std::string& path, const Mat& m, const std::vector<int>& params) |
| 137 | { |
| 138 | const char* _ext = strrchr(path.c_str(), '.'); |
| 139 | if (!_ext) |
| 140 | { |
| 141 | // missing extension |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | std::string ext = _ext; |
| 146 | Mat img = m.clone(); |
| 147 | |
| 148 | // bgr to rgb |
| 149 | int c = 0; |
| 150 | if (img.type() == CV_8UC1) |
| 151 | { |
| 152 | c = 1; |
| 153 | } |
| 154 | else if (img.type() == CV_8UC3) |
| 155 | { |
| 156 | c = 3; |
| 157 | uchar* p = img.data; |
| 158 | for (int i = 0; i < img.cols * img.rows; i++) |
| 159 | { |
| 160 | std::swap(p[0], p[2]); |
| 161 | p += 3; |
| 162 | } |
| 163 | } |
| 164 | else if (img.type() == CV_8UC4) |
| 165 | { |
| 166 | c = 4; |
| 167 | uchar* p = img.data; |
| 168 | for (int i = 0; i < img.cols * img.rows; i++) |
| 169 | { |
| 170 | std::swap(p[0], p[2]); |
| 171 | p += 4; |
| 172 | } |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | // unexpected image channels |
| 177 | return false; |
| 178 | } |
| 179 | |
| 180 | bool success = false; |
| 181 | |
| 182 | if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG") |
| 183 | { |
| 184 | int quality = 95; |
| 185 | for (size_t i = 0; i < params.size(); i += 2) |
| 186 | { |
| 187 | if (params[i] == IMWRITE_JPEG_QUALITY) |
| 188 | { |
| 189 | quality = params[i + 1]; |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | success = stbi_write_jpg(path.c_str(), img.cols, img.rows, c, img.data, quality); |
| 194 | } |
| 195 | else if (ext == ".png" || ext == ".PNG") |
| 196 | { |
| 197 | success = stbi_write_png(path.c_str(), img.cols, img.rows, c, img.data, 0); |
| 198 | } |
| 199 | else if (ext == ".bmp" || ext == ".BMP") |
| 200 | { |
| 201 | success = stbi_write_bmp(path.c_str(), img.cols, img.rows, c, img.data); |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | // unknown extension type |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | return success; |
| 210 | } |
| 211 | |
| 212 | } // namespace cv |