다음 두 가지 루틴을 고려하십시오.int [,] 변환의 비트 맵
//Tested
///Working fine.
public static Bitmap ToBitmap(int [,] image)
{
int Width = image.GetLength(0);
int Height = image.GetLength(1);
int i, j;
Bitmap bitmap = new Bitmap(Width, Height);
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, Width, Height),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* address = (byte*)bitmapData.Scan0;
for (i = 0; i < bitmapData.Height; i++)
{
for (j = 0; j < bitmapData.Width; j++)
{
// write the logic implementation here
address[0] = (byte)image[j, i];
address[1] = (byte)image[j, i];
address[2] = (byte)image[j, i];
address[3] = (byte)255;
//4 bytes per pixel
address += 4;
}//end for j
//4 bytes per pixel
address += (bitmapData.Stride - (bitmapData.Width * 4));
}//end for i
}//end unsafe
bitmap.UnlockBits(bitmapData);
return bitmap;// col;
}
//Tested
///Working fine.
public static int[,] ToInteger(Bitmap bitmap)
{
int[,] array2D = new int[bitmap.Width, bitmap.Height];
BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadWrite,
PixelFormat.Format32bppRgb);
unsafe
{
byte* address = (byte*)bitmapData.Scan0;
int paddingOffset = bitmapData.Stride - (bitmap.Width * 4);//4 bytes per pixel
for (int i = 0; i < bitmap.Width; i++)
{
for (int j = 0; j < bitmap.Height; j++)
{
byte[] temp = new byte[4];
temp[0] = address[0];
temp[1] = address[1];
temp[2] = address[2];
temp[3] = address[3];
array2D[j, i] = BitConverter.ToInt32(temp, 0);
//4-bytes per pixel
address += 4;//4-channels
}
address += paddingOffset;
}
}
bitmap.UnlockBits(bitmapData);
return array2D;
}
이 두 루틴은 32bpp 이미지에 잘 작동합니다. 이러한 루틴은 픽셀 형식이 PixelFormat.Format32bpp
으로 설정된 경우에만 작동합니다. PixelFormat.Format8bppIndexed
을 사용하면 예외가 발생합니다. 나는 32 비트 Bitmap
이하는 것을 그레이 스케일에게 int[,]
가 다시 변환 될 때마다 변환해야합니다 (또한, 내가 byte
때문에 주소 계산 문제의 int
사이의 원활한 전환을 달성 할 수있다) 그 예외를 방지하기 위해
Bitmap
에 이 문제를 해결하고 싶습니다.
Bitmap grayscale = Grayscale.ToGrayscale(InputImage);
//Here, the Bitmap is treated as a 32bit image
//to avoid the exception eventhough it is already
//an 8bpp grayscale image.
int[,] i1 = ImageDataConverter.ToInteger(grayscale);
Complex[,] comp = ImageDataConverter.ToComplex(i1);
int[,] i2 = ImageDataConverter.ToInteger(comp);
Bitmap b2 = ImageDataConverter.ToBitmap(i2);
//It is already a Grayscale image.
//But, the problem is, b2.PixelFormat is set to
//PixelFormat.Formap32bpp because of those routines.
//Hence the unnecessay conversion.
b2 = Grayscale.ToGrayscale(b2);
8bpp 색인화 된 (회색 음영) 이미지에서만 작동하도록 수정해야합니다.
어떻게하면됩니까?
무슨 작업을 원하는가 : 이미지를 저장하면 반대 논리를 수행해야합니다? 'int [,]'대신'byte [,]'를 전달할 수 있기를 원하십니까? 그렇지 않다면, int [,]는 어떻게 해석 될 것인가? 각 32 비트 값은 0에서 255 사이의 값을 저장하나요? 당신이 글을 쓸 때 색인이 실제로 만들어 집니까? 그렇다면 어떤 팔레트를 사용해야합니까? 아니면 그들이 정말로 회색 음영입니까? 각 32 비트 값에 4 개의 픽셀이 압축됩니까? 어느 쪽이든, 8 비트 값은 어떻게 해석 될까요? 32bpp 이미지를 출력 하시겠습니까, 아니면 출력도 8bpp이어야합니까? 사양의 모든 중요한 부분을 거의 생략했습니다. –