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