Leptonica 1.68
C Image Processing Library

writefile.c File Reference

Pix to memory and disk; selecting format. More...

#include <string.h>
#include "allheaders.h"

Go to the source code of this file.

Data Structures

struct  ExtensionMap

Defines

#define WRITE_AS_NAMED   1
#define L_BUF_SIZE   512

Functions

l_int32 pixaWriteFiles (const char *rootname, PIXA *pixa, l_int32 format)
l_int32 pixWrite (const char *filename, PIX *pix, l_int32 format)
l_int32 pixWriteStream (FILE *fp, PIX *pix, l_int32 format)
l_int32 pixWriteImpliedFormat (const char *filename, PIX *pix, l_int32 quality, l_int32 progressive)
l_int32 pixWriteTempfile (const char *dir, const char *tail, PIX *pix, l_int32 format, char **pfilename)
l_int32 pixChooseOutputFormat (PIX *pix)
l_int32 getImpliedFileFormat (const char *filename)
const char * getFormatExtension (l_int32 format)
l_int32 pixWriteMem (l_uint8 **pdata, size_t *psize, PIX *pix, l_int32 format)
l_int32 pixDisplay (PIX *pixs, l_int32 x, l_int32 y)
l_int32 pixDisplayWithTitle (PIX *pixs, l_int32 x, l_int32 y, const char *title, l_int32 dispflag)
l_int32 pixDisplayMultiple (const char *filepattern)
l_int32 pixDisplayWrite (PIX *pixs, l_int32 reduction)
l_int32 pixDisplayWriteFormat (PIX *pixs, l_int32 reduction, l_int32 format)
l_int32 pixSaveTiled (PIX *pixs, PIXA *pixa, l_int32 reduction, l_int32 newrow, l_int32 space, l_int32 dp)
l_int32 pixSaveTiledOutline (PIX *pixs, PIXA *pixa, l_int32 reduction, l_int32 newrow, l_int32 space, l_int32 linewidth, l_int32 dp)
l_int32 pixSaveTiledWithText (PIX *pixs, PIXA *pixa, l_int32 outwidth, l_int32 newrow, l_int32 space, l_int32 linewidth, L_BMF *bmf, const char *textstr, l_uint32 val, l_int32 location)
void l_chooseDisplayProg (l_int32 selection)

Variables

static l_int32 var_DISPLAY_PROG = L_DISPLAY_WITH_XZGV
static const l_int32 MAX_DISPLAY_WIDTH = 1000
static const l_int32 MAX_DISPLAY_HEIGHT = 800
static const l_int32 MAX_SIZE_FOR_PNG = 200
static const l_float32 DEFAULT_SCALING = 1.0
LEPT_DLL l_int32 NumImageFileFormatExtensions = 19
LEPT_DLL const char * ImageFileFormatExtensions []
static struct ExtensionMap extension_map []

Detailed Description

Pix to memory and disk; selecting format.

Definition in file writefile.c.


Define Documentation

#define WRITE_AS_NAMED   1

Definition at line 52 of file writefile.c.

#define L_BUF_SIZE   512

Function Documentation

l_int32 pixaWriteFiles ( const char *  rootname,
PIXA pixa,
l_int32  format 
)

pixaWriteFiles()

Input: rootname pixa format (defined in imageio.h) Return: 0 if OK; 1 on error

Definition at line 134 of file writefile.c.

References ERROR_INT, ImageFileFormatExtensions, L_BUF_SIZE, L_CLONE, NumImageFileFormatExtensions, pixaGetCount(), pixaGetPix(), pixDestroy(), pixWrite(), and PROCNAME.

Referenced by main().

l_int32 pixWrite ( const char *  filename,
PIX pix,
l_int32  format 
)

pixWrite()

Input: filename pix format (defined in imageio.h) Return: 0 if OK; 1 on error

Notes: (1) Open for write using binary mode (with the "b" flag) to avoid having Windows automatically translate the NL into CRLF, which corrupts image files. On non-windows systems this flag should be ignored, per ISO C90. Thanks to Dave Bryan for pointing this out. (2) If the default image format is requested, we use the input format; if the input format is unknown, a lossless format is assigned. (3) There are two modes with respect to file naming. (a) The default code writes to . (b) If WRITE_AS_NAMED is defined to 0, it's a bit fancier. Then, if does not have a file extension, one is automatically appended, depending on the requested format. The original intent for providing option (b) was to insure that filenames on Windows have an extension that matches the image compression. However, this is not the default.

Definition at line 190 of file writefile.c.

References CALLOC, ERROR_INT, ExtensionMap::extension, FREE, genPathname(), IFF_DEFAULT, IFF_GIF, IFF_JP2, IFF_UNKNOWN, ImageFileFormatExtensions, NULL, pixChooseOutputFormat(), pixWriteStream(), PROCNAME, and splitPathAtExtension().

Referenced by convertFilesTo1bpp(), convertTiffMultipageToPS(), convertToPSEmbed(), DisplayPix(), DoComparisonDwa1(), DoComparisonDwa3(), DoPageSegmentation(), GetImageMask(), ioFormatTest(), jbCorrelation(), jbDataWrite(), jbRankHaus(), main(), pixaGenerateFont(), pixaWriteCompressedToPS(), pixaWriteFiles(), pixColorsForQuantization(), pixConnCompBB(), pixConnCompPixa(), pixDebugFlipDetect(), pixDisplayWithTitle(), pixDisplayWriteFormat(), pixExtractBarcodes(), pixFindBaselines(), pixFindLargestRectangle(), pixGenerateG4Data(), pixGetEdgeProfile(), pixHtmlViewer(), pixItalicWords(), pixThinExamples(), pixUpDownDetectGeneral(), pixUpDownDetectGeneralDwa(), pixWriteImpliedFormat(), pixWriteMemGif(), pixWriteMixedToPS(), pixWriteRGBAPng(), pixWriteTempfile(), regTestWritePixAndCheck(), rotateTest(), test_gif(), and TestImage().

l_int32 pixWriteImpliedFormat ( const char *  filename,
PIX pix,
l_int32  quality,
l_int32  progressive 
)

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.

Definition at line 367 of file writefile.c.

References ERROR_INT, ExtensionMap::format, getImpliedFileFormat(), IFF_JFIF_JPEG, IFF_PNG, IFF_TIFF, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_ZIP, IFF_UNKNOWN, L_MAX, L_MIN, L_WARNING, pixGetDepth(), pixWrite(), pixWriteJpeg(), and PROCNAME.

Referenced by main().

l_int32 pixWriteTempfile ( const char *  dir,
const char *  tail,
PIX pix,
l_int32  format,
char **  pfilename 
)

pixWriteTempfile()

Input: dir (directory name; use '.' for local dir; no trailing '/') tail (<optional> tailname, including extension if any) pix format &filename (<optional> return actual filename used; use null to skip) Return: 0 if OK; 1 on error

Notes: (1) This generates a temp filename, writes the pix to it, and optionally returns the temp filename. (2) If the filename is returned to a windows program from a DLL, use lept_free() to free it. (3) See genTempFilename() for details. We omit the time and pid here.

Definition at line 434 of file writefile.c.

References ERROR_INT, filename, FREE, genTempFilename(), NULL, pixWrite(), and PROCNAME.

Referenced by dewarpApplyDisparity(), dewarpBuildModel(), pixGetRankColorArray(), pixSetUnderTransparency(), selaAddCrossJunctions(), and selaAddTJunctions().

l_int32 pixChooseOutputFormat ( PIX pix)

pixChooseOutputFormat()

Input: pix Return: output format, or 0 on error

Notes: (1) This should only be called if the requested format is IFF_DEFAULT. (2) If the pix wasn't read from a file, its input format value will be IFF_UNKNOWN, and in that case it is written out in a compressed but lossless format.

Definition at line 479 of file writefile.c.

References ERROR_INT, ExtensionMap::format, IFF_PNG, IFF_TIFF_G4, IFF_UNKNOWN, pixGetDepth(), pixGetInputFormat(), and PROCNAME.

Referenced by pixWrite(), pixWriteMem(), and pixWriteStream().

l_int32 getImpliedFileFormat ( const char *  filename)

getImpliedFileFormat()

Input: filename Return: output format, or IFF_UNKNOWN on error or invalid extension.

Notes: (1) This determines the output file format from the extension of the input filename.

Definition at line 512 of file writefile.c.

References ExtensionMap::extension, extension_map, ExtensionMap::format, FREE, IFF_UNKNOWN, NULL, and splitPathAtExtension().

Referenced by pixWriteImpliedFormat().

const char* getFormatExtension ( l_int32  format)

getFormatExtension()

Input: format (integer) Return: extension (string), or null if format is out of range

Notes: (1) This string is NOT owned by the caller; it is just a pointer to a global string. Do not free it.

Definition at line 545 of file writefile.c.

References ERROR_PTR, ExtensionMap::format, ImageFileFormatExtensions, NULL, NumImageFileFormatExtensions, and PROCNAME.

l_int32 pixWriteMem ( l_uint8 **  pdata,
size_t *  psize,
PIX pix,
l_int32  format 
)

pixWriteMem()

Input: &data (<return> data of tiff compressed image) &size (<return> size of returned data) pix format (defined in imageio.h) Return: 0 if OK, 1 on error

Notes: (1) On windows, this will only write tiff and PostScript to memory. For other formats, it requires open_memstream(3). (2) PostScript output is uncompressed, in hex ascii. Most printers support level 2 compression (tiff_g4 for 1 bpp, jpeg for 8 and 32 bpp).

Definition at line 576 of file writefile.c.

References DEFAULT_SCALING, ERROR_INT, IFF_BMP, IFF_DEFAULT, IFF_GIF, IFF_JFIF_JPEG, IFF_JP2, IFF_PNG, IFF_PNM, IFF_PS, IFF_SPIX, IFF_TIFF, IFF_TIFF_G3, IFF_TIFF_G4, IFF_TIFF_LZW, IFF_TIFF_PACKBITS, IFF_TIFF_RLE, IFF_TIFF_ZIP, NULL, pixChooseOutputFormat(), pixWriteMemBmp(), pixWriteMemGif(), pixWriteMemJpeg(), pixWriteMemPng(), pixWriteMemPnm(), pixWriteMemPS(), pixWriteMemSpix(), pixWriteMemTiff(), and PROCNAME.

Referenced by main(), pixcompCreateFromPix(), test_mem_gif(), test_mem_png(), and test_writemem().

l_int32 pixDisplay ( PIX pixs,
l_int32  x,
l_int32  y 
)

pixDisplay()

Input: pix (1, 2, 4, 8, 16, 32 bpp) x, y (location of display frame on the screen) Return: 0 if OK; 1 on error

Notes: (1) This displays the image using xv, xli or xzgv on Unix, or i_view on Windows. The display program must be on your $PATH variable. It is chosen by setting the global var_DISPLAY_PROG, using l_chooseDisplayProg(). Default on Unix is xv. (2) Images with dimensions larger than MAX_DISPLAY_WIDTH or MAX_DISPLAY_HEIGHT are downscaled to fit those constraints. This is particulary important for displaying 1 bpp images with xv, because xv automatically downscales large images by subsampling, which looks lousy. For 1 bpp, we use scale-to-gray to get decent-looking anti-aliased images. In all cases, we write a temporary file to /tmp, that is read by the display program. (3) Note: this function uses a static internal variable to number output files written by a single process. Behavior with a shared library may be unpredictable.

Definition at line 677 of file writefile.c.

References NULL, and pixDisplayWithTitle().

Referenced by dewarpBuildModel(), DisplayPix(), DoComparisonDwa1(), GetImageMask(), main(), pixaGenerateFont(), pixColorMorphSequence(), pixExtractBarcodes(), pixFindBaselines(), pixGetBinnedComponentRange(), pixGetRegionsBinary(), pixGrayMorphSequence(), pixMorphCompSequence(), pixMorphCompSequenceDwa(), pixMorphSequence(), pixMorphSequenceDwa(), selaAddCrossJunctions(), and selaAddTJunctions().

l_int32 pixDisplayWithTitle ( PIX pixs,
l_int32  x,
l_int32  y,
const char *  title,
l_int32  dispflag 
)
l_int32 pixDisplayMultiple ( const char *  filepattern)

pixDisplayMultiple()

Input: filepattern Return: 0 if OK; 1 on error

Notes: (1) This allows display of multiple images using gthumb on unix and i_view32 on windows. The is a regular expression that is expanded by the shell. (2) _fullpath automatically changes '/' to '\' if necessary.

Definition at line 838 of file writefile.c.

References ERROR_INT, FREE, genPathname(), L_BUF_SIZE, NULL, PROCNAME, and splitPathAtDirectory().

Referenced by main().

l_int32 pixDisplayWrite ( PIX pixs,
l_int32  reduction 
)

pixDisplayWrite()

Input: pix (1, 2, 4, 8, 16, 32 bpp) reduction (-1 to reset/erase; 0 to disable; otherwise this is a reduction factor) Return: 0 if OK; 1 on error

Notes: (1) This defaults to jpeg output for pix that are 32 bpp or 8 bpp without a colormap. If you want to write all images losslessly, use format == IFF_PNG in pixDisplayWriteFormat(). (2) See pixDisplayWriteFormat() for usage details.

Definition at line 888 of file writefile.c.

References IFF_JFIF_JPEG, and pixDisplayWriteFormat().

Referenced by count_pieces(), DoPageSegmentation(), main(), pixCompare(), pixGenHalftoneMask(), pixGenTextlineMask(), pixGetRegionsBinary(), test_gif(), and test_writemem().

l_int32 pixDisplayWriteFormat ( PIX pixs,
l_int32  reduction,
l_int32  format 
)

pixDisplayWriteFormat()

Input: pix (1, 2, 4, 8, 16, 32 bpp) reduction (-1 to reset/erase; 0 to disable; otherwise this is a reduction factor) format (IFF_PNG or IFF_JFIF_JPEG) Return: 0 if OK; 1 on error

Notes: (1) This writes files if reduction > 0. These can be displayed using pixDisplayMultiple("/tmp/junk_write_display*"); (2) All previously written files can be erased by calling with reduction < 0; the value of pixs is ignored. (3) If reduction > 1 and depth == 1, this does a scale-to-gray reduction. (4) This function uses a static internal variable to number output files written by a single process. Behavior with a shared library may be unpredictable. (5) Output file format is as follows: format == IFF_JFIF_JPEG: png if d < 8 or d == 16 or if the output pix has a colormap. Otherwise, output is jpg. format == IFF_PNG: png (lossless) on all images. (6) For 16 bpp, the choice of full dynamic range with log scale is the best for displaying these images. Alternative outputs are pix8 = pixMaxDynamicRange(pixt, L_LINEAR_SCALE); pix8 = pixConvert16To8(pixt, 0); // low order byte pix8 = pixConvert16To8(pixt, 1); // high order byte

Definition at line 927 of file writefile.c.

References ERROR_INT, IFF_JFIF_JPEG, IFF_PNG, L_BUF_SIZE, L_LOG_SCALE, pixClone(), pixDestroy(), pixGetColormap(), pixGetDepth(), pixMaxDynamicRange(), pixScale(), pixScaleToGray(), pixWrite(), and PROCNAME.

Referenced by AdaptiveGrayBlend(), ColorBlend(), DoPageSegmentation(), GrayBlend(), main(), pixDisplayWrite(), pixGenHalftoneMask(), pixGenTextblockMask(), pixGenTextlineMask(), and pixGetRegionsBinary().

l_int32 pixSaveTiled ( PIX pixs,
PIXA pixa,
l_int32  reduction,
l_int32  newrow,
l_int32  space,
l_int32  dp 
)

pixSaveTiled()

Input: pixs (1, 2, 4, 8, 32 bpp) pixa (the pix are accumulated here) reduction (0 to disable; otherwise this is a reduction factor) newrow (0 if placed on the same row as previous; 1 otherwise) space (horizontal and vertical spacing, in pixels) dp (depth of pixa; 8 or 32 bpp; only used on first call) Return: 0 if OK, 1 on error.

Definition at line 1001 of file writefile.c.

References pixSaveTiledOutline().

Referenced by AddScaledImages(), AddTestSet(), DisplayCaptcha(), DisplayResult(), main(), pixDisplayColorArray(), PixSave32(), PixSavePlots1(), PixSavePlots2(), pixSaveTiledWithText(), pixSetUnderTransparency(), PixTest1(), PixTest2(), RotateTest(), shearTest(), TestDistance(), TestHardlight(), TestImage(), and TranslateAndSave1().

l_int32 pixSaveTiledOutline ( PIX pixs,
PIXA pixa,
l_int32  reduction,
l_int32  newrow,
l_int32  space,
l_int32  linewidth,
l_int32  dp 
)

pixSaveTiledOutline()

Input: pixs (1, 2, 4, 8, 32 bpp) pixa (the pix are accumulated here) reduction (0 to disable; otherwise this is a reduction factor) newrow (0 if placed on the same row as previous; 1 otherwise) space (horizontal and vertical spacing, in pixels) linewidth (width of added outline for image; 0 for no outline) dp (depth of pixa; 8 or 32 bpp; only used on first call) Return: 0 if OK, 1 on error.

Notes: (1) Before calling this function for the first time, use pixaCreate() to make the that will accumulate the pix. This is passed in each time pixSaveTiled() is called. (2) is the integer reduction factor for the input image. After reduction and possible depth conversion, the image is saved in the input pixa, along with a box that specifies the location to place it when tiled later. Disable saving the pix by setting reduction == 0. (3) and specify the location of the new pix with respect to the last one(s) that were entered. (4) specifies the depth at which all pix are saved. It can be only 8 or 32 bpp. Any colormap is removed. This is only used at the first invocation. (5) This function uses two variables from call to call. If they were static, the function would not be .so or thread safe, and furthermore, there would be interference with two or more pixa accumulating images at a time. Consequently, we use the first pix in the pixa to store and obtain both the depth and the current position of the bottom (one pixel below the lowest image raster line when laid out using the boxa). The bottom variable is stored in the input format field, which is the only field available for storing an int.

Definition at line 1050 of file writefile.c.

References boxCreate(), ERROR_INT, L_CLONE, L_INSERT, L_MAX, L_WARNING, NULL, pixaAddBox(), pixaAddPix(), pixAddBorder(), pixaGetBoxGeometry(), pixaGetCount(), pixaGetPix(), pixClone(), pixConvertTo32(), pixConvertTo8(), pixDestroy(), pixGetDepth(), pixGetDimensions(), pixGetInputFormat(), pixScale(), pixScaleToGray(), pixSetInputFormat(), and PROCNAME.

Referenced by AddTextAndSave(), main(), pixItalicWords(), pixSaveTiled(), rotateTest(), and TestProjection().

l_int32 pixSaveTiledWithText ( PIX pixs,
PIXA pixa,
l_int32  outwidth,
l_int32  newrow,
l_int32  space,
l_int32  linewidth,
L_BMF bmf,
const char *  textstr,
l_uint32  val,
l_int32  location 
)

pixSaveTiledWithText()

Input: pixs (1, 2, 4, 8, 32 bpp) pixa (the pix are accumulated here; as 32 bpp) outwidth (in pixels; use 0 to disable entirely) newrow (1 to start a new row; 0 to go on same row as previous) space (horizontal and vertical spacing, in pixels) linewidth (width of added outline for image; 0 for no outline) bmf (<optional> font struct) textstr (<optional> text string to be added) val (color to set the text) location (L_ADD_ABOVE, L_ADD_AT_TOP, L_ADD_AT_BOTTOM, L_ADD_BELOW) Return: 0 if OK, 1 on error.

Notes: (1) Before calling this function for the first time, use pixaCreate() to make the that will accumulate the pix. This is passed in each time pixSaveTiled() is called. (2) is the scaled width. After scaling, the image is saved in the input pixa, along with a box that specifies the location to place it when tiled later. Disable saving the pix by setting == 0. (3) and specify the location of the new pix with respect to the last one(s) that were entered. (4) All pix are saved as 32 bpp RGB. (5) If both and are defined, this generates a pix with the additional text; otherwise, no text is written. (6) The text is written before scaling, so it is properly antialiased in the scaled pix. However, if the pix on different calls have different widths, the size of the text will vary. (7) See pixSaveTiledOutline() for other implementation details.

Definition at line 1177 of file writefile.c.

References ERROR_INT, NULL, pixAddBorder(), pixAddSingleTextblock(), pixClone(), pixConvertTo32(), pixDestroy(), pixSaveTiled(), pixScaleToSize(), and PROCNAME.

Referenced by pixDisplayColorArray(), and PixSave().

void l_chooseDisplayProg ( l_int32  selection)

Variable Documentation

l_int32 var_DISPLAY_PROG = L_DISPLAY_WITH_XZGV [static]

Definition at line 61 of file writefile.c.

Referenced by l_chooseDisplayProg(), and pixDisplayWithTitle().

const l_int32 MAX_DISPLAY_WIDTH = 1000 [static]

Definition at line 63 of file writefile.c.

Referenced by pixDisplayWithTitle().

const l_int32 MAX_DISPLAY_HEIGHT = 800 [static]

Definition at line 64 of file writefile.c.

Referenced by pixDisplayWithTitle().

const l_int32 MAX_SIZE_FOR_PNG = 200 [static]

Definition at line 65 of file writefile.c.

Referenced by pixDisplayWithTitle().

const l_float32 DEFAULT_SCALING = 1.0 [static]

Definition at line 68 of file writefile.c.

Referenced by pixWriteMem(), and pixWriteStream().

LEPT_DLL const char* ImageFileFormatExtensions[]
Initial value:
         {"unknown",
          "bmp",
          "jpg",
          "png",
          "tif",
          "tif",
          "tif",
          "tif",
          "tif",
          "tif",
          "tif",
          "pnm",
          "ps",
          "gif",
          "jp2",
          "webp",
          "pdf",
          "default",
          ""}

Definition at line 80 of file writefile.c.

Referenced by get_format_data(), get_header_data(), getFormatExtension(), main(), pixaWriteFiles(), pixcompWriteStreamInfo(), pixReadRGBAPng(), pixWrite(), and regTestWritePixAndCheck().

struct ExtensionMap extension_map[] [static]
Initial value:
                            { { ".bmp",  IFF_BMP       },
                              { ".jpg",  IFF_JFIF_JPEG },
                              { ".jpeg", IFF_JFIF_JPEG },
                              { ".png",  IFF_PNG       },
                              { ".tif",  IFF_TIFF      },
                              { ".tiff", IFF_TIFF      },
                              { ".pnm",  IFF_PNM       },
                              { ".gif",  IFF_GIF       },
                              { ".jp2",  IFF_JP2       },
                              { ".ps",   IFF_PS        },
                              { ".pdf",  IFF_LPDF      },
                              { ".webp", IFF_WEBP      } }

Definition at line 107 of file writefile.c.

Referenced by getImpliedFileFormat().

 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines