big warning hunt commit
[blender.git] / source / blender / imbuf / intern / util.c
1 /**
2  *
3  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL/BL DUAL LICENSE BLOCK *****
30  * util.c
31  *
32  * $Id$
33  */
34
35 #include "BLI_blenlib.h"
36
37 #include "DNA_userdef_types.h"
38 #include "BKE_global.h"
39
40 #include "imbuf.h"
41 #include "imbuf_patch.h"
42 #include "IMB_imbuf_types.h"
43 #include "IMB_imbuf.h"
44
45 #include "IMB_targa.h"
46 #include "IMB_png.h"
47 #include "IMB_bmp.h"
48
49 #include "IMB_anim.h"
50
51 #ifdef WITH_QUICKTIME
52 #include "quicktime_import.h"
53 #endif
54 #ifdef WITH_FREEIMAGE
55 #include "IMB_freeimage.h"
56 #endif
57 #ifdef WITH_IMAGEMAGICK
58 #include "IMB_imagemagick.h"
59 #endif
60
61
62 #define UTIL_DEBUG 0
63
64 /* from misc_util: flip the bytes from x  */
65 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
66
67 /* this one is only def-ed once, strangely... */
68 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
69
70 static int IMB_ispic_name(char *name)
71 {
72         struct stat st;
73         int fp, buf[10];
74         int ofs = 0;
75
76         if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
77         
78         if (ib_stat(name,&st) == -1) return(0);
79         if (((st.st_mode) & S_IFMT) == S_IFREG){
80                 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
81                         if (read(fp,buf,32)==32){
82                                 close(fp);
83                                 if (buf[ofs] == CAT) ofs += 3;
84                                 if (buf[ofs] == FORM){
85                                         if (buf[ofs + 2] == ILBM) return(AMI);
86                                         if (buf[ofs + 2] == ANIM){
87                                                 if (buf[ofs + 3] == FORM){
88                                                         return(ANIM);
89                                                 }else{
90                                                         return(Anim);
91                                                 }
92                                         }
93                                 } else {
94                                         if (GS(buf) == IMAGIC) return(IMAGIC);
95                                         if (GSS(buf) == IMAGIC) return(IMAGIC);
96                                         if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
97
98                                         /* at windows there are ".ffl" files with the same magic numnber... 
99                                            besides that,  tim images are not really important anymore! */
100                                         /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
101
102                                 }
103                                 if (imb_is_a_png(buf)) return(PNG);
104                                 if (imb_is_a_targa(buf)) return(TGA);
105 /*
106                                 if (imb_is_a_bmp(buf)) return(BMP);
107 */
108
109 #ifdef WITH_QUICKTIME
110 #if defined(_WIN32) || defined(__APPLE__)
111                                 if(G.have_quicktime) {
112                                         if (imb_is_a_quicktime(name)) return(QUICKTIME);
113                                 }
114 #endif
115 #endif
116
117 #ifdef WITH_FREEIMAGE
118                                 if (imb_is_a_freeimage(name)) return(FREEIMAGE);
119 #endif
120
121 #ifdef WITH_IMAGEMAGICK
122                                 if (imb_is_imagick(name)) return(IMAGEMAGICK);
123 #endif
124
125                                 return(FALSE);
126                         }
127                         close(fp);
128                 }
129         }
130         return(FALSE);
131 }
132
133
134
135 int IMB_ispic(char *filename)
136 {
137         if(U.uiflag & USER_FILTERFILEEXTS) {
138                 if (G.have_quicktime){
139                         if(             BLI_testextensie(filename, ".jpg")
140                                 ||      BLI_testextensie(filename, ".jpeg")
141                                 ||      BLI_testextensie(filename, ".tga")
142                                 ||      BLI_testextensie(filename, ".rgb")
143                                 ||      BLI_testextensie(filename, ".bmp")
144                                 ||      BLI_testextensie(filename, ".png")
145                                 ||      BLI_testextensie(filename, ".iff")
146                                 ||      BLI_testextensie(filename, ".lbm")
147                                 ||      BLI_testextensie(filename, ".gif")
148                                 ||      BLI_testextensie(filename, ".psd")
149                                 ||      BLI_testextensie(filename, ".tif")
150                                 ||      BLI_testextensie(filename, ".tiff")
151                                 ||      BLI_testextensie(filename, ".pct")
152                                 ||      BLI_testextensie(filename, ".pict")
153                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
154                                 ||      BLI_testextensie(filename, ".qtif")
155 #if defined(WITH_FREEIMAGE) || defined (WITH_IMAGEMAGICK) //nasty for now
156                                 ||      BLI_testextensie(filename, ".jng")
157                                 ||      BLI_testextensie(filename, ".mng")
158                                 ||      BLI_testextensie(filename, ".pbm")
159                                 ||      BLI_testextensie(filename, ".pgm")
160                                 ||      BLI_testextensie(filename, ".ppm")
161                                 ||      BLI_testextensie(filename, ".wbmp")
162                                 ||      BLI_testextensie(filename, ".cut")
163                                 ||      BLI_testextensie(filename, ".ico")
164                                 ||      BLI_testextensie(filename, ".koa")
165                                 ||      BLI_testextensie(filename, ".koala")
166                                 ||      BLI_testextensie(filename, ".pcd")
167                                 ||      BLI_testextensie(filename, ".pcx")
168                                 ||      BLI_testextensie(filename, ".ras")
169 #endif
170                                 ||      BLI_testextensie(filename, ".sgi")) {
171                                 return IMB_ispic_name(filename);
172                         } else {
173                                 return(FALSE);                  
174                         }
175                 } else { // no quicktime
176                         if(             BLI_testextensie(filename, ".jpg")
177                                 ||      BLI_testextensie(filename, ".jpeg")
178                                 ||      BLI_testextensie(filename, ".tga")
179                                 ||      BLI_testextensie(filename, ".rgb")
180                                 ||      BLI_testextensie(filename, ".bmp")
181                                 ||      BLI_testextensie(filename, ".png")
182                                 ||      BLI_testextensie(filename, ".iff")
183                                 ||      BLI_testextensie(filename, ".lbm")
184 #if defined(WITH_FREEIMAGE) || defined (WITH_IMAGEMAGICK) //nasty for now
185                                 ||      BLI_testextensie(filename, ".jng")
186                                 ||      BLI_testextensie(filename, ".mng")
187                                 ||      BLI_testextensie(filename, ".pbm")
188                                 ||      BLI_testextensie(filename, ".pgm")
189                                 ||      BLI_testextensie(filename, ".ppm")
190                                 ||      BLI_testextensie(filename, ".wbmp")
191                                 ||      BLI_testextensie(filename, ".cut")
192                                 ||      BLI_testextensie(filename, ".ico")
193                                 ||      BLI_testextensie(filename, ".koa")
194                                 ||      BLI_testextensie(filename, ".koala")
195                                 ||      BLI_testextensie(filename, ".pcd")
196                                 ||      BLI_testextensie(filename, ".pcx")
197                                 ||      BLI_testextensie(filename, ".ras")
198                                 ||      BLI_testextensie(filename, ".gif")
199                                 ||      BLI_testextensie(filename, ".psd")
200                                 ||      BLI_testextensie(filename, ".tif")
201                                 ||      BLI_testextensie(filename, ".tiff")
202 #endif
203                         ||      BLI_testextensie(filename, ".sgi")) {
204                                 return IMB_ispic_name(filename);
205                         }
206                         else  {
207                                 return(FALSE);
208                         }
209                 }
210         } else { // no FILTERFILEEXTS
211                 return IMB_ispic_name(filename);
212         }
213 }
214
215
216
217 static int isavi (char *name) {
218         return AVI_is_avi (name);
219 }
220
221 #ifdef WITH_QUICKTIME
222 static int isqtime (char *name) {
223         return anim_is_quicktime (name);
224 }
225 #endif
226
227 int imb_get_anim_type(char * name) {
228         int type;
229         struct stat st;
230
231         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
232
233         if (ib_stat(name,&st) == -1) return(0);
234         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
235         
236         if (isavi(name)) return (ANIM_AVI);
237
238         if (ismovie(name)) return (ANIM_MOVIE);
239 #ifdef WITH_QUICKTIME
240         if (isqtime(name)) return (ANIM_QTIME);
241 #endif
242         type = IMB_ispic(name);
243         if (type == ANIM) return (ANIM_ANIM5);
244         if (type) return(ANIM_SEQUENCE);
245         return(0);
246 }
247  
248 int IMB_isanim(char *filename) {
249         int type;
250         
251         if(U.uiflag & USER_FILTERFILEEXTS) {
252                 if (G.have_quicktime){
253                         if(             BLI_testextensie(filename, ".avi")
254                                 ||      BLI_testextensie(filename, ".flc")
255                                 ||      BLI_testextensie(filename, ".mov")
256                                 ||      BLI_testextensie(filename, ".movie")
257                                 ||      BLI_testextensie(filename, ".mv")) {
258                                 type = imb_get_anim_type(filename);
259                         } else {
260                                 return(FALSE);                  
261                         }
262                 } else { // no quicktime
263                         if(             BLI_testextensie(filename, ".avi")
264                                 ||      BLI_testextensie(filename, ".mv")) {
265                                 type = imb_get_anim_type(filename);
266                         }
267                         else  {
268                                 return(FALSE);
269                         }
270                 }
271         } else { // no FILTERFILEEXTS
272                 type = imb_get_anim_type(filename);
273         }
274         
275         return (type && type!=ANIM_SEQUENCE);
276 }