w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

ximtoppm.c
Go to the documentation of this file.
1 /* ximtoppm.c - read an Xim file and produce a portable pixmap
2 **
3 ** Copyright (C) 1991 by Jef Poskanzer.
4 **
5 ** Permission to use, copy, modify, and distribute this software and its
6 ** documentation for any purpose and without fee is hereby granted, provided
7 ** that the above copyright notice appear in all copies and that both that
8 ** copyright notice and this permission notice appear in supporting
9 ** documentation. This software is provided "as is" without express or
10 ** implied warranty.
11 */
12 
13 #include <string.h>
14 #include "ppm.h"
15 #include "xim.h"
16 #include "shhopt.h"
17 
18 struct cmdline_info {
19  /* All the information the user supplied in the command line,
20  in a form easy for the program to use.
21  */
22  char *input_filename;
23  char *alpha_filename;
24  int alpha_stdout;
26 
27 
28 static void
29 parse_command_line(int argc, char ** argv, struct cmdline_info *cmdline_p);
30 static int ReadXim ARGS(( FILE *in_fp, XimImage *xim ));
31 static int ReadXimHeader ARGS(( FILE *in_fp, XimImage *header ));
32 static int ReadXimImage ARGS(( FILE *in_fp, XimImage *xim ));
33 static int ReadImageChannel ARGS(( FILE *infp, byte *buf, unsigned int *bufsize, int encoded ));
34 
35 int
37  int argc;
38  char *argv[];
39  {
40  FILE *ifp, *imageout_file, *alpha_file;
41  XimImage xim;
42  pixel *pixelrow, colormap[256];
43  gray *alpharow;
44  /* The alpha channel of the row we're currently converting, in pgm fmt*/
45  register pixel *pP;
46  gray *aP;
47  int rows, cols, row, mapped;
48  register int col;
49  pixval maxval;
50 
51 
52  ppm_init( &argc, argv );
53 
55 
56  if ( cmdline.input_filename != NULL )
58  else
59  ifp = stdin;
60 
63  else if (cmdline.alpha_filename == NULL)
64  alpha_file = NULL;
65  else {
67  }
68 
69  if (cmdline.alpha_stdout)
70  imageout_file = NULL;
71  else
72  imageout_file = stdout;
73 
74 
75  if ( ! ReadXim( ifp, &xim ) )
76  pm_error( "can't read Xim file" );
77  rows = xim.height;
78  cols = xim.width;
79  if ( xim.nchannels == 1 && xim.bits_channel == 8 )
80  {
81  int i;
82 
83  mapped = 1;
84  maxval = 255;
85  for ( i = 0; i < xim.ncolors; ++i )
86  {
87  PPM_ASSIGN(
88  colormap[i], xim.colors[i].red,
89  xim.colors[i].grn, xim.colors[i].blu );
90  /* Should be colormap[xim.colors[i].pixel], but Xim is broken. */
91  }
92  }
93  else if ( xim.nchannels == 3 || xim.nchannels == 4 )
94  {
95  mapped = 0;
97  }
98  else
99  pm_error(
100  "unknown Xim file type, nchannels == %d, bits_channel == %d",
101  xim.nchannels, xim.bits_channel );
102 
103  if (imageout_file)
104  ppm_writeppminit( imageout_file, cols, rows, maxval, 0 );
105  if (alpha_file)
107 
109  alpharow = pgm_allocrow( cols );
110 
111  for ( row = 0; row < rows; ++row )
112  {
113  if ( mapped )
114  {
115  register byte *xbp;
116 
117  for ( col = 0, pP = pixelrow,
118  xbp = xim.data + row * xim.bytes_per_line;
119  col < cols;
120  ++col, ++pP, ++xbp )
121  *pP = colormap[*xbp];
122  *aP = 0;
123  }
124  else
125  {
126  register byte *xrbp, *xgbp, *xbbp;
127 
128  for ( col = 0, pP = pixelrow, aP = alpharow,
129  xrbp = xim.data + row * xim.bytes_per_line,
130  xgbp = xim.grn_data + row * xim.bytes_per_line,
131  xbbp = xim.blu_data + row * xim.bytes_per_line;
132  col < cols;
133  ++col, ++pP, ++xrbp, ++xgbp, ++xbbp ) {
134  PPM_ASSIGN( *pP, *xrbp, *xgbp, *xbbp );
135  if (xim.nchannels > 3)
136  *aP = *(xim.other + row * xim.bytes_per_line + col);
137  else
138  *aP = 0;
139  }
140  }
141  if (imageout_file)
142  ppm_writeppmrow( imageout_file, pixelrow, cols, maxval, 0 );
143  if (alpha_file)
144  pgm_writepgmrow( alpha_file, alpharow, cols, maxval, 0 );
145  }
146  pm_close( ifp );
147  if (imageout_file)
148  pm_close( imageout_file );
149  if (alpha_file)
150  pm_close( alpha_file );
151  exit( 0 );
152  }
153 
154 /* The rest is excerpted and slightly modified from the X.V11R4 version
155 ** of xim_io.c.
156 */
157 
158 /***********************************************************************
159 * File: xlib.c
160 * Author: Philip Thompson
161 * $Date: 89/11/01 10:14:23 $
162 * $Revision: 1.14 $
163 * Purpose: General xim libray of utililities
164 * Copyright (c) 1988 Philip R. Thompson
165 * Computer Resource Laboratory (CRL)
166 * Dept. of Architecture and Planning
167 * M.I.T., Rm 9-526
168 * Cambridge, MA 02139
169 * This software and its documentation may be used, copied, modified,
170 * and distributed for any purpose without fee, provided:
171 * -- The above copyright notice appears in all copies.
172 * -- This disclaimer appears in all source code copies.
173 * -- The names of M.I.T. and the CRL are not used in advertising
174 * or publicity pertaining to distribution of the software
175 * without prior specific written permission from me or CRL.
176 * I provide this software freely as a public service. It is NOT a
177 * commercial product, and therefore is not subject to an an implied
178 * warranty of merchantability or fitness for a particular purpose. I
179 * provide it as is, without warranty.
180 * This software is furnished only on the basis that any party who
181 * receives it indemnifies and holds harmless the parties who furnish
182 * it against any claims, demands, or liabilities connected with using
183 * it, furnishing it to others, or providing it to a third party.
184 *
185 * Philip R. Thompson (phils@athena.mit.edu)
186 ***********************************************************************/
187 
188 static int
189 ReadXim(in_fp, xim)
190  FILE *in_fp;
191  XimImage *xim;
192 {
193  if (!ReadXimHeader(in_fp, xim)) {
194  pm_message("can't read xim header" );
195  return(0);
196  }
197  if (!ReadXimImage(in_fp, xim)) {
198  pm_message("can't read xim data" );
199  return(0);
200  }
201  return(1);
202 }
203 
204 static int
206  FILE *in_fp;
207  XimImage *header;
208 {
209  int i;
210  char *cp;
211  XimAsciiHeader a_head;
212 
213  cp = (char *) header;
214  for (i = 0; i < sizeof(XimImage); ++i )
215  *cp++ = 0;
216  /* Read header and verify image file formats */
217  if (fread((char *)&a_head, sizeof(ImageHeader), 1, in_fp) != 1) {
218  pm_message("ReadXimHeader: unable to read file header" );
219  return(0);
220  }
221  if (atoi(a_head.header_size) != sizeof(ImageHeader)) {
222  pm_message("ReadXimHeader: header size mismatch" );
223  return(0);
224  }
225  if (atoi(a_head.file_version) != IMAGE_VERSION) {
226  pm_message("ReadXimHeader: incorrect Image_file version" );
227  return(0);
228  }
229  header->width = atoi(a_head.image_width);
230  header->height = atoi(a_head.image_height);
231  header->ncolors = atoi(a_head.num_colors);
232  header->nchannels = atoi(a_head.num_channels);
233  header->bytes_per_line = atoi(a_head.bytes_per_line);
234 /* header->npics = atoi(a_head.num_pictures);
235 */
236  header->bits_channel = atoi(a_head.bits_per_channel);
237  header->alpha_flag = atoi(a_head.alpha_channel);
238  if (strlen(a_head.author)) {
239  if (!(header->author = calloc((unsigned int)strlen(a_head.author)+1,
240  1))) {
241  pm_message("ReadXimHeader: can't calloc author string" );
242  return(0);
243  }
244  header->width = atoi(a_head.image_width);
245  strncpy(header->author, a_head.author, strlen(a_head.author));
246  }
247  if (strlen(a_head.date)) {
248  if (!(header->date =calloc((unsigned int)strlen(a_head.date)+1,1))){
249  pm_message("ReadXimHeader: can't calloc date string" );
250  return(0);
251  }
252  header->width = atoi(a_head.image_width);
253  strncpy(header->date, a_head.date, strlen(a_head.date));
254  }
255  if (strlen(a_head.program)) {
256  if (!(header->program = calloc(
257  (unsigned int)strlen(a_head.program) + 1, 1))) {
258  pm_message("ReadXimHeader: can't calloc program string" );
259  return(0);
260  }
261  header->width = atoi(a_head.image_width);
262  strncpy(header->program, a_head.program,strlen(a_head.program));
263  }
264  /* Do double checking for bakwards compatibility */
265  if (header->npics == 0)
266  header->npics = 1;
267  if (header->bits_channel == 0)
268  header->bits_channel = 8;
269  else if (header->bits_channel == 24) {
270  header->nchannels = 3;
271  header->bits_channel = 8;
272  }
273  if ((int)header->bytes_per_line == 0)
274  header->bytes_per_line =
275  (header->bits_channel == 1 && header->nchannels == 1) ?
276  (header->width + 7) / 8 :
277  header->width;
278  header->datasize =(unsigned int)header->bytes_per_line * header->height;
279  if (header->nchannels == 3 && header->bits_channel == 8)
280  header->ncolors = 0;
281  else if (header->nchannels == 1 && header->bits_channel == 8) {
282  header->colors = (Color *)calloc((unsigned int)header->ncolors,
283  sizeof(Color));
284  if (header->colors == NULL) {
285  pm_message("ReadXimHeader: can't calloc colors" );
286  return(0);
287  }
288  for (i=0; i < header->ncolors; i++) {
289  header->colors[i].red = a_head.c_map[i][0];
290  header->colors[i].grn = a_head.c_map[i][1];
291  header->colors[i].blu = a_head.c_map[i][2];
292  }
293  }
294  return(1);
295 }
296 
297 static int
298 ReadXimImage(in_fp, xim)
299  FILE *in_fp;
300  XimImage *xim;
301 {
302  if (xim->data) {
303  free((char *)xim->data);
304  xim->data = (byte *)0;
305  }
306  if (xim->grn_data) {
307  free((char *)xim->grn_data);
308  xim->grn_data = (byte *)0;
309  }
310  if (xim->blu_data) {
311  free((char *)xim->blu_data);
312  xim->blu_data = (byte *)0;
313  }
314  if (xim->other) {
315  free((char *)xim->other);
316  xim->other = (byte *)0;
317  }
318  xim->npics = 0;
319  if (!(xim->data = (byte *)calloc(xim->datasize, 1))) {
320  pm_message("ReadXimImage: can't malloc pixmap data" );
321  return(0);
322  }
323  if (!ReadImageChannel(in_fp, xim->data, &xim->datasize, 0)) {
324  pm_message("ReadXimImage: end of the images" );
325  return(0);
326  }
327  if (xim->nchannels == 3) {
328  xim->grn_data = (byte *)malloc(xim->datasize);
329  xim->blu_data = (byte *)malloc(xim->datasize);
330  if (xim->grn_data == NULL || xim->blu_data == NULL) {
331  pm_message("ReadXimImage: can't malloc rgb channel data" );
332  free((char *)xim->data);
333  if (xim->grn_data) free((char *)xim->grn_data);
334  if (xim->blu_data) free((char *)xim->blu_data);
335  xim->data = xim->grn_data = xim->blu_data = (byte*)0;
336  return(0);
337  }
338  if (!ReadImageChannel(in_fp, xim->grn_data, &xim->datasize, 0))
339  return(0);
340  if (!ReadImageChannel(in_fp, xim->blu_data, &xim->datasize, 0))
341  return(0);
342  }
343  if (xim->nchannels > 3) {
344  /* In theory, this can be any fourth channel, but the only one we
345  know about is an Alpha channel, so we'll call it that, even
346  though we process it generically.
347  */
348  if ((xim->other = (byte *)malloc(xim->datasize)) == NULL) {
349  pm_message("ReadXimImage: can't malloc alpha data" );
350  return(0);
351  }
352  if (!ReadImageChannel(in_fp, xim->other, &xim->datasize, 0))
353  return(0);
354  }
355  xim->npics = 1;
356  return(1);
357 }
358 
359 static int
361 FILE *infp;
362  byte *buf;
363  unsigned int *bufsize;
364  int encoded;
365 {
366  register int i, runlen, nbytes;
367  register unsigned int j;
368  register byte *line;
369  long marker;
370 
371  if (!encoded)
372  j = fread((char *)buf, 1, (int)*bufsize, infp);
373  else {
374  if ((line=(byte *)malloc((unsigned int)BUFSIZ)) == NULL) {
375  pm_message("ReadImageChannel: can't malloc() fread string" );
376  return(0);
377  }
378  /* Unrunlength encode data */
379  marker = ftell(infp);
380  j = 0;
381  while (((nbytes=fread((char *)line, 1, BUFSIZ, infp)) > 0) &&
382  (j < *bufsize)) {
383  for (i=0; (i < nbytes) && (j < *bufsize); i++) {
384  runlen = (int)line[i]+1;
385  i++;
386  while (runlen--)
387  buf[j++] = line[i];
388  }
389  marker += i;
390  }
391  /* return to the begining of the next image's bufffer */
392  if (fseek(infp, marker, 0) == -1) {
393  pm_message("ReadImageChannel: can't fseek to location in image buffer" );
394  return(0);
395  }
396  free((char *)line);
397  }
398  if (j != *bufsize) {
399  pm_message("unable to complete channel: %u / %u (%d%%)",
400  j, *bufsize, (int)(j*100.0 / *bufsize) );
401  *bufsize = j;
402  }
403  return(1);
404 }
405 
406 
407 
408 static void
410  struct cmdline_info *cmdline_p) {
411 /*----------------------------------------------------------------------------
412  Note that many of the strings that this function returns in the
413  *cmdline_p structure are actually in the supplied argv array. And
414  sometimes, one of these strings is actually just a suffix of an entry
415  in argv!
416 -----------------------------------------------------------------------------*/
417  optStruct *option_def = malloc(100*sizeof(optStruct));
418  /* Instructions to OptParseOptions on how to parse our options.
419  */
420  unsigned int option_def_index;
421 
422  option_def_index = 0; /* incremented by OPTENTRY */
423  OPTENTRY(0, "alphaout", OPT_STRING, &cmdline_p->alpha_filename, 0);
424 
425  /* Set the defaults */
426  cmdline_p->alpha_filename = NULL;
427 
428  pm_optParseOptions(&argc, argv, option_def, 0);
429  /* Uses and sets argc, argv, and all of *cmdline_p. */
430 
431  if (argc - 1 == 0)
432  cmdline_p->input_filename = NULL; /* he wants stdin */
433  else if (argc - 1 == 1)
434  cmdline_p->input_filename = strdup(argv[1]);
435  else
436  pm_error("Too many arguments. The only argument accepted\n"
437  "is the input file specificaton");
438 
439  if (cmdline_p->alpha_filename &&
440  strcmp(cmdline_p->alpha_filename, "-") == 0 )
441  cmdline_p->alpha_stdout = 1;
442  else
443  cmdline_p->alpha_stdout = 0;
444 }
#define strdup
Definition: Utility.h:167
cp
Definition: action.c:1035
FILE * infp
Definition: aftopl.c:21
static gray maxval
Definition: asciitopgm.c:38
#define free(a)
Definition: decNumber.cpp:310
#define fread
Definition: xxstdio.h:25
#define fseek
Definition: xxstdio.h:30
#define ftell
Definition: xxstdio.h:31
char * strncpy()
int strcmp()
Definition: coll.cpp:143
int col
Definition: gsftopk.c:443
unsigned char pixelrow[1023]
Definition: hbf2gf.c:377
unsigned nbytes
Definition: in_pcx.cpp:355
#define bufsize
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
void exit()
voidp calloc()
int atoi(const char *)
#define buf
union hdr header
Definition: pbmtomacp.c:291
#define malloc
Definition: alloca.c:91
FILE * alpha_file
Definition: mainbody.h:82
FILE * pm_openw(char *name)
Definition: libpbm1.c:624
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
int pm_bitstomaxval(int bits)
Definition: libpbm1.c:201
void pm_optParseOptions(int *argc, char *argv[], optStruct opt[], int allowNegNum)
Definition: libpbm1.c:149
static int rows
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
#define OPTENTRY(shortvalue, longvalue, typevalue, outputvalue, flagvalue)
Definition: pbmplus.h:311
#define ARGS(alist)
Definition: pbmplus.h:235
void pgm_writepgminit(FILE *file, int cols, int rows, gray maxval, int forceplain)
Definition: libpgm2.c:21
void pgm_writepgmrow(FILE *file, gray *grayrow, int cols, gray maxval, int forceplain)
Definition: libpgm2.c:125
#define pgm_allocrow(cols)
Definition: pgm.h:58
unsigned int gray
Definition: pgm.h:10
set set set set set set set macro pixldst1 abits if abits op else op endif endm macro pixldst2 abits if abits op else op endif endm macro pixldst4 abits if abits op else op endif endm macro pixldst0 abits op endm macro pixldst3 mem_operand op endm macro pixldst30 mem_operand op endm macro pixldst abits if abits elseif abits elseif abits elseif abits elseif abits pixldst0 abits else pixldst0 abits pixldst0 abits pixldst0 abits pixldst0 abits endif elseif abits else pixldst0 abits pixldst0 abits endif elseif abits else error unsupported bpp *numpix else pixst endif endm macro pixld1_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl mov asr adds SRC_WIDTH_FIXED bpl add asl else error unsupported endif endm macro pixld2_s mem_operand if mov asr add asl add asl mov asr sub UNIT_X add asl mov asr add asl add asl mov asr add UNIT_X add asl else pixld1_s mem_operand pixld1_s mem_operand endif endm macro pixld0_s mem_operand if asr adds SRC_WIDTH_FIXED bpl add asl elseif asr adds SRC_WIDTH_FIXED bpl add asl endif endm macro pixld_s_internal mem_operand if mem_operand pixld2_s mem_operand pixdeinterleave basereg elseif mem_operand elseif mem_operand elseif mem_operand elseif mem_operand pixld0_s mem_operand else pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else pixld0_s mem_operand pixld0_s mem_operand endif elseif mem_operand else error unsupported mem_operand if bpp mem_operand endif endm macro vuzp8 reg2 vuzp d d &reg2 endm macro vzip8 reg2 vzip d d &reg2 endm macro pixdeinterleave basereg basereg basereg basereg basereg endif endm macro pixinterleave basereg basereg basereg basereg basereg endif endm macro PF boost_increment endif if endif PF tst PF addne PF subne PF cmp ORIG_W if endif if endif if endif PF subge ORIG_W PF subges if endif if endif if endif endif endm macro cache_preload_simple endif if dst_r_bpp pld[DST_R, #(PREFETCH_DISTANCE_SIMPLE *dst_r_bpp/8)] endif if mask_bpp pld if[MASK, #(PREFETCH_DISTANCE_SIMPLE *mask_bpp/8)] endif endif endm macro fetch_mask_pixblock pixld mask_basereg pixblock_size MASK endm macro ensure_destination_ptr_alignment process_pixblock_tail_head if beq irp skip1(dst_w_bpp<=(lowbit *8)) &&((lowbit *8)<(pixblock_size *dst_w_bpp)) .if lowbit< 16 tst DST_R
#define pm_error
Definition: png22pnm.c:118
#define pm_close(file)
Definition: png22pnm.c:120
#define pm_message
Definition: png22pnm.c:116
void ppm_init(int *argcP, argv)
Definition: libppm1.c:21
void ppm_writeppmrow(FILE *file, pixel *pixelrow, int cols, pixval maxval, int forceplain)
Definition: libppm2.c:129
void ppm_writeppminit(FILE *file, int cols, int rows, pixval maxval, int forceplain)
Definition: libppm2.c:23
#define PPM_ASSIGN(p, red, grn, blu)
Definition: ppm.h:46
gray pixval
Definition: ppm.h:9
#define ppm_allocrow(cols)
Definition: ppm.h:72
char line[1024]
Definition: process_score.c:29
static int row
Definition: ps2pk.c:587
rle_map * colormap
Definition: rletopnm.c:57
@ OPT_STRING
Definition: shhopt.h:13
Definition: xim.h:41
byte blu
Definition: xim.h:42
byte grn
Definition: xim.h:42
byte red
Definition: xim.h:42
Definition: tga.h:5
char image_height[8]
Definition: xim.h:24
char program[16]
Definition: xim.h:34
char date[32]
Definition: xim.h:33
char image_width[8]
Definition: xim.h:23
char bytes_per_line[5]
Definition: xim.h:27
char bits_per_channel[4]
Definition: xim.h:29
char alpha_channel[4]
Definition: xim.h:30
char num_colors[8]
Definition: xim.h:25
char header_size[8]
Definition: xim.h:22
char author[48]
Definition: xim.h:32
char file_version[8]
Definition: xim.h:21
unsigned char c_map[256][3]
Definition: xim.h:36
char num_channels[3]
Definition: xim.h:26
Definition: xim.h:45
short bits_channel
Definition: xim.h:50
byte * grn_data
Definition: xim.h:53
int width
Definition: xim.h:46
Color * colors
Definition: xim.h:62
unsigned datasize
Definition: xim.h:48
short bytes_per_line
Definition: xim.h:51
byte * blu_data
Definition: xim.h:54
short nchannels
Definition: xim.h:49
short npics
Definition: xim.h:60
short ncolors
Definition: xim.h:61
int height
Definition: xim.h:47
byte * other
Definition: xim.h:55
byte * data
Definition: xim.h:52
char * alpha_filename
Definition: rletopnm.c:68
char * input_filename
Definition: jpegtopnm.c:56
int alpha_stdout
Definition: rletopnm.c:69
Definition: bdf.c:133
Definition: ppm.h:33
#define FILE
Definition: t1stdio.h:34
int j
Definition: t4ht.c:1589
*job_name strlen((char *) job_name) - 4)
return() int(((double) *(font_tbl[cur_fnt].wtbl+(int)(*(font_tbl[cur_fnt].char_wi+(int)(ch - font_tbl[cur_fnt].char_f)% 256)))/(double)(1L<< 20)) *(double) font_tbl[cur_fnt].scale)
FILE * ifp
Definition: t1asm.c:88
static int mapped
Definition: tgatoppm.c:23
#define BUFSIZ
Definition: ps.c:61
struct ImageHeader ImageHeader
#define IMAGE_VERSION
Definition: xim.h:13
struct Color Color
struct XimImage XimImage
struct cmdline_info cmdline
static int ReadXim()
static int ReadXimHeader()
static int ReadXimImage()
static void parse_command_line(int argc, char **argv, struct cmdline_info *cmdline_p)
Definition: ximtoppm.c:409
int main(int argc, argv)
Definition: ximtoppm.c:36
static int ReadImageChannel()
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269