| 274 | } |
| 275 | |
| 276 | nvcv::ImageFormat InferImageFormat(const std::vector<nvcv::DataType> &planePixTypes) |
| 277 | { |
| 278 | if (planePixTypes.empty()) |
| 279 | { |
| 280 | return nvcv::FMT_NONE; |
| 281 | } |
| 282 | |
| 283 | static_assert(NVCV_PACKING_0 == 0, "Invalid 0 packing value"); |
| 284 | NVCV_ASSERT(planePixTypes.size() <= 4); |
| 285 | |
| 286 | nvcv::Packing packing[4] = {nvcv::Packing::NONE}; |
| 287 | |
| 288 | int numChannels = 0; |
| 289 | |
| 290 | for (size_t p = 0; p < planePixTypes.size(); ++p) |
| 291 | { |
| 292 | packing[p] = planePixTypes[p].packing(); |
| 293 | numChannels += planePixTypes[p].numChannels(); |
| 294 | |
| 295 | if (planePixTypes[p].dataKind() != planePixTypes[0].dataKind()) |
| 296 | { |
| 297 | throw std::invalid_argument("Planes must all have the same data type"); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | nvcv::DataKind dataKind = planePixTypes[0].dataKind(); |
| 302 | |
| 303 | int numPlanes = planePixTypes.size(); |
| 304 | |
| 305 | // Planar or packed? |
| 306 | if (numPlanes == 1 || numChannels == numPlanes) |
| 307 | { |
| 308 | static const nvcv::ImageFormat baseFormatList[4] |
| 309 | = {nvcv::FMT_U8, nvcv::FMT_2F32, nvcv::FMT_RGB8, nvcv::FMT_RGBA8}; |
| 310 | |
| 311 | // Validate array index to prevent buffer overrun |
| 312 | if (numChannels < 1 || numChannels > 4) |
| 313 | { |
| 314 | throw std::invalid_argument( |
| 315 | util::FormatString("Invalid number of channels %d, must be between 1 and 4", numChannels)); |
| 316 | } |
| 317 | |
| 318 | nvcv::ImageFormat baseFormat = baseFormatList[numChannels - 1]; |
| 319 | |
| 320 | nvcv::ColorModel model = baseFormat.colorModel(); |
| 321 | switch (model) |
| 322 | { |
| 323 | case nvcv::ColorModel::YCbCr: |
| 324 | return nvcv::ImageFormat(baseFormat.colorSpec(), baseFormat.chromaSubsampling(), baseFormat.memLayout(), |
| 325 | dataKind, baseFormat.swizzle(), packing[0], packing[1], packing[2], packing[3]); |
| 326 | |
| 327 | case nvcv::ColorModel::UNDEFINED: |
| 328 | return nvcv::ImageFormat(baseFormat.memLayout(), dataKind, baseFormat.swizzle(), packing[0], packing[1], |
| 329 | packing[2], packing[3]); |
| 330 | case nvcv::ColorModel::RAW: |
| 331 | return nvcv::ImageFormat(baseFormat.rawPattern(), baseFormat.memLayout(), dataKind, baseFormat.swizzle(), |
| 332 | packing[0], packing[1], packing[2], packing[3]); |
| 333 | default: |
no test coverage detected