ctesseractleptonica

How to properly solve "invalid use of undefined type ‘struct Pixa’"?


I am including OCR with Leptonica and Tesseract capi.h in my C project. For regular images loaded as Pix, all is good, but for multipage TIFFs loaded as Pixa, I get the following compiler error:

ocr.c: In function ‘main’:
ocr.c:20:36: error: invalid use of undefined type ‘struct Pixa’
   20 |     TessBaseAPISetImage2(api, image->pix[0]);
      | 

This can be solved by including <leptonica/pix_internal.h>, but it looks like a workaround to me. How could this be solved properly?

For reference, here is the simplified code:

#include <stdio.h>
#include <stdlib.h>
#include <tesseract/capi.h>
#include <leptonica/allheaders.h>
#include <leptonica/pix_internal.h>

int main(int argc, char** argv) {
    struct Pixa *image = pixaReadMultipageTiff("multi.tiff");

    TessBaseAPI *api = TessBaseAPICreate();
    TessBaseAPIInit3(api, NULL, "eng");

    TessBaseAPISetImage2(api, image->pix[0]);
    TessBaseAPIRecognize(api, NULL);
    const char *text = TessBaseAPIGetUTF8Text(api);
    puts(text);

    TessDeleteText(text);
    TessBaseAPIEnd(api);
    TessBaseAPIDelete(api);
    pixaDestroy(&image);

    return EXIT_SUCCESS;
}

I am compiling it like this:

gcc -std=gnu23 -g -ggdb -Wall -I. -I/usr/include/x86_64-linux-gnu -I/usr/local/include ocr.c -ltesseract -lleptonica -o ocr

The same with the C++ API baseapi.h gives:

ocr.c: In function ‘int main(int, char**)’:
ocr.c:23:24: error: invalid use of incomplete type ‘struct Pixa’
   23 |     api->SetImage(image->pix[0]);
      |                        ^~
In file included from ocr.c:10:
/usr/include/tesseract/baseapi.h:36:8: note: forward declaration of ‘struct Pixa’
   36 | struct Pixa;
      |        ^~~~

and can also be solved by including <leptonica/pix_internal.h>.

This is with Leptonica 1.84, Tesseract 5.5.0 and gcc/g++ 14.2.0 on Debian 13.


Solution

  • "invalid use of incomplete type ‘struct Pixa’" hints that struct Pixa is declared and not defined. User's can use a pointer to it. It also hints users should not attempt to use its internal members. @chux
    This is good example of information hiding.

    ... you need pixaGetPix() to get a PIX from a PIXA ... @ Peter Hull