-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathtga_read_header.m
162 lines (145 loc) · 4.96 KB
/
tga_read_header.m
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function info = tga_read_header(fname)
% function for reading header of Truevision TARGA file, TGA, VDA, ICB VST
%
% info = tga_read_header(filename);
%
% examples:
% 1, info=tga_read_header()
% 2, info=tga_read_header('example.tga');
%
% typedef struct
%
% byte identsize; // size of ID field that follows 18 byte header (0 usually)
% byte Colormaptype; // type of Color map 0=none, 1=has palette
% byte imagetype; // type of image 0=none,1=indexed,2=rgb,3=grey,+8=rle packed
%
% short Colormapstart; // first Color map entry in palette
% short Colormaplength; // number of Colors in palette
% byte Colormapbits; // number of bits per palette entry 15,16,24,32%
%
% short xstart; // image x origin
% short ystart; // image y origin
% short width; // image width in pixels
% short height; // image height in pixels
% byte bits; // image bits per pixel 8,16,24,32
% byte descriptor; // image descriptor bits (vh flip bits)
%
% // pixel data follows header
% 24 bits
% TGA_HEADER
if(exist('fname','var')==0)
[filename, pathname] = uigetfile('*.tga;*.vda;*.icb,*.vst', 'Read tga-file');
fname = [pathname filename];
end
f=fopen(fname,'rb','l');
if(f<0)
fprintf('could not open file %s\n',fname);
return
end
info.Filename=fname;
% Footer Header
fseek(f,-26,'eof');
info.ExtensionArea=fread(f, 1, 'long');
info.DeveloperDirectory=fread(f, 1, 'long');
info.Signature=fread(f, 17, 'char=>char')';
if(strcmp(info.Signature,'TRUEVISION-XFILE.'))
info.Version='new';
else
info.Version='old';
end
% File Start Header
fseek(f,0,'bof');
info.IDlength = fread(f, 1, 'uint8');
info.ColorMapType = fread(f, 1, 'uint8');
info.ImageType =fread(f, 1, 'uint8');
switch(info.ImageType )
case 0
info.ImageTypeString='No Image Data';
info.Rle=false;
case 1
info.ImageTypeString='Uncompressed, Color-mapped image';
info.Rle=false;
case 2
info.ImageTypeString='Uncompressed, True-color image';
info.Rle=false;
case 3
info.ImageTypeString='Uncompressed, True-color image';
info.Rle=false;
case 9
info.ImageTypeString='Run-length encoded Color-mapped Image';
info.Rle=true;
case 10
info.ImageTypeString='Run-length encoded True-color Image Image';
info.Rle=true;
case 11
info.ImageTypeString='Run-length encoded Black-and-white Image';
info.Rle=true;
otherwise
info.ImageTypeString='unknown';
info.Rle=false;
end
% Color Map Specification (offset 3)
info.ColorMapStart = fread(f, 1, 'short');
info.ColorMapLength = fread(f, 1, 'short');
info.ColorMapBits = fread(f, 1, 'uint8');
info.ColorMapStoreBits = 8*ceil(info.ColorMapBits/8);
% Image specification offset 8
info.XOrigin = fread(f, 1, 'short');
info.YOrigin = fread(f, 1, 'short');
info.Width = fread(f, 1, 'short');
info.Height = fread(f, 1, 'short');
info.Depth = fread(f, 1, 'uint8');
b= bitget(fread(f, 1, 'uint8=>uint8'),1:8);
info.ImageDescriptor =b;
info.AlphaChannelBits=b(1)+b(2)*2+b(3)*4+b(4)*8;
if((b(6)==0)&&(b(5)==0)), info.ImageOrigin='bottom left'; end
if((b(6)==0)&&(b(5)==1)), info.ImageOrigin='bottom right'; end
if((b(6)==1)&&(b(5)==0)), info.ImageOrigin='top left'; end
if((b(6)==1)&&(b(5)==1)), info.ImageOrigin='top right'; end
info.ImageID= fread(f, info.IDlength, 'uint8');
if(info.ColorMapType==0)
info.ColorMap=[];
else
ColorMap = fread(f,info.ColorMapLength*(info.ColorMapStoreBits/8),'uint8=>uint8');
switch(info.ColorMapStoreBits)
case 16
% BitsPerColor = min( info.bits/3, 8);
%info.ColorMap=reshape(ColorMap,[info.ColorMapLength 2]);
case 24
info.ColorMap=reshape(ColorMap,[3 info.ColorMapLength])';
info.ColorMap=info.ColorMap(:,3:-1:1);
case 32
info.ColorMap=reshape(ColorMap,[4 info.ColorMapLength])';
info.ColorMap=info.ColorMap(:,[3 2 1 4]);
end
info.ColorMap=double(info.ColorMap)/255;
end
% Get the header size
info.HeaderSize=ftell(f);
% Get the file length
fseek(f,0,'eof');
info.FileSize = ftell(f);
fclose(f);
% Closing Header Contains :
% Developer Fields
% Developer Directory
% Extension Size (offset =0) , short
% Author Name (offset = 2) , asci 41
% Author Comments ( offset = 43), asci 324
% Date Time, (offset=367), shorts 6
% Job Name/ID(offset=379) , 41 ascii
% Job Time (offset = 420) , 41 asci
% Software version (offset=467), 3 bytes
% Key color (offset=470), long
% Pixel Aspect Ratio (offset = 474) , 2shorts
% Gamma Value (offset = 478 ), 2 shorts
% Color Correction Offset (offset=482), long
% Postage Stamp Offset(offset=486), long
% Scan Line Offset ( offset=490), long
% Attributes Type (offset=494)
% Scan Line Table
% Postage Stamp Image
% Color Correction Table (1K shorts)
% Extentsion Area (offset=0), long
% Developer Directory (offset=4),long
% Signature (offset=8) , ASCI