source: abuse/trunk/src/imlib/loader.cpp @ 2

Last change on this file since 2 was 2, checked in by Sam Hocevar, 14 years ago
  • imported original 0.7.0 tarball
File size: 3.9 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#include "targa.hpp"
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, palette *ref_pal)
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    case LOADER_targa :
79    {
80      if (ref_pal)
81      {
82        images=(image **)jmalloc(sizeof(image *),"loader::targa image * array");
83        total_read=1;
84        images[0]=load_targa(filename,ref_pal);
85        pal=ref_pal->copy();
86      }
87
88    } break;
89    default :
90      set_error(imNOT_SUPPORTED);
91  }   
92  if (current_error())
93  {
94    short i;   
95    i=current_error();
96    set_error(0);
97    return i;
98  }
99  return 0;
100}
101
102
103graphics_type tell_file_type(char *filename)
104{
105  FILE *fp;
106  unsigned char header[10];
107  fp=fopen(filename,"rb");
108  if (!fp)
109    return LOADER_not_supported;
110  else if (fread(header,1,12,fp)!=12)
111  {
112    fclose(fp);
113    return LOADER_not_supported;   
114  }
115  fclose(fp);
116
117
118  if (header[0]=='J' && header[1]=='C'
119      && header[2]=='2' && header[3]=='0')
120    return LOADER_mdl;
121  else if (header[4]==0 && header[5]==0 && header[6]==0 &&
122           header[7]==7)
123    return LOADER_xwd;
124  else if (header[0]=='P' && header[1]=='6')
125    return LOADER_ppm;
126  else if (header[10]==8 && header[11]==0xff)
127    return LOADER_pic;
128  else if (header[0]=='F' && header[1]=='O' && header[2]=='R' && header[3]=='M')
129    return LOADER_lbm;
130  else if (header[0]=='B' && header[1]=='M')
131  {
132    switch (tell_color_size(filename))
133    {
134      case 24 : return LOADER_bmp24; break;     
135      case 8 : return LOADER_bmp8; break;       
136      default : return LOADER_not_supported; break;
137    }
138  } else if (header[0]==10)
139  { 
140    switch (tell_color_size(filename))
141    {
142      case 24 : return LOADER_pcx24; break;     
143      case 8 : return LOADER_pcx8; break;       
144      default : return LOADER_not_supported; break;
145    }
146  } else if (header[0]==0 && header[1]==0 && header[2]==10 && header[3]==0 && header[4]==0)
147  {
148    return LOADER_targa;
149  }
150
151  return LOADER_not_supported;
152}
153
154
155
156
157
158
159
160
161
162
163
164
Note: See TracBrowser for help on using the repository browser.