File: targa.c

package info (click to toggle)
abuse 2.00-12
  • links: PTS
  • area: main
  • in suites: slink
  • size: 12,708 kB
  • ctags: 15,389
  • sloc: ansic: 115,852; cpp: 6,792; lisp: 2,066; sh: 1,734; makefile: 1,601; asm: 264
file content (123 lines) | stat: -rw-r--r-- 2,002 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include "image.hpp"
#include "specs.hpp"

image *load_targa(char *filename, palette *pal)
{
  bFILE *fp=open_file(filename,"rb");
  if (!fp)
    return 0;

  if (fp->open_failure())
  {
    delete fp;
    return 0;
  }
  
  unsigned char id,color_map,im_type;

  id=fp->read_byte();
  color_map=fp->read_byte();
  im_type=fp->read_byte();

  if (color_map!=0)
  {
    delete fp;
    return 0;
  }

  if (!(im_type==2 || im_type==10))
  {
    delete fp;
    return 0;
  }

  fp->read_short();
  fp->read_short();
  fp->read_byte();

  fp->read_short();
  fp->read_short();


  int w=fp->read_short();
  int h=fp->read_short();
  unsigned char bpp=fp->read_byte();
  unsigned char im_des=fp->read_byte();

  if (bpp!=32)
  {
    delete fp;
    return 0;
  }

  image *im=new image(w,h);

  int x,y;
  unsigned char ctrl;
  unsigned char bgra[4],*sl,c,lr,lg,lb,ll=0,lc;
  
  
  

  if (im_type==10)
  {    
    for (y=0;y<h;y++)
    {
      sl=im->scan_line(h-y-1);

      for (x=0;x<w;)
      {
        ctrl=fp->read_byte();
        if (ctrl&0x80)
        {
          fp->read(bgra,4);
          ctrl&=(~0x80);
          ctrl++;
          if (bgra[3])
          {
            if (ll && lr==bgra[2] && lg==bgra[1] && lb==bgra[0])
              c=lc;
            else
            {
              c=pal->find_closest_non0(bgra[2],bgra[1],bgra[0]);
              lr=bgra[2];
              lg=bgra[1];
              lb=bgra[0];
              lc=c;
              ll=1;
            }

          }
          else c=0;

          while (ctrl--)
          {
            *sl=c;
            sl++;
            x++;
          }
        } else
        {
          ctrl++;          
          while (ctrl--)
          {
            fp->read(bgra,4);
            if (bgra[3])
            {
              c=pal->find_closest_non0(bgra[2],bgra[1],bgra[0]);
            }
            else c=0;
            *sl=c;
            sl++;
            x++;
          }
        }
      }
    }    
  }



  delete fp;
  return im;
}