我的代码可以正确旋转 bmp 图片,但前提是像素数是 4 的倍数……谁能看出哪里出了问题? [英] My code rotates a bmp picture correctly but only if the number of pixels is a muliple of 4... can anyone see whats wrong?

查看:29
本文介绍了我的代码可以正确旋转 bmp 图片,但前提是像素数是 4 的倍数……谁能看出哪里出了问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它只在宽度和高度是四的倍数时才有效,缺少什么?2x2... 4x4....16x16... 400x400... 一切正常,但如果它只是一个随机的 .bmp 图片就不行.

It only works when the width and height are multiples of four, whats missing? 2x2... 4x4....16x16... 400x400... all work but not if its just a random .bmp pic.

 void rotate90(Image& image)
    {
        Pixel * tempPixel = new Pixel[(image.infoHeader.biWidth * image.infoHeader.biHeight)];
        int tempWidth = image.infoHeader.biWidth;
        image.infoHeader.biWidth = image.infoHeader.biHeight;
        image.infoHeader.biHeight = tempWidth;
        for(int r = 0; r < image.infoHeader.biHeight; r ++)
        {
            for(int c = 0; c < image.infoHeader.biWidth; c++)
            {

                int f = c+(r*image.infoHeader.biWidth);
                int t = (image.infoHeader.biHeight-r-1) + (image.infoHeader.biWidth*c);
                tempPixel[f]=image.pixels[t];
            }
        }

        delete[] image.pixels;
        image.pixels=tempPixel;
    }

推荐答案

内部 for 循环应该是

The inner for loop should probably be

int f = c+(r*image.infoHeader.biHeight);
int t = (image.infoHeader.biWidth-r-1) + (image.infoHeader.biHeight*c);
tempPixel[f]=image.pixels[t];

要计算 [x,y] 处像素的平面阵列的索引,您需要使用 i=y+Y*xi=x+X*y 如果 YX 分别是宽度和高度.

To calculate the index for a flat array of a pixel at [x,y], you need to use i=y+Y*x or i=x+X*y if Y and X are the width and height, respectively.

这篇关于我的代码可以正确旋转 bmp 图片,但前提是像素数是 4 的倍数……谁能看出哪里出了问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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