Image Rotation in a C++ Method

I had posted before on Stack Overflow for the question how exactly to rotate a BMP image in a c++ program. Now, however, I have more to show in regards of my progress.

I was wondering how (or why) my program will not output the image after I do the image calculation:

void BMPImage::Rotate45Left(float point1, float point2, float point3)
{
  float radians = (2 * 3.1416*45) / 360;

  float cosine = (float)cos(radians);
  float sine = (float)sin(radians);

  float point1Xtreme = 0;
  float point1Yearly = 0;
  float point2Xtreme = 0;
  float point2Yearly = 0;
  float point3Xtreme = 0;
  float point3Yearly = 0;

int SourceBitmapHeight = m_BIH.biHeight;
int SourceBitmapWidth = m_BIH.biWidth;

point1Xtreme = (-m_BIH.biHeight*sine);
point1Yearly = (m_BIH.biHeight*cosine);
point2Xtreme = (m_BIH.biWidth*cosine - m_BIH.biHeight*sine);
point2Yearly = (m_BIH.biHeight*cosine + m_BIH.biWidth*sine);
point3Xtreme = (m_BIH.biWidth*cosine);
point3Yearly = (m_BIH.biWidth*sine);

float Minx = min(0, min(point1Xtreme, min(point2Xtreme, point3Xtreme)));
float Miny = min(0, min(point1Yearly, min(point2Yearly, point3Yearly)));
float Maxx = max(point1Xtreme, max(point2Xtreme, point3Xtreme));
float Maxy = max(point1Yearly, max(point2Yearly, point3Yearly));

int FinalBitmapWidth = (int)ceil(fabs(Maxx) - Minx);
int FinalBitmapHeight = (int)ceil(fabs(Maxy) - Miny);
FinalBitmapHeight = m_BIH.biHeight;
FinalBitmapWidth = m_BIH.biWidth;
int finalBitmap;

If anyone has any helpful pointers, that would be great. I should mention that:

  • I can't use other outside libraries for the purpose of this program
  • It is a small image processing program, which has a menu system

Answers


Image transformation is usually done by projecting a target pixel onto a source pixel then calculating the value for that target pixel. This way you can easily incorporate different interpolation methods.

template <typename T>
struct Image {
    Image(T* data, size_t rows, size_t cols) : 
        data_(data), rows_(rows), cols_(cols) {}
    T* data_;
    size_t rows_;
    size_t cols_;
    T& operator()(size_t row, size_t col) {
        return data_[col + row * cols_];
    }
 };

template <typename T>
T clamp(T value, T lower_bound, T upper_bound) {
    value = std::min(std::max(value, lower_bound), upper_bound);
}

void rotate_image(Image const &src, Image &dst, float ang) {
    // Affine transformation matrix 
    // H = [a, b, c]
    //     [d, e, f]

    // Remember, we are transforming from destination to source, 
    // thus the negated angle. 
    float H[] = {cos(-ang), -sin(-ang), dst.cols_/2 - src.cols_*cos(-ang)/2,
                 sin(-ang),  cos(-ang), dst.rows_/2 - src.rows_*cos(-ang)/2}; 


    for (size_t row = 0; row < dst.rows_; ++row) {
       for (size_t col = 0; col < dst.cols_; ++cols) {
           int src_col = round(H[0] * col + H[1] * row + H[2]);
           src_col = clamp(src_col, 0, src.cols_ - 1);
           int src_row = round(H[3] * col + H[4] * row + H[5]);
           src_row = clamp(src_row, 0, src.rows_ - 1);               

           dst(row, col) = src(src_row, src_col);
       }
    }
}

The above method rotates an image with an arbitrary angle and uses nearest-neighbour interpolation. I typed it directly into stackoverflow, so it is full of bugs; nonetheless, the concept is there.


Need Your Help

Find duplicates lines based on some delimited fileds on line

duplicates field ksh partial lines

I have a file with lines having some fields delimited by "|".

What are the cons of using a contentEditable div rather than a textarea?

javascript html css contenteditable

Would I be shooting myself in the foot by using a div with attribute contentEditable="true" as a text field rather than a textarea?