簡體   English   中英

將24位bmp轉換為16位?

[英]Convert 24-bit bmp to 16-bit?

我知道.NET Framework附帶了一個圖像轉換類(System.Drawing.Image.Save方法)。

但我需要將一個24位(R8G8B8)位圖圖像轉換為16位(X1R5G5B5),我真的不知道這種轉換,並且bmp頭中的24到16位更改不會'工作(因為我們需要轉換整個圖像數據)。

另外我想知道我是否可以控制圖像抖動等。

想法? 任何形式的幫助將不勝感激。

聲明了Format16bppRgb1555像素格式,但GDI +實際上並不支持它。 沒有使用該像素格式的主流視頻驅動程序或圖像編解碼器。 GDI +設計師猜到的東西可能已經發生了,他們的時間機器不夠准確。 否則,程序員可以使用System.Drawing進行相當粗糙的復制/粘貼。

Rgb555是最接近的硬件和編解碼器匹配:

public static Bitmap ConvertTo16bpp(Image img) {
    var bmp = new Bitmap(img.Width, img.Height,
                  System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
    using (var gr = Graphics.FromImage(bmp))
        gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
    return bmp;
}

您需要使用指定顏色深度的編碼器參數來保存位圖。

    myEncoder = Encoder.ColorDepth;
    myEncoderParameters = new EncoderParameters(1);

    // Save the image with a color depth of 24 bits per pixel.
    myEncoderParameter = new EncoderParameter(myEncoder, 24L);
    myEncoderParameters.Param[0] = myEncoderParameter;

    myBitmap.Save("MyBitmap.bmp", myImageCodecInfo, myEncoderParameters);

一種非常直接的方法是循環舊的位圖數據並將每對r8-g8-b8值轉換為x1-r5-g5-b5,類似於此函數:

char condense(char i)
{ 
  return (char)(i*255.0f/31.0f);
}

short transform(long input)// note that the last 8 bytes are ignored
{
  return condense(input&0xff) || (condense((input&0xff00)>>8)<<5)
    || (condense((intput&0xff0000)>>16)<<10);
}

// and somewhere in your program
{
  int len; // the length of your data in pixels
  char *data; // your data
  char *newdata; // this is where you store the new data; make sure it's allocated

  for(char *p=data; p<data+len*3; p+=3)
    *(short *)newdata=transform(*(long *)data);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM