/* * The Npic library * * Copyright (C) 2003 Edouard Thiel * * This library is free software under the terms of the * GNU Lesser General Public License (LGPL) version 2.1. */ /* * files_byext.c - 22/11/2010 * * Read or write files by extensions */ #include /*--------------------- P U B L I C - I N T E R F A C E ----------------------*/ /* * Write image in file according to its extension. * Do nothing if image is not ok. * The filename can contain hints, see files_hints.c * * Return NPIC_SUCCESS, else error code. Verbose. */ int NpicWriteImage (Npic_image *np, const char *filename) { Npic_file_hints h; int k, res; if (NpicImageIsOK (np, __func__) != NPIC_SUCCESS) return NPIC_ERR_NOT_OK; /* After this call, memory must be freed with NpicFileFreeHints() */ k = NpicFileParseHints (filename, &h); if (k != NPIC_SUCCESS) NpicError (__func__, k, ": file \"%s\".\n", filename); if (strcmp (h.extension, ".npz") == 0 && h.comp == NPIC_COMPRESS_NONE) res = NpicWriteNPZ (np, h.realname); else if (strcmp (h.extension, ".vol") == 0) res = NpicWriteVOL (np, h.realname, h.comp); else if (strcmp (h.extension, ".pan") == 0) res = NpicWritePAN (np, h.realname, h.comp); else if (strcmp (h.extension, ".pnm") == 0 || strcmp (h.extension, ".pbm") == 0 || strcmp (h.extension, ".pgm") == 0 || strcmp (h.extension, ".ppm") == 0) res = NpicWritePNMwithHints(np, &h); else res = NpicError (__func__, NPIC_ERR_BAD_EXT, ": file \"%s\".\n Known extensions are: " NPIC_KNOWN_IMAGE_EXT, h.realname); /* Free memory */ NpicFileFreeHints (&h); return res; } /* * Read image from file according to its extension and create appropriate image. * The filename can contain hints, see files_hints.c * * Return image on success, else return NULL. Verbose. */ Npic_image *NpicReadImage (const char *filename) { Npic_image *np = NULL; Npic_file_hints h; int k; /* After this call, memory must be freed with NpicFileFreeHints() */ k = NpicFileParseHints (filename, &h); if (k != NPIC_SUCCESS) NpicError (__func__, k, ": file \"%s\".\n", filename); if (strcmp (h.extension, ".npz") == 0 && h.comp == NPIC_COMPRESS_NONE) np = NpicReadNPZ (h.realname); else if (strcmp (h.extension, ".vol") == 0) np = NpicReadVOL (h.realname, h.comp); else if (strcmp (h.extension, ".pan") == 0) np = NpicReadPAN (h.realname, h.comp); else if (strcmp (h.extension, ".pnm") == 0 || strcmp (h.extension, ".pbm") == 0 || strcmp (h.extension, ".pgm") == 0 || strcmp (h.extension, ".ppm") == 0) np = NpicReadPNMwithHints (&h); else NpicError (__func__, NPIC_ERR_BAD_EXT, ": file \"%s\".\n Known extensions are: " NPIC_KNOWN_IMAGE_EXT, h.realname); /* Free memory */ NpicFileFreeHints (&h); return np; } /* * Print image informations according to its extension. * The filename can contain hints, see files_hints.c * * Return NPIC_SUCCESS, else error code. Verbose. */ int NpicInfoImage (const char *filename) { Npic_file_hints h; int k, res; /* After this call, memory must be freed with NpicFileFreeHints() */ k = NpicFileParseHints (filename, &h); if (k != NPIC_SUCCESS) NpicError (__func__, k, ": file \"%s\".\n", filename); if (strcmp (h.extension, ".npz") == 0 && h.comp == NPIC_COMPRESS_NONE) res = NpicInfoNPZ (h.realname); else if (strcmp (h.extension, ".vol") == 0) res = NpicInfoVOL (h.realname, h.comp); else if (strcmp (h.extension, ".pan") == 0) res = NpicInfoPAN (h.realname, h.comp); else if (strcmp (h.extension, ".pnm") == 0 || strcmp (h.extension, ".pbm") == 0 || strcmp (h.extension, ".pgm") == 0 || strcmp (h.extension, ".ppm") == 0) res = NpicInfoPNM (h.realname, h.comp); else res = NPIC_ERR_BAD_EXT; /* Free memory */ NpicFileFreeHints (&h); return res; } /*-------------------- P R I V A T E - F U N C T I O N S ---------------------*/