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

Last change on this file since 494 was 494, checked in by Sam Hocevar, 8 years ago

style: remove trailing spaces, fix copyright statements.

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