QVector for QImage

4

I have to transform a QVector 2d into a QImage to display the image in a label. The QVector in this case is an array of integers with value from 0 to 255 representing an image in PGM or PPM, this vector makes grayscale or RGB transformations that are necessary for PDI exercises, but to make it a little easier and the easiest generic methods to use, I decided to put the image in a QImage and present the changes.

The question is how can I move QVector<QVector<int> > to QImage , where I can also add file format information, in P2 and P3 , comment line, number of rows and columns, and color scale.

It would look something like this:

P2
# Comentário do arquivo
número de linhas  número de colunas
255
vetor 2d com as informações da imagem.
    
asked by anonymous 03.04.2014 / 02:34

1 answer

2

You can convert your QVector<QVector<int> > to PGM format in const char* and let Qt do the rest because QImage supports PGM.

Example:

In your case, just replace pgm_file with what will be generated by your QVector .

const char* pgm_file =
        "P2\n"
        "# Exemplo\n"
        "24 7\n"
        "15\n"
        "0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n"
        "0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0\n"
        "0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0\n"
        "0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0\n"
        "0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0\n"
        "0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0\n"
        "0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0\n";

QByteArray bytes(pgm_file);

QImage img;
img.loadFromData(bytes, "PGM"); // Pronto. Tá convertido.

// Se quiser ver o resultado.
img.save("resultado.bmp", "BMP");

Converted image:

    
03.04.2014 / 03:00