Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get QVector<unsigned char> with all the data of an image
Forum Updated to NodeBB v4.3 + New Features

Get QVector<unsigned char> with all the data of an image

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JesusM
    wrote on last edited by
    #1

    How can I get a QVector<unsigned char> with all the data of a QImage?
    I need to get all the unsigned char information of an image to use it as a texture in the GPU.

    jsulmJ 1 Reply Last reply
    0
    • J JesusM

      How can I get a QVector<unsigned char> with all the data of a QImage?
      I need to get all the unsigned char information of an image to use it as a texture in the GPU.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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

        J Offline
        J Offline
        JesusM
        wrote on last edited by
        #3

        @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 image

        PagTexture::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?.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          It might be simpler to use a QOpenGLTexture.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          1
          • SGaistS SGaist

            Hi,

            It might be simpler to use a QOpenGLTexture.

            J Offline
            J Offline
            JesusM
            wrote on last edited by
            #5

            @SGaist so, if I use QOpenGLTextures, is it better to use QShaderProgram and QVertexArrayObjects instead of doing all the steps to get a GLuint id with the opengl functions?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J 1 Reply Last reply
              3
              • SGaistS SGaist

                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.

                J Offline
                J Offline
                JesusM
                wrote on last edited by
                #7

                @SGaist I am doing an OpenGL application to play with light settings in diferrent scenes woth different objects and materials

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Then I confirm my last suggestion.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  J 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Then I confirm my last suggestion.

                    J Offline
                    J Offline
                    JesusM
                    wrote on last edited by
                    #9

                    @SGaist ok, perfect so. Thanks a lot

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved