Ejemplo n.º 1
0
            public Bitmap Draw(double[] samples)
            {
                int width  = samples.Length / _nfft;
                int height = _nfft / 2;
                var bmp    = new FastBitmap(new Bitmap(width, height));

                bmp.LockImage();
                for (int x = 0; x < width; x++)
                {
                    // process 2 segments offset by -1/4 and 1/4 fft size, resulting in 1/2 fft size
                    // window spacing (the minimum overlap to avoid discarding parts of the signal)
                    ProcessSegment(samples, (x * _nfft) - (x > 0 ? _nfft / 4 : 0), _magnitude1);
                    ProcessSegment(samples, (x * _nfft) + (x < width - 1 ? _nfft / 4 : 0), _magnitude2);

                    // draw
                    for (int y = 0; y < height; y++)
                    {
                        int colorIndex = _mapper.Map((_magnitude1[y] + _magnitude2[y]) / 2.0);
                        bmp.SetPixel(x, height - y - 1, _palette[colorIndex]);
                    }
                }
                bmp.UnlockImage();
                return(bmp.GetBitmap());
            }
Ejemplo n.º 2
0
        private static int GenerateBitmap(FastBitmap bmp, byte[] buf, List <Color> fourColors)
        {
            int w            = bmp.Width;
            int h            = bmp.Height;
            int nibbleOffset = 0;
            var nibbleEnd    = buf.Length * 2;
            var x            = 0;
            var y            = 0;

            for (; ;)
            {
                if (nibbleOffset >= nibbleEnd)
                {
                    return(-1);
                }

                var v = GetNibble(buf, nibbleOffset++);
                if (v < 0x4)
                {
                    v = (v << 4) | GetNibble(buf, nibbleOffset++);
                    if (v < 0x10)
                    {
                        v = (v << 4) | GetNibble(buf, nibbleOffset++);
                        if (v < 0x040)
                        {
                            v = (v << 4) | GetNibble(buf, nibbleOffset++);
                            if (v < 4)
                            {
                                v |= (w - x) << 2;
                            }
                        }
                    }
                }

                var len = v >> 2;
                if (len > w - x)
                {
                    len = w - x;
                }

                var color = v & 0x03;
                if (color > 0)
                {
                    var c = fourColors[color];
                    bmp.SetPixel(x, y, c, len);
                }

                x += len;
                if (x >= w)
                {
                    y++;
                    if (y >= h)
                    {
                        break;
                    }

                    x             = 0;
                    nibbleOffset += (nibbleOffset & 1);
                }
            }
            return(0);
        }