| 357 | } |
| 358 | |
| 359 | void FillNVCVImageBufferStrided(NVCVImageData &imgData, const std::vector<DLPackTensor> &infos, nvcv::ImageFormat fmt) |
| 360 | { |
| 361 | // If user passes an image format, we must check if the given buffers are consistent with it. |
| 362 | // Otherwise, we need to infer the image format from the given buffers. |
| 363 | |
| 364 | // Here's the plan: |
| 365 | // 1. Loop through all buffers and infer its dimensions, number of channels and data type. |
| 366 | // In case of ambiguity in inferring data type for a buffer, |
| 367 | // - If available, use given image format for disambiguation |
| 368 | // - Otherwise, if number of channels in last dimension is <= 4, treat it as packed, or else it's planar |
| 369 | // 2. Validate the data collected to see if it represents a real image format |
| 370 | // 3. If available, compare the given image format with the inferred one, they're data layout must be the same. |
| 371 | |
| 372 | // Let the games begin. |
| 373 | |
| 374 | NVCVImageBufferStrided &dataStrided = imgData.buffer.strided; |
| 375 | |
| 376 | dataStrided = {}; // start anew |
| 377 | |
| 378 | std::vector<BufferImageInfo> bufferInfoList = ExtractBufferImageInfo(infos, fmt); |
| 379 | std::vector<nvcv::DataType> planeDataTypes; |
| 380 | |
| 381 | int curPlane = 0; |
| 382 | for (const BufferImageInfo &b : bufferInfoList) |
| 383 | { |
| 384 | for (int p = 0; p < b.numPlanes; ++p, ++curPlane) |
| 385 | { |
| 386 | NVCV_ASSERT(curPlane <= 4); |
| 387 | |
| 388 | dataStrided.planes[curPlane].width = b.size.w; |
| 389 | dataStrided.planes[curPlane].height = b.size.h; |
| 390 | dataStrided.planes[curPlane].rowStride = b.rowStride; |
| 391 | dataStrided.planes[curPlane].basePtr = reinterpret_cast<NVCVByte *>(b.data) + b.planeStride * p; |
| 392 | |
| 393 | planeDataTypes.push_back(MakePackedType(b.dtype, b.isChannelLast ? b.numChannels : 1)); |
| 394 | } |
| 395 | } |
| 396 | dataStrided.numPlanes = curPlane; |
| 397 | |
| 398 | if (dataStrided.numPlanes == 0) |
| 399 | { |
| 400 | throw std::invalid_argument("Number of planes must be >= 1"); |
| 401 | } |
| 402 | |
| 403 | nvcv::ImageFormat inferredFormat = InferImageFormat(planeDataTypes); |
| 404 | |
| 405 | nvcv::ImageFormat finalFormat; |
| 406 | |
| 407 | // User explicitely specifies the image format? |
| 408 | if (fmt != nvcv::FMT_NONE) |
| 409 | { |
| 410 | if (!HasSameDataLayout(fmt, inferredFormat)) |
| 411 | { |
| 412 | throw std::invalid_argument( |
| 413 | util::FormatString("Format inferred from buffers %s isn't compatible with given image format %s", |
| 414 | util::ToString(inferredFormat).c_str(), util::ToString(fmt).c_str())); |
| 415 | } |
| 416 | finalFormat = fmt; |
no test coverage detected