D3D12HelloTexture - Setting up texture from a bitmap in GenerateTextureData()

Started by
0 comments, last by JedTheKrampus 8 years, 1 month ago

I was experimenting with HelloTexture Sample from Microsoft's DirectX Graphics Samples. The sample shows how to to apply a Texture2D to triangle. I was learning this sample especially because, I'm building a similar application and sample uses a different approach than what I have been doing.

I plan to load an image using WIC and get the texture data so that I can display the image in the triage region, instead of the boxes.

I've loaded my image in 32bppPRGBA. But when I run the program I'm only seeing some gray scale pixels.

Here my modified GenerateTextureData() (line no: 430) function.


std::vector<UINT8> D3D12HelloTexture::GenerateTextureData()
{ 

    const UINT rowPitch = TextureWidth * TexturePixelSize; // tex_width = 256, tex_pixel_size = 4
    const UINT textureSize = rowPitch * TextureHeight;

    HRESULT hr;
    HANDLE hd;

    std::vector<UINT8> data(textureSize);
    UINT8* pData = &data[0];

    //Load image
        IWICImagingFactory * pFactory = nullptr;
        IWICBitmapDecoder * pDecoder = nullptr;
        IWICBitmapFrameDecode * pSource = nullptr;
        IWICStream * pStream = nullptr;
        IWICFormatConverter * pConverter = nullptr;
        IWICBitmapScaler * pScaler = nullptr;
        UINT width = 0;
        UINT height = 0;
        WICPixelFormatGUID pixelFormat;
        DXGI_FORMAT format;

        hr = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pFactory));


        //Open the file - Make sure the file is in the same diretory


        hd = CreateFile2(L"1.jpg", GENERIC_READ, FILE_SHARE_READ, OPEN_EXISTING, nullptr);

        pFactory->CreateDecoderFromFilename(L"1.jpg", nullptr, GENERIC_READ, WICDecodeMetadataCacheOnLoad, &pDecoder);

        //Get the first frame (useful for cases when the image is gif)
        pDecoder->GetFrame(0, &pSource);

        //Create a converter
        pFactory->CreateFormatConverter(&pConverter);

        //Use the converter to convert image to 32ppPBGRA
        hr = pConverter->Initialize(pSource, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.f, WICBitmapPaletteTypeMedianCut);

        hr = pSource->GetSize(&width, &height);

        assert((width > 0) && (height > 0));

        WICRect rt = { 0 };
        rt.Height = height;
        rt.Width = width;

        //Use CopyPixels to copy the pixels to the new array
        UINT bSize = 0;

       // UINT rowPitch1 = 4 * width;
        UINT stride = 0; // stride  = bytes per row
                         // (width * (bpp/8)) + padding and round to the nearest DWORD
        stride = ((((width * 32) + 31) & ~31) >> 3);
        bSize = height*stride;

        BYTE *pixels = new BYTE[height*stride]();

        pSource->CopyPixels(&rt, stride, bSize, pixels);

       //My image is small, bSize is less than textureSize, so I'm filling upto bSize
        for (UINT n = 0; n < bSize; n += TexturePixelSize)
       {

        //pData[n] = pixels[n];
        //memcpy(&pData[n], &pixels[n], sizeof(BYTE)*4);
         
        pData[n] = pixels[n];   // R
        pData[n + 1] = pixels[n+1];// G
        pData[n + 2] = pixels[n+2];// B
        pData[n + 3] = pixels[n+3];// A
       

    }
    delete[] pixels;


    return data;
}

I've loaded an image of 256 * 256 pixels size to match with the sample's requirement. But still I'm seeing the same problem.

See the attachment for the output.

Advertisement

JPEG is a 24 bit per pixel, RGB data format that doesn't support an alpha channel. Try changing everything to 24 bits per pixel and see if it works then.

This topic is closed to new replies.

Advertisement