"Fossies" - the Fresh Open Source Software Archive

Member "libgd-2.3.2/src/gd_filename.c" (4 Mar 2021, 6801 Bytes) of package /linux/www/libgd-2.3.2.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "gd_filename.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.3.1_vs_2.3.2.

    1 /* Convenience functions to read or write images from or to disk,
    2  * determining file type from the filename extension. */
    3 
    4 #ifdef HAVE_CONFIG_H
    5 #   include "config.h"
    6 #endif
    7 
    8 #include <stdio.h>
    9 #include <string.h>
   10 
   11 #include "gd.h"
   12 
   13 typedef gdImagePtr (BGD_STDCALL *ReadFn)(FILE *in);
   14 typedef void (BGD_STDCALL *WriteFn)(gdImagePtr im, FILE *out);
   15 typedef gdImagePtr (BGD_STDCALL *LoadFn)(char *filename);
   16 
   17 #ifdef HAVE_LIBZ
   18 static void BGD_STDCALL writegd2(gdImagePtr im, FILE *out) {
   19     gdImageGd2(im, out, 0, GD2_FMT_COMPRESSED);
   20 }/* writegd*/
   21 #endif
   22 
   23 #ifdef HAVE_LIBJPEG
   24 static void BGD_STDCALL writejpeg(gdImagePtr im, FILE *out) {
   25     gdImageJpeg(im, out, -1);
   26 }/* writejpeg*/
   27 #endif
   28 
   29 static void BGD_STDCALL writewbmp(gdImagePtr im, FILE *out) {
   30     int fg = gdImageColorClosest(im, 0, 0, 0);
   31 
   32     gdImageWBMP(im, fg, out);
   33 }/* writejpeg*/
   34 
   35 static void BGD_STDCALL writebmp(gdImagePtr im, FILE *out) {
   36     gdImageBmp(im, out, GD_TRUE);
   37 }/* writejpeg*/
   38 
   39 
   40 static const struct FileType {
   41     const char *ext;
   42     ReadFn reader;
   43     WriteFn writer;
   44     LoadFn loader;
   45 } Types[] = {
   46     {".gif",    gdImageCreateFromGif,   gdImageGif,     NULL},
   47     {".gd",     gdImageCreateFromGd,    gdImageGd,      NULL},
   48     {".wbmp",   gdImageCreateFromWBMP,  writewbmp,      NULL},
   49     {".bmp",    gdImageCreateFromBmp,   writebmp,       NULL},
   50 
   51     {".xbm",    gdImageCreateFromXbm,   NULL,           NULL},
   52     {".tga",    gdImageCreateFromTga,   NULL,           NULL},
   53 
   54 #ifdef HAVE_LIBAVIF
   55     {".avif",   gdImageCreateFromAvif,  gdImageAvif,    NULL},
   56 #endif
   57 
   58 #ifdef HAVE_LIBPNG
   59     {".png",    gdImageCreateFromPng,   gdImagePng,     NULL},
   60 #endif
   61 
   62 #ifdef HAVE_LIBJPEG
   63     {".jpg",    gdImageCreateFromJpeg,  writejpeg,      NULL},
   64     {".jpeg",   gdImageCreateFromJpeg,  writejpeg,      NULL},
   65 #endif
   66 
   67 #ifdef HAVE_LIBHEIF
   68     {".heic",   gdImageCreateFromHeif,  gdImageHeif,    NULL},
   69     {".heix",   gdImageCreateFromHeif,  NULL,           NULL},
   70 #endif
   71 
   72 #ifdef HAVE_LIBTIFF
   73     {".tiff",   gdImageCreateFromTiff,  gdImageTiff,    NULL},
   74     {".tif" ,   gdImageCreateFromTiff,  gdImageTiff,    NULL},
   75 #endif
   76 
   77 #ifdef HAVE_LIBZ
   78     {".gd2",    gdImageCreateFromGd2,   writegd2,       NULL},
   79 #endif
   80 
   81 #ifdef HAVE_LIBWEBP
   82     {".webp",   gdImageCreateFromWebp,  gdImageWebp,    NULL},
   83 #endif
   84 
   85 #ifdef HAVE_LIBXPM
   86     {".xpm",    NULL,                   NULL,           gdImageCreateFromXpm},
   87 #endif
   88 
   89     {NULL, NULL, NULL}
   90 };
   91 
   92 
   93 static const struct FileType *
   94 ftype(const char *filename) {
   95     int n;
   96     char *ext;
   97 
   98     /* Find the file extension (i.e. the last period in the string. */
   99     ext = strrchr(filename, '.');
  100     if (!ext) return NULL;
  101 
  102     for (n = 0; Types[n].ext; n++) {
  103         if (strcasecmp(ext, Types[n].ext) == 0) {
  104             return &Types[n];
  105         }/* if */
  106     }/* for */
  107 
  108     return NULL;
  109 }/* ftype*/
  110 
  111 
  112 /*
  113   Function: gdSupportsFileType
  114 
  115     Tests if a given file type is supported by GD.
  116 
  117     Given the name of an image file (which does not have to exist),
  118     returns 1 (i.e. TRUE) if <gdImageCreateFromFile> can read a file
  119     of that type.  This is useful if you do not know which image types
  120     were enabled at compile time.
  121 
  122     If _writing_ is true, the result will be true only if
  123     <gdImageFile> can write a file of this type.
  124 
  125     Note that filename parsing is done exactly the same as is done by
  126     <gdImageCreateFromFile> and <gdImageFile> and is subject to the
  127     same limitations.
  128 
  129     Assuming LibGD is compiled with support for these image types, the
  130     following extensions are supported:
  131 
  132         - .gif
  133         - .gd, .gd2
  134         - .wbmp
  135         - .bmp
  136         - .xbm
  137         - .tga
  138         - .png
  139         - .jpg, .jpeg
  140         - .heif, .heix
  141         - .avif
  142         - .tiff, .tif
  143         - .webp
  144         - .xpm
  145 
  146     Names are parsed case-insenstively.
  147 
  148   Parameters:
  149 
  150     filename    - Filename with tested extension.
  151     writing     - Flag: true tests if writing works
  152 
  153   Returns:
  154 
  155     GD_TRUE (1) if the file type is supported, GD_FALSE (0) if not.
  156 
  157 */
  158 BGD_DECLARE(int)
  159 gdSupportsFileType(const char *filename, int writing) {
  160     const struct FileType *entry = ftype(filename);
  161     return !!entry && (!writing || !!entry->writer);
  162 }/* gdSupportsFileType*/
  163 
  164 
  165 /*
  166   Function: gdImageCreateFromFile
  167 
  168     Read an image file of any supported.
  169 
  170     Given the path to a file, <gdImageCreateFromFile> will open the
  171     file, read its contents with the appropriate _gdImageCreateFrom*_
  172     function and return it.
  173 
  174     File type is determined by the filename extension, so having an
  175     incorrect extension will probably not work.  For example, renaming
  176     PNG image "foo.png" to "foo.gif" and then attempting to load it
  177     will fail even if GD supports both formats.  See
  178     <gdSupportsFiletype> for more details.
  179 
  180     NULL is returned on error.
  181 
  182   Parameters:
  183 
  184     filename    - the input file name
  185 
  186   Returns:
  187 
  188     A pointer to the new image or NULL if an error occurred.
  189 
  190 */
  191 
  192 BGD_DECLARE(gdImagePtr)
  193 gdImageCreateFromFile(const char *filename) {
  194     const struct FileType *entry = ftype(filename);
  195     FILE *fh;
  196     gdImagePtr result;
  197 
  198     if (!entry) return NULL;
  199     if (entry->loader) return entry->loader((char *)filename);
  200     if (!entry->reader) return NULL;
  201 
  202     fh = fopen(filename, "rb");
  203     if (!fh) return NULL;
  204 
  205     result = entry->reader(fh);
  206 
  207     fclose(fh);
  208 
  209     return result;
  210 }/* gdImageCreateFromFile*/
  211 
  212 
  213 
  214 /*
  215   Function: gdImageFile
  216 
  217     Writes an image to a file in the format indicated by the filename.
  218 
  219     File type is determined by the extension of the file name.  See
  220     <gdSupportsFiletype> for an overview of the parsing.
  221 
  222     For file types that require extra arguments, <gdImageFile>
  223     attempts to use sane defaults:
  224 
  225     <gdImageGd2>    - chunk size = 0, compression is enabled.
  226     <gdImageJpeg>   - quality = -1 (i.e. the reasonable default)
  227     <gdImageWBMP>   - foreground is the darkest available color
  228 
  229     Everything else is called with the two-argument function and so
  230     will use the default values.
  231 
  232     <gdImageFile> has some rudimentary error detection and will return
  233     GD_FALSE (0) if a detectable error occurred.  However, the image
  234     loaders do not normally return their error status so a result of
  235     GD_TRUE (1) does **not** mean the file was saved successfully.
  236 
  237   Parameters:
  238 
  239     im          - The image to save.
  240     filename    - The path to the file to which the image is saved.
  241 
  242   Returns:
  243 
  244     GD_FALSE (0) if an error was detected, GD_TRUE (1) if not.
  245 
  246 */
  247 
  248 BGD_DECLARE(int)
  249 gdImageFile(gdImagePtr im, const char *filename) {
  250     const struct FileType *entry = ftype(filename);
  251     FILE *fh;
  252 
  253     if (!entry || !entry->writer) return GD_FALSE;
  254 
  255     fh = fopen(filename, "wb");
  256     if (!fh) return GD_FALSE;
  257 
  258     entry->writer(im, fh);
  259 
  260     fclose(fh);
  261 
  262     return GD_TRUE;
  263 }/* gdImageFile*/