~ubuntu-branches/ubuntu/trusty/xfig/trusty

« back to all changes in this revision

Viewing changes to f_readxpm.c

  • Committer: Bazaar Package Importer
  • Author(s): Roland Rosenfeld
  • Date: 2002-03-22 10:39:49 UTC
  • Revision ID: james.westby@ubuntu.com-20020322103949-63082hoxulq6n7u1
Tags: upstream-3.2.3.d-rel
ImportĀ upstreamĀ versionĀ 3.2.3.d-rel

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * FIG : Facility for Interactive Generation of figures
 
3
 * Copyright (c) 1989-2000 by Brian V. Smith
 
4
 *
 
5
 * Any party obtaining a copy of these files is granted, free of charge, a
 
6
 * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
 
7
 * nonexclusive right and license to deal in this software and
 
8
 * documentation files (the "Software"), including without limitation the
 
9
 * rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
 * and/or sell copies of the Software, and to permit persons who receive
 
11
 * copies from any such party to do so, with the only requirement being
 
12
 * that this copyright notice remain intact.
 
13
 *
 
14
 */
 
15
 
 
16
#include "fig.h"
 
17
#include "resources.h"
 
18
#include "object.h"
 
19
#include "w_color.h"
 
20
#include "w_msgpanel.h"
 
21
#include "w_setup.h"
 
22
 
 
23
/* attempt to read a XPM (color pixmap) file */
 
24
/* the filename is passed since XpmReadFileToXpmImage needs a name 
 
25
   instead of a FILE pointer.  This is fine because it handles compressed files */
 
26
 
 
27
/* return codes:  PicSuccess (1) : success
 
28
                  FileInvalid (-2) : invalid file
 
29
*/
 
30
 
 
31
int
 
32
read_xpm(filename,filetype,pic)
 
33
    char           *filename;
 
34
    int             filetype;
 
35
    F_pic          *pic;
 
36
{
 
37
    int             status;
 
38
    XpmImage        image;
 
39
    int             i;
 
40
    char           *c;
 
41
    XColor          exact_def;
 
42
 
 
43
    /* make scale factor smaller for metric */
 
44
    float scale = (appres.INCHES ?
 
45
                        (float)PIX_PER_INCH :
 
46
                        2.54*PIX_PER_CM)/(float)DISPLAY_PIX_PER_INCH;
 
47
 
 
48
    status = XpmReadFileToXpmImage(filename, &image, NULL);
 
49
    /* if out of colors, try switching colormaps and reading again */
 
50
    if (status == XpmColorFailed) {
 
51
        if (!switch_colormap())
 
52
            return PicSuccess;
 
53
        status = XpmReadFileToXpmImage(pic->file, &image, NULL);
 
54
    }
 
55
    if (status == XpmSuccess) {
 
56
        /* now look up the colors in the image and put them in the pic colormap */
 
57
        for (i=0; i<image.ncolors; i++) {
 
58
            c = (image.colorTable + i)->c_color;
 
59
            if (c == NULL || *c == '\0') {      /* use white for null color */
 
60
                c = "white";
 
61
                file_msg("white used for *NULL color");
 
62
            }
 
63
            if (XParseColor(tool_d, tool_cm, c, &exact_def) == 0) {
 
64
                file_msg("Error parsing color %s",c);
 
65
                exact_def.red = exact_def.green = exact_def.blue = 65535;
 
66
            }
 
67
            pic->cmap[i].red = exact_def.red >> 8;
 
68
            pic->cmap[i].green = exact_def.green >> 8;
 
69
            pic->cmap[i].blue = exact_def.blue >> 8;
 
70
        }
 
71
        pic->subtype = T_PIC_XPM;
 
72
        pic->numcols = image.ncolors;
 
73
        pic->pixmap = None;
 
74
        pic->bitmap = (unsigned char *) malloc(image.width*image.height*sizeof(unsigned char));
 
75
        if (pic->bitmap == NULL) {
 
76
            file_msg("cannot allocate space for XPM image");
 
77
            return PicSuccess;
 
78
        }
 
79
        for (i=0; i<image.width*image.height; i++)
 
80
            pic->bitmap[i] = (unsigned char) image.data[i]; /* int to unsigned char */
 
81
        pic->hw_ratio = (float) image.height / image.width;
 
82
        pic->bit_size.x = image.width;
 
83
        pic->size_x = image.width * scale;
 
84
        pic->bit_size.y = image.height;
 
85
        pic->size_y = image.height * scale;
 
86
        XpmFreeXpmImage(&image);        /* get rid of the image */
 
87
        /* if monochrome display map bitmap */
 
88
        if (tool_cells <= 2 || appres.monochrome)
 
89
            map_to_mono(pic);
 
90
 
 
91
        return PicSuccess;
 
92
    }
 
93
    return FileInvalid;
 
94
}