~ubuntu-branches/ubuntu/warty/aqsis/warty

« back to all changes in this revision

Viewing changes to plugins/pcx2tif/pcx2tif.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:25:04 UTC
  • Revision ID: james.westby@ubuntu.com-20040824072504-zf993vnevvisdsvb
Tags: upstream-0.9.1
ImportĀ upstreamĀ versionĀ 0.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Aqsis
 
2
// Copyright ļæ½ 1997 - 2001, Paul C. Gregory
 
3
//
 
4
// Contact: pgregory@aqsis.com
 
5
//
 
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.
 
10
//
 
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.
 
15
//
 
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
 
19
 
 
20
 
 
21
/** \file
 
22
                \brief Implements a pcx importer; ONLY SUPPORT RLE PALETTE PCX FILE
 
23
                \author Michel Joron (joron@sympatico.ca)
 
24
*/
 
25
 
 
26
 
 
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>
 
38
 
 
39
#include <stdio.h>
 
40
#include <string.h>
 
41
#include <malloc.h>
 
42
 
 
43
#include "../common/pixelsave.h"
 
44
 
 
45
#ifdef  WIN32
 
46
#define __export        __declspec(dllexport)
 
47
#else   // WIN32
 
48
#define __export
 
49
#endif  // WIN32
 
50
 
 
51
/* structure for PaintBrush file */
 
52
 
 
53
/* this structure needs to be byte aligned! */
 
54
/* Program need a directive pack(1) on UNIX!! */
 
55
typedef struct
 
56
{
 
57
        unsigned char r;
 
58
        unsigned char g;
 
59
        unsigned char b;
 
60
}
 
61
CMAP;
 
62
 
 
63
 
 
64
/* this structure needs to be byte aligned! */
 
65
typedef struct
 
66
{
 
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
 
78
                                         * 48 bytes */
 
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 */
 
83
}
 
84
PCX_HDR;
 
85
 
 
86
 
 
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 );
 
91
 
 
92
static char tiffname[ 1024 ];
 
93
 
 
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/
 
97
 */
 
98
__export char *pcx2tif( char *in )
 
99
{
 
100
        FILE * pcxfile;
 
101
        char *result = NULL;
 
102
 
 
103
 
 
104
        strcpy( tiffname, in );
 
105
        if ( ( result = strstr( tiffname, ".pcx" ) ) != 0 ) strcpy( result, ".tif" );
 
106
        if ( !result )
 
107
        {
 
108
                if ( ( result = strstr( tiffname, ".pcx" ) ) != 0 ) strcpy( result, ".tif" );
 
109
        }
 
110
        if ( !result ) return result;
 
111
 
 
112
        pcxfile = fopen( in, "rb" );
 
113
        result = pcx_open( pcxfile, tiffname );
 
114
        fclose( pcxfile );
 
115
 
 
116
 
 
117
        return result;
 
118
}
 
119
 
 
120
/* PRIVATE FUNCTIONS */
 
121
/*
 
122
 * pcx_open(): extract the colourmap of PCX; compute the 
 
123
 *   pixels in RGB format and finally save to a tiff file
 
124
 */
 
125
static char *pcx_open( FILE *pcxfile, char *tiffname )
 
126
{
 
127
        PCX_HDR pcx_hdr;
 
128
        register int i, j;
 
129
        unsigned short h, w;
 
130
        unsigned char *s;
 
131
        unsigned char *r, *g, *b;
 
132
        int palette_size = 256;
 
133
        unsigned char * pixels;
 
134
 
 
135
 
 
136
        /*
 
137
         * read the pbrush header
 
138
        */
 
139
        if ( fread( &pcx_hdr, sizeof( pcx_hdr ), 1, pcxfile ) != 1 )
 
140
        {
 
141
                fprintf( stderr, "pcx2bmp: can't read PCX header\n" );
 
142
                return NULL;
 
143
        }
 
144
        /*
 
145
         * should really validate header here to make sure it is 256 RLE colour
 
146
         * mapped !
 
147
         */
 
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 );
 
154
#endif
 
155
 
 
156
        w = pcx_hdr.X2 - pcx_hdr.X1 + 1;
 
157
        h = pcx_hdr.Y2 - pcx_hdr.Y1 + 1;
 
158
 
 
159
 
 
160
#ifdef DEBUG
 
161
        printf( "%dx%d\n", w, h );
 
162
#endif
 
163
 
 
164
        if ( !( s = ( unsigned char * ) malloc( w ) ) )
 
165
        {
 
166
#ifdef DEBUG
 
167
                fprintf( stderr, "pcx2bmp: not enough memory for scanline!\n" );
 
168
#endif
 
169
                return NULL;
 
170
        }
 
171
 
 
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 ) );
 
176
 
 
177
        extract_pcx_colour_map( pcxfile, palette_size, r, g, b );
 
178
 
 
179
        /*
 
180
         * read image complete than now create the 256 colours/NOCOMPRESSION to
 
181
        * tiff file
 
182
         */
 
183
        pixels = malloc( w * h * 3 );
 
184
 
 
185
        /* convert each scan line */
 
186
        for ( j = 0; j < h; j++ )
 
187
        {
 
188
                read_pcx_rle_line( pcxfile, s, w );
 
189
 
 
190
                for ( i = 0; i < w; i++ )
 
191
                {
 
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 */
 
195
                }
 
196
        }
 
197
        save_tiff( tiffname, pixels, w, h, 3, "pcx2tif" );
 
198
 
 
199
        free( pixels );
 
200
        free( r ); free( g ); free( b );
 
201
        return tiffname;
 
202
}
 
203
 
 
204
/*
 
205
 * extract_pc_colour_map(): extract the colourmap of PCX 
 
206
 */
 
207
static void
 
208
extract_pcx_colour_map(
 
209
    FILE * f,
 
210
    int size,
 
211
    unsigned char *r, unsigned char *g, unsigned char *b )
 
212
{
 
213
        long posn;
 
214
        int i;
 
215
        CMAP rgb;
 
216
 
 
217
 
 
218
        /* save current position in file */
 
219
        posn = ftell( f );
 
220
 
 
221
        /* position to presumed start of PCX colour map */
 
222
        fseek( f, -3L * size, 2 );
 
223
 
 
224
 
 
225
        /* read colour map */
 
226
        for ( i = 0; i < size; i++ )
 
227
        {
 
228
                fread( &rgb, sizeof( rgb ), 1, f );
 
229
                r[ i ] = rgb.r;
 
230
                g[ i ] = rgb.g;
 
231
                b[ i ] = rgb.b;
 
232
        }
 
233
 
 
234
        /* reposition to where we started */
 
235
        fseek( f, posn, 0 );
 
236
 
 
237
}
 
238
 
 
239
 
 
240
/*
 
241
 * read_pcx_rle_line() : extract the bits of PCX.
 
242
 */
 
243
static
 
244
void
 
245
read_pcx_rle_line( FILE * f, unsigned char *s, int n )
 
246
{
 
247
        int i, j, cnt;
 
248
        int c;
 
249
 
 
250
 
 
251
        for ( i = 0; i < n; )
 
252
        {
 
253
 
 
254
                if ( ( c = getc( f ) ) == EOF )
 
255
                {
 
256
                        if ( feof( f ) )
 
257
                                return ;
 
258
                }
 
259
                if ( ( c & 0xc0 ) == 0xc0 )
 
260
                {
 
261
 
 
262
                        cnt = c & 0x3f;
 
263
                        if ( ( c = getc( f ) ) == EOF )
 
264
                        {
 
265
                                if ( feof( f ) )
 
266
                                        return ;
 
267
                        }
 
268
                        if ( i + cnt > n )
 
269
                                cnt = n - i;
 
270
                        for ( j = 0; j < cnt; j++ )
 
271
                                s[ i + j ] = c;
 
272
                        i += cnt;
 
273
                }
 
274
                else
 
275
                {
 
276
                        s[ i++ ] = c;
 
277
                }
 
278
        }
 
279
}
 
280
 
 
281
static unsigned short swap2( unsigned short s )
 
282
{
 
283
        unsigned short n;
 
284
        unsigned char *in, *out;
 
285
 
 
286
        out = ( unsigned char* ) & n;
 
287
        in = ( unsigned char* ) & s;
 
288
 
 
289
        out[ 0 ] = in[ 1 ];
 
290
        out[ 1 ] = in[ 0 ];
 
291
        return n;
 
292
 
 
293
}
 
294
 
 
295
#ifdef MAIN
 
296
int main( int argc, char *argv[] )
 
297
{
 
298
        char * result;
 
299
 
 
300
        if ( argc != 2 )
 
301
        {
 
302
                fprintf( stderr, "Usage %s: %s some.pcx", argv[ 0 ], argv[ 1 ] );
 
303
                exit( 2 );
 
304
        }
 
305
        result = pcx2tif( argv[ 1 ] );
 
306
        if ( result )
 
307
        {
 
308
                puts( result );
 
309
        }
 
310
        return 1;
 
311
}
 
312
#endif