source: abuse/tags/pd/macabuse/imlib/loader.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1#include "loader.hpp"
2#include "image.hpp"
3#include "palette.hpp"
4#include "mdlread.hpp"
5#include "ppmread.hpp"
6#include "xwdread.hpp"
7#include "glread.hpp"
8#include "pcxread.hpp"
9#include "lbmread.hpp"
10
11
12int tell_color_size(char *filename)
13{
14  if (bmp_bits(filename)==24) return 24; 
15  if (PCX_file_type(filename)==PCX_24) return 24;
16  clear_errors(); 
17  return 8; 
18}
19
20image24 *load_any24(char *filename)
21{
22  if (bmp_bits(filename)==24)
23    return read_bmp24(filename);
24  else if (PCX_file_type(filename)==PCX_24)
25    return read_PCX24(filename);
26  else return NULL;
27 
28}
29
30
31// if total_read returned<0 then, this function returns a error message number
32short load_any(char *filename, image **&images, palette *&pal, short &total_read)
33{
34  pal=NULL; images=NULL; total_read=0;
35  switch (tell_file_type(filename))
36  {
37    case LOADER_not_supported : break;
38    case LOADER_mdl : 
39      total_read=mdl_total_images(filename);
40      if (!current_error())
41        images=read_mdl(filename,pal,1,total_read,total_read);
42      break;
43    case LOADER_xwd :
44      images=(image **)jmalloc(sizeof(image *),"loader::xwd image * array");
45      total_read=1;
46      images[0]=readxwd(filename,pal);
47      break;
48    case LOADER_ppm : 
49      images=(image **)jmalloc(sizeof(image *),"loader::ppm image * array");
50      total_read=1;
51      pal=new palette;
52      images[0]=read_ppm(filename,pal,PPM_REG);
53      break;
54    case LOADER_pic : 
55      images=(image **)jmalloc(sizeof(image *),"loader::pic image * array");
56      total_read=1;
57      pal=NULL;
58      images[0]=read_pic(filename,pal);
59      break;
60    case LOADER_bmp8 : 
61      images=(image **)jmalloc(sizeof(image *),"loader::bmp image * array");
62      total_read=1;
63      pal=NULL;
64      images[0]=read_bmp(pal,filename);
65      break;
66    case LOADER_pcx8 : 
67      images=(image **)jmalloc(sizeof(image *),"loader::pcx image * array");
68      total_read=1;
69      pal=NULL;
70      images[0]=read_PCX(filename,pal);   
71      break;   
72    case LOADER_lbm :
73      images=(image **)jmalloc(sizeof(image *),"loader::pcx image * array");
74      total_read=1;
75      pal=NULL;
76      images[0]=read_lbm(filename,pal);   
77      break;
78    default :
79      set_error(imNOT_SUPPORTED);
80  }   
81  if (current_error())
82  {
83    short i;   
84    i=current_error();
85    set_error(0);
86    return i;
87  }
88  return 0;
89}
90
91
92graphics_type tell_file_type(char *filename)
93{
94  FILE *fp;
95  unsigned char header[10];
96  fp=fopen(filename,"rb");
97  if (!fp)
98    return LOADER_not_supported;
99  else if (fread(header,1,12,fp)!=12)
100  {
101    fclose(fp);
102    return LOADER_not_supported;   
103  }
104  fclose(fp);
105
106
107  if (header[0]=='J' && header[1]=='C'
108      && header[2]=='2' && header[3]=='0')
109    return LOADER_mdl;
110  else if (header[4]==0 && header[5]==0 && header[6]==0 &&
111           header[7]==7)
112    return LOADER_xwd;
113  else if (header[0]=='P' && header[1]=='6')
114    return LOADER_ppm;
115  else if (header[10]==8 && header[11]==0xff)
116    return LOADER_pic;
117  else if (header[0]=='F' && header[1]=='O' && header[2]=='R' && header[3]=='M')
118    return LOADER_lbm;
119  else if (header[0]=='B' && header[1]=='M')
120  {
121    switch (tell_color_size(filename))
122    {
123      case 24 : return LOADER_bmp24; break;     
124      case 8 : return LOADER_bmp8; break;       
125      default : return LOADER_not_supported; break;
126    }
127  } else if (header[0]==10)
128  { 
129    switch (tell_color_size(filename))
130    {
131      case 24 : return LOADER_pcx24; break;     
132      case 8 : return LOADER_pcx8; break;       
133      default : return LOADER_not_supported; break;
134    }
135  }
136  return LOADER_not_supported;
137}
138
139
140
141
142
143
144
145
146
147
148
149
Note: See TracBrowser for help on using the repository browser.