1 /*  Part of XPCE --- The SWI-Prolog GUI toolkit
2 
3     Author:        Jan Wielemaker and Anjo Anjewierden
4     E-mail:        jan@swi.psy.uva.nl
5     WWW:           http://www.swi.psy.uva.nl/projects/xpce/
6     Copyright (c)  2003-2011, University of Amsterdam
7     All rights reserved.
8 
9     Redistribution and use in source and binary forms, with or without
10     modification, are permitted provided that the following conditions
11     are met:
12 
13     1. Redistributions of source code must retain the above copyright
14        notice, this list of conditions and the following disclaimer.
15 
16     2. Redistributions in binary form must reproduce the above copyright
17        notice, this list of conditions and the following disclaimer in
18        the documentation and/or other materials provided with the
19        distribution.
20 
21     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23     LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24     FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25     COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27     BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31     ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32     POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <h/kernel.h>
36 #include <h/graphics.h>
37 
38 /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39 Guess the image content type from the   first  <size> bytes of the image
40 data. The answer of this routine  often   isn't  authoritive, but can be
41 used to select the proper loading routine  without blind testing and the
42 risc crashing poorly designed loading  routines   on  what is actually a
43 valid image format.  Size is supposed to be at least 64
44 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
45 
46 static int string_prefix(const char *data, int len, int offset, const char *match);
47 
48 int
image_type_from_data(char * data,int size)49 image_type_from_data(char *data, int size)
50 { if ( size > 2 )
51   { unsigned short beshort = data[0]<<8|data[1];
52 
53     switch(beshort)
54     { case 0xffd8:
55 	return IMG_IS_JPEG;
56       default:
57 	break;
58     }
59   }
60 
61   if ( string_prefix(data, size, 0, "#define ") )
62     return IMG_IS_XBM;			/* X11 bitmap (.BM) file */
63   if ( string_prefix(data, size, 0, "/* Format_version=1, Width=") )
64     return IMG_IS_SUNICON;		/* Old SUN Icon files */
65   if ( string_prefix(data, size, 0, "/* XPM */") )
66     return IMG_IS_XPM;			/* XPM Image file */
67   if ( string_prefix(data, size, 0, "GIG8") )
68     return IMG_IS_GIF;			/* GIF Image files */
69   if ( data[0] == 'P' && data[1] >= '1' && data[1] <= '7' )
70     return IMG_IS_PNM;			/* BPMPLUS images */
71   if ( string_prefix(data, size, 0, "\x89PNG\x0d\x0a\x1a\x0a") )
72     return IMG_IS_PNG;			/* PNG's (Not GIF) images */
73   if ( string_prefix(data, size, 0, "BM") )
74     return IMG_IS_BMP;			/* Windows BMP files */
75   if ( string_prefix(data, size, 0, "IC") )
76     return IMG_IS_ICO;			/* Windows ICO files */
77   if ( string_prefix(data, size, 0, "CI") )
78     return IMG_IS_ICO;			/* Windows ICO (color) files */
79 
80   return IMG_IS_UNKNOWN;		/* Don't know */
81 }
82 
83 
84 static int
string_prefix(const char * data,int len,int offset,const char * match)85 string_prefix(const char *data, int len, int offset, const char *match)
86 { data += offset;
87   len  -= offset;
88 
89   while(*data == *match && len > 0 )
90   { data++;
91     match++;
92     len--;
93   }
94   if ( len >= 0 && *match == 0 )
95     return TRUE;
96 
97   return FALSE;
98 }
99