File: write-ps.c

package info (click to toggle)
fbi 2.07-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,172 kB
  • ctags: 2,303
  • sloc: ansic: 17,896; sh: 60; makefile: 35; perl: 12
file content (475 lines) | stat: -rw-r--r-- 11,976 bytes parent folder | download | duplicates (4)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <inttypes.h>

#include <X11/Xlib.h>
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/Text.h>
#include <Xm/SelectioB.h>
#include <Xm/DrawingA.h>
#include <Xm/RowColumn.h>
#include <Xm/PushB.h>
#include <Xm/Scale.h>
#include <Xm/Label.h>
#include "RegEdit.h"

#include "ida.h"
#include "readers.h"
#include "writers.h"
#include "viewer.h"

struct PAPER {
    char  *name;
    int   width,height;
};

static struct PAPER formats[] = {
    {
	name:   "A4",
	width:  595,
	height: 842,
    },{
	name:   "Letter",
	width:  612,
	height: 792,
    },{
	/* EOF */
    }
};

static const char *header =
"%%!PS-Adobe-2.0 EPSF-2.0\n"
"%%%%Creator: ida " VERSION " (http://bytesex.org/ida/)\n"
"%%%%Pages: 1\n"
"%%%%BoundingBox: %d %d %d %d\n"
"%%%%DocumentFonts: \n"
"%%%%EndComments\n"
"%%%%EndProlog\n"
"\n"
"%%%%Page: 1 1"
"\n"
"/origstate save def\n"
"20 dict begin\n";

static const char *footer =
"\n"
"showpage\n"
"end\n"
"origstate restore\n"
"%%%%Trailer\n";

/* taken from xwd2ps, ftp://ftp.x.org/R5contrib/xwd2ps.tar.Z */
static const char *ColorImage =
"% define 'colorimage' if it isn't defined\n"
"%   ('colortogray' and 'mergeprocs' come from xwd2ps\n"
"%     via xgrab)\n"
"/colorimage where   % do we know about 'colorimage'?\n"
"  { pop }           % yes: pop off the 'dict' returned\n"
"  {                 % no:  define one\n"
"    /colortogray {  % define an RGB->I function\n"
"      /rgbdata exch store    % call input 'rgbdata'\n"
"      rgbdata length 3 idiv\n"
"      /npixls exch store\n"
"      /rgbindx 0 store\n"
"      0 1 npixls 1 sub {\n"
"        grays exch\n"
"        rgbdata rgbindx       get 20 mul    % Red\n"
"        rgbdata rgbindx 1 add get 32 mul    % Green\n"
"        rgbdata rgbindx 2 add get 12 mul    % Blue\n"
"        add add 64 idiv      % I = .5G + .31R + .18B\n"
"        put\n"
"        /rgbindx rgbindx 3 add store\n"
"      } for\n"
"      grays 0 npixls getinterval\n"
"    } bind def\n"
"\n"
"    % Utility procedure for colorimage operator.\n"
"    % This procedure takes two procedures off the\n"
"    % stack and merges them into a single procedure.\n"
"\n"
"    /mergeprocs { % def\n"
"      dup length\n"
"      3 -1 roll\n"
"      dup\n"
"      length\n"
"      dup\n"
"      5 1 roll\n"
"      3 -1 roll\n"
"      add\n"
"      array cvx\n"
"      dup\n"
"      3 -1 roll\n"
"      0 exch\n"
"      putinterval\n"
"      dup\n"
"      4 2 roll\n"
"      putinterval\n"
"    } bind def\n"
"\n"
"    /colorimage { % def\n"
"      pop pop     % remove 'false 3' operands\n"
"      {colortogray} mergeprocs\n"
"      image\n"
"    } bind def\n"
"  } ifelse          % end of 'false' case\n"
"\n";


/* ---------------------------------------------------------------------- */
/* save                                                                   */

#define PORTRAIT    0
#define LANDSCAPE   1

#define DRAW_SIZE   200
#define DRAW_SCALE  6
#define DSCALED(x)  (((x)+DRAW_SCALE/2)/DRAW_SCALE)

static struct ps_options {
    Widget shell,draw,scale,geo;
    int xscale,yscale;
    GC gc;
    int lastx,lasty;

    int format;
    int ori;
    int scaling;

    int iwidth,iheight,ires;
    int pwidth,pheight;
    int mwidth,mheight;
    int width,height,xcenter,ycenter;
} ps;

static void
ps_draw(Widget widget, XtPointer client_data, XtPointer calldata)
{
    XmDrawingAreaCallbackStruct *cb = calldata;
    XExposeEvent *e;
    XmString str;
    char buf[128];
    int x,y,w,h;
    
    if (!(cb->reason == XmCR_EXPOSE))
	return;
    e = (XExposeEvent*)cb->event;
    if (e->count)
	return;

    if (!ps.gc)
	ps.gc = XCreateGC(dpy,XtWindow(app_shell),0,NULL);

    w = DSCALED(ps.pwidth);
    h = DSCALED(ps.pheight);
    x = (DRAW_SIZE-w) / 2;
    y = (DRAW_SIZE-h) / 2;
    XDrawRectangle(dpy,XtWindow(widget),ps.gc, x,y,w,h);

    w = DSCALED(ps.width);
    h = DSCALED(ps.height);
    x += DSCALED(ps.xcenter - ps.width/2);
    y += DSCALED(ps.ycenter - ps.height/2);
    XFillRectangle(dpy,XtWindow(widget),ps.gc, x,y,w,h);

    sprintf(buf,"%d dpi",ps.iwidth * 72 / ps.width);
    str = XmStringGenerate(buf, NULL, XmMULTIBYTE_TEXT, NULL);
    XtVaSetValues(ps.geo,XmNlabelString,str,NULL);
    XmStringFree(str);
}

static void
ps_defaults(void)
{
    /* max size, keep aspect ratio */
    if (ps.ori == PORTRAIT) {
	ps.pwidth  = formats[ps.format].width;
	ps.pheight = formats[ps.format].height;
    } else {
	ps.pheight = formats[ps.format].width;
	ps.pwidth  = formats[ps.format].height;
    }

    if (ps.iwidth  * ps.pheight > ps.iheight * ps.pwidth) {
	ps.mwidth  = ps.pwidth;
	ps.mheight = ps.iheight * ps.mwidth / ps.iwidth;
    } else {
	ps.mheight = ps.pheight;
	ps.mwidth  = ps.iwidth * ps.mheight / ps.iheight;
    }
    ps.scaling = 0;
    if (ps.ires) {
	/* Use image resolution to calculate default scaling factor.
	 * The image will be printed in original size if it fits into
	 * one page */
	ps.scaling = ps.iwidth * 72 * 1000 / ps.mwidth / ps.ires;
    }
    if (ps.scaling > 1000 || ps.scaling < 1) {
	/* default: maxpect with some border */
	ps.scaling = 1000;
	while (ps.mwidth  * ps.scaling / 1000 + 50 > ps.mwidth ||
	       ps.mheight * ps.scaling / 1000 + 50 > ps.mheight)
	    ps.scaling--;
    }
    XmScaleSetValue(ps.scale,ps.scaling);
    ps.width  = ps.mwidth * ps.scaling  / 1000;
    ps.height = ps.mheight * ps.scaling / 1000;
    ps.xcenter = ps.pwidth/2;
    ps.ycenter = ps.pheight/2;

    if (XtWindow(ps.draw))
	XClearArea(XtDisplay(ps.draw), XtWindow(ps.draw),
		   0,0,0,0, True);
}

static void
ps_ranges(void)
{
    if (ps.width == 0)
	ps.width = 1;
    if (ps.height == 0)
	ps.height = 1;
    if (ps.xcenter - ps.width/2 < 0)
	ps.xcenter = ps.width/2;
    if (ps.xcenter + ps.width/2 > ps.pwidth)
	ps.xcenter = ps.pwidth - ps.width/2;
    if (ps.ycenter - ps.height/2 < 0)
	ps.ycenter = ps.height/2;
    if (ps.ycenter + ps.height/2 > ps.pheight)
	ps.ycenter = ps.pheight - ps.height/2;
}

static void
ps_mouse(Widget widget, XtPointer client_data,
	 XEvent *ev, Boolean *cont)
{
    switch (ev->type) {
    case ButtonPress:
    {
	XButtonEvent *e = (XButtonEvent*)ev;

	ps.lastx = e->x;
	ps.lasty = e->y;
	break;
    }
    case MotionNotify:
    {
	XMotionEvent *e = (XMotionEvent*)ev;

	if (e->state & Button1Mask) {
	    ps.xcenter += (e->x - ps.lastx) * DRAW_SCALE;
	    ps.ycenter += (e->y - ps.lasty) * DRAW_SCALE;
	    ps.lastx = e->x;
	    ps.lasty = e->y;
	}
	break;
    default:
	return;
    }
    }
    ps_ranges();
    if (XtWindow(ps.draw))
	XClearArea(XtDisplay(ps.draw), XtWindow(ps.draw),
		   0,0,0,0, True);
}

static void
ps_paper_cb(Widget widget, XtPointer clientdata, XtPointer call_data)
{
    ps.format = (intptr_t)clientdata;
    ps_defaults();
}

static void
ps_ori_cb(Widget widget, XtPointer clientdata, XtPointer call_data)
{
    ps.ori = (intptr_t)clientdata;
    ps_defaults();
}

static void
ps_scaling_cb(Widget widget, XtPointer clientdata, XtPointer call_data)
{
    XmScaleCallbackStruct *cd = call_data;

    ps.scaling = cd->value;
    ps.width  = ps.mwidth  * ps.scaling / 1000;
    ps.height = ps.mheight * ps.scaling / 1000;
    ps_ranges();
    if (XtWindow(ps.draw))
	XClearArea(XtDisplay(ps.draw), XtWindow(ps.draw),
		   0,0,0,0, True);
}

static void
ps_button_cb(Widget widget, XtPointer clientdata, XtPointer call_data)
{
    XmSelectionBoxCallbackStruct *cb = call_data;

    if (XmCR_OK == cb->reason) {
	do_save_print();
    }
    XtUnmanageChild(ps.shell);
}

static int
ps_conf(Widget parent, struct ida_image *img)
{
    Widget rc,menu,push,opt;
    Arg args[2];
    intptr_t i;
    
    if (!ps.shell) {
	/* build dialog */
	ps.shell = XmCreatePromptDialog(parent,"ps",NULL,0);
	XmdRegisterEditres(XtParent(ps.shell));
	XtUnmanageChild(XmSelectionBoxGetChild(ps.shell,XmDIALOG_HELP_BUTTON));
	XtUnmanageChild(XmSelectionBoxGetChild(ps.shell,XmDIALOG_SELECTION_LABEL));
	XtUnmanageChild(XmSelectionBoxGetChild(ps.shell,XmDIALOG_TEXT));
	XtAddCallback(ps.shell,XmNokCallback,ps_button_cb,NULL);
	XtAddCallback(ps.shell,XmNcancelCallback,ps_button_cb,NULL);

	rc = XtVaCreateManagedWidget("rc1",xmRowColumnWidgetClass,
				     ps.shell,NULL);
	ps.draw = XtVaCreateManagedWidget("draw",xmDrawingAreaWidgetClass,rc,
					  XtNwidth,DRAW_SIZE,
					  XtNheight,DRAW_SIZE,
					  NULL);
	XtAddCallback(ps.draw,XmNexposeCallback,ps_draw,NULL);
	XtAddEventHandler(ps.draw,
			  ButtonPressMask   |
			  ButtonReleaseMask |
			  ButtonMotionMask,
			  False,ps_mouse,NULL);
	rc = XtVaCreateManagedWidget("rc2",xmRowColumnWidgetClass,
				     rc,NULL);

	/* paper */
	menu = XmCreatePulldownMenu(rc,"paperM",NULL,0);
	XtSetArg(args[0],XmNsubMenuId,menu);
	opt = XmCreateOptionMenu(rc,"paper",args,1);
	XtManageChild(opt);
	for (i = 0; formats[i].name != NULL; i++) {
	    push = XtVaCreateManagedWidget(formats[i].name,xmPushButtonWidgetClass,menu,NULL);
	    XtAddCallback(push,XmNactivateCallback,ps_paper_cb,(XtPointer)i);
	}

	/* orientation */
	menu = XmCreatePulldownMenu(rc,"oriM",NULL,0);
	XtSetArg(args[0],XmNsubMenuId,menu);
	opt = XmCreateOptionMenu(rc,"ori",args,1);
	XtManageChild(opt);
	push = XtVaCreateManagedWidget("portrait",xmPushButtonWidgetClass,
				       menu,NULL);
	XtAddCallback(push,XmNactivateCallback,ps_ori_cb,(XtPointer)PORTRAIT);
	push = XtVaCreateManagedWidget("landscape",xmPushButtonWidgetClass,
				       menu,NULL);
	XtAddCallback(push,XmNactivateCallback,ps_ori_cb,(XtPointer)LANDSCAPE);

	ps.scale = XtVaCreateManagedWidget("scale",xmScaleWidgetClass,rc,NULL);
	XtAddCallback(ps.scale,XmNdragCallback,ps_scaling_cb,NULL);
	XtAddCallback(ps.scale,XmNvalueChangedCallback,ps_scaling_cb,NULL);
	
	/* output */
	ps.geo = XtVaCreateManagedWidget("geo",xmLabelWidgetClass,rc,NULL);
    }

    ps.iwidth  = img->i.width;
    ps.iheight = img->i.height;
    ps.ires    = 0;
    if (ida->img.i.dpi)
	ps.ires = ida->img.i.dpi;
    ps_defaults();
    
    XtManageChild(ps.shell);
    return 0;
}

static int
ps_write(FILE *fp, struct ida_image *img)
{
    unsigned int width,height,xoff,yoff;
    unsigned int iwidth,iheight;
    unsigned int x,y;
    unsigned char *p;

    if (ps.ori == PORTRAIT) {
	iwidth  = img->i.width;
	iheight = img->i.height;
	width   = ps.width;
	height  = ps.height;
	xoff    = ps.xcenter - ps.width/2;
	yoff    = (ps.pheight - ps.ycenter) - ps.height/2;
    } else{
	iwidth  = img->i.height;
	iheight = img->i.width;
	width   = ps.height;
	height  = ps.width;
	xoff    = ps.ycenter - ps.height/2;
	yoff    = ps.xcenter - ps.width/2;
    }

    /* PS header */
    fprintf(fp,header, /* includes bbox */
	    xoff,yoff,xoff+width,yoff+height);
    fprintf(fp,"\n"
	    "/pix %d string def\n"
	    "/grays %d string def\n"
	    "/npixls 0 def\n"
	    "/rgbindx 0 def\n"
	    "\n",
	    img->i.width*3,img->i.width);
    fwrite(ColorImage,strlen(ColorImage),1,fp);

    fprintf(fp,"%d %d translate\n",xoff,yoff);
    fprintf(fp,"%d %d scale\n",width,height);

    fprintf(fp,"\n"
	    "%d %d 8\n"
	    "[%d 0 0 -%d 0 %d]\n"
	    "{currentfile pix readhexstring pop}\n"
	    "false 3 colorimage\n",
	    iwidth,iheight,iwidth,iheight,iheight);

    /* image data + ps footer */
    if (ps.ori == PORTRAIT) {
	p = img->data;
	for (y = 0; y < img->i.height; y++) {
	    for (x = 0; x < img->i.width; x++) {
		if (0 == (x % 10))
		    fprintf(fp,"\n");
		fprintf(fp,"%02x%02x%02x ",p[0],p[1],p[2]);
		p += 3;
	    }
	    fprintf(fp,"\n");
	}
    } else {
	for (x = img->i.width-1; x != -1; x--) {
	    p = img->data + 3*x;
	    for (y = 0; y < img->i.height; y++) {
		if (0 == (y % 10))
		    fprintf(fp,"\n");
		fprintf(fp,"%02x%02x%02x ",p[0],p[1],p[2]);
		p += img->i.width*3;
	    }
	    fprintf(fp,"\n");
	}
    }
    fprintf(fp,footer);
    return 0;
}

struct ida_writer ps_writer = {
    label:  "PostScript",
    ext:    { "ps", "eps", NULL},
    write:  ps_write,
    conf:   ps_conf,
};

static void __init init_wr(void)
{
    write_register(&ps_writer);
}