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

Last change on this file since 512 was 512, checked in by Sam Hocevar, 7 years ago

imlib: use vec2i for image::size and unroll all necessary changes
everywhere else in the code.

File size: 2.7 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 "common.h"
14
15#include "pcxread.h"
16#include "specs.h"
17
18struct PCX_header_type
19{
20  char manufactururer,version,encoding,bits_per_pixel;
21  short xmin,ymin,xmax,ymax,hres,vres;
22  char palette[48];
23  char reserved,color_planes;
24  short bytes_per_line,palette_type;
25  char filter[58];
26} PCX_header;
27
28int write_PCX_header(FILE *fp)
29{
30  if (!fwrite(&PCX_header.manufactururer,1,1,fp)) return 0;
31  if (!fwrite(&PCX_header.version,1,1,fp)) return 0;
32  if (!fwrite(&PCX_header.encoding,1,1,fp)) return 0;
33  if (!fwrite(&PCX_header.bits_per_pixel,1,1,fp)) return 0;
34  write_uint16(fp,PCX_header.xmin);
35  write_uint16(fp,PCX_header.ymin);
36  write_uint16(fp,PCX_header.xmax);
37  write_uint16(fp,PCX_header.ymax);
38  write_uint16(fp,PCX_header.hres);
39  write_uint16(fp,PCX_header.vres);
40  if (!fwrite(PCX_header.palette,1,48,fp)) return 0;
41  if (!fwrite(&PCX_header.reserved,1,1,fp)) return 0;
42  if (!fwrite(&PCX_header.color_planes,1,1,fp)) return 0;
43  write_uint16(fp,PCX_header.bytes_per_line);
44  write_uint16(fp,PCX_header.palette_type);
45  if (!fwrite(PCX_header.filter,1,58,fp)) return 0;
46  return 1;
47}
48
49void write_PCX(image *im, palette *pal, char const *filename)
50{
51  FILE *fp=fopen(filename,"wb");
52  if (!fp)
53  {
54    set_error(imWRITE_ERROR);
55    return ;
56  }
57
58  PCX_header.manufactururer=10;
59  PCX_header.version=5;
60  PCX_header.encoding=1;
61  PCX_header.bits_per_pixel=8;
62  PCX_header.xmin=0;
63  PCX_header.ymin=0;
64  PCX_header.xmax=im->Size().x-1;
65  PCX_header.ymax=im->Size().y-1;
66  PCX_header.hres=320;
67  PCX_header.vres=200;
68  PCX_header.reserved=0;
69  PCX_header.color_planes=1;
70  PCX_header.bytes_per_line=im->Size().x;
71  PCX_header.palette_type=0;
72  memset(PCX_header.filter,0,58);
73
74  if (!write_PCX_header(fp))
75  {
76    set_error( imWRITE_ERROR);
77    return ;
78  }
79
80  int y,run_length,x;
81  unsigned char *sl,code;
82  for (y=0; y<im->Size().y; y++)
83  {
84    sl=im->scan_line(y);
85    for (x=0; x<im->Size().x; )
86    {
87      run_length=1;
88      while (x+run_length<im->Size().x && sl[x]==sl[x+run_length])
89        run_length++;
90      if (run_length==1 && sl[x]<64)
91        fputc(sl[x],fp);
92      else
93      {
94        if (run_length>=64)
95      run_length=63;
96    code=0xc0 | run_length;
97    fputc(code,fp);
98    fputc(sl[x],fp);
99
100      }
101      x+=run_length;
102
103    }
104  }
105  fputc(12,fp);  // note that there is a palette attached
106  fwrite(pal->addr(),1,256*3,fp);
107  fclose(fp);
108}
109
Note: See TracBrowser for help on using the repository browser.