3dphotoshoot
annotate libs/imago/ftype_module.c @ 14:06dc8b9b4f89
added libimago, libjpeg and libpng
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 07 Jun 2015 17:25:49 +0300 |
parents | |
children |
rev | line source |
---|---|
nuclear@14 | 1 /* |
nuclear@14 | 2 libimago - a multi-format image file input/output library. |
nuclear@14 | 3 Copyright (C) 2010 John Tsiombikas <nuclear@member.fsf.org> |
nuclear@14 | 4 |
nuclear@14 | 5 This program is free software: you can redistribute it and/or modify |
nuclear@14 | 6 it under the terms of the GNU Lesser General Public License as published |
nuclear@14 | 7 by the Free Software Foundation, either version 3 of the License, or |
nuclear@14 | 8 (at your option) any later version. |
nuclear@14 | 9 |
nuclear@14 | 10 This program is distributed in the hope that it will be useful, |
nuclear@14 | 11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
nuclear@14 | 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
nuclear@14 | 13 GNU Lesser General Public License for more details. |
nuclear@14 | 14 |
nuclear@14 | 15 You should have received a copy of the GNU Lesser General Public License |
nuclear@14 | 16 along with this program. If not, see <http://www.gnu.org/licenses/>. |
nuclear@14 | 17 */ |
nuclear@14 | 18 |
nuclear@14 | 19 #include <stdlib.h> |
nuclear@14 | 20 #include <string.h> |
nuclear@14 | 21 #include "ftype_module.h" |
nuclear@14 | 22 |
nuclear@14 | 23 static struct list_node { |
nuclear@14 | 24 struct ftype_module *module; |
nuclear@14 | 25 struct list_node *next; |
nuclear@14 | 26 } *modules; |
nuclear@14 | 27 |
nuclear@14 | 28 /* defined in modules.c which is generated by configure */ |
nuclear@14 | 29 void img_modules_init(); |
nuclear@14 | 30 |
nuclear@14 | 31 static int done_init; |
nuclear@14 | 32 |
nuclear@14 | 33 int img_register_module(struct ftype_module *mod) |
nuclear@14 | 34 { |
nuclear@14 | 35 struct list_node *node; |
nuclear@14 | 36 |
nuclear@14 | 37 if(!(node = malloc(sizeof *node))) { |
nuclear@14 | 38 return -1; |
nuclear@14 | 39 } |
nuclear@14 | 40 |
nuclear@14 | 41 node->module = mod; |
nuclear@14 | 42 node->next = modules; |
nuclear@14 | 43 modules = node; |
nuclear@14 | 44 return 0; |
nuclear@14 | 45 } |
nuclear@14 | 46 |
nuclear@14 | 47 struct ftype_module *img_find_format_module(struct img_io *io) |
nuclear@14 | 48 { |
nuclear@14 | 49 struct list_node *node; |
nuclear@14 | 50 |
nuclear@14 | 51 if(!done_init) { |
nuclear@14 | 52 img_modules_init(); |
nuclear@14 | 53 done_init = 1; |
nuclear@14 | 54 } |
nuclear@14 | 55 |
nuclear@14 | 56 node = modules; |
nuclear@14 | 57 while(node) { |
nuclear@14 | 58 if(node->module->check(io) != -1) { |
nuclear@14 | 59 return node->module; |
nuclear@14 | 60 } |
nuclear@14 | 61 node = node->next; |
nuclear@14 | 62 } |
nuclear@14 | 63 return 0; |
nuclear@14 | 64 } |
nuclear@14 | 65 |
nuclear@14 | 66 struct ftype_module *img_guess_format(const char *fname) |
nuclear@14 | 67 { |
nuclear@14 | 68 struct list_node *node; |
nuclear@14 | 69 char *suffix; |
nuclear@14 | 70 int suffix_len; |
nuclear@14 | 71 |
nuclear@14 | 72 if(!done_init) { |
nuclear@14 | 73 img_modules_init(); |
nuclear@14 | 74 done_init = 1; |
nuclear@14 | 75 } |
nuclear@14 | 76 |
nuclear@14 | 77 if(!(suffix = strrchr(fname, '.'))) { |
nuclear@14 | 78 return 0; /* no suffix, can't guess ... */ |
nuclear@14 | 79 } |
nuclear@14 | 80 suffix_len = (int)strlen(suffix); |
nuclear@14 | 81 |
nuclear@14 | 82 node = modules; |
nuclear@14 | 83 while(node) { |
nuclear@14 | 84 char *suflist = node->module->suffix; |
nuclear@14 | 85 char *start, *end; |
nuclear@14 | 86 |
nuclear@14 | 87 while(*suflist) { |
nuclear@14 | 88 if(!(start = strstr(suflist, suffix))) { |
nuclear@14 | 89 break; |
nuclear@14 | 90 } |
nuclear@14 | 91 end = start + suffix_len; |
nuclear@14 | 92 |
nuclear@14 | 93 if(*end == ':' || *end == 0) { |
nuclear@14 | 94 return node->module; /* found it */ |
nuclear@14 | 95 } |
nuclear@14 | 96 suflist = end; |
nuclear@14 | 97 } |
nuclear@14 | 98 |
nuclear@14 | 99 node = node->next; |
nuclear@14 | 100 } |
nuclear@14 | 101 return 0; |
nuclear@14 | 102 } |
nuclear@14 | 103 |
nuclear@14 | 104 struct ftype_module *img_get_module(int idx) |
nuclear@14 | 105 { |
nuclear@14 | 106 struct list_node *node; |
nuclear@14 | 107 |
nuclear@14 | 108 if(!done_init) { |
nuclear@14 | 109 img_modules_init(); |
nuclear@14 | 110 done_init = 1; |
nuclear@14 | 111 } |
nuclear@14 | 112 |
nuclear@14 | 113 node = modules; |
nuclear@14 | 114 while(node && idx--) { |
nuclear@14 | 115 node = node->next; |
nuclear@14 | 116 } |
nuclear@14 | 117 return node->module; |
nuclear@14 | 118 } |