SoftwareBitmap LoadImageFile()

in Tools/WinMLRunner/src/BindingUtilities.cpp [270:336]


    SoftwareBitmap LoadImageFile(const ILearningModelFeatureDescriptor& modelFeatureDescriptor,
                                 const InputDataType inputDataType, const hstring& filePath,
                                 const CommandLineArgs& args, uint32_t iterationNum,
                                 ColorManagementMode colorManagementMode)
    {
        // We assume NCHW and NCDHW
        uint64_t width = 0;
        uint64_t height = 0;
        GetHeightAndWidthFromLearningModelFeatureDescriptor(modelFeatureDescriptor, width, height);
        IRandomAccessStream stream;
        BitmapDecoder decoder = NULL;
        try
        {
            // open the file
            StorageFile file = StorageFile::GetFileFromPathAsync(filePath).get();
            // get a stream on it
            stream = file.OpenAsync(FileAccessMode::Read).get();
            // Create the decoder from the stream
            decoder = BitmapDecoder::CreateAsync(stream).get();
        }
        catch (hresult_error hr)
        {
            printf("    Failed to load the image file, make sure you are using fully qualified paths\r\n");
            printf("    %ws\n", hr.message().c_str());
            exit(hr.code());
        }
        BitmapPixelFormat format = inputDataType == InputDataType::Tensor
                                       ? decoder.BitmapPixelFormat()
                                       : TypeHelper::GetBitmapPixelFormat(inputDataType);
        try
        {
            // If input dimensions are different from tensor input, then scale / crop while reading
            if (args.IsAutoScale() && (decoder.PixelHeight() != height || decoder.PixelWidth() != width))
            {
                if (!args.TerseOutput() || iterationNum == 0)
                    std::cout << std::endl
                              << "Binding Utilities: AutoScaling input image to match model input dimensions...";

                // Create a transform object with default parameters (no transform)
                auto transform = BitmapTransform();
                transform.ScaledHeight(static_cast<uint32_t>(height));
                transform.ScaledWidth(static_cast<uint32_t>(width));
                transform.InterpolationMode(args.AutoScaleInterpMode());

                // get the bitmap
                return decoder
                    .GetSoftwareBitmapAsync(format, decoder.BitmapAlphaMode(), transform,
                                            ExifOrientationMode::RespectExifOrientation, colorManagementMode)
                    .get();
            }
            else
            {
                // get the bitmap
                return decoder
                    .GetSoftwareBitmapAsync(format, decoder.BitmapAlphaMode(), BitmapTransform(),
                                            ExifOrientationMode::RespectExifOrientation, colorManagementMode)
                    .get();
            }
        }
        catch (hresult_error hr)
        {
            printf("    Failed to create SoftwareBitmap! Please make sure that input image is within the model's "
                   "colorspace.\n");
            printf("    %ws\n", hr.message().c_str());
            exit(hr.code());
        }
    }