2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
4
// Contact: pgregory@aqsis.com
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU General Public
8
// License as published by the Free Software Foundation; either
9
// version 2 of the License, or (at your option) any later version.
11
// This library is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
// General Public License for more details.
16
// You should have received a copy of the GNU General Public
17
// License along with this library; if not, write to the Free Software
18
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
\brief Implements a pcx importer; ONLY SUPPORT RLE PALETTE PCX FILE
23
\author Michel Joron (joron@sympatico.ca)
27
// The conversion process assumes a minimal conversion
28
// the function to do convertion must be name
29
// as the name of the dll or .so on Unix
30
// The conversion must create a valid .tif file and return
31
// the filename of the new file created.
32
// Eg. this function pcx2tif() created a .tif file at the same place
33
// as the .jpg filename provided.
34
// Minimal implemantation is just doing a call or two to libjpeg provided with the
35
// standard libjpeg source codes.
36
// IN ORDER TO CONVERSION OCCURRED the dll/dso must be present in
37
// procedures directory under <AQSIS_BASE_PATH>
43
#include "../common/pixelsave.h"
46
#define __export __declspec(dllexport)
51
/* structure for PaintBrush file */
53
/* this structure needs to be byte aligned! */
54
/* Program need a directive pack(1) on UNIX!! */
64
/* this structure needs to be byte aligned! */
67
unsigned char manuf; /* don't ask */
68
unsigned char ver; /* PCX format version */
69
unsigned char rle_flag; /* 1 = run-length encoded */
70
unsigned char bits_per_pixel; /* number of bits per pixel */
71
unsigned short X1; /* upper LH pixel co-ord */
72
unsigned short Y1; /* upper LH pixel co-ord */
73
unsigned short X2; /* lower RH pixel co-ord */
74
unsigned short Y2; /* lower RH pixel co-ord */
75
unsigned short Hres; /* horizontal resolution of device */
76
unsigned short Vres; /* vertical resolution of device */
77
CMAP cmap_16[ 16 ]; /* first 16 entries of colour map - MUST be
79
unsigned char Vmode; /* ??? */
80
unsigned char nplanes; /* number of image planes */
81
unsigned short bytes_per_line; /* number of bytes per scan line */
82
unsigned char filler[ 60 ]; /* pad out to 128 bytes */
87
static void extract_pcx_colour_map( FILE * f, int size, unsigned char *r, unsigned char *g, unsigned char *b );
88
static void read_pcx_rle_line( FILE * f, unsigned char *s, int n );
89
static char *pcx_open( FILE *pcxfile, char *tiffname ) ;
90
static unsigned short swap2( unsigned short s );
92
static char tiffname[ 1024 ];
94
/* Main function to convert any pcx to tif format
95
* It used the standard standard definition of pcx/paintbrush structure and
96
* output RGB tif file for PC and Unix/
98
__export char *pcx2tif( char *in )
104
strcpy( tiffname, in );
105
if ( ( result = strstr( tiffname, ".pcx" ) ) != 0 ) strcpy( result, ".tif" );
108
if ( ( result = strstr( tiffname, ".pcx" ) ) != 0 ) strcpy( result, ".tif" );
110
if ( !result ) return result;
112
pcxfile = fopen( in, "rb" );
113
result = pcx_open( pcxfile, tiffname );
120
/* PRIVATE FUNCTIONS */
122
* pcx_open(): extract the colourmap of PCX; compute the
123
* pixels in RGB format and finally save to a tiff file
125
static char *pcx_open( FILE *pcxfile, char *tiffname )
131
unsigned char *r, *g, *b;
132
int palette_size = 256;
133
unsigned char * pixels;
137
* read the pbrush header
139
if ( fread( &pcx_hdr, sizeof( pcx_hdr ), 1, pcxfile ) != 1 )
141
fprintf( stderr, "pcx2bmp: can't read PCX header\n" );
145
* should really validate header here to make sure it is 256 RLE colour
148
#ifndef AQSIS_SYSTEM_WIN32
149
/* swap the short on Unix */
150
pcx_hdr.X1 = swap2( pcx_hdr.X1 );
151
pcx_hdr.X2 = swap2( pcx_hdr.X2 );
152
pcx_hdr.Y1 = swap2( pcx_hdr.Y1 );
153
pcx_hdr.Y2 = swap2( pcx_hdr.Y2 );
156
w = pcx_hdr.X2 - pcx_hdr.X1 + 1;
157
h = pcx_hdr.Y2 - pcx_hdr.Y1 + 1;
161
printf( "%dx%d\n", w, h );
164
if ( !( s = ( unsigned char * ) malloc( w ) ) )
167
fprintf( stderr, "pcx2bmp: not enough memory for scanline!\n" );
172
/* read the palette R,G, B values */
173
r = ( unsigned char * ) calloc( palette_size, sizeof( unsigned char ) );
174
b = ( unsigned char * ) calloc( palette_size, sizeof( unsigned char ) );
175
g = ( unsigned char * ) calloc( palette_size, sizeof( unsigned char ) );
177
extract_pcx_colour_map( pcxfile, palette_size, r, g, b );
180
* read image complete than now create the 256 colours/NOCOMPRESSION to
183
pixels = malloc( w * h * 3 );
185
/* convert each scan line */
186
for ( j = 0; j < h; j++ )
188
read_pcx_rle_line( pcxfile, s, w );
190
for ( i = 0; i < w; i++ )
192
pixels[ ( j * w * 3 ) + i * 3 + 0 ] = r[ s[ i ] ]; /* red */
193
pixels[ ( j * w * 3 ) + i * 3 + 1 ] = g[ s[ i ] ]; /* green */
194
pixels[ ( j * w * 3 ) + i * 3 + 2 ] = b[ s[ i ] ]; /* blue */
197
save_tiff( tiffname, pixels, w, h, 3, "pcx2tif" );
200
free( r ); free( g ); free( b );
205
* extract_pc_colour_map(): extract the colourmap of PCX
208
extract_pcx_colour_map(
211
unsigned char *r, unsigned char *g, unsigned char *b )
218
/* save current position in file */
221
/* position to presumed start of PCX colour map */
222
fseek( f, -3L * size, 2 );
225
/* read colour map */
226
for ( i = 0; i < size; i++ )
228
fread( &rgb, sizeof( rgb ), 1, f );
234
/* reposition to where we started */
241
* read_pcx_rle_line() : extract the bits of PCX.
245
read_pcx_rle_line( FILE * f, unsigned char *s, int n )
251
for ( i = 0; i < n; )
254
if ( ( c = getc( f ) ) == EOF )
259
if ( ( c & 0xc0 ) == 0xc0 )
263
if ( ( c = getc( f ) ) == EOF )
270
for ( j = 0; j < cnt; j++ )
281
static unsigned short swap2( unsigned short s )
284
unsigned char *in, *out;
286
out = ( unsigned char* ) & n;
287
in = ( unsigned char* ) & s;
296
int main( int argc, char *argv[] )
302
fprintf( stderr, "Usage %s: %s some.pcx", argv[ 0 ], argv[ 1 ] );
305
result = pcx2tif( argv[ 1 ] );