array 1D for Bitmap

Asked

Viewed 114 times

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 wrote a simple code with some image to simulate sending and receiving 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;
}

Exit 1:

Saída

After doing a lot of digging, I realized that by changing the PixelFormat::Format8bppIndexed for PixelFormat::Format24bppRgb, that is, treating the code and format as a color image the problem has been solved:

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);

    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 I 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.

  • What programming language is it? Put the language tag on the question, so it helps the community find it. :)

  • Another thing, indexed is not colored (at least in other languages/environments). The difference is that you use your own scale for color mapping. RGB, on the other hand, is already the "common" mapping (usually with values from 0 to 255 for each of the three bands). I do not know absolutely nothing FPGA, but if vc will use indexed, maybe missed you set the indexing table (sometimes also called Palette).

  • Further information (in English): https://en.wikipedia.org/wiki/Indexed_color

  • 1

    The language is c++/cli and I use visual studio, I tried to tag the programming language, but it is the first time I put something and by the site criteria only above 300 points to allow the tag: c++/cli. I really appreciate your help, I’ll do some research.

  • Not at all. I added the tag. :)

1 answer

2


The index table was missing (Palette), 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;

Exit:

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.