File: bmpcx.c

package info (click to toggle)
ted 2.11-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 11,064 kB
  • ctags: 13,935
  • sloc: ansic: 120,446; makefile: 7,469; sh: 253
file content (297 lines) | stat: -rw-r--r-- 7,994 bytes parent folder | download | duplicates (3)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#   include	"bitmapConfig.h"

#   include	<stdio.h>
#   include	<stdlib.h>
#   include	<string.h>
#   include	"bmintern.h"
#   include	<utilEndian.h>
#   include	<appDebugon.h>

/************************************************************************/
/*  Header of a PCX file.						*/
/************************************************************************/

typedef struct _PcxHeader		/* Offset   Description            */
{
    unsigned char	Id;		/*  00h     Manufacturer ID        */
    unsigned char	Version;	/*  01h     Version                */
    unsigned char	Format;		/*  02h     Encoding Scheme        */
    unsigned char	BitsPixelPlane;	/*  03h     Bits/Pixel/Plane       */
    unsigned short	Xmin;		/*  04h     X Start (upper left)   */
    unsigned short	Ymin;		/*  06h     Y Start (top)          */
    unsigned short	Xmax;		/*  08h     X End (lower right)    */
    unsigned short	Ymax;		/*  0Ah     Y End (bottom)         */
    unsigned short	Hdpi;		/*  0Ch     Horizontal Resolution  */
    unsigned short	Vdpi;		/*  0Eh     Vertical Resolution    */
    unsigned char	EgaPalette[48];	/*  10h     16-Color EGA Palette   */
    unsigned char	Reserved;	/*  40h     Reserved               */
    unsigned char	NumberOfPlanes;	/*  41h     Number of Color Planes */
    unsigned short	BytesLinePlane;	/*  42h     Bytes/Line/Plane       */
    unsigned short	PaletteInfo;	/*  44h     Palette Interpretation */
    unsigned short	HScreenSize;	/*  46h     Horizontal Screen Size */
    unsigned short	VScreenSize;	/*  48h     Vertical Screen Size   */
    unsigned char	Filler[54];	/*  4Ah     Reserved               */
} PCXHEADER;

/************************************************************************/
/*  Read a PCX file header.						*/
/************************************************************************/

static int bmReadPcxFileHeader(	PCXHEADER *	pch,
				FILE *		f	)
    {
    int			i;

    pch->Id= getc( f );
    pch->Version= getc( f );
    pch->Format= getc( f );
    pch->BitsPixelPlane= getc( f );

    pch->Xmin= utilGetLeInt16( f );
    pch->Ymin= utilGetLeInt16( f );
    pch->Xmax= utilGetLeInt16( f );
    pch->Ymax= utilGetLeInt16( f );
    pch->Hdpi= utilGetLeInt16( f );
    pch->Vdpi= utilGetLeInt16( f );

    for ( i= 0; i < 48; i++ )
	{ pch->EgaPalette[i]= getc( f ); }

    pch->Reserved= getc( f );
    pch->NumberOfPlanes= getc( f );

    pch->BytesLinePlane= utilGetLeInt16( f );
    pch->PaletteInfo= utilGetLeInt16( f );
    pch->HScreenSize= utilGetLeInt16( f );
    pch->VScreenSize= utilGetLeInt16( f );

    for ( i= 0; i < 54; i++ )
	{ (void)getc( f ); }

    return 0;
    }

/************************************************************************/
/*  Read a PCX row.							*/
/************************************************************************/
static int bmReadPcxRow(	unsigned char *	to,
				int		width,
				FILE *		f,
				unsigned char *	pValue,
				int *		pCount	)
    {
    int		col= 0;
    int		done= 0;

    int		c;

    unsigned char	value= *pValue;
    int			count= *pCount;

    do  {
	done += count;
	while( count > 0 && col < width )
	    { to[col++]= value; count--; }

	if  ( count > 0 )
	    { done -= count; break; }

	c= getc( f );
	if  ( ( c & 0xc0 ) == 0xc0 )
	    { count= c & 0x3f;	value= getc( f );	}
	else{ count= 1;		value= c;		}

	} while ( col < width );

    *pValue= value; *pCount= count; return done;
    }

/************************************************************************/
/*  Read a PCX file.							*/
/************************************************************************/
int bmReadPcxFile(	const char *		filename,
			unsigned char **	pBuffer,
			BitmapDescription *	bd,
			int *			pPrivateFormat,
			double *		pCompressionFactor	)
    {
    FILE *		f;
    PCXHEADER		pch;

    int			row;
    int			count;
    unsigned char	c;

    unsigned char *	buffer;

    f= fopen( filename, "rb");
    if  ( ! f )
	{ SDEB(filename); return -1;	}

    if  ( bmReadPcxFileHeader( &pch, f ) )
	{ SDEB(filename); fclose( f ); return -1;	}

    if  ( pch.Id != 0xa )
	{ SXDEB(filename,pch.Id); fclose( f ); return -1;	}

    if  ( pch.Format != 1 )
	{ SXDEB(filename,pch.Format); fclose( f ); return -1;	}

    bd->bdPixelsWide= pch.Xmax- pch.Xmin+ 1;
    bd->bdPixelsHigh= pch.Ymax- pch.Ymin+ 1;

    if  ( 0 && pch.Hdpi != 0 && pch.Vdpi != 0 )
	{
	bd->bdUnit= BMunINCH;
	bd->bdXResolution= pch.Hdpi;
	bd->bdYResolution= pch.Vdpi;
	}
    else{
	bd->bdUnit= BMunPIXEL;
	bd->bdXResolution= 1;
	bd->bdYResolution= 1;
	}

    bd->bdHasAlpha= 0;

    switch( pch.BitsPixelPlane )
	{
	case 1:
	    switch( pch.NumberOfPlanes )
		{
		case 1:
		    bd->bdBitsPerSample= 1;
		    bd->bdSamplesPerPixel= 1;
		    bd->bdBitsPerPixel= 1;
		    bd->bdColorEncoding= BMcoWHITEBLACK;
		    bd->bdBytesPerRow= ( bd->bdPixelsWide+ 7 )/8;
		    bd->bdBufferLength= bd->bdPixelsHigh* bd->bdBytesPerRow;
		    break;
		case 2:
		case 3:
		case 4:
		default:
		    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
		    SDEB(filename); fclose( f ); return -1;
		}
	    break;
	case 2:
	    switch( pch.NumberOfPlanes )
		{
		case 1:
		case 4:
		default:
		    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
		    SDEB(filename); fclose( f ); return -1;
		}
	    break;
	case 4:
	    switch( pch.NumberOfPlanes )
		{
		case 1:
		default:
		    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
		    SDEB(filename); fclose( f ); return -1;
		}
	    break;
	case 8:
	    switch( pch.NumberOfPlanes )
		{
		case 1:
		    bd->bdBitsPerSample= 8;
		    bd->bdSamplesPerPixel= 3;
		    bd->bdBitsPerPixel= 8;
		    bd->bdColorEncoding= BMcoRGB8PALETTE;
		    bd->bdColorCount= 256;
		    bd->bdRGB8Palette= (RGB8Color *)0;
		    bd->bdBytesPerRow= bd->bdPixelsWide;
		    bd->bdBufferLength= bd->bdPixelsHigh* bd->bdBytesPerRow;

		    bd->bdRGB8Palette= (RGB8Color *)
					    malloc( 256* sizeof(RGB8Color) );
		    if  ( ! bd->bdRGB8Palette )
			{ XDEB(bd->bdRGB8Palette); fclose(f); return -1; }
		    break;
		case 3:
		default:
		    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
		    SDEB(filename); fclose( f ); return -1;
		}
	    break;
	default:
	    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
	    SDEB(filename); fclose( f ); return -1;
	}

    buffer= (unsigned char *)malloc( bd->bdBufferLength+ 1 );
    if  ( ! buffer )
	{ LLDEB(bd->bdBufferLength,buffer); fclose( f ); return -1;	}

    switch( pch.NumberOfPlanes )
	{
	case 1:
	    c= 0; count= 0;

	    for ( row= 0; row < bd->bdPixelsHigh; row++ )
		{
		int	ret;

		ret= bmReadPcxRow( buffer+ row* bd->bdBytesPerRow,
					pch.BytesLinePlane, f, &c, &count );

		if  ( ret != pch.BytesLinePlane )
		    { LLDEB(ret,pch.BytesLinePlane);	}

		if  ( ferror( f ) )
		    { SDEB(filename); return -1;	}
		}
	    break;
	case 2:
	case 3:
	case 4:
	default:
	    LLDEB(pch.BitsPixelPlane,pch.NumberOfPlanes);
	    SDEB(filename); fclose( f ); return -1;
	}

    if  ( pch.BitsPixelPlane == 8 && pch.NumberOfPlanes == 1 )
	{
	if  ( fseek( f, -769, SEEK_END ) < 0 )
	    { LSDEB(-769,filename); fclose(f); return -1; }

	c= getc( f );
	if  ( c != 0xc )
	    { XSDEB(c,filename); fclose(f); return -1; }

	for ( row= 0; row < 256; row++ )
	    {
	    bd->bdRGB8Palette[row].rgb8Red= getc( f );
	    bd->bdRGB8Palette[row].rgb8Green= getc( f );
	    bd->bdRGB8Palette[row].rgb8Blue= getc( f );

	    if  ( ferror( f ) )
		{ SDEB(filename); return -1;	}
	    }
	}

    *pPrivateFormat= 0;
    *pBuffer= buffer;

    fclose( f ); return 0;
    }

/************************************************************************/
/*  Write a PCX file.							*/
/************************************************************************/

int bmWritePcxFile(	const char *			filename,
			const unsigned char *		buffer,
			const BitmapDescription *	bd,
			int				privateFormat,
			double				compressionFactor )
    { LDEB(-1); return -1; }

int bmCanWritePcxFile( const BitmapDescription *	bd,
			int				privateFormat,
			double				compressionFactor )
    { return -1; }