Tcp binary data to image

Hello friends :slight_smile:
i’m a beginner for using OF
i had a problem that:get a char* from tcpClient.receiveRawMsg();this message contain a image buffer.Now i want draw it in screen.How can i do it ?
Thank for your answer

If the char* buffer is the right size and format you expect, you can load it straight into a texture-

  
someTexture.loadData( (unsigned char*) buffer, 320, 240, GL_RGB ); // expects 320*240 rgb image  

If you don’t know the format and size, eg. image was compressed before sent, you can use an ofBuffer and ofLoadImage-

  
  
ofBuffer videoBuffer(buffer, numBytes); // match numBytes you put into receiveRawBytes(char * receiveBuffer, int numByte)  
ofLoadImage(someTexture, videoBuffer);  
  

Of course both these methods assume the buffer contains the whole image. If the buffer is coming through in parts of the image, you just need a while loop which calls receiveRawBytes() until you have enough bytes for the whole image.

Hey!

Are there any updates on this communication? I would like to do something similar…

The general idea is to use a ofxTCPServer (TCPraw) to receive an array of images of various (unknown) sizes, which are sent from a TCPclient in a Unix machine.

So I am cutting the task in smaller steps, and at first I would like to transmit and view one (small) image in (ofTexture) testTexture with known size of 1224bytes. That is why, in my update function I have :

 for (int i = 0; i < TCPraw.getLastID(); i++) {       
   if( TCPraw.isClientConnected(i) ) { 
        char receiveBytes;
        int numBytes; 
        
        int received = 0;
        ofBuffer buffer;
        while (received != 1224) {
            int tempNum = TCPraw.receiveRawBytes(i, (char *) &receiveBytes, 1224);
            ofBuffer buffer(&receiveBytes, 1224);
            if (tempNum > 0) {
                received += tempNum;
                printf("RAW Bytes received from client: %d\n\n", received);
                if (received == 1224) {
                    ofLoadImage(testTexture, buffer);
                }
            } else {
                break;
            }
        }
     }
  }

The thing is that the code above throws a memory error in a specific address by the end of the update function. Do you have any idea of what is going wrong?

And later when I would have to send an array of images I suppose that I will have to use a delimiter to notify the beginning/end of each image?

And if it is possible, how could the TCP server identify if the message is string or raw data, so that I can use the corresponding function?

Many many thanks in advance, this forum (and OF in general) are life-saviours!

Hey, you haven’t allocated any memory for ‘char receiveBytes’- this is causing the crash. Try capturing all the bytes into the allocated ofBuffer…

for (int i = 0; i < TCPraw.getLastID(); i++) {
    if( TCPraw.isClientConnected(i) ) {
        
        int receiveSize = 1224;
        ofBuffer buffer;
        buffer.allocate(receiveSize);
        int totalBytes = 0;
        while(totalBytes < receiveSize) {
            int result = client->receiveRawBytes(&buffer.getBinaryBuffer()[totalBytes], receiveSize-totalBytes);
            if(result > 0) {
                totalBytes += result;
                if(totalBytes == receiveSize) {
                    ofLoadImage(testTexture, buffer);
                } 
            }
        }
    }
}

As for identifying different messages - you need to come up with your own ‘message framing’. eg. for each message (raw data) you want to send, first send a small header message (string with fixed size or a delimiter on the end) with some info about the next message, eg. size in bytes of raw data.

Thank you so much for the correction, it works properly now!
And thank you also for the tip!