Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to fragments large data into small packets. since i am beginner i am unable achieve it, SOMEBODY HELP TO FRAGMENT LARGE DATA INTO SMALL PACKETS .

say i have 22kb of jpeg file i want it to devide into small packets with 960 bytes of each. for jpeg there will be STARTING two bits and ENDING two bits will fixed values.

what i have tried is:

What I have tried:

pstart =buffer;

         if (!frame_active)
         {
             if ((pstart[0] == 0xff) && (pstart[1] == 0xd8))
             {
                 frame_active = 1;
                 frame_started = 1;
             }
         }

         if (frame_active)
         {
         // If an end of frame marker was pending (last byte of previous sample
         // was 0xff) then check first byte might be EOF marker.
             if (frame_eof_pending)
             {
                 if (*pstart == 0xd9)
                 {
                     // Only first byte of this sample is valid.
                     len = 1;
                     frame_active = 0;
                     frame_ended = 1;
                     //m_SendIdx++;
                 }
                 frame_eof_pending = 0;
             }

             // Look for end of frame markers in this sample.
             if (frame_active)
             {
                 pmark = pstart;
                 while (pmark)
                 {
                     pmark = memchr(pmark, 0xff, sample_length - (pmark - pstart));
                     if (pmark)
                     {
                         pmark++;
                         if ((pmark - pstart) == sample_length)
                         {
                         //The last byte of the sample is 0xff so there might be
                         //and end of frame marker continued on the next sample.
                             frame_eof_pending = 1;
                         }
                         else if (*pmark == 0xd9)
                         {
                         // Length of remaining data is up to and including the
                         // 0xd9 of the end of frame marker.
                             len = pmark - pstart + 1;
                             frame_active = 0;
                             frame_ended = 1;
                         }
                     }
                 }
             }
ANY HELP WILL BE APPRECIATED , THANKS.
Posted
Updated 4-Apr-18 20:30pm
v2
Comments
Richard MacCutchan 4-Apr-18 9:17am    
It's quite a simple matter to read the file 960 (or any other number) bytes at a time, and send them to the remote end. Adding prefix and suffix bytes helps each end to keep synchronised.
Balaraj Nayak 6-Apr-18 2:31am    
Hi @Richard MacCutchan if possible can you please show written code for me? , say ther is 22k bytes size file, if i want sendd every packet with 960 size, last packet will not be exact 960 bytes.

Your code will not work because the image data might contain the JPEG markers too (here: the FF D9 end of image marker).

So you have to write a parser that gets also the segment length for all non stand alone markers. When such a segment is processed, pass all bytes of the length without checking for markers. You might have to handle also compressed data which have no length but stop at the next 0xff marker not followed by 0x00 (0x00 is inserted in compressed data after all 0xff bytes).

I suggest to read about the JPEG file format. JPEG File Interchange Format - Wikipedia[^] might be a starting point.

Pseudo code (without pending checks):
C++
if (seg_length)
{
    // Pass seg_length data as they are (e.g. using a function)
    int len = PassSegment(pstart, seg_length);
    pstart += len;
    seg_length -= len;
}
else if (compressed)
{
    // Pass data as they are until 0xff not followed by 0x00
    int len = PassCompressed(pstart);
    pstart += len;
    if (pstart[0] == 0xff && pstart[1])
        compressed = 0; // All compressed data has been passed
}
else if (pstart[0] == 0xff)
{
    // Pass marker (two bytes)

    // Check if marker has length bytes
    if (MarkerHasLength(pstart[1]))
    {
        // Pass length (two bytes)

        // Includes the two length bytes
        seg_length = pstart[2] * 256 + pstart[3] - 2;
        pstart += 2; // length (marker is adjusted below)
    }
    // Special handling for compressed data (SOS marker)
    // These stop at the next 0xff not followed by 0x00
    else if (pstart[1] == 0xda)
    {
        compressed = 1;
    }
    pstart += 2; // marker
}
else
{
    // Parsing error: 0xff expected
}
 
Share this answer
 
Comments
Balaraj Nayak 5-Apr-18 1:31am    
Thank you sir @Jochen Arndt, thank you for your answer , yeah i am aware about jpeg data ,as i said above there will be fixed bits in biginning an ending , i meant that jpeg marker bits. your answer would helpful to me, thanks once again.
For sending over the network the splitting of the binary data is enough.

If you want smaller jpeg than you must use higher compression or split the decompressed picture in sub pictures and than compress. The article CXImage shows these techniques.
 
Share this answer
 
Comments
Balaraj Nayak 5-Apr-18 4:46am    
Hi KarstenK thank you sir, for your valuable response, yea i'll read that article ,if it would help me to solve my problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900