Leptonica 1.68
C Image Processing Library

convertformat.c

Go to the documentation of this file.
00001 /*====================================================================*
00002  -  Copyright (C) 2001 Leptonica.  All rights reserved.
00003  -  This software is distributed in the hope that it will be
00004  -  useful, but with NO WARRANTY OF ANY KIND.
00005  -  No author or distributor accepts responsibility to anyone for the
00006  -  consequences of using this software, or for whether it serves any
00007  -  particular purpose or works at all, unless he or she says so in
00008  -  writing.  Everyone is granted permission to copy, modify and
00009  -  redistribute this source code, for commercial or non-commercial
00010  -  purposes, with the following restrictions: (1) the origin of this
00011  -  source code must not be misrepresented; (2) modified versions must
00012  -  be plainly marked as such; and (3) this notice may not be removed
00013  -  or altered from any source or modified source distribution.
00014  *====================================================================*/
00015 
00016 /*
00017  * convertformat.c
00018  *
00019  *   Converts an image file from one format to another.
00020  *
00021  *   Syntax: convertformat filein fileout [format]
00022  *
00023  *      where format is one of these:
00024  *
00025  *         BMP
00026  *         JPEG  (only applicable for 8 bpp or rgb)
00027  *         PNG
00028  *         TIFF
00029  *         TIFF_G4  (only applicable for 1 bpp)
00030  *         PNM
00031  *
00032  *   The output format can be chosen by the extension of fileout:
00033  *         BMP       .bmp
00034  *         JPEG      .jpg
00035  *         PNG       .png
00036  *         TIFF      .tif
00037  *         TIFF_G4   .tif
00038  *         PNM       .pnm
00039  */
00040 
00041 #include <string.h>
00042 #include "allheaders.h"
00043 
00044 main(int    argc,
00045      char **argv)
00046 {
00047 PIX         *pixs;
00048 char        *filein, *fileout, *base, *ext;
00049 const char  *format;
00050 char         error_msg[] = "Valid formats: BMP, JPEG, PNG, TIFF, TIFF_G4, PNM";
00051 l_int32      d;
00052 static char  mainName[] = "convertformat";
00053 
00054     if (argc != 3 && argc != 4) {
00055         fprintf(stderr, "Syntax: convertformat filein fileout [format]\n");
00056         fprintf(stderr, "%s\n", error_msg);
00057         fprintf(stderr, "If you don't specify a format, the output file needs");
00058         fprintf(stderr, " an extension such as:\n");
00059         fprintf(stderr, " .bmp, .jpg, .png, .tif or .pnm\n");
00060         return 1;
00061     }
00062 
00063     filein = argv[1];
00064     fileout = argv[2];
00065 
00066     if (argc == 3) {
00067         splitPathAtExtension(fileout, NULL, &ext);
00068         if (!strcmp(ext, ".bmp"))
00069             format = "BMP";
00070         else if (!strcmp(ext, ".jpg"))
00071             format = "JPEG";
00072         else if (!strcmp(ext, ".png"))
00073             format = "PNG";
00074         else if (!strcmp(ext, ".tif"))
00075             format = "TIFF_G4";
00076         else if (!strcmp(ext, ".pnm"))
00077             format = "PNM";
00078         else
00079             return ERROR_INT(error_msg, mainName, 1);
00080         lept_free(ext);
00081     }
00082     else
00083         format = argv[3];
00084 
00085     pixs = pixRead(filein);
00086     d = pixGetDepth(pixs);
00087     if (d != 1 && !strcmp(format, "TIFF_G4")) {
00088         L_WARNING("can't convert to tiff_g4; converting to tiff", mainName);
00089         format = "TIFF";
00090     }
00091     if (d < 8 && !strcmp(format, "JPEG")) {
00092         L_WARNING("can't convert to jpeg; converting to png", mainName);
00093         splitPathAtExtension(fileout, &base, &ext);
00094         fileout = stringJoin(base, ".png");
00095         format = "PNG";
00096     }
00097 
00098     if (strcmp(format, "BMP") == 0)
00099         pixWrite(fileout, pixs, IFF_BMP);
00100     else if (strcmp(format, "JPEG") == 0)
00101         pixWrite(fileout, pixs, IFF_JFIF_JPEG);
00102     else if (strcmp(format, "PNG") == 0)
00103         pixWrite(fileout, pixs, IFF_PNG);
00104     else if (strcmp(format, "TIFF") == 0)
00105         pixWrite(fileout, pixs, IFF_TIFF_ZIP);
00106     else if (strcmp(format, "TIFF_G4") == 0)
00107         pixWrite(fileout, pixs, IFF_TIFF_G4);
00108     else if (strcmp(format, "PNM") == 0)
00109         pixWrite(fileout, pixs, IFF_PNM);
00110     else
00111         return ERROR_INT(error_msg, mainName, 1);
00112 
00113     return 0;
00114 }
00115 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines