/*! * pixWriteImpliedFormat() * * Input: filename * pix * quality (iff JPEG; 1 - 100, 0 for default) * progressive (iff JPEG; 0 for baseline seq., 1 for progressive) * Return: 0 if OK; 1 on error * * Notes: * (1) This determines the output format from the filename extension. * (2) The last two args are ignored except for requests for jpeg files. * (3) The jpeg default quality is 75. */ l_int32 pixWriteImpliedFormat(const char *filename, PIX *pix, l_int32 quality, l_int32 progressive) { l_int32 format; PROCNAME("pixWriteImpliedFormat"); if (!filename) return ERROR_INT("filename not defined", procName, 1); if (!pix) return ERROR_INT("pix not defined", procName, 1); /* Determine output format */ format = getImpliedFileFormat(filename); if (format == IFF_UNKNOWN) { format = IFF_PNG; } else if (format == IFF_TIFF) { if (pixGetDepth(pix) == 1) format = IFF_TIFF_G4; else #ifdef _WIN32 format = IFF_TIFF_LZW; /* poor compression */ #else format = IFF_TIFF_ZIP; /* native windows tools can't handle this */ #endif /* _WIN32 */ } if (format == IFF_JFIF_JPEG) { quality = L_MIN(quality, 100); quality = L_MAX(quality, 0); if (progressive != 0 && progressive != 1) { progressive = 0; L_WARNING("invalid progressive; setting to baseline\n", procName); } if (quality == 0) quality = 75; pixWriteJpeg (filename, pix, quality, progressive); } else { pixWrite(filename, pix, format); } return 0; }
bool MakeIndividualGlyphs(Pix* pix, const std::vector<BoxChar*>& vbox, const int input_tiff_page) { // If checks fail, return false without exiting text2image if (!pix) { tprintf("ERROR: MakeIndividualGlyphs(): Input Pix* is nullptr\n"); return false; } else if (FLAGS_glyph_resized_size <= 0) { tprintf("ERROR: --glyph_resized_size must be positive\n"); return false; } else if (FLAGS_glyph_num_border_pixels_to_pad < 0) { tprintf("ERROR: --glyph_num_border_pixels_to_pad must be 0 or positive\n"); return false; } const int n_boxes = vbox.size(); int n_boxes_saved = 0; int current_tiff_page = 0; int y_previous = 0; static int glyph_count = 0; for (int i = 0; i < n_boxes; i++) { // Get one bounding box Box* b = vbox[i]->mutable_box(); if (!b) continue; const int x = b->x; const int y = b->y; const int w = b->w; const int h = b->h; // Check present tiff page (for multipage tiff) if (y < y_previous-pixGetHeight(pix)/10) { tprintf("ERROR: Wrap-around encountered, at i=%d\n", i); current_tiff_page++; } if (current_tiff_page < input_tiff_page) continue; else if (current_tiff_page > input_tiff_page) break; // Check box validity if (x < 0 || y < 0 || (x+w-1) >= pixGetWidth(pix) || (y+h-1) >= pixGetHeight(pix)) { tprintf("ERROR: MakeIndividualGlyphs(): Index out of range, at i=%d" " (x=%d, y=%d, w=%d, h=%d\n)", i, x, y, w, h); continue; } else if (w < FLAGS_glyph_num_border_pixels_to_pad && h < FLAGS_glyph_num_border_pixels_to_pad) { tprintf("ERROR: Input image too small to be a character, at i=%d\n", i); continue; } // Crop the boxed character Pix* pix_glyph = pixClipRectangle(pix, b, nullptr); if (!pix_glyph) { tprintf("ERROR: MakeIndividualGlyphs(): Failed to clip, at i=%d\n", i); continue; } // Resize to square Pix* pix_glyph_sq = pixScaleToSize(pix_glyph, FLAGS_glyph_resized_size, FLAGS_glyph_resized_size); if (!pix_glyph_sq) { tprintf("ERROR: MakeIndividualGlyphs(): Failed to resize, at i=%d\n", i); continue; } // Zero-pad Pix* pix_glyph_sq_pad = pixAddBorder(pix_glyph_sq, FLAGS_glyph_num_border_pixels_to_pad, 0); if (!pix_glyph_sq_pad) { tprintf("ERROR: MakeIndividualGlyphs(): Failed to zero-pad, at i=%d\n", i); continue; } // Write out Pix* pix_glyph_sq_pad_8 = pixConvertTo8(pix_glyph_sq_pad, false); char filename[1024]; snprintf(filename, 1024, "%s_%d.jpg", FLAGS_outputbase.c_str(), glyph_count++); if (pixWriteJpeg(filename, pix_glyph_sq_pad_8, 100, 0)) { tprintf("ERROR: MakeIndividualGlyphs(): Failed to write JPEG to %s," " at i=%d\n", filename, i); continue; } pixDestroy(&pix_glyph); pixDestroy(&pix_glyph_sq); pixDestroy(&pix_glyph_sq_pad); pixDestroy(&pix_glyph_sq_pad_8); n_boxes_saved++; y_previous = y; } if (n_boxes_saved == 0) { return false; } else { tprintf("Total number of characters saved = %d\n", n_boxes_saved); return true; } }
main(int argc, char **argv) { l_int32 w, h, d, wpl, count, i, format, xres, yres; FILE *fp; PIX *pix, *pixt1, *pixt2; PIXCMAP *cmap; char *filein; char *fileout = NULL; static char mainName[] = "iotest"; if (argc != 2 && argc != 3) exit(ERROR_INT(" Syntax: iotest filein [fileout]", mainName, 1)); filein = argv[1]; if (argc == 3) fileout = argv[2]; #if 1 if ((pix = pixRead(filein)) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); #else if ((pix = pixReadJpeg(filein, 0, 4, NULL)) == NULL) exit(ERROR_INT("pix not made", mainName, 1)); #endif pixGetDimensions(pix, &w, &h, &d); wpl = pixGetWpl(pix); fprintf(stderr, "w = %d, h = %d, d = %d, wpl = %d\n", w, h, d, wpl); xres = pixGetXRes(pix); yres = pixGetXRes(pix); if (xres != 0 && yres != 0) fprintf(stderr, "xres = %d, yres = %d\n", xres, yres); if (pixGetColormap(pix)) { /* Write and read back the colormap */ pixcmapWriteStream(stderr, pixGetColormap(pix)); fp = lept_fopen("/tmp/junkcmap1", "wb"); pixcmapWriteStream(fp, pixGetColormap(pix)); lept_fclose(fp); fp = lept_fopen("/tmp/junkcmap1", "rb"); cmap = pixcmapReadStream(fp); lept_fclose(fp); fp = lept_fopen("/tmp/junkcmap2", "wb"); pixcmapWriteStream(fp, cmap); lept_fclose(fp); pixcmapDestroy(&cmap); /* Remove and regenerate colormap */ pixt1 = pixRemoveColormap(pix, REMOVE_CMAP_BASED_ON_SRC); if (pixGetDepth(pixt1) == 8) { fprintf(stderr, "Colormap: represents grayscale image\n"); pixt2 = pixConvertGrayToColormap(pixt1); } else { /* 32 bpp */ fprintf(stderr, "Colormap: represents RGB image\n"); pixt2 = pixConvertRGBToColormap(pixt1, 1); } pixWrite("/tmp/junkpixt2.png", pixt2, IFF_PNG); pixDestroy(&pixt1); pixDestroy(&pixt2); } else { fprintf(stderr, "no colormap\n"); } format = pixGetInputFormat(pix); fprintf(stderr, "Input format extension: %s\n", ImageFileFormatExtensions[format]); if (format == IFF_JFIF_JPEG) fprintf(stderr, "Jpeg comment: %s\n", pixGetText(pix)); if (d == 1) { pixCountPixels(pix, &count, NULL); fprintf(stderr, "pixel ratio ON/OFF = %6.3f\n", (l_float32)count / (l_float32)(pixGetWidth(pix) * pixGetHeight(pix))); } if (argc == 3) { #if 1 d = pixGetDepth(pix); if (d == 16 || d < 8 || pixGetColormap(pix)) pixWrite(fileout, pix, IFF_PNG); else pixWriteJpeg(fileout, pix, 75, 0); #elif 0 pixWrite(fileout, pix, IFF_BMP); #elif 0 pixWrite(fileout, pix, IFF_PNG); #elif 0 pixWrite(fileout, pix, IFF_TIFF); fprintTiffInfo(stderr, fileout); #elif 0 pixWrite(fileout, pix, IFF_TIFF_PACKBITS); fprintTiffInfo(stderr, fileout); #elif 0 pixWrite(fileout, pix, IFF_TIFF_G3); fprintTiffInfo(stderr, fileout); #elif 0 pixWrite(fileout, pix, IFF_TIFF_G4); fprintTiffInfo(stderr, fileout); #elif 0 pixWrite(fileout, pix, IFF_JFIF_JPEG); #elif 0 pixWriteJpeg(fileout, pix, 75, 0); #elif 0 pixWrite(fileout, pix, IFF_PNM); #elif 0 pixWrite(fileout, pix, IFF_PS); #endif } pixDestroy(&pix); #if 0 /* test tiff header reader */ { l_int32 w, h, bps, spp, res, cmap; if (readHeaderTiff(filein, 0, &w, &h, &bps, &spp, &res, &cmap) == 0) fprintf(stderr, "w = %d, h = %d, bps = %d, spp = %d, res = %d, cmap = %d\n", w, h, bps, spp, res, cmap); } #endif return 0; }