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