%% options copyright owner = Dirk Krause copyright year = 2011-2014 license = bsd %% header %% module #include "dk3all.h" #include "dkt-version.h" #include "dk3bif.h" #include "dk3bmeo.h" #include "dk3bmeps.h" #include "dk3bm.h" $!trace-include /** Help text shown if no help file is found. */ static dkChar const * const bmeps3_default_help_text[] = { $!text file=bmpp.txt,macro=dkT NAME bmpp - Bitmap to PS/PDF converter SYNOPSIS bmpp [-l[.][,[=]...]] [] DESCRIPTION The program converts bitmap images from PNG, JPEG, or TIFF to PS/EPS and PDF. Different output modes are available: - object to produce an object which is embedded into a text or drawing by a text processing or drawing software. - image to produce an image for stand-alone viewing. - document to fit the output to a given paper size. OPTIONS -h --help shows this help text. -v --version shows the version number. -L --license-terms shows the license terms. -t specifies the image type, "png", "jpeg", or "tiff". The image type must be specified when processing standard input or if the input file name suffix does not indicate the type. -m works like "make" and runs only the conversions necessary when processing a directory. Conversion is skipped if the output file for an input file already exists and is up to date. -s writes output to standard output instead of an output file. -o [=] specifies detailed conversion options. NAME=VALUE PAIRS The key=value pairs allow to set detailed conversion options. For boolean settings the value can be omitted, the presence of the name sets the option to active. The following keys (and values) can be used: level= specifies the PS level, "2" or "3". Default: 3. color[=] enables/disables color output for PS output. For PDF output color is always enabled. This setting is ignored when passing DCT data from JPEG files to output directly. Default: On. dsc[=] enables/disables DSC comments when producing PS/EPS output. Default: Off, except for ps.document. draft[=] enables/disables draft mode. Default: Off. predictor= chooses a predictor for flate compressions, "tiff", "sub", "up", "average", or "paeth". Default: none. duplex[=] enables/disables duplex mode when producing documents. Default: Off. tumble[=] enables/disables tumble in addition to duplex mode. Default: Off. dct[=] enables/disables direct pass-through of DCT encoded data found in JPEG files. Default: On. bg=::[:] specifies a default background RGB color to be used if the input file does not contain a background color chunk. The optional boolean value at the end can be used to enforce this background over the background chunk. RGB values are in the range 0...255. Default: White, off. interpolation[=] enables/disables image interpolation. Default: On. jpeg-interpolation[=] enables/disables image interpolation if DCT data is passed through directly. Default: Off. resolution= chooses a resolution, either "1:1" (use a 1 point x 1 point square for each source pixel), "chunk" (use resolution data from image) or an integer value for dpi. Default: 1:1 for objects, chunk for images, fit to paper size for documents. paper= specify a paper size for documents. The paper size must be configured in a dk3paper.conf file. Only one from "resolution" or "paper" can be used. make[=] enables/disables make mode when processing directories. Default: Off. bpc[=] attempt to reduce the number of bits per component. Default: On. RETURN VALUE The program returns exit status code 0 on success, all other status codes indicate an error. EXAMPLES bmpp -lps.document,level=2,paper=A4,duplex,bpc myfax.tif converts a fax TIFF file "myfax.tif" to "myfax.ps" for a PS 2 printer. bmpp -l pdf.document,paper=A4,duplex fax.tif creates a PDF file fax.pdf. bmpp -l ps.object screenshot.png creates a PS object file screenshot.ps for use with latex/dvips. bmpp -l pdf.object screenshot.png creates a PDF object file screenshot.pdf for use with pdflatex. bmpp -l ps.image file.png creates a PS image file.ps. bmpp -l pdf.image file.png creates a PDF image file.pdf. FILES dk3paper.conf If paper=... the named paper size must be configured in a dk3paper.conf file. RESTRICTIONS The program uses the TIFFReadRGBAImage() function from the libtiff library to read TIFF images. The input file must meet the following conditions: - The file must be compliant to the "TIFF baseline v6.0 standard". - The number of bits of component must be 8 or less. - The frames must not be too large, the system must be able to allocate the 4*w*h bytes needed for image date in one piece. TIFF refers to a group of different compression and encoding mechanisms. A TIFF file is a sequence of data chunks, each data chunk contains a tag (chunk type identifier). A lot of tags is defined in standards, i.e. the "TIFF baseline v6.0 standard". Some software vendors add vendor-specific or non-standard tags when writing TIFF files. So when processing TIFF files in bmpp, you should not be surprised to see warnings or error messages about unknown TIFF tags. Neither bmpp nor the tifflib library are to blame for this, it's the responsibility of the people adding the non-standard tags. Sometimes bmpp can read the image despite of the warnings/errors. If you create bitmaps you want to process with bmpp, you should create PNG images instead of TIFF images if you have the choice. AUTHOR Dirk Krause HISTORY The bmpp program replaces the bmeps program found in previous versions of dktools. COPYRIGHT AND LICENSE Run bmpp --license-terms to see the license conditions. SEE ALSO http://dktools.sourceforge.net $!end }; /** Strings used to show version number. */ static dkChar const * const dk3bm_version_strings[] = { dkT("bmpp "), DKT_VERSION }; /** Options used by bmeps. */ static dk3_option_t const bmeps3_options[] = { { dkT('l'), dkT("language"), 1 }, { dkT('m'), dkT("make"), 0 }, { dkT('s'), dkT("stdout"), 0 }, { dkT('t'), dkT("type"), 1 }, { dkT('h'), dkT("help"), 0 }, { dkT('v'), dkT("version"), 0 }, { dkT('L'), dkT("license-terms"), 0 }, { dkT('\0'),dkT("license"), 0 } }; /** Number of options in @a bmeps3_options. */ static size_t const bmeps3_szoptions = sizeof(bmeps3_options)/sizeof(dk3_option_t); /** Names of supported input types. */ static dkChar const * const bmeps3_image_type_names[] = { $!string-table macro=dkT png jpg jpeg tif tiff $!end }; /** Suffixes for output files. */ static dkChar const * const bmeps3_output_suffixes[] = { dkT(".ps"), dkT(".eps"), dkT(".pdf"), dkT(".bb") }; /** Strings, not localized. */ static dkChar const * const bmeps3_kw[] = { $!string-table macro=dkT # # 0: Program name # dkt-3 # # 1: String table name # bmeps3.str # # 2: Option keyword # option $!end }; /** Strings, replaced by localized version. */ static dkChar const * const bmeps3_message_texts[] = { $!string-table macro=dkT $!end }; /** License overview text. */ static dkChar const * const bmeps3_overview_license[] = { dkT(""), dkT("Overview"), dkT("========"), dkT("This software uses code from the following projects either directly"), dkT("or as a library:"), dkT(""), dkT("dktools\t\tDirk Krause's tools and libraries."), dkT("\t\tSee http://dktools.sourceforge.net for information."), #if DK3_HAVE_PNG_H dkT(""), dkT("libpng\t\tPNG reference library."), dkT("\t\tSee http://www.libpng.org/pub/png/libpng.html for information."), #endif #if DK3_HAVE_JPEGLIB_H dkT(""), dkT("jpeg\t\tIndependent JPEG groups free JPEG library."), dkT("\t\tSee http://www.ijg.org/ for information."), #endif #if DK3_HAVE_TIFF_H dkT(""), dkT("libtiff\t\tTIFF library."), dkT("\t\tSee http://www.remotesensing.org/libtiff/ for information."), #endif #if DK3_HAVE_ZLIB_H dkT(""), dkT("zlib\t\tCompression library."), dkT("\t\tSee http://www.zlib.net/ for information."), #endif dkT(""), dkT("Your use of the bmpp/wxmbpp program must be in compliance with the"), dkT("license conditions for the dktools project and the license conditions"), dkT("for all the libraries used by bmpp."), dkT(""), NULL }; /** License terms for the dktools project itself. */ static dkChar const * const bmpp_dk_license[] = { $!text macro=dkT License for the dktools project =============================== Copyright (c) 2011-2013, Dirk Krause All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder(s) nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. $!end }; #if DK3_HAVE_PNG_H static dkChar const * const bmpp_png_license[] = { $!text macro=dkT libpng license ============== This copy of the libpng notices is provided for your convenience. In case of any discrepancy between this copy and the notices in the file png.h that is included in the libpng distribution, the latter shall prevail. COPYRIGHT NOTICE, DISCLAIMER, and LICENSE: If you modify libpng you may insert additional notices immediately following this sentence. This code is released under the libpng license. libpng versions 1.2.6, August 15, 2004, through 1.2.46, July 9, 2011, are Copyright (c) 2004, 2006-2009 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-1.2.5 with the following individual added to the list of Contributing Authors Cosmin Truta libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-1.0.6 with the following individuals added to the list of Contributing Authors Simon-Pierre Cadieux Eric S. Raymond Gilles Vollant and with the following additions to the disclaimer: There is no warranty against interference with your enjoyment of the library or against infringement. There is no warranty that our efforts or the library will fulfill any of your particular purposes or needs. This library is provided with all faults, and the entire risk of satisfactory quality, performance, accuracy, and effort is with the user. libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are distributed according to the same disclaimer and license as libpng-0.96, with the following individuals added to the list of Contributing Authors: Tom Lane Glenn Randers-Pehrson Willem van Schaik libpng versions 0.89, June 1996, through 0.96, May 1997, are Copyright (c) 1996, 1997 Andreas Dilger Distributed according to the same disclaimer and license as libpng-0.88, with the following individuals added to the list of Contributing Authors: John Bowler Kevin Bracey Sam Bushell Magnus Holmgren Greg Roelofs Tom Tanner libpng versions 0.5, May 1995, through 0.88, January 1996, are Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc. For the purposes of this copyright and license, "Contributing Authors" is defined as the following set of individuals: Andreas Dilger Dave Martindale Guy Eric Schalnat Paul Schmidt Tim Wegner The PNG Reference Library is supplied "AS IS". The Contributing Authors and Group 42, Inc. disclaim all warranties, expressed or implied, including, without limitation, the warranties of merchantability and of fitness for any purpose. The Contributing Authors and Group 42, Inc. assume no liability for direct, indirect, incidental, special, exemplary, or consequential damages, which may result from the use of the PNG Reference Library, even if advised of the possibility of such damage. Permission is hereby granted to use, copy, modify, and distribute this source code, or portions hereof, for any purpose, without fee, subject to the following restrictions: 1. The origin of this source code must not be misrepresented. 2. Altered versions must be plainly marked as such and must not be misrepresented as being the original source. 3. This Copyright notice may not be removed or altered from any source or altered source distribution. The Contributing Authors and Group 42, Inc. specifically permit, without fee, and encourage the use of this source code as a component to supporting the PNG file format in commercial products. If you use this source code in a product, acknowledgment is not required but would be appreciated. A "png_get_copyright" function is available, for convenient use in "about" boxes and the like: printf("%s",png_get_copyright(NULL)); Also, the PNG logo (in PNG format, of course) is supplied in the files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31). Libpng is OSI Certified Open Source Software. OSI Certified Open Source is a certification mark of the Open Source Initiative. Glenn Randers-Pehrson glennrp at users.sourceforge.net July 9, 2011 $!end }; #endif #if DK3_HAVE_JPEGLIB_H static dkChar const * const bmpp_jpeg_license[] = { $!text macro=dkT jpeglib license =============== In plain English: 1. We don't promise that this software works. (But if you find any bugs, please let us know!) 2. You can use this software for whatever you want. You don't have to pay us. 3. You may not pretend that you wrote this software. If you use it in a program, you must acknowledge somewhere in your documentation that you've used the IJG code. In legalese: The authors make NO WARRANTY or representation, either express or implied, with respect to this software, its quality, accuracy, merchantability, or fitness for a particular purpose. This software is provided "AS IS", and you, its user, assume the entire risk as to its quality and accuracy. This software is copyright (C) 1991-1998, Thomas G. Lane. All Rights Reserved except as specified below. Permission is hereby granted to use, copy, modify, and distribute this software (or portions thereof) for any purpose, without fee, subject to these conditions: (1) If any part of the source code for this software is distributed, then this README file must be included, with this copyright and no-warranty notice unaltered; and any additions, deletions, or changes to the original files must be clearly indicated in accompanying documentation. (2) If only executable code is distributed, then the accompanying documentation must state that "this software is based in part on the work of the Independent JPEG Group". (3) Permission for use of this software is granted only if the user accepts full responsibility for any undesirable consequences; the authors accept NO LIABILITY for damages of any kind. These conditions apply to any software derived from or based on the IJG code, not just to the unmodified library. If you use our work, you ought to acknowledge us. Permission is NOT granted for the use of any IJG author's name or company name in advertising or publicity relating to this software or products derived from it. This software may be referred to only as "the Independent JPEG Group's software". We specifically permit and encourage the use of this software as the basis of commercial products, provided that all warranty or liability claims are assumed by the product vendor. ansi2knr.c is included in this distribution by permission of L. Peter Deutsch, sole proprietor of its copyright holder, Aladdin Enterprises of Menlo Park, CA. ansi2knr.c is NOT covered by the above copyright and conditions, but instead by the usual distribution terms of the Free Software Foundation; principally, that you must include source code if you redistribute it. (See the file ansi2knr.c for full details.) However, since ansi2knr.c is not needed as part of any program generated from the IJG code, this does not limit you more than the foregoing paragraphs do. The Unix configuration script "configure" was produced with GNU Autoconf. It is copyright by the Free Software Foundation but is freely distributable. The same holds for its supporting scripts (config.guess, config.sub, ltconfig, ltmain.sh). Another support script, install-sh, is copyright by M.I.T. but is also freely distributable. It appears that the arithmetic coding option of the JPEG spec is covered by patents owned by IBM, AT&T, and Mitsubishi. Hence arithmetic coding cannot legally be used without obtaining one or more licenses. For this reason, support for arithmetic coding has been removed from the free JPEG software. (Since arithmetic coding provides only a marginal gain over the unpatented Huffman mode, it is unlikely that very many implementations will support it.) So far as we are aware, there are no patent restrictions on the remaining code. The IJG distribution formerly included code to read and write GIF files. To avoid entanglement with the Unisys LZW patent, GIF reading support has been removed altogether, and the GIF writer has been simplified to produce "uncompressed GIFs". This technique does not use the LZW algorithm; the resulting GIF files are larger than usual, but are readable by all standard GIF decoders. We are required to state that "The Graphics Interchange Format(c) is the Copyright property of CompuServe Incorporated. GIF(sm) is a Service Mark property of CompuServe Incorporated." $!end }; #endif #if DK3_HAVE_TIFF_H static dkChar const * const bmpp_tiff_license[] = { $!text macro=dkT libtiff license =============== Copyright (c) 1988-1997 Sam Leffler Copyright (c) 1991-1997 Silicon Graphics, Inc. Permission to use, copy, modify, distribute, and sell this software and its documentation for any purpose is hereby granted without fee, provided that (i) the above copyright notices and this permission notice appear in all copies of the software and related documentation, and (ii) the names of Sam Leffler and Silicon Graphics may not be used in any advertising or publicity relating to the software without the specific, prior written permission of Sam Leffler and Silicon Graphics. THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. $!end }; #endif #if DK3_HAVE_ZLIB_H static dkChar const * const bmpp_zlib_license[] = { $!text macro=dkT zlib license ============ (C) 1995-2004 Jean-loup Gailly and Mark Adler This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Jean-loup Gailly Mark Adler jloup@gzip.org madler@alumni.caltech.edu If you use the zlib library in a product, we would appreciate *not* receiving lengthy legal documents to sign. The sources are provided for free but without warranty of any kind. The library has been entirely written by Jean-loup Gailly and Mark Adler; it does not include third-party code. If you redistribute modified sources, we would appreciate that you include in the file ChangeLog history information documenting your changes. Please read the FAQ for more information on the distribution of modified source versions. $!end }; #endif /** Show one section of text. @param ltext Text to show. */ static void bmeps3_show_one_license(dkChar const * const *ltext) { dkChar const * const * sptr; sptr = ltext; while(*sptr) { dk3sf_fputs(*(sptr++), stdout); dk3sf_fputc(dkT('\n'), stdout); } } /** Show license for the program itself and for the used libraries. */ static void bmeps3_show_license(void) { bmeps3_show_one_license(bmeps3_overview_license); bmeps3_show_one_license(bmpp_dk_license); #if DK3_HAVE_PNG_H bmeps3_show_one_license(bmpp_png_license); #endif #if DK3_HAVE_JPEGLIB_H bmeps3_show_one_license(bmpp_jpeg_license); #endif #if DK3_HAVE_TIFF_H bmeps3_show_one_license(bmpp_tiff_license); #endif #if DK3_HAVE_ZLIB_H bmeps3_show_one_license(bmpp_zlib_license); #endif } /** Process a complete directory. @param bmeo Conversion options. @param fnb File name for directory. @param app Application structure for diagnostics. @return 1 on success, 0 on error. */ static int bmeps3_process_directory( dk3_bm_eps_options_t *bmeo, dkChar const *fnb, dk3_app_t *app ) { dkChar fni[DK3_MAX_PATH]; /* Input file name. */ dkChar fno[DK3_MAX_PATH]; /* output file name. */ dkChar const * const *msg; /* Localized message texts. */ dk3_dir_t *dir; /* Directory. */ dkChar const *en; /* Full file name. */ dkChar const *sn; /* Short file name. */ dkChar const *nsptr; /* Suffix output file. */ dkChar const *oldsourcefile; /* Old source file name. */ dkChar *sptr; /* Suffix input file. */ dk3_stat_t const *es; /* Stat info input file. */ dk3_stat_t stb; /* Stat buffer output file. */ int mustrun; /* Flag: Must convert file. */ int sit; /* Image type. */ int back = 0; $? "+ bmeps3_process_directory" msg = dk3app_messages( app, dk3bmep_str_get_string_table_name(), (dkChar const **)dk3bmep_str_get_message_texts() ); if(!(msg)) { msg = dk3bmep_str_get_message_texts(); } oldsourcefile = dk3app_get_source_file(app); dir = dk3dir_open_app(fnb, app); if(dir) { $? ". dr: %d", bmeo->dr back = 1; $? ". bmeo.app: %s", TR_PTR(bmeo->app) while(dk3dir_get_next_file(dir)) { $? ". yet another entry" en = dk3dir_get_fullname(dir); $? ". entry \"%s\"", en sn = dk3dir_get_shortname(dir); es = dk3dir_get_stat(dir); if((en) && (es)) { $? ". have name and stat" if(dk3bif_type_supported(sit = dk3bif_check_file_name(en))) { $? ". ok" if(((es->ft) & (~(DK3_FT_SYMLINK))) == DK3_FT_REGULAR) { $? ".ft" mustrun = 1; if(bmeo->make) { if(dk3str_len(en) < DK3_SIZEOF(fni,dkChar)) { dk3str_cpy_not_overlapped(fni, en); dk3str_cpy_not_overlapped(fno, en); sptr = dk3str_get_suffix(fno); if(sptr) { *sptr = dkT('\0'); $? ". dr: %d", bmeo->dr if((bmeo->dr >= 0) && (bmeo->dr <= 3)) { nsptr = bmeps3_output_suffixes[bmeo->dr]; if((dk3str_len(fno) + dk3str_len(nsptr)) < DK3_SIZEOF(fno,dkChar) ) { dk3str_cpy_not_overlapped(sptr, nsptr); $? ". check \"%s\"", fno if(dk3sf_stat_app(&stb, fno, NULL)) { if(stb.mod > es->mod) { mustrun = 0; } } } } } } } if(mustrun) { $? ". run for \"%s\" dr: %d", en, bmeo->dr dk3app_log_3(app, DK3_LL_PROGRESS, msg, 41, 42, ((sn) ? sn : en)); dk3app_set_source_file(app, ((sn) ? sn : en)); if(!dk3bm_process_file_name(bmeo,en,sit,0,1000,NULL)) { back = 0; } dk3app_set_source_file(app, oldsourcefile); dk3app_log_3(app, DK3_LL_PROGRESS, msg, 43, 44, ((sn) ? sn : en)); } else { $? ". no need to run for \"%s\"", en dk3app_log_3(app, DK3_LL_PROGRESS, msg, 45, 46, ((sn) ? sn : en)); } } else { $? ". not a regular file" } } else { $? ". no need to process this file" } } else { $? "! name or stat missing" back = 0; /* BUG: Should be handled by dk3dir. */ } } dk3dir_close(dir); } else { $? "! failed to open dir" /* ERROR: Failed to open directory! */ } dk3app_set_source_file(app, oldsourcefile); $? "- bmeps3_process_directory %d", back return back; } /** Copy contents of one file to another. @param fipoout Destination file. @param fipoin Source file. @param app Application structure for diagnostics. @return 1 on success, 0 on error. */ static int bmeps3_copy_files(FILE *fipoout, FILE *fipoin, dk3_app_t *app) { char bu[4096]; /* Buffer for data. */ size_t rb; /* Number of bytes read. */ int cc; /* Flag: Can continue. */ int back = 1; do { cc = 0; rb = dk3sf_fread_app(bu, 1, sizeof(bu), fipoin, app); if(rb > 0) { cc = 1; if(!dk3sf_fwrite_app(bu, 1, rb, fipoout, app)) { back = 0; cc = 0; } } } while(cc); return back; } /** Run with application structure and localized messages. @param app Application structure. @param msg Localized messages. @param optset Option set. @return 1 on success, 0 on error. */ static int bmeps3_run_with_optset( dk3_app_t *app, dkChar const * const *msg, dk3_option_set_t *optset ) { dkChar fnb[DK3_MAX_PATH]; /* File name. */ dk3_bm_eps_options_t bmeo; /* Conversion option set. */ dk3_stat_t stb; /* Stat buffer. */ dk3_dir_t *fne; /* File name expander. */ dk3_stat_t const *es; /* Entry (file) stat information. */ FILE *fipo; /* Temporary file. */ dkChar const *larg; /* Option argument. */ dkChar const *fo; /* Further opt currently processed. */ dkChar const *en; /* Entry (file) name. */ int fonum; /* Number of further options. */ int i; /* Traverse options and arguments. */ int sit; /* Standard input image type. */ int numargs; /* Number of command line args. */ int found; /* Flag: At least one file found. */ #if DK3_ON_WINDOWS int oldmode; /* Stdin mode before start. */ #endif int back = 0; $? "+ bmeps3_run_with_optset" sit = DK3_BIF_IMAGE_TYPE_UNKNOWN; dk3bmeo_init(&bmeo); bmeo.app = app; $? ". app: %s", TR_PTR(app) back = 1; $? ". dr: %d", bmeo.dr if(dk3opt_is_set(optset, dkT('l'))) { larg = dk3opt_get_short_arg(optset, dkT('l')); if(larg) { if(!dk3bmeo_set_language(&bmeo, larg, app)) { back = 0; /* ERROR during option processing! */ } bmeo.app = app; } else { /* BUG: Option -l set, but no argument. Should be handled by dk3opt. */ } } $? ". dr: %d", bmeo.dr if(dk3opt_is_set(optset, dkT('m'))) { bmeo.make = 1; } if(dk3opt_is_set(optset, dkT('s'))) { bmeo.to_stdout = 1; } if(dk3opt_is_set(optset, dkT('t'))) { larg = dk3opt_get_short_arg(optset, dkT('t')); if(larg) { switch(dk3str_array_index(bmeps3_image_type_names, larg, 0)) { case 0: { sit = DK3_BIF_IMAGE_TYPE_PNG; } break; case 1: case 2: { sit = DK3_BIF_IMAGE_TYPE_JPEG; } break; case 3: case 4: { sit = DK3_BIF_IMAGE_TYPE_TIFF; } break; default: { /* ERROR: Unknown file type ... */ dk3app_log_3(app, DK3_LL_ERROR, msg, 63, 64, larg); back = 0; } break; } } else { /* BUG: Option -t set, but no argument. Should be handled by dk3opt. */ } } if(back) { if((fonum = dk3opt_get_num_fo(optset)) > 0) { for(i = 0; i < fonum; i++) { fo = dk3opt_get_fo(optset, i); if(fo) { if(!dk3bmeo_apply_option(&bmeo, fo, msg)) { back = 0; /* ERROR: Error while adding option! */ } } else { /* BUG: Option not available. Should be handled by dk3opt. */ } } } } $? ". dr: %d", bmeo.dr if(back) { if(dk3opt_is_set(optset, dkT('h')) || dk3opt_is_set(optset, dkT('v')) || dk3opt_is_set(optset, dkT('L')) ) { if(dk3opt_is_set(optset, dkT('v'))) { /* Print version number */ dk3sf_initialize_stdout(); dk3sf_fputs(dk3bm_version_strings[0], stdout); dk3sf_fputs(dk3bm_version_strings[1], stdout); dk3sf_fputc(dkT('\n'), stdout); } if(dk3opt_is_set(optset, dkT('L'))) { dk3sf_initialize_stdout(); bmeps3_show_license(); } else { if(dk3opt_is_set_long(optset, dkT("license"))) { dk3sf_initialize_stdout(); bmeps3_show_license(); } } if(dk3opt_is_set(optset, dkT('h'))) { dk3sf_initialize_stdout(); dk3app_help(app, dkT("bmpp.txt"), bmeps3_default_help_text); } } else { dk3bmeo_check(&bmeo, 0, app, NULL); $? ". dr: %d", bmeo.dr numargs = dk3opt_get_num_args(optset); if(numargs > 0) { $? ". file and/or directory names." for(i = 0; i < numargs; i++) { larg = dk3opt_get_arg(optset, i); if(larg) { if(dk3str_len(larg) < DK3_SIZEOF(fnb,dkChar)) { dk3str_cpy_not_overlapped(fnb, larg); dk3str_correct_filename(fnb); if(dk3sf_must_expand(fnb)) { $? ". must expand fnb" found = 0; fne = dk3dir_fne_open_app(fnb, app); if(fne) { while(dk3dir_get_next_file(fne)) { en = dk3dir_get_fullname(fne); es = dk3dir_get_stat(fne); if((en) && (es)) { if(((es->ft) & (~(DK3_FT_SYMLINK))) == DK3_FT_REGULAR) { sit = DK3_BIF_IMAGE_TYPE_UNKNOWN; if(!dk3bm_process_file_name(&bmeo,en,sit,0,1000,NULL)) { back = 0; } } else { /* ERROR: Not a regular file! */ dk3app_log_i3(app, DK3_LL_ERROR, 255, 256, fnb); } } else { /* BUG: Should be handled by dk3dir. */ back = 0; } } dk3dir_close(fne); if(!(found)) { back = 0; /* ERROR: No such file! */ dk3app_log_i3(app, DK3_LL_ERROR, 215, 216, fnb); } } else { /* ERROR: Failed to expand file name! */ back = 0; } } else { $? ". use fnb directly" if(dk3sf_stat_app(&stb, fnb, app)) { switch((stb.ft) & (~(DK3_FT_SYMLINK))) { case DK3_FT_DIRECTORY: { if(!bmeps3_process_directory(&bmeo, fnb, app)) { back = 0; } } break; case DK3_FT_REGULAR: { if(!dk3bm_process_file_name(&bmeo,fnb,sit,0,1000,NULL)) { back = 0; } } break; default: { /* ERROR: Illegal file type! */ back = 0; dk3app_log_3(app, DK3_LL_ERROR, msg, 65, 66, fnb); } break; } } else { /* ERROR: No information about file! */ back = 0; } } } else { /* ERROR: File name too long! */ back = 0; dk3app_log_i3(app, DK3_LL_ERROR, 65, 66, larg); } } else { $? "! arg not available" /* BUG: Option not available, should be handled by dk3opt. */ } } } else { $? ". no arguments, process standard input." if(sit != DK3_BIF_IMAGE_TYPE_UNKNOWN) { if(dk3app_get_temp_file_name(app, fnb, DK3_SIZEOF(fnb,dkChar))) { fipo = dk3sf_fopen_app(fnb, dk3app_not_localized(53), app); if(fipo) { #if DK3_ON_WINDOWS oldmode = _setmode(_fileno(stdin), _O_BINARY); #endif i = bmeps3_copy_files(fipo, stdin, app); #if DK3_ON_WINDOWS _setmode(_fileno(stdin), oldmode); #endif #if VERSION_BEFORE_2012_04_19 fclose(fipo); #else if(!dk3sf_fclose_fn_app(fipo, fnb, app)) { i = 0; } #endif if(i) { bmeo.to_stdout = 1; back = dk3bm_process_file_name(&bmeo,fnb,sit,0,1000,NULL); } } else { /* ERROR: Failed to open file for writing! */ back = 0; } } else { back = 0; /* ERROR: Failed to obtain temporary file name! */ } } else { /* ERROR: No file type specified for standard input! */ back = 0; dk3app_log_1(app, DK3_LL_ERROR, msg, 67); } } } } $? "- bmeps3_run_with_optset %d", back return back; } /** Run with application structure and localized messages. @param app Application structure. @param msg Localized messages. @return 1 on success, 0 on error. */ static int bmeps3_run_with_app(dk3_app_t *app, dkChar const * const *msg) { dk3_option_set_t *optset; /* Options provided on command line. */ int back = 0; $? "+ bmeps3_run_with_app" optset = dk3opt_open_from_app( bmeps3_options, bmeps3_szoptions, dkT('o'), bmeps3_kw[2], app ); if(optset) { $? ". optset" if(0 == dk3opt_get_error_code(optset)) { $? ". optset no error" back = bmeps3_run_with_optset(app, msg, optset); } else { $? "! optset error" /* ERROR: Error in options */ } dk3opt_close(optset); } else { $? "! optset" } $? "- bmeps3_run_with_app %d", back return back; } /** The main() function. @param argc Number of command line arguments. @param argv Command line arguments array. @return 0 on success, any other value indicates an error. */ DK3_MAIN { dk3_app_t *app; /* Application structure. */ dkChar const * const *msg; /* Localizd messages. */ int exval; /* Exit code. */ $!trace-init bmeps3.deb $? "+ main" exval = 1; app = dk3app_open_command( argc, (dkChar const * const *)argv, bmeps3_kw[0] ); if(app) { $? ". app ok" msg = dk3app_messages( app, dk3bmep_str_get_string_table_name(), (dkChar const **)dk3bmep_str_get_message_texts() ); if(msg) { $? ". msg ok" if(bmeps3_run_with_app(app, msg)) { exval = 0; } } else { $? "! msg" /* BUG: Should not happen, as the fucntion returns the default array! */ } dk3app_close(app); } else { $? "! app" /* ERROR: Memory */ fputs("ERROR: Not enough memory (RAM/swap space)!\n", stderr); fflush(stderr); } $? "- main %d", exval $!trace-end fflush(stdout); exit(exval); return exval; }