1D array for Bitmap

4

I've been working on an FPGA project that sends an array (1D) of Bytes from a grayscale image to the PC. Well, I made a simple code with some image to simulate the sending and receiving of an array and the error remained:

System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    Bitmap ^bmpGray = gcnew Bitmap("C:\users\riacho\pictures\TestGray.jpg");         
    BitmapData^ bmGrayData = bmpGray->LockBits( Rectangle(0,0,bmpGray->Width,bmpGray->Height), ImageLockMode::ReadWrite, bmpGray->PixelFormat );

    array<Byte> ^arrayBmpGray = gcnew array<Byte>(bmpGray->Width * bmpGray->Height);

    Marshal::Copy( bmGrayData->Scan0, arrayBmpGray, 0, arrayBmpGray->Length);

    Bitmap ^bmpNewGray = gcnew Bitmap(bmpGray->Width, bmpGray->Height, PixelFormat::Format8bppIndexed);
    BitmapData^ bmpNewGrayData = bmpNewGray->LockBits(Rectangle(0,0,bmpGray->Width, bmpGray->Height), ImageLockMode::ReadWrite, PixelFormat::Format8bppIndexed );

    Marshal::Copy(arrayBmpGray, 0, bmpNewGrayData->Scan0, arrayBmpGray->Length);

    bmpGray->UnlockBits(bmGrayData);
    bmpNewGray->UnlockBits(bmpNewGrayData);

    pictureBox1->Image = bmpGray;
    pictureBox2->Image = bmpNewGray;
}

Output 1:

Aftersearchingalot,InoticedthatchangingPixelFormat::Format8bppIndexedtoPixelFormat::Format24bppRgb,thatis,treatingthecodeandformatasacolorimage,theproblemwassolved:

System::Voidbutton1_Click(System::Object^sender,System::EventArgs^e){Bitmap^bmpGray=gcnewBitmap("C:\users\riacho\pictures\TestGray.jpg");         
    BitmapData^ bmGrayData = bmpGray->LockBits( Rectangle(0,0,bmpGray->Width,bmpGray->Height), ImageLockMode::ReadWrite, bmpGray->PixelFormat );

    array<Byte> ^arrayBmpGray = gcnew array<Byte>(bmpGray->Width * bmpGray->Height);

    Marshal::Copy( bmGrayData->Scan0, arrayBmpGray, 0, arrayBmpGray->Length);

    array<Byte> ^arraybmpGrayCpy = gcnew array<Byte>(bmpGray->Width * bmpGray->Height * 3);

    int c = 0;
    for(int i = 3; i<arraybmpGrayCpy->Length; i += 3)
    {
     arraybmpGrayCpy[i-1] = arrayBmpGray[c];
     arraybmpGrayCpy[i-2] = arrayBmpGray[c];
     arraybmpGrayCpy[i-3] = arrayBmpGray[c];
     c++;
    }

    Bitmap ^bmpNewGray = gcnew Bitmap(bmpGray->Width, bmpGray->Height, PixelFormat::Format24bppRgb);
    BitmapData^ bmpNewGrayData = bmpNewGray->LockBits(Rectangle(0,0,bmpGray->Width, bmpGray->Height), ImageLockMode::ReadWrite, PixelFormat::Format24bppRgb );

    Marshal::Copy(arraybmpGrayCpy, 0, bmpNewGrayData->Scan0, arraybmpGrayCpy->Length);

    bmpGray->UnlockBits(bmGrayData);
    bmpNewGray->UnlockBits(bmpNewGrayData);

    pictureBox1->Image = bmpGray;
    pictureBox2->Image = bmpNewGray;
}

I am new to programming and would like to know for the first code what is missing, there must be some property that I do not know, because the second code is looking like a "gambiarra" because there is an adaptation to work.

    
asked by anonymous 27.04.2016 / 03:34

1 answer

2

The indexing table ( palette ) was missing, the problem was solved by adding the code:

ColorPalette ^palette = bmpNewGray->Palette;
array<Color> ^entries = palette->Entries;
for(int i = 0; i < 256; i++)
    entries[i] = Color::FromArgb(i,i,i);
bmpNewGray->Palette = palette;

Output:

    
03.05.2016 / 04:19