src/writefile.c
l_int32
pixWrite(const char  *filename,
         PIX         *pix,
         l_int32      format)
{
char  *fname;
FILE  *fp;

    PROCNAME("pixWrite");

    if (!pix)
        return ERROR_INT("pix not defined", procName, 1);
    if (!filename)
        return ERROR_INT("filename not defined", procName, 1);
    if (format == IFF_JP2)
        return ERROR_INT("jp2 not supported", procName, 1);

    fname = genPathname(filename, NULL);

#if  WRITE_AS_NAMED  /* Default */

    if ((fp = fopen(fname, "wb+")) == NULL) {
        FREE(fname);
        return ERROR_INT("stream not opened", procName, 1);
    }

#else  /* Add an extension to the output name if none exists */

    {l_int32  extlen;
     char    *extension, *filebuf;
        splitPathAtExtension(fname, NULL, &extension);
        extlen = strlen(extension);
        FREE(extension);
        if (extlen == 0) {
            if (format == IFF_DEFAULT || format == IFF_UNKNOWN)
                format = pixChooseOutputFormat(pix);

            filebuf = (char *)CALLOC(strlen(fname) + 10, sizeof(char));
            if (!filebuf) {
                return ERROR_INT("filebuf not made", procName, 1);
                FREE(fname);
            }
            strncpy(filebuf, fname, strlen(fname));
            strcat(filebuf, ".");
            strcat(filebuf, ImageFileFormatExtensions[format]);
        }
        else
            filebuf = (char *)fname;

        fp = fopen(filebuf, "wb+");
        if (filebuf != fname)
            FREE(filebuf);
        if (fp == NULL) {
            FREE(fname);
            return ERROR_INT("stream not opened", procName, 1);
        }
    }

#endif  /* WRITE_AS_NAMED */

    FREE(fname);
    if (pixWriteStream(fp, pix, format)) {
        fclose(fp);
        return ERROR_INT("pix not written to stream", procName, 1);
    }

        /* Close the stream except if GIF under windows, because
         * EGifCloseFile() closes the windows file stream! */
    if (format != IFF_GIF)
        fclose(fp);
#ifndef _WIN32
    else  /* gif file */
        fclose(fp);
#endif  /* ! _WIN32 */

    return 0;
}
/*!
 *  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 @filename.
 *          (b) If WRITE_AS_NAMED is defined to 0, it's a bit fancier.
 *              Then, if @filename 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.
 */
pixWrite()
Input:  filename
pix
format  (defined in imageio.h)
Return: 0 if OK; 1 on error

(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 @filename.
(b) If WRITE_AS_NAMED is defined to 0, it's a bit fancier.
Then, if @filename 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.
pixWrite () Input : filename pix format ( defined in imageio . h ) Return : 0 if OK ; 1 on error ( 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 @filename . ( b ) If WRITE_AS_NAMED is defined to 0 it ' s a bit fancier . Then if @filename 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 .
l_int32
pixWrite(const char  *filename,
         PIX         *pix,
         l_int32      format)
{
char  *fname;
FILE  *fp;
    PROCNAME("pixWrite");
    if (!pix)
        return ERROR_INT("pix not defined", procName, 1);
    if (!filename)
        return ERROR_INT("filename not defined", procName, 1);
    if (format == IFF_JP2)
        return ERROR_INT("jp2 not supported", procName, 1);
    fname = genPathname(filename, NULL);
#if  WRITE_AS_NAMED  
    if ((fp = fopen(fname, "wb+")) == NULL) {
        FREE(fname);
        return ERROR_INT("stream not opened", procName, 1);
    }
#else  
    {l_int32  extlen;
     char    *extension, *filebuf;
        splitPathAtExtension(fname, NULL, &extension);
        extlen = strlen(extension);
        FREE(extension);
        if (extlen == 0) {
            if (format == IFF_DEFAULT || format == IFF_UNKNOWN)
                format = pixChooseOutputFormat(pix);
            filebuf = (char *)CALLOC(strlen(fname) + 10, sizeof(char));
            if (!filebuf) {
                return ERROR_INT("filebuf not made", procName, 1);
                FREE(fname);
            }
            strncpy(filebuf, fname, strlen(fname));
            strcat(filebuf, ".");
            strcat(filebuf, ImageFileFormatExtensions[format]);
        }
        else
            filebuf = (char *)fname;
        fp = fopen(filebuf, "wb+");
        if (filebuf != fname)
            FREE(filebuf);
        if (fp == NULL) {
            FREE(fname);
            return ERROR_INT("stream not opened", procName, 1);
        }
    }
#endif  
    FREE(fname);
    if (pixWriteStream(fp, pix, format)) {
        fclose(fp);
        return ERROR_INT("pix not written to stream", procName, 1);
    }
    if (format != IFF_GIF)
        fclose(fp);
#ifndef _WIN32
    else  
        fclose(fp);
#endif  
    return 0;
}
l_int32 pixWrite ( const char * filename , PIX * pix , l_int32 format ) { char * fname ; FILE * fp ; PROCNAME ( " " ) ; if ( ! pix ) return ERROR_INT ( " " , procName , 1 ) ; if ( ! filename ) return ERROR_INT ( " " , procName , 1 ) ; if ( format == IFF_JP2 ) return ERROR_INT ( " " , procName , 1 ) ; fname = genPathname ( filename , NULL ) ; #if WRITE_AS_NAMED if ( ( fp = fopen ( fname , " " ) ) == NULL ) { FREE ( fname ) ; return ERROR_INT ( " " , procName , 1 ) ; } #else { l_int32 extlen ; char * extension , * filebuf ; splitPathAtExtension ( fname , NULL , & extension ) ; extlen = strlen ( extension ) ; FREE ( extension ) ; if ( extlen == 0 ) { if ( format == IFF_DEFAULT || format == IFF_UNKNOWN ) format = pixChooseOutputFormat ( pix ) ; filebuf = ( char * ) CALLOC ( strlen ( fname ) + 10 , sizeof ( char ) ) ; if ( ! filebuf ) { return ERROR_INT ( " " , procName , 1 ) ; FREE ( fname ) ; } strncpy ( filebuf , fname , strlen ( fname ) ) ; strcat ( filebuf , " " ) ; strcat ( filebuf , ImageFileFormatExtensions [ format ] ) ; } else filebuf = ( char * ) fname ; fp = fopen ( filebuf , " " ) ; if ( filebuf != fname ) FREE ( filebuf ) ; if ( fp == NULL ) { FREE ( fname ) ; return ERROR_INT ( " " , procName , 1 ) ; } } #endif FREE ( fname ) ; if ( pixWriteStream ( fp , pix , format ) ) { fclose ( fp ) ; return ERROR_INT ( " " , procName , 1 ) ; } if ( format != IFF_GIF ) fclose ( fp ) ; #ifndef _WIN32 else fclose ( fp ) ; #endif return 0 ; }
pixWrite()
      Input:  filename
              pix
              format  (defined in imageio.h)
      Return: 0 if OK; 1 on error
pixWrite () Input : filename pix format ( defined in imageio . h ) Return : 0 if OK ; 1 on error
/* Default */ /* Add an extension to the output name if none exists */ /* WRITE_AS_NAMED */ /* Close the stream except if GIF under windows, because
         * EGifCloseFile() closes the windows file stream! */ /* gif file */ /* ! _WIN32 */