src/image.c
/* [<][>][^][v][top][bottom][index][help] */
FUNCTIONS
This source file includes following functions.
- VF_ImageOut_PBMAscii
- VF_ImageOut_PGMAscii
- PGM_BIT
- VF_ImageOut_PGMRaw
- PGM_BIT
- VF_ImageOut_EPS
- EPS_SIZE
- EPS_PIX
- EPS_PUT_PIX
- VF_ImageOut_ASCIIArt
- VF_ImageOut_ASCIIArtV
- culc_margins
1 /* image.c
2 * --- Print bitmap in several graphics formats
3 *
4 * by Hirotsugu Kakugawa (h.kakugawa@computer.org)
5 *
6 */
7 /*
8 * Copyright (C) 1996, 1997, 1998 Hirotsugu Kakugawa.
9 * All rights reserved.
10 *
11 * This file is part of the VFlib Library. This library is free
12 * software; you can redistribute it and/or modify it under the terms of
13 * the GNU Library General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. This library is distributed in the hope
16 * that it will be useful, but WITHOUT ANY WARRANTY; without even the
17 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 * PURPOSE. See the GNU Library General Public License for more details.
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23 #include <stdio.h>
24 #include <stdlib.h>
25 #ifdef HAVE_UNISTD_H
26 # include <unistd.h>
27 #endif
28 #include <math.h>
29 #include "config.h"
30 #include "VFlib-3_6.h"
31 #include "VFsys.h"
32 #include "bitmap.h"
33 #include "consts.h"
34
35
36 Private void culc_margins(int bm_w, int bm_h,
37 int image_width, int image_height,
38 int position_x, int position_y,
39 int *margin_l, int *margin_r,
40 int *margin_t, int *margin_b);
41
42 Private unsigned char bits[] = {
43 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
44
45
46
47 /**
48 ** VF_ImageOut_PBMAscii()
49 ** --- PBM ASCII
50 **/
51
52 Public int
53 VF_ImageOut_PBMAscii(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
54 int image_width, int image_height,
55 int position_x, int position_y,
56 int margin_l, int margin_r, int margin_t, int margin_b,
57 int reverse, int shrink,
58 char *prog, char *title)
59 {
60 int x, y, n;
61 unsigned char w, *p;
62 char pix0, pix1;
63 #define PBM_ASCII_BITS_PER_LINE 25
64
65 culc_margins(bm->bbx_width, bm->bbx_height,
66 image_width, image_height, position_x, position_y,
67 &margin_l, &margin_r, &margin_t, &margin_b);
68
69 if (shrink <= 0)
70 shrink = 1;
71
72 if (reverse == 0){
73 pix0 = '0';
74 pix1 = '1';
75 } else {
76 pix0 = '1';
77 pix1 = '0';
78 }
79
80 fprintf(fp, "P1\n");
81 fprintf(fp, "%d %d\n",
82 margin_l + bm->bbx_width + margin_r,
83 margin_t + bm->bbx_height + margin_b);
84 if (prog == NULL)
85 prog = "VFlib";
86 fprintf(fp, "# Created by %s\n", prog);
87 fprintf(fp, "# %s\n", title);
88
89 n = 0;
90 p = bm->bitmap;
91
92 /* top margin */
93 for (y = 0; y < margin_t; y++){
94 for (x = 0; x < margin_l + bm->bbx_width + margin_r; x++){
95 fprintf(fp, "%c ", pix0);
96 if (++n >= PBM_ASCII_BITS_PER_LINE){
97 n = 0;
98 fprintf(fp, "\n");
99 }
100 }
101 }
102
103 for (y = 0; y < bm->bbx_height; y++){
104 /* left margin */
105 for (x = 0; x < margin_l; x++)
106 fprintf(fp, "%c ", pix0);
107 /* bitmap */
108 for (x = 0; x < bm->bbx_width; x++){
109 w = p[x/8];
110 fprintf(fp, "%c ", ((w&bits[x%8])==0)?pix0:pix1);
111 if (++n >= PBM_ASCII_BITS_PER_LINE){
112 n = 0;
113 fprintf(fp, "\n");
114 }
115 }
116 /* right margin */
117 for (x = 0; x < margin_r; x++){
118 fprintf(fp, "%c ", pix0);
119 if (++n >= PBM_ASCII_BITS_PER_LINE){
120 n = 0;
121 fprintf(fp, "\n");
122 }
123 }
124
125 p = p + bm->raster;
126 }
127
128 /* bottom margin */
129 for (y = 0; y < margin_b; y++){
130 for (x = 0; x < margin_l + bm->bbx_width + margin_r; x++){
131 fprintf(fp, "%c ", pix0);
132 if (++n >= PBM_ASCII_BITS_PER_LINE){
133 n = 0;
134 fprintf(fp, "\n");
135 }
136 }
137 }
138 fprintf(fp, "\n");
139
140 return 0;
141 }
142
143
144 /**
145 ** VF_ImageOut_PGMAscii()
146 ** --- PGM ASCII
147 **/
148 Public int
149 VF_ImageOut_PGMAscii(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
150 int image_width, int image_height,
151 int position_x, int position_y,
152 int margin_l, int margin_r, int margin_t, int margin_b,
153 int reverse, int shrink,
154 char *prog, char *title)
155 {
156 int x, y, s, n;
157 unsigned char *p;
158 int *buff;
159 int w, h, y2, max_val;
160 #define PGM_ASCII_BITS_PER_LINE 15
161 #define PGM_BIT(x) (reverse==1) ? ((255*(x))/max_val) \
/* [<][>][^][v][top][bottom][index][help] */
162 : (255 - ((255*(x))/max_val))
163
164 if (shrink <= 0)
165 shrink = 1;
166 max_val = shrink * shrink;
167 w = (bm->bbx_width + shrink - 1) / shrink;
168 h = (bm->bbx_height + shrink - 1) / shrink;
169 if ((buff = calloc(w, sizeof(int))) == NULL){
170 vf_error = VF_ERR_NO_MEMORY;
171 return -1;
172 }
173
174 culc_margins(w, h, image_width, image_height, position_x, position_y,
175 &margin_l, &margin_r, &margin_t, &margin_b);
176
177 fprintf(fp, "P2\n");
178 if (prog == NULL)
179 prog = "VFlib";
180 fprintf(fp, "# Created by %s\n", prog);
181 fprintf(fp, "# %s\n", title);
182 fprintf(fp, "%d %d\n", margin_l + w + margin_l, margin_t + h + margin_b);
183 fprintf(fp, "%d\n", 255);
184
185 n = 0;
186
187 /* top margin */
188 for (y = 0; y < margin_t; y++){
189 for (x = 0; x < margin_l + w + margin_r; x++){
190 fprintf(fp, "%d ", PGM_BIT(0));
191 if (++n >= PGM_ASCII_BITS_PER_LINE){
192 n = 0;
193 fprintf(fp, "\n");
194 }
195 }
196 }
197
198 for (y = 0; y < h; y++){
199 /* left margin */
200 for (x = 0; x < margin_l; x++)
201 fprintf(fp, "%d ", PGM_BIT(0));
202 /* make graymap */
203 for (x = 0; x < w; x++)
204 buff[x] = 0;
205 p = &bm->bitmap[y * shrink * bm->raster];
206 s = ((bm->bbx_height/shrink) == y) ? (bm->bbx_height % shrink) : shrink;
207 for (y2 = 0; y2 < s; y2++){
208 for (x = 0; x < bm->bbx_width; x++){
209 if ((p[x/8] & bits[x%8]) != 0)
210 buff[x/shrink] += 1;
211 }
212 p += bm->raster;
213 }
214 /* output graymap */
215 for (x = 0; x < w; x++){
216 fprintf(fp, "%d ", PGM_BIT(buff[x]));
217 if (++n >= PGM_ASCII_BITS_PER_LINE){
218 n = 0;
219 fprintf(fp, "\n");
220 }
221 }
222 /* right margin */
223 for (x = 0; x < margin_r; x++){
224 fprintf(fp, "%d ", PGM_BIT(0));
225 if (++n >= PGM_ASCII_BITS_PER_LINE){
226 n = 0;
227 fprintf(fp, "\n");
228 }
229 }
230 }
231
232 /* bottom margin */
233 for (y = 0; y < margin_b; y++){
234 for (x = 0; x < margin_l + w + margin_r; x++){
235 fprintf(fp, "%d ", PGM_BIT(0));
236 if (++n >= PGM_ASCII_BITS_PER_LINE){
237 n = 0;
238 fprintf(fp, "\n");
239 }
240 }
241 }
242 fprintf(fp, "\n");
243
244 vf_free(buff);
245 return 0;
246 }
247
248
249 /**
250 ** VF_ImageOut_PGMRaw()
251 ** --- PGM RAW
252 **/
253 Public int
254 VF_ImageOut_PGMRaw(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
255 int image_width, int image_height,
256 int position_x, int position_y,
257 int margin_l, int margin_r, int margin_t, int margin_b,
258 int reverse, int shrink,
259 char *prog, char *title)
260 {
261 int x, y;
262 unsigned char *p;
263 int *buff;
264 int w, h, s, y2, max_val;
265 #define PGM_BIT(x) (reverse==1) ? ((255*(x))/max_val) \
/* [<][>][^][v][top][bottom][index][help] */
266 : (255 - ((255*(x))/max_val))
267
268 if (shrink <= 0)
269 shrink = 1;
270 max_val = shrink * shrink;
271 w = (bm->bbx_width + shrink - 1) / shrink;
272 h = (bm->bbx_height + shrink - 1) / shrink;
273 if ((buff = calloc(w, sizeof(int))) == NULL){
274 vf_error = VF_ERR_NO_MEMORY;
275 return -1;
276 }
277
278 culc_margins(w, h, image_width, image_height, position_x, position_y,
279 &margin_l, &margin_r, &margin_t, &margin_b);
280
281 fprintf(fp, "P5\n");
282 fprintf(fp, "# Created by %s\n", prog);
283 fprintf(fp, "# %s\n", title);
284 fprintf(fp, "%d %d\n",
285 margin_l + w + margin_r, margin_t + h + margin_b);
286 fprintf(fp, "%d\n", 255);
287
288 /* top margin */
289 for (y = 0; y < margin_t; y++){
290 for (x = 0; x < margin_l + w + margin_r; x++){
291 fprintf(fp, "%c", (unsigned char)PGM_BIT(0));
292 }
293 }
294
295 for (y = 0; y < h; y++){
296 /* left margin */
297 for (x = 0; x < margin_l; x++)
298 fprintf(fp, "%c", (unsigned char)PGM_BIT(0));
299 /* make graymap */
300 for (x = 0; x < w; x++)
301 buff[x] = 0;
302 p = &bm->bitmap[y * shrink * bm->raster];
303 s = ((bm->bbx_height/shrink) == y) ? (bm->bbx_height % shrink) : shrink;
304 for (y2 = 0; y2 < s; y2++){
305 for (x = 0; x < bm->bbx_width; x++){
306 if ((p[x/8] & bits[x%8]) != 0)
307 buff[x/shrink] += 1;
308 }
309 p += bm->raster;
310 }
311 /* output graymap */
312 for (x = 0; x < w; x++)
313 fprintf(fp, "%c", (unsigned char)PGM_BIT(buff[x]));
314 /* right margin */
315 for (x = 0; x < margin_r; x++)
316 fprintf(fp, "%c", (unsigned char)PGM_BIT(0));
317 }
318
319 /* bottom margin */
320 for (y = 0; y < margin_b; y++){
321 for (x = 0; x < margin_l + w + margin_r; x++)
322 fprintf(fp, "%c", (unsigned char)PGM_BIT(0));
323 }
324
325 vf_free(buff);
326 return 0;
327 }
328
329
330 /**
331 ** VF_ImageOut_EPS()
332 ** --- EPS
333 **/
334 Public int
335 VF_ImageOut_EPS(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
336 int image_width, int image_height,
337 int position_x, int position_y,
338 int margin_l, int margin_r, int margin_t, int margin_b,
339 int reverse, int shrink,
340 char *prog, char *title,
341 double ptsize, int pixsize)
342 {
343 int x, y;
344 unsigned char *p;
345 int *buff;
346 int w, h, y2, s, max_val;
347 int eps_w, eps_h, bbxx, bbxy, n;
348 #define EPS_SIZE(s) \
/* [<][>][^][v][top][bottom][index][help] */
349 ((ptsize <= 0) ? (12.0*(s)*shrink/16.0) \
350 : (ptsize*(s)*shrink/(double)pixsize))
351 #define EPS_PIX(x) \
/* [<][>][^][v][top][bottom][index][help] */
352 ((reverse==1) ? ((255*(x))/max_val) : (255 - ((255*(x))/max_val)))
353 #define EPS_PUT_PIX(b) \
/* [<][>][^][v][top][bottom][index][help] */
354 { fprintf(fp,"%02x",b); if (++n > 32){ fprintf(fp,"\n"); n=0;} }
355
356 if (shrink < 0)
357 shrink = 1;
358 max_val = shrink * shrink;
359 w = (bm->bbx_width + shrink - 1) / shrink;
360 h = (bm->bbx_height + shrink - 1) / shrink;
361 if ((buff = calloc(w, sizeof(int))) == NULL){
362 vf_error = VF_ERR_NO_MEMORY;
363 return -1;
364 }
365
366 culc_margins(w, h, image_width, image_height, position_x, position_y,
367 &margin_l, &margin_r, &margin_t, &margin_b);
368
369 eps_w = margin_l + w + margin_r;
370 eps_h = margin_t + h + margin_b;
371
372 bbxx = 72;
373 bbxy = 792 - EPS_SIZE(h);
374
375 fprintf(fp, "%%!PS-Adobe-2.0 EPSF-2.0\n");
376 fprintf(fp, "%%%%Creator: %s\n", prog);
377 fprintf(fp, "%%%%Title: %s\n", title);
378 fprintf(fp, "%%%%Pages: 1\n");
379 fprintf(fp, "%%%%BoundingBox: %.3f %.3f %.3f %.3f\n",
380 (double)bbxx, (double)bbxy,
381 bbxx+EPS_SIZE(eps_w), bbxy+EPS_SIZE(eps_h));
382 fprintf(fp, "%%%%EndComments\n");
383 fprintf(fp, "/readstr {\n");
384 fprintf(fp, " currentfile exch readhexstring pop\n");
385 fprintf(fp, "} bind def\n");
386 fprintf(fp, "/picstr %d string def\n", eps_w);
387 fprintf(fp, "%%%%EndProlog\n");
388 fprintf(fp, "%%%%Page: 1 1\n");
389 fprintf(fp, "gsave\n");
390 fprintf(fp, "%d %d translate\n", bbxx, bbxy);
391 fprintf(fp, "%.3f %.3f scale\n", EPS_SIZE(eps_w), EPS_SIZE(eps_h));
392 fprintf(fp, "%d %d 8\n", eps_w, eps_h);
393 fprintf(fp, "[ %d 0 0 -%d 0 %d ]\n", eps_w, eps_h, eps_h);
394 fprintf(fp, "{ picstr readstr }\n");
395 fprintf(fp, "bind image\n");
396
397 n = 0;
398 /* top margin */
399 for (y = 0; y < margin_t; y++){
400 for (x = 0; x < margin_l + w + margin_r; x++)
401 EPS_PUT_PIX(EPS_PIX(0));
402 }
403 p = bm->bitmap;
404 for (y = 0; y < h; y++){
405 /* left margin */
406 for (x = 0; x < margin_l; x++)
407 EPS_PUT_PIX(EPS_PIX(0));
408 /* make graymap */
409 for (x = 0; x < w; x++)
410 buff[x] = 0;
411 s = ((bm->bbx_height/shrink) == y) ? (bm->bbx_height % shrink) : shrink;
412 for (y2 = 0; y2 < s; y2++){
413 for (x = 0; x < bm->bbx_width; x++){
414 if ((p[x/8] & bits[x%8]) != 0)
415 buff[x/shrink] += 1;
416 }
417 p += bm->raster;
418 }
419 /* output graymap */
420 for (x = 0; x < w; x++)
421 EPS_PUT_PIX(EPS_PIX(buff[x]));
422 /* right margin */
423 for (x = 0; x < margin_r; x++)
424 EPS_PUT_PIX(EPS_PIX(0));
425 }
426 /* bottom margin */
427 for (y = 0; y < margin_b; y++){
428 for (x = 0; x < margin_l + w + margin_r; x++)
429 EPS_PUT_PIX(EPS_PIX(0));
430 }
431
432 if (n != 0)
433 fprintf(fp, "\n");
434 fprintf(fp, "grestore\n");
435 fprintf(fp, "showpage\n");
436 fprintf(fp, "%%%%Trailer\n");
437
438 vf_free(buff);
439 return 0;
440 }
441
442 /**
443 ** VF_ImageOut_ASCIIArt()
444 ** --- ASCII Art (Horizontal)
445 **/
446 Public int
447 VF_ImageOut_ASCIIArt(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
448 int image_width, int image_height,
449 int position_x, int position_y,
450 int margin_l, int margin_r, int margin_t, int margin_b,
451 int reverse, int shrink)
452 {
453 int x, y, w, h, y2, s, max_val, j;
454 int *buff;
455 unsigned char *p;
456 char *pixs;
457 char *chspec = " ****";
458 int speclen;
459
460 if (shrink < 0)
461 shrink = 1;
462 max_val = shrink * shrink;
463 w = (bm->bbx_width + shrink - 1) / shrink;
464 h = (bm->bbx_height + shrink - 1) / shrink;
465 if ((buff = (int*)calloc(w, sizeof(int))) == NULL){
466 vf_error = VF_ERR_NO_MEMORY;
467 return -1;
468 }
469
470 culc_margins(w, h, image_width, image_height, position_x, position_y,
471 &margin_l, &margin_r, &margin_t, &margin_b);
472
473 if ((pixs = (char*)malloc(max_val+1)) == NULL){
474 vf_error = VF_ERR_NO_MEMORY;
475 return -1;
476 }
477
478 speclen = strlen(chspec);
479 if (reverse == 0){
480 for (j = 0; j <= max_val; j++)
481 pixs[j] = chspec[(speclen * j) / (max_val+1)];
482 pixs[0] = chspec[0];
483 } else {
484 for (j = 0; j <= max_val; j++)
485 pixs[max_val - j] = chspec[(speclen * j) / (max_val+1)];
486 pixs[max_val] = chspec[0];
487 }
488
489 p = bm->bitmap;
490
491 /* top margin */
492 for (y = 0; y < margin_t; y++){
493 for (x = 0; x < margin_l + w + margin_r; x++)
494 fprintf(fp, "%c", pixs[0]);
495 fprintf(fp, "\n");
496 }
497
498 for (y = 0; y < h; y++){
499 /* left margin */
500 for (x = 0; x < margin_l; x++)
501 fprintf(fp, "%c", pixs[0]);
502 /* make graymap */
503 for (x = 0; x < w; x++)
504 buff[x] = 0;
505 p = &bm->bitmap[y * shrink * bm->raster];
506 s = ((bm->bbx_height/shrink) == y) ? (bm->bbx_height % shrink) : shrink;
507 for (y2 = 0; y2 < s; y2++){
508 for (x = 0; x < bm->bbx_width; x++){
509 if ((p[x/8] & bits[x%8]) != 0)
510 buff[x/shrink] += 1;
511 }
512 p += bm->raster;
513 }
514 /* output bitmap */
515 for (x = 0; x < w; x++)
516 fprintf(fp, "%c", pixs[buff[x]]);
517 /* right margin */
518 for (x = 0; x < margin_r; x++)
519 fprintf(fp, "%c", pixs[0]);
520 fprintf(fp, "\n");
521 }
522
523 /* bottom margin */
524 for (y = 0; y < margin_b; y++){
525 for (x = 0; x < margin_l + w + margin_r; x++)
526 fprintf(fp, "%c", pixs[0]);
527 fprintf(fp, "\n");
528 }
529
530 vf_free(buff);
531 vf_free(pixs);
532 return 0;
533 }
534
535
536 /**
537 ** VF_ImageOut_ASCIIArtV()
538 ** --- ASCII Art (Vertical)
539 **/
540 Public int
541 VF_ImageOut_ASCIIArtV(VF_BITMAP bm, FILE *fp,
/* [<][>][^][v][top][bottom][index][help] */
542 int image_width, int image_height,
543 int position_x, int position_y,
544 int margin_l, int margin_r, int margin_t, int margin_b,
545 int reverse, int shrink)
546 {
547 int x, y, w, h, x2, s, max_val, j;
548 int *buff;
549 unsigned char *p;
550 char *pixs;
551 char *chspec = " ****";
552 int speclen;
553
554 if (shrink < 0)
555 shrink = 1;
556 max_val = shrink * shrink;
557 w = (bm->bbx_width + shrink - 1) / shrink;
558 h = (bm->bbx_height + shrink - 1) / shrink;
559 if ((buff = (int*)calloc(h, sizeof(int))) == NULL){
560 vf_error = VF_ERR_NO_MEMORY;
561 return -1;
562 }
563
564 culc_margins(w, h, image_width, image_height, position_x, position_y,
565 &margin_l, &margin_r, &margin_t, &margin_b);
566
567 if ((pixs = (char*)malloc(max_val+1)) == NULL){
568 vf_error = VF_ERR_NO_MEMORY;
569 return -1;
570 }
571 speclen = strlen(chspec);
572 if (reverse == 0){
573 for (j = 0; j <= max_val; j++)
574 pixs[j] = chspec[(speclen * j) / (max_val+1)];
575 pixs[0] = chspec[0];
576 } else {
577 for (j = 0; j <= max_val; j++)
578 pixs[max_val - j] = chspec[(speclen * j) / (max_val+1)];
579 pixs[max_val] = chspec[0];
580 }
581
582 /* left margin */
583 for (x = 0; x < margin_l; x++){
584 for (y = margin_b-1; y >= 0; --y)
585 fprintf(fp, "%c", pixs[0]);
586 for (y = h-1; y >= 0; --y)
587 fprintf(fp, "%c", pixs[0]);
588 for (y = margin_t-1; y >= 0; --y)
589 fprintf(fp, "%c", pixs[0]);
590 fprintf(fp, "\n");
591 }
592
593 /* body */
594 for (x = 0; x < w; x++){
595 /* bottom margin */
596 for (y = margin_b; y > 0; --y)
597 fprintf(fp, "%c", pixs[0]);
598 /* make graymap */
599 for (y = 0; y < h; y++)
600 buff[y] = 0;
601 p = bm->bitmap;
602 for (y = 0; y < bm->bbx_height; y++){
603 s = ((bm->bbx_width/shrink) == x) ? (bm->bbx_width % shrink) : shrink;
604 for (x2 = 0; x2 < s; x2++){
605 if ((p[(x * shrink + x2)/8] & bits[(x * shrink + x2)%8]) != 0)
606 buff[y/shrink] += 1;
607 }
608 p += bm->raster;
609 }
610 /* output bitmap */
611 for (y = h-1; y >= 0; --y)
612 fprintf(fp, "%c", pixs[buff[y]]);
613 /* top margin */
614 for (y = margin_t; y > 0; --y)
615 fprintf(fp, "%c", pixs[0]);
616 fprintf(fp, "\n");
617 }
618
619 /* right margin */
620 for (x = 0; x < margin_r; x++){
621 for (y = margin_b-1; y >= 0; --y)
622 fprintf(fp, "%c", pixs[0]);
623 for (y = h-1; y >= 0; --y)
624 fprintf(fp, "%c", pixs[0]);
625 for (y = margin_t-1; y >= 0; --y)
626 fprintf(fp, "%c", pixs[0]);
627 fprintf(fp, "\n");
628 }
629
630 vf_free(buff);
631 vf_free(pixs);
632 return 0;
633 }
634
635
636
637
638 Private void
639 culc_margins(int bm_w, int bm_h,
/* [<][>][^][v][top][bottom][index][help] */
640 int image_width, int image_height,
641 int position_x, int position_y,
642 int *margin_l, int *margin_r, int *margin_t, int *margin_b)
643 {
644 /* top and bottom margins */
645 if (image_height < 0){
646 image_height = bm_h;
647 if (*margin_t >= 0)
648 image_height += *margin_t;
649 else
650 *margin_t = 0;
651 if (*margin_b >= 0)
652 image_height += *margin_b;
653 else
654 *margin_b = 0;
655 } else /* image_height >= 0 */ {
656 switch (position_y){
657 default:
658 case VF_IMAGEOUT_POSITION_NONE:
659 case VF_IMAGEOUT_POSITION_TOP:
660 if (*margin_t < 0)
661 *margin_t = 0;
662 *margin_b = image_height - *margin_t - bm_h;
663 break;
664 case VF_IMAGEOUT_POSITION_BOTTOM:
665 if (*margin_b < 0)
666 *margin_b = image_height - bm_h;
667 *margin_t = image_height - bm_h - *margin_b;
668 break;
669 case VF_IMAGEOUT_POSITION_CENTER:
670 *margin_t = (image_height - bm_h) / 2;
671 *margin_b = image_height - *margin_t - bm_h;
672 break;
673 }
674 }
675 if (*margin_t < 0){
676 fprintf(stderr, "VFlib Warning: page height is small. (>=%d)\n",
677 *margin_b + bm_h);
678 *margin_t = 0;
679 }
680 if (*margin_b < 0){
681 fprintf(stderr, "VFlib Warning: page height is small. (>=%d)\n",
682 *margin_t + bm_h);
683 *margin_b = 0;
684 }
685
686 /* left and right margins */
687 if (image_width < 0){
688 image_width = bm_w;
689 if (*margin_l >= 0)
690 image_width += *margin_l;
691 else
692 *margin_l = 0;
693 if (*margin_r >= 0)
694 image_width += *margin_r;
695 else
696 *margin_r = 0;
697 } else /* image_width >= 0 */ {
698 switch (position_x){
699 default:
700 case VF_IMAGEOUT_POSITION_NONE:
701 case VF_IMAGEOUT_POSITION_LEFT:
702 if (*margin_l < 0)
703 *margin_l = 0;
704 *margin_r = image_width - *margin_l - bm_w;
705 break;
706 case VF_IMAGEOUT_POSITION_RIGHT:
707 if (*margin_r < 0)
708 *margin_r = image_width - bm_w;
709 *margin_l = image_width - bm_w - *margin_l;
710 break;
711 case VF_IMAGEOUT_POSITION_CENTER:
712 *margin_l = (image_width - bm_w) / 2;
713 *margin_r = image_width - *margin_l - bm_w;
714 break;
715 }
716 }
717 if (*margin_l < 0){
718 fprintf(stderr, "VFlib Warning: page width is small. (>=%d)\n",
719 *margin_r + bm_w);
720 *margin_l = 0;
721 }
722 if (*margin_r < 0){
723 fprintf(stderr, "VFlib Warning: page width is small. (>=%d)\n",
724 *margin_l + bm_w);
725 *margin_r = 0;
726 }
727
728 }
729
730
731 /*EOF*/