pcx.c
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
** Nofrendo (c) 1998-2000 Matthew Conte (matt@conte.com)
**
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of version 2 of the GNU Library General
** Public License as published by the Free Software Foundation.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Library General Public License for more details. To obtain a
** copy of the GNU Library General Public License, write to the Free
** Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** Any permitted reproduction of these routines, in whole or in part,
** must bear this legend.
**
**
** pcx.c
**
** PCX format screen-saving routines
** $Id: pcx.c,v 1.2 2001/04/27 14:37:11 neil Exp $
*/
#include <stdio.h>
#include <string.h>
#include <noftypes.h>
#include <bitmap.h>
#include <pcx.h>
/* Save a PCX snapshot from a given NES bitmap */
int pcx_write(char *filename, bitmap_t *bmp, rgb_t *pal)
{
FILE *fp;
pcxheader_t header;
int i, line;
int width, height, x_min, y_min;
ASSERT(bmp);
width = bmp->width;
height = bmp->height;
x_min = 0;
y_min = 0;
fp = fopen(filename, "wb");
if (NULL == fp)
return -1;
/* Fill in the header nonsense */
memset(&header, 0, sizeof(header));
header.Manufacturer = 10;
header.Version = 5;
header.Encoding = 1;
header.BitsPerPixel = 8;
header.Xmin = x_min;
header.Ymin = y_min;
header.Xmax = width - 1;
header.Ymax = height - 1;
header.NPlanes = 1;
header.BytesPerLine = width;
header.PaletteInfo = 1;
header.HscreenSize = width - 1;
header.VscreenSize = height - 1;
fwrite(&header, 1, sizeof(header), fp);
/* RLE encoding */
for (line = 0; line < height; line++)
{
uint8 last, *mem;
int xpos = 0;
mem = bmp->line[line + y_min] + x_min;
while (xpos < width)
{
int rle_count = 0;
do
{
last = *mem++;
xpos++;
rle_count++;
}
while (*mem == last && xpos < width && rle_count < 0x3F);
if (rle_count > 1 || 0xC0 == (last & 0xC0))
{
fputc(0xC0 | rle_count, fp);
fputc(last, fp);
}
else
{
fputc(last, fp);
}
}
}
/* Write palette */
fputc(0x0C, fp); /* $0C signifies 256 color palette */
for (i = 0; i < 256; i++)
{
fputc(pal[i].r, fp);
fputc(pal[i].g, fp);
fputc(pal[i].b, fp);
}
/* We're done! */
fclose(fp);
return 0;
}
/*
** $Log: pcx.c,v $
** Revision 1.2 2001/04/27 14:37:11 neil
** wheeee
**
** Revision 1.1.1.1 2001/04/27 07:03:54 neil
** initial
**
** Revision 1.8 2000/11/06 05:17:48 matt
** better!
**
** Revision 1.7 2000/11/05 16:37:18 matt
** rolled rgb.h into bitmap.h
**
** Revision 1.6 2000/07/17 01:52:27 matt
** made sure last line of all source files is a newline
**
** Revision 1.5 2000/06/26 04:55:13 matt
** changed routine name, big whoop
**
** Revision 1.4 2000/06/09 15:12:25 matt
** initial revision
**
*/