How to efficently divide big 2d array into small 2d arrays?

  • Thread starter Thread starter Macin
  • Start date Start date
M

Macin

Hi,

How to efficently divide big array into small arrays as showed below?

IN

xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx ==>> xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxx xxxxx xxxxx

Here is a piece of code I`ve done but it takes a lot of calculations for
bigger bitmaps... Is there any posibility to optimize it to make it faster?
The task of this method is to copy a bitmap into 8x8 square 2d arrays and
return a list of those 2d arrays

public ArrayList podzielNaKwdaraty(Bitmap obraz, char aKolor)
{
ArrayList wartosciRGB = new ArrayList();
for (int l=0;l<wys;l+=8)
{
for (int k=0;k<szer;k+=8)
{
byte[,] temp = new byte[8,8];
for (int i = 0; i<8; i++)
{
for (int j = 0; j<8; j++)
{
if (i+l>obraz.Height-1 || j+k>obraz.Width-1)
temp[i,j]=0;
else
{
if (aKolor == 'R')
temp[i,j]=m_bitmap.GetPixel(j+k,i+l).R;
else if (aKolor == 'G')
temp[i,j]=m_bitmap.GetPixel(j+k,i+l).G;
else if (aKolor == 'B')
temp[i,j]=m_bitmap.GetPixel(j+k,i+l).G;
}
}
}
wartosciRGB.Add(temp);
}
}
return wartosciRGB;
}
 
Macin said:
How to efficently divide big array into small arrays as showed below?

IN

xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx ==>> xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxxxxxxxxxxx xxxxx xxxxx xxxxx
xxxxx xxxxx xxxxx

Here is a piece of code I`ve done but it takes a lot of calculations for
bigger bitmaps... Is there any posibility to optimize it to make it faster?

Well, the first thing to *try* would be to effectively have three
different methods - one for red, one for green and one for blue. There
seems little point in doing the same comparison for every pixel when
the result will always be the same. The JIT *may* be optimising this
already, but it's worth a try.
 
What you mean as JIT?

Uzytkownik "Jon Skeet said:
Well, the first thing to *try* would be to effectively have three
different methods - one for red, one for green and one for blue. There
seems little point in doing the same comparison for every pixel when
the result will always be the same. The JIT *may* be optimising this
already, but it's worth a try.
 
Macin said:
Hi,

How to efficently divide big array into small arrays as showed below?
Here is a piece of code I`ve done but it takes a lot of calculations for
bigger bitmaps... Is there any posibility to optimize it to make it faster?

DISCLAIMER: I know nothing about the graphics-libraries of .NET. You can
probably get much better performance by changing representation than by
anything else.

If GetPixel is the only way you can index the Bitmap, then you do need
x*y invocations of it.

For a small thing, you could try to move the branching on aKolor outside
the loop to see if the compiler/JIT is doing the possible code-hoisting
on that.

It would probably be even better to rearrange the code to use just x*y
invocations instead of 3*x*y of GetPixel by making all of the R,G and B
byte[] for one pixel at the same time.
The task of this method is to copy a bitmap into 8x8 square 2d arrays and
return a list of those 2d arrays

If interactivity is the problem, you can return an adapter with an IList
interface instead. Passing an adapter will distribute the cost of the
conversion over the access to it, and won't reqire copying.
 
Back
Top