帮助与位图德codeR [英] Help with Bitmap decoder

查看:131
本文介绍了帮助与位图德codeR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直工作在一个位图德codeR,但我的算法处理的像素数据似乎并不很正确:

 公众的IntPtr ReadPixels(流FS,诠释抵消,诠释的宽度,高度INT,INT BPP)
    {
        IntPtr的bBits;

        INT pixelCount = BPP *宽*高;
        INT行= 0;
        十进制值=((BPP *宽)/ 32)/ 4;
        INT行大小=(int)的Math.Ceiling(值);
        INT ARRAYSIZE =行大小* Math.Abs​​(高);
        INT列= 0;
        字节[] BMPData =新的字节[ARRAYSIZE]

        BinaryReader在R = BinaryReader在新(FS);
        r.BaseStream.Seek(偏移,SeekOrigin.Begin);

        而(行<高)
        {
            字节ReadByte;

            如果((西>!=行大小))
            {
                ReadByte = r.ReadByte();
                BMPData [(行*行大小)+上校] = ReadByte;
                山口+ = 1;
            }

            如果(西> =行大小)
            {
                列= 0;
                行+ = 1;
            }
        }

        bBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(BMPData.Length);
        System.Runtime.InteropServices.Marshal.Copy(BMPData,0,bBits,BMPData.Length);

        返回bBits;
}
 

我只能处理单色位图和一些,位图的部分被处理好。都不是COM pressed,他们呈现倒过来,翻转左右。我真的可以做的这一块一定的帮助。

解决方案

 十进制值=((BPP *宽)/ 32)/ 4;
    INT行大小=(int)的Math.Ceiling(值);
 

这是不正确的。您的行大小的变量实际上是所谓的跨越。你计算它是这样的:

  INT字节=(宽* bitsPerPixel + 7)/ 8;
    INT跨距= 4 *((字节+ 3)/ 4);
 

I've been working on a bitmap decoder, but my algorithm for processing the pixel data doesn't seem to be quite right:

    public IntPtr ReadPixels(Stream fs, int offset, int width, int height, int bpp)
    {
        IntPtr bBits;

        int pixelCount = bpp * width * height;
        int Row = 0;
        decimal value = ((bpp*width)/32)/4;
        int RowSize = (int)Math.Ceiling(value);
        int ArraySize = RowSize * Math.Abs(height);
        int Col = 0;
        Byte[] BMPData = new Byte[ArraySize];

        BinaryReader r = new BinaryReader(fs);
        r.BaseStream.Seek(offset, SeekOrigin.Begin);

        while (Row < height)
        {
            Byte ReadByte;

            if (!(Col >= RowSize))
            {                       
                ReadByte = r.ReadByte();
                BMPData[(Row * RowSize) + Col] = ReadByte;
                Col += 1;                    
            }

            if (Col >= RowSize)
            {
                Col = 0;
                Row += 1;
            }
        }

        bBits = System.Runtime.InteropServices.Marshal.AllocHGlobal(BMPData.Length);
        System.Runtime.InteropServices.Marshal.Copy(BMPData, 0, bBits, BMPData.Length);

        return bBits;
}

I can process only monochrome bitmaps and on some, parts of the bitmap is processed fine. None are compressed and they are rendered upside down and flipped around. I really could do with some help on this one.

解决方案

    decimal value = ((bpp*width)/32)/4;
    int RowSize = (int)Math.Ceiling(value);

That isn't correct. Your RowSize variable is actually called "stride". You compute it like this:

    int bytes = (width * bitsPerPixel + 7) / 8;
    int stride = 4 * ((bytes + 3) / 4);

这篇关于帮助与位图德codeR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆