Problem developing with Potrace

Help
Jim Tzenes
2008-06-16
2013-03-04
  • Jim Tzenes

    Jim Tzenes - 2008-06-16

    I'm trying to include Potrace in my c++ project.  Now I'm compiling and linking fine, but for some reason when I call potrace_trace I crash.

    My code looks something along the lines of this:

    extern "C"
    {
    #include <potracelib.h>
    }
    #define ARCHITECTURE (sizeof(unsigned long)*8)
    #define PlacePoPixel(poobject,i,j) (poobject->map + j*poobject->dy)[i/ARCHITECTURE] |= (1 << (ARCHITECTURE-1-i%ARCHITECTURE))
    #define GetPoPixel(poobject,i,j) ((poobject->map + j*poobject->dy)[i/ARCHITECTURE] & (1 << (ARCHITECTURE-1-i%ARCHITECTURE))) ? 1 : 0

    ...
    void somefunction(){
        static potrace_param_t *parameters = potrace_param_default();
        potrace_state_t* state;
        potrace_bitmap_t** VGbmps;
        VGbmps = (potrace_bitmap_t**)malloc(sizeof(potrace_bitmap_t*)*num_ccs);
        for(int k=0; k<num_ccs; k++){
                compcolor[k] = complist[k] = segmentsize[k] = pixeldistribution[k] = 0;
                VGbmps[k] = new potrace_bitmap_t();
                VGbmps[k]->w = img->width;
                VGbmps[k]->h = img->height;
                VGbmps[k]->dy = (img->height*img->width)/ARCHITECTURE;
                //printf("test %d", sizeof(int));
                if((img->height*img->width)%ARCHITECTURE >0){ //add an extra word
                       VGbmps[k]->dy++;//this is probably super bad
                       printf("BAD THINGS");
                }
                VGbmps[k]->map = (potrace_word*)malloc((ARCHITECTURE)*VGbmps[k]->dy*VGbmps[k]->h);
               
        }

        for(int k=0; k<num_ccs; k++){
                state = potrace_trace(parameters,VGbmps[k]);
                if (!state || state->status != POTRACE_STATUS_OK){
                   system("pause");
                }
                system("pause");
        }

    ...
    }

    Now it seems to crash at the line: "state = potrace_trace(parameters,VGbmps[k]);" as it runs fine when I comment this out.  Now I realize the problem maybe in the rest of my code (which is where I've been looking for it), but on the off chance I've missed something obvious I figured I'd check in here as well.

    Incidentally I'm on a 32 bit machine and sizeof(unsigned long) returns 4.  I also realize that it probably helps to put something in the map (and I do elsewhere), but I've commented that out as the algorithm should run on whatever random data malloc gives me.  I also am unsure of what happens if my image dimensions do not lend themselves to a number divisible into an equal number of words (the image I'm working with does, but I'm curious none the less).

     
    • Jim Tzenes

      Jim Tzenes - 2008-06-16

      also, it might be helpful to note I am running on a win32 version of XP and using Dev-C++ as my development environment

       
    • Peter Selinger

      Peter Selinger - 2008-06-16

      Hi Jim,

      thanks for writing. You are not defining dy correctly; it is supposed to be the offset from the first line to the second, not from the beginning to the end of the image. See the potracelib documentation, http://potrace.sourceforge.net/potracelib.pdf, pages 2-3.

      Also, you should use potrace_word (defined in potracelib.h), instead of unsigned long. However, this didn't cause your problem, as both will be 4 bytes wide in your context.

      Your other question (about the image dimension not being a multiple of the word size) is also answered in the documentation, p.2-3 (with an example in Figure 3).

      Hope this helps, -- Peter

       
    • Jim Tzenes

      Jim Tzenes - 2008-06-16

      Oh I see.  My mistake. I was thinking of dy as being the number of words, not the size of the word.  It makes perfect sense now. I really appreciate you taking the time to respond.

       
      • Peter Selinger

        Peter Selinger - 2008-06-16

        Just to clarify: "dy" is not the size of the word; it is usually the number of words in one horizontal line of the image. (Or negative this number, if the image is stored top-to-bottom). If you leave padding between one line and the next, then "dy" includes the words in this padding too. So it is really the offset (in words) from the first word of the first line of the image to the first word of the second line of the image.

        Enjoy! -- Peter

         

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks