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.
DebugPixa()15   DebugPixa() {
16     pixa_ = pixaCreate(0);
17 #ifdef TESSERACT_DISABLE_DEBUG_FONTS
18     fonts_ = NULL;
19 #else
20     fonts_ = bmfCreate(nullptr, 14);
21 #endif
22   }
23   // If the filename_ has been set and there are any debug images, they are
24   // written to the set filename_.
~DebugPixa()25   ~DebugPixa() {
26     pixaDestroy(&pixa_);
27     bmfDestroy(&fonts_);
28   }
29 
30   // Adds the given pix to the set of pages in the PDF file, with the given
31   // caption added to the top.
AddPix(const Image pix,const char * caption)32   void AddPix(const Image pix, const char *caption) {
33     int depth = pixGetDepth(pix);
34     int color = depth < 8 ? 1 : (depth > 8 ? 0x00ff0000 : 0x80);
35     Image pix_debug =
36         pixAddSingleTextblock(pix, fonts_, caption, color, L_ADD_BELOW, nullptr);
37     pixaAddPix(pixa_, pix_debug, L_INSERT);
38   }
39 
40   // Sets the destination filename and enables images to be written to a PDF
41   // on destruction.
WritePDF(const char * filename)42   void WritePDF(const char *filename) {
43     if (pixaGetCount(pixa_) > 0) {
44       pixaConvertToPdf(pixa_, 300, 1.0f, 0, 0, "AllDebugImages", filename);
45       pixaClear(pixa_);
46     }
47   }
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_
59