/src/tesseract/src/ccstruct/debugpixa.h
Line | Count | Source |
1 | | #ifndef TESSERACT_CCSTRUCT_DEBUGPIXA_H_ |
2 | | #define TESSERACT_CCSTRUCT_DEBUGPIXA_H_ |
3 | | |
4 | | #include "image.h" |
5 | | |
6 | | namespace tesseract { |
7 | | |
8 | | // Class to hold a Pixa collection of debug images with captions and save them |
9 | | // to a PDF file. |
10 | | class DebugPixa { |
11 | | public: |
12 | | // TODO(rays) add another constructor with size control. |
13 | 4 | DebugPixa() { |
14 | 4 | pixa_ = pixaCreate(0); |
15 | | #ifdef TESSERACT_DISABLE_DEBUG_FONTS |
16 | | fonts_ = nullptr; |
17 | | #else |
18 | 4 | fonts_ = bmfCreate(nullptr, 14); |
19 | 4 | #endif |
20 | 4 | } |
21 | | // If the filename_ has been set and there are any debug images, they are |
22 | | // written to the set filename_. |
23 | 0 | ~DebugPixa() { |
24 | 0 | pixaDestroy(&pixa_); |
25 | 0 | bmfDestroy(&fonts_); |
26 | 0 | } |
27 | | |
28 | | // Adds the given pix to the set of pages in the PDF file, with the given |
29 | | // caption added to the top. |
30 | 0 | void AddPix(const Image pix, const char *caption) { |
31 | 0 | int depth = pixGetDepth(pix); |
32 | 0 | int color = depth < 8 ? 1 : (depth > 8 ? 0x00ff0000 : 0x80); |
33 | 0 | Image pix_debug = |
34 | 0 | pixAddSingleTextblock(pix, fonts_, caption, color, L_ADD_BELOW, nullptr); |
35 | 0 | pixaAddPix(pixa_, pix_debug, L_INSERT); |
36 | 0 | } |
37 | | |
38 | | // Sets the destination filename and enables images to be written to a PDF |
39 | | // on destruction. |
40 | 15.4k | void WritePDF(const char *filename) { |
41 | 15.4k | if (pixaGetCount(pixa_) > 0) { |
42 | 0 | pixaConvertToPdf(pixa_, 300, 1.0f, 0, 0, "AllDebugImages", filename); |
43 | 0 | pixaClear(pixa_); |
44 | 0 | } |
45 | 15.4k | } |
46 | | |
47 | | private: |
48 | | // The collection of images to put in the PDF. |
49 | | Pixa *pixa_; |
50 | | // The fonts used to draw text captions. |
51 | | L_Bmf *fonts_; |
52 | | }; |
53 | | |
54 | | } // namespace tesseract |
55 | | |
56 | | #endif // TESSERACT_CCSTRUCT_DEBUGPIXA_H_ |