访问32位TBitmap中的像素

时间:2018-03-01 05:39:00

标签: png c++builder tbitmap c++builder-10.2-tokyo

我正在使用C ++ Builder 10.2 Tokyo处理VCL Windows桌面应用程序。

我有以下内容尝试从TBitmap对象中读出像素,以便进行进一步的基于像素的操作。但是,即使代码正确识别32位格式并相应地创建位图图像,我也无法使用TBitmap :: ScanLine读出正确的像素信息。

使用的图像不是黑色,但从pBMP读取的RGB值全部为零,Alpha值为6,17,34等。这似乎是损坏或未初始化的数据。

public class MainActivity extends AppCompatActivity {

GridView gridView;
GridAdapter gridAdapter;
ImageView fav;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);




    gridView=(GridView) findViewById(R.id.gridView1);

    // Adapter class
    gridAdapter=new GridAdapter(MainActivity.this);

    //initializing favourite icon
    fav=(ImageView)findViewById(R.id.favicon);
    gridView.setAdapter(gridAdapter);
    gridView.setFastScrollEnabled(true);

    // gridView setting click listener
    //goes another Activity
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent =new Intent(getApplicationContext(),SliderActivity.class);
            intent.putExtra("index",position);
            startActivity(intent);
        }
    });



}

public void onClickViaMain(View v){
    if (fav.isPressed()){
        Toast.makeText(getApplicationContext(),"Added to Favourites",Toast.LENGTH_SHORT).show();
    }

}

Pixel的定义如下:

//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());
  }
}

0 个答案:

没有答案