source: oup/releases/0.11a/Unit6_imgfuncs.pas@ 455

Last change on this file since 455 was 19, checked in by alloc, 18 years ago
File size: 8.9 KB
Line 
1UNIT Unit6_imgfuncs;
2INTERFACE
3USES Math, Unit3_data;
4
5TYPE
6 TImgPackage=RECORD
7 imgx,imgy:Word;
8 imgdepth:Byte;
9 storetype:Byte;
10 datasize:LongWord;
11 raw_addr:LongWord;
12 imgdata:Tdata;
13 END;
14FUNCTION ResizeImage(oldx,oldy:LongWord; imgdepth:Byte; data:Tdata):Tdata;
15FUNCTION RevertImage(imgx,imgy,imgdepth:LongWord; imgdata:Tdata):Tdata;
16FUNCTION DecompressImage(imgx,imgy:LongWord; imgdata:Tdata):Tdata;
17FUNCTION ImgdataToBmp(imgx,imgy,imgdepth,storetype:LongWord; imgdata:Tdata):Tdata;
18FUNCTION LoadImgData(fileid:LongWord):TImgPackage;
19
20IMPLEMENTATION
21USES Unit2_functions;
22
23
24FUNCTION ResizeImage(oldx,oldy:LongWord; imgdepth:Byte; data:Tdata):Tdata;
25 VAR
26 i,j:LongWord;
27 col,row,row_orig:LongWord;
28 temparray:Tdata;
29 BEGIN
30 SetLength(temparray,(oldx DIV 2)*(oldy DIV 2)*(imgdepth DIV 8));
31 row_orig:=0;
32 row:=0;
33 col:=0;
34 FOR i:=0 TO (oldx*oldy)-1 DO BEGIN
35 IF ((i MOD oldx)=0) AND (i>0) THEN BEGIN
36 Inc(row_orig);
37 IF (row_orig MOD 2)=0 THEN BEGIN
38 Inc(row);
39 col:=0;
40 END;
41 END;
42 IF (row_orig MOD 2)=0 THEN BEGIN
43 IF (i MOD 2)=0 THEN BEGIN
44 FOR j:=0 TO (imgdepth DIV 8)-1 DO
45 temparray[((row*(oldx DIV 2))+col)*(imgdepth DIV 8)+j]:=data[i*2+j];
46 Inc(col);
47 END;
48 END;
49 END;
50 Result:=temparray;
51 END;
52
53
54FUNCTION RevertImage(imgx,imgy,imgdepth:LongWord; imgdata:Tdata):Tdata;
55 VAR
56 x,y,i:LongWord;
57 BEGIN
58 SetLength(Result,imgx*imgy*(imgdepth DIV 8));
59 FOR y:=0 TO imgy-1 DO
60 FOR x:=0 TO imgx-1 DO
61 FOR i:=0 TO (imgdepth DIV 8)-1 DO
62 Result[((imgx*(imgy-1-y)+x)*(imgdepth DIV 8))+i]:=
63 imgdata[(imgx*y+x)*(imgdepth DIV 8)+i];
64 END;
65
66
67FUNCTION DecompressImage(imgx,imgy:LongWord; imgdata:Tdata):Tdata;
68 TYPE
69 Tcolor=RECORD
70 RGBb:Byte;
71 RGBg:Byte;
72 RGBr:Byte;
73 RGBa:Byte;
74 END;
75 VAR
76 i,j,x,y:LongWord;
77 color:Array[1..4] OF Tcolor;
78 pixel:Array[1..16] OF Byte;
79 BEGIN
80 x:=0;
81 y:=0;
82 SetLength(Result,imgx*imgy*4);
83 FOR i:=0 TO ((imgx*imgy) DIV 16)-1 DO BEGIN
84 Color[1].RGBb:=Floor(((imgdata[(i*8)+0]+imgdata[(i*8)+1]*256) AND $001F) / $001F * 255);
85 Color[1].RGBg:=Floor(((imgdata[(i*8)+0]+imgdata[(i*8)+1]*256) AND $07E0) / $07E0 * 255);
86 Color[1].RGBr:=Floor(((imgdata[(i*8)+0]+imgdata[(i*8)+1]*256) AND $F800) / $F800 * 255);
87 Color[1].RGBa:=255;
88 Color[2].RGBb:=Floor(((imgdata[(i*8)+2]+imgdata[(i*8)+3]*256) AND $001F) / $001F * 255);
89 Color[2].RGBg:=Floor(((imgdata[(i*8)+2]+imgdata[(i*8)+3]*256) AND $07E0) / $07E0 * 255);
90 Color[2].RGBr:=Floor(((imgdata[(i*8)+2]+imgdata[(i*8)+3]*256) AND $F800) / $F800 * 255);
91 Color[2].RGBa:=255;
92 Color[3].RGBb:=Floor( Color[1].RGBb/3*2 + Color[2].RGBb/3 );
93 Color[3].RGBg:=Floor( Color[1].RGBg/3*2 + Color[2].RGBg/3 );
94 Color[3].RGBr:=Floor( Color[1].RGBr/3*2 + Color[2].RGBr/3 );
95 Color[3].RGBa:=255;
96 Color[4].RGBb:=Floor( Color[1].RGBb/3 + Color[2].RGBb/3*2 );
97 Color[4].RGBg:=Floor( Color[1].RGBg/3 + Color[2].RGBg/3*2 );
98 Color[4].RGBr:=Floor( Color[1].RGBr/3 + Color[2].RGBr/3*2 );
99 Color[4].RGBa:=255;
100 Pixel[1]:=Floor( (imgdata[(i*8)+4] AND $C0) / $40 + 1 );
101 Pixel[2]:=Floor( (imgdata[(i*8)+4] AND $30) / $10 + 1 );
102 Pixel[3]:=Floor( (imgdata[(i*8)+4] AND $0C) / $04 + 1 );
103 Pixel[4]:=Floor( (imgdata[(i*8)+4] AND $03) + 1 );
104 Pixel[5]:=Floor( (imgdata[(i*8)+5] AND $C0) / $40 + 1 );
105 Pixel[6]:=Floor( (imgdata[(i*8)+5] AND $30) / $10 + 1 );
106 Pixel[7]:=Floor( (imgdata[(i*8)+5] AND $0C) / $04 + 1 );
107 Pixel[8]:=Floor( (imgdata[(i*8)+5] AND $03) + 1 );
108 Pixel[9]:=Floor( (imgdata[(i*8)+6] AND $C0) / $40 + 1 );
109 Pixel[10]:=Floor( (imgdata[(i*8)+6] AND $30) / $10 + 1 );
110 Pixel[11]:=Floor( (imgdata[(i*8)+6] AND $0C) / $04 + 1 );
111 Pixel[12]:=Floor( (imgdata[(i*8)+6] AND $03) + 1 );
112 Pixel[13]:=Floor( (imgdata[(i*8)+7] AND $C0) / $40 + 1 );
113 Pixel[14]:=Floor( (imgdata[(i*8)+7] AND $30) / $10 + 1 );
114 Pixel[15]:=Floor( (imgdata[(i*8)+7] AND $0C) / $04 + 1 );
115 Pixel[16]:=Floor( (imgdata[(i*8)+7] AND $03) + 1 );
116 FOR j:=0 TO 3 DO BEGIN
117 Result[((y+3)*imgx+x+j)*3+0]:=Color[Pixel[16-j]].RGBb;
118 Result[((y+3)*imgx+x+j)*3+1]:=Color[Pixel[16-j]].RGBg;
119 Result[((y+3)*imgx+x+j)*3+2]:=Color[Pixel[16-j]].RGBr;
120 END;
121 FOR j:=0 TO 3 DO BEGIN
122 Result[((y+2)*imgx+x+j)*3+0]:=Color[Pixel[12-j]].RGBb;
123 Result[((y+2)*imgx+x+j)*3+1]:=Color[Pixel[12-j]].RGBg;
124 Result[((y+2)*imgx+x+j)*3+2]:=Color[Pixel[12-j]].RGBr;
125 END;
126 FOR j:=0 TO 3 DO BEGIN
127 Result[((y+1)*imgx+x+j)*3+0]:=Color[Pixel[8-j]].RGBb;
128 Result[((y+1)*imgx+x+j)*3+1]:=Color[Pixel[8-j]].RGBg;
129 Result[((y+1)*imgx+x+j)*3+2]:=Color[Pixel[8-j]].RGBr;
130 END;
131 FOR j:=0 TO 3 DO BEGIN
132 Result[((y+0)*imgx+x+j)*3+0]:=Color[Pixel[4-j]].RGBb;
133 Result[((y+0)*imgx+x+j)*3+1]:=Color[Pixel[4-j]].RGBg;
134 Result[((y+0)*imgx+x+j)*3+2]:=Color[Pixel[4-j]].RGBr;
135 END;
136 x:=x+4;
137 IF x=imgx THEN BEGIN
138 y:=y+4;
139 x:=0;
140 END;
141 END;
142 END;
143
144
145FUNCTION ImgdataToBmp(imgx,imgy,imgdepth,storetype:LongWord; imgdata:Tdata):Tdata;
146 CONST BMPheader:Array[0..53] OF Byte=
147 ($42,$4D,0,0,0,0,0,0,0,0,54,0,0,0,
148 40,0,0,0,0,0,0,0,0,0,0,0,1,0,$18,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
149 VAR
150 i,x,y:LongWord;
151 BEGIN
152 CASE storetype OF
153 0: BEGIN
154 SetLength(Result,imgx*imgy*3);
155 FOR y:=0 TO imgy-1 DO BEGIN
156 FOR x:=0 TO imgx-1 DO BEGIN
157 Result[((imgx*y+x)*3)+0]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $000F ) / $000F * 255);
158 Result[((imgx*y+x)*3)+1]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $00F0 ) / $00F0 * 255);
159 Result[((imgx*y+x)*3)+2]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $0F00 ) / $0F00 * 255);
160 END;
161 END;
162 END;
163 1,2,8: BEGIN
164 SetLength(Result,imgx*imgy*3);
165 FOR y:=0 TO imgy-1 DO BEGIN
166 FOR x:=0 TO imgx-1 DO BEGIN
167 Result[((imgx*y+x)*3)+0]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $001F ) / $001F * 255);
168 Result[((imgx*y+x)*3)+1]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $03E0 ) / $03E0 * 255);
169 Result[((imgx*y+x)*3)+2]:=Floor( ( (imgdata[(imgx*y+x)*2]+imgdata[(imgx*y+x)*2+1]*256) AND $7C00 ) / $7C00 * 255);
170 END;
171 END;
172 END;
173 9: BEGIN
174 Result:=DecompressImage(imgx,imgy,imgdata);
175 END;
176 END;
177 Result:=RevertImage(imgx,imgy,24,Result);
178 SetLength(Result,imgx*imgy*3+54);
179 FOR i:=High(Result)-54 DOWNTO 0 DO Result[i+54]:=Result[i];
180
181 FOR i:=0 TO High(BMPheader) DO Result[i]:=BMPheader[i];
182 Result[2]:=((imgx*imgy*3+54) AND $000000FF);
183 Result[3]:=((imgx*imgy*3+54) AND $0000FF00) DIV $100;
184 Result[4]:=((imgx*imgy*3+54) AND $00FF0000) DIV $10000;
185 Result[5]:=((imgx*imgy*3+54) AND $FF000000) DIV $1000000;
186 Result[18]:=(imgx AND $000000FF) DIV $1;
187 Result[19]:=(imgx AND $0000FF00) DIV $100;
188 Result[20]:=(imgx AND $00FF0000) DIV $10000;
189 Result[21]:=(imgx AND $FF000000) DIV $1000000;
190 Result[22]:=(imgy AND $000000FF) DIV $1;
191 Result[23]:=(imgy AND $0000FF00) DIV $100;
192 Result[24]:=(imgy AND $00FF0000) DIV $10000;
193 Result[25]:=(imgy AND $FF000000) DIV $1000000;
194 Result[34]:=((imgx*imgy*3) AND $000000FF) DIV $1;
195 Result[35]:=((imgx*imgy*3) AND $0000FF00) DIV $100;
196 Result[36]:=((imgx*imgy*3) AND $00FF0000) DIV $10000;
197 Result[37]:=((imgx*imgy*3) AND $FF000000) DIV $1000000;
198 END;
199
200FUNCTION LoadImgData(fileid:LongWord):TImgPackage;
201{ VAR
202 raw_addr:LongWord;
203 storetype:Byte;
204 imgx,imgy:Word;
205 imgdepth:Byte;
206 data:Tdata;
207 datasize:LongWord;}
208 BEGIN
209 LoadDatFilePart(fileid,$9C,SizeOf(Result.raw_addr),@Result.raw_addr);
210 LoadDatFilePart(fileid,$8C,SizeOf(Result.imgx),@Result.imgx);
211 LoadDatFilePart(fileid,$8E,SizeOf(Result.imgy),@Result.imgy);
212 LoadDatFilePart(fileid,$90,SizeOf(Result.storetype),@Result.storetype);
213
214 CASE Result.storetype OF
215 0,1,2: BEGIN
216 Result.datasize:=Result.imgx*Result.imgy*2;
217 Result.imgdepth:=16;
218 END;
219 8: BEGIN
220 Result.datasize:=Result.imgx*Result.imgy*4;
221 Result.imgdepth:=32;
222 END;
223 9: BEGIN
224 Result.datasize:=Result.imgx*Result.imgy DIV 2;
225 Result.imgdepth:=16;
226 END;
227 ELSE
228 Exit;
229 END;
230 SetLength(Result.imgdata,Result.datasize);
231
232 LoadRawFilePart(Result.raw_addr,Result.datasize,@Result.imgdata[0]);
233 END;
234
235END.
Note: See TracBrowser for help on using the repository browser.