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

Last change on this file since 481 was 481, checked in by Sam Hocevar, 12 years ago

Fuck the history, I'm renaming all .hpp files to .h for my own sanity.

File size: 2.6 KB
RevLine 
[56]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
[481]12#include "pcxread.h"
13#include "specs.h"
[2]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{
[124]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;
[17]31  write_uint16(fp,PCX_header.xmin);
[124]32  write_uint16(fp,PCX_header.ymin);
[17]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);
[2]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;
[124]40  write_uint16(fp,PCX_header.bytes_per_line);
41  write_uint16(fp,PCX_header.palette_type);
[2]42  if (!fwrite(PCX_header.filter,1,58,fp)) return 0;
43  return 1;
44}
45
[39]46void write_PCX(image *im, palette *pal, char const *filename)
[2]47{
48  FILE *fp=fopen(filename,"wb");
[124]49  if (!fp)
[2]50  {
51    set_error(imWRITE_ERROR);
52    return ;
53  }
[124]54
55  PCX_header.manufactururer=10;
56  PCX_header.version=5;
[2]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
[124]71  if (!write_PCX_header(fp))
[2]72  {
73    set_error( imWRITE_ERROR);
[124]74    return ;
[2]75  }
[124]76
[2]77  int y,run_length,x;
[124]78  unsigned char *sl,code;
[2]79  for (y=0;y<im->height();y++)
80  {
81    sl=im->scan_line(y);
82    for (x=0;x<im->width();)
83    {
[124]84      run_length=1;
[2]85      while (x+run_length<im->width() && sl[x]==sl[x+run_length])
86        run_length++;
[124]87      if (run_length==1 && sl[x]<64)
88        fputc(sl[x],fp);
[2]89      else
90      {
91        if (run_length>=64)
[124]92      run_length=63;
93    code=0xc0 | run_length;
94    fputc(code,fp);
95    fputc(sl[x],fp);
96   
97      }
[2]98      x+=run_length;
[124]99
100    }
[2]101  }
102  fputc(12,fp);  // note that there is a palette attached
103  fwrite(pal->addr(),1,256*3,fp);
[124]104  fclose(fp);
[2]105}
106
Note: See TracBrowser for help on using the repository browser.