How to copy the one QImage into another QImage? - qt

How to copy one QImage into another QImage?
I do not want to change the size of image
QImage src=function_name();
QImage dst;

You can use the QImage::copy() function. For example:
QImage src = function_name();
QImage dst = src.copy();

Related

Use QPixmap instead of QImage?

I have an application where I copy some raw image data into a QImage directly:
QImage* img = new QImage(desc.Width, desc.Height, QImage::Format_RGB32);
for (y = 0; y < img->height(); y++)
{
memcpy(img->scanLine(y), &rawData[y * pRes->RowPitch], pRes->RowPitch);
}
return img;
Later this QImage is drawn via a call
painter.drawPixmap();
Unfortunately drawPixmap() cannot handle a QImage directly, so it first has to be converted:
m_bgImage = new QPixmap();
m_bgImage->convertFromImage(image);
Due to timing reasons I would like to drop this additional conversion step.
Thus my question: are there any function in QPixmap that allow direct image data manipulation right as in QImage?
My idea would be to start with a QPixmap from the very beginning, copy the raw image data into the QPixmap object and then use it directly.
Thanks :-)
First of all you won't need that loop to create the QImage. You can:
QImage* img = new QImage(&rawData, desc.Width, desc.Height, pRes->RowPitch * 4, QImage::Format_RGB32);
Then you can
painter.drawImage(QPointF(0,0),*img);
If there is any specific reason to use QPixmap (like QPixmap caching) you will have no other choice than convert it to QPixmap first.

Convert jpeg image to thumbnail image in Qt

A QImage needs to be finally shown as a thumbnail image in a QLabel. In between it's serialized in QByteArray and sent over the network. So, can we convert the QImage to QByteArray for thumbnail? Basically I want to avoid the QImage::scale() method as it's quite CPU consuming.
Like QImage converted to a QByteArray which will hold data for thumbnail images.
On this case you cannot avoid QImage::scale. Why do you want?
This is the answer to your question how to save QImage to QByteArray.
QFile file(imagePath);
if (file.open(QIODevice::ReadOnly)) {
QByteArray bytes;
QImage img;
img = img.fromData(file.readAll());
img = img.scaled(200, 100, Qt::IgnoreAspectRatio, Qt::FastTransformation);
SetLabelImage(img);
QBuffer buffer(&bytes);
img.save(&buffer, "PNG");
}

load image from char buffer

I need to construct an image with unsigned char data I receive from a compressed/decompressed image. For this, I just wrote a simple program to test buffer loading from image and vice versa. As I run the code, I cannot setpixmap the image to the background.
void MainWindow::LoadImage()
{
//======== Load buffer from image
unsigned char buffer[_width*_height*COLOR_COMPONENTS]; //1024 * 768 * 3
QImage image;
image.load("://image.jpg", "JPEG");
memcpy(buffer, image.bits(), _width*_height*COLOR_COMPONENTS);
//========= Load image from buffer
QImage img;
img.loadFromData((const char*)buffer);
QPixmap px = QPixmap::fromImage(img);
ui->label->setPixmap(px);
}
UPDATED:
I changed the code to this, however, I get segmentation fault with memcpy.
unsigned char buffer[400*300*3];
QImage image(_width, _height, QImage::Format_RGB32);
image.load("://image.jpg", "JPEG");
memcpy(buffer, image.bits(), 400*300*3);
QImage img(400, 300, QImage::Format_RGB32);
img.loadFromData((const uchar*)buffer, sizeof(buffer)/sizeof(char), "JPG");
QPixmap px = QPixmap::fromImage(img);
ui->label->setPixmap(px);
loadFromData requires the data to be in a certain format (PNG, JPG...), the plain array is not a valid one
however doing
QImage img(buffer,_width, _height, QImage::Format_RGB888);
will return a image of the correct size and format but will only be valid for as long as buffer is alive

Passing image to QPixMap not as path in QT

Is there a way to pass an image as a name not as a path to Qpixmap in QT(C++),, for example i have the following code in which the processed image should be displayed using Qpixmap label but when i tried that i have to save it and then to pass it to Qpixmap to be displayed,,, but it's not an efficient way so could anybody help me please i'm new to Qt and have no experience..
void MainWindow::on_pushButton_3_clicked()
{
cv::Mat hsv_img, seg_img,infected_area, hsv_infected,seg_input,hsv_seg;
Mat filter_img,hsv;
cv::Mat input_image = imread(this->file_name.toAscii().data());
medianBlur(input_image,filter_img,7);
cvtColor(filter_img, hsv, CV_BGR2HSV);
hsvSeg(filter_img,hsv, hsv_img, seg_img,14.0,0.0,117.0,255.0,133.0,179.0);//call hsv segmentation function to segment leaf
cvtColor(seg_img, hsv_seg, CV_BGR2HSV);
hsvSeg(seg_img,hsv_seg, hsv_infected, infected_area,0.09*255,0.01*255,0.1*255,0.14*255,1.0*255,1.0*255);//call hsv segmentation to segment infected areas
imwrite( "C:/Image.jpg", filter_img );
this->ui->ImageView_2->setPixmap(QPixmap("C:/Image.jpg"));// here the problem
Try this:
cvtColor(mat, mat, CV_BGR2RGB);
QImage qimg((uchar*)mat.data, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(qimg));

Qt - QImage is there a method to paste Qimage into another Qimage?

I am looking for a way to simply paste some Qimage into bigger one, starting with some given (x,y). Now, I am copying pixel by pixel all Qimage.
QImage srcImage = QImage(100, 100);
QImage destImage = QImage(200, 200);
QPoint destPos = QPoint(25, 25); // The location to draw the source image within the dest
srcImage.fill(Qt::red);
destImage.fill(Qt::white);
QPainter painter(&destImage);
painter.drawImage(destPos, srcImage);
painter.end();
Yes, use a QPainter to paint into a QPaintDevice, QImage is a QPaintDevice, so it works.

Resources