Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are three main points of confusion:</p> <ol> <li>It's best not to use char* when the buffer the pointer is pointing to is not a null-terminated string as it's confusing. </li> <li>strlen() will only give you the length of a null-terminated string, it won't give you the size of an arbitrary buffer in memory. You need to get that information elsewhere.</li> <li>The buffer you pass to lzo1x_1_compress() actually needs to contain the data you want to compress and not an empty buffer full of zeros.</li> </ol> <p>Assuming you can get the size of your image from imageIn using something like imageIn->getFrameSizeBytes(), try this:</p> <pre><code> int r; lzo_bytep out; lzo_voidp wrkmem; lzo_uint out_len; lzo_uint new_len; /* * Step 1: initialize the LZO library */ if (lzo_init() != LZO_E_OK) { cout &lt;&lt; "internal error - lzo_init() failed !!!" &lt;&lt; endl; cout &lt;&lt; "(this usually indicates a compiler bug - try recompiling\nwithout optimizations, and enable '-DLZO_DEBUG' for diagnostics)" &lt;&lt; endl; //return 4; } // here I get the data I want to compress lzo_bytep imageData = (lzo_bytep) imageIn-&gt;getFrame(); size_t uncompressedImageSize = imageIn-&gt;getFrameSizeBytes(); /* * Step 2: allocate blocks and the work-memory */ out = (lzo_bytep) xmalloc((uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3)); wrkmem = (lzo_voidp) xmalloc(LZO1X_1_MEM_COMPRESS); if (out == NULL || wrkmem == NULL) { cout &lt;&lt; "out of memory" &lt;&lt; endl; //return 3; } /* * Step 4: compress from 'imageData' to 'out' with LZO1X-1 */ r = lzo1x_1_compress(imageData,uncompressedImageSize,out,&amp;out_len,wrkmem); if (r != LZO_E_OK) { /* this should NEVER happen */ cout &lt;&lt; "internal error - compression failed: " &lt;&lt; r &lt;&lt; endl; //return 2; } /* check for an incompressible block */ if (out_len &gt;= uncompressedImageSize) { cout &lt;&lt; "This block contains incompressible data." &lt;&lt; endl; //return 0; } </code></pre> <p>Don't forget to free wrkmem. Even better, use C++ and std::vector for the working memory so it is freed automatically.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload