I'm working on a VCL Windows desktop application using C++Builder 10.2 Tokyo.
I have the following to try to read out pixels from a TBitmap object in order to make further pixel-based manipulations. However, even though the codes recognizes 32bit format correctly and creates the bitmap image accordingly, I couldn't read out correct pixel info using TBitmap::ScanLine.
The image used is not black, but the RGB values read from pBMP are all of zero and Alpha values are 6, 17, 34, ect. which seems as corrupted or uninitialized data.
//given png image saved as application resource
std::unique_ptr<TResourceStream> pRes(new TResourceStream(
reinterpret_cast<NativeUInt>(HInstance),
L"a_png_resource_name",
RT_RCDATA));
std::unique_ptr<TPngImage> pPNG(new TPngImage());
pPNG->LoadFromStream(pRes.get());
std::unique_ptr<TBitmap> pBMP(new TBitmap());
pBMP->Assign(pPNG.get());
pBMP->SaveToFile(L"resulting_image.bmp"); //it creates bmp with correct RGBA content
assert(pBMP->PixelFormat == pf32bit); //just confirm we have correct format to access pixels
for (int i = 0; i < pBMP->Height; i++) {
Pixel* FirstPixelInLine = (Pixel*)pBMP->ScanLine[i];
for (int j = 0; j < pBMP->Width; j++) {
Pixel* p = FirstPixelInLine + j;
OutputDebugString((L"R: " + std::to_wstring(p->Red) +
L" G: " + std::to_wstring(p->Green) +
L" B: " + std::to_wstring(p->Blue) +
L" A: " + std::to_wstring(p->Alpha)).c_str());
}
}
And the definition of Pixel is as follows:
struct Pixel {
BYTE Blue;
BYTE Green;
BYTE Red;
BYTE Alpha;
};