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