This is a modified version of patch #2995
[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 #include "IMB_tiff.h"
49 #include "IMB_radiance_hdr.h"
50
51 #include "IMB_anim.h"
52
53 #ifdef WITH_QUICKTIME
54 #include "quicktime_import.h"
55 #endif
56
57 #define UTIL_DEBUG 0
58
59 /* from misc_util: flip the bytes from x  */
60 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
61
62 /* this one is only def-ed once, strangely... */
63 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
64
65 static int IMB_ispic_name(char *name)
66 {
67         struct stat st;
68         int fp, buf[10];
69         int ofs = 0;
70
71         if(UTIL_DEBUG) printf("IMB_ispic_name: loading %s\n", name);
72         
73         if (ib_stat(name,&st) == -1) return(0);
74         if (((st.st_mode) & S_IFMT) == S_IFREG){
75                 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
76                         if (read(fp,buf,32)==32){
77                                 close(fp);
78                                 if (buf[ofs] == CAT) ofs += 3;
79                                 if (buf[ofs] == FORM){
80                                         if (buf[ofs + 2] == ILBM) return(AMI);
81                                         if (buf[ofs + 2] == ANIM){
82                                                 if (buf[ofs + 3] == FORM){
83                                                         return(ANIM);
84                                                 }else{
85                                                         return(Anim);
86                                                 }
87                                         }
88                                 } else {
89                                         if (GS(buf) == IMAGIC) return(IMAGIC);
90                                         if (GSS(buf) == IMAGIC) return(IMAGIC);
91                                         if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
92
93                                         /* at windows there are ".ffl" files with the same magic numnber... 
94                                            besides that,  tim images are not really important anymore! */
95                                         /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
96
97                                 }
98                                 if (imb_is_a_png(buf)) return(PNG);
99                                 if (imb_is_a_targa(buf)) return(TGA);
100
101                                 if (imb_is_a_tiff(buf)) return(TIF);
102
103                                 /* radhdr: check if hdr format */
104                                 if (imb_is_a_hdr(buf)) return(RADHDR);
105
106 /*
107                                 if (imb_is_a_bmp(buf)) return(BMP);
108 */
109
110 #ifdef WITH_QUICKTIME
111 #if defined(_WIN32) || defined(__APPLE__)
112                                 if(G.have_quicktime) {
113                                         if (imb_is_a_quicktime(name)) return(QUICKTIME);
114                                 }
115 #endif
116 #endif
117
118                                 return(FALSE);
119                         }
120                         close(fp);
121                 }
122         }
123         return(FALSE);
124 }
125
126
127
128 int IMB_ispic(char *filename)
129 {
130         if(U.uiflag & USER_FILTERFILEEXTS) {
131                 if (G.have_libtiff && (BLI_testextensie(filename, ".tif")
132                                 ||      BLI_testextensie(filename, ".tiff"))) {
133                                 return IMB_ispic_name(filename);
134                 }
135                 if (G.have_quicktime){
136                         if(             BLI_testextensie(filename, ".jpg")
137                                 ||      BLI_testextensie(filename, ".jpeg")
138                                 ||      BLI_testextensie(filename, ".tif")
139                                 ||      BLI_testextensie(filename, ".tiff")
140                                 ||      BLI_testextensie(filename, ".hdr")
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, ".pct")
150                                 ||      BLI_testextensie(filename, ".pict")
151                                 ||      BLI_testextensie(filename, ".pntg") //macpaint
152                                 ||      BLI_testextensie(filename, ".qtif")
153                                 ||      BLI_testextensie(filename, ".sgi")) {
154                                 return IMB_ispic_name(filename);
155                         } else {
156                                 return(FALSE);                  
157                         }
158                 } else { /* no quicktime or libtiff */
159                         if(             BLI_testextensie(filename, ".jpg")
160                                 ||      BLI_testextensie(filename, ".jpeg")
161                                 ||      BLI_testextensie(filename, ".hdr")
162                                 ||      BLI_testextensie(filename, ".tga")
163                                 ||      BLI_testextensie(filename, ".rgb")
164                                 ||      BLI_testextensie(filename, ".bmp")
165                                 ||      BLI_testextensie(filename, ".png")
166                                 ||      BLI_testextensie(filename, ".iff")
167                                 ||      BLI_testextensie(filename, ".lbm")
168                                 ||      BLI_testextensie(filename, ".sgi")) {
169                                 return IMB_ispic_name(filename);
170                         }
171                         else  {
172                                 return(FALSE);
173                         }
174                 }
175         } else { /* no FILTERFILEEXTS */
176                 return IMB_ispic_name(filename);
177         }
178 }
179
180
181
182 static int isavi (char *name) {
183         return AVI_is_avi (name);
184 }
185
186 #ifdef WITH_QUICKTIME
187 static int isqtime (char *name) {
188         return anim_is_quicktime (name);
189 }
190 #endif
191
192 int imb_get_anim_type(char * name) {
193         int type;
194         struct stat st;
195
196         if(UTIL_DEBUG) printf("in getanimtype: %s\n", name);
197
198         if (ib_stat(name,&st) == -1) return(0);
199         if (((st.st_mode) & S_IFMT) != S_IFREG) return(0);
200         
201         if (isavi(name)) return (ANIM_AVI);
202
203         if (ismovie(name)) return (ANIM_MOVIE);
204 #ifdef WITH_QUICKTIME
205         if (isqtime(name)) return (ANIM_QTIME);
206 #endif
207         type = IMB_ispic(name);
208         if (type == ANIM) return (ANIM_ANIM5);
209         if (type) return(ANIM_SEQUENCE);
210         return(0);
211 }
212  
213 int IMB_isanim(char *filename) {
214         int type;
215         
216         if(U.uiflag & USER_FILTERFILEEXTS) {
217                 if (G.have_quicktime){
218                         if(             BLI_testextensie(filename, ".avi")
219                                 ||      BLI_testextensie(filename, ".flc")
220                                 ||      BLI_testextensie(filename, ".mov")
221                                 ||      BLI_testextensie(filename, ".movie")
222                                 ||      BLI_testextensie(filename, ".mv")) {
223                                 type = imb_get_anim_type(filename);
224                         } else {
225                                 return(FALSE);                  
226                         }
227                 } else { // no quicktime
228                         if(             BLI_testextensie(filename, ".avi")
229                                 ||      BLI_testextensie(filename, ".mv")) {
230                                 type = imb_get_anim_type(filename);
231                         }
232                         else  {
233                                 return(FALSE);
234                         }
235                 }
236         } else { // no FILTERFILEEXTS
237                 type = imb_get_anim_type(filename);
238         }
239         
240         return (type && type!=ANIM_SEQUENCE);
241 }