GreyDolls
GreyDolls

Reputation: 1

QImage::pixel and QImage::setPixel coordinate out of range error

I am working on an image manipulation program and while the program I keep getting these messages, here are some examples but I am getting hundreds of them to the point where the program does not fully execute.

QImage::setPixel: coordinate (1043968,0) out of range
QImage::pixel: coordinate (1043968,0) out of range

I have looked at other questions and I can't seem to find the error in my code, unfortunately I have about 8 functions, but I think I narrowed it down to just one where there might be the issue. It is a function that is suppose to rotate the given images and I think that might be what is causing the error based on the other questions I have reviewed but I cannot see it. I would appreciate any help I can get, or if this function is fine how I could go about asking about the others without making 8 different posts, thank you!

    void makeRotate(QImage originalImage){
QImage inImage = originalImage;    // Copies the original image into a new QImage object.


int width = originalImage.width();
int height = originalImage.height();

//a double for loop

//first loop through the HEIGHT OF inImage  (width of newImage)
for (int i = 0; i < height; i++){
//loop through the WIDTH OF inImage  (height of newImage)
for (int j = 0; j < width; j++){
    //set the pixel
    inImage.setPixel(i , j,(new QColor (getRgbaPixel(i, j, originalImage).red()), (getRgbaPixel(i, j, originalImage).green()), (getRgbaPixel(i, j, originalImage).blue()), 255));
}
}

inImage.save("../Images/rotate.png");

}

Upvotes: 0

Views: 1673

Answers (1)

If you try to rotate 90 degrees, this is the code

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QtDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "/home",
                                                    tr("Images (*.png *.xpm *.jpg *.jpeg *.png)"));
    QImage image = QImage(fileName);
    makeRotate(image);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::makeRotate(QImage originalImage)
{
    QImage inImage = originalImage;    // Copies the original image into a new QImage object.


    int width = originalImage.width();
    int height = originalImage.height();

    QTransform rotate;
    rotate.rotate(90);
    inImage = inImage.transformed(rotate);


    //a double for loop

    //first loop through the HEIGHT OF inImage  (width of newImage)
    for (int i = 0; i < width; i++)
    {
        //loop through the WIDTH OF inImage  (height of newImage)
        for (int j = 0; j < height; j++)
        {
            //set the pixel
            QRgb rgb = originalImage.pixel(i, j);
            inImage.setPixel(j, i, (rgb));
        }
    }
    QString str = QFileDialog::getSaveFileName(this, tr("Open File"), fileName);
    inImage.save(str);

    qDebug() << inImage.size();
}

you need to make sure that the measurements are correct and your code works fine for copying the image if you change the width and height counters (like I did in the code above).

Upvotes: 1

Related Questions