Click to See Complete Forum and Search --> : C# color bitmap bil. scaling - problem with coding


dizhat
March 8th, 2005, 02:48 PM
Hi,
I am currently making my first App in C# (earlier had exp. with C++ Builder), and have problem with representing data , or as I can call it also, with gathering my bitmap in matrix back to image. I was trying to implement bilinear scaling of image (code attached) , and don't know why, but color avering doesn't work, i get the same data for all red, another same for all green and so is for blue...
I u mind, help me with writint this function so it does it's purpose...


using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace ScaleMe_
{

public class BitmapProcess

{
struct PixelColor
{
public byte red;
public byte blue;
public byte green;
};


//public long ABS(int a) { if (a>=0) return (a); else return -(a); }
//public long AVERAGE(int a, int b) { return (((((a) ^ (b)) & 0xfffefefeL) >> 1) + ((a) & (b))); }

public static bool Scale(Bitmap bmp, int oldX, int oldY, int newX, int newY)
{
int a,b,c,d;
PixelColor[,] myBitMatrix = new PixelColor[bmp.Width,bmp.Height];
PixelColor[,] myBitNewMatrix = new PixelColor[newX,newY];
Rectangle myRect = new Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData bmData = bmp.LockBits(myRect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
//here i count my constants for transform..
if ((newX-1)!=0 && (newY-1)!=0) {
a=((oldX - 1)/(newX - 1));
b=0;
d=((oldY - 1)/(newY - 1));
c=0;
}
else {
a=1;
b=1;
c=1;
d=1;
}
Console.WriteLine(a+" | " +b+ " | "+c + " | " +d);

int stride = bmData.Stride;
Console.WriteLine("STRIDE:"+stride);

System.IntPtr Scan0 = bmData.Scan0;

unsafe {
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - bmp.Width*3;
//creating matrix from bitmap
for(int y=0;y<bmp.Height;++y) {
for(int x=0; x<bmp.Width; ++x ) {
myBitMatrix[x,y].red=p[2];
myBitMatrix[x,y].green=p[1];
myBitMatrix[x,y].blue=p[0];
Console.WriteLine("x ,y - blue:"+x+"."+y+" :"+myBitMatrix[x,y].blue);
p += 3;
}
p += nOffset;
}
for (int i=0; i<newX; i++) {
double x = (double)i * a;
int i1 = (int)x;
double alpha = x - (double)i1;
if (i1 == oldX-1) {
i1 = oldX-2;
alpha = 1.0;
}
for (int j=0; j<newY; j++) {
double beta = (double)j * d;
int j1 = (int)beta;
double u = beta - (double)j1;
if (j1 == oldY-1) {
j1 = oldY-2;
beta = 1.0;
}

//bilinear interpolation ? doesn't work :( in myBitMatrixNew.red green blue are for all x,y same

//red
double g_interp_red = (1-alpha)*(1-beta)*(double) myBitMatrix[i1,j1].red+alpha*(1-beta)*(double) myBitMatrix[i1+1,j1].red
+ (beta)*(alpha)*(double) myBitMatrix[i1+1,j1+1].red+(1-alpha)*beta*(double) myBitMatrix[i1+1,j1+1].red
+ (1-alpha)*beta*(double) myBitMatrix[i1,j1+1].red;
myBitNewMatrix[i,j].red= (byte)g_interp_red;
//green
double g_interp_gr = (1-alpha)*(1-beta)*(double) myBitMatrix[i1,j1].green+alpha*(1-beta)*(double) myBitMatrix[i1+1,j1].green
+ (beta)*(alpha)*(double) myBitMatrix[i1+1,j1+1].green+(1-alpha)*beta*(double) myBitMatrix[i1+1,j1+1].green
+ (1-alpha)*beta*(double) myBitMatrix[i1,j1+1].green;
myBitNewMatrix[i,j].green= (byte)g_interp_gr;
//blue
double g_interp_b = (1-alpha)*(1-beta)*(double) myBitMatrix[i1,j1].blue+alpha*(1-beta)*(double) myBitMatrix[i1+1,j1].blue
+ (beta)*(alpha)*(double) myBitMatrix[i1+1,j1+1].blue+(1-alpha)*beta*(double) myBitMatrix[i1+1,j1+1].blue
+ (1-alpha)*beta*(double) myBitMatrix[i1,j1+1].blue;
myBitNewMatrix[i,j].blue= (byte)g_interp_b;
Console.WriteLine("i ,j - NewRed:"+i+"."+j+" :"+ myBitNewMatrix[i,j].red);
}
}
//what now ?
bmp.UnlockBits(bmData);
}
return true;
}

}