minor (2.26 release) cleanup
[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 "imbuf.h"
38 #include "imbuf_patch.h"
39 #include "IMB_imbuf_types.h"
40 #include "IMB_imbuf.h"
41
42 #include "IMB_targa.h"
43 #include "IMB_png.h"
44 #include "IMB_bmp.h"
45
46 #ifdef HAVE_CONFIG_H
47 #include <config.h>
48 #endif
49
50 /* from misc_util: flip the bytes from x  */
51 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
52
53 /* this one is only def-ed once, strangely... */
54 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
55
56 int IMB_ispic(char *name)
57 {
58         struct stat st;
59         int fp, buf[16];
60         int ofs = 0;
61         
62         if (ib_stat(name,&st) == -1) return(0);
63         if (((st.st_mode) & S_IFMT) == S_IFREG){
64                 if ((fp = open(name,O_BINARY|O_RDONLY)) >= 0){
65                         if (read(fp,buf,64)==64){
66                                 close(fp);
67                                 if (buf[ofs] == CAT) ofs += 3;
68                                 if (buf[ofs] == FORM){
69                                         if (buf[ofs + 2] == ILBM) return(AMI);
70                                         if (buf[ofs + 2] == ANIM){
71                                                 if (buf[ofs + 3] == FORM){
72                                                         return(ANIM);
73                                                 }else{
74                                                         return(Anim);
75                                                 }
76                                         }
77                                 } else {
78                                         if (GS(buf) == IMAGIC) return(IMAGIC);
79                                         if (GSS(buf) == IMAGIC) return(IMAGIC);
80                                         if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0) return(JPG);
81
82                                         /* at windows there are ".ffl" files with the same magic numnber... 
83                                            besides that,  tim images are not really important anymore! */
84                                         /* if ((BIG_LONG(buf[0]) == 0x10000000) && ((BIG_LONG(buf[1]) & 0xf0ffffff) == 0)) return(TIM); */
85
86                                 }
87                                 if (imb_is_a_png(buf))          return(PNG);
88                                 if (imb_is_a_targa(buf))        return(TGA);
89                                 if (imb_is_a_bmp(buf))          return(BMP);
90                                 return(FALSE);
91                         }
92                         close(fp);
93                 }
94         }
95         return(FALSE);
96 }