如何在 Assembly 中转置图像?

发布于 2024-09-02 03:19:08 字数 126 浏览 12 评论 0原文

我正在开发一个项目,需要根据图像的行和列进行计算。获取图像行的位很容易。但是,要获取每列的位,我需要转置图像,使列变成行。

我使用 BMP 图片作为输入。 BMP图片有多少行X列?如果可能的话,我也想看看伪代码或其他东西。

I'm working on a project and I need to compute something based on the rows and columns of an image. It's easy to take the bits of the rows of the image. However, to take the bits of each column I need to transpose the image so the columns become rows.

I'm using a BMP picture as the input. How many rows X columns are in BMP picture? I'd like to see a pseudocode or something if possible too.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神经暖 2024-09-09 03:19:08

听起来您想要执行矩阵转置,这与旋转有点不同。在旋转中,行可能会变成列,但根据旋转方向,行或列的顺序将相反。转置保持行和列的原始顺序。

我认为使用正确的算法比使用汇编还是 C 语言更重要。旋转 90 度或转置实际上归结为只是移动内存。如果您使用这样的简单算法,最需要考虑的是缓存未命中的影响:

for(int x=0; x<width; x++)
{
    for(y=0; y<height; y++)
        out[x][y] = in[y][x];
}

这将导致大量缓存未命中,因为您在内存中频繁跳转。使用基于块的方法更有效。谷歌搜索“缓存高效矩阵转置”。

您可能能够获得一些收益的一个地方是使用 SSE 指令一次移动多个数据。这些可以在汇编语言和 C 语言中找到。另请查看 此链接。大约一半的地方有一个关于计算快速矩阵转置的部分。

编辑:
我刚刚看到你的评论,你正在为装配课做这件事,所以你可能可以忽略我所说的大部分内容。我假设您在使用汇编时希望获得最佳性能。

It sounds like you are wanting to perform a matrix transpose which is a little different than rotation. In rotation, the rows may become columns, but either the rows or the columns will be in reverse order depending on the rotation direction. Transposition maintains the original ordering of the rows and columns.

I think using the right algorithm is much more important than whether you use assembly or just C. Rotation by 90 degrees or transposition really boils down to just moving memory. The biggest thing to consider is the effect of cache misses if you use a naive algorithm like this:

for(int x=0; x<width; x++)
{
    for(y=0; y<height; y++)
        out[x][y] = in[y][x];
}

This will cause a lot of cache misses because you are jumping around in the memory a lot. It is more efficient to use a block based approach. Google for "cache efficient matrix transpose".

One place you may be able to make some gains is using SSE instructions to move more than one piece of data at a time. These are available in assembly and in C. Also check out this link. About half way down they have a section on computing a fast matrix transpose.

edit:
I just saw your comment that you are doing this for a class in assembly so you can probably disregard most of what I said. I assumed you were looking to squeeze out the best performance since you were using assembly.

狼亦尘 2024-09-09 03:19:08

它有所不同。 BMP 可以具有任意大小(最多有限制),并且也可以采用不同的格式(32 位 RBG、24 位 RBG、16 位调色板、8 位调色板、1 位单色)等。

与大多数其他问题一样,最好首先使用您选择的高级语言编写解决方案,然后根据需要将部分或全部转换为 ASM。

但是,是的,对于此任务来说,以最简单的形式(即 32 位 RGB 格式)旋转 90 度的倍数就像旋转 2-D 数组一样。

It varies. BMPs can have any size (up to a limit), and they can be in different formats too (32-bit RBG, 24-bit RBG, 16-bit paletted, 8-bit paletted, 1-bit monochrome), and so on.

As with most other problems, it's best to write a solution first in the high-level language of your choice and then convert parts or all of it to ASM as needed.

But yes, in its simplest form for this task, which would be the 32-bit RGB format, rotating with some multiple of 90 degress will be like rotating a 2-D array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文