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)  

ppmtopjxl.c
Go to the documentation of this file.
1 /* ppmtopcl.c - convert portable pixmap into PCL language for HP PaintJet and
2  * PaintJet XL colour printers
3  * AJCD 12/3/91
4  *
5  * usage:
6  * ppmtopcl [-nopack] [-gamma <n>] [-presentation] [-dark]
7  * [-diffuse] [-cluster] [-dither]
8  * [-xshift <s>] [-yshift <s>]
9  * [-xshift <s>] [-yshift <s>]
10  * [-xsize|-width|-xscale <s>] [-ysize|-height|-yscale <s>]
11  * [ppmfile]
12  *
13  */
14 
15 #include <stdio.h>
16 #include <math.h>
17 #include "ppm.h"
18 #include "ppmcmap.h"
19 
20 #define MAXCOLORS 1024
21 
22 int bitsperpixel ARGS((int v));
23 void putbits ARGS((int b, int n));
24 
25 char *usage="[-nopack] [-gamma <n>] [-presentation] [-dark]\n\
26  [-diffuse] [-cluster] [-dither]\n\
27  [-xshift <s>] [-yshift <s>]\n\
28  [-xshift <s>] [-yshift <s>]\n\
29  [-xsize|-width|-xscale <s>] [-ysize|-height|-yscale <s>]\n\
30  [ppmfile]";
31 
32 #define PCL_MAXWIDTH 2048
33 #define PCL_MAXHEIGHT 32767
34 #define PCL_MAXVAL 255
35 
36 static int nopack = 0;
37 static int dark = 0;
38 static int diffuse = 0;
39 static int dither = 0;
40 static int cluster = 0;
41 static int xsize = 0;
42 static int ysize = 0;
43 static int xshift = 0;
44 static int yshift = 0;
45 static int quality = 0;
46 static double xscale = 0.0;
47 static double yscale = 0.0;
48 static double gamma_val = 0.0;
49 
50 /* argument types */
51 #define DIM 0
52 #define REAL 1
53 #define BOOL 2
54 static struct options {
55  char *name;
56  int type;
57  char *value;
58 } options[] = {
59  {"-gamma", REAL, (char *)&gamma_val },
60  {"-presentation", BOOL, (char *)&quality },
61  {"-width", DIM, (char *)&xsize },
62  {"-xsize", DIM, (char *)&xsize },
63  {"-height", DIM, (char *)&ysize },
64  {"-ysize", DIM, (char *)&ysize },
65  {"-xscale", REAL, (char *)&xscale },
66  {"-yscale", REAL, (char *)&yscale },
67  {"-xshift", DIM, (char *)&xshift },
68  {"-yshift", DIM, (char *)&yshift },
69  {"-dark", BOOL, (char *)&dark },
70  {"-diffuse", BOOL, (char *)&diffuse },
71  {"-dither", BOOL, (char *)&dither },
72  {"-cluster", BOOL, (char *)&cluster },
73  {"-nopack", BOOL, (char *)&nopack },
74 };
75 
76 #define putword(w) (putchar(((w)>>8) & 255), putchar((w) & 255))
77 
79  int v;
80 {
81  int bpp = 0;
82  while (v > 0) { /* calculate # bits for value */
83  ++bpp;
84  v>>=1;
85  }
86  return (bpp);
87 }
88 
89 static char *inrow = NULL;
90 static char *outrow = NULL;
91 static /*signed*/ char *runcnt = NULL;
92 
93 void putbits(b, n) /* put #n bits in b out, packing into bytes; n=0 flushes bits */
94  int b, n; /* n should never be > 8 */
95 {
96  static int out = 0;
97  static int cnt = 0;
98  static int num = 0;
99  static int pack = 0;
100  if (n) {
101  int xo = 0;
102  int xc = 0;
103  if (cnt+n > 8) { /* overflowing current byte? */
104  xc = cnt + n - 8;
105  xo = (b & ~(-1 << xc)) << (8-xc);
106  n -= xc;
107  b >>= xc;
108  }
109  cnt += n;
110  out |= (b & ~(-1 << n)) << (8-cnt);
111  if (cnt >= 8) {
112  inrow[num++] = out;
113  out = xo;
114  cnt = xc;
115  }
116  } else { /* flush row */
117  int i;
118  if (cnt) {
119  inrow[num++] = out;
120  out = cnt = 0;
121  }
122  for (; num > 0 && inrow[num-1] == 0; num--); /* remove trailing zeros */
123  printf("\033*b");
124  if (num && !nopack) { /* TIFF 4.0 packbits encoding */
125  int start = 0;
126  int next;
127  runcnt[start] = 0;
128  for (i = 1; i < num; i++) {
129  if (inrow[i] == inrow[i-1]) {
130  if (runcnt[start] <= 0 && runcnt[start] > -127)
131  runcnt[start]--;
132  else
133  runcnt[start = i] = 0;
134  } else {
135  if (runcnt[start] >= 0 && runcnt[start] < 127)
136  runcnt[start]++;
137  else
138  runcnt[start = i] = 0;
139  }
140  }
141  start = 0;
142  for (i = 0; i < num; i = next) {
143  int count = runcnt[i];
144  int from = i;
145  if (count >= 0) { /* merge two-byte runs */
146  for (;;) {
147  next = i+1+runcnt[i];
148  if(next >= num || runcnt[next] < 0 ||
149  count+runcnt[next]+1 > 127)
150  break;
151  count += runcnt[next]+1;
152  i = next;
153  }
154  }
155  next = i + 1 + ((runcnt[i] < 0) ? -runcnt[i] : runcnt[i]);
156  if (next < num && count > 0 &&
157  runcnt[next] < 0 && runcnt[next] > -127) {
158  count--;
159  next--;
160  runcnt[next] = runcnt[next+1]-1;
161  }
162  outrow[start++] = count;
163  if (count >= 0) {
164  while (count-- >= 0)
165  outrow[start++] = inrow[from++];
166  } else
167  outrow[start++] = inrow[from];
168  }
169  if (start < num) {
170  num = start;
171  if (!pack) {
172  printf("2m");
173  pack = 1;
174  }
175  } else {
176  if (pack) {
177  printf("0m");
178  pack = 0;
179  }
180  }
181  }
182  printf("%dW", num);
183  for (i = 0; i < num; i++)
184  putchar(pack ? outrow[i] : inrow[i]);
185  num = 0; /* new row */
186  }
187 }
188 
189 int
191  int argc;
192  char *argv[];
193 {
194  FILE *ifd;
195  register pixel **pixels, *pixrow;
196  register int row, col, bpp, i;
197  int rows, cols;
198  pixval maxval;
199  int bpr, bpg, bpb;
200  int render;
201  int colours, pclindex;
202  colorhist_vector chv;
204 
205  ppm_init( &argc, argv );
206 
207  while (argc > 1 && argv[1][0] == '-') {
208  char *c;
209  for (i = 0; i < sizeof(options)/sizeof(struct options); i++) {
210  if (pm_keymatch(argv[1], options[i].name,
211  min(strlen(argv[1]), strlen(options[i].name)))) {
212  switch (options[i].type) {
213  case DIM:
214  if (++argv, --argc == 1)
215  pm_usage(usage);
216  for (c = argv[1]; isdigit(*c); c++);
217  if (c[0] == 'p' && c[1] == 't') /* points */
218  *(int *)(options[i].value) = atoi(argv[1])*10;
219  else if (c[0] == 'd' && c[1] == 'p') /* decipoints */
220  *(int *)(options[i].value) = atoi(argv[1]);
221  else if (c[0] == 'i' && c[1] == 'n') /* inches */
222  *(int *)(options[i].value) = atoi(argv[1])*720;
223  else if (c[0] == 'c' && c[1] == 'm') /* centimetres */
224  *(int *)(options[i].value) = atoi(argv[1])*283.46457;
225  else if (!c[0]) /* dots */
226  *(int *)(options[i].value) = atoi(argv[1])*4;
227  else
228  pm_error("illegal unit of measure %s", c);
229  break;
230  case REAL:
231  if (++argv, --argc == 1)
232  pm_usage(usage);
233  *(double *)(options[i].value) = atof(argv[1]);
234  break;
235  case BOOL:
236  *(int *)(options[i].value) = 1;
237  break;
238  }
239  break;
240  }
241  }
242  if (i >= sizeof(options)/sizeof(struct options))
243  pm_usage(usage);
244  argv++; argc--;
245  }
246  if (argc > 2)
247  pm_usage(usage);
248  else if (argc == 2)
249  ifd = pm_openr(argv[1]);
250  else
251  ifd = stdin ;
252 
253  /* validate arguments */
254  if (diffuse+cluster+dither > 1)
255  pm_error("only one of -diffuse, -dither and -cluster may be used");
256  render = diffuse ? 4 : dither ? 3 : cluster ? 7 : 0;
257 
258  if (xsize != 0.0 && xscale != 0.0)
259  pm_error("only one of -xsize and -xscale may be used");
260 
261  if (ysize != 0.0 && yscale != 0.0)
262  pm_error("only one of -ysize and -yscale may be used");
263 
264  pixels = ppm_readppm( ifd, &cols, &rows, &maxval );
265  pm_close( ifd );
266 
267  /* limit checks */
268  if (cols > PCL_MAXWIDTH || rows > PCL_MAXHEIGHT)
269  pm_error("image too large; reduce with ppmscale");
270  if (maxval > PCL_MAXVAL)
271  pm_error("colour range too large; reduce with ppmcscale");
272 
273  /* Figure out the colormap. */
274  fprintf( stderr, "(Computing colormap..." ); fflush( stderr );
275  chv = ppm_computecolorhist( pixels, cols, rows, MAXCOLORS, &colours );
276  if ( chv == (colorhist_vector) 0 )
277  pm_error("too many colours; reduce with ppmquant", 0,0,0,0,0 );
278  fprintf( stderr, " Done. %d colors found.)\n", colours );
279 
280  /* And make a hash table for fast lookup. */
281  cht = ppm_colorhisttocolorhash( chv, colours );
282 
283  /* work out colour downloading mode */
284  pclindex = bitsperpixel(colours);
285  if (pclindex > 8) /* can't use indexed mode */
286  pclindex = 0;
287  else
288  switch (pclindex) { /* round up to 1,2,4,8 */
289  case 0: /* direct mode (no palette) */
290  bpp = bitsperpixel(maxval); /* bits per pixel */
291  bpg = bpp; bpb = bpp;
292  bpp = (bpp*3+7)>>3; /* bytes per pixel now */
293  bpr = (bpp<<3)-bpg-bpb;
294  bpp *= cols; /* bytes per row now */
295  break;
296  case 5: pclindex++;
297  case 6: pclindex++;
298  case 3: case 7: pclindex++;
299  default:
300  bpp = 8/pclindex;
301  bpp = (cols+bpp-1)/bpp; /* bytes per row */
302  }
303 
304  if ((inrow = (char *)malloc((unsigned)bpp)) == NULL ||
305  (outrow = (char *)malloc((unsigned)bpp*2)) == NULL ||
306  (runcnt = (/*signed*/ char *)malloc((unsigned)bpp)) == NULL)
307  pm_error("can't allocate space for row", 0,0,0,0,0);
308 
309  /* set up image details */
310  if (xscale != 0.0)
311  xsize = cols * xscale * 4;
312  if (yscale != 0.0)
313  ysize = rows * yscale * 4;
314 
315 #ifdef DEBUG
316  fprintf(stderr, "dark =%d\n", dark);
317  fprintf(stderr, "diffuse =%d\n", diffuse);
318  fprintf(stderr, "dither =%d\n", dither);
319  fprintf(stderr, "cluster =%d\n", cluster);
320  fprintf(stderr, "quality =%d\n", quality);
321  fprintf(stderr, "xsize =%d\n", xsize);
322  fprintf(stderr, "ysize =%d\n", ysize);
323  fprintf(stderr, "xshift =%d\n", xshift);
324  fprintf(stderr, "yshift =%d\n", yshift);
325  fprintf(stderr, "xscale =%lf\n", xscale);
326  fprintf(stderr, "yscale =%lf\n", yscale);
327  fprintf(stderr, "gamma =%lf\n", gamma_val);
328  fprintf(stderr, "pclindex =%d\n", pclindex);
329  fprintf(stderr, "nopack =%d\n", nopack);
330 #endif
331 
332  /* write PCL header */
333 #if 0
334  printf("\033&l26A"); /* paper size */
335 #endif
336  printf("\033*r%ds%dT", cols, rows); /* source width, height */
337  if (xshift != 0 || yshift != 0)
338  printf("\033&a%+dh%+dV", xshift, yshift); /* xshift, yshift */
339  if (quality)
340  printf("\033*o%dQ", quality); /* print quality */
341  printf("\033*t");
342  if (xsize == 0 && ysize == 0)
343  printf("180r"); /* resolution */
344  else { /* destination width, height */
345  if (xsize != 0)
346  printf("%dh", xsize);
347  if (ysize != 0)
348  printf("%dv", ysize);
349  }
350  if (gamma_val != 0)
351  printf("%.3fi", gamma_val); /* gamma correction */
352  if (dark)
353  printf("%dk", dark); /* scaling algorithms */
354  printf("%dJ", render); /* rendering algorithm */
355  printf("\033*v18W"); /* configure image data */
356  putchar(0); /* relative colours */
357  putchar(pclindex ? 1 : 3); /* index/direct pixel mode */
358  putchar(pclindex); /* ignored in direct pixel mode */
359  if (pclindex) {
360  putchar(0);
361  putchar(0);
362  putchar(0);
363  } else {
364  putchar(bpr); /* bits per red */
365  putchar(bpg); /* bits per green */
366  putchar(bpb); /* bits per blue */
367  }
368  putword(maxval); /* max red reference */
369  putword(maxval); /* max green reference */
370  putword(maxval); /* max blue reference */
371  putword(0); /* min red reference */
372  putword(0); /* min green reference */
373  putword(0); /* min blue reference */
374  if (pclindex) { /* set palette */
375  for (i = 0; i < colours; i++) {
376  int r, g, b;
377  r = PPM_GETR( chv[i].color );
378  g = PPM_GETG( chv[i].color );
379  b = PPM_GETB( chv[i].color );
380  if (i == 0)
381  printf("\033*v");
382  if (r)
383  printf("%da", r);
384  if (g)
385  printf("%db", g);
386  if (b)
387  printf("%dc", b);
388  if (i == colours-1)
389  printf("%dI", i); /* assign colour index */
390  else
391  printf("%di", i); /* assign colour index */
392  }
393  }
394  ppm_freecolorhist( chv );
395 
396  /* start raster graphics at CAP */
397  printf("\033*r%dA", (xsize != 0 || ysize != 0) ? 3 : 1);
398 
399  for (row = 0; row < rows; row++) {
400  if (pclindex) { /* indexed colour mode */
401  int out, cnt;
402  out = cnt = 0;
403  for (col = 0, pixrow=pixels[row]; col < cols; col++, pixrow++) {
404  putbits(ppm_lookupcolor( cht, pixrow ), pclindex);
405  }
406  putbits(0, 0); /* flush row */
407  } else { /* direct colour mode */
408  for (col = 0, pixrow=pixels[row]; col < cols; col++, pixrow++) {
409  putbits(PPM_GETR( *pixrow ), bpr);
410  putbits(PPM_GETG( *pixrow ), bpg);
411  putbits(PPM_GETB( *pixrow ), bpb); /* don't need to flush */
412  }
413  putbits(0, 0); /* flush row */
414  }
415  }
416  printf("\033*rC"); /* end raster graphics */
417  exit(0);
418 }
#define type(a)
Definition: aptex-macros.h:171
#define count(a)
Definition: aptex-macros.h:781
#define name
#define next(a)
Definition: aptex-macros.h:924
static gray maxval
Definition: asciitopgm.c:38
#define n
Definition: t4ht.c:1290
#define b
Definition: jpegint.h:372
int v
Definition: dviconv.c:10
#define fflush
Definition: xxstdio.h:24
int printf()
int pixels
Definition: dvipng.h:106
#define c(n)
Definition: gpos-common.c:150
int col
Definition: gsftopk.c:443
FILE * out
Definition: hbf2gf.c:286
#define NULL
Definition: ftobjs.h:61
small capitals from c petite p scientific i
Definition: afcover.h:80
void exit()
int atoi(const char *)
double atof(const char *)
int num
Definition: disdvi.c:621
@ render
Definition: spc_dvips.c:728
#define fprintf
Definition: mendex.h:64
#define malloc
Definition: alloca.c:91
#define isdigit(c)
Definition: snprintf.c:177
void pm_usage(char *usage)
Definition: libpbm1.c:343
FILE * pm_openr(char *name)
Definition: libpbm1.c:600
static int rows
Definition: pbmclean.c:15
static int cols
Definition: pbmmask.c:21
#define ARGS(alist)
Definition: pbmplus.h:235
#define min(a, b)
Definition: pbmplus.h:223
static int pack
Definition: pbmtolj.c:35
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
static int cnt
Definition: pkout.c:166
#define pm_error
Definition: png22pnm.c:118
#define pm_keymatch(stra, strb, _x)
Definition: png22pnm.c:121
#define pm_close(file)
Definition: png22pnm.c:120
void ppm_init(int *argcP, argv)
Definition: libppm1.c:21
pixel ** ppm_readppm(FILE *file, int *colsP, int *rowsP, pixval *maxvalP)
Definition: libppm1.c:178
int ppm_lookupcolor(colorhash_table cht, pixel *colorP)
Definition: libppm3.c:226
void ppm_freecolorhist(colorhist_vector chv)
Definition: libppm3.c:242
colorhist_vector ppm_computecolorhist(pixel **pixels, int cols, int rows, int maxcolors, int *colorsP)
Definition: libppm3.c:28
colorhash_table ppm_colorhisttocolorhash(colorhist_vector chv, int colors)
Definition: libppm3.c:193
#define PPM_GETR(p)
Definition: ppm.h:36
#define PPM_GETG(p)
Definition: ppm.h:37
#define PPM_GETB(p)
Definition: ppm.h:38
gray pixval
Definition: ppm.h:9
int g
Definition: ppmqvga.c:68
int r
Definition: ppmqvga.c:68
static colorhash_table cht
Definition: ppmtoacad.c:34
static pixel * pixrow
Definition: ppmtoilbm.c:128
static double yscale
Definition: ppmtopjxl.c:47
static char * runcnt
Definition: ppmtopjxl.c:91
#define putword(w)
Definition: ppmtopjxl.c:76
#define PCL_MAXWIDTH
Definition: ppmtopjxl.c:32
#define PCL_MAXHEIGHT
Definition: ppmtopjxl.c:33
#define PCL_MAXVAL
Definition: ppmtopjxl.c:34
static struct options options[]
static char * inrow
Definition: ppmtopjxl.c:89
void putbits()
#define REAL
Definition: ppmtopjxl.c:52
static int yshift
Definition: ppmtopjxl.c:44
#define MAXCOLORS
Definition: ppmtopjxl.c:20
static int cluster
Definition: ppmtopjxl.c:40
char * usage
Definition: ppmtopjxl.c:25
static char * outrow
Definition: ppmtopjxl.c:90
static int diffuse
Definition: ppmtopjxl.c:38
static int dither
Definition: ppmtopjxl.c:39
#define DIM
Definition: ppmtopjxl.c:51
static int ysize
Definition: ppmtopjxl.c:42
static double xscale
Definition: ppmtopjxl.c:46
static double gamma_val
Definition: ppmtopjxl.c:48
static int quality
Definition: ppmtopjxl.c:45
static int dark
Definition: ppmtopjxl.c:37
static int xshift
Definition: ppmtopjxl.c:43
static int xsize
Definition: ppmtopjxl.c:41
#define BOOL
Definition: ppmtopjxl.c:53
int bitsperpixel()
static int nopack
Definition: ppmtopjxl.c:36
int main(int argc, argv)
Definition: ppmtopjxl.c:190
static int row
Definition: ps2pk.c:587
Definition: pdfdev.c:706
char * name
Definition: ppmtopjxl.c:55
int type
Definition: ppmtopjxl.c:56
char * value
Definition: ppmtopjxl.c:57
Definition: ppm.h:33
#define FILE
Definition: t1stdio.h:34
*job_name strlen((char *) job_name) - 4)
Definition: obx.h:51
@ start
Definition: preamble.c:52
#define argv
Definition: xmain.c:270
#define argc
Definition: xmain.c:269