source: abuse/trunk/src/imlib/pcxread.cpp @ 134

Last change on this file since 134 was 134, checked in by Sam Hocevar, 14 years ago
  • Removed 4300 lines of unused code.
File size: 2.6 KB
Line 
1/*
2 *  Abuse - dark 2D side-scrolling platform game
3 *  Copyright (c) 1995 Crack dot Com
4 *
5 *  This software was released into the Public Domain. As with most public
6 *  domain software, no warranty is made or implied by Crack dot Com or
7 *  Jonathan Clark.
8 */
9
10#include "config.h"
11
12#include "pcxread.hpp"
13#include "specs.hpp"
14
15struct PCX_header_type
16{
17  char manufactururer,version,encoding,bits_per_pixel;
18  short xmin,ymin,xmax,ymax,hres,vres;
19  char palette[48];
20  char reserved,color_planes;
21  short bytes_per_line,palette_type;
22  char filter[58];
23} PCX_header;
24
25int write_PCX_header(FILE *fp)
26{
27  if (!fwrite(&PCX_header.manufactururer,1,1,fp)) return 0;
28  if (!fwrite(&PCX_header.version,1,1,fp)) return 0;
29  if (!fwrite(&PCX_header.encoding,1,1,fp)) return 0;
30  if (!fwrite(&PCX_header.bits_per_pixel,1,1,fp)) return 0;
31  write_uint16(fp,PCX_header.xmin);
32  write_uint16(fp,PCX_header.ymin);
33  write_uint16(fp,PCX_header.xmax);
34  write_uint16(fp,PCX_header.ymax);
35  write_uint16(fp,PCX_header.hres);
36  write_uint16(fp,PCX_header.vres);
37  if (!fwrite(PCX_header.palette,1,48,fp)) return 0;
38  if (!fwrite(&PCX_header.reserved,1,1,fp)) return 0;
39  if (!fwrite(&PCX_header.color_planes,1,1,fp)) return 0;
40  write_uint16(fp,PCX_header.bytes_per_line);
41  write_uint16(fp,PCX_header.palette_type);
42  if (!fwrite(PCX_header.filter,1,58,fp)) return 0;
43  return 1;
44}
45
46void write_PCX(image *im, palette *pal, char const *filename)
47{
48  FILE *fp=fopen(filename,"wb");
49  if (!fp)
50  {
51    set_error(imWRITE_ERROR);
52    return ;
53  }
54
55  PCX_header.manufactururer=10;
56  PCX_header.version=5;
57  PCX_header.encoding=1;
58  PCX_header.bits_per_pixel=8;
59  PCX_header.xmin=0;
60  PCX_header.ymin=0;
61  PCX_header.xmax=im->width()-1;
62  PCX_header.ymax=im->height()-1;
63  PCX_header.hres=320;
64  PCX_header.vres=200;
65  PCX_header.reserved=0;
66  PCX_header.color_planes=1;
67  PCX_header.bytes_per_line=im->width();
68  PCX_header.palette_type=0;
69  memset(PCX_header.filter,0,58);
70
71  if (!write_PCX_header(fp))
72  {
73    set_error( imWRITE_ERROR);
74    return ;
75  }
76
77  int y,run_length,x;
78  unsigned char *sl,code;
79  for (y=0;y<im->height();y++)
80  {
81    sl=im->scan_line(y);
82    for (x=0;x<im->width();)
83    {
84      run_length=1;
85      while (x+run_length<im->width() && sl[x]==sl[x+run_length])
86        run_length++;
87      if (run_length==1 && sl[x]<64)
88        fputc(sl[x],fp);
89      else
90      {
91        if (run_length>=64)
92      run_length=63;
93    code=0xc0 | run_length;
94    fputc(code,fp);
95    fputc(sl[x],fp);
96   
97      }
98      x+=run_length;
99
100    }
101  }
102  fputc(12,fp);  // note that there is a palette attached
103  fwrite(pal->addr(),1,256*3,fp);
104  fclose(fp);
105}
106
Note: See TracBrowser for help on using the repository browser.