|
| 1 | +// ========================================================== |
| 2 | +// Despeckle histogram filter (for BW only) based on the FreeImage library |
| 3 | +// |
| 4 | +// This code is taken from the CxImage Library at http://www.xdp.it/cximage.htm |
| 5 | +// and adapted for the FreeImage Library |
| 6 | +// |
| 7 | + |
| 8 | +#include <unistd.h> |
| 9 | +#include <FreeImage.h> |
| 10 | +#include "imthresholdfreeimage.h" |
| 11 | + |
| 12 | +//////////////////////////////////////////////////////////////////////////////// |
| 13 | + |
| 14 | +void ImthresholdFilterDphistTitle() |
| 15 | +{ |
| 16 | + printf("ImThreshold.\n"); |
| 17 | + printf("BookScanLib Project: http://djvu-soft.narod.ru/\n\n"); |
| 18 | + printf("Despeckle histogram filter (for BW only) based on the FreeImage library.\n"); |
| 19 | + printf("TerraNoNames: http://mykaralw.narod.ru/.\n\n"); |
| 20 | +} |
| 21 | + |
| 22 | +void ImthresholdFilterDphistUsage() |
| 23 | +{ |
| 24 | + printf("Usage : imthreshold-fdphist [options] <input_file>(BW) <output_file>(BW)\n\n"); |
| 25 | + printf("options:\n"); |
| 26 | + printf(" -i invert (bool, optional, default = false)\n"); |
| 27 | + printf(" -h this help\n"); |
| 28 | +} |
| 29 | + |
| 30 | +// ---------------------------------------------------------- |
| 31 | + |
| 32 | +int main(int argc, char *argv[]) |
| 33 | +{ |
| 34 | + // call this ONLY when linking with FreeImage as a static library |
| 35 | +#ifdef FREEIMAGE_LIB |
| 36 | + FreeImage_Initialise(); |
| 37 | +#endif // FREEIMAGE_LIB |
| 38 | + |
| 39 | + int opt; |
| 40 | + double kdp = 0.0; |
| 41 | + bool finv = false; |
| 42 | + bool fhelp = false; |
| 43 | + while ((opt = getopt(argc, argv, ":ih")) != -1) |
| 44 | + { |
| 45 | + switch(opt) |
| 46 | + { |
| 47 | + case 'i': |
| 48 | + finv = true; |
| 49 | + break; |
| 50 | + case 'h': |
| 51 | + fhelp = true; |
| 52 | + break; |
| 53 | + case ':': |
| 54 | + printf("option needs a value\n"); |
| 55 | + break; |
| 56 | + case '?': |
| 57 | + printf("unknown option: %c\n", optopt); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + ImthresholdFilterDphistTitle(); |
| 63 | + |
| 64 | + if(optind + 2 > argc || fhelp) |
| 65 | + { |
| 66 | + ImthresholdFilterDphistUsage();; |
| 67 | + return 0; |
| 68 | + } |
| 69 | + const char *src_filename = argv[optind]; |
| 70 | + const char *output_filename = argv[optind + 1]; |
| 71 | + |
| 72 | + FreeImage_SetOutputMessage(FreeImageErrorHandler); |
| 73 | + |
| 74 | + printf("Input= %s\n", src_filename); |
| 75 | + FIBITMAP *dib = ImthresholdGenericLoader(src_filename, 0); |
| 76 | + if (dib) |
| 77 | + { |
| 78 | + if (FreeImage_GetImageType(dib) == FIT_BITMAP) |
| 79 | + { |
| 80 | + FIBITMAP *despeckled; |
| 81 | + if (FreeImage_GetBPP(dib) == 1) |
| 82 | + { |
| 83 | + unsigned width = FreeImage_GetWidth(dib); |
| 84 | + unsigned height = FreeImage_GetHeight(dib); |
| 85 | + unsigned y; |
| 86 | + |
| 87 | + BYTE** p_im; |
| 88 | + p_im = (BYTE**)malloc(height * sizeof(BYTE*)); |
| 89 | + for (y = 0; y < height; y++) {p_im[y] = (BYTE*)malloc(width * sizeof(BYTE));} |
| 90 | + |
| 91 | + ImthresholdGetDataBW(dib, p_im); |
| 92 | + if (finv) {IMTFilterInvertBW(p_im, height, width);} |
| 93 | + kdp = IMTFilterDphist(p_im, height, width); |
| 94 | + printf("Despeckle= %f\n", kdp); |
| 95 | + despeckled = FreeImage_Allocate(width, height, 1); |
| 96 | + ImthresholdSetDataBW(despeckled, p_im); |
| 97 | + for (y = 0; y < height; y++){free(p_im[y]);} |
| 98 | + free(p_im); |
| 99 | + } else { |
| 100 | + despeckled = ImthresholdFilterNone(dib); |
| 101 | + printf("%s\n", "Unsupported color mode."); |
| 102 | + } |
| 103 | + |
| 104 | + if (despeckled) |
| 105 | + { |
| 106 | + FREE_IMAGE_FORMAT out_fif = FreeImage_GetFIFFromFilename(output_filename); |
| 107 | + if(out_fif != FIF_UNKNOWN) |
| 108 | + { |
| 109 | + FreeImage_Save(out_fif, despeckled, output_filename, 0); |
| 110 | + printf("Output= %s\n\n", output_filename); |
| 111 | + } |
| 112 | + FreeImage_Unload(despeckled); |
| 113 | + } |
| 114 | + } else { |
| 115 | + printf("%s\n", "Unsupported format type."); |
| 116 | + FreeImage_Unload(dib); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // call this ONLY when linking with FreeImage as a static library |
| 121 | +#ifdef FREEIMAGE_LIB |
| 122 | + FreeImage_DeInitialise(); |
| 123 | +#endif // FREEIMAGE_LIB |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
0 commit comments