Note that there are some explanatory texts on larger screens.

plurals
  1. POWebP encoding - Segmentation Fault
    primarykey
    data
    text
    <p>So I'm trying to use the webp API to encode images. Right now I'm going to be using openCV to open and manipulate the images, then I want to save them off as webp. Here's the source I'm using:</p> <pre><code>#include &lt;stdlib.h&gt; #include &lt;stdio.h&gt; #include &lt;math.h&gt; #include &lt;cv.h&gt; #include &lt;highgui.h&gt; #include &lt;webp/encode.h&gt; int main(int argc, char *argv[]) { IplImage* img = 0; int height,width,step,channels; uchar *data; int i,j,k; if (argc&lt;2) { printf("Usage:main &lt;image-file-name&gt;\n\7"); exit(0); } // load an image img=cvLoadImage(argv[1]); if(!img){ printf("could not load image file: %s\n",argv[1]); exit(0); } // get the image data height = img-&gt;height; width = img-&gt;width; step = img-&gt;widthStep; channels = img-&gt;nChannels; data = (uchar *)img-&gt;imageData; printf("processing a %dx%d image with %d channels \n", width, height, channels); // create a window cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE); cvMoveWindow("mainWin",100,100); // invert the image for (i=0;i&lt;height;i++) { for (j=0;j&lt;width;j++) { for (k=0;k&lt;channels;k++) { data[i*step+j*channels+k] = 255-data[i*step+j*channels+k]; } } } // show the image cvShowImage("mainWin", img); // wait for a key cvWaitKey(0); // release the image cvReleaseImage(&amp;img); float qualityFactor = .9; uint8_t** output; FILE *opFile; size_t datasize; printf("encoding image\n"); datasize = WebPEncodeRGB((uint8_t*)data,width,height,step,qualityFactor,output); printf("writing file out\n"); opFile=fopen("output.webp","w"); fwrite(output,1,(int)datasize,opFile); } </code></pre> <p>When I execute this, I get this:</p> <pre><code>nato@ubuntu:~/webp/webp_test$ ./helloWorld ~/Pictures/mars_sunrise.jpg processing a 2486x1914 image with 3 channels encoding image Segmentation fault </code></pre> <p>It displays the image just fine, but segfaults on the encoding. My initial guess was that it's because I'm releasing the img before I try to write out the data, but it doesn't seem to matter whether I release it before or after I try the encoding. Is there something else I'm missing that might cause this problem? Do I have to make a copy of the image data or something?</p> <p>The WebP api docs are... sparse. Here's what the README says about WebPEncodeRGB:</p> <pre><code>The main encoding functions are available in the header src/webp/encode.h The ready-to-use ones are: size_t WebPEncodeRGB(const uint8_t* rgb, int width, int height, int stride, float quality_factor, uint8_t** output); </code></pre> <p>The docs specifically do not say what the 'stride' is, but I'm assuming that it's the same as the 'step' from opencv. Is that reasonable? </p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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