1

Is there any function which I can use in order to pad my QImage object? I have tried to search over the net unsuccefuly. Thx in advance.

Here is my code:

#include "mainwindow.h"
#include <QApplication>
#include "qimage.h"
#include <QImage>
#include <QLabel>
#include <QColor>
#include "qcolor.h"
#include <Qdebug>
#include <QGraphicsPixmapItem>
#include <QGraphicsScene>
#include <QGraphicsView>


int main(int argc, char *argv[])
{
    printf("Init!");
    qDebug() << "C++ Style Debug Message";
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    int height;
    int width;
    unsigned char *p, *p_begin;
    QImage img("C:\\Users\\Owner\\Pictures\\2013-09-26\\IMG_0836.JPG");
    height = img.height();
    width = img.width();
    p = (unsigned char *)malloc(height * width * sizeof(unsigned char));
    p_begin = p;

    qDebug() << "Begin For Loop";
    for (int row = 0; row < height; ++row)
    {
    for (int col = 0; col < width; ++col)
    {
        QColor clrCurrent( img.pixel( col, row ));
        *p = (unsigned char)((clrCurrent.green() * 0.587) + (clrCurrent.blue() * 0.114) + (clrCurrent.red() * 0.299));
        p++;
    }
}

qDebug() << "Finished First Loop!";
p = p_begin;
for (int row = 0; row < height; ++row)
{
    for (int col = 0; col < width; ++col)
    {
        QColor clrCurrent(img.pixel(col, row));
        clrCurrent.setBlue((int)(*p));
        clrCurrent.setGreen((int)(*p));
        clrCurrent.setRed((int)(*p));
        img.setPixel(col, row, clrCurrent.rgba());
        p++;
    }
}

QPixmap pixmap = QPixmap::fromImage(img);
QGraphicsPixmapItem *item = new QGraphicsPixmapItem(pixmap);
scene.addItem(item);
view.show();

return a.exec();
 }

Hi I edited my question, i added my code in order to give you more feeling of what is going on. any help would be appreciated.

8
  • What actually do you want to achieve?
    – Apin
    Mar 13, 2016 at 9:23
  • I want to pad my qimage with the same boudary pixels. Mar 13, 2016 at 9:36
  • Is that make sese? Mar 13, 2016 at 9:38
  • 1
    I mean, what do you want to do with the pad of the image? Do you want to paint it to what, use it on scene, label, or what? Or do you want to save it? What is the purpose of adding pad?
    – Apin
    Mar 13, 2016 at 9:42
  • 1
    @DanielEitan - just use an image editor and post an example of the desired result.
    – dtech
    Mar 13, 2016 at 12:36

1 Answer 1

1

The QImage offers no way to change its size. You need to create a new, larger image, erase its contents, start a QPainter on it, then draw the source image in the center of the new image. That way you'll have padding.

Below is a function that returns a padded version of the image, with a given color used for padding, and a test harness for it.

// https://github.com/KubaO/stackoverflown/tree/master/questions/image-pad-35968431
#include <QtGui>

template <typename T>
QImage paddedImage(const QImage & source, int padWidth, T padValue) {
  QImage padded{source.width() + 2*padWidth, source.height() + 2*padWidth, source.format()};
  padded.fill(padValue);
  QPainter p{&padded};
  p.drawImage(QPoint(padWidth, padWidth), source);
  return padded;
}

int main() {
   QImage source{64, 64, QImage::Format_ARGB32_Premultiplied};
   source.fill(Qt::red);
   auto padded = paddedImage(source, 16, Qt::blue);
   padded.save("test.png");
}

Output:

the generated padded image

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.