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

Last change on this file since 90 was 56, checked in by Sam Hocevar, 12 years ago
  • Add licensing terms to most C / C++ files (Ref #5).
File size: 4.2 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include "loader.hpp"
13#include "image.hpp"
14#include "palette.hpp"
15#include "mdlread.hpp"
16#include "ppmread.hpp"
17#include "xwdread.hpp"
18#include "glread.hpp"
19#include "pcxread.hpp"
20#include "lbmread.hpp"
21#include "targa.hpp"
22
23int tell_color_size(char *filename)
24{
25  if (bmp_bits(filename)==24) return 24; 
26  if (PCX_file_type(filename)==PCX_24) return 24;
27  clear_errors(); 
28  return 8; 
29}
30
31image24 *load_any24(char *filename)
32{
33  if (bmp_bits(filename)==24)
34    return read_bmp24(filename);
35  else if (PCX_file_type(filename)==PCX_24)
36    return read_PCX24(filename);
37  else return NULL;
38 
39}
40
41
42// if total_read returned<0 then, this function returns a error message number
43short load_any(char *filename, image **&images, palette *&pal, short &total_read, palette *ref_pal)
44{
45  pal=NULL; images=NULL; total_read=0;
46  switch (tell_file_type(filename))
47  {
48    case LOADER_not_supported : break;
49    case LOADER_mdl : 
50      total_read=mdl_total_images(filename);
51      if (!current_error())
52        images=read_mdl(filename,pal,1,total_read,total_read);
53      break;
54    case LOADER_xwd :
55      images=(image **)jmalloc(sizeof(image *),"loader::xwd image * array");
56      total_read=1;
57      images[0]=readxwd(filename,pal);
58      break;
59    case LOADER_ppm : 
60      images=(image **)jmalloc(sizeof(image *),"loader::ppm image * array");
61      total_read=1;
62      pal=new palette;
63      images[0]=read_ppm(filename,pal,PPM_REG);
64      break;
65    case LOADER_pic : 
66      images=(image **)jmalloc(sizeof(image *),"loader::pic image * array");
67      total_read=1;
68      pal=NULL;
69      images[0]=read_pic(filename,pal);
70      break;
71    case LOADER_bmp8 : 
72      images=(image **)jmalloc(sizeof(image *),"loader::bmp image * array");
73      total_read=1;
74      pal=NULL;
75      images[0]=read_bmp(pal,filename);
76      break;
77    case LOADER_pcx8 : 
78      images=(image **)jmalloc(sizeof(image *),"loader::pcx image * array");
79      total_read=1;
80      pal=NULL;
81      images[0]=read_PCX(filename,pal);   
82      break;   
83    case LOADER_lbm :
84      images=(image **)jmalloc(sizeof(image *),"loader::pcx image * array");
85      total_read=1;
86      pal=NULL;
87      images[0]=read_lbm(filename,pal);   
88      break;
89    case LOADER_targa :
90    {
91      if (ref_pal)
92      {
93        images=(image **)jmalloc(sizeof(image *),"loader::targa image * array");
94        total_read=1;
95        images[0]=load_targa(filename,ref_pal);
96        pal=ref_pal->copy();
97      }
98
99    } break;
100    default :
101      set_error(imNOT_SUPPORTED);
102  }   
103  if (current_error())
104  {
105    short i;   
106    i=current_error();
107    set_error(0);
108    return i;
109  }
110  return 0;
111}
112
113
114graphics_type tell_file_type(char *filename)
115{
116  FILE *fp;
117  unsigned char header[10];
118  fp=fopen(filename,"rb");
119  if (!fp)
120    return LOADER_not_supported;
121  else if (fread(header,1,12,fp)!=12)
122  {
123    fclose(fp);
124    return LOADER_not_supported;   
125  }
126  fclose(fp);
127
128
129  if (header[0]=='J' && header[1]=='C'
130      && header[2]=='2' && header[3]=='0')
131    return LOADER_mdl;
132  else if (header[4]==0 && header[5]==0 && header[6]==0 &&
133           header[7]==7)
134    return LOADER_xwd;
135  else if (header[0]=='P' && header[1]=='6')
136    return LOADER_ppm;
137  else if (header[10]==8 && header[11]==0xff)
138    return LOADER_pic;
139  else if (header[0]=='F' && header[1]=='O' && header[2]=='R' && header[3]=='M')
140    return LOADER_lbm;
141  else if (header[0]=='B' && header[1]=='M')
142  {
143    switch (tell_color_size(filename))
144    {
145      case 24 : return LOADER_bmp24; break;     
146      case 8 : return LOADER_bmp8; break;       
147      default : return LOADER_not_supported; break;
148    }
149  } else if (header[0]==10)
150  { 
151    switch (tell_color_size(filename))
152    {
153      case 24 : return LOADER_pcx24; break;     
154      case 8 : return LOADER_pcx8; break;       
155      default : return LOADER_not_supported; break;
156    }
157  } else if (header[0]==0 && header[1]==0 && header[2]==10 && header[3]==0 && header[4]==0)
158  {
159    return LOADER_targa;
160  }
161
162  return LOADER_not_supported;
163}
164
165
166
167
168
169
170
171
172
173
174
175
Note: See TracBrowser for help on using the repository browser.