Get QVector<unsigned char> with all the data of an image
-
@JesusM Why do you want to have the data in a QVector?! It would be quite inefficient.
There is https://doc.qt.io/qt-5/qimage.html#bits which you should use.
Even better is https://doc.qt.io/qt-5/qimage.html#constBits -
@jsulm
I need to translate this code to QT. This coe is done un MVS 2017 with c++ and I needed to use an external class to read the imagePagTexture::PagTexture(std::vector<std::string> rutas, std::vector<PagTexturesTypes> tipo) { for (int i = 0; i < rutas.size(); i++) { std::string filename = rutas[i]; PagTexturesTypes tipoTextura = tipo[i]; /** Carga un png de disco https://lodev.org/lodepng/ */ std::vector<unsigned char> image; // Los píxeles de la imagen unsigned width, height; unsigned error = lodepng::decode(image, width, height, filename); if (error) { std::cout << filename << " cannot be loaded" << std::endl; return; } // La textura se carga del revés, así que vamos a darle la vuelta unsigned char *imgPtr = &image[0]; int numColorComponents = 4; int wInc = width * 4; //width in char unsigned char* top = nullptr; unsigned char* bot = nullptr; unsigned char temp = 0; for (int i = 0; i < height / 2; i++) { top = imgPtr + i * wInc; bot = imgPtr + (height - i - 1) * wInc; for (int j = 0; j < wInc; j++) { temp = *top; *top = *bot; *bot = temp; ++top; ++bot; } } //Construimos la textura con las imágenes que queremos y la función de cada imagen GLuint texture = 0; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE, image.data()); glGenerateMipmap(GL_TEXTURE_2D); this->color = new PagImagen(image,width,height,texture); } }
So I am using QImage but I don't know hoow to translate this lane .
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE, image.data());
What I've done by now is this. I used the .bit() you told me:
texture::texture(QVector<QString> rutas, QVector<TexturesTypes> tipo) { for (int i = 0; i < rutas.size(); i++) { QString filename = rutas[i]; TexturesTypes tipoTextura = tipo[i]; QImage* imagen = new QImage(filename); // Los píxeles de la imagen int width, height; if (imagen == nullptr) { std::cout << filename.toStdString() << " cannot be loaded" << std::endl; return; } // La textura se carga del revés, así que vamos a darle la vuelta QImage imagenInvertida = imagen->rgbSwapped(); width = imagenInvertida.width(); height = imagenInvertida.height(); //Construimos la textura con las imágenes que queremos y la función de cada imagen GLuint texture = 0; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,0, GL_RGBA, GL_UNSIGNED_BYTE, imagenInvertida.bits()); //glGenerateMipmap(GL_TEXTURE_2D); this->color = new image(&imagenInvertida,width,height,texture); } }
Is that rigth now? Is it tha same as I had before?.
-
Hi,
It might be simpler to use a QOpenGLTexture.
-
Depending on what where you would like to go with your application, you should consider studying modern OpenGL and go through the related Qt examples.
-
Then I confirm my last suggestion.