Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:06:59+00:00 2026-06-02T19:06:59+00:00

So I’m trying to use the webp API to encode images. Right now I’m

  • 0

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:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
#include <webp/encode.h>

int main(int argc, char *argv[])
{

    IplImage* img = 0;
    int height,width,step,channels;
    uchar *data;
    int i,j,k;
    if (argc<2) {
        printf("Usage:main <image-file-name>\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->height;
    width       = img->width;
    step        = img->widthStep;
    channels    = img->nChannels;
    data        = (uchar *)img->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<height;i++) {
        for (j=0;j<width;j++) {
            for (k=0;k<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(&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);
}

When I execute this, I get this:

nato@ubuntu:~/webp/webp_test$ ./helloWorld ~/Pictures/mars_sunrise.jpg 
processing a 2486x1914 image with 3 channels 
encoding image
Segmentation fault

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?

The WebP api docs are… sparse. Here’s what the README says about WebPEncodeRGB:

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);

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?

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:07:01+00:00Added an answer on June 2, 2026 at 7:07 pm

    so it looks like the problem was here:

    // load an image
    img=cvLoadImage(argv[1]);
    

    The function cvLoadImage takes an extra parameter

    cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)
    

    and when I changed to

    img=cvLoadImage(argv[1],1);
    

    the segfault went away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm making a simple page using Google Maps API 3. My first. One marker
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.