source: abuse/tags/pd/imlib/targa.c @ 161

Last change on this file since 161 was 49, checked in by Sam Hocevar, 15 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 2.0 KB
Line 
1#include "image.hpp"
2#include "specs.hpp"
3
4image *load_targa(char *filename, palette *pal)
5{
6  bFILE *fp=open_file(filename,"rb");
7  if (!fp)
8    return 0;
9
10  if (fp->open_failure())
11  {
12    delete fp;
13    return 0;
14  }
15 
16  unsigned char id,color_map,im_type;
17
18  id=fp->read_byte();
19  color_map=fp->read_byte();
20  im_type=fp->read_byte();
21
22  if (color_map!=0)
23  {
24    delete fp;
25    return 0;
26  }
27
28  if (!(im_type==2 || im_type==10))
29  {
30    delete fp;
31    return 0;
32  }
33
34  fp->read_short();
35  fp->read_short();
36  fp->read_byte();
37
38  fp->read_short();
39  fp->read_short();
40
41
42  int w=fp->read_short();
43  int h=fp->read_short();
44  unsigned char bpp=fp->read_byte();
45  unsigned char im_des=fp->read_byte();
46
47  if (bpp!=32)
48  {
49    delete fp;
50    return 0;
51  }
52
53  image *im=new image(w,h);
54
55  int x,y;
56  unsigned char ctrl;
57  unsigned char bgra[4],*sl,c,lr,lg,lb,ll=0,lc;
58 
59 
60 
61
62  if (im_type==10)
63  {   
64    for (y=0;y<h;y++)
65    {
66      sl=im->scan_line(h-y-1);
67
68      for (x=0;x<w;)
69      {
70        ctrl=fp->read_byte();
71        if (ctrl&0x80)
72        {
73          fp->read(bgra,4);
74          ctrl&=(~0x80);
75          ctrl++;
76          if (bgra[3])
77          {
78            if (ll && lr==bgra[2] && lg==bgra[1] && lb==bgra[0])
79              c=lc;
80            else
81            {
82              c=pal->find_closest_non0(bgra[2],bgra[1],bgra[0]);
83              lr=bgra[2];
84              lg=bgra[1];
85              lb=bgra[0];
86              lc=c;
87              ll=1;
88            }
89
90          }
91          else c=0;
92
93          while (ctrl--)
94          {
95            *sl=c;
96            sl++;
97            x++;
98          }
99        } else
100        {
101          ctrl++;         
102          while (ctrl--)
103          {
104            fp->read(bgra,4);
105            if (bgra[3])
106            {
107              c=pal->find_closest_non0(bgra[2],bgra[1],bgra[0]);
108            }
109            else c=0;
110            *sl=c;
111            sl++;
112            x++;
113          }
114        }
115      }
116    }   
117  }
118
119
120
121  delete fp;
122  return im;
123}
Note: See TracBrowser for help on using the repository browser.