]> 4ch.mooo.com Git - 16.git/blob - src/v2/source/verge/MAPED/PCX.C
wwww
[16.git] / src / v2 / source / verge / MAPED / PCX.C
1 /*\r
2 Copyright (C) 1998 BJ Eirich (aka vecna)\r
3 This program is free software; you can redistribute it and/or\r
4 modify it under the terms of the GNU General Public License\r
5 as published by the Free Software Foundation; either version 2\r
6 of the License, or (at your option) any later version.\r
7 This program is distributed in the hope that it will be useful,\r
8 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
10 See the GNU General Public Lic\r
11 See the GNU General Public License for more details.\r
12 You should have received a copy of the GNU General Public License\r
13 along with this program; if not, write to the Free Software\r
14 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\r
15 */\r
16 \r
17 #include <stdio.h>\r
18 #include "timer.h"\r
19 #include "vga.h"\r
20 \r
21 char manufacturer;                     // pcx header\r
22 char version;\r
23 char encoding;\r
24 char bits_per_pixel;\r
25 short int xmin,ymin;\r
26 short int xmax,ymax;\r
27 short int hres;\r
28 short int vres;\r
29 char palette[48];\r
30 char reserved;\r
31 char color_planes;\r
32 short int bytes_per_line;\r
33 short int palette_type;\r
34 char filler[58];\r
35 \r
36 unsigned short int width,depth;\r
37 unsigned short int bytes, i;\r
38 unsigned char c, run, ss=0;\r
39 unsigned int vidoffset, n=0;\r
40 FILE *pcxf;\r
41 \r
42 void ReadPCXLine(unsigned char *dest)\r
43 { int j;\r
44   n=0;\r
45 \r
46   do {\r
47   c=fgetc(pcxf) & 0xff;\r
48     if ((c & 0xc0)==0xc0) {\r
49        run=c & 0x3f;\r
50        c=fgetc(pcxf);\r
51        for (j=0; j<run; j++)\r
52            dest[vidoffset+n+j]=c;\r
53        n+=run; }\r
54     else { dest[vidoffset+n]=c;\r
55            n++; }\r
56   } while (n<bytes);\r
57   fread(strbuf, 1, bytes_per_line-width, pcxf);\r
58 }\r
59 \r
60 void LoadPCXHeader(char *fname)\r
61 {\r
62   if (!(pcxf=fopen(fname,"rb"))) err("Could not open specified PCX file.");\r
63   fread(&manufacturer,1,1,pcxf);\r
64   fread(&version,1,1,pcxf);\r
65   fread(&encoding,1,1,pcxf);\r
66   fread(&bits_per_pixel,1,1,pcxf);\r
67   fread(&xmin,1,2,pcxf);\r
68   fread(&ymin,1,2,pcxf);\r
69   fread(&xmax,1,2,pcxf);\r
70   fread(&ymax,1,2,pcxf);\r
71   fread(&hres,1,2,pcxf);\r
72   fread(&vres,1,2,pcxf);\r
73   fread(&palette,1,48,pcxf);\r
74   fread(&reserved,1,1,pcxf);\r
75   fread(&color_planes,1,1,pcxf);\r
76   fread(&bytes_per_line,1,2,pcxf);\r
77   fread(&palette_type,1,2,pcxf);\r
78   fread(&filler,1,58,pcxf);\r
79   fseek(pcxf,-768L,SEEK_END);\r
80   fread(pal,1,768,pcxf);\r
81   fseek(pcxf,128L,SEEK_SET);\r
82   width=xmax-xmin+1;\r
83   depth=ymax-ymin+1;\r
84   bytes=bytes_per_line;\r
85 \r
86   for (i=0; i<768; i++)\r
87     pal[i]=pal[i] >> 2;\r
88   set_intensity(63);\r
89 }\r
90 \r
91 void LoadPCXHeaderNP(char *fname)\r
92 {\r
93   if (!(pcxf=fopen(fname,"rb"))) err("Could not open specified PCX file.");\r
94   fread(&manufacturer,1,1,pcxf);\r
95   fread(&version,1,1,pcxf);\r
96   fread(&encoding,1,1,pcxf);\r
97   fread(&bits_per_pixel,1,1,pcxf);\r
98   fread(&xmin,1,2,pcxf);\r
99   fread(&ymin,1,2,pcxf);\r
100   fread(&xmax,1,2,pcxf);\r
101   fread(&ymax,1,2,pcxf);\r
102   fread(&hres,1,2,pcxf);\r
103   fread(&vres,1,2,pcxf);\r
104   fread(&palette,1,48,pcxf);\r
105   fread(&reserved,1,1,pcxf);\r
106   fread(&color_planes,1,1,pcxf);\r
107   fread(&bytes_per_line,1,2,pcxf);\r
108   fread(&palette_type,1,2,pcxf);\r
109   fread(&filler,1,58,pcxf);\r
110   width=xmax-xmin+1;\r
111   depth=ymax-ymin+1;\r
112   bytes=bytes_per_line;\r
113 }\r
114 \r
115 void loadpcx(char *fname, char *dest)\r
116 {\r
117   LoadPCXHeader(fname);\r
118 \r
119   for (i=0; i<depth; i++)\r
120     { vidoffset=5648+(i*352);\r
121       ReadPCXLine(dest); }\r
122 \r
123   fclose(pcxf);\r
124 }\r
125 \r
126 void WritePCXLine(unsigned char *p)\r
127 { int i;\r
128   unsigned char byte,samect,repcode;\r
129 \r
130   i=0;\r
131   do\r
132   {   byte=p[i++];\r
133       samect=1;\r
134       while (samect<(unsigned) 63 && i<320 && byte==p[i])\r
135       {\r
136          samect++;\r
137          i++;\r
138       }\r
139       if (samect>1 || (byte & 0xC0) != 0)\r
140       {\r
141          repcode=0xC0 | samect;\r
142          fwrite(&repcode,1,1,pcxf);\r
143       }\r
144       fwrite(&byte,1,1,pcxf);\r
145   } while (i<320);\r
146 }\r
147 \r
148 void WritePalette()\r
149 { char b;\r
150   int i;\r
151 \r
152   for (i=0; i<768; i++)\r
153       pal[i]=pal[i] << 2;\r
154 \r
155   b=12; fwrite(&b, 1, 1, pcxf);\r
156   fwrite(pal, 1, 768, pcxf);\r
157 \r
158   for (i=0; i<768; i++)\r
159       pal[i]=pal[i] >> 2;\r
160 }\r
161 \r
162 void ScreenShot()\r
163 { unsigned char b1;\r
164   unsigned short int w1;\r
165   char fnamestr[13];\r
166 \r
167   // Takes a snapshot of the current screen.\r
168 \r
169    dec_to_asciiz(ss,&fnamestr);\r
170    b1=strlen(&fnamestr);\r
171    fnamestr[b1++]='.';\r
172    fnamestr[b1++]='P';\r
173    fnamestr[b1++]='C';\r
174    fnamestr[b1++]='X';\r
175    fnamestr[b1++]=0;\r
176 \r
177    pcxf=fopen(&fnamestr,"wb");\r
178    ss++;\r
179 \r
180 // Write PCX header\r
181 \r
182    b1=10; fwrite(&b1, 1, 1, pcxf); // manufacturer always = 10\r
183    b1=5; fwrite(&b1, 1, 1, pcxf);  // version = 3.0, >16 colors\r
184    b1=1; fwrite(&b1, 1, 1, pcxf);  // encoding always = 1\r
185    b1=8; fwrite(&b1, 1, 1, pcxf);  // 8 bits per pixel, for 256 colors\r
186    w1=0; fwrite(&w1, 1, 2, pcxf);  // xmin = 0;\r
187    w1=0; fwrite(&w1, 1, 2, pcxf);  // ymin = 0;\r
188  w1=319; fwrite(&w1, 1, 2, pcxf);  // xmax = 319;\r
189  w1=199; fwrite(&w1, 1, 2, pcxf);  // ymax = 199;\r
190  w1=320; fwrite(&w1, 1, 2, pcxf);  // hres = 320;\r
191  w1=200; fwrite(&w1, 1, 2, pcxf);  // vres = 200;\r
192 \r
193  fwrite(virscr,1,48,pcxf);     // 16-color palette data. Who knows what's\r
194                                // actually in here. It doesn't matter since\r
195                                // the 256-color palette is stored elsewhere.\r
196 \r
197  b1=0; fwrite(&b1, 1, 1, pcxf);   // reserved always = 0.\r
198  b1=1; fwrite(&b1, 1, 1, pcxf);   // number of color planes. Just 1 for 8bit.\r
199  w1=320; fwrite(&w1, 1, 2, pcxf); // number of bytes per line\r
200 \r
201  w1=0; fwrite(&w1, 1, 1, pcxf);\r
202  fwrite(virscr, 1, 59, pcxf);          // filler\r
203 \r
204  for (w1=0; w1<200; w1++)\r
205      WritePCXLine(screen+(w1*320));\r
206 \r
207  WritePalette();\r
208  fclose(pcxf);\r
209  timer_count=0;\r
210 }\r