【问题标题】:What is the equivlent WIC version for this bitmap manipulating routine?此位图操作例程的等效 WIC 版本是什么?
【发布时间】:2014-06-17 02:34:29
【问题描述】:

我们有一些位图图像,它们实际上是RAW 格式,但它使用 Windows 位图格式,但是如果不使用相机提供的 lib 对其进行解码,则无法获得良好的结果。

以下是可以正常工作的代码:

    public static Bitmap DecodeImage(Bitmap raw_image)
    {
        if (raw_image == null || raw_image.PixelFormat != PixelFormat.Format8bppIndexed) 
            return null;

        BitmapData data_in, data_out;
        Bitmap rgb_image = new Bitmap(raw_image.Width, raw_image.Height, PixelFormat.Format24bppRgb);
        data_in = raw_image.LockBits(new Rectangle(0, 0, raw_image.Width, raw_image.Height), ImageLockMode.ReadWrite, raw_image.PixelFormat);
        data_out = rgb_image.LockBits(new Rectangle(0, 0, rgb_image.Width, rgb_image.Height), ImageLockMode.ReadWrite, rgb_image.PixelFormat);
        try
        {
            DecodeImageRaw(raw_image.Width, raw_image.Height, data_in.Scan0, data_out.Scan0);
            return rgb_image;
        }
        finally
        {
            raw_image.UnlockBits(data_in);
            rgb_image.UnlockBits(data_out);
        }
    }

这里DecodeImageRaw是一种使用相机库对图像进行解码的方法。以上对我们来说很好。

使用windows成像组件的相同代码的新版本:

    public static BitmapSource DecodeImage(BitmapSource raw_image)
    {
        if (raw_image == null || raw_image.Format != PixelFormats.Indexed8) 
            return null;

        var w = raw_image.PixelWidth;
        var h = raw_image.PixelHeight;
        var dx = raw_image.DpiX;
        var dy = raw_image.DpiY;
        var in_image = new WriteableBitmap(w, h, dx, dy, PixelFormats.Indexed8, raw_image.Palette);
        var out_image = new WriteableBitmap(w, h, dx, dy, PixelFormats.Rgb24, null);

        in_image.Lock();
        out_image.Lock();

        var bufin = in_image.BackBuffer;
        var bufout = out_image.BackBuffer;
        var s = in_image.BackBufferStride;
        try
        {
            raw_image.CopyPixels(Int32Rect.Empty, bufin, h*s, s);

            out_image.AddDirtyRect(new Int32Rect(0, 0, w, h));
            DecodeImageRaw(w, h, bufin, bufout);
            return out_image;
        }
        finally
        {
            in_image.Unlock();
            out_image.Unlock();
        }
    }

上面实际上输出了一个形状相同的图像,但是颜色不对(例如,背景应该是“蓝色”的天空,但它变成了棕色。

那么新代码有什么问题呢?如何使它与原来的等效?我也想知道原因的解释。

解码后的图像应该是这样的

但它是用这样的新代码解码的

编辑

自第一次编辑代码以来,进行了一些小的调整,以使其更易于阅读并排除一些不相关的细节。例如,有一个 bool 标志可以在算法之间进行选择,但现在已被删除。但是,行为保持不变。

【问题讨论】:

    标签: c# writeablebitmap wic


    【解决方案1】:

    您的频道倒数:RGB vs BGR

    我意识到了这种影响。为了确认它,我在 Photoshop 中打开了您的解码图像,并手动交换了 RedBlue 通道。然后我得到了你所期望的图像(不管是什么鬼):

    我不明白你的代码,但试试PixelFormats.Bgr24,而不是PixelFormats.Rgb24

    【讨论】:

    • 像素格式的 WIC 命名约定是按照组件在文件中出现的方式对组件进行排序(最不重要的在前),这与 GDI+ 的命名约定相反。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 2014-03-12
    • 1970-01-01
    • 2017-01-27
    相关资源
    最近更新 更多