An uncompressed 24-bit .bmp file

I need to rotate a .bmp file given a rotation multiple of 90. For example, i have an image and im given a +90 or -90 rotation factor. my image will rotate 90 degrees to the left or to the right according to the rotation factor. my program works fine when the dimensions of the file are equal, meaning height & width are equal, but when i do it with an image thats not a square i get seg errors.

this is the code that i have so far.

if(rotation == 90 || rotation == -270 )
{
    /* 90 = -270 */
    for(row = 0; row < rows; row++)
    {
        for(col = 0; col < cols; col++ )
        {
            PIXEL* o = original+ (row*cols) + col;
            PIXEL* n = (*new)+((cols-col-1)*cols) + row;
            *n = *o;
        }
    }
    *newcols = cols;
    *newrows = rows;

the header of this method is:

int rotate(PIXEL* original, int rows, int cols, int rotation,
   PIXEL** new, int* newrows, int* newcols)

where PIXEL* original contains the original .bmp file

rows and cols are obtained by a call to the method that read the .bmp file

rotation = is the rotation factor given by the user

有帮助吗?

解决方案

Here is your problem. You should be multiplying by rows and not cols here:

PIXEL* n = (*new)+((cols-col-1)*rows) + row;

You want to be multiplying by the width of a row in your new image, and that is the same as the number of rows in your original image.

Also, you should be swapping rows and cols here:

*newcols = rows;
*newrows = cols;

To rotate -90:

PIXEL* n = (*new)+(col*rows) + (rows-row-1);
*newcols = rows;
*newrows = cols;

To rotate 180:

PIXEL* n = (*new)+((rows-row-1)*cols) + (cols-col-1);
*newcols = cols;
*newrows = rows;

In general, the formula is:

PIXEL* n = (*new)+(newrow*newcols) + newcol;

You just need to figure out what how to determine newrow, newcols, and newcol from the previous unrotated BMP. Drawing pictures helps.

其他提示

It is a bit hard to speculate given only part of the code, but this line certainly seems wrong:

PIXEL* o = original+ (row*newCols) + col;

If newCols is the width of the newly created image as opposed to the original image, then this addressing would be wrong. Don't you mean to be doing the following instead?

PIXEL* o = original+ (row*cols) + col;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top