none
raw file 轉 bmp問題 RRS feed

  • 問題

  • 各位好,想請教下問題,感謝

    目前需要做RAW檔轉BMP,RAW檔有(Raw8 or Raw10 or Raw12 or Raw14 ...)

    這是我的code:

    public void ReadImage(out byte[] Data, string path, uint Width, uint Height,uint BPP)
    {
            byte[] fileData = null;
            if (!File.Exists(path))
            {
                throw new FileNotFoundException(path);
            }
    
            fileData = File.ReadAllBytes(path);
            var bytesPerPixel = (BPP + 7) / 8;
            var dataSize = Width * Height * bytesPerPixel;
            Data = new byte[Width * Height * bytesPerPixel];
            var sdata = new short[dataSize / 2];
            if (bytesPerPixel == 1)
            {
               for (int i = 0, shortIndex = 0; i < dataSize; i += 2, shortIndex++)
               {
                CopyBytesToShort(fileData[i], fileData[i + 1], out sdata[shortIndex], (int)BPP, false);
               }
            }
            if (bytesPerPixel == 2)
            {
               for (int i = 0, shortIndex = 0; i < dataSize; i += 2, shortIndex++)
               {
                CopyBytesToShort(fileData[i],fileData[i + 1], out sdata[shortIndex], (int)BPP, true);
               }
            }
            Buffer.BlockCopy(sdata, 0, Data, 0, Data.Length);
            int w = Convert.ToInt32(Width);
            int h = Convert.ToInt32(Height);
            unsafe
            {
                fixed (byte* ptr = Data)
                {
                    using (var bmp = new Bitmap(w / 4, h / 4, w * 4, PixelFormat.Format32bppRgb, new IntPtr(ptr)))
                    {
    
                        Bitmap dmybp = new Bitmap(bmp, new Size(w, h));
                        dmybp.Save(@"RAW.bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                    }
                }
            }
        }
            private void CopyBytesToShort(byte byte1, byte byte2, out short retShort, int bitsPerPixel, bool isPerformShift)
        {
            short lsb, msb;
            lsb = byte1;
            msb = byte2;
            if (isPerformShift)
            {
                lsb <<= 16 - bitsPerPixel;
                msb <<= (24 - bitsPerPixel);
            }
            else
            {
                msb <<= 8;
            }
            retShort = (short)(msb | lsb);
        }

    執行上面程式時~用RAW8-bit檔可以成功轉出圖

    但如果是跑不是RAW8-bit的檔案,像是Raw10 or Raw12 or Raw14...轉出來的圖片就會失敗

    想請教哪裡出問題?

    2019年1月24日 上午 06:53